diff --git a/lisp/Cargo.toml b/lisp/Cargo.toml deleted file mode 100644 index c7685a7e3..000000000 --- a/lisp/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "zklisp" -version = "0.1.0" -authors = ["mileschet"] - -[dependencies] -lazy_static = "1.4.0" -regex = "1.3.1" -itertools = "0.8.0" -fnv = "1.0.6" -bellman = { version = "0.8", default-features = false, features = ["groth16"] } -sapvi = { path = "../" } - -[[bin]] -name = "lisp" -path = "lisp.rs" diff --git a/lisp/bits.lisp b/lisp/bits.lisp index 5738d7013..eb43aed21 100644 --- a/lisp/bits.lisp +++ b/lisp/bits.lisp @@ -1,9 +1,9 @@ -(def! bit-dec (fn* [x] ( - (def! bits (unpack-bits x 256)) - (def! enforce-step-1 (fn* [b] (enforce (add-one-lc0 (sub-lc0 b) (add-lc1 b)))) - (map enforce-step-1 bits) - ;; this can be improved like declaring the function before using like the example above - (map (fn* [b] ((add-lc0 x) double-coeff-lc) bits) - (enforce reset-coeff-lc sub-lc0 add-one-lc1) - ))))) +(def! bit-dec + (fn* [x] ( + (def! bits (unpack-bits x 256)) + (def! enforce-step-1 (fn* [b] (enforce (add-one-lc0 (sub-lc0 b) (add-lc1 b)))) + (map enforce-step-1 bits) + (map (fn* [b] ((add-lc0 b) double-coeff-lc) bits) + (enforce reset-coeff-lc sub-lc0 add-one-lc1) + ))))) diff --git a/lisp/core.rs b/lisp/core.rs index f49b93836..3f7a4ffb0 100644 --- a/lisp/core.rs +++ b/lisp/core.rs @@ -4,6 +4,25 @@ use std::rc::Rc; use std::sync::Mutex; use std::time::{SystemTime, UNIX_EPOCH}; +use sapvi::bls_extensions::BlsStringConversion; +use sapvi::error::{Error, Result}; +use sapvi::serial::{Decodable, Encodable}; +use sapvi::vm::{ + AllocType, ConstraintInstruction, CryptoOperation, VariableIndex, VariableRef, ZKVMCircuit, + ZKVirtualMachine, +}; +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 crate::printer::pr_seq; use crate::reader::read_str; use crate::types::MalErr::ErrMalVal; @@ -153,17 +172,20 @@ fn nth(a: MalArgs) -> MalRet { } } -// (unpack-bits x 256 it produces a Vec fn unpack_bits(a: MalArgs) -> MalRet { - // Scalar::from_string( + let mut result = vec![]; match (a[0].clone(), a[1].clone()) { - (List(seq, _), Int(idx)) | (Vector(seq, _), Int(idx)) => { - if seq.len() <= idx as usize { - return error("nth: index out of range"); - } - Ok(seq[idx as usize].clone()) + (Str(ref s), Int(size)) => { + let value = Scalar::from_string(s); + for (_, bit) in value.to_le_bits().into_iter().cloned().enumerate() { + match bit { + true => result.push(Scalar::one()), + false => result.push(Scalar::zero()), + } + } + Ok(list!(result.iter().map(|a| Str(std::string::ToString::to_string(&a))).collect::>())) } - _ => error("invalid args to nth"), + _ => error("invalid args to unpack-bits"), } } @@ -243,6 +265,22 @@ fn seq(a: MalArgs) -> MalRet { } } +fn add_one_lc0(a: MalArgs) -> MalRet { + Ok(Sym("add-one-lc0".to_string())) +} + +fn enforce(a: MalArgs) -> MalRet { + Ok(Sym("enforce".to_string())) +} + +fn double_coeff_lc(a: MalArgs) -> MalRet { + Ok(Sym("double-coeff-lc".to_string())) +} + +fn reset_coeff_lc(a: MalArgs) -> MalRet { + Ok(Sym("reset-coeff-lc".to_string())) +} + pub fn ns() -> Vec<(&'static str, MalVal)> { vec![ ("=", func(|a| Ok(Bool(a[0] == a[1])))), @@ -329,6 +367,30 @@ pub fn ns() -> Vec<(&'static str, MalVal)> { ("deref", func(|a| a[0].deref())), ("reset!", func(|a| a[0].reset_bang(&a[1]))), ("swap!", func(|a| a[0].swap_bang(&a[1..].to_vec()))), - ("unpack_bits", func(unpack_bits)), + ("unpack-bits", func(unpack_bits)), + ("add-lc0", + func(|a| { + println!("add-lc0 {}", pr_seq(&a, false, "", "", " ")); + Ok(Nil) + })), + ("sub-lc0", + func(|a| { + println!("sub-lc0 {}", pr_seq(&a, false, "", "", " ")); + Ok(Nil) + })), + ("enforce", func(enforce)), + ("double-coeff-lc", func(double_coeff_lc)), + ("reset-coeff-lc", func(double_coeff_lc)), + ("sub-lc0", + func(|a| { + println!("sub-lc0 {}", pr_seq(&a, false, "", "", " ")); + Ok(Nil) + })), + ("add-one-lc0", func(add_one_lc0)), + ("add-lc1", + func(|a| { + println!("add-lc1 {}", pr_seq(&a, false, "", "", " ")); + Ok(Nil) + })), ] } diff --git a/lisp/lib.rs b/lisp/lib.rs deleted file mode 100644 index 01a5b6fdb..000000000 --- a/lisp/lib.rs +++ /dev/null @@ -1,12 +0,0 @@ -use bellman::groth16; -use bls12_381::{Bls12, Scalar}; -use std::collections::{HashMap, HashSet}; - -pub use crate::bls_extensions::BlsStringConversion; -pub use crate::error::{Error, Result}; -pub use crate::serial::{Decodable, Encodable}; -pub use crate::vm::{ - AllocType, ConstraintInstruction, CryptoOperation, VariableIndex, VariableRef, ZKVMCircuit, - ZKVirtualMachine, -}; - diff --git a/lisp/new.lisp b/lisp/new.lisp index a8162d468..e9e714f7c 100644 --- a/lisp/new.lisp +++ b/lisp/new.lisp @@ -1,2 +1,10 @@ -(def! C_D "0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1") -(println C_D) + +(def! dec "73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000000") +(def! bits (unpack-bits dec 256)) +(println (count bits)) +(map (fn* [b] (println (add-one-lc0))) bits) + +;; (map (fn* [b] ((add-lc0 b) (double-coeff-lc))) bits) +;; (reset-coeff-lc) +;; (sub-lc0 x) +;; (add-one-lc1)