Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,29 @@ bignum can evaluate large integer arithmetic by defining a modulus() that is a p

## High level overview

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.
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.

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.

See `./src/lib.nr` for the list of exported bignums.

See `./src/fields/` for files which shows how those bignums were created; you can copy this approach to generate your own bignums.

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

To start using the library you need to do 2 things:
To start using the library:

If the bignum you need is in the pre-defined list, import it and use it:

```rust
use dep::bignum::U256;
```

If the bignum you need is not in the pre-defined list, you'll need to create it:
1. Define or import a **parameter set** with info about your modulus
2. Define the correct **type** for your big number

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.
Instructions on how to do this can be found below.

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:
* `BigNum`, if modulus is known at compile-time
Expand All @@ -48,34 +62,45 @@ In your _Nargo.toml_ file, add the version of this library you would like to ins
bignum = { tag = "v0.4.2", git = "https://github.com/noir-lang/noir-bignum" }
```

### Import
### Import a pre-defined bignum:

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

```rust
use bignum::fields::U256::U256Params;
use bignum::BigNum;
use dep::bignum::U256;
```
### Quick example: Addition in U256

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).
### Create a custom bignum:

```rust
> We use U256 as an illustrative example, even though it's actually a pre-defined bignum.

Use the paramgen tool to generate your bignum's params (see below). Then define your custom bignum from those params:

use bignum::fields::U256::U256Params;
use bignum::BigNum;
```rust
use dep::bignum::fields::U256::U256Params;
use dep::bignum::BigNum;

// Define (compile-time) BigNum type
// number of limbs, number of bits of modulus, parameter set
type U256 = BigNum<3, 257, U256Params>;
```

### Quick example: Addition in U256

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).

```rust
use dep::bignum::U256;

fn main() {
let one: U256 = BigNum::from_slice([1, 0, 0]);
let two: U256 = BigNum::from_slice([2, 0, 0]);
let three: U256 = BigNum::from_slice([3, 0, 0]);
let one = U256::from_slice([1, 0, 0]);
let two = U256::from_slice([2, 0, 0]);
let three = U256::from_slice([3, 0, 0]);
assert((one + two) == three);
}
```


## Types

### `BigNum` / `RuntimeBigNum` definition
Expand Down
5 changes: 4 additions & 1 deletion src/fields/U1024.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct U1024Params {}

pub type U1024 = BigNum<9, 1025, U1024Params>;

impl BigNumParamsGetter<9, 1025> for U1024Params {
fn get_params() -> BigNumParams<9, 1025> {
U1024_PARAMS
}
}

