Skip to content

Commit 41d9f44

Browse files
committed
switch to u253
1 parent 81c07ea commit 41d9f44

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

packages/contracts/noir/common/src/lib.nr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use protocol_types::hash::poseidon2_hash_with_separator;
22

33
mod context;
4-
mod uint252;
4+
mod uint253;
55
mod erc20_note;
66
pub(crate) mod note;
77
mod owned_note;
@@ -46,7 +46,7 @@ pub global GENERATOR_INDEX__NOTE_HASH: Field = 3;
4646
// Note: keep in sync with other languages
4747
pub global U256_LIMBS: u32 = 3;
4848

49-
pub type U256 = uint252::U252;
49+
pub type U256 = uint253::U253;
5050

5151
/// User address within the rollup
5252
#[derive(Eq, Serialize)]

packages/contracts/noir/common/src/uint252.nr renamed to packages/contracts/noir/common/src/uint253.nr

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
use std::cmp::{Eq, Ord, Ordering};
55
use std::ops::{Add, Div, Mul, Rem, Sub};
66

7-
// Maximum value for U252 (2^252 - 1), chosen to fit within Aztec's field arithmetic bounds
8-
pub global MAX_U252: Field = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
7+
// Maximum value for U253 (2^253 - 1), chosen to fit within Aztec's field arithmetic bounds
8+
pub global MAX_U253: Field = 0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
99

10-
pub global U252_PACKED_LEN: u32 = 1;
10+
pub global U253_PACKED_LEN: u32 = 1;
1111

12-
pub struct U252 {
12+
pub struct U253 {
1313
value: Field,
1414
}
1515

16-
impl U252 {
16+
impl U253 {
1717
pub fn new(value: Field) -> Self {
18-
value.assert_max_bit_size::<252>();
18+
value.assert_max_bit_size::<253>();
1919
Self { value }
2020
}
2121

@@ -24,7 +24,7 @@ impl U252 {
2424
}
2525

2626
pub fn from_integer(value: Field) -> Self {
27-
value.assert_max_bit_size::<252>();
27+
value.assert_max_bit_size::<253>();
2828
Self { value }
2929
}
3030

@@ -41,7 +41,7 @@ impl U252 {
4141
}
4242

4343
pub fn max() -> Self {
44-
Self { value: MAX_U252 }
44+
Self { value: MAX_U253 }
4545
}
4646

4747
pub fn is_zero(self) -> bool {
@@ -53,17 +53,17 @@ impl U252 {
5353
pub unconstrained fn div_rem_unconstrained(self, other: Self) -> (Self, Self) {
5454
assert(!(other.value == 0), "Division by zero");
5555

56-
self.value.assert_max_bit_size::<252>();
57-
other.value.assert_max_bit_size::<252>();
56+
self.value.assert_max_bit_size::<253>();
57+
other.value.assert_max_bit_size::<253>();
5858

59-
let bits: [u1; 252] = self.value.to_be_bits();
59+
let bits: [u1; 253] = self.value.to_be_bits();
6060
let divisor = other.value;
6161

6262
let mut quotient: Field = 0;
6363
let mut remainder: Field = 0;
6464

6565
// Process each bit from MSB to LSB, similar to paper-and-pencil division
66-
for i in 0..252 {
66+
for i in 0..253 {
6767
// Shift remainder left by 1 bit and add next bit
6868
remainder = remainder * 2 + (bits[i] as Field);
6969

@@ -107,73 +107,72 @@ impl U252 {
107107
}
108108
}
109109

110-
// Adds two U252 values without overflow checks - use with caution
110+
// Adds two U253 values without overflow checks - use with caution
111111
pub fn add_unchecked(self, other: Self) -> Self {
112112
Self { value: self.value + other.value }
113113
}
114114

115-
// Subtracts two U252 values without underflow checks - use with caution
115+
// Subtracts two U253 values without underflow checks - use with caution
116116
pub fn sub_unchecked(self, other: Self) -> Self {
117117
Self { value: self.value - other.value }
118118
}
119119
}
120120

121-
122-
impl Add for U252 {
121+
impl Add for U253 {
123122
fn add(self, other: Self) -> Self {
124123
let result = self.value + other.value;
125-
result.assert_max_bit_size::<252>();
124+
result.assert_max_bit_size::<253>();
126125

127-
assert(!MAX_U252.lt(result), "U252 addition overflow");
128-
assert(!result.lt(self.value), "U252 addition overflow");
129-
assert(!result.lt(other.value), "U252 addition overflow");
126+
assert(!MAX_U253.lt(result), "U253 addition overflow");
127+
assert(!result.lt(self.value), "U253 addition overflow");
128+
assert(!result.lt(other.value), "U253 addition overflow");
130129
Self { value: result }
131130
}
132131
}
133132

134-
impl Sub for U252 {
133+
impl Sub for U253 {
135134
fn sub(self, other: Self) -> Self {
136135
assert(
137136
other.value.lt(self.value) | other.value.eq(self.value),
138-
"U252 subtraction underflow",
137+
"U253 subtraction underflow",
139138
);
140139
let result = self.value - other.value;
141-
result.assert_max_bit_size::<252>();
140+
result.assert_max_bit_size::<253>();
142141
Self { value: result }
143142
}
144143
}
145144

146-
impl Mul for U252 {
145+
impl Mul for U253 {
147146
fn mul(self, other: Self) -> Self {
148147
let result = self.value * other.value;
149148

150-
result.assert_max_bit_size::<252>();
149+
result.assert_max_bit_size::<253>();
151150
// Allow multiplication by 1 without additional checks, otherwise check for overflow
152151
assert(
153152
(self.value == 1)
154153
| (other.value == 1)
155-
| (result.lt(MAX_U252 + 1) & !result.lt(self.value) & !result.lt(other.value)),
156-
"U252 multiplication overflow",
154+
| (result.lt(MAX_U253 + 1) & !result.lt(self.value) & !result.lt(other.value)),
155+
"U253 multiplication overflow",
157156
);
158157
Self { value: result }
159158
}
160159
}
161160

162-
impl Div for U252 {
161+
impl Div for U253 {
163162
fn div(self, other: Self) -> Self {
164163
let (quotient, _) = self.div_rem(other);
165164
quotient
166165
}
167166
}
168167

169-
impl Rem for U252 {
168+
impl Rem for U253 {
170169
fn rem(self, other: Self) -> Self {
171170
let (_, remainder) = self.div_rem(other);
172171
remainder
173172
}
174173
}
175174

176-
impl Ord for U252 {
175+
impl Ord for U253 {
177176
fn cmp(self, other: Self) -> Ordering {
178177
if self.value.lt(other.value) {
179178
Ordering::less()
@@ -185,7 +184,7 @@ impl Ord for U252 {
185184
}
186185
}
187186

188-
impl Eq for U252 {
187+
impl Eq for U253 {
189188
fn eq(self, other: Self) -> bool {
190189
self.value.eq(other.value)
191190
}

0 commit comments

Comments
 (0)