Skip to content

Commit

Permalink
Add and Sub for u64 finished
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-barrett committed Aug 16, 2023
1 parent 130c0df commit 6eecd56
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
19 changes: 19 additions & 0 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ pub trait LurkField: PrimeField + PrimeFieldBits {
Some(u64::from_le_bytes(byte_array))
}

/// Attempts to convert the field element to a u64
fn to_u128(&self) -> Option<u128> {
for x in &self.to_repr().as_ref()[16..] {
if *x != 0 {
return None;
}
}
let mut byte_array = [0u8; 16];
byte_array.copy_from_slice(&self.to_repr().as_ref()[0..16]);
Some(u128::from_le_bytes(byte_array))
}

/// Converts the first 4 bytes of the field element to a u32
fn to_u32_unchecked(&self) -> u32 {
let mut byte_array = [0u8; 4];
Expand All @@ -155,6 +167,13 @@ pub trait LurkField: PrimeField + PrimeFieldBits {
u64::from_le_bytes(byte_array)
}

/// Converts the first 16 bytes of the field element to a u128
fn to_u128_unchecked(&self) -> u128 {
let mut byte_array = [0u8; 16];
byte_array.copy_from_slice(&self.to_repr().as_ref()[0..16]);
u128::from_le_bytes(byte_array)
}

/// Constructs a field element from a u64
fn from_u64(x: u64) -> Self {
x.into()
Expand Down
36 changes: 31 additions & 5 deletions src/lem/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ fn apply_cont() -> Func {
let err: Cont::Error;
let nil: Expr::Nil;
let t = Symbol("t");
let zero = Num(0);
let size_u64 = Num(18446744073709551616);

match ctrl.tag {
Ctrl::ApplyContinuation => {
Expand Down Expand Up @@ -615,7 +617,6 @@ fn apply_cont() -> Func {
return(secret, env, continuation, makethunk)
}
Symbol("commit") => {
let zero = Num(0);
let comm = hide(zero, result);
return(comm, env, continuation, makethunk)
}
Expand Down Expand Up @@ -746,8 +747,19 @@ fn apply_cont() -> Func {
return (val, env, continuation, makethunk)
}
Num(2) => {
// TODO
return (result, env, err, errctrl)
let val = add(evaled_arg, result);
let not_overflow = lt(val, size_u64);
match not_overflow.val {
Num(0) => {
let val = sub(val, size_u64);
let val = cast(val, Expr::U64);
return (val, env, continuation, makethunk)
}
Num(1) => {
let val = cast(val, Expr::U64);
return (val, env, continuation, makethunk)
}
}
}
}
}
Expand All @@ -761,8 +773,22 @@ fn apply_cont() -> Func {
return (val, env, continuation, makethunk)
}
Num(2) => {
// TODO
return (result, env, err, errctrl)
// Subtraction in U64 is almost the same as subtraction
// in the field. If the difference is negative, we need
// to add 2^64 to get back to U64 domain.
let val = sub(evaled_arg, result);
let is_neg = lt(val, zero);
match is_neg.val {
Num(0) => {
let val = add(val, size_u64);
let val = cast(val, Expr::U64);
return (val, env, continuation, makethunk)
}
Num(1) => {
let val = cast(val, Expr::U64);
return (val, env, continuation, makethunk)
}
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ impl std::fmt::Display for Tag {
/// LEM literals
#[derive(Debug, PartialEq, Clone, Eq, Hash)]
pub enum Lit {
// TODO maybe it should be a LurkField instead of u64
Num(u64),
// TODO maybe it should be a LurkField instead of u128
Num(u128),
String(String),
Symbol(Symbol),
}
Expand All @@ -166,7 +166,7 @@ impl Lit {
match self {
Self::Symbol(s) => store.intern_symbol(s.clone()),
Self::String(s) => store.intern_string(s),
Self::Num(num) => Ptr::num((*num).into()),
Self::Num(num) => Ptr::num(F::from_u128(*num)),
}
}
pub fn from_ptr<F: LurkField>(ptr: &Ptr<F>, store: &Store<F>) -> Option<Self> {
Expand All @@ -175,7 +175,7 @@ impl Lit {
match ptr.tag() {
Expr(Num) => match ptr {
Ptr::Leaf(_, f) => {
let num = LurkField::to_u64_unchecked(f);
let num = LurkField::to_u128_unchecked(f);
Some(Self::Num(num))
}
_ => unreachable!(),
Expand Down

0 comments on commit 6eecd56

Please sign in to comment.