global U1024_PARAMS: BigNumParams<9, 1025> = BigNumParams {
pub global U1024_PARAMS: BigNumParams<9, 1025> = BigNumParams {
has_multiplicative_inverse: false,
modulus: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x010000000000000000],
double_modulus: [
Expand Down
5 changes: 4 additions & 1 deletion src/fields/U2048.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct U2048Params {}

pub type U2048 = BigNum<18, 2049, U2048Params>;

impl BigNumParamsGetter<18, 2049> for U2048Params {
fn get_params() -> BigNumParams<18, 2049> {
U2048_PARAMS
}
}

global U2048_PARAMS: BigNumParams<18, 2049> = BigNumParams {
pub global U2048_PARAMS: BigNumParams<18, 2049> = BigNumParams {
has_multiplicative_inverse: false,
modulus: [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down
5 changes: 4 additions & 1 deletion src/fields/U256.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct U256Params {}

pub type U256 = BigNum<3, 257, U256Params>;

impl BigNumParamsGetter<3, 257> for U256Params {
fn get_params() -> BigNumParams<3, 257> {
U256_PARAMS
}
}

global U256_PARAMS: BigNumParams<3, 257> = BigNumParams {
pub global U256_PARAMS: BigNumParams<3, 257> = BigNumParams {
has_multiplicative_inverse: false,
modulus: [0x00, 0x00, 0x010000],
double_modulus: [
Expand Down
9 changes: 6 additions & 3 deletions src/fields/U384.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct U384_Params {}
pub struct U384Params {}

impl BigNumParamsGetter<4, 385> for U384_Params {
pub type U384 = BigNum<4, 385, U384Params>;

impl BigNumParamsGetter<4, 385> for U384Params {
fn get_params() -> BigNumParams<4, 385> {
U384_PARAMS
}
}

global U384_PARAMS: BigNumParams<4, 385> = BigNumParams {
pub global U384_PARAMS: BigNumParams<4, 385> = BigNumParams {
has_multiplicative_inverse: false,
modulus: [0x00, 0x00, 0x00, 0x01000000],
double_modulus: [
Expand Down
5 changes: 4 additions & 1 deletion src/fields/U4096.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct U4096Params {}

pub type U4096 = BigNum<35, 4097, U4096Params>;

impl BigNumParamsGetter<35, 4097> for U4096Params {
fn get_params() -> BigNumParams<35, 4097> {
U4096_PARAMS
}
}

global U4096_PARAMS: BigNumParams<35, 4097> = BigNumParams {
pub global U4096_PARAMS: BigNumParams<35, 4097> = BigNumParams {
has_multiplicative_inverse: false,
modulus: [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down
6 changes: 4 additions & 2 deletions src/fields/U512.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct U512Params {}

pub type U512 = BigNum<5, 513, U512Params>;

impl BigNumParamsGetter<5, 513> for U512Params {
fn get_params() -> BigNumParams<5, 513> {
U512_PARAMS
}
}

global U512_PARAMS: BigNumParams<5, 513> = BigNumParams {
pub global U512_PARAMS: BigNumParams<5, 513> = BigNumParams {
has_multiplicative_inverse: false,
modulus: [0x00, 0x00, 0x00, 0x00, 0x0100000000],
double_modulus: [
Expand Down Expand Up @@ -49,4 +52,3 @@ global U512_PARAMS: BigNumParams<5, 513> = BigNumParams {
},
redc_param: [0x00, 0x00, 0x00, 0x00, 0x4000000000],
};

5 changes: 4 additions & 1 deletion src/fields/U768.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct U768Params {}

pub type U768 = BigNum<7, 769, U768Params>;

impl BigNumParamsGetter<7, 769> for U768Params {
fn get_params() -> BigNumParams<7, 769> {
U768_PARAMS
}
}

global U768_PARAMS: BigNumParams<7, 769> = BigNumParams {
pub global U768_PARAMS: BigNumParams<7, 769> = BigNumParams {
has_multiplicative_inverse: false,
modulus: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01000000000000],
double_modulus: [
Expand Down
3 changes: 3 additions & 0 deletions src/fields/U8192.nr
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct U8192Params {}

pub type U8192 = BigNum<35, 4097, U8192Params>;

impl BigNumParamsGetter<69, 8193> for U8192Params {
fn get_params() -> BigNumParams<69, 8193> {
U8192_PARAMS
Expand Down
3 changes: 3 additions & 0 deletions src/fields/bls12_377Fq.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
//! * G1 curve equation: y^2 = x^3 + 1
//! * G2 curve equation: y^2 = x^3 + B, where
//! * B = Fq2(0, 155198655607781456406391640216936120121836107652948796323930557600032281009004493664981332883744016074664192874906)
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct BLS12_377_Fq_Params {}

pub type BLS12_377_Fq = BigNum<4, 377, BLS12_377_Fq_Params>;

impl BigNumParamsGetter<4, 377> for BLS12_377_Fq_Params {
fn get_params() -> BigNumParams<4, 377> {
BLS12_377_Fq_PARAMS
Expand Down
5 changes: 4 additions & 1 deletion src/fields/bls12_377Fr.nr
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@
//! * G2 curve equation: y^2 = x^3 + B, where
//! * B = Fq2(0, 155198655607781456406391640216936120121836107652948796323930557600032281009004493664981332883744016074664192874906)

use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct BLS12_377_Fr_Params {}

pub type BLS12_377_Fr = BigNum<3, 253, BLS12_377_Fr_Params>;

impl BigNumParamsGetter<3, 253> for BLS12_377_Fr_Params {
fn get_params() -> BigNumParams<3, 253> {
BLS12_377_Fr_PARAMS
}
}

global BLS12_377_Fr_PARAMS: BigNumParams<3, 253> = BigNumParams {
pub global BLS12_377_Fr_PARAMS: BigNumParams<3, 253> = BigNumParams {
has_multiplicative_inverse: true,
modulus: [0xaa76fed00000010a11800000000001, 0x655e9a2ca55660b44d1e5c37b00159, 0x12ab],
double_modulus: [
Expand Down
5 changes: 4 additions & 1 deletion src/fields/bls12_381Fq.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@
//! * valuation(r - 1, 2) = 32
//! * G1 curve equation: y^2 = x^3 + 4
//! * G2 curve equation: y^2 = x^3 + Fq2(4, 4)
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct BLS12_381_Fq_Params {}

pub type BLS12_381_Fq = BigNum<4, 381, BLS12_381_Fq_Params>;

impl BigNumParamsGetter<4, 381> for BLS12_381_Fq_Params {
fn get_params() -> BigNumParams<4, 381> {
BLS12_381_Fq_PARAMS
}
}

global BLS12_381_Fq_PARAMS: BigNumParams<4, 381> = BigNumParams {
pub global BLS12_381_Fq_PARAMS: BigNumParams<4, 381> = BigNumParams {
has_multiplicative_inverse: true,
modulus: [
0xabfffeb153ffffb9feffffffffaaab,
Expand Down
7 changes: 5 additions & 2 deletions src/fields/bls12_381Fr.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Blurb sourced from https://github.com/arkworks-rs
//! This library implements the base field of the BLS12_381 curve generated by [Sean Bowe](https://electriccoin.co/blog/new-snark-curve/).
//! This library implements the scalar field of the BLS12_381 curve generated by [Sean Bowe](https://electriccoin.co/blog/new-snark-curve/).
//! The name denotes that it is a Barreto--Lynn--Scott curve of embedding degree
//! 12, defined over a 381-bit (prime) field.
//! This curve was intended to replace the BN254 curve to provide a higher
Expand All @@ -14,19 +14,22 @@
//! * valuation(r - 1, 2) = 32
//! * G1 curve equation: y^2 = x^3 + 4
//! * G2 curve equation: y^2 = x^3 + Fq2(4, 4)
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct BLS12_381_Fr_Params {}

pub type BLS12_381_Fr = BigNum<3, 255, BLS12_381_Fr_Params>;

impl BigNumParamsGetter<3, 255> for BLS12_381_Fr_Params {
fn get_params() -> BigNumParams<3, 255> {
BLS12_381_Fr_PARAMS
}
}

global BLS12_381_Fr_PARAMS: BigNumParams<3, 255> = BigNumParams {
pub global BLS12_381_Fr_PARAMS: BigNumParams<3, 255> = BigNumParams {
has_multiplicative_inverse: true,
modulus: [0xbda402fffe5bfeffffffff00000001, 0xa753299d7d483339d80809a1d80553, 0x73ed],
double_modulus: [
Expand Down
5 changes: 4 additions & 1 deletion src/fields/bn254Fq.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct BN254_Fq_Params {}

pub type BN254_Fq = BigNum<3, 254, BN254_Fq_Params>;

impl BigNumParamsGetter<3, 254> for BN254_Fq_Params {
fn get_params() -> BigNumParams<3, 254> {
BN254_Fq_PARAMS
}
}

global BN254_Fq_PARAMS: BigNumParams<3, 254> = BigNumParams {
pub global BN254_Fq_PARAMS: BigNumParams<3, 254> = BigNumParams {
has_multiplicative_inverse: true,
modulus: [0x816a916871ca8d3c208c16d87cfd47, 0x4e72e131a029b85045b68181585d97, 0x3064],
double_modulus: [
Expand Down
5 changes: 4 additions & 1 deletion src/fields/ed25519Fq.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use crate::bignum::BigNum;
use crate::params::BigNumParams;
use crate::params::BigNumParamsGetter;
use crate::utils::u60_representation::U60Repr;

pub struct ED25519_Fq_Params {}

pub type ED25519_Fq = BigNum<3, 255, ED25519_Fq_Params>;

impl BigNumParamsGetter<3, 255> for ED25519_Fq_Params {
fn get_params() -> BigNumParams<3, 255> {
ED25519_Fq_PARAMS
}
}

global ED25519_Fq_PARAMS: BigNumParams<3, 255> = BigNumParams {
pub global ED25519_Fq_PARAMS: BigNumParams<3, 255> = BigNumParams {
has_multiplicative_inverse: true,
modulus: [0xffffffffffffffffffffffffffffed, 0xffffffffffffffffffffffffffffff, 0x7fff],
double_modulus: [
Expand Down
Loading