mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
refactoring lisp zk eval
This commit is contained in:
@@ -181,7 +181,7 @@ fn unpack_bits(a: MalArgs) -> MalRet {
|
||||
}
|
||||
Ok(list!(result
|
||||
.iter()
|
||||
.map(|a| Str(std::string::ToString::to_string(&a)))
|
||||
.map(|a| Str(std::string::ToString::to_string(&a)[2..].to_string()))
|
||||
.collect::<Vec<MalVal>>()))
|
||||
}
|
||||
_ => error("invalid args to unpack-bits"),
|
||||
@@ -364,5 +364,6 @@ pub fn ns() -> Vec<(&'static str, MalVal)> {
|
||||
("add", func(add)),
|
||||
("sub", func(sub)),
|
||||
("lc0", func(|a| Ok(MalVal::Lc0))),
|
||||
("lc1", func(|a| Ok(MalVal::Lc1))),
|
||||
]
|
||||
}
|
||||
|
||||
148
lisp/lisp.rs
148
lisp/lisp.rs
@@ -11,8 +11,7 @@ use std::time::Instant;
|
||||
use fnv::FnvHashMap;
|
||||
use itertools::Itertools;
|
||||
use sapvi::vm::{
|
||||
AllocType, ConstraintInstruction, CryptoOperation, VariableIndex, VariableRef,
|
||||
ZKVirtualMachine,
|
||||
AllocType, ConstraintInstruction, CryptoOperation, VariableIndex, VariableRef, ZKVirtualMachine,
|
||||
};
|
||||
|
||||
#[macro_use]
|
||||
@@ -275,96 +274,22 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
|
||||
}
|
||||
Sym(ref a0sym) if a0sym == "zkcons!" => {
|
||||
let (a1, a2) = (l[1].clone(), l[2].clone());
|
||||
match env_get(&env, &a1).ok().unwrap() {
|
||||
Zk(mut zk) => match eval_ast(&a2, &env)? {
|
||||
List(ref el, _) => {
|
||||
let args = el.to_vec();
|
||||
for b in args.iter() {
|
||||
match b {
|
||||
Sym(_) => {}
|
||||
Add(b1, b2) => {
|
||||
println!("Add {:?} {:?}", b1, b2);
|
||||
// TODO extract a method
|
||||
zk.private.push(Scalar::from_string(
|
||||
&b2.pr_str(false).to_string(),
|
||||
));
|
||||
let const_a: ConstraintInstruction = match b1.as_ref() {
|
||||
Lc0 =>
|
||||
ConstraintInstruction::Lc0Add(
|
||||
zk.private.len(),
|
||||
|
||||
),
|
||||
Lc1 =>
|
||||
ConstraintInstruction::Lc1Add(
|
||||
zk.private.len(),
|
||||
),
|
||||
Lc2 =>
|
||||
ConstraintInstruction::Lc2Add(
|
||||
zk.private.len(),
|
||||
),
|
||||
_ => ConstraintInstruction::Lc0Add(0)
|
||||
};
|
||||
zk.constraints.push(const_a);
|
||||
env_set(
|
||||
&env,
|
||||
a1.clone(),
|
||||
types::MalVal::Zk(zk.clone()),
|
||||
);
|
||||
}
|
||||
Sub(b1, b2) => {
|
||||
println!("Sub {:?} {:?}", b1, b2);
|
||||
zk.private.push(Scalar::from_string(
|
||||
&b2.pr_str(false).to_string(),
|
||||
));
|
||||
let const_a: ConstraintInstruction = match b1.as_ref() {
|
||||
Lc0 =>
|
||||
ConstraintInstruction::Lc0Sub(
|
||||
zk.private.len(),
|
||||
|
||||
),
|
||||
Lc1 =>
|
||||
ConstraintInstruction::Lc1Add(
|
||||
zk.private.len(),
|
||||
),
|
||||
Lc2 =>
|
||||
ConstraintInstruction::Lc2Add(
|
||||
zk.private.len(),
|
||||
),
|
||||
_ => ConstraintInstruction::Lc0Add(0)
|
||||
};
|
||||
zk.constraints.push(const_a);
|
||||
env_set(
|
||||
&env,
|
||||
a1.clone(),
|
||||
types::MalVal::Zk(zk.clone()),
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
println!("not mapped");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
error("not mapped");
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
error("zk circuit not found on env");
|
||||
let value = eval(a2.clone(), env.clone())?;
|
||||
println!("{:?}", value);
|
||||
match value {
|
||||
List(ref el, _) => {
|
||||
zkcons_eval(el.to_vec(), &a1, &env);
|
||||
}
|
||||
_ => println!("invalid format"),
|
||||
}
|
||||
Ok(Nil)
|
||||
}
|
||||
Sym(ref a0sym) if a0sym == "defzk!" => {
|
||||
let (a1, a2) = (l[1].clone(), l[2].clone());
|
||||
let zk_circuit = MalVal::Zk(ZKCircuit {
|
||||
name: a1.pr_str(true),
|
||||
constraints: Vec::new(),
|
||||
private: Vec::new(),
|
||||
public: Vec::new(),
|
||||
});
|
||||
env_set(&env, l[1].clone(), zk_circuit.clone());
|
||||
Ok(zk_circuit)
|
||||
let circuit = zk_circuit_create(&a1, &env);
|
||||
let val = types::MalVal::Zk(circuit.clone());
|
||||
env_set(&env, a1.clone(), val.clone());
|
||||
Ok(val.clone())
|
||||
}
|
||||
Sym(ref a0sym) if a0sym == "fn*" => {
|
||||
let (a1, a2) = (l[1].clone(), l[2].clone());
|
||||
@@ -421,6 +346,57 @@ fn eval(mut ast: MalVal, mut env: Env) -> MalRet {
|
||||
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(),
|
||||
};
|
||||
zk_circuit
|
||||
}
|
||||
|
||||
fn zkcons_eval(elements: Vec<MalVal>, a1: &MalVal, env: &Env) -> MalRet {
|
||||
let mut zk = match env_get(&env, &a1).ok().unwrap() {
|
||||
Zk(v) => v,
|
||||
n => zk_circuit_create(a1, env),
|
||||
};
|
||||
println!("{:?}", zk);
|
||||
for b in elements.iter() {
|
||||
match b {
|
||||
Sym(_) => {}
|
||||
Add(b1, b2) => {
|
||||
zk.private
|
||||
.push(Scalar::from_string(&b2.pr_str(false).to_string()));
|
||||
let const_a: ConstraintInstruction = match b1.as_ref() {
|
||||
Lc0 => ConstraintInstruction::Lc0Add(zk.private.len()),
|
||||
Lc1 => ConstraintInstruction::Lc1Add(zk.private.len()),
|
||||
Lc2 => ConstraintInstruction::Lc2Add(zk.private.len()),
|
||||
_ => ConstraintInstruction::Lc0Add(0),
|
||||
};
|
||||
zk.constraints.push(const_a);
|
||||
env_set(&env, a1.clone(), types::MalVal::Zk(zk.clone()));
|
||||
println!("{:?}", a1.clone());
|
||||
println!("{:?}", zk.clone());
|
||||
}
|
||||
Sub(b1, b2) => {
|
||||
zk.private
|
||||
.push(Scalar::from_string(&b2.pr_str(false).to_string()));
|
||||
let const_a: ConstraintInstruction = match b1.as_ref() {
|
||||
Lc0 => ConstraintInstruction::Lc0Sub(zk.private.len()),
|
||||
Lc1 => ConstraintInstruction::Lc1Add(zk.private.len()),
|
||||
Lc2 => ConstraintInstruction::Lc2Add(zk.private.len()),
|
||||
_ => ConstraintInstruction::Lc0Add(0),
|
||||
};
|
||||
zk.constraints.push(const_a);
|
||||
env_set(&env, a1.clone(), types::MalVal::Zk(zk.clone()));
|
||||
}
|
||||
v => println!("not match"),
|
||||
}
|
||||
}
|
||||
Ok(Nil)
|
||||
}
|
||||
|
||||
// print
|
||||
fn print(ast: &MalVal) -> String {
|
||||
ast.pr_str(true)
|
||||
|
||||
@@ -1,27 +1,33 @@
|
||||
(def! x "73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000")
|
||||
(def! one "0000000000000000000000000000000000000000000000000000000000000001")
|
||||
(defzk! circuit ())
|
||||
(def! bits (unpack-bits x))
|
||||
;;(def! bits (unpack-bits x))
|
||||
(def! bits [one x one])
|
||||
;;zkcons add a constraints instruction to the circuit
|
||||
(zkcons! circuit (
|
||||
(add lc0 one)
|
||||
(sub lc0 x)
|
||||
)
|
||||
)
|
||||
;; (map (fn* [b] (zkcons! circuit (
|
||||
;; (zkcons! circuit (
|
||||
;; (add lc0 one)
|
||||
;; '(sub lc0 b)
|
||||
;; '(add lc1 x)
|
||||
;; (sub lc0 x)
|
||||
;; )
|
||||
;; )
|
||||
(zkcons! circuit (map (fn* [b] (
|
||||
(add lc0 one)
|
||||
(sub lc0 b)
|
||||
(add lc1 x)
|
||||
;; 'enforce)
|
||||
;; )) bits)
|
||||
;; (map (fn* [b] (zkcons! circuit (
|
||||
;; '(add lc0 b)
|
||||
;; 'double-coeff-lc)
|
||||
;; )) bits)
|
||||
)) bits))
|
||||
(def! enforce-loop-const (map (fn* [b] (
|
||||
(add lc0 one)
|
||||
(sub lc0 b)
|
||||
(add lc1 x)
|
||||
;; 'enforce)
|
||||
)) bits))
|
||||
(zkcons! circuit enforce-loop-const)
|
||||
;; (zkcons! circuit (
|
||||
;; 'reset-coeff-lc
|
||||
;; '(sub lc0 x)
|
||||
;; '(add lc1 one)
|
||||
;; 'enforce)
|
||||
;; )
|
||||
(println circuit)
|
||||
;;(println circuit)
|
||||
;;(println enforce-loop-const)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user