From 2863fe6df06c4ecc5ee2678fb1c5038d090ff315 Mon Sep 17 00:00:00 2001 From: ada Date: Wed, 16 Dec 2020 23:56:47 +0100 Subject: [PATCH] added prove code and env_get for the circuit --- lisp/lisp.rs | 29 ++++++++++++++++++++++++----- lisp/new-cs.lisp | 2 +- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lisp/lisp.rs b/lisp/lisp.rs index 613371b63..9b42d4cea 100644 --- a/lisp/lisp.rs +++ b/lisp/lisp.rs @@ -1,8 +1,9 @@ #![allow(non_snake_case)] +use crate::MalVal::Zk; use crate::groth16::VerifyingKey; use crate::types::LispCircuit; -use sapvi::ZKVMCircuit; +use sapvi::{ZKVMCircuit, ZKVirtualMachine}; use sapvi::bls_extensions::BlsStringConversion; use simplelog::*; @@ -49,6 +50,8 @@ use crate::env::{env_bind, env_find, env_get, env_new, env_set, env_sets, Env}; #[macro_use] mod core; +pub const ZK_CIRCUIT_ENV_KEY : &str = "ZKC"; + // read fn read(str: &str) -> MalRet { reader::read_str(str.to_string()) @@ -288,10 +291,14 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet { let a1 = l[1].clone(); let circuit = setup(&ast)?; println!("{:?}", a1); - env_sets(&env, &a1.pr_str(false), circuit); + env_sets(&env, ZK_CIRCUIT_ENV_KEY, circuit); eval(a1.clone(), env.clone()) } - //Sym(ref a0sym) if a0sym == "prove" => { + Sym(ref a0sym) if a0sym == "prove" => { + let a1 = l[1].clone(); + println!("{:?}", a1); + prove(a1.clone(), env.clone()) + } //Sym(ref a0sym) if a0sym == "verify" => { Sym(ref a0sym) if a0sym == "enforce" => { let (a1, a2) = (l[0].clone(), l[1].clone()); @@ -373,10 +380,22 @@ pub fn setup(ast: &MalVal) -> MalRet { Ok(MalVal::Zk(Rc::new(c))) } -pub fn prove(ast: &MalVal) -> MalRet { +pub fn prove(mut ast: MalVal, mut env: Env) -> MalRet { + let c = match env_find(&env, ZK_CIRCUIT_ENV_KEY) { + Some(e) => match env_get(&e, &Sym(ZK_CIRCUIT_ENV_KEY.to_string()))? { + Zk(c) => { + MalVal::Zk(c) + } + _ => { MalVal::Nil } + } + None => { println!("circuit not found."); MalVal::Nil } + }; + + println!("{:?}", c); + // 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 = LispCircuit { params: Rc::new(vector![vec![ diff --git a/lisp/new-cs.lisp b/lisp/new-cs.lisp index 1169988c0..ad6e2bfec 100644 --- a/lisp/new-cs.lisp +++ b/lisp/new-cs.lisp @@ -25,7 +25,7 @@ ))) (def! a (scalar "0000000000000000000000000000000000000000000000000000000000000003")) (setup (MyCircuit (a (* a a) (* (* a a) a)))) -;; (prove MyCircuit) +(prove MyCircuit) ;; (verify (prove MyCircuit) (scalar 27)) ;; (U - A - B) / (1 - C) ;; [(1 - C)] * [y] = [U - A - B]