Browse Source

debugging

ada 5 years ago
parent
commit
b652168355
4 changed files with 98 additions and 75 deletions
  1. BIN
      lisp/lisp-cheat-sheet.png
  2. 40 35
      lisp/lisp.rs
  3. 1 1
      lisp/new-cs.lisp
  4. 57 39
      lisp/types.rs

BIN
lisp/lisp-cheat-sheet.png


+ 40 - 35
lisp/lisp.rs

@@ -29,6 +29,7 @@ extern crate regex;
 mod types;
 use crate::types::MalErr::{ErrMalVal, ErrString};
 use crate::types::MalVal::{Bool, Enforce, Func, Hash, List, MalFunc, Nil, Str, Sym, Vector};
+use crate::types::VerifyKeyParams;
 use crate::types::{error, format_error, MalArgs, MalErr, MalRet, MalVal};
 mod env;
 mod printer;
@@ -297,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" => {
@@ -517,69 +518,73 @@ pub fn get_allocations(env: &Env, key: &str) -> Rc<FnvHashMap<String, MalVal>> {
     }
 }
 
-pub fn setup(_ast: MalVal, env: Env) -> Result<PreparedVerifyingKey<Bls12>, MalErr> {
+pub fn setup(_ast: MalVal, env: Env) -> Result<VerifyKeyParams, MalErr> {
     let start = Instant::now();
-    // Create parameters for our circuit. In a production deployment these would
-    // be generated securely using a multiparty computation.
-
-    let c = LispCircuit {
-        params: None,
-        allocs: None,
-        alloc_inputs: None,
-        constraints: None,
-    };
-    // TODO move to another fn
+        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);
     println!("Setup: [{:?}]", start.elapsed());
 
-    Ok(pvk)
+    Ok(VerifyKeyParams {
+        verifying_key: pvk,
+        random_params: random_parameters,
+    })
 }
 
 pub fn prove(_ast: MalVal, env: Env) -> MalRet {
+    let start = Instant::now();
     let allocs_input = get_allocations(&env, "AllocationsInput");
     let allocs = get_allocations(&env, "Allocations");
     let enforce_allocs = get_enforce_allocs(&env);
     let allocs_const = get_allocations(&env, "AllocationsConst");
 
-    let start = Instant::now();
+    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 verifying_key = Some(groth16::prepare_verifying_key(&params.as_ref().unwrap().vk));
+
     let circuit = LispCircuit {
-        params: Some(allocs_const.as_ref().clone()),
-        allocs: Some(allocs.as_ref().clone()),
-        alloc_inputs: Some(allocs_input.as_ref().clone()),
-        constraints: Some(enforce_allocs),
-    };
-    let params = {
-        let c = circuit.clone();
-        groth16::generate_random_parameters::<Bls12, _, _>(c, &mut OsRng).unwrap()
+        params: allocs_const.as_ref().clone(),
+        allocs: allocs.as_ref().clone(),
+        alloc_inputs: allocs_input.as_ref().clone(),
+        constraints: enforce_allocs.clone(),
     };
 
-    let proof = groth16::create_random_proof(circuit, &params, &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<bls12_381::Bls12> = groth16::Proof::read(reader).unwrap();
+    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() {
         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);
+    println!("vec input {:?}", vec_input);
+    let result = groth16::verify_proof(
+        verifying_key.as_ref().unwrap(),
+        &proof,
+        vec_input.as_slice(),
+    );
+    println!("{:?}", result);
+
     Ok(MalVal::Nil)
 }
 
 pub fn verify(_ast: &MalVal) -> MalRet {
     let _public_input = vec![bls12_381::Scalar::from(27)];
     let start = Instant::now();
-    // Check the proof!
-    //assert!(groth16::verify_proof(&pvk, &proof, &public_input).is_ok());
     println!("Verify: [{:?}]", start.elapsed());
     Ok(MalVal::Nil)
 }

+ 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 27))
+      input (alloc-input "input" (scalar 3))
       ]
 (prove
  (setup 

+ 57 - 39
lisp/types.rs

@@ -11,6 +11,7 @@ use crate::env::{env_bind, Env};
 use crate::types::MalErr::{ErrMalVal, ErrString};
 use crate::types::MalVal::{Atom, Bool, Func, Hash, Int, List, MalFunc, Nil, Str, Sym, Vector};
 use bellman::Variable;
+use bls12_381::Bls12;
 use bls12_381::Scalar;
 
 #[derive(Debug, Clone)]
@@ -26,12 +27,42 @@ pub struct EnforceAllocation {
     pub output: Vec<(String, String)>,
 }
 
+pub struct VerifyKeyParams {
+    pub random_params: groth16::Parameters<Bls12>,
+    pub verifying_key: groth16::PreparedVerifyingKey<Bls12>,
+}
+
 #[derive(Debug, Clone)]
 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 params: FnvHashMap<String, MalVal>,
+    pub allocs: FnvHashMap<String, MalVal>,
+    pub alloc_inputs: FnvHashMap<String, MalVal>,
+    pub constraints: Vec<EnforceAllocation>,
+}
+
+#[derive(Debug, Clone)]
+pub enum MalVal {
+    Nil,
+    Bool(bool),
+    Int(i64),
+    Str(String),
+    Sym(String),
+    List(Rc<Vec<MalVal>>, Rc<MalVal>),
+    Vector(Rc<Vec<MalVal>>, Rc<MalVal>),
+    Hash(Rc<FnvHashMap<String, MalVal>>, Rc<MalVal>),
+    Func(fn(MalArgs) -> MalRet, Rc<MalVal>),
+    MalFunc {
+        eval: fn(ast: MalVal, env: Env) -> MalRet,
+        ast: Rc<MalVal>,
+        env: Env,
+        params: Rc<MalVal>,
+        is_macro: bool,
+        meta: Rc<MalVal>,
+    },
+    Atom(Rc<RefCell<MalVal>>),
+    Zk(Rc<LispCircuit>), // TODO remote it
+    Enforce(Rc<Vec<EnforceAllocation>>),
+    ZKScalar(bls12_381::Scalar),
 }
 
 impl Circuit<bls12_381::Scalar> for LispCircuit {
@@ -40,10 +71,10 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
         cs: &mut CS,
     ) -> Result<(), SynthesisError> {
         let mut variables: FnvHashMap<String, Variable> = FnvHashMap::default();
-        let mut params_const = self.params.unwrap_or(FnvHashMap::default());
+        let mut params_const = self.params;
 
         println!("Allocations\n");
-        for (k, v) in &self.allocs.unwrap_or(FnvHashMap::default()) {
+        for (k, v) in &self.allocs {
             println!("k {:?} v {:?}", k, v);
             match v {
                 MalVal::ZKScalar(val) => {
@@ -62,7 +93,7 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
         }
 
         println!("Allocations Input\n");
-        for (k, v) in &self.alloc_inputs.unwrap_or(FnvHashMap::default()) {
+        for (k, v) in &self.alloc_inputs {
             println!("k {:?} v {:?}", k, v);
             match v {
                 MalVal::ZKScalar(val) => {
@@ -81,15 +112,15 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
         }
 
         println!("Enforce Allocations\n");
-        // we need to keep order 
-        for alloc_value in self.constraints.unwrap_or(Vec::<EnforceAllocation>::new()).iter() {
+        // we need to keep order
+        for alloc_value in self.constraints.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);
+                println!("left: a {:?} b {:?}", a, b);
                 let mut val_b = CS::one();
                 if b != "cs::one" {
                     val_b = *variables.get(b).unwrap();
@@ -103,12 +134,14 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
                         if let MalVal::ZKScalar(val) = value {
                             left = left + (*val, val_b);
                         }
-                    } 
+                    }
+                    println!("here i am");
                 }
             }
 
             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();
@@ -117,19 +150,24 @@ 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");
                 }
             }
 
             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 + (coeff, val_b);
+                    output = output + (Scalar::one(), val_b);
                 } else if a == "scalar::one::neg" {
-                    output = output + (coeff.neg(), val_b);
+                    output = output + (Scalar::one().neg(), val_b);
+                } else { 
+                    println!("here i am");
                 }
             }
 
@@ -139,44 +177,24 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
                 |_| right.clone(),
                 |_| output.clone(),
             );
