Skip to content

Commit 78e7216

Browse files
kashbrtiKhashayar BarootiTomAFrench
authored
feat!: refactor library to work on u128 limbs (#120)
Co-authored-by: Khashayar Barooti <[email protected]> Co-authored-by: Tom French <[email protected]>
1 parent e338c51 commit 78e7216

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+899
-2512
lines changed

.github/workflows/benchmark.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ jobs:
1717
- name: Install Nargo
1818
uses: noir-lang/[email protected]
1919
with:
20-
toolchain: 1.0.0-beta.0
20+
toolchain: 1.0.0-beta.3
2121

2222
- name: Install bb
2323
run: |
2424
npm install -g bbup
25-
bbup -nv 1.0.0-beta.0
25+
bbup -nv 1.0.0-beta.3
2626
sudo apt install libc++-dev
2727
2828
- name: Build Noir benchmark programs

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
env:
1010
CARGO_TERM_COLOR: always
11-
MINIMUM_NOIR_VERSION: v1.0.0-beta.0
11+
MINIMUM_NOIR_VERSION: v1.0.0-beta.3
1212

1313
jobs:
1414
noir-version-list:
@@ -85,4 +85,4 @@ jobs:
8585
fi
8686
env:
8787
# We treat any cancelled, skipped or failing jobs as a failure for the workflow as a whole.
88-
FAIL: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') || contains(needs.*.result, 'skipped') }}
88+
FAIL: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') || contains(needs.*.result, 'skipped') }}

src/bignum.nr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::fns::{
1818
use std::ops::{Add, Div, Mul, Neg, Sub};
1919

2020
pub struct BigNum<let N: u32, let MOD_BITS: u32, Params> {
21-
pub limbs: [Field; N],
21+
pub limbs: [u128; N],
2222
}
2323
// We aim to avoid needing to add a generic parameter to this trait, for this reason we do not allow
2424
// accessing the limbs of the bignum except through slices.
@@ -30,16 +30,16 @@ pub trait BigNumTrait: Neg + Add + Sub + Mul + Div + Eq {
3030
fn one() -> Self;
3131
fn derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self;
3232
unconstrained fn __derive_from_seed<let SeedBytes: u32>(seed: [u8; SeedBytes]) -> Self;
33-
fn from_slice(limbs: [Field]) -> Self;
33+
fn from_slice(limbs: [u128]) -> Self;
3434
fn from_be_bytes<let NBytes: u32>(x: [u8; NBytes]) -> Self;
3535
fn to_le_bytes<let NBytes: u32>(self) -> [u8; NBytes];
3636

3737
fn modulus() -> Self;
3838
fn modulus_bits(self) -> u32;
3939
fn num_limbs(self) -> u32;
40-
fn get_limbs_slice(self) -> [Field];
41-
fn get_limb(self, idx: u32) -> Field;
42-
fn set_limb(&mut self, idx: u32, value: Field);
40+
fn get_limbs_slice(self) -> [u128];
41+
fn get_limb(self, idx: u32) -> u128;
42+
fn set_limb(&mut self, idx: u32, value: u128);
4343

4444
unconstrained fn __eq(self, other: Self) -> bool;
4545
unconstrained fn __is_zero(self) -> bool;
@@ -138,7 +138,7 @@ where
138138
Self { limbs: __derive_from_seed::<_, MOD_BITS, _>(params, seed) }
139139
}
140140

141-
fn from_slice(limbs: [Field]) -> Self {
141+
fn from_slice(limbs: [u128]) -> Self {
142142
Self { limbs: limbs.as_array() }
143143
}
144144

@@ -162,15 +162,15 @@ where
162162
N
163163
}
164164

165-
fn get_limbs_slice(self) -> [Field] {
165+
fn get_limbs_slice(self) -> [u128] {
166166
self.limbs
167167
}
168168

169-
fn get_limb(self, idx: u32) -> Field {
169+
fn get_limb(self, idx: u32) -> u128 {
170170
self.limbs[idx]
171171
}
172172

173-
fn set_limb(&mut self, idx: u32, value: Field) {
173+
fn set_limb(&mut self, idx: u32, value: u128) {
174174
self.limbs[idx] = value;
175175
}
176176

@@ -292,7 +292,7 @@ where
292292
}
293293

294294
fn validate_in_range(self) {
295-
validate_in_range::<_, MOD_BITS>(self.limbs);
295+
validate_in_range::<_, _, MOD_BITS>(self.limbs);
296296
}
297297

298298
fn assert_is_not_equal(self, other: Self) {

src/fields/U1024.nr

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::params::BigNumParams;
22
use crate::params::BigNumParamsGetter;
3-
use crate::utils::u60_representation::U60Repr;
43

54
pub struct U1024Params {}
65

@@ -24,18 +23,5 @@ global U1024_PARAMS: BigNumParams<9, 1025> = BigNumParams {
2423
0xffffffffffffffffffffffffffffff,
2524
0x01ffffffffffffffff,
2625
],
27-
modulus_u60: U60Repr {
28-
limbs: [
29-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30-
0x00, 0x00, 0x00, 0x10,
31-
],
32-
},
33-
modulus_u60_x4: U60Repr {
34-
limbs: [
35-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
36-
0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38-
],
39-
},
4026
redc_param: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x400000000000000000],
4127
};

src/fields/U2048.nr

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::params::BigNumParams;
22
use crate::params::BigNumParamsGetter;
3-
use crate::utils::u60_representation::U60Repr;
43

54
pub struct U2048Params {}
65

@@ -36,23 +35,6 @@ global U2048_PARAMS: BigNumParams<18, 2049> = BigNumParams {
3635
0xffffffffffffffffffffffffffffff,
3736
0x01ff,
3837
],
39-
modulus_u60: U60Repr {
40-
limbs: [
41-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0100, 0x00,
44-
],
45-
},
46-
modulus_u60_x4: U60Repr {
47-
limbs: [
48-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0100, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53-
0x00, 0x00,
54-
],
55-
},
5638
redc_param: [
5739
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5840
0x00, 0x00, 0x4000,

src/fields/U256.nr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::params::BigNumParams;
22
use crate::params::BigNumParamsGetter;
3-
use crate::utils::u60_representation::U60Repr;
43

54
pub struct U256Params {}
65

@@ -18,9 +17,5 @@ global U256_PARAMS: BigNumParams<3, 257> = BigNumParams {
1817
0xffffffffffffffffffffffffffffff,
1918
0x01ffff,
2019
],
21-
modulus_u60: U60Repr { limbs: [0x00, 0x00, 0x00, 0x00, 0x010000, 0x00] },
22-
modulus_u60_x4: U60Repr {
23-
limbs: [0x00, 0x00, 0x00, 0x00, 0x010000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
24-
},
2520
redc_param: [0x00, 0x00, 0x400000],
2621
};

src/fields/U384.nr

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::params::BigNumParams;
22
use crate::params::BigNumParamsGetter;
3-
use crate::utils::u60_representation::U60Repr;
43

54
pub struct U384_Params {}
65

@@ -19,12 +18,5 @@ global U384_PARAMS: BigNumParams<4, 385> = BigNumParams {
1918
0xffffffffffffffffffffffffffffff,
2019
0x01ffffff,
2120
],
22-
modulus_u60: U60Repr { limbs: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01000000, 0x00] },
23-
modulus_u60_x4: U60Repr {
24-
limbs: [
25-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01000000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
26-
0x00, 0x00, 0x00,
27-
],
28-
},
2921
redc_param: [0x00, 0x00, 0x00, 0x40000000],
3022
};

src/fields/U4096.nr

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::params::BigNumParams;
22
use crate::params::BigNumParamsGetter;
3-
use crate::utils::u60_representation::U60Repr;
43

54
pub struct U4096Params {}
65

@@ -54,29 +53,6 @@ global U4096_PARAMS: BigNumParams<35, 4097> = BigNumParams {
5453
0xffffffffffffffffffffffffffffff,
5554
0x01ffff,
5655
],
57-
modulus_u60: U60Repr {
58-
limbs: [
59-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x010000, 0x00,
64-
],
65-
},
66-
modulus_u60_x4: U60Repr {
67-
limbs: [
68-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x010000, 0x00,
73-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78-
],
79-
},
8056
redc_param: [
8157
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8258
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

src/fields/U512.nr

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::params::BigNumParams;
22
use crate::params::BigNumParamsGetter;
3-
use crate::utils::u60_representation::U60Repr;
43

54
pub struct U512Params {}
65

@@ -20,32 +19,5 @@ global U512_PARAMS: BigNumParams<5, 513> = BigNumParams {
2019
0xffffffffffffffffffffffffffffff,
2120
0x01ffffffff,
2221
],
23-
modulus_u60: U60Repr {
24-
limbs: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0100000000, 0x00],
25-
},
26-
modulus_u60_x4: U60Repr {
27-
limbs: [
28-
0x00,
29-
0x00,
30-
0x00,
31-
0x00,
32-
0x00,
33-
0x00,
34-
0x00,
35-
0x00,
36-
0x0100000000,
37-
0x00,
38-
0x00,
39-
0x00,
40-
0x00,
41-
0x00,
42-
0x00,
43-
0x00,
44-
0x00,
45-
0x00,
46-
0x00,
47-
0x00,
48-
],
49-
},
5022
redc_param: [0x00, 0x00, 0x00, 0x00, 0x4000000000],
5123
};

src/fields/U768.nr

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::params::BigNumParams;
22
use crate::params::BigNumParamsGetter;
3-
use crate::utils::u60_representation::U60Repr;
43

54
pub struct U768Params {}
65

@@ -22,55 +21,5 @@ global U768_PARAMS: BigNumParams<7, 769> = BigNumParams {
2221
0xffffffffffffffffffffffffffffff,
2322
0x01ffffffffffff,
2423
],
25-
modulus_u60: U60Repr {
26-
limbs: [
27-
0x00,
28-
0x00,
29-
0x00,
30-
0x00,
31-
0x00,
32-
0x00,
33-
0x00,
34-
0x00,
35-
0x00,
36-
0x00,
37-
0x00,
38-
0x00,
39-
0x01000000000000,
40-
0x00,
41-
],
42-
},
43-
modulus_u60_x4: U60Repr {
44-
limbs: [
45-
0x00,
46-
0x00,
47-
0x00,
48-
0x00,
49-
0x00,
50-
0x00,
51-
0x00,
52-
0x00,
53-
0x00,
54-
0x00,
55-
0x00,
56-
0x00,
57-
0x01000000000000,
58-
0x00,
59-
0x00,
60-
0x00,
61-
0x00,
62-
0x00,
63-
0x00,
64-
0x00,
65-
0x00,
66-
0x00,
67-
0x00,
68-
0x00,
69-
0x00,
70-
0x00,
71-
0x00,
72-
0x00,
73-
],
74-
},
7524
redc_param: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40000000000000],
7625
};

0 commit comments

Comments
 (0)