Skip to content

Commit

Permalink
updated readme, added some extra params to BigNumInstance to reduce…
Browse files Browse the repository at this point in the history
… derivation work
  • Loading branch information
zac-williamson committed Jul 24, 2024
1 parent fde7e67 commit 2aa6a66
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 210 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,15 @@ BigNumInstance parameters (`modulus`, `redc_param`) can be provided at runtime v
Example usage:

```rust
use crate::bignum::fields::bn254Fq::BNParams;
use crate::bignum::fields::BN254Instance;
use crate::bignum::BigNum;
use crate::bignum::fields::bn254Fq{BNParams, BN254INSTANCE};
use crate::bignum::runtime_bignum::BigNumInstance;
use crate::bignum::BigNum;

type Fq = BigNum<3, BNParams>;
type FqInst = BigNumInstance<3, BNParams>;

fn example(Fq a, Fq b) -> Fq {
let instance = BN254Instance();
let instance: FqInst = BN254INSTANCE;
instance.mul(a, b)
}
```
Expand Down
110 changes: 1 addition & 109 deletions src/bignum_test.nr
Original file line number Diff line number Diff line change
Expand Up @@ -81,120 +81,11 @@ impl BigNumParamsTrait<18> for Test2048Params {
];
BigNumInstance::new(modulus, redc_param)
}
// fn modulus() -> [Field; 18] {[
// 0x0000000000000000000000000000000000c0a197a5ae0fcdceb052c9732614fe,
// 0x0000000000000000000000000000000000656ae034423283422243918ab83be3,
// 0x00000000000000000000000000000000006bf590da48a7c1070b7d5aabaac678,
// 0x00000000000000000000000000000000000cce39f530238b606f24b296e2bda9,
// 0x000000000000000000000000000000000001e1fef9bb9c1c3ead98f226f1bfa0,
// 0x0000000000000000000000000000000000ad8c1c816e12e0ed1379055e373abf,
// 0x0000000000000000000000000000000000cebe80e474f753aa9d1461c435123d,
// 0x0000000000000000000000000000000000aee5a18ceedef88d115a8b93c167ad,
// 0x0000000000000000000000000000000000268ba83c4a65c4307427fc495d9e44,
// 0x0000000000000000000000000000000000dd2777926848667b7df79f342639d4,
// 0x0000000000000000000000000000000000f455074c96855ca0068668efe7da3d,
// 0x00000000000000000000000000000000005ddba6b30bbc168bfb3a1225f27d65,
// 0x0000000000000000000000000000000000591fec484f36707524133bcd6f4258,
// 0x000000000000000000000000000000000059641b756766aeebe66781dd01d062,
// 0x000000000000000000000000000000000058bc5eaff4b165e142bf9e2480eebb,
// 0x0000000000000000000000000000000000667a3964f08e06df772ce64b229a72,
// 0x00000000000000000000000000000000009c1fdb18907711bfe3e3c1cf918395,
// 0x00000000000000000000000000000000000000000000000000000000000000b8
// ]}
// fn double_modulus() -> [Field; 18] {[
// 0x000000000000000000000000000000000181432f4b5c1f9b9d60a592e64c29fc,
// 0x0000000000000000000000000000000001cad5c06884650684448723157077c6,
// 0x0000000000000000000000000000000001d7eb21b4914f820e16fab557558cef,
// 0x0000000000000000000000000000000001199c73ea604716c0de49652dc57b51,
// 0x000000000000000000000000000000000103c3fdf37738387d5b31e44de37f3f,
// 0x00000000000000000000000000000000015b183902dc25c1da26f20abc6e757d,
// 0x00000000000000000000000000000000019d7d01c8e9eea7553a28c3886a247a,
// 0x00000000000000000000000000000000015dcb4319ddbdf11a22b5172782cf5a,
// 0x00000000000000000000000000000000014d17507894cb8860e84ff892bb3c88,
// 0x0000000000000000000000000000000001ba4eef24d090ccf6fbef3e684c73a7,
// 0x0000000000000000000000000000000001e8aa0e992d0ab9400d0cd1dfcfb47a,
// 0x0000000000000000000000000000000001bbb74d6617782d17f674244be4faca,
// 0x0000000000000000000000000000000001b23fd8909e6ce0ea4826779ade84af,
// 0x0000000000000000000000000000000001b2c836eacecd5dd7cccf03ba03a0c3,
// 0x0000000000000000000000000000000001b178bd5fe962cbc2857f3c4901dd75,
// 0x0000000000000000000000000000000001ccf472c9e11c0dbeee59cc964534e3,
// 0x0000000000000000000000000000000001383fb63120ee237fc7c7839f230729,
// 0x0000000000000000000000000000000000000000000000000000000000000170
// ]}
// fn redc_param() -> [Field; 18] {[
// 0x000000000000000000000000000000000091697def7100cd5cf8d890b4ef2ec3,
// 0x00000000000000000000000000000000006765ba8304214dac764d3f4adc3185,
// 0x000000000000000000000000000000000048404bd14d927ea230e60d4bebf940,
// 0x00000000000000000000000000000000007c4d53a23bacc251ecbfc4b7ba5a0b,
// 0x000000000000000000000000000000000093eaf3499474a6f5b2fff83f1259c8,
// 0x00000000000000000000000000000000005bff4c737b97281f1a5f2384a8c16d,
// 0x000000000000000000000000000000000061b4cf2f55358476b5323782999055,
// 0x00000000000000000000000000000000001e7a804e8eacfe3a2a5673bc3885b8,
// 0x0000000000000000000000000000000000eabadeae4282906c817adf70eab4ae,
// 0x0000000000000000000000000000000000166f7df257fe2bf27f0809aceed9b0,
// 0x00000000000000000000000000000000007d90fb7428901b8bed11f6b81e36bf,
// 0x0000000000000000000000000000000000f36e6ba885c60b7024c563605df7e0,
// 0x000000000000000000000000000000000052b7c58d2fb5d2c8478963ae6d4a44,
// 0x000000000000000000000000000000000036ee761de26635f114ccc3f7d74f85,
// 0x0000000000000000000000000000000000e3fb726a10cf2220897513f05243de,
// 0x0000000000000000000000000000000000f43a26bbd732496eb4d828591b8056,
// 0x0000000000000000000000000000000000ff4e42304e60fb3a54fca735499f2c,
// 0x0000000000000000000000000000000000000000000000000000000000000162
// ]}
// fn k() -> u64 {
// 2048
// }
// fn modulus_bits() -> u64 {
// 2048
// }
}