-            
         }
 
         Ok(())
     }
 }
 
-#[derive(Debug, Clone)]
-pub enum MalVal {
-    Nil,
-    Bool(bool),
-    Int(i64),
-    Str(String),
-    Sym(String),
-    List(Rc<Vec<MalVal>>, Rc<MalVal>),
-    Vector(Rc<Vec<MalVal>>, Rc<MalVal>),
-    Hash(Rc<FnvHashMap<String, MalVal>>, Rc<MalVal>),
-    Func(fn(MalArgs) -> MalRet, Rc<MalVal>),
-    MalFunc {
-        eval: fn(ast: MalVal, env: Env) -> MalRet,
-        ast: Rc<MalVal>,
-        env: Env,
-        params: Rc<MalVal>,
-        is_macro: bool,
-        meta: Rc<MalVal>,
-    },
-    Atom(Rc<RefCell<MalVal>>),
-    Zk(Rc<LispCircuit>), // TODO remote it
-    Enforce(Rc<Vec<EnforceAllocation>>),
-    ZKScalar(bls12_381::Scalar),
-}
-
 #[derive(Debug)]
 pub enum MalErr {
     ErrString(String),
     ErrMalVal(MalVal),
 }
 
+impl From<SynthesisError> for MalErr {
+    fn from(err: SynthesisError) -> MalErr {
+        ErrString("SynthesisError".to_string())
+    }
+}
+
 pub type MalArgs = Vec<MalVal>;
 pub type MalRet = Result<MalVal, MalErr>;