change eval mode for alloc, alloc-input and alloc-const

This commit is contained in:
ada
2021-04-08 12:29:05 +02:00
parent 7c9b4dc18b
commit 65d6616f47
4 changed files with 59 additions and 40 deletions

View File

@@ -330,6 +330,7 @@ fn sub_scalar(a: MalArgs) -> MalRet {
}
fn mul_scalar(a: MalArgs) -> MalRet {
println!("mul {:?}", a[0]);
match (a[0].clone(), a[1].clone()) {
(Func(_, _), ZKScalar(a1)) => {
if let Vector(ref values, _) = a[0].apply(vec![]).unwrap() {
@@ -627,7 +628,8 @@ fn scalar_is_zero(a: MalArgs) -> MalRet {
}
}
fn add_scalar(a: MalArgs) -> MalRet {
fn add_scalar(a: MalArgs) -> MalRet {
println!("add_scalar {:?}", a);
match (a[0].clone(), a[1].clone()) {
(Func(_, _), ZKScalar(a1)) => {
if let Vector(ref values, _) = a[0].apply(vec![]).unwrap() {
@@ -672,6 +674,20 @@ fn add_scalar(a: MalArgs) -> MalRet {
s0.add_assign(s1);
Ok(ZKScalar(s0))
}
(ZKScalar(a1), Str(a0)) => {
let (mut s0, s1) = (bls12_381::Scalar::from_string(&a0), a1);
s0.add_assign(s1);
Ok(ZKScalar(s0))
}
// (List(a0, _), ZKScalar(mut a1)) => {
// let first_slice = a0.to_vec();
// let result = first_slice[0].apply(first_slice[1..].to_vec());
// println!("result {:?}", result);
// if let ZKScalar(value) = result.unwrap() {
// a1.add_assign(value);
// }
// Ok(ZKScalar(a1))
// }
_ => error(&format!("scalar add expect (zkscalar, zkscalar) found \n {:?}", a).to_string()),
}
}

View File

@@ -213,6 +213,7 @@
(def! u-add (get add-result "u3"))
(def! v-add (get add-result "v3"))
(def! val (last (last (zk-double u-add v-add))))
(println acc val)
(def! acc (i+ acc 1))
))
(val)
@@ -287,11 +288,9 @@
(conj value-result
(get (last (last
(rangeproof-alloc bit digit))) "lc")))
(println 'digit digit 'bit bit)
(def! digit (double digit))
(def! idx (i+ idx 1))
))
(println 'value-result value-result)
(def! value-alloc (alloc-input "value-alloc" value))
(enforce
(value-result)
@@ -307,10 +306,10 @@
(def! generator-value-commit-v (scalar "09d2a25018194750e9adacf78531ee3bfddbadd767671d517aa788c352641ff1"))
(def! generator-value-random-u (scalar "002924d15ccf8014ce724a41753d17dce3a9f7382a3db18fba3c8e286bb77382"))
(def! generator-value-random-v (scalar "0cb825b790b0601c4999e52d9added7d10d013b33fd95ca7d2ddd51691a09075"))
(def! mint-contract (fn* [secret value serial rnd-coin rnd-value] (
(def! result-mul (last (last (jj-mul generator-coin-u generator-coin-v secret))) "lc")
(def! public-u (alloc "public-u" (get result-mul "u")))
(def! public-v (alloc "public-v" (get result-mul "v")))
(def! mint-contract (fn* [public-u public-v value serial rnd-coin rnd-value] (
;; (def! result-mul (last (last (jj-mul generator-coin-u generator-coin-v secret))) "lc")
;; (def! public-u (alloc "public-u" (get result-mul "u3")))
;; (def! public-v (alloc "public-v" (get result-mul "v3")))
(def! mimc-round-1 (get (last (mimc public-u public-v)) "result"))
(def! mimc-round-2 (get (last (mimc mimc-round-1 value)) "result"))
(def! mimc-round-3 (get (last (mimc mimc-round-2 serial)) "result"))
@@ -318,14 +317,15 @@
(rangeproof value)
(def! result-mul-value
(last (last (jj-mul generator-value-commit-u generator-value-commit-v value))))
(println result-mul-value)
(def! result-mul-rnd-value
(last (last (jj-mul generator-value-random-u generator-value-random-v rnd-value))))
(println result-mul-rnd-value)
(def! add-result (last
(jj-add (get result-mul-value "u") (get result-mul-value "u")
(get result-mul-rnd-value "u") (get result-mul-rnd-value "u"))))
(def! value-commit add-result)
(println 'value-commit value-commit)
(alloc-input "value-commit" value-commit)
(jj-add (get result-mul-value "u3") (get result-mul-value "v3")
(get result-mul-rnd-value "u3") (get result-mul-rnd-value "v3"))))
(println 'add-result add-result)
(alloc-input "value-commit" add-result)
)))
;; (def! spend-contract (fn*
@@ -336,12 +336,13 @@
(prove
(
(def! secret (scalar 1))
(def! value (scalar 2))
(def! serial (scalar 3))
(def! public-u (scalar "0000000000000000000000000000000000000000000000001000000000000000"))
(def! public-v (scalar "0000000000000000000000000000000000000000000000002000000000000000"))
(def! value (scalar 3))
(def! serial (scalar 4))
(def! rnd-coin (rnd-scalar))
(def! rnd-value (rnd-scalar))
(mint-contract secret value serial rnd-coin rnd-value)
(mint-contract public-u public-v value serial rnd-coin rnd-value)
)
)

View File

@@ -14,8 +14,7 @@ use rand::rngs::OsRng;
use std::rc::Rc;
use std::time::Instant;
use std::{
borrow::{Borrow, BorrowMut},
fs,
borrow::{Borrow, BorrowMut},
};
use std::{cell::RefCell, collections::HashMap};
use types::EnforceAllocation;
@@ -150,6 +149,8 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
let start = Instant::now();
'tco: loop {
// TODO check DEBUG symbol on env
println!("debug eval \t {:?} \t {:?}", ast, start.elapsed());
ret = match ast.clone() {
List(l, _) => {
if l.len() == 0 {
@@ -296,7 +297,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
match eval(l[1].clone(), env.clone())? {
MalVal::Int(v) => {
for _i in 0..v {
eval(l[2].clone(), env.clone())?;
ast = eval_ast(&l[2], &env)?;
}
Ok(Nil)
}
@@ -350,10 +351,16 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
// TODO add debug param
prove(a1.clone(), env.clone())
}
Sym(ref a0sym) if a0sym == "kill" => {
error(&format!("KILL at: {:?}", ast).to_string())
}
Sym(ref a0sym) if a0sym == "alloc-const" => {
// let start = Instant::now();
let start = Instant::now();
let a1 = l[1].clone();
let value = eval(l[2].clone(), env.clone())?;
let mut value = eval_ast(&l[2], &env)?;
if let Func(_, _) = value {
value = value.apply(vec![]).unwrap();
}
let result = eval(value.clone(), env.clone())?;
let allocs = get_allocations(&env, "AllocationsConst");
allocs.borrow_mut().insert(a1.pr_str(false), result.clone());
@@ -362,13 +369,16 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
} else {
env_set(&env, Sym("AllocationsConst".to_string()), Alloc(allocs))?;
}
// println!("Alloc Const: {:?}", start.elapsed());
println!("Alloc Const: {:?}", start.elapsed());
Ok(result.clone())
}
Sym(ref a0sym) if a0sym == "alloc-input" => {
// let start = Instant::now();
let start = Instant::now();
let a1 = l[1].clone();
let value = eval(l[2].clone(), env.clone())?;
let mut value = eval_ast(&l[2], &env)?;
if let Func(_, _) = value {
value = value.apply(vec![]).unwrap();
}
let result = eval(value.clone(), env.clone())?;
let allocs = get_allocations(&env, "AllocationsInput");
allocs.borrow_mut().insert(a1.pr_str(false), result.clone());
@@ -377,13 +387,13 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
} else {
env_set(&env, Sym("AllocationsInput".to_string()), Alloc(allocs))?;
}
// println!("Alloc Input: {:?}", start.elapsed());
println!("Alloc Input: {:?}", start.elapsed());
Ok(result.clone())
}
Sym(ref a0sym) if a0sym == "alloc" => {
// let start = Instant::now();
let start = Instant::now();
let a1 = l[1].clone();
let mut value = eval(l[2].clone(), env.clone())?;
let mut value = eval_ast(&l[2], &env)?;
if let Func(_, _) = value {
value = value.apply(vec![]).unwrap();
}
@@ -395,7 +405,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
} else {
env_set(&env, Sym("Allocations".to_string()), Alloc(allocs))?;
}
// println!("Alloc: {:?}", start.elapsed());
println!("Alloc:\t{:?}\t{:?}\t{:?}", value, result, start.elapsed());
Ok(result.clone())
}
//Sym(ref a0sym) if a0sym == "verify" => {
@@ -515,10 +525,6 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
output: out_vec,
};
enforce_vec.push(enforce);
// let mut new_vec: Vec<EnforceAllocation> = vec![enforce];
// for value in enforce_vec.iter() {
// new_vec.push(value.clone());
// }
if let Some(e) = &env.outer {
env_set(
&e,
@@ -533,12 +539,6 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
)?;
}
// println!(
// "allocs here {:?}",
// get_allocations_nested(&env, "Allocations")
// );
// println!("enforce here {:?}", get_enforce_allocs_nested(&env));
Ok(MalVal::Str("enforce-eof".to_string()))
}
_ => match eval_ast(&ast, &env)? {
@@ -576,8 +576,7 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
break;
} // end 'tco loop
// println!("eval end \t {:?} \t {:?}", ast, start.elapsed());
// println!("debug eval \t {:?} \t {:?}", ast, start.elapsed());
ret
}

View File

@@ -71,11 +71,13 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
cs: &mut CS,
) -> Result<(), SynthesisError> {
let mut variables: HashMap<String, Variable> = HashMap::default();
// TODO change the name from alloc-const to constant
let params_const = self.params;
let circuitTime = Instant::now();
let start = Instant::now();
// println!("Allocations\n");
// TODO is the private and params
for (k, v) in &self.allocs {
match v {
MalVal::ZKScalar(val) => {
@@ -101,6 +103,7 @@ impl Circuit<bls12_381::Scalar> for LispCircuit {
println!("circuit alloc \t {:?}", start.elapsed());
let start = Instant::now();
// println!("Allocations Input\n");
// TODO alloc-input is the public value
for (k, v) in &self.alloc_inputs {
match v {
MalVal::ZKScalar(val) => {