Browse Source

added zkcircuit to start setup process

ada 5 years ago
parent
commit
8f37dc8aa5
4 changed files with 79 additions and 27 deletions
  1. 9 0
      lisp/core.rs
  2. 63 11
      lisp/lisp.rs
  3. 7 14
      lisp/new-cs.lisp
  4. 0 2
      src/old/basic_minimal.rs

+ 9 - 0
lisp/core.rs

@@ -186,6 +186,14 @@ fn unpack_bits(a: MalArgs) -> MalRet {
     }
     }
 }
 }
 
 
+fn last(a: MalArgs) -> MalRet {
+    match a[0].clone() {
+        List(ref seq, _) | Vector(ref seq, _) if seq.len() == 0 => Ok(Nil),
+        List(ref seq, _) | Vector(ref seq, _) => Ok(seq[seq.len() - 1].clone()),
+        Nil => Ok(Nil),
+        _ => error("invalid args to first"),
+    }
+}
 fn first(a: MalArgs) -> MalRet {
 fn first(a: MalArgs) -> MalRet {
     match a[0].clone() {
     match a[0].clone() {
         List(ref seq, _) | Vector(ref seq, _) if seq.len() == 0 => Ok(Nil),
         List(ref seq, _) | Vector(ref seq, _) if seq.len() == 0 => Ok(Nil),
@@ -439,6 +447,7 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
         ("empty?", func(|a| a[0].empty_q())),
         ("empty?", func(|a| a[0].empty_q())),
         ("nth", func(nth)),
         ("nth", func(nth)),
         ("first", func(first)),
         ("first", func(first)),
+        ("last", func(last)),
         ("rest", func(rest)),
         ("rest", func(rest)),
         ("count", func(|a| a[0].count())),
         ("count", func(|a| a[0].count())),
         ("apply", func(apply)),
         ("apply", func(apply)),

+ 63 - 11
lisp/lisp.rs

@@ -1,10 +1,22 @@
 #![allow(non_snake_case)]
 #![allow(non_snake_case)]
 
 
+use sapvi::ZKVMCircuit;
 use sapvi::bls_extensions::BlsStringConversion;
 use sapvi::bls_extensions::BlsStringConversion;
 
 
 use simplelog::*;
 use simplelog::*;
 
 
-
+use bellman::{
+    gadgets::{
+        Assignment,
+    },
+    groth16, Circuit, ConstraintSystem, SynthesisError,
+};
+use bls12_381::Bls12;
+use bls12_381::Scalar;
+use ff::{Field, PrimeField};
+use rand::rngs::OsRng;
+use std::ops::{AddAssign, MulAssign, SubAssign};
+use std::time::Instant;
 use std::rc::Rc;
 use std::rc::Rc;
 
 
 //use std::collections::HashMap;
 //use std::collections::HashMap;
@@ -347,16 +359,56 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
     ret
     ret
 }
 }
 
 
-fn zk_circuit_create(a1: &MalVal, _env: &Env) -> ZKCircuit {
-    let zk_circuit = ZKCircuit {
-        name: a1.pr_str(true),
-        constraints: Vec::new(),
-        private: Vec::new(),
-        public: Vec::new(),
-        params: Vec::new(),
-        verifying_key: Vec::new(),
+struct MyCircuit {
+    params: Vec<Option<bls12_381::Scalar>>,
+}
+
+impl Circuit<bls12_381::Scalar> for MyCircuit {
+    fn synthesize<CS: ConstraintSystem<bls12_381::Scalar>>(
+        self,
+        cs: &mut CS,
+    ) -> Result<(), SynthesisError> {
+        Ok(())
+    }
+}
+
+pub fn setup(ast: &MalVal) -> Result<String, MalErr> {
+    let start = Instant::now();
+    // Create parameters for our circuit. In a production deployment these would
+    // be generated securely using a multiparty computation.
+    let params = {
+        let c = MyCircuit { params: vec![None] };
+        groth16::generate_random_parameters::<Bls12, _, _>(c, &mut OsRng).unwrap()
     };
     };
-    zk_circuit
+    println!("Setup: [{:?}]", start.elapsed());
+
+    // Prepare the verification key (for proof verification).
+    let pvk = groth16::prepare_verifying_key(&params.vk);
+/*
+    // Pick a preimage and compute its hash.
+    let quantity = bls12_381::Scalar::from(3);
+
+    // Create an instance of our circuit (with the preimage as a witness).
+    let c = MyCircuit {
+        params: vec![
+            Some(quantity),
+            Some(quantity * quantity),
+            Some(quantity * quantity * quantity),
+        ],
+    };
+
+    let start = Instant::now();
+    // Create a Groth16 proof with our parameters.
+    let proof = groth16::create_random_proof(c, &params, &mut OsRng).unwrap();
+    println!("Prove: [{:?}]", start.elapsed());
+    
+    
+    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());
+*/
 }
 }
 
 
 // print
 // print
@@ -373,7 +425,7 @@ fn rep(str: &str, env: &Env) -> Result<String, MalErr> {
 fn main() -> Result<(), ()> {
 fn main() -> Result<(), ()> {
     let matches = clap_app!(zklisp =>
     let matches = clap_app!(zklisp =>
         (version: "0.1.0")
         (version: "0.1.0")
-        (author: "Roberto Santacroce Martins <miles.chet@gmail.com>")
+        (author: "mileschet <miles.chet@gmail.com>")
         (about: "A Lisp Interpreter for Zero Knowledge Virtual Machine")
         (about: "A Lisp Interpreter for Zero Knowledge Virtual Machine")
         (@subcommand load =>
         (@subcommand load =>
             (about: "Load the file into the interpreter")
             (about: "Load the file into the interpreter")

+ 7 - 14
lisp/new-cs.lisp

@@ -3,6 +3,7 @@
 ;; alloc
 ;; alloc
 ;; alloc-input 
 ;; alloc-input 
 ;; scalar::one
 ;; scalar::one
+;; scalar::zero
 ;; scalar
 ;; scalar
 ;; cs::one
 ;; cs::one
 ;; bellman::zero
 ;; bellman::zero
@@ -12,23 +13,15 @@
 (println "new-cs.lisp")
 (println "new-cs.lisp")
 (def! MyCircuit (fn* [aux]  
 (def! MyCircuit (fn* [aux]  
 (let* [x  (alloc "num" (first aux))
 (let* [x  (alloc "num" (first aux))
-     x2 (alloc "product num" (second aux)) 
+     x2 (alloc "product num" (first (rest aux))) 
      x3 (alloc "product num" (last aux))
      x3 (alloc "product num" (last aux))
-     input (alloc-input "input variable" (last aux))
-     ;; let coeff = bls12_381::Scalar::one();
-     coeff scalar::one]
-    (enforce "mc" coeff 
-         ;; let lc0 = bellman::LinearCombination::zero() + (coeff, x_var);
-         (zero + x) 
-         ;; let lc1 = bellman::LinearCombination::zero() + (coeff, x_var);
-         (zero + x) 
-         ;; let lc2 = bellman::LinearCombination::zero() + (coeff, x2_var);
-         (zero + x3))
-    (enforce "mc" coeff (zero + x2) (zero + x) (zero + x3))
-    (enforce "mc" coeff (zero + input) (zero + cs::one) (zero + x3))
+     input (alloc-input "input variable" (last aux))]
+   ;; Lc0: [(Scalar::one(), CS::one()), (Scalar::one().neg(), C)]
+;; Lc1: [(Scalar::one(), y)]
+;; Lc2: [(Scalar::one(), U), (Scalar::one().neg(), A), (Scalar::one().neg(), B)]
+    (enforce (scalar::one ) (zero + cs::one) (zero + x3))
 )))
 )))
 (def! a (scalar "0000000000000000000000000000000000000000000000000000000000000003"))
 (def! a (scalar "0000000000000000000000000000000000000000000000000000000000000003"))
-(println (* (* a a) a))
 (setup (MyCircuit (a (* a a) (* (* a a) a))))
 (setup (MyCircuit (a (* a a) (* (* a a) a))))
 ;; (prove MyCircuit)
 ;; (prove MyCircuit)
 ;; (verify (prove MyCircuit) (scalar 27))
 ;; (verify (prove MyCircuit) (scalar 27))

+ 0 - 2
src/old/basic_minimal.rs

@@ -104,8 +104,6 @@ fn main() {
     let proof = groth16::create_random_proof(c, &params, &mut OsRng).unwrap();
     let proof = groth16::create_random_proof(c, &params, &mut OsRng).unwrap();
     println!("Prove: [{:?}]", start.elapsed());
     println!("Prove: [{:?}]", start.elapsed());
 
 
-    let _start = Instant::now();
-
     let public_input = vec![bls12_381::Scalar::from(27)];
     let public_input = vec![bls12_381::Scalar::from(27)];
 
 
     let start = Instant::now();
     let start = Instant::now();