type Fq = BigNum<3, BNParams>;
type Fqq = BigNum<18, Test2048Params>;

// // // fn main(x: Field, y: pub Field) {
// // // let z = x + y;
// // // let a = z * z;
// // // let b = a * a;

// // // let mut A: Fqq= BigNum::__derive_from_seed([a as u8]);
// // // let mut B: Fqq= BigNum::__derive_from_seed([b as u8]);
// // // let mut C: Fqq= BigNum::__derive_from_seed([(b * b) as u8]);
// // // let mut D: Fqq= BigNum::__derive_from_seed([(b + a) as u8]);

// // // let X = A * B;
// // // let predicate = x == y;

// // // // 1872 gates
// // // let mut result: Fqq = BigNum::new();
// // // if (predicate) {
// // // result = A * B;
// // // } else {
// // // result = C * D;
// // // }

// // // // 1152 gates
// // // let mut L: [Fqq; 2] = [A, C];
// // // let mut R: [Fqq; 2] = [B, D];
// // // let idx = predicate as Field;
// // // let result = L[idx] * R[idx];

// // // // 7429 - 6588 = 841
// // // let result = A * C;
// // // println(f"{result}");
// // // // println(f"{R}");
// // // // let mut lhs2 = BNExpressionInput::new(lhs, false);
// // // // let rhs2= BNExpressionInput::new(rhs, false);
// // // // for _ in 0..1 {
// // // // let out: Fqq = lhs2.value.__mulmod(rhs2.value);
// // // // out.validate_in_range();
// // // // let rem2 = BNExpressionInput::new(out, true);
// // // // BigNum::evaluate_quadratic_expression([[lhs2]], [[rhs2]], [rem2]);
// // // // lhs2.value = out;
// // // // }
// // // // 68?
// // // }

