move circuit and witness load functions outside of circuit struct
This commit is contained in:
parent
27ad5eb7c5
commit
c8f7e68f3a
@ -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<String> = 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()
|
||||
};
|
||||
|
||||
|
@ -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::<Bn256>::witness_from_json_file(witness_filename));
|
||||
let mut circuit = circuit_from_json_file(circuit_filename);
|
||||
circuit.witness = Some(witness_from_json_file::<Bn256>(witness_filename));
|
||||
|
||||
println!("Proving...");
|
||||
let proof = prove(circuit.clone(), ¶ms, rng).unwrap();
|
||||
|
@ -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<String> = 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);
|
||||
}
|
||||
|
@ -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<E: Engine> {
|
||||
}
|
||||
|
||||
impl<'a, E: Engine> CircomCircuit<E> {
|
||||
pub fn from_json_file(filename: &str) -> CircomCircuit::<E> {
|
||||
let reader = OpenOptions::new()
|
||||
.read(true)
|
||||
.open(filename)
|
||||
.expect("unable to open.");
|
||||
return CircomCircuit::from_json(reader);
|
||||
}
|
||||
|
||||
pub fn from_json<R: Read>(reader: R) -> CircomCircuit::<E> {
|
||||
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<String, String>| {
|
||||
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<E::Fr> {
|
||||
let reader = OpenOptions::new()
|
||||
.read(true)
|
||||
.open(filename)
|
||||
.expect("unable to open.");
|
||||
return CircomCircuit::<E>::witness_from_json(reader);
|
||||
}
|
||||
|
||||
pub fn witness_from_json<R: Read>(reader: R) -> Vec<E::Fr>{
|
||||
let witness: Vec<String> = serde_json::from_reader(reader).unwrap();
|
||||
return witness.into_iter().map(|x| E::Fr::from_str(&x).unwrap()).collect::<Vec<E::Fr>>();
|
||||
}
|
||||
|
||||
pub fn get_public_inputs(&self) -> Option<Vec<E::Fr>> {
|
||||
return match self.witness.clone() {
|
||||
None => None,
|
||||
@ -349,6 +305,50 @@ pub fn verification_key_json_file(params: &Parameters<Bn256>, filename: &str) ->
|
||||
return fs::write(filename, str.as_bytes());
|
||||
}
|
||||
|
||||
pub fn witness_from_json_file<E: Engine>(filename: &str) -> Vec<E::Fr> {
|
||||
let reader = OpenOptions::new()
|
||||
.read(true)
|
||||
.open(filename)
|
||||
.expect("unable to open.");
|
||||
return witness_from_json::<E, File>(reader);
|
||||
}
|
||||
|
||||
pub fn witness_from_json<E: Engine, R: Read>(reader: R) -> Vec<E::Fr>{
|
||||
let witness: Vec<String> = serde_json::from_reader(reader).unwrap();
|
||||
return witness.into_iter().map(|x| E::Fr::from_str(&x).unwrap()).collect::<Vec<E::Fr>>();
|
||||
}
|
||||
|
||||
pub fn circuit_from_json_file<E: Engine>(filename: &str) -> CircomCircuit::<E> {
|
||||
let reader = OpenOptions::new()
|
||||
.read(true)
|
||||
.open(filename)
|
||||
.expect("unable to open.");
|
||||
return circuit_from_json(reader);
|
||||
}
|
||||
|
||||
pub fn circuit_from_json<E: Engine, R: Read>(reader: R) -> CircomCircuit::<E> {
|
||||
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<String, String>| {
|
||||
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<dyn Rng> {
|
||||
return Box::new(OsRng::new().unwrap())
|
||||
}
|
Loading…
Reference in New Issue
Block a user