fix other scripts for updated circom class
This commit is contained in:
parent
d770eb36c6
commit
85f64db46e
@ -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()
|
||||
};
|
||||
|
||||
|
@ -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<String>,
|
||||
pub pi_b: Vec<Vec<String>>,
|
||||
pub pi_c: Vec<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() != 5 {
|
||||
println!("Usage: \n<circuit.json> <witness.json> <params> <proof.json>");
|
||||
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 : &<Bn256 as Engine>::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 : &<Bn256 as Engine>::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!")
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -49,14 +49,14 @@ pub struct CircomCircuit<E: Engine> {
|
||||
}
|
||||
|
||||
impl<'a, E: Engine> CircomCircuit<E> {
|
||||
pub fn load_witness_json(&mut self, filename: &str) {
|
||||
pub fn load_witness_json_file(&mut self, filename: &str) {
|
||||
let witness: Vec<String> = 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::<Vec<E::Fr>>();
|
||||
self.inputs = witness[..self.num_inputs].to_vec();
|
||||
self.aux = witness[self.num_inputs..].to_vec();
|
||||
}
|
||||
|
||||
pub fn from_json(filename: &str) -> CircomCircuit::<E> {
|
||||
pub fn from_json_file(filename: &str) -> CircomCircuit::<E> {
|
||||
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;
|
||||
|
@ -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
|
||||
cargo run --release --bin prove circuit.json witness.json circom4.params proof.json
|
||||
snarkjs verify --vk patched_transformed_vk.json --proof proof.json
|
Loading…
Reference in New Issue
Block a user