fn test_eq<BigNum, let N: u64>(_: BigNum, __: [Field; N]) where BigNum: BigNumTrait {
let a = BigNum::__derive_from_seed([1, 2, 3, 4]);
let b = BigNum::__derive_from_seed([1, 2, 3, 4]);
Expand Down Expand Up @@ -418,6 +309,7 @@ fn test_add_modulus_overflow() {

#[test]
fn test_mul_BN() {
let foo = BNParams::get_instance().modulus_u60_x4;
let stub: BigNum<3, BNParams> = BigNum::new();
test_mul(stub);
}
Expand Down
10 changes: 0 additions & 10 deletions src/fields.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ use crate::fields::bls12381Fr::Bls12_381_Fr_Params;
// example typedef when using a defined bignum instance
type Fq = BigNum<3, BNParams>;

pub fn BN254Instance() -> BigNumInstance<3, BNParams> {
let modulus = [
0x0000000000000000000000000000000000816a916871ca8d3c208c16d87cfd47, 0x00000000000000000000000000000000004e72e131a029b85045b68181585d97, 0x0000000000000000000000000000000000000000000000000000000000003064
];
let redc_param = [
0x000000000000000000000000000000000059785d9f353021bcebb62866fe4394, 0x0000000000000000000000000000000000d18988e8129eac1d2961a01cc04eba, 0x0000000000000000000000000000000000000000000000000000000000015291
];

BigNumInstance::new(modulus, redc_param)
}

pub fn BLS12381Instance() -> BigNumInstance<3, Bls12_381_Fr_Params> {
let modulus = [0xbda402fffe5bfeffffffff00000001, 0xa753299d7d483339d80809a1d80553, 0x0073ed];
Expand Down
3 changes: 2 additions & 1 deletion src/fields/bls12381Fr.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::BigNum;
use crate::runtime_bignum::BigNumInstance;
use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait;
use crate::BigNumParamsTrait as BigNumParamsTrait;
use crate::utils::u60_representation::U60Repr;

struct Bls12_381_Fr_Params {}

Expand All @@ -20,4 +21,4 @@ impl BigNumParamsTrait<3> for Bls12_381_Fr_Params {
let redc_param = [0x410fad2f92eb5c509cde80830358e4, 0x253b7fb78ddf0e2d772dc1f823b4d9, 0x008d54];
BigNumInstance::new(modulus, redc_param)
}
}
}
59 changes: 17 additions & 42 deletions src/fields/bn254Fq.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@ use crate::BigNum;
use crate::BigNumParamsTrait;
use crate::runtime_bignum::BigNumInstance;
use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait;

// struct BNParamsInner {}
// impl RuntimeBigNumParamsTrait<3> for BNParamsInner {
// fn k() -> u64 {
// 255
// }
// fn modulus_bits() -> u64 {
// 254
// }
// }
use crate::utils::u60_representation::U60Repr;
use crate::utils::arrayX::ArrayX;

struct BNParams {}
impl RuntimeBigNumParamsTrait<3> for BNParams {
Expand All @@ -22,39 +14,22 @@ impl RuntimeBigNumParamsTrait<3> for BNParams {
254
}
}

global BN254INSTANCE: BigNumInstance<3, BNParams> = BigNumInstance {
redc_param: [
0x000000000000000000000000000000000059785d9f353021bcebb62866fe4394, 0x0000000000000000000000000000000000d18988e8129eac1d2961a01cc04eba, 0x0000000000000000000000000000000000000000000000000000000000015291
],
modulus: [
0x0000000000000000000000000000000000816a916871ca8d3c208c16d87cfd47, 0x00000000000000000000000000000000004e72e131a029b85045b68181585d97, 0x0000000000000000000000000000000000000000000000000000000000003064
],
modulus_u60: U60Repr { limbs: ArrayX { segments: [[873852357459115335, 582839116012234963, 19622440730123671], [353301755347508101, 12388, 0]] } },
modulus_u60_x4: U60Repr { limbs: ArrayX { segments: [[873852357459115335, 582839116012234963, 19622440730123671], [353301755347508101, 12388, 0], [0, 0, 0], [0, 0, 0]] } },
double_modulus: [
0x000000000000000000000000000000000102d522d0e3951a7841182db0f9fa8e, 0x00000000000000000000000000000000019ce5c263405370a08b6d0302b0bb2e, 0x00000000000000000000000000000000000000000000000000000000000060c7
]
};
impl BigNumParamsTrait<3> for BNParams {
fn get_instance() -> BigNumInstance<3, Self> {
BigNumInstance {
redc_param: [
0x000000000000000000000000000000000059785d9f353021bcebb62866fe4394, 0x0000000000000000000000000000000000d18988e8129eac1d2961a01cc04eba, 0x0000000000000000000000000000000000000000000000000000000000015291
],
modulus: [
0x0000000000000000000000000000000000816a916871ca8d3c208c16d87cfd47, 0x00000000000000000000000000000000004e72e131a029b85045b68181585d97, 0x0000000000000000000000000000000000000000000000000000000000003064
],
double_modulus: [
0x000000000000000000000000000000000102d522d0e3951a7841182db0f9fa8e, 0x00000000000000000000000000000000019ce5c263405370a08b6d0302b0bb2e, 0x00000000000000000000000000000000000000000000000000000000000060c7
]
}
BN254INSTANCE
}
// fn redc_param() -> [Field; 3] {
// [
// 0x000000000000000000000000000000000059785d9f353021bcebb62866fe4394, 0x0000000000000000000000000000000000d18988e8129eac1d2961a01cc04eba, 0x0000000000000000000000000000000000000000000000000000000000015291
// ]
// }
// fn modulus() -> [Field; 3] {
// [
// 0x0000000000000000000000000000000000816a916871ca8d3c208c16d87cfd47, 0x00000000000000000000000000000000004e72e131a029b85045b68181585d97, 0x0000000000000000000000000000000000000000000000000000000000003064
// ]
// }
// fn double_modulus() -> [Field; 3] {
// [
// 0x000000000000000000000000000000000102d522d0e3951a7841182db0f9fa8e, 0x00000000000000000000000000000000019ce5c263405370a08b6d0302b0bb2e, 0x00000000000000000000000000000000000000000000000000000000000060c7
// ]
// }
// fn k() -> u64 {
// 255
// }
// fn modulus_bits() -> u64 {
// 254
// }
}
Loading

0 comments on commit 2aa6a66

Please sign in to comment.