From 85f64db46e1d1bc0b06f0d639f295cd111e8411b Mon Sep 17 00:00:00 2001 From: poma Date: Fri, 17 Jan 2020 01:59:34 +0700 Subject: [PATCH] fix other scripts for updated circom class --- phase2/src/bin/new.rs | 4 +- phase2/src/bin/prove.rs | 106 ++++++++++++++++++++------ phase2/src/bin/verify_contribution.rs | 4 +- phase2/src/circom_circuit.rs | 4 +- phase2/test.sh | 12 ++- 5 files changed, 93 insertions(+), 37 deletions(-) diff --git a/phase2/src/bin/new.rs b/phase2/src/bin/new.rs index 9efd41c..72d800d 100644 --- a/phase2/src/bin/new.rs +++ b/phase2/src/bin/new.rs @@ -20,9 +20,7 @@ fn main() { // Import the circuit and create the initial parameters using phase 1 println!("Creating initial parameters for {}...", circuit_filename); let params = { - let c = CircomCircuit { - file_name: &circuit_filename, - }; + let c = CircomCircuit::from_json_file(&circuit_filename); MPCParameters::new(c, should_filter_points_at_infinity).unwrap() }; diff --git a/phase2/src/bin/prove.rs b/phase2/src/bin/prove.rs index 793e648..e5ac712 100644 --- a/phase2/src/bin/prove.rs +++ b/phase2/src/bin/prove.rs @@ -1,47 +1,58 @@ -#![allow(unused_imports)] - extern crate phase2; extern crate bellman_ce; +extern crate num_bigint; +extern crate num_traits; +extern crate exitcode; +extern crate serde; -use phase2::circom_circuit::CircomCircuit; +use std::fs; use std::fs::OpenOptions; +use num_bigint::BigUint; +use num_traits::Num; +use serde::{Deserialize, Serialize}; use phase2::parameters::MPCParameters; -use bellman_ce::groth16::{Proof, generate_random_parameters, prepare_verifying_key, create_random_proof, verify_proof}; -use std::sync::Arc; - -use bellman_ce::pairing::bn256::{ - Bn256, -}; - +use phase2::circom_circuit::CircomCircuit; +use bellman_ce::groth16::{prepare_verifying_key, create_random_proof, verify_proof}; use bellman_ce::pairing::{ Engine, CurveAffine, ff::{ - Field, PrimeField, }, + bn256::{ + Bn256, + }, }; -use bellman_ce::{ - Circuit, - SynthesisError, - Variable, - Index, - ConstraintSystem, - LinearCombination, -}; +#[derive(Serialize, Deserialize)] +struct ProofJson { + pub protocol: String, + pub pi_a: Vec, + pub pi_b: Vec>, + pub pi_c: Vec, +} fn main() { + let args: Vec = std::env::args().collect(); + if args.len() != 5 { + println!("Usage: \n "); + std::process::exit(exitcode::USAGE); + } + let circuit_filename = &args[1]; + let witness_filename = &args[2]; + let params_filename = &args[3]; + let proof_filename = &args[4]; + let should_filter_points_at_infinity = false; let rng = &mut rand::XorShiftRng::new_unseeded(); // TODO: change this unsafe unseeded random (!) - let mut c = CircomCircuit::from_json("circuit.json"); - c.load_witness_json("witness.json"); + let mut c = CircomCircuit::from_json_file(circuit_filename); + c.load_witness_json_file(witness_filename); let input = c.inputs.to_vec(); let reader = OpenOptions::new() .read(true) - .open("circom4.params") + .open(params_filename) .expect("unable to open."); let mut params = MPCParameters::read(reader, should_filter_points_at_infinity, true).expect("unable to read params"); @@ -60,5 +71,56 @@ fn main() { &input[1..] ).unwrap(); assert!(result, "Proof is correct"); + + let repr_to_big = |r| { + BigUint::from_str_radix(&format!("{}", r)[2..], 16).unwrap().to_str_radix(10) + }; + 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 + }; + 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 + }; + + let proof = ProofJson { + protocol: "groth".to_string(), + pi_a: p1_to_vec(&proof.a), + pi_b: p2_to_vec(&proof.b), + pi_c: p1_to_vec(&proof.c), + }; + + let proof_json = serde_json::to_string(&proof).unwrap(); + fs::write(proof_filename, proof_json.as_bytes()).unwrap(); + println!("Done!") } \ No newline at end of file diff --git a/phase2/src/bin/verify_contribution.rs b/phase2/src/bin/verify_contribution.rs index 3bcf9c9..a11c161 100644 --- a/phase2/src/bin/verify_contribution.rs +++ b/phase2/src/bin/verify_contribution.rs @@ -34,9 +34,7 @@ fn main() { let contribution = verify_contribution(&old_params, &new_params).expect("should verify"); let should_filter_points_at_infinity = false; - let verification_result = new_params.verify(CircomCircuit { - file_name: &circuit_filename, - }, should_filter_points_at_infinity).unwrap(); + let verification_result = new_params.verify(CircomCircuit::from_json_file(&circuit_filename), should_filter_points_at_infinity).unwrap(); assert!(contains_contribution(&verification_result, &contribution)); println!("Contribution {} verified.", new_params_filename); } diff --git a/phase2/src/circom_circuit.rs b/phase2/src/circom_circuit.rs index 0d262ac..699fefd 100644 --- a/phase2/src/circom_circuit.rs +++ b/phase2/src/circom_circuit.rs @@ -49,14 +49,14 @@ pub struct CircomCircuit { } impl<'a, E: Engine> CircomCircuit { - pub fn load_witness_json(&mut self, filename: &str) { + pub fn load_witness_json_file(&mut self, filename: &str) { let witness: Vec = serde_json::from_str(&fs::read_to_string(filename).unwrap()).unwrap(); let witness = witness.into_iter().map(|x| E::Fr::from_str(&x).unwrap()).collect::>(); self.inputs = witness[..self.num_inputs].to_vec(); self.aux = witness[self.num_inputs..].to_vec(); } - pub fn from_json(filename: &str) -> CircomCircuit:: { + pub fn from_json_file(filename: &str) -> CircomCircuit:: { let circuit_json: CircuitJson = serde_json::from_str(&fs::read_to_string(filename).unwrap()).unwrap(); let num_inputs = circuit_json.num_inputs + circuit_json.num_outputs + 1; diff --git a/phase2/test.sh b/phase2/test.sh index 24481ed..70f937e 100755 --- a/phase2/test.sh +++ b/phase2/test.sh @@ -29,13 +29,11 @@ cargo run --release --bin export_keys circom4.params vk.json pk.json # create dummy keys in circom format npx snarkjs setup --protocol groth # patch dummy keys with actual keys params -node patch_vk.js -# generate binary version of proving key -node node_modules/websnark/tools/buildpkey.js -i transformed_pk.json -o transformed_pk.bin +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 # try to generate and verify proof snarkjs calculatewitness -node node_modules/websnark/tools/buildwitness.js -i witness.json -o witness.bin -snarkjs proof # to get public inputs json only -./cli.js -snarkjs verify --vk transformed_vk.json --proof proof.json \ No newline at end of file +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