Skip to content

Commit

Permalink
Merge pull request #97 from LayerXcom/feature/sonic
Browse files Browse the repository at this point in the history
Implement SONIC outline
  • Loading branch information
osuketh authored Apr 18, 2019
2 parents 4fc2d06 + 89a93c2 commit 0e45937
Show file tree
Hide file tree
Showing 45 changed files with 4,302 additions and 2,414 deletions.
2,305 changes: 1,110 additions & 1,195 deletions Cargo.lock

Large diffs are not rendered by default.

36 changes: 16 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ clap = "~2.32"
[dependencies.basic-authorship]
git = 'https://github.com/paritytech/substrate.git'
package = 'substrate-basic-authorship'
rev = '9112a28af677b75cd6f935dda34d283bb4f676e7'
rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7'

[dependencies.consensus]
git = 'https://github.com/paritytech/substrate.git'
package = 'substrate-consensus-aura'
rev = '9112a28af677b75cd6f935dda34d283bb4f676e7'
rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7'

[dependencies.ctrlc]
features = ['termination']
Expand All @@ -50,56 +50,52 @@ version = '3.0'
[dependencies.inherents]
git = 'https://github.com/paritytech/substrate.git'
package = 'substrate-inherents'
rev = '9112a28af677b75cd6f935dda34d283bb4f676e7'
rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7'

[dependencies.network]
git = 'https://github.com/paritytech/substrate.git'
package = 'substrate-network'
rev = '9112a28af677b75cd6f935dda34d283bb4f676e7'

[dependencies.node-executor]
git = 'https://github.com/paritytech/substrate.git'
rev = '9112a28af677b75cd6f935dda34d283bb4f676e7'
rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7'

[dependencies.zero-chain-runtime]
path = 'runtime'

[dependencies.primitives]
git = 'https://github.com/paritytech/substrate.git'
package = 'substrate-primitives'
rev = '9112a28af677b75cd6f935dda34d283bb4f676e7'
rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7'

[dependencies.sr-io]
git = 'https://github.com/paritytech/substrate.git'
rev = '9112a28af677b75cd6f935dda34d283bb4f676e7'
rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7'

[dependencies.substrate-cli]
git = 'https://github.com/paritytech/substrate.git'
rev = '9112a28af677b75cd6f935dda34d283bb4f676e7'
rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7'

[dependencies.substrate-client]
git = 'https://github.com/paritytech/substrate.git'
rev = '9112a28af677b75cd6f935dda34d283bb4f676e7'
rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7'

[dependencies.substrate-executor]
git = 'https://github.com/paritytech/substrate.git'
rev = '9112a28af677b75cd6f935dda34d283bb4f676e7'
rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7'

[dependencies.substrate-service]
git = 'https://github.com/paritytech/substrate.git'
rev = '9112a28af677b75cd6f935dda34d283bb4f676e7'
rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7'

[dependencies.transaction-pool]
git = 'https://github.com/paritytech/substrate.git'
package = 'substrate-transaction-pool'
rev = '9112a28af677b75cd6f935dda34d283bb4f676e7'
rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7'

[package]
authors = ['Osuke Sudo<[email protected]>']
build = 'build.rs'
edition = '2018'
name = 'zero-chain'
version = '0.9.0'
version = '1.0.0'

