Skip to content

Commit 41c3882

Browse files
feat: export pre-defined bignum types (#125)
Co-authored-by: Tom French <[email protected]>
1 parent 3b5e7c6 commit 41c3882

33 files changed

+282
-222
lines changed

README.md

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,29 @@ bignum can evaluate large integer arithmetic by defining a modulus() that is a p
1515

1616
## High level overview
1717

18-
This library provides modular arithmetic operations for big numbers. The Noir std library provides integers up to 128 bits and a field type up to 254 bits; this library supports arbitrary length numbers.
18+
This library provides modular arithmetic operations for big numbers. The Noir std library provides integers up to 128 bits and a field type up to 254 bits; this library supports arbitrary length numbers.
19+
20+
A number of pre-defined bignum and finite field types are provided. If you need a bignum or finite field that's not on this list, this repo also provides the tools you'll need to generate your own.
21+
22+
See `./src/lib.nr` for the list of exported bignums.
23+
24+
See `./src/fields/` for files which shows how those bignums were created; you can copy this approach to generate your own bignums.
1925

2026
More details about this library are described in the rest of this document, this is just a quick high level overview.
2127

22-
To start using the library you need to do 2 things:
28+
To start using the library:
29+
30+
If the bignum you need is in the pre-defined list, import it and use it:
31+
32+
```rust
33+
use dep::bignum::U256;
34+
```
35+
36+
If the bignum you need is not in the pre-defined list, you'll need to create it:
2337
1. Define or import a **parameter set** with info about your modulus
2438
2. Define the correct **type** for your big number
2539

26-
For step 1, the library contains parameters for predefined fields or integer types. Otherwise, you can define your own parameters; instructions on how to do this can be found below.
40+
Instructions on how to do this can be found below.
2741

2842
Step 2 depends on when you know your modulus; this can be either at compile-time or runtime. Use the correct type for your situation:
2943
* `BigNum`, if modulus is known at compile-time
@@ -48,34 +62,45 @@ In your _Nargo.toml_ file, add the version of this library you would like to ins
4862
bignum = { tag = "v0.4.2", git = "https://github.com/noir-lang/noir-bignum" }
4963
```
5064

51-
### Import
65+
### Import a pre-defined bignum:
5266

5367
Add imports at the top of your Noir code, for example:
5468

5569
```rust
56-
use bignum::fields::U256::U256Params;
57-
use bignum::BigNum;
70+
use dep::bignum::U256;
5871
```
59-
### Quick example: Addition in U256
6072

61-
A simple 1 + 2 = 3 check in 256-bit unsigned integers. Note that for performing multiple arithmetic operations up to degree 2 it is recommended to use `evaluate_quadratic_expression` (see explanation below).
73+
### Create a custom bignum:
6274

63-
```rust
75+
> We use U256 as an illustrative example, even though it's actually a pre-defined bignum.
76+
77+
Use the paramgen tool to generate your bignum's params (see below). Then define your custom bignum from those params:
6478

65-
use bignum::fields::U256::U256Params;
66-
use bignum::BigNum;
79+
```rust
80+
use dep::bignum::fields::U256::U256Params;
81+
use dep::bignum::BigNum;
6782

6883
// Define (compile-time) BigNum type
6984
// number of limbs, number of bits of modulus, parameter set
7085
type U256 = BigNum<3, 257, U256Params>;
86+
```
87+
88+
### Quick example: Addition in U256
89+
90+
A simple 1 + 2 = 3 check in 256-bit unsigned integers. Note that for performing multiple arithmetic operations up to degree 2 it is recommended to use `evaluate_quadratic_expression` (see explanation below).
91+
92+
```rust
93+
use dep::bignum::U256;
7194

7295
fn main() {
73-
let one: U256 = BigNum::from_slice([1, 0, 0]);
74-
let two: U256 = BigNum::from_slice([2, 0, 0]);
75-
let three: U256 = BigNum::from_slice([3, 0, 0]);
96+
let one = U256::from_slice([1, 0, 0]);
97+
let two = U256::from_slice([2, 0, 0]);
98+
let three = U256::from_slice([3, 0, 0]);
7699
assert((one + two) == three);
77100
}
78101
```
102+
103+
79104
## Types
80105

81106
### `BigNum` / `RuntimeBigNum` definition

src/fields/U1024.nr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
use crate::bignum::BigNum;
12
use crate::params::BigNumParams;
23
use crate::params::BigNumParamsGetter;
34

45
pub struct U1024Params {}
56

7+
pub type U1024 = BigNum<9, 1025, U1024Params>;
8+
69
impl BigNumParamsGetter<9, 1025> for U1024Params {
710
fn get_params() -> BigNumParams<9, 1025> {
811
U1024_PARAMS
912
}
1013
}
1114

12-
global U1024_PARAMS: BigNumParams<9, 1025> = BigNumParams {
15+
pub global U1024_PARAMS: BigNumParams<9, 1025> = BigNumParams {
1316
has_multiplicative_inverse: false,
1417
modulus: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x010000000000000000],
1518
double_modulus: [

src/fields/U2048.nr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
use crate::bignum::BigNum;
12
use crate::params::BigNumParams;
23
use crate::params::BigNumParamsGetter;
34

45
pub struct U2048Params {}
56

7+
pub type U2048 = BigNum<18, 2049, U2048Params>;
8+
69
impl BigNumParamsGetter<18, 2049> for U2048Params {
710
fn get_params() -> BigNumParams<18, 2049> {
811
U2048_PARAMS
912
}
1013
}
1114

12-
global U2048_PARAMS: BigNumParams<18, 2049> = BigNumParams {
15+
pub global U2048_PARAMS: BigNumParams<18, 2049> = BigNumParams {
1316
has_multiplicative_inverse: false,
1417
modulus: [
1518
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

src/fields/U256.nr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
use crate::bignum::BigNum;
12
use crate::params::BigNumParams;
23
use crate::params::BigNumParamsGetter;
34

45
pub struct U256Params {}
56

7+
pub type U256 = BigNum<3, 257, U256Params>;
8+
69
impl BigNumParamsGetter<3, 257> for U256Params {
710
fn get_params() -> BigNumParams<3, 257> {
811
U256_PARAMS
912
}
1013
}
1114

12-
global U256_PARAMS: BigNumParams<3, 257> = BigNumParams {
15+
pub global U256_PARAMS: BigNumParams<3, 257> = BigNumParams {
1316
has_multiplicative_inverse: false,
1417
modulus: [0x00, 0x00, 0x010000],
1518
double_modulus: [

src/fields/U384.nr

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
use crate::bignum::BigNum;
12
use crate::params::BigNumParams;
23
use crate::params::BigNumParamsGetter;
34

4-
pub struct U384_Params {}
5+
pub struct U384Params {}
56

6-
impl BigNumParamsGetter<4, 385> for U384_Params {
7+
pub type U384 = BigNum<4, 385, U384Params>;
8+
9+
impl BigNumParamsGetter<4, 385> for U384Params {
710
fn get_params() -> BigNumParams<4, 385> {
811
U384_PARAMS
912
}
1013
}
1114

12-
global U384_PARAMS: BigNumParams<4, 385> = BigNumParams {
15+
pub global U384_PARAMS: BigNumParams<4, 385> = BigNumParams {
1316
has_multiplicative_inverse: false,
1417
modulus: [0x00, 0x00, 0x00, 0x01000000],
1518
double_modulus: [

src/fields/U4096.nr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
use crate::bignum::BigNum;
12
use crate::params::BigNumParams;
23
use crate::params::BigNumParamsGetter;
34

45
pub struct U4096Params {}
56

7+
pub type U4096 = BigNum<35, 4097, U4096Params>;
8+
69
impl BigNumParamsGetter<35, 4097> for U4096Params {
710
fn get_params() -> BigNumParams<35, 4097> {
811
U4096_PARAMS
912
}
1013
}
1114

12-
global U4096_PARAMS: BigNumParams<35, 4097> = BigNumParams {
15+
pub global U4096_PARAMS: BigNumParams<35, 4097> = BigNumParams {
1316
has_multiplicative_inverse: false,
1417
modulus: [
1518
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

src/fields/U512.nr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
use crate::bignum::BigNum;
12
use crate::params::BigNumParams;
23
use crate::params::BigNumParamsGetter;
34

45
pub struct U512Params {}
56

7+
pub type U512 = BigNum<5, 513, U512Params>;
8+
69
impl BigNumParamsGetter<5, 513> for U512Params {
710
fn get_params() -> BigNumParams<5, 513> {
811
U512_PARAMS
912
}
1013
}
1114

12-
global U512_PARAMS: BigNumParams<5, 513> = BigNumParams {
15+
pub global U512_PARAMS: BigNumParams<5, 513> = BigNumParams {
1316
has_multiplicative_inverse: false,
1417
modulus: [0x00, 0x00, 0x00, 0x00, 0x0100000000],
1518
double_modulus: [

src/fields/U768.nr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
use crate::bignum::BigNum;
12
use crate::params::BigNumParams;
23
use crate::params::BigNumParamsGetter;
34

45
pub struct U768Params {}
56

7+
pub type U768 = BigNum<7, 769, U768Params>;
8+
69
impl BigNumParamsGetter<7, 769> for U768Params {
710
fn get_params() -> BigNumParams<7, 769> {
811
U768_PARAMS
912
}
1013
}
1114

12-
global U768_PARAMS: BigNumParams<7, 769> = BigNumParams {
15+
pub global U768_PARAMS: BigNumParams<7, 769> = BigNumParams {
1316
has_multiplicative_inverse: false,
1417
modulus: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01000000000000],
1518
double_modulus: [

src/fields/U8192.nr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use crate::bignum::BigNum;
12
use crate::params::BigNumParams;
23
use crate::params::BigNumParamsGetter;
34

45
pub struct U8192Params {}
56

7+
pub type U8192 = BigNum<35, 4097, U8192Params>;
8+
69
impl BigNumParamsGetter<69, 8193> for U8192Params {
710
fn get_params() -> BigNumParams<69, 8193> {
811
U8192_PARAMS

src/fields/bls12_377Fq.nr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
//! * G1 curve equation: y^2 = x^3 + 1
1717
//! * G2 curve equation: y^2 = x^3 + B, where
1818
//! * B = Fq2(0, 155198655607781456406391640216936120121836107652948796323930557600032281009004493664981332883744016074664192874906)
19+
use crate::bignum::BigNum;
1920
use crate::params::BigNumParams;
2021
use crate::params::BigNumParamsGetter;
2122

2223
pub struct BLS12_377_Fq_Params {}
2324

25+
pub type BLS12_377_Fq = BigNum<4, 377, BLS12_377_Fq_Params>;
26+
2427
impl BigNumParamsGetter<4, 377> for BLS12_377_Fq_Params {
2528
fn get_params() -> BigNumParams<4, 377> {
2629
BLS12_377_Fq_PARAMS

0 commit comments

Comments
 (0)