diff --git a/phase2/src/bin/new.rs b/phase2/src/bin/new.rs index 72d800d..e6b0244 100644 --- a/phase2/src/bin/new.rs +++ b/phase2/src/bin/new.rs @@ -4,7 +4,7 @@ extern crate exitcode; use std::fs::File; use phase2::parameters::MPCParameters; -use phase2::circom_circuit::CircomCircuit; +use phase2::circom_circuit::circuit_from_json_file; fn main() { let args: Vec = std::env::args().collect(); @@ -20,7 +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::from_json_file(&circuit_filename); + let c = circuit_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 48cd492..f627bdf 100644 --- a/phase2/src/bin/prove.rs +++ b/phase2/src/bin/prove.rs @@ -9,12 +9,13 @@ extern crate itertools; use std::fs; use bellman_ce::pairing::bn256::Bn256; use phase2::circom_circuit::{ - CircomCircuit, load_params_file, prove, verify, create_rng, - proof_to_json_file + proof_to_json_file, + circuit_from_json_file, + witness_from_json_file }; fn main() { @@ -31,8 +32,8 @@ fn main() { let rng = create_rng(); let params = load_params_file(params_filename); - let mut circuit = CircomCircuit::from_json_file(circuit_filename); - circuit.witness = Some(CircomCircuit::::witness_from_json_file(witness_filename)); + let mut circuit = circuit_from_json_file(circuit_filename); + circuit.witness = Some(witness_from_json_file::(witness_filename)); println!("Proving..."); let proof = prove(circuit.clone(), ¶ms, rng).unwrap(); diff --git a/phase2/src/bin/verify_contribution.rs b/phase2/src/bin/verify_contribution.rs index a11c161..838c42e 100644 --- a/phase2/src/bin/verify_contribution.rs +++ b/phase2/src/bin/verify_contribution.rs @@ -4,7 +4,7 @@ extern crate exitcode; use std::fs::OpenOptions; use phase2::parameters::*; -use phase2::circom_circuit::CircomCircuit; +use phase2::circom_circuit::circuit_from_json_file; fn main() { let args: Vec = std::env::args().collect(); @@ -34,7 +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::from_json_file(&circuit_filename), should_filter_points_at_infinity).unwrap(); + let verification_result = new_params.verify(circuit_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 146eb9e..5519f65 100644 --- a/phase2/src/circom_circuit.rs +++ b/phase2/src/circom_circuit.rs @@ -3,7 +3,7 @@ extern crate rand; use std::str; use std::fs; -use std::fs::OpenOptions; +use std::fs::{OpenOptions, File}; use std::io::{Read, Write}; use std::collections::BTreeMap; use std::iter::repeat; @@ -110,50 +110,6 @@ pub struct CircomCircuit { } impl<'a, E: Engine> CircomCircuit { - pub fn from_json_file(filename: &str) -> CircomCircuit:: { - let reader = OpenOptions::new() - .read(true) - .open(filename) - .expect("unable to open."); - return CircomCircuit::from_json(reader); - } - - pub fn from_json(reader: R) -> CircomCircuit:: { - let circuit_json: CircuitJson = serde_json::from_reader(reader).unwrap(); - - let num_inputs = circuit_json.num_inputs + circuit_json.num_outputs + 1; - let num_aux = circuit_json.num_variables - num_inputs; - - let convert_constraint = |lc: &BTreeMap| { - lc.iter().map(|(index, coeff)| (index.parse().unwrap(), E::Fr::from_str(coeff).unwrap())).collect_vec() - }; - - let constraints = circuit_json.constraints.iter().map( - |c| (convert_constraint(&c[0]), convert_constraint(&c[1]), convert_constraint(&c[2])) - ).collect_vec(); - - return CircomCircuit { - num_inputs: num_inputs, - num_aux: num_aux, - num_constraints: circuit_json.num_variables, - witness: None, - constraints: constraints, - }; - } - - pub fn witness_from_json_file(filename: &str) -> Vec { - let reader = OpenOptions::new() - .read(true) - .open(filename) - .expect("unable to open."); - return CircomCircuit::::witness_from_json(reader); - } - - pub fn witness_from_json(reader: R) -> Vec{ - let witness: Vec = serde_json::from_reader(reader).unwrap(); - return witness.into_iter().map(|x| E::Fr::from_str(&x).unwrap()).collect::>(); - } - pub fn get_public_inputs(&self) -> Option> { return match self.witness.clone() { None => None, @@ -349,6 +305,50 @@ pub fn verification_key_json_file(params: &Parameters, filename: &str) -> return fs::write(filename, str.as_bytes()); } +pub fn witness_from_json_file(filename: &str) -> Vec { + let reader = OpenOptions::new() + .read(true) + .open(filename) + .expect("unable to open."); + return witness_from_json::(reader); +} + +pub fn witness_from_json(reader: R) -> Vec{ + let witness: Vec = serde_json::from_reader(reader).unwrap(); + return witness.into_iter().map(|x| E::Fr::from_str(&x).unwrap()).collect::>(); +} + +pub fn circuit_from_json_file(filename: &str) -> CircomCircuit:: { + let reader = OpenOptions::new() + .read(true) + .open(filename) + .expect("unable to open."); + return circuit_from_json(reader); +} + +pub fn circuit_from_json(reader: R) -> CircomCircuit:: { + let circuit_json: CircuitJson = serde_json::from_reader(reader).unwrap(); + + let num_inputs = circuit_json.num_inputs + circuit_json.num_outputs + 1; + let num_aux = circuit_json.num_variables - num_inputs; + + let convert_constraint = |lc: &BTreeMap| { + lc.iter().map(|(index, coeff)| (index.parse().unwrap(), E::Fr::from_str(coeff).unwrap())).collect_vec() + }; + + let constraints = circuit_json.constraints.iter().map( + |c| (convert_constraint(&c[0]), convert_constraint(&c[1]), convert_constraint(&c[2])) + ).collect_vec(); + + return CircomCircuit { + num_inputs: num_inputs, + num_aux: num_aux, + num_constraints: circuit_json.num_variables, + witness: None, + constraints: constraints, + }; +} + pub fn create_rng() -> Box { return Box::new(OsRng::new().unwrap()) } \ No newline at end of file