Răsfoiți Sursa

prove without refactor

ada 5 ani în urmă
părinte
comite
8e06b5c00f
6 a modificat fișierele cu 31 adăugiri și 39 ștergeri
  1. 0 3
      lisp/core.rs
  2. 1 1
      lisp/inverse.lisp
  3. 15 16
      lisp/lisp.rs
  4. 1 1
      lisp/new-cs.lisp
  5. 2 2
      lisp/run.sh
  6. 12 16
      lisp/types.rs

+ 0 - 3
lisp/core.rs

@@ -283,7 +283,6 @@ fn sub_scalar(a: MalArgs) -> MalRet {
 }
 
 fn mul_scalar(a: MalArgs) -> MalRet {
-    println!("{:?}", a);
     match (a[0].clone(), a[1].clone()) {
         (Func(_, _), ZKScalar(a1)) => {
             if let Vector(ref values, _) = a[0].apply(vec![]).unwrap() {
@@ -306,7 +305,6 @@ fn mul_scalar(a: MalArgs) -> MalRet {
 }
 
 fn div_scalar(a: MalArgs) -> MalRet {
-    println!("{:?}", a);
     match (a[0].clone(), a[1].clone()) {
         (ZKScalar(s0), ZKScalar(s1)) => {
             let ret = s1.invert().map(|other| *&s0 * other);
@@ -390,7 +388,6 @@ fn scalar_from(a: MalArgs) -> MalRet {
             Ok(ZKScalar(s0))
         }
         Int(a0) => {
-            println!("{:?}", a0);
             let s0 = bls12_381::Scalar::from(a0 as u64);
             Ok(ZKScalar(s0))
         }

+ 1 - 1
lisp/inverse.lisp

@@ -4,7 +4,7 @@
       x (alloc "x" aux)
       x2 (alloc "x2" (* aux aux))
       x3 (alloc "x3" (* aux (* aux aux)))
-      input (alloc-input "input" (scalar 3))
+      input (alloc-input "input" (scalar 27))
       ]
 (prove
  (setup 

+ 15 - 16
lisp/lisp.rs

@@ -298,7 +298,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
                         let a1 = l[1].clone();
                         // todo
                         ast = eval(a1.clone(), env.clone())?;
-//                        let _pvk = setup(a1.clone(), env.clone())?;
+                        //                        let _pvk = setup(a1.clone(), env.clone())?;
                         continue 'tco;
                     }
                     Sym(ref a0sym) if a0sym == "prove" => {
@@ -520,12 +520,12 @@ pub fn get_allocations(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
 
 pub fn setup(_ast: MalVal, env: Env) -> Result<VerifyKeyParams, MalErr> {
     let start = Instant::now();
-        let c = LispCircuit {
-            params: FnvHashMap::default(),
-            allocs: FnvHashMap::default(),
-            alloc_inputs: FnvHashMap::default(),
-            constraints: Vec::new()
-        };
+    let c = LispCircuit {
+        params: FnvHashMap::default(),
+        allocs: FnvHashMap::default(),
+        alloc_inputs: FnvHashMap::default(),
+        constraints: Vec::new(),
+    };
     let random_parameters =
         groth16::generate_random_parameters::<Bls12, _, _>(c, &mut OsRng).unwrap();
     let pvk = groth16::prepare_verifying_key(&random_parameters.vk);
@@ -545,13 +545,13 @@ pub fn prove(_ast: MalVal, env: Env) -> MalRet {
     let allocs_const = get_allocations(&env, "AllocationsConst");
 
     let params = Some({
-        let c = LispCircuit {
-            params: FnvHashMap::default(),
-            allocs: FnvHashMap::default(),
-            alloc_inputs: FnvHashMap::default(),
-            constraints: Vec::new()
-        };
-        groth16::generate_random_parameters::<Bls12, _, _>(c, &mut OsRng)?
+    let circuit = LispCircuit {
+        params: allocs_const.as_ref().clone(),
+        allocs: allocs.as_ref().clone(),
+        alloc_inputs: allocs_input.as_ref().clone(),
+        constraints: enforce_allocs.clone(),
+    };
+        groth16::generate_random_parameters::<Bls12, _, _>(circuit, &mut OsRng)?
     });
     let verifying_key = Some(groth16::prepare_verifying_key(&params.as_ref().unwrap().vk));
 
@@ -562,8 +562,7 @@ pub fn prove(_ast: MalVal, env: Env) -> MalRet {
         constraints: enforce_allocs.clone(),
     };
 
-    let proof =
-        groth16::create_random_proof(circuit, params.as_ref().unwrap(), &mut OsRng)?;
+    let proof = groth16::create_random_proof(circuit, params.as_ref().unwrap(), &mut OsRng)?;
 
     let mut vec_input = vec![];
     for (k, val) in allocs_input.iter() {

+ 1 - 1
lisp/new-cs.lisp

@@ -4,7 +4,7 @@
       x (alloc "x" aux)
       x2 (alloc "x2" (* aux aux))
       x3 (alloc "x3" (* aux (* aux aux)))
-      input (alloc-input "input" (scalar 3))
+      input (alloc-input "input" (scalar 27))
       ]
 (prove
  (setup 

+ 2 - 2
lisp/run.sh

@@ -1,3 +1,3 @@
 export RUST_BACKTRACE=full
-#cargo run --bin lisp load jubjub-add.lisp
-cargo run --bin lisp load inverse.lisp
+cargo run --bin lisp load jubjub-add.lisp
+#cargo run --bin lisp load inverse.lisp

+ 12 - 16
lisp/types.rs

@@ -78,12 +78,12 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
             println!("k {:?} v {:?}", k, v);
             match v {
                 MalVal::ZKScalar(val) => {
-                    let var = cs.alloc(|| "alloc", || Ok(*val))?;
+                    let var = cs.alloc(|| k, || Ok(*val))?;
                     variables.insert(k.to_string(), var);
                 }
                 MalVal::Str(val) => {
                     let val_scalar = bls12_381::Scalar::from_string(&*val);
-                    let var = cs.alloc(|| "alloc", || Ok(val_scalar))?;
+                    let var = cs.alloc(|| k, || Ok(val_scalar))?;
                     variables.insert(k.to_string(), var);
                 }
                 _ => {
@@ -97,12 +97,12 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
             println!("k {:?} v {:?}", k, v);
             match v {
                 MalVal::ZKScalar(val) => {
-                    let var = cs.alloc_input(|| "alloc", || Ok(*val))?;
+                    let var = cs.alloc_input(|| k, || Ok(*val))?;
                     variables.insert(k.to_string(), var);
                 }
                 MalVal::Str(val) => {
                     let val_scalar = bls12_381::Scalar::from_string(&*val);
-                    let var = cs.alloc_input(|| "alloc", || Ok(val_scalar))?;
+                    let var = cs.alloc_input(|| k, || Ok(val_scalar))?;
                     variables.insert(k.to_string(), var);
                 }
                 _ => {
@@ -120,13 +120,12 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
             let mut output = bellman::LinearCombination::<Scalar>::zero();
             for values in alloc_value.left.iter() {
                 let (a, b) = values;
-                println!("left: a {:?} b {:?}", a, b);
                 let mut val_b = CS::one();
                 if b != "cs::one" {
                     val_b = *variables.get(b).unwrap();
                 }
                 if a == "scalar::one" {
-                    left = left + val_b;
+                    left = left + (coeff, val_b);
                 } else if a == "scalar::one::neg" {
                     left = left + (coeff.neg(), val_b);
                 } else {
@@ -135,13 +134,12 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
                             left = left + (*val, val_b);
                         }
                     }
-                    println!("here i am");
                 }
+                println!("left: a {:?} b {:?} val_b: {:?}", a, b, val_b);
             }
 
             for values in alloc_value.right.iter() {
                 let (a, b) = values;
-                println!("right: a {:?} b {:?}", a, b);
                 let mut val_b = CS::one();
                 if b != "cs::one" {
                     val_b = *variables.get(b).unwrap();
@@ -150,27 +148,25 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
                     right = right + (coeff, val_b);
                 } else if a == "scalar::one::neg" {
                     right = right + (coeff.neg(), val_b);
-                } else { 
-                    println!("here i am");
                 }
+                println!("right: a {:?} b {:?} val_b: {:?}", a, b, val_b);
             }
 
             for values in alloc_value.output.iter() {
                 let (a, b) = values;
-                println!("output: a {:?} b {:?}", a, b);
                 let mut val_b = CS::one();
                 if b != "cs::one" {
                     val_b = *variables.get(b).unwrap();
                 }
                 if a == "scalar::one" {
-                    output = output + (Scalar::one(), val_b);
+                    output = output + (coeff, val_b);
                 } else if a == "scalar::one::neg" {
-                    output = output + (Scalar::one().neg(), val_b);
-                } else { 
-                    println!("here i am");
+                    output = output + (coeff.neg(), val_b);
                 }
+                println!("output: a {:?} b {:?} val_b: {:?}", a, b, val_b);
             }
 
+            println!("Enforcing ...");
             cs.enforce(
                 || "constraint",
                 |_| left.clone(),
@@ -191,7 +187,7 @@ pub enum MalErr {
 
 impl From<SynthesisError> for MalErr {
     fn from(err: SynthesisError) -> MalErr {
-        ErrString("SynthesisError".to_string())
+        ErrString(err.to_string())
     }
 }