[[bin]]
name = 'zero-chain'
Expand All @@ -113,17 +109,17 @@ path = 'demo/cli/src/main.rs'
members = [
"core/bellman-verifier",
"core/crypto",
"demo/cli",
"demo/cli",
"core/jubjub",
"core/pairing",
"core/primitives",
"core/proofs",
"runtime",
"core/keys",
"modules/indices",
"core/sonic",
"modules/executive",
]
exclude = [
"runtime/wasm",
"demo/wasm-utils",
"runtime/wasm",
"demo/wasm-utils",
]
14 changes: 7 additions & 7 deletions core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jubjub = { path = "../jubjub", default-features = false }
zcrypto = { package = "zero-chain-crypto", path = "../crypto", default-features = false }
rand = { version = "0.4", default-features = false }
fixed-hash = { version = "0.3.0", default-features = false }
substrate-primitives = { git = "https://github.com/paritytech/substrate", rev = '9112a28af677b75cd6f935dda34d283bb4f676e7', default-features = false }
runtime-primitives = { package = "sr-primitives", git = "https://github.com/paritytech/substrate", rev = '9112a28af677b75cd6f935dda34d283bb4f676e7', default-features = false }
substrate-primitives = { git = "https://github.com/paritytech/substrate", rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7', default-features = false }
runtime-primitives = { package = "sr-primitives", git = "https://github.com/paritytech/substrate", rev = '59d5ec144704cec017a01f3e4dbb6b73c5542bf7', default-features = false }
keys = { path = "../keys", default-features = false }
hex-literal = '0.1'

Expand All @@ -32,19 +32,19 @@ features = ["spin_no_std"]

[features]
default = ["std"]
std = [
"parity-codec/std",
std = [
"parity-codec/std",
"parity-codec-derive/std",
"serde",
"serde/std",
"serde_derive",
"byteorder/std",
"byteorder/std",
"pairing/std",
"bellman-verifier/std",
"bellman-verifier/std",
"jubjub/std",
"zcrypto/std",
"rand/std",
"blake2-rfc/std",
"blake2-rfc/std",
"fixed-hash/std",
"substrate-primitives/std",
"runtime-primitives/std",
Expand Down
1 change: 0 additions & 1 deletion core/primitives/src/sig_vk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ impl AsBytesRef for SigVerificationKey {
}
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
38 changes: 0 additions & 38 deletions core/primitives/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ use substrate_primitives::bytes;

use parity_codec::{Encode, Decode, Input};


// #[derive(Eq, PartialEq, Clone, Default, Encode, Decode)]
// #[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
// pub struct RedjubjubSignature(H512);

const SIZE: usize = 64;

construct_fixed_hash! {
Expand Down Expand Up @@ -75,12 +70,6 @@ impl Verify for RedjubjubSignature {

let p_g = FixedGenerators::Diversifier;

// Compute the signature's message for rk/auth_sig
// let mut data_to_be_signed = [0u8; 64];
// rk.0.write(&mut data_to_be_signed[0..32])
// .expect("message buffer should be 32 bytes");
// (&mut data_to_be_signed[32..64]).copy_from_slice(&sighash_value[..]);

match signer.into_verification_key() {
Some(vk) => return vk.verify(msg.get(), &sig, p_g, &JUBJUB),
None => return false
Expand All @@ -89,33 +78,6 @@ impl Verify for RedjubjubSignature {
}
}

// pub fn verify_auth_sig (
// rk: PublicKey<Bls12>,
// auth_sig: RedjubjubSignature,
// sighash_value: &[u8; 32],
// params: &JubjubBls12,
// ) -> bool {
// // Compute the signature's message for rk/auth_sig
// let mut data_to_be_signed = [0u8; 64];
// rk.0.write(&mut data_to_be_signed[0..32])
// .expect("message buffer should be 32 bytes");
// (&mut data_to_be_signed[32..64]).copy_from_slice(&sighash_value[..]);

// // Verify the auth_sig
// rk.verify(
// &data_to_be_signed,
// &auth_sig,
// FixedGenerators::SpendingKeyGenerator,
// &params,
// )
// }

// impl From<H512> for RedjubjubSignature {
// fn from(h: H512) -> RedjubjubSignature {
// RedjubjubSignature(h)
// }
// }

impl RedjubjubSignature {
pub fn into_signature(&self) -> Option<redjubjub::Signature> {
redjubjub::Signature::read(&self.0[..]).ok()
Expand Down
12 changes: 12 additions & 0 deletions core/sonic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "sonic"
version = "0.1.0"
authors = ["Osuke Sudo <[email protected]>"]
edition = "2018"

[dependencies]
pairing = { git = "https://github.com/LayerXcom/librustzcash", branch = "zero-chain" }
bellman = { git = "https://github.com/LayerXcom/librustzcash", branch = "zero-chain" }
rand = "0.4"
merlin = "1.0"
futures = "0.1"
160 changes: 160 additions & 0 deletions core/sonic/src/cs/lc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
//! This module contains some type difinitions like `LinearCombination`, `Variable`, and `Coeff`
//! and implementation of some operator overloadings for those types.
use pairing::{Engine, Field};
use std::ops::{Add, Sub, Neg};


/// This represents a linear combination of some variables, with coefficients
/// in the scalar field of a pairing-friendly elliptic curve group.
#[derive(Clone)]
pub struct LinearCombination<E: Engine>(Vec<(Variable, Coeff<E>)>);

impl<E: Engine> From<Variable> for LinearCombination<E> {
fn from(var: Variable) -> LinearCombination<E> {
LinearCombination::<E>::zero() + var
}
}

impl<E: Engine> AsRef<[(Variable, Coeff<E>)]> for LinearCombination<E> {
fn as_ref(&self) -> &[(Variable, Coeff<E>)] {
&self.0
}
}

/// Return an empty linear combination
impl<E: Engine> LinearCombination<E> {
pub fn zero() -> LinearCombination<E> {
LinearCombination(vec![])
}
}

/// Operetor overloading for linear combination
/// `LinearCombination` + `(Coeff, Variable)` = `LinearCombination`
impl<E: Engine> Add<(Coeff<E>, Variable)> for LinearCombination<E> {
type Output = LinearCombination<E>;

fn add(mut self, (coeff, var): (Coeff<E>, Variable)) -> LinearCombination<E> {
self.0.push((var, coeff));
self
}
}

/// Operetor overloading for linear combination
/// `LinearCombination` - `(Coeff, Variable)` = `LinearCombination`
impl<E: Engine> Sub<(Coeff<E>, Variable)> for LinearCombination<E> {
type Output = LinearCombination<E>;

fn sub(self, (coeff, var): (Coeff<E>, Variable)) -> LinearCombination<E> {
self + (-coeff, var)
}
}

/// Operetor overloading for linear combination
/// `LinearCombination` + `(Coeff::One, Variable)` = `LinearCombination`
impl<E: Engine> Add<Variable> for LinearCombination<E> {
type Output = LinearCombination<E>;

fn add(self, var: Variable) -> LinearCombination<E> {
self + (Coeff::One, var)
}
}

/// Operetor overloading for linear combination
/// `LinearCombination` - `(Coeff::one, Variable) = `LinearCombination`
impl<E: Engine> Sub<Variable> for LinearCombination<E> {
type Output = LinearCombination<E>;

fn sub(self, var: Variable) -> LinearCombination<E> {
self - (Coeff::One, var)
}
}

/// Operetor overloading for linear combination
/// `LinearCombination` + `&LinearCombination` = `LinearCombination`
impl<'a, E: Engine> Add<&'a LinearCombination<E>> for LinearCombination<E> {
type Output = LinearCombination<E>;

fn add(mut self, other: &'a LinearCombination<E>) -> LinearCombination<E> {
for s in &other.0 {
// `LinearCombination` = `LinearCombination` + `(Coeff, Variable)`
self = self + (s.1, s.0);
}

self
}
}

/// Operetor overloading for linear combination
/// `LinearCombination` - `&LinearCombination` = `LinearCombination`
impl<'a, E: Engine> Sub<&'a LinearCombination<E>> for LinearCombination<E> {
type Output = LinearCombination<E>;

fn sub(mut self, other: &'a LinearCombination<E>) -> LinearCombination<E> {
for s in &other.0 {
// `LinearCombination` = `LinearCombination` - `(Coeff, Variable)`
self = self - (s.1, s.0);
}

self
}
}

/// A difinition of Variable for linear combination used in our constraint system.
#[derive(Copy, Clone, Debug)]
pub enum Variable {
A(usize), // input variable in r1cs
B(usize), // Auxillary variable in r1cs
C(usize),
}

/// A difinition of Coefficient for linear combination used in our constraint system.
#[derive(Debug)]
pub enum Coeff<E: Engine> {
Zero,
One,
NegativeOne,
Full(E::Fr),
}

impl<E: Engine> Copy for Coeff<E> {}
impl<E: Engine> Clone for Coeff<E> {
fn clone(&self) -> Self {
*self
}
}

/// Multiply the coefficient with a given variable.
impl<E: Engine> Coeff<E> {
pub fn multiply(&self, with: &mut E::Fr) {
match self {
Coeff::Zero => {
*with = E::Fr::zero();
},
Coeff::One => {},
Coeff::NegativeOne => {
with.negate();
},
Coeff::Full(val) => {
with.mul_assign(val);
}
}
}
}

/// Operetor overloading for Coefficient
/// used for Substraction overloading for linear combination
impl<E: Engine> Neg for Coeff<E> {
type Output = Coeff<E>;

fn neg(self) -> Self {
match self {
Coeff::Zero => Coeff::Zero,
Coeff::One => Coeff::NegativeOne,
Coeff::NegativeOne => Coeff::One,
Coeff::Full(mut a) => {
a.negate();
Coeff::Full(a)
}
}
}
}
Loading

0 comments on commit 0e45937

Please sign in to comment.