Skip to content

Commit

Permalink
Revert u128 usage to U128
Browse files Browse the repository at this point in the history
  • Loading branch information
guipublic committed Jan 9, 2024
1 parent d2e0949 commit e6202e1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/lexer/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum LexerErrorKind {
InvalidIntegerLiteral { span: Span, found: String },
#[error("{:?} is not a valid attribute", found)]
MalformedFuncAttribute { span: Span, found: String },
#[error("Integer type is larger than the maximum supported size of u128")]
#[error("Integer type is larger than the maximum supported size of u127")]
TooManyBits { span: Span, max: u32, got: u32 },
#[error("Logical and used instead of bitwise and")]
LogicalAnd { span: Span },
Expand Down
4 changes: 0 additions & 4 deletions compiler/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,6 @@ impl IntType {

let max_bits = FieldElement::max_num_bits() / 2;

if !is_signed && str_as_u32 == 128 {
return Ok(Some(Token::Ident("U128".to_string())));
}

if str_as_u32 > max_bits {
return Err(LexerErrorKind::TooManyBits { span, max: max_bits, got: str_as_u32 });
}
Expand Down
32 changes: 18 additions & 14 deletions noir_stdlib/src/uint128.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct U128 {

impl U128 {

pub fn new(lo: u64, hi: u64) -> U128 {
pub fn from_u64s_le(lo: u64, hi: u64) -> U128 {
// in order to handle multiplication, we need to represent the product of two u64 without overflow
assert(crate::field::modulus_num_bits() as u32 > 128);
U128 {
Expand All @@ -19,6 +19,10 @@ impl U128 {
}
}

pub fn from_u64s_be(hi: u64, lo: u64) -> U128 {
U128::from_u64s_le(lo,hi)
}

pub fn from_le_bytes(bytes: [u8; 16]) -> U128 {
let mut lo = 0;
let mut base = 1;
Expand Down Expand Up @@ -54,7 +58,7 @@ impl U128 {
let bytes = hex.as_bytes();
// string must starts with "0x"
assert((bytes[0] == 48) & (bytes[1] == 120), "Invalid hexadecimal string");
assert(N < 35, "Input does not fit into a u128");
assert(N < 35, "Input does not fit into a U128");

let mut lo = 0;
let mut hi = 0;
Expand Down Expand Up @@ -96,15 +100,15 @@ impl U128 {

unconstrained fn unconstrained_div(self: Self, b: U128) -> (U128, U128) {
if self < b {
(U128::new(0, 0), self)
(U128::from_u64s_le(0, 0), self)
} else {
//TODO check if this can overflow?
let (q,r) = self.unconstrained_div(b * U128::new(2,0));
let q_mul_2 = q * U128::new(2,0);
let (q,r) = self.unconstrained_div(b * U128::from_u64s_le(2,0));
let q_mul_2 = q * U128::from_u64s_le(2,0);
if r < b {
(q_mul_2, r)
} else {
(q_mul_2 + U128::new(1,0), r - b)
(q_mul_2 + U128::from_u64s_le(1,0), r - b)
}

}
Expand Down Expand Up @@ -257,9 +261,9 @@ impl BitXor for U128 {
}
}

impl Shl for u128 {
fn shl(self, other: u128) -> u128 {
assert(other < u128::new(128,0), "attempt to shift left with overflow");
impl Shl for U128 {
fn shl(self, other: U128) -> U128 {
assert(other < U128::from_u64s_le(128,0), "attempt to shift left with overflow");
let exp_bits = other.lo.to_be_bits(7);

let mut r: Field = 2;
Expand All @@ -268,13 +272,13 @@ impl Shl for u128 {
y = (exp_bits[7-i] as Field) * (r * y) + (1 - exp_bits[7-i] as Field) * y;
r *= r;
}
self.wrapping_mul(u128::from_integer(y))
self.wrapping_mul(U128::from_integer(y))
}
}

impl Shr for u128 {
fn shr(self, other: u128) -> u128 {
assert(other < u128::new(128,0), "attempt to shift right with overflow");
impl Shr for U128 {
fn shr(self, other: U128) -> U128 {
assert(other < U128::from_u64s_le(128,0), "attempt to shift right with overflow");
let exp_bits = other.lo.to_be_bits(7);

let mut r: Field = 2;
Expand All @@ -283,6 +287,6 @@ impl Shr for u128 {
y = (exp_bits[7-i] as Field) * (r * y) + (1 - exp_bits[7-i] as Field) * y;
r *= r;
}
self / u128::from_integer(y)
self / U128::from_integer(y)
}
}
34 changes: 17 additions & 17 deletions test_programs/execution_success/u128/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
use dep::std;

fn main(mut x: u32, y: u32, z: u32, big_int: u128, hexa: str<7>) {
let a = u128::new(x as u64, x as u64);
let b = u128::new(y as u64, x as u64);
fn main(mut x: u32, y: u32, z: u32, big_int: U128, hexa: str<7>) {
let a = U128::from_u64s_le(x as u64, x as u64);
let b = U128::from_u64s_le(y as u64, x as u64);
let c = a + b;
assert(c.lo == z as Field);
assert(c.hi == 2 * x as Field);
assert(u128::from_hex(hexa).lo == 0x1f03a);
let t1 = u128::from_hex("0x9d9c7a87771f03a23783f9d9c7a8777");
let t2 = u128::from_hex("0x45a26c708BFCF39041");
assert(U128::from_hex(hexa).lo == 0x1f03a);
let t1 = U128::from_hex("0x9d9c7a87771f03a23783f9d9c7a8777");
let t2 = U128::from_hex("0x45a26c708BFCF39041");
let t = t1 + t2;
assert(t.lo == 0xc5e4b029996e17b8);
assert(t.hi == 0x09d9c7a87771f07f);
let t3 = u128::from_le_bytes(t.to_le_bytes());
let t3 = U128::from_le_bytes(t.to_le_bytes());
assert(t == t3);

let t4 = t - t2;
assert(t4 == t1);

let t5 = u128::new(0, 1);
let t6 = u128::new(1, 0);
let t5 = U128::from_u64s_le(0, 1);
let t6 = U128::from_u64s_le(1, 0);
assert((t5 - t6).hi == 0);

assert(
(u128::from_hex("0x71f03a23783f9d9c7a8777") * u128::from_hex("0x8BFCF39041")).hi
== u128::from_hex("0x3e4e0471b873470e247c824e61445537").hi
(U128::from_hex("0x71f03a23783f9d9c7a8777") * U128::from_hex("0x8BFCF39041")).hi
== U128::from_hex("0x3e4e0471b873470e247c824e61445537").hi
);
let q = u128::from_hex("0x3e4e0471b873470e247c824e61445537") / u128::from_hex("0x8BFCF39041");
assert(q == u128::from_hex("0x71f03a23783f9d9c7a8777"));
let q = U128::from_hex("0x3e4e0471b873470e247c824e61445537") / U128::from_hex("0x8BFCF39041");
assert(q == U128::from_hex("0x71f03a23783f9d9c7a8777"));

assert(big_int.hi == 2);

let mut small_int = u128::from_integer(x);
let mut small_int = U128::from_integer(x);
assert(small_int.lo == x as Field);
assert(x == small_int.to_integer());
let shift = small_int << small_int;
assert(shift == u128::from_integer(x << x));
assert(shift == U128::from_integer(x << x));
assert(shift >> small_int == small_int);
assert(shift >> u128::from_integer(127) == u128::from_integer(0));
assert(shift << u128::from_integer(127) == u128::from_integer(0));
assert(shift >> U128::from_integer(127) == U128::from_integer(0));
assert(shift << U128::from_integer(127) == U128::from_integer(0));

}

0 comments on commit e6202e1

Please sign in to comment.