From 32746d07481b5e9511ec8fcdc70ce0556422c3ff Mon Sep 17 00:00:00 2001 From: ada Date: Wed, 10 Feb 2021 21:51:54 +0100 Subject: [PATCH] adding verify on lisp --- lisp/inverse.lisp | 34 ++++++++++++++++++++++++++++++++++ lisp/lisp.rs | 40 +++++++++++++++++----------------------- lisp/new-cs.lisp | 8 ++------ lisp/run.sh | 3 ++- lisp/types.rs | 10 ++++++---- 5 files changed, 61 insertions(+), 34 deletions(-) create mode 100644 lisp/inverse.lisp diff --git a/lisp/inverse.lisp b/lisp/inverse.lisp new file mode 100644 index 000000000..34aa17c42 --- /dev/null +++ b/lisp/inverse.lisp @@ -0,0 +1,34 @@ +(println "new-cs.lisp") + +( (let* [aux (scalar 3) + x (alloc "x" aux) + x2 (alloc "x2" (* aux aux)) + x3 (alloc "x3" (* aux (* aux aux))) + input (alloc-input "input" (scalar 3)) + ] +(prove + (setup + ( + (enforce + (scalar::one input) + (scalar::one cs::one) + (scalar::one x3) + ) + + (enforce + (scalar::one x2) + (scalar::one x) + (scalar::one x3) + ) + + (enforce + (scalar::one x) + (scalar::one x) + (scalar::one x2) + ) + ) + ) + ) +) +) +;; (println 'verify (MyCircuit (scalar 27))) diff --git a/lisp/lisp.rs b/lisp/lisp.rs index 1cf3fb0ea..be133575e 100644 --- a/lisp/lisp.rs +++ b/lisp/lisp.rs @@ -521,16 +521,12 @@ pub fn setup(_ast: MalVal, env: Env) -> Result, MalE let start = Instant::now(); // Create parameters for our circuit. In a production deployment these would // be generated securely using a multiparty computation. - let allocs_input = get_allocations(&env, "AllocationsInput"); - let allocs = get_allocations(&env, "Allocations"); - let allocs_const = get_allocations(&env, "AllocationsConst"); - let enforce_allocs = get_enforce_allocs(&env); let c = LispCircuit { params: None, allocs: None, alloc_inputs: None, - constraints: None + constraints: None, }; // TODO move to another fn let random_parameters = @@ -558,26 +554,24 @@ pub fn prove(_ast: MalVal, env: Env) -> MalRet { let c = circuit.clone(); groth16::generate_random_parameters::(c, &mut OsRng).unwrap() }; - let proof = groth16::create_random_proof(circuit, ¶ms, &mut OsRng).unwrap(); - /* - match proof { - Ok(v) => { - println!("Prove: [{:?}]", start.elapsed()); - let mut vec_input = vec![]; - for (k, val) in allocs_input.iter() { - if let MalVal::ZKScalar(v) = val { - vec_input.push(*v); - } - } - // let verify_result = groth16::verify_proof(&pvk, &proof, &vec_input.as_slice()).is_ok(); - // println!("{:?}", verify_result); + let proof = groth16::create_random_proof(circuit, ¶ms, &mut OsRng).unwrap(); + let mut buf = File::create("proof.output").unwrap(); + proof.write(buf); + println!("Prove: [{:?}]", start.elapsed()); + let proof_file = File::open("proof.output").unwrap(); + let reader = std::io::BufReader::new(proof_file); + let proof_read: groth16::Proof = groth16::Proof::read(reader).unwrap(); + let mut vec_input = vec![]; + for (k, val) in allocs_input.iter() { + if let MalVal::ZKScalar(v) = val { + vec_input.push(*v); } - Err(e) => { - println!("Error {:?}", e); - } - }; - */ + } + let pvk = setup(_ast.clone(), env.clone()).unwrap(); + println!("{:?}", vec_input); + let verify_result = groth16::verify_proof(&pvk, &proof, &vec_input.as_slice()); + println!("{:?}", verify_result); Ok(MalVal::Nil) } diff --git a/lisp/new-cs.lisp b/lisp/new-cs.lisp index bc33782e2..f28a446cf 100644 --- a/lisp/new-cs.lisp +++ b/lisp/new-cs.lisp @@ -4,17 +4,13 @@ x (alloc "x" aux) x2 (alloc "x2" (* aux aux)) x3 (alloc "x3" (* aux (* aux aux))) - input (alloc-input "input" aux) + input (alloc-input "input" (scalar 27)) ] (prove (setup ( (enforce - ( - (scalar::one x) - (scalar::one x2) - ) - ;;(scalar::one::neg x) + (scalar::one x) (scalar::one x) (scalar::one x2) ) diff --git a/lisp/run.sh b/lisp/run.sh index f34da13f7..9d9b6f16d 100755 --- a/lisp/run.sh +++ b/lisp/run.sh @@ -1,2 +1,3 @@ export RUST_BACKTRACE=full -cargo run --bin lisp load jubjub-add.lisp +#cargo run --bin lisp load jubjub-add.lisp +cargo run --bin lisp load inverse.lisp diff --git a/lisp/types.rs b/lisp/types.rs index b92d6a19b..129fad361 100644 --- a/lisp/types.rs +++ b/lisp/types.rs @@ -31,7 +31,7 @@ pub struct LispCircuit { pub params: Option>, pub allocs: Option>, pub alloc_inputs: Option>, - pub constraints: Option> + pub constraints: Option>, } impl Circuit for LispCircuit { @@ -81,14 +81,15 @@ impl Circuit for LispCircuit { } println!("Enforce Allocations\n"); - for alloc_value in &self.constraints.unwrap_or(Vec::::new()) { - println!("{:?}", alloc_value); + // we need to keep order + for alloc_value in self.constraints.unwrap_or(Vec::::new()).iter() { let coeff = bls12_381::Scalar::one(); let mut left = bellman::LinearCombination::::zero(); let mut right = bellman::LinearCombination::::zero(); let mut output = bellman::LinearCombination::::zero(); for values in alloc_value.left.iter() { let (a, b) = values; + println!("a {:?} b {:?}", a, b); let mut val_b = CS::one(); if b != "cs::one" { val_b = *variables.get(b).unwrap(); @@ -102,7 +103,7 @@ impl Circuit for LispCircuit { if let MalVal::ZKScalar(val) = value { left = left + (*val, val_b); } - } + } } } @@ -138,6 +139,7 @@ impl Circuit for LispCircuit { |_| right.clone(), |_| output.clone(), ); + } Ok(())