فهرست منبع

adding verify on lisp

ada 5 سال پیش
والد
کامیت
32746d0748
5فایلهای تغییر یافته به همراه61 افزوده شده و 34 حذف شده
  1. 34 0
      lisp/inverse.lisp
  2. 17 23
      lisp/lisp.rs
  3. 2 6
      lisp/new-cs.lisp
  4. 2 1
      lisp/run.sh
  5. 6 4
      lisp/types.rs

+ 34 - 0
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)))

+ 17 - 23
lisp/lisp.rs

@@ -521,16 +521,12 @@ pub fn setup(_ast: MalVal, env: Env) -> Result<PreparedVerifyingKey<Bls12>, 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::<Bls12, _, _>(c, &mut OsRng).unwrap()
     };
+
     let proof = groth16::create_random_proof(circuit, &params, &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);
-        }
-        Err(e) => {
-            println!("Error {:?}", e);
+    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<bls12_381::Bls12> = 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);
         }
-    };
-    */
+    }
+    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)
 }
 

+ 2 - 6
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)
   )

+ 2 - 1
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

+ 6 - 4
lisp/types.rs

@@ -31,7 +31,7 @@ pub struct LispCircuit {
     pub params: Option<FnvHashMap<String, MalVal>>,
     pub allocs: Option<FnvHashMap<String, MalVal>>,
     pub alloc_inputs: Option<FnvHashMap<String, MalVal>>,
-    pub constraints: Option<Vec<EnforceAllocation>>
+    pub constraints: Option<Vec<EnforceAllocation>>,
 }
 
 impl Circuit<bls12_381::Scalar> for LispCircuit {
@@ -81,14 +81,15 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
         }
 
         println!("Enforce Allocations\n");
-        for alloc_value in &self.constraints.unwrap_or(Vec::<EnforceAllocation>::new()) {
-            println!("{:?}", alloc_value);
+        // we need to keep order 
+        for alloc_value in self.constraints.unwrap_or(Vec::<EnforceAllocation>::new()).iter() {
             let coeff = bls12_381::Scalar::one();
             let mut left = bellman::LinearCombination::<Scalar>::zero();
             let mut right = bellman::LinearCombination::<Scalar>::zero();
             let mut output = bellman::LinearCombination::<Scalar>::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<bls12_381::Scalar> for LispCircuit {
                         if let MalVal::ZKScalar(val) = value {
                             left = left + (*val, val_b);
                         }
-                    }
+                    } 
                 }
             }
 
@@ -138,6 +139,7 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
                 |_| right.clone(),
                 |_| output.clone(),
             );
+            
         }
 
         Ok(())