Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]Fix wasm-bindgen #93

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,657 changes: 902 additions & 755 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ zcrypto = { package = "zero-chain-crypto", path = "core/crypto" }
jubjub = { path = "core/jubjub" }
zpairing = { package = "zero-chain-pairing", path = "core/pairing" }
zprimitives = { package = "zero-chain-primitives", path = "core/primitives" }
proofs = { package = "zero-chain-proofs", path = "core/proofs" }
scrypto = { package = "sapling-crypto", git = "https://github.com/LayerXcom/librustzcash", branch = "zero-chain" }
proofs = { package = "zero-chain-proofs", path = "core/proofs", features = ["multithread"] }
scrypto = { package = "sapling-crypto", git = "https://github.com/LayerXcom/librustzcash", branch = "zero-chain", features = ["multithread"] }
keys = { path = "core/keys"}
bellman = { git = "https://github.com/LayerXcom/librustzcash", branch = "zero-chain" }
bellman = { git = "https://github.com/LayerXcom/librustzcash", branch = "zero-chain", features = ["multithread"] }
pairing = { git = "https://github.com/LayerXcom/librustzcash", branch = "zero-chain" }
rand = "0.4"
lazy_static = "1.3.0"
Expand Down Expand Up @@ -113,7 +113,7 @@ path = 'demo/cli/src/main.rs'
members = [
"core/bellman-verifier",
"core/crypto",
"demo/cli",
"demo/cli",
"core/jubjub",
"core/pairing",
"core/primitives",
Expand All @@ -124,6 +124,6 @@ members = [
"modules/executive",
]
exclude = [
"runtime/wasm",
"demo/wasm-utils",
"runtime/wasm",
"demo/wasm-utils",
]
7 changes: 7 additions & 0 deletions core/proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ hex-literal = '0.1'
[dependencies.blake2-rfc]
git = "https://github.com/gtank/blake2-rfc"
rev = "7a5b5fc99ae483a0043db7547fb79a6fa44b88a9"

[features]
default = []
multithread = [
"bellman/multithread",
"scrypto/multithread",
]
140 changes: 70 additions & 70 deletions core/proofs/src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
use bellman::groth16::{
create_random_proof,
verify_proof,
Parameters,
PreparedVerifyingKey,
Proof,
create_random_proof,
verify_proof,
Parameters,
PreparedVerifyingKey,
Proof,
};
use pairing::Field;
use rand::{Rand, Rng};
use scrypto::{
use scrypto::{
jubjub::{
JubjubEngine,
FixedGenerators,
},
redjubjub::{
PublicKey,
JubjubEngine,
FixedGenerators,
},
redjubjub::{
PublicKey,
},
};
use crate::circuit_transfer::Transfer;
use crate::primitives::{
EncryptionKey,
ProofGenerationKey,
use crate::primitives::{
EncryptionKey,
ProofGenerationKey,
};
use crate::elgamal::Ciphertext;

pub struct TransferProof<E: JubjubEngine> {
pub proof: Proof<E>,
pub rvk: PublicKey<E>, // re-randomization sig-verifying key
pub address_sender: EncryptionKey<E>,
pub rvk: PublicKey<E>, // re-randomization sig-verifying key
pub address_sender: EncryptionKey<E>,
pub address_recipient: EncryptionKey<E>,
pub cipher_val_s: Ciphertext<E>,
pub cipher_val_r: Ciphertext<E>,
pub cipher_balance: Ciphertext<E>,
pub cipher_val_r: Ciphertext<E>,
pub cipher_balance: Ciphertext<E>,
}

impl<E: JubjubEngine> TransferProof<E> {
pub fn gen_proof<R: Rng>(
value: u32,
remaining_balance: u32,
alpha: E::Fs,
proving_key: &Parameters<E>,
impl<E: JubjubEngine> TransferProof<E> {
pub fn gen_proof<R: Rng>(
value: u32,
remaining_balance: u32,
alpha: E::Fs,
proving_key: &Parameters<E>,
prepared_vk: &PreparedVerifyingKey<E>,
proof_generation_key: ProofGenerationKey<E>,
address_recipient: EncryptionKey<E>,
address_recipient: EncryptionKey<E>,
ciphertext_balance: Ciphertext<E>,
rng: &mut R,
params: &E::Params,
rng: &mut R,
params: &E::Params,
) -> Result<Self, &'static str>
{
let randomness = E::Fs::rand(rng);
let randomness = E::Fs::rand(rng);

let bdk = proof_generation_key.bdk();
let ek_sender = proof_generation_key.into_encryption_key(params);
let ek_sender = proof_generation_key.into_encryption_key(params);

let rvk = PublicKey(proof_generation_key.0.clone().into())
.randomize(
alpha,
FixedGenerators::NoteCommitmentRandomness,
params,
);
);

let instance = Transfer {
params: params,
Expand All @@ -68,27 +68,27 @@ impl<E: JubjubEngine> TransferProof<E> {
proof_generation_key: Some(proof_generation_key.clone()),
decryption_key: Some(bdk.clone()),
pk_d_recipient: Some(address_recipient.0.clone()),
encrypted_balance: Some(ciphertext_balance.clone())
encrypted_balance: Some(ciphertext_balance.clone())
};

// Crate proof
let proof = create_random_proof(instance, proving_key, rng)
.expect("proving should not fail");

let mut public_input = [E::Fr::zero(); 16];

let cipher_val_s = Ciphertext::encrypt(
value,
randomness,
&ek_sender.0,
value,
randomness,
&ek_sender.0,
FixedGenerators::NoteCommitmentRandomness,
params
);

let cipher_val_r = Ciphertext::encrypt(
value,
randomness,
&address_recipient.0,
value,
randomness,
&address_recipient.0,
FixedGenerators::NoteCommitmentRandomness,
params
);
Expand Down Expand Up @@ -121,7 +121,7 @@ impl<E: JubjubEngine> TransferProof<E> {
{
let (x, y) = ciphertext_balance.left.into_xy();
public_input[10] = x;
public_input[11] = y;
public_input[11] = y;
}
{
let (x, y) = ciphertext_balance.right.into_xy();
Expand All @@ -132,24 +132,24 @@ impl<E: JubjubEngine> TransferProof<E> {
let (x, y) = rvk.0.into_xy();
public_input[14] = x;
public_input[15] = y;
}
}

if let Err(_) = verify_proof(prepared_vk, &proof, &public_input[..]) {
return Err("Invalid zk proof")
}
}

let transfer_proof = TransferProof {
proof: proof,
rvk: rvk,
address_sender: ek_sender,
address_recipient: address_recipient,
proof: proof,
rvk: rvk,
address_sender: ek_sender,
address_recipient: address_recipient,
cipher_val_s: cipher_val_s,
cipher_val_r: cipher_val_r,
cipher_balance: ciphertext_balance,
};

Ok(transfer_proof)
}
}
}

#[cfg(test)]
Expand All @@ -163,11 +163,11 @@ mod tests {
use std::path::Path;
use std::fs::File;
use std::io::{BufReader, Read};
use hex_literal::{hex, hex_impl};
use hex_literal::{hex, hex_impl};

fn get_pk_and_vk() -> (Parameters<Bls12>, PreparedVerifyingKey<Bls12>) {
let pk_path = Path::new("../../demo/cli/proving.params");
let vk_path = Path::new("../../demo/cli/verification.params");
let pk_path = Path::new("../../demo/cli/proving.params");
let vk_path = Path::new("../../demo/cli/verification.params");

let pk_file = File::open(&pk_path).unwrap();
let vk_file = File::open(&vk_path).unwrap();
Expand All @@ -181,67 +181,67 @@ mod tests {
let mut buf_vk = vec![];
vk_reader.read_to_end(&mut buf_vk).unwrap();

let proving_key = Parameters::<Bls12>::read(&mut &buf_pk[..], true).unwrap();
let proving_key = Parameters::<Bls12>::read(&mut &buf_pk[..], true).unwrap();
let prepared_vk = PreparedVerifyingKey::<Bls12>::read(&mut &buf_vk[..]).unwrap();

(proving_key, prepared_vk)
}

#[test]
fn test_gen_proof() {
fn test_gen_proof() {
let params = &JubjubBls12::new();
let mut rng = &mut XorShiftRng::from_seed([0x5dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let p_g = FixedGenerators::NoteCommitmentRandomness;

let value = 10 as u32;
let remaining_balance = 30 as u32;
let balance = 100 as u32;
let alpha = fs::Fs::rand(rng);
let alpha = fs::Fs::rand(rng);

let sender_ok = fs::Fs::rand(rng);
let recipient_ok = fs::Fs::rand(rng);
let recipient_ok = fs::Fs::rand(rng);

let proof_generation_key = ProofGenerationKey::<Bls12>::from_origin_key(&sender_ok, params);

let pgk_sender = ProofGenerationKey::<Bls12>::from_origin_key(&sender_ok, params);
let ek_recipient = EncryptionKey::<Bls12>::from_origin_key(&recipient_ok, params);
let ek_recipient = EncryptionKey::<Bls12>::from_origin_key(&recipient_ok, params);
let bdk = pgk_sender.bdk();

let r_fs = fs::Fs::rand(rng);
let public_key = params.generator(p_g).mul(bdk, params).into();
let ciphertext_balance = Ciphertext::encrypt(balance, r_fs, &public_key, p_g, params);

let (proving_key, prepared_vk) = get_pk_and_vk();
let (proving_key, prepared_vk) = get_pk_and_vk();

let proofs = TransferProof::gen_proof(
value,
remaining_balance,
alpha,
&proving_key,
&prepared_vk,
proof_generation_key,
ek_recipient,
ciphertext_balance,
value,
remaining_balance,
alpha,
&proving_key,
&prepared_vk,
proof_generation_key,
ek_recipient,
ciphertext_balance,
&mut rng,
params
);

assert!(proofs.is_ok());
}
}

#[test]
fn test_read_proving_key() {
let pk_path = Path::new("../../demo/cli/proving.params");
let pk_path = Path::new("../../demo/cli/proving.params");

let pk_file = File::open(&pk_path).unwrap();
let pk_file = File::open(&pk_path).unwrap();

let mut pk_reader = BufReader::new(pk_file);
let mut pk_reader = BufReader::new(pk_file);
println!("{:?}", pk_reader);
let mut buf = vec![];

pk_reader.read_to_end(&mut buf).unwrap();
println!("{:?}", buf.len());

let _proving_key = Parameters::<Bls12>::read(&mut &buf[..], true).unwrap();
}
}
7 changes: 3 additions & 4 deletions demo/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ edition = "2018"

[dependencies]
rand = "0.4"
proofs = { package = "zero-chain-proofs", path = "../../core/proofs" }
proofs = { package = "zero-chain-proofs", path = "../../core/proofs", features = ["multithread"] }
clap = "~2.32"
primitives = { package = "substrate-primitives", git = "https://github.com/paritytech/substrate", rev = '9112a28af677b75cd6f935dda34d283bb4f676e7' }
zpairing = { package = "zero-chain-pairing", path = "../../core/pairing" }
pairing = { git = "https://github.com/LayerXcom/librustzcash", branch = "zero-chain" }
bellman = { git = "https://github.com/LayerXcom/librustzcash", branch = "zero-chain" }
scrypto = { package = "sapling-crypto", git = "https://github.com/LayerXcom/librustzcash", branch = "zero-chain" }
bellman = { git = "https://github.com/LayerXcom/librustzcash", branch = "zero-chain", features = ["multithread"] }
scrypto = { package = "sapling-crypto", git = "https://github.com/LayerXcom/librustzcash", branch = "zero-chain", features = ["multithread"] }
bellman-verifier = { path = "../../core/bellman-verifier"}
zcrypto = { package = "zero-chain-crypto", path = "../../core/crypto"}
zprimitives = { package = "zero-chain-primitives", path = "../../core/primitives" }
zjubjub = { package = "jubjub", path = "../../core/jubjub"}
lazy_static = "1.3.0"
byteorder = "1"
keys = { path = "../../core/keys" }
wasm-utils = {package = "zerochain-wasm-utils", path = "../wasm-utils"}
hex = "0.3.2"

[dependencies.blake2-rfc]
Expand Down
Binary file modified demo/cli/proving.params
Binary file not shown.
Loading