Skip to content

Commit

Permalink
Progress on u64
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-barrett committed Aug 14, 2023
1 parent c810654 commit 1eba1f3
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 21 deletions.
21 changes: 13 additions & 8 deletions src/lem/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ use bellperson::{

use crate::circuit::gadgets::{
constraints::{
add, alloc_equal, alloc_equal_const, and, enforce_selector_with_premise, implies_equal,
mul, sub, alloc_is_zero, pick, div,
add, alloc_equal, alloc_equal_const, alloc_is_zero, and, div,
enforce_selector_with_premise, implies_equal, mul, pick, sub,
},
data::{allocate_constant, hash_poseidon},
pointer::AllocatedPtr,
Expand Down Expand Up @@ -559,27 +559,31 @@ impl Func {
let a_num = a.hash();
let b_num = b.hash();

let b_is_zero = &alloc_is_zero(&mut cs.namespace(|| "b_is_zero"), b)?;
let b_is_zero = &alloc_is_zero(&mut cs.namespace(|| "b_is_zero"), b_num)?;
let one = g.global_allocator.get_or_alloc_const(cs, F::ONE)?;

let divisor = pick(
&mut cs.namespace(|| "maybe-dummy divisor"),
b_is_zero,
&one,
b,
b_num,
)?;

let quotient = div(&mut cs.namespace(|| "quotient"), a, &divisor)?;
let quotient = div(&mut cs.namespace(|| "quotient"), a_num, &divisor)?;

let tag = g
.global_allocator
.get_or_alloc_const(cs, Tag::Expr(Num).to_field())?;
let c = AllocatedPtr::from_parts(tag, quotient);
bound_allocations.insert(tgt.clone(), c);

}
Op::Lt(_tgt, _a, _b) => {
Op::Lt(tgt, _a, _b) => {
// TODO
let allocated_ptr = AllocatedPtr::from_parts(
g.global_allocator.get_or_alloc_const(cs, F::ZERO)?,
g.global_allocator.get_or_alloc_const(cs, F::ZERO)?,
);
bound_allocations.insert(tgt.clone(), allocated_ptr);
}
Op::Emit(_) => (),
Op::Hide(tgt, _sec, _pay) => {
Expand Down Expand Up @@ -870,7 +874,8 @@ impl Func {
num_constraints += 1;
}
Op::Div(_, _, _) => {
// TODO
globals.insert(FWrap(F::ONE));
num_constraints += 5;
}
Op::Lt(_, _, _) => {
// TODO
Expand Down
52 changes: 41 additions & 11 deletions src/lem/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,9 @@ fn apply_cont() -> Func {
return (val, env, continuation, makethunk)
}
Symbol("div") => {
return (result, env, err, errctrl)
// TODO deal with U64
let val = div(evaled_arg, result);
return (val, env, continuation, makethunk)
}
Symbol("%") => {
// TODO
Expand All @@ -724,20 +726,48 @@ fn apply_cont() -> Func {
return (result, env, err, errctrl)
}
Symbol("<") => {
// TODO
return (result, env, err, errctrl)
let val = lt(evaled_arg, result);
match val.val {
Num(0) => {
return (nil, env, continuation, makethunk)
}
Num(1) => {
return (t, env, continuation, makethunk)
}
}
}
Symbol(">") => {
// TODO
return (result, env, err, errctrl)
let val = lt(result, evaled_arg);
match val.val {
Num(0) => {
return (nil, env, continuation, makethunk)
}
Num(1) => {
return (t, env, continuation, makethunk)
}
}
}
Symbol("<=") => {
// TODO
return (result, env, err, errctrl)
let val = lt(result, evaled_arg);
match val.val {
Num(0) => {
return (t, env, continuation, makethunk)
}
Num(1) => {
return (nil, env, continuation, makethunk)
}
}
}
Symbol(">=") => {
// TODO
return (result, env, err, errctrl)
let val = lt(evaled_arg, result);
match val.val {
Num(0) => {
return (t, env, continuation, makethunk)
}
Num(1) => {
return (nil, env, continuation, makethunk)
}
}
}
};
return (result, env, err, errctrl)
Expand Down Expand Up @@ -808,8 +838,8 @@ mod tests {
use blstrs::Scalar as Fr;

const NUM_INPUTS: usize = 1;
const NUM_AUX: usize = 8092;
const NUM_CONSTRAINTS: usize = 10125;
const NUM_AUX: usize = 8120;
const NUM_CONSTRAINTS: usize = 10198;
const NUM_SLOTS: SlotsCounter = SlotsCounter {
hash2: 16,
hash3: 4,
Expand Down
14 changes: 13 additions & 1 deletion src/lem/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::field::{FWrap, LurkField};
use crate::num::Num;
use anyhow::{bail, Result};
use std::collections::VecDeque;

Expand Down Expand Up @@ -142,7 +143,18 @@ impl Block {
bindings.insert(tgt.clone(), c);
}
Op::Lt(tgt, a, b) => {
todo!()
let a = bindings.get(a)?;
let b = bindings.get(b)?;
let c = match (a, b) {
(Ptr::Leaf(Tag::Expr(Num), f), Ptr::Leaf(Tag::Expr(Num), g)) => {
let f = Num::Scalar(*f);
let g = Num::Scalar(*g);
let b = if f < g { F::ONE } else { F::ZERO };
Ptr::Leaf(Tag::Expr(Num), b)
}
_ => bail!("`<` only works on numbers"),
};
bindings.insert(tgt.clone(), c);
}
Op::Emit(a) => {
let a = bindings.get(a)?;
Expand Down
17 changes: 17 additions & 0 deletions src/lem/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ macro_rules! op {
$crate::var!($b),
)
};
( let $tgt:ident = lt($a:ident, $b:ident) ) => {
$crate::lem::Op::Lt(
$crate::var!($tgt),
$crate::var!($a),
$crate::var!($b),
)
};
( emit($v:ident) ) => {
$crate::lem::Op::Emit($crate::var!($v))
};
Expand Down Expand Up @@ -283,6 +290,16 @@ macro_rules! block {
$($tail)*
)
};
(@seq {$($limbs:expr)*}, let $tgt:ident = lt($a:ident, $b:ident) ; $($tail:tt)*) => {
$crate::block! (
@seq
{
$($limbs)*
$crate::op!(let $tgt = lt($a, $b))
},
$($tail)*
)
};
(@seq {$($limbs:expr)*}, emit($v:ident) ; $($tail:tt)*) => {
$crate::block! (
@seq
Expand Down
2 changes: 1 addition & 1 deletion src/lem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ pub enum Op {
Mul(Var, Var, Var),
/// `Div(y, a, b)` binds `y` to the sum of `a` and `b`
Div(Var, Var, Var),
/// `Div(y, a, b)` binds `y` to `t` if `a < b`, or to `nil` otherwise
/// `Lt(y, a, b)` binds `y` to `t` if `a < b`, or to `nil` otherwise
Lt(Var, Var, Var),
/// `Emit(v)` simply prints out the value of `v` when interpreting the code
Emit(Var),
Expand Down

0 comments on commit 1eba1f3

Please sign in to comment.