diff --git a/README.md b/README.md index c938cfc7..e798fb0c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/fields/U1024.nr b/src/fields/U1024.nr index 0e2110f8..b92c5fc5 100644 --- a/src/fields/U1024.nr +++ b/src/fields/U1024.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; 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: [ diff --git a/src/fields/U2048.nr b/src/fields/U2048.nr index ca83aceb..7f61b19f 100644 --- a/src/fields/U2048.nr +++ b/src/fields/U2048.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; 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, diff --git a/src/fields/U256.nr b/src/fields/U256.nr index 18b85be0..e556ba7a 100644 --- a/src/fields/U256.nr +++ b/src/fields/U256.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; 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: [ diff --git a/src/fields/U384.nr b/src/fields/U384.nr index 40701cb0..fcbc1ab8 100644 --- a/src/fields/U384.nr +++ b/src/fields/U384.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; -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: [ diff --git a/src/fields/U4096.nr b/src/fields/U4096.nr index 1b211f86..87e8beb4 100644 --- a/src/fields/U4096.nr +++ b/src/fields/U4096.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; 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, diff --git a/src/fields/U512.nr b/src/fields/U512.nr index 86df95bf..b86f064f 100644 --- a/src/fields/U512.nr +++ b/src/fields/U512.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; 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: [ diff --git a/src/fields/U768.nr b/src/fields/U768.nr index 7762913d..bb7d54a8 100644 --- a/src/fields/U768.nr +++ b/src/fields/U768.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; 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: [ diff --git a/src/fields/U8192.nr b/src/fields/U8192.nr index adfed4f9..ed6612ce 100644 --- a/src/fields/U8192.nr +++ b/src/fields/U8192.nr @@ -1,8 +1,11 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct U8192Params {} +pub type U8192 = BigNum<35, 4097, U8192Params>; + impl BigNumParamsGetter<69, 8193> for U8192Params { fn get_params() -> BigNumParams<69, 8193> { U8192_PARAMS diff --git a/src/fields/bls12_377Fq.nr b/src/fields/bls12_377Fq.nr index 977378a9..19358559 100644 --- a/src/fields/bls12_377Fq.nr +++ b/src/fields/bls12_377Fq.nr @@ -16,11 +16,14 @@ //! * 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; 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 diff --git a/src/fields/bls12_377Fr.nr b/src/fields/bls12_377Fr.nr index 64824f5c..8b098c41 100644 --- a/src/fields/bls12_377Fr.nr +++ b/src/fields/bls12_377Fr.nr @@ -17,18 +17,21 @@ //! * 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; 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: [ diff --git a/src/fields/bls12_381Fq.nr b/src/fields/bls12_381Fq.nr index 895777ac..dd2a4825 100644 --- a/src/fields/bls12_381Fq.nr +++ b/src/fields/bls12_381Fq.nr @@ -14,18 +14,21 @@ //! * 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; 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, diff --git a/src/fields/bls12_381Fr.nr b/src/fields/bls12_381Fr.nr index 099525d4..03b9db74 100644 --- a/src/fields/bls12_381Fr.nr +++ b/src/fields/bls12_381Fr.nr @@ -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 @@ -14,18 +14,21 @@ //! * 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; 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: [ diff --git a/src/fields/bn254Fq.nr b/src/fields/bn254Fq.nr index ff1cc27d..ce04241f 100644 --- a/src/fields/bn254Fq.nr +++ b/src/fields/bn254Fq.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; 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: [ diff --git a/src/fields/ed25519Fq.nr b/src/fields/ed25519Fq.nr index eb4c47fe..e3275d8f 100644 --- a/src/fields/ed25519Fq.nr +++ b/src/fields/ed25519Fq.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; 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: [ diff --git a/src/fields/ed25519Fr.nr b/src/fields/ed25519Fr.nr index 6d1d1279..d8d111b0 100644 --- a/src/fields/ed25519Fr.nr +++ b/src/fields/ed25519Fr.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct ED25519_Fr_Params {} +pub type ED25519_Fr = BigNum<3, 255, ED25519_Fr_Params>; + impl BigNumParamsGetter<3, 255> for ED25519_Fr_Params { fn get_params() -> BigNumParams<3, 255> { ED25519_Fr_PARAMS } } -global ED25519_Fr_PARAMS: BigNumParams<3, 255> = BigNumParams { +pub global ED25519_Fr_PARAMS: BigNumParams<3, 255> = BigNumParams { has_multiplicative_inverse: true, modulus: [0xdef9dea2f79cd65812631a5cf5d3ed, 0x14, 0x1000], double_modulus: [ diff --git a/src/fields/mnt4_753Fq.nr b/src/fields/mnt4_753Fq.nr index 5eb4f44d..e9e77d91 100644 --- a/src/fields/mnt4_753Fq.nr +++ b/src/fields/mnt4_753Fq.nr @@ -19,18 +19,21 @@ //! * B = Fq2(0, b * NON_RESIDUE) //! * NON_RESIDUE = 13 is the quadratic non-residue used to conpub struct the //! extension field Fq2 +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct MNT4_753_Fq_Params {} +pub type MNT4_753_Fq = BigNum<7, 753, MNT4_753_Fq_Params>; + impl BigNumParamsGetter<7, 753> for MNT4_753_Fq_Params { fn get_params() -> BigNumParams<7, 753> { MNT4_753_Fq_PARAMS } } -global MNT4_753_Fq_PARAMS: BigNumParams<7, 753> = BigNumParams { +pub global MNT4_753_Fq_PARAMS: BigNumParams<7, 753> = BigNumParams { has_multiplicative_inverse: true, modulus: [ 0x9d54522cdd119f5e9063de245e8001, diff --git a/src/fields/mnt4_753Fr.nr b/src/fields/mnt4_753Fr.nr index 8cba2843..616c7574 100644 --- a/src/fields/mnt4_753Fr.nr +++ b/src/fields/mnt4_753Fr.nr @@ -19,18 +19,21 @@ //! * B = Fq2(0, b * NON_RESIDUE) //! * NON_RESIDUE = 13 is the quadratic non-residue used to conpub struct the //! extension field Fq2 +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct MNT4_753_Fr_Params {} +pub type MNT4_753_Fr = BigNum<7, 753, MNT4_753_Fr_Params>; + impl BigNumParamsGetter<7, 753> for MNT4_753_Fr_Params { fn get_params() -> BigNumParams<7, 753> { MNT4_753_Fr_PARAMS } } -global MNT4_753_Fr_PARAMS: BigNumParams<7, 753> = BigNumParams { +pub global MNT4_753_Fr_PARAMS: BigNumParams<7, 753> = BigNumParams { has_multiplicative_inverse: true, modulus: [ 0xa099170fa13a4fd90776e240000001, diff --git a/src/fields/mnt6_753Fq.nr b/src/fields/mnt6_753Fq.nr index 5cfd0dfb..f33d9a82 100644 --- a/src/fields/mnt6_753Fq.nr +++ b/src/fields/mnt6_753Fq.nr @@ -19,18 +19,21 @@ //! * B = Fq3(b * NON_RESIDUE, 0, 0) //! * NON_RESIDUE = 11 is the cubic non-residue used to conpub struct the //! extension field Fq3 +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct MNT6_753_Fq_Params {} +pub type MNT6_753_Fq = BigNum<7, 753, MNT6_753_Fq_Params>; + impl BigNumParamsGetter<7, 753> for MNT6_753_Fq_Params { fn get_params() -> BigNumParams<7, 753> { MNT6_753_Fq_PARAMS } } -global MNT6_753_Fq_PARAMS: BigNumParams<7, 753> = BigNumParams { +pub global MNT6_753_Fq_PARAMS: BigNumParams<7, 753> = BigNumParams { has_multiplicative_inverse: true, modulus: [ 0xa099170fa13a4fd90776e240000001, diff --git a/src/fields/mnt6_753Fr.nr b/src/fields/mnt6_753Fr.nr index be3479a7..0008d213 100644 --- a/src/fields/mnt6_753Fr.nr +++ b/src/fields/mnt6_753Fr.nr @@ -19,18 +19,21 @@ //! * B = Fq3(b * NON_RESIDUE, 0, 0) //! * NON_RESIDUE = 11 is the cubic non-residue used to conpub struct the //! extension field Fq3 +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct MNT6_753_Fr_Params {} +pub type MNT6_753_Fr = BigNum<7, 753, MNT6_753_Fr_Params>; + impl BigNumParamsGetter<7, 753> for MNT6_753_Fr_Params { fn get_params() -> BigNumParams<7, 753> { MNT6_753_Fr_PARAMS } } -global MNT6_753_Fr_PARAMS: BigNumParams<7, 753> = BigNumParams { +pub global MNT6_753_Fr_PARAMS: BigNumParams<7, 753> = BigNumParams { has_multiplicative_inverse: true, modulus: [ 0x9d54522cdd119f5e9063de245e8001, diff --git a/src/fields/mod.nr b/src/fields/mod.nr index c9cd45a7..c0f37501 100644 --- a/src/fields/mod.nr +++ b/src/fields/mod.nr @@ -27,10 +27,3 @@ pub mod U1024; pub mod U2048; pub mod U4096; pub mod U8192; - -// example typedef when using a defined bignum instance: -// -// use crate::bignum::BigNum; -// use bn254Fq::BN254_Fq_Params; -// -// type Fq = BigNum<3, 254, BN254_Fq_Params>; diff --git a/src/fields/pallasFq.nr b/src/fields/pallasFq.nr index c6f84d96..b052fead 100644 --- a/src/fields/pallasFq.nr +++ b/src/fields/pallasFq.nr @@ -13,18 +13,21 @@ //! * Curve equation: y^2 = x^3 + 5 //! * Valuation(q - 1, 2) = 32 //! * Valuation(r - 1, 2) = 32 +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct Pallas_Fq_Params {} +pub type Pallas_Fq = BigNum<3, 255, Pallas_Fq_Params>; + impl BigNumParamsGetter<3, 255> for Pallas_Fq_Params { fn get_params() -> BigNumParams<3, 255> { Pallas_Fq_PARAMS } } -global Pallas_Fq_PARAMS: BigNumParams<3, 255> = BigNumParams { +pub global Pallas_Fq_PARAMS: BigNumParams<3, 255> = BigNumParams { has_multiplicative_inverse: true, modulus: [0x4698fc094cf91b992d30ed00000001, 0x22, 0x4000], double_modulus: [ diff --git a/src/fields/pallasFr.nr b/src/fields/pallasFr.nr index f7c4a9fa..a4b3a663 100644 --- a/src/fields/pallasFr.nr +++ b/src/fields/pallasFr.nr @@ -13,18 +13,21 @@ //! * Curve equation: y^2 = x^3 + 5 //! * Valuation(q - 1, 2) = 32 //! * Valuation(r - 1, 2) = 32 +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct Pallas_Fr_Params {} +pub type Pallas_Fr = BigNum<3, 255, Pallas_Fr_Params>; + impl BigNumParamsGetter<3, 255> for Pallas_Fr_Params { fn get_params() -> BigNumParams<3, 255> { Pallas_Fr_PARAMS } } -global Pallas_Fr_PARAMS: BigNumParams<3, 255> = BigNumParams { +pub global Pallas_Fr_PARAMS: BigNumParams<3, 255> = BigNumParams { has_multiplicative_inverse: true, modulus: [0x4698fc0994a8dd8c46eb2100000001, 0x22, 0x4000], double_modulus: [ diff --git a/src/fields/secp256k1Fq.nr b/src/fields/secp256k1Fq.nr index 2ee241fb..f30a3392 100644 --- a/src/fields/secp256k1Fq.nr +++ b/src/fields/secp256k1Fq.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct Secp256k1_Fq_Params {} +pub type Secp256k1_Fq = BigNum<3, 256, Secp256k1_Fq_Params>; + impl BigNumParamsGetter<3, 256> for Secp256k1_Fq_Params { fn get_params() -> BigNumParams<3, 256> { Secp256k1_Fq_PARAMS } } -global Secp256k1_Fq_PARAMS: BigNumParams<3, 256> = BigNumParams { +pub global Secp256k1_Fq_PARAMS: BigNumParams<3, 256> = BigNumParams { has_multiplicative_inverse: true, modulus: [0xfffffffffffffffffffffefffffc2f, 0xffffffffffffffffffffffffffffff, 0xffff], double_modulus: [ diff --git a/src/fields/secp256k1Fr.nr b/src/fields/secp256k1Fr.nr index 8f4a70ec..1e7055a9 100644 --- a/src/fields/secp256k1Fr.nr +++ b/src/fields/secp256k1Fr.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct Secp256k1_Fr_Params {} +pub type Secp256k1_Fr = BigNum<3, 256, Secp256k1_Fr_Params>; + impl BigNumParamsGetter<3, 256> for Secp256k1_Fr_Params { fn get_params() -> BigNumParams<3, 256> { Secp256k1_Fr_PARAMS } } -global Secp256k1_Fr_PARAMS: BigNumParams<3, 256> = BigNumParams { +pub global Secp256k1_Fr_PARAMS: BigNumParams<3, 256> = BigNumParams { has_multiplicative_inverse: true, modulus: [0xaedce6af48a03bbfd25e8cd0364141, 0xfffffffffffffffffffffffffffeba, 0xffff], double_modulus: [ diff --git a/src/fields/secp256r1Fq.nr b/src/fields/secp256r1Fq.nr index 3aeec025..f69cea99 100644 --- a/src/fields/secp256r1Fq.nr +++ b/src/fields/secp256r1Fq.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct Secp256r1_Fq_Params {} +pub type Secp256r1_Fq = BigNum<3, 256, Secp256r1_Fq_Params>; + impl BigNumParamsGetter<3, 256> for Secp256r1_Fq_Params { fn get_params() -> BigNumParams<3, 256> { Secp256r1_Fq_PARAMS } } -global Secp256r1_Fq_PARAMS: BigNumParams<3, 256> = BigNumParams { +pub global Secp256r1_Fq_PARAMS: BigNumParams<3, 256> = BigNumParams { has_multiplicative_inverse: true, modulus: [0xffffffffffffffffffffffff, 0xffff00000001000000000000000000, 0xffff], double_modulus: [ diff --git a/src/fields/secp256r1Fr.nr b/src/fields/secp256r1Fr.nr index 89ca249f..3ead4409 100644 --- a/src/fields/secp256r1Fr.nr +++ b/src/fields/secp256r1Fr.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct Secp256r1_Fr_Params {} +pub type Secp256r1_Fr = BigNum<3, 256, Secp256r1_Fr_Params>; + impl BigNumParamsGetter<3, 256> for Secp256r1_Fr_Params { fn get_params() -> BigNumParams<3, 256> { Secp256r1_Fr_PARAMS } } -global Secp256r1_Fr_PARAMS: BigNumParams<3, 256> = BigNumParams { +pub global Secp256r1_Fr_PARAMS: BigNumParams<3, 256> = BigNumParams { has_multiplicative_inverse: true, modulus: [0xe6faada7179e84f3b9cac2fc632551, 0xffff00000000ffffffffffffffffbc, 0xffff], double_modulus: [ diff --git a/src/fields/secp384r1Fq.nr b/src/fields/secp384r1Fq.nr index 6206f97b..2f0f9204 100644 --- a/src/fields/secp384r1Fq.nr +++ b/src/fields/secp384r1Fq.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct Secp384r1_Fq_Params {} +pub type Secp384r1_Fq = BigNum<4, 384, Secp384r1_Fq_Params>; + impl BigNumParamsGetter<4, 384> for Secp384r1_Fq_Params { fn get_params() -> BigNumParams<4, 384> { Secp384r1_Fq_PARAMS } } -global Secp384r1_Fq_PARAMS: BigNumParams<4, 384> = BigNumParams { +pub global Secp384r1_Fq_PARAMS: BigNumParams<4, 384> = BigNumParams { has_multiplicative_inverse: true, modulus: [ 0xffffff0000000000000000ffffffff, diff --git a/src/fields/secp384r1Fr.nr b/src/fields/secp384r1Fr.nr index 7794dcbd..af18afce 100644 --- a/src/fields/secp384r1Fr.nr +++ b/src/fields/secp384r1Fr.nr @@ -1,15 +1,18 @@ +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct Secp384r1_Fr_Params {} +pub type Secp384r1_Fr = BigNum<4, 384, Secp384r1_Fr_Params>; + impl BigNumParamsGetter<4, 384> for Secp384r1_Fr_Params { fn get_params() -> BigNumParams<4, 384> { Secp384r1_Fr_PARAMS } } -global Secp384r1_Fr_PARAMS: BigNumParams<4, 384> = BigNumParams { +pub global Secp384r1_Fr_PARAMS: BigNumParams<4, 384> = BigNumParams { has_multiplicative_inverse: true, modulus: [ 0x1a0db248b0a77aecec196accc52973, diff --git a/src/fields/vestaFq.nr b/src/fields/vestaFq.nr index 99d37936..72d4b61f 100644 --- a/src/fields/vestaFq.nr +++ b/src/fields/vestaFq.nr @@ -14,18 +14,21 @@ //! * Curve equation: y^2 = x^3 + 5 //! * Valuation(q - 1, 2) = 32 //! * Valuation(r - 1, 2) = 32 +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct Vesta_Fq_Params {} +pub type Vesta_Fq = BigNum<3, 255, Vesta_Fq_Params>; + impl BigNumParamsGetter<3, 255> for Vesta_Fq_Params { fn get_params() -> BigNumParams<3, 255> { Vesta_Fq_PARAMS } } -global Vesta_Fq_PARAMS: BigNumParams<3, 255> = BigNumParams { +pub global Vesta_Fq_PARAMS: BigNumParams<3, 255> = BigNumParams { has_multiplicative_inverse: true, modulus: [0x4698fc0994a8dd8c46eb2100000001, 0x22, 0x4000], double_modulus: [ diff --git a/src/fields/vestaFr.nr b/src/fields/vestaFr.nr index 72bfbb73..044163d3 100644 --- a/src/fields/vestaFr.nr +++ b/src/fields/vestaFr.nr @@ -14,18 +14,21 @@ //! * Curve equation: y^2 = x^3 + 5 //! * Valuation(q - 1, 2) = 32 //! * Valuation(r - 1, 2) = 32 +use crate::bignum::BigNum; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; pub struct Vesta_Fr_Params {} +pub type Vesta_Fr = BigNum<3, 255, Vesta_Fr_Params>; + impl BigNumParamsGetter<3, 255> for Vesta_Fr_Params { fn get_params() -> BigNumParams<3, 255> { Vesta_Fr_PARAMS } } -global Vesta_Fr_PARAMS: BigNumParams<3, 255> = BigNumParams { +pub global Vesta_Fr_PARAMS: BigNumParams<3, 255> = BigNumParams { has_multiplicative_inverse: true, modulus: [0x4698fc094cf91b992d30ed00000001, 0x22, 0x4000], double_modulus: [ diff --git a/src/lib.nr b/src/lib.nr index 2dff0346..b458a7b2 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -1,8 +1,8 @@ -// BigNum -pub mod bignum; - -// RuntimeBigNum -pub mod runtime_bignum; +mod bignum; +mod runtime_bignum; +mod tests; +mod benchmarks; +mod constants; // Pre-defined fields and bignums for people to use: pub mod fields; @@ -15,14 +15,36 @@ pub(crate) mod fns; pub(crate) mod utils; // Re-export the main structs so that users don't have to specify the paths -pub use bignum::BigNum; -pub use bignum::BigNumTrait; // So that external code can operate on a generic BigNum, `where BigNum: BigNumTrait`. +pub use bignum::{BigNum, BigNumTrait}; pub use runtime_bignum::RuntimeBigNum; -mod constants; - -// Tests -mod tests; - -// Benchmarks -mod benchmarks; +// Re-export the pre-defined bignum types, for easier access: +pub use fields::bls12_377Fq::BLS12_377_Fq; +pub use fields::bls12_377Fr::BLS12_377_Fr; +pub use fields::bls12_381Fq::BLS12_381_Fq; +pub use fields::bls12_381Fr::BLS12_381_Fr; +pub use fields::bn254Fq::BN254_Fq; +pub use fields::ed25519Fq::ED25519_Fq; +pub use fields::ed25519Fr::ED25519_Fr; +pub use fields::mnt4_753Fq::MNT4_753_Fq; +pub use fields::mnt4_753Fr::MNT4_753_Fr; +pub use fields::mnt6_753Fq::MNT6_753_Fq; +pub use fields::mnt6_753Fr::MNT6_753_Fr; +pub use fields::pallasFq::Pallas_Fq; +pub use fields::pallasFr::Pallas_Fr; +pub use fields::secp256k1Fq::Secp256k1_Fq; +pub use fields::secp256k1Fr::Secp256k1_Fr; +pub use fields::secp256r1Fq::Secp256r1_Fq; +pub use fields::secp256r1Fr::Secp256r1_Fr; +pub use fields::secp384r1Fq::Secp384r1_Fq; +pub use fields::secp384r1Fr::Secp384r1_Fr; +pub use fields::U1024::U1024; +pub use fields::U2048::U2048; +pub use fields::U256::U256; +pub use fields::U384::U384; +pub use fields::U4096::U4096; +pub use fields::U512::U512; +pub use fields::U768::U768; +pub use fields::U8192::U8192; +pub use fields::vestaFq::Vesta_Fq; +pub use fields::vestaFr::Vesta_Fr; diff --git a/src/tests/bignum_test.nr b/src/tests/bignum_test.nr index 0c4f62fc..06d91210 100644 --- a/src/tests/bignum_test.nr +++ b/src/tests/bignum_test.nr @@ -6,10 +6,10 @@ use crate::fns::unconstrained_helpers::__helper_add; use crate::params::BigNumParams; use crate::params::BigNumParamsGetter; -use crate::fields::bls12_381Fq::BLS12_381_Fq_Params; -use crate::fields::bls12_381Fr::BLS12_381_Fr_Params; -use crate::fields::bn254Fq::BN254_Fq_Params; -use crate::fields::U256::U256Params; +use crate::fields::bls12_381Fq::BLS12_381_Fq; +use crate::fields::bls12_381Fr::BLS12_381_Fr; +use crate::fields::bn254Fq::{BN254_Fq, BN254_Fq_Params}; +use crate::fields::U256::U256; struct Test2048Params {} @@ -89,18 +89,14 @@ impl BigNumParamsGetter<18, 2048> for Test2048Params { } } -type Fq = BigNum<3, 254, BN254_Fq_Params>; -type BN256 = BigNum<3, 257, U256Params>; -type BN381 = BigNum<4, 381, BLS12_381_Fq_Params>; type BN2048 = BigNum<18, 2048, Test2048Params>; -type BLS_FR = BigNum<3, 255, BLS12_381_Fr_Params>; /** * @brief this example was failing - sanity test to validate it now works **/ #[test] fn test_bls_reduction() { - let X1: BN381 = BigNum { + let X1 = BLS12_381_Fq { limbs: [ 0x55e83ff97a1aeffb3af00adb22c6bb, 0x8c4f9774b905a14e3a3f171bac586c, @@ -110,7 +106,7 @@ fn test_bls_reduction() { }; X1.validate_in_field(); // Safety: test code - let mut (_, XX_mul_3): (BN381, BN381) = unsafe { + let mut (_, XX_mul_3): (BLS12_381_Fq, BLS12_381_Fq) = unsafe { BigNum::__compute_quadratic_expression( [[X1, X1, X1]], [[false, false, false]], @@ -126,7 +122,7 @@ fn test_bls_reduction() { #[test] fn test_derive_from_seed() { let seed = [1, 2, 3, 4]; - let result_bn = Fq::derive_from_seed(seed); + let result_bn = BN254_Fq::derive_from_seed(seed); result_bn.validate_in_field(); } @@ -292,89 +288,26 @@ where #[test] fn test_eq_BN() { - // let stub: Fq = BigNum::new(); - test_eq::<3, Fq>(); + // let stub = BN254_Fq::new(); + test_eq::<3, BN254_Fq>(); } #[test] fn test_is_zero_BN() { - test_is_zero::<3, Fq>(); + test_is_zero::<3, BN254_Fq>(); } #[test] -fn test_u128_sub_hex() { - let a: u128 = 0x816a916871ca8d3c208c16d87cfd46; - let b = 0x816a916871ca8d3c208c16d87cfd45; - let c = a - 1; - assert(c == b); -} - -#[test] -fn test_u128_add_hex() { - let a: u128 = 0x816a916871ca8d3c208c16d87cfd46; - let b = 0x816a916871ca8d3c208c16d87cfd45; - let c = b + 1; - assert(c == a); -} - -#[test] -fn test_u128_mul_hex() { - let a: u128 = 0x40b548b438e5469e10460b6c3e7ea3; - let b = 0x816a916871ca8d3c208c16d87cfd46; - let c = 2 * a; - assert(c == b); -} - -#[test] -fn test_u128_div_hex() { - let a: u128 = 0x816a916871ca8d3c208c16d87cfd46; - let b = 0x40b548b438e5469e10460b6c3e7ea3; - let c = a / 2; - assert(c == b); -} - -#[test] -fn test_u128_sub() { - let a: u128 = 671967750576550571863734675757137222; - let b = 671967750576550571863734675757137221; - let c = a - 1; - assert(c == b); -} - -#[test] -fn test_u128_add() { - let a: u128 = 671967750576550571863734675757137222; - let b: u128 = 671967750576550571863734675757137221; - let c: u128 = b + 1; - assert(c == a); -} - -#[test] -fn test_u128_mul() { - let a: u128 = 671967750576550571863734675757137222; - let b: u128 = 335983875288275285931867337878568611; - let c: u128 = b * 2; - assert(c == a); -} - -#[test] -fn test_u128_div() { - let a: u128 = 671967750576550571863734675757137222; - let b: u128 = 335983875288275285931867337878568611; - let c = a / 2; - assert(c == b); -} - -#[test] -unconstrained fn test_add_BN() { - let mut a: Fq = BigNum::modulus(); - let mut b: Fq = BigNum::modulus(); +fn test_add_BN() { + let mut a = BN254_Fq::modulus(); + let mut b = BN254_Fq::modulus(); + let mut expected = BN254_Fq::modulus(); a.limbs[0] -= 1; b.limbs[0] -= 1; - let mut expected: Fq = BigNum::modulus(); + let mut expected: BN254_Fq = BigNum::modulus(); expected.limbs[0] -= 2; let c = a + b; @@ -383,9 +316,9 @@ unconstrained fn test_add_BN() { #[test] fn test_add_test_BN_wrap_around() { - let mut a: Fq = BigNum::modulus(); - let mut b: Fq = Fq { limbs: [3, 0, 0] }; - let mut expected: Fq = Fq::one(); + let mut a: BN254_Fq = BigNum::modulus(); + let mut b: BN254_Fq = BN254_Fq { limbs: [3, 0, 0] }; + let mut expected: BN254_Fq = BN254_Fq::one(); a.limbs[0] -= 2; let c = a + b; @@ -396,9 +329,9 @@ fn test_add_test_BN_wrap_around() { #[test] fn test_sub_test_BN() { // 0 - 1 should equal p - 1 - let mut a: Fq = BigNum::zero(); - let mut b: Fq = BigNum::one(); - let mut expected: Fq = BigNum::modulus(); + let mut a = BN254_Fq::zero(); + let mut b = BN254_Fq::one(); + let mut expected = BN254_Fq::modulus(); expected.limbs[0] -= 1; // p - 1 let result = a - b; assert(result == expected); @@ -406,8 +339,8 @@ fn test_sub_test_BN() { #[test] fn test_eq_wrap() { - let a: Fq = BigNum::modulus(); - let b = Fq::zero(); + let a: BN254_Fq = BigNum::modulus(); + let b = BN254_Fq::zero(); assert(a == b); } @@ -416,8 +349,8 @@ fn test_sub_modulus_limit() { // if we underflow, maximum result should be ... // 0 - 1 = o-1 // 0 - p = 0 - let mut a: Fq = BigNum::zero(); - let mut b: Fq = BigNum::modulus(); + let mut a = BN254_Fq::zero(); + let mut b = BN254_Fq::modulus(); let mut expected = BigNum::zero(); let result = a - b; @@ -427,8 +360,8 @@ fn test_sub_modulus_limit() { #[test(should_fail_with = "call to assert_max_bit_size")] fn test_sub_modulus_underflow() { // 0 - (p + 1) is smaller than p and should produce unsatisfiable constraints - let mut a: Fq = BigNum::zero(); - let mut b: Fq = BigNum::modulus(); + let mut a = BN254_Fq::zero(); + let mut b = BN254_Fq::modulus(); b.limbs[0] += 1; let mut expected = BigNum::one(); @@ -444,8 +377,8 @@ fn test_add_modulus_limit() { let p: [u128; 3] = BN254_Fq_Params::get_params().modulus; let two_pow_254_minus_1: [u128; 3] = [0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0x3fff]; - let a: Fq = BigNum { limbs: p }; - let b: Fq = BigNum { limbs: two_pow_254_minus_1 }; + let a = BN254_Fq { limbs: p }; + let b = BN254_Fq { limbs: two_pow_254_minus_1 }; let result = a + b; assert(result == b); } @@ -459,27 +392,27 @@ fn test_add_modulus_overflow() { [0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0x3fff]; let one = [1, 0, 0]; // Safety: test code - let a: Fq = BigNum { limbs: unsafe { __helper_add(p, one) } }; - let b: Fq = BigNum { limbs: two_pow_254_minus_1 }; + let a = BN254_Fq { limbs: unsafe { __helper_add(p, one) } }; + let b = BN254_Fq { limbs: two_pow_254_minus_1 }; let result = a + b; assert(result == b); } #[test] fn test_mul_BN() { - test_mul::<3, Fq>(); + test_mul::<3, BN254_Fq>(); } #[test] fn test_add_BN2() { - test_add::<3, Fq>(); + test_add::<3, BN254_Fq>(); } #[test] fn test_mul_BN_with_one() { - let a: Fq = BigNum::modulus() - (BigNum::one() + BigNum::one()); + let a: BN254_Fq = BN254_Fq::modulus() - (BN254_Fq::one() + BN254_Fq::one()); - let b: Fq = BigNum::one(); + let b: BN254_Fq = BN254_Fq::one(); let c = a * b; assert(c == a); @@ -487,37 +420,37 @@ fn test_mul_BN_with_one() { #[test] fn test_div_BN() { - test_div::<3, Fq>(); + test_div::<3, BN254_Fq>(); } #[test] fn test_invmod_BN() { - test_invmod::<3, Fq>(); + test_invmod::<3, BN254_Fq>(); } #[test] fn test_assert_is_not_equal_BN() { - assert_is_not_equal::<3, Fq>(); + assert_is_not_equal::<3, BN254_Fq>(); } #[test(should_fail_with = "asssert_is_not_equal fail")] fn test_assert_is_not_equal_fail_BN() { - assert_is_not_equal_fail::<3, Fq>(); + assert_is_not_equal_fail::<3, BN254_Fq>(); } #[test(should_fail_with = "asssert_is_not_equal fail")] fn test_assert_is_not_equal_overloaded_lhs_fail_BN() { - assert_is_not_equal_overloaded_lhs_fail::<3, Fq>(); + assert_is_not_equal_overloaded_lhs_fail::<3, BN254_Fq>(); } #[test(should_fail_with = "asssert_is_not_equal fail")] fn test_assert_is_not_equal_overloaded_rhs_fail_BN() { - assert_is_not_equal_overloaded_rhs_fail::<3, Fq>(); + assert_is_not_equal_overloaded_rhs_fail::<3, BN254_Fq>(); } #[test(should_fail_with = "asssert_is_not_equal fail")] fn test_assert_is_not_equal_overloaded_fail_BN() { - assert_is_not_equal_overloaded_fail::<3, Fq>(); + assert_is_not_equal_overloaded_fail::<3, BN254_Fq>(); } #[test] @@ -567,59 +500,57 @@ fn test_assert_is_not_equal_overloaded_fail_2048() { #[test] fn test_eq_U256() { - test_eq::<3, BN256>(); + test_eq::<3, U256>(); } #[test] fn test_is_zero_U256() { - test_is_zero::<3, BN256>(); + test_is_zero::<3, U256>(); } #[test] fn test_mul_U256() { - test_mul::<3, BN256>(); + test_mul::<3, U256>(); } #[test] fn test_add_U256() { - test_add::<3, BN256>(); + test_add::<3, U256>(); } #[test] fn test_assert_is_not_equal_U256() { - assert_is_not_equal::<3, BN256>(); + assert_is_not_equal::<3, U256>(); } #[test(should_fail_with = "asssert_is_not_equal fail")] fn test_assert_is_not_equal_fail_U256() { - assert_is_not_equal_fail::<3, BN256>(); + assert_is_not_equal_fail::<3, U256>(); } #[test(should_fail_with = "asssert_is_not_equal fail")] fn test_assert_is_not_equal_overloaded_lhs_fail_U256() { - assert_is_not_equal_overloaded_lhs_fail::<3, BN256>(); + assert_is_not_equal_overloaded_lhs_fail::<3, U256>(); } #[test(should_fail_with = "asssert_is_not_equal fail")] fn test_assert_is_not_equal_overloaded_rhs_fail_U256() { - assert_is_not_equal_overloaded_rhs_fail::<3, BN256>(); + assert_is_not_equal_overloaded_rhs_fail::<3, U256>(); } #[test(should_fail_with = "asssert_is_not_equal fail")] fn test_assert_is_not_equal_overloaded_fail_U256() { - assert_is_not_equal_overloaded_fail::<3, BN256>(); + assert_is_not_equal_overloaded_fail::<3, U256>(); } -type U256 = BN256; - #[test] fn test_udiv_mod_U256() { - let a: U256 = BigNum::from_slice([ + let a = U256::from_slice([ 0xec0ca0c0adce359af6fcea1a7ab2dc, 0xdd52c4aa3fde93685d3f7cc285de32, 0x6fea, ]); - let b: U256 = BigNum::from_slice([12, 0, 0]); + let b = U256::from_slice([12, 0, 0]); let (q, r) = a.udiv_mod(b); @@ -742,35 +673,35 @@ fn test_expressions() { 0, ]; - let y: Fq = BigNum { limbs: [0x1, 0x1, 0x0] }; - let z: Fq = BigNum { limbs: [0x2, 0x2, 0x0] }; + let y = BN254_Fq { limbs: [0x1, 0x1, 0x0] }; + let z = BN254_Fq { limbs: [0x2, 0x2, 0x0] }; // Safety: test code let yy = unsafe { y.__add(y) }; assert(yy.limbs == z.limbs); - let uu: Fq = BigNum { + let uu = BN254_Fq { limbs: [ 0x0000000000000000000000000000000000b4a832748da6ad742a1fd81b787643, 0x00000000000000000000000000000000009575f594e04080471712c1d7f18e89, 0x000000000000000000000000000000000000000000000000000000000000063, ], }; - let vv: Fq = BigNum { + let vv = BN254_Fq { limbs: [ 0x0000000000000000000000000000000000b4aec2748da6ad742a1fd81b787643, 0x00000000000000000000000000000000009575f594e0408047171a01d7f18e89, 0x0000000000000000000000000000000000000000000000000000000000000062, ], }; - let w: Fq = BigNum { + let w = BN254_Fq { limbs: [ 0x0000000000000000000000000000000000b4a832748da6ad742a1fd81b787643, 0x00000000000000000000000000000000009575f594e04080471712c1d7f18e89, 0x0000000000000000000000000000000000000000000000000000000000001f93, ], }; - let x: Fq = BigNum { + let x = BN254_Fq { limbs: [ 0x0000000000000000000000000000000000b4aec2748da6ad742a1fd81b787643, 0x00000000000000000000000000000000009575f594e0408047171a01d7f18e89, @@ -810,15 +741,15 @@ fn test_expressions() { #[test] fn test_from_field_1_digit() { let field: Field = 1; - let result = Fq::from(field); - assert(result == Fq::one()); + let result = BN254_Fq::from(field); + assert(result == BN254_Fq::one()); } #[test] fn test_from_field_2_digits() { let field: Field = 762576765071760201410184025311678064293966151975347778787092903729041075; - let result = Fq::from(field); - let expected: Fq = + let result = BN254_Fq::from(field); + let expected: BN254_Fq = BigNum { limbs: [0xe88ed97f8f707abd3fa65763c80eb3, 0x6e7d8b5586595aa1fb2ee04d5cb4f5, 0x0] }; assert(result == expected); } @@ -826,8 +757,8 @@ fn test_from_field_2_digits() { #[test] fn test_from_field_3_digits() { let field: Field = -1; - let result = Fq::from(field); - let expected: Fq = BigNum { + let result = BN254_Fq::from(field); + let expected = BN254_Fq { limbs: [0x33e84879b9709143e1f593f0000000, 0x4e72e131a029b85045b68181585d28, 0x3064], }; assert(result == expected); @@ -835,8 +766,8 @@ fn test_from_field_3_digits() { #[test] fn test_do_nothing() { - let a: Fq = BigNum { limbs: [1, 2, 0] }; - let b: Fq = BigNum { limbs: [1, 2, 0] }; + let a: BN254_Fq = BN254_Fq { limbs: [1, 2, 0] }; + let b: BN254_Fq = BN254_Fq { limbs: [1, 2, 0] }; // Safety: test code let c = unsafe { __helper_add(a.limbs, b.limbs) }; // Safety: test code @@ -849,8 +780,8 @@ fn test_do_nothing() { #[test] fn test_from_field_3_digits_BLS381() { let field: Field = -1; - let result = BN381::from(field); - let expected: BN381 = BigNum { + let result = BLS12_381_Fq::from(field); + let expected = BLS12_381_Fq { limbs: [0x33e84879b9709143e1f593f0000000, 0x4e72e131a029b85045b68181585d28, 0x3064, 0x0], }; assert(result == expected); @@ -859,7 +790,7 @@ fn test_from_field_3_digits_BLS381() { #[test] fn test_to_field_one() { let field: Field = 1; - let bn = Fq::one(); + let bn = BN254_Fq::one(); let result = to_field(bn); assert(result == field); } @@ -867,7 +798,7 @@ fn test_to_field_one() { #[test] fn test_to_field_one_digit() { let field: Field = 1066513542066841864585910935480267774; - let bn = Fq { limbs: [0xcd672d695ef3129e4c40867a7173fe, 0x0, 0x0] }; + let bn = BN254_Fq { limbs: [0xcd672d695ef3129e4c40867a7173fe, 0x0, 0x0] }; let result = to_field(bn); assert(result == field); } @@ -875,8 +806,9 @@ fn test_to_field_one_digit() { #[test] fn test_to_field_two_digits() { let field: Field = 697955470585821007263499235110798476786097877002667034107578965871052378; - let bn = - Fq { limbs: [0x5a10b956d41840745e0a9f6e34465a, 0x65209b74583b912262843211905e41, 0x0] }; + let bn = BN254_Fq { + limbs: [0x5a10b956d41840745e0a9f6e34465a, 0x65209b74583b912262843211905e41, 0x0], + }; let result = to_field(bn); assert(result == field); } @@ -884,15 +816,16 @@ fn test_to_field_two_digits() { #[test] fn test_to_field_three_digits() { let field: Field = 2330301921655783950764183713945533646391233209687308929386184468126823563744; - let bn = - Fq { limbs: [0x862cf8ea69d6c70c9cc8d8871b41e0, 0xe7763528201566c2fc8d93973cf1b4, 0x526] }; + let bn = BN254_Fq { + limbs: [0x862cf8ea69d6c70c9cc8d8871b41e0, 0xe7763528201566c2fc8d93973cf1b4, 0x526], + }; let result = to_field(bn); assert(result == field); } #[test(should_fail_with = "BigNum::validate_gt check fails")] fn test_to_field_three_digits_overflow() { - let bn: Fq = BigNum { + let bn = BN254_Fq { limbs: [0x4e6405505a33bb9b9c0563df2bd59a, 0x48dbe03a9bb4865ba961e41ef9dded, 0x3a36], }; let _ = to_field(bn); @@ -900,7 +833,7 @@ fn test_to_field_three_digits_overflow() { #[test(should_fail_with = "BigNum::validate_gt check fails")] fn test_to_field_too_many_digits() { - let bn: BN381 = BN381 { + let bn = BLS12_381_Fq { limbs: [0xea1742447ee9d92f9f18e1c80a481e, 0x3d89ad3d3ae85f3f482a08435c93ec, 0x1e9f, 0x1], }; let _ = to_field(bn); @@ -909,35 +842,35 @@ fn test_to_field_too_many_digits() { #[test] fn test_from_to_field_1() { let a = 20192735083400333763152317277081729935089452774154199134677444560763605803197; - let b = Fq::from(a); + let b = BN254_Fq::from(a); let c = to_field(b); assert(c == a); } #[test] fn test_from_to_field_fuzz(a: Field) { - let b = BN381::from(a); + let b = BLS12_381_Fq::from(a); let c = to_field(b); assert(c == a); } #[test] fn test_to_from_field_1() { - let a: Fq = BigNum { + let a = BN254_Fq { limbs: [0x3c768db7732ea1b536c06ae66bce70, 0xb9936c1401d91e7e9e1138375650b4, 0x8c8], }; let b = to_field(a); - let c = Fq::from(b); + let c = BN254_Fq::from(b); assert(a == c); } #[test] fn test_to_from_field_2() { - let a: BN381 = BigNum { + let a = BLS12_381_Fq { limbs: [0xd7562bf2b1fe13d458685c96a46d28, 0x2079950acd45bb43a9beeba69d5dc9, 0x18ca, 0x0], }; let b = to_field(a); - let c = BN381::from(b); + let c = BLS12_381_Fq::from(b); assert(a == c); } @@ -953,7 +886,7 @@ where #[test] unconstrained fn test_batch_inversion_BN381(seeds: [[u8; 2]; 3]) { - let fields = seeds.map(|seed| BN381::derive_from_seed(seed)); + let fields = seeds.map(|seed| BLS12_381_Fq::derive_from_seed(seed)); test_batch_inversion(fields) } @@ -970,21 +903,21 @@ where #[test] unconstrained fn test_batch_inversion_slice_BN381(seeds: [[u8; 2]; 3]) { - let fields = seeds.map(|seed| BN381::derive_from_seed(seed)).as_slice(); + let fields = seeds.map(|seed| BLS12_381_Fq::derive_from_seed(seed)).as_slice(); test_batch_inversion_slice(fields) } #[test] unconstrained fn test_batch_inversion_BN381_regression(seeds: [[u8; 2]; 5]) { - let fields = seeds.map(|seed| BN381::derive_from_seed(seed)); + let fields = seeds.map(|seed| BLS12_381_Fr::derive_from_seed(seed)); test_batch_inversion(fields) } #[test] fn test_sub_underflow_regression() { let limbs = [605231426910671071918217543292637230, 925169190305713195541137574269511054, 4915]; - let a = BLS_FR { limbs: limbs }; - let b = BLS_FR { limbs: limbs }; + let a = BLS12_381_Fr { limbs: limbs }; + let b = BLS12_381_Fr { limbs: limbs }; let c = b - a; assert(c.limbs == [0, 0, 0]); }