From ee3715148358c9f5c6c12216476d737178c14941 Mon Sep 17 00:00:00 2001 From: poma Date: Sat, 18 Jan 2020 21:02:47 +0700 Subject: [PATCH] export alphabeta_12 pairing to verifying key --- phase2/src/bin/copy_json.rs | 4 -- phase2/src/bin/export_keys.rs | 87 ++++++++++++++++++++++------------- phase2/test.sh | 3 +- 3 files changed, 56 insertions(+), 38 deletions(-) diff --git a/phase2/src/bin/copy_json.rs b/phase2/src/bin/copy_json.rs index 71b11d0..be9a2a7 100644 --- a/phase2/src/bin/copy_json.rs +++ b/phase2/src/bin/copy_json.rs @@ -21,10 +21,6 @@ fn main() { reference_json[key] = value.clone(); } - if reference_json.contains_key("vk_alfabeta_12") { - reference_json.remove("vk_alfabeta_12").unwrap(); - } - fs::write(out_file, serde_json::to_string(&reference_json).unwrap().as_bytes()).unwrap(); println!("Done"); } diff --git a/phase2/src/bin/export_keys.rs b/phase2/src/bin/export_keys.rs index 9d0e665..50f7926 100644 --- a/phase2/src/bin/export_keys.rs +++ b/phase2/src/bin/export_keys.rs @@ -49,6 +49,7 @@ struct VerifyingKeyJson { pub vk_beta_2: Vec>, pub vk_gamma_2: Vec>, pub vk_delta_2: Vec>, + pub vk_alfabeta_12: Vec>>, } fn main() { @@ -89,40 +90,60 @@ fn main() { }; let p1_to_vec = |p : &::G1Affine| { - let mut v = vec![]; - //println!("test: {}", p.get_x().into_repr()); - let x = repr_to_big(p.get_x().into_repr()); - v.push(x); - let y = repr_to_big(p.get_y().into_repr()); - v.push(y); - if p.is_zero() { - v.push("0".to_string()); - } else { - v.push("1".to_string()); - } - v + vec![ + repr_to_big(p.get_x().into_repr()), + repr_to_big(p.get_y().into_repr()), + if p.is_zero() { "0".to_string() } else { "1".to_string() } + ] }; let p2_to_vec = |p : &::G2Affine| { - let mut v = vec![]; - let x = p.get_x(); - let mut x_v = vec![]; - x_v.push(repr_to_big(x.c0.into_repr())); - x_v.push(repr_to_big(x.c1.into_repr())); - v.push(x_v); - - let y = p.get_y(); - let mut y_v = vec![]; - y_v.push(repr_to_big(y.c0.into_repr())); - y_v.push(repr_to_big(y.c1.into_repr())); - v.push(y_v); - - if p.is_zero() { - v.push(["0".to_string(), "0".to_string()].to_vec()); - } else { - v.push(["1".to_string(), "0".to_string()].to_vec()); - } - - v + vec![ + vec![ + repr_to_big(p.get_x().c0.into_repr()), + repr_to_big(p.get_x().c1.into_repr()), + ], + vec![ + repr_to_big(p.get_y().c0.into_repr()), + repr_to_big(p.get_y().c1.into_repr()), + ], + if p.is_zero() { + vec!["0".to_string(), "0".to_string()] + } else { + vec!["1".to_string(), "0".to_string()] + } + ] + }; + let pairing_to_vec = |p : bellman_ce::pairing::bn256::Fq12| { + vec![ + vec![ + vec![ + repr_to_big(p.c0.c0.c0.into_repr()), + repr_to_big(p.c0.c0.c1.into_repr()), + ], + vec![ + repr_to_big(p.c0.c1.c0.into_repr()), + repr_to_big(p.c0.c1.c1.into_repr()), + ], + vec![ + repr_to_big(p.c0.c2.c0.into_repr()), + repr_to_big(p.c0.c2.c1.into_repr()), + ] + ], + vec![ + vec![ + repr_to_big(p.c1.c0.c0.into_repr()), + repr_to_big(p.c1.c0.c1.into_repr()), + ], + vec![ + repr_to_big(p.c1.c1.c0.into_repr()), + repr_to_big(p.c1.c1.c1.into_repr()), + ], + vec![ + repr_to_big(p.c1.c2.c0.into_repr()), + repr_to_big(p.c1.c2.c1.into_repr()), + ] + ], + ] }; let a = params.a.clone(); for e in a.iter() { @@ -170,6 +191,7 @@ fn main() { vk_beta_2: vec![], vk_gamma_2: vec![], vk_delta_2: vec![], + vk_alfabeta_12: vec![], }; let ic = params.vk.ic.clone(); @@ -182,6 +204,7 @@ fn main() { let vk_gamma_2 = params.vk.gamma_g2.clone(); verification_key.vk_gamma_2 = p2_to_vec(&vk_gamma_2); verification_key.vk_delta_2 = p2_to_vec(&vk_delta_2); + verification_key.vk_alfabeta_12 = pairing_to_vec(Bn256::pairing(vk_alfa_1, vk_beta_2)); let pk_json = serde_json::to_string(&proving_key).unwrap(); fs::write(pk_filename, pk_json.as_bytes()).unwrap(); diff --git a/phase2/test.sh b/phase2/test.sh index f24b886..5d8e35b 100755 --- a/phase2/test.sh +++ b/phase2/test.sh @@ -30,7 +30,6 @@ npx snarkjs setup --protocol groth # patch dummy keys with actual keys params cargo run --release --bin copy_json proving_key.json pk.json transformed_pk.json cargo run --release --bin copy_json verification_key.json vk.json transformed_vk.json -node ./tools/patch_vk/patch_vk.js # generate solidity verifier cargo run --release --bin generate_verifier circom4.params verifier.sol @@ -38,4 +37,4 @@ cargo run --release --bin generate_verifier circom4.params verifier.sol # try to generate and verify proof snarkjs calculatewitness cargo run --release --bin prove circuit.json witness.json circom4.params proof.json -snarkjs verify --vk patched_transformed_vk.json --proof proof.json \ No newline at end of file +snarkjs verify --vk transformed_vk.json --proof proof.json \ No newline at end of file