From 53f7420619280c66a8438d918848624aaf60ddf2 Mon Sep 17 00:00:00 2001 From: zac-williamson Date: Wed, 9 Oct 2024 12:12:06 +0100 Subject: [PATCH] fixed reduction parameter error redc_param previously was only large enough to cover barrett reductions whose input was < 2^{modulus_bits * 2 + 2} this was insufficient for elliptic curve arithmetic in bignum redc_param is now large enough to cover barrett reduction inputs of at least 16 * modulus^2 additonally, library upgraded to not trigger compiler warnings for nargo 0.35.0 --- src/bignum_test.nr | 199 ++++++++-------- src/fields/U1024.nr | 15 +- src/fields/U2048.nr | 15 +- src/fields/U256.nr | 14 +- src/fields/U384.nr | 19 +- src/fields/U4096.nr | 15 +- src/fields/U512.nr | 17 +- src/fields/U768.nr | 33 ++- src/fields/U8192.nr | 15 +- src/fields/bls12_377Fq.nr | 10 +- src/fields/bls12_377Fr.nr | 9 +- src/fields/bls12_381Fq.nr | 8 +- src/fields/bls12_381Fr.nr | 9 +- src/fields/bn254Fq.nr | 23 +- src/fields/ed25519Fq.nr | 11 +- src/fields/ed25519Fr.nr | 9 +- src/fields/mnt4_753Fq.nr | 10 +- src/fields/mnt4_753Fr.nr | 10 +- src/fields/mnt6_753Fq.nr | 10 +- src/fields/mnt6_753Fr.nr | 10 +- src/fields/pallasFq.nr | 10 +- src/fields/pallasFr.nr | 10 +- src/fields/secp256k1Fq.nr | 9 +- src/fields/secp256k1Fr.nr | 9 +- src/fields/secp256r1Fq.nr | 9 +- src/fields/secp256r1Fr.nr | 9 +- src/fields/secp384r1Fq.nr | 9 +- src/fields/secp384r1Fr.nr | 9 +- src/fields/vestaFq.nr | 8 +- src/fields/vestaFr.nr | 8 +- src/lib.nr | 48 ++-- src/runtime_bignum.nr | 93 +++++--- src/runtime_bignum_test.nr | 336 +++++++++++++++++++++++---- src/utils/u60_representation_test.nr | 50 +++- 34 files changed, 653 insertions(+), 425 deletions(-) diff --git a/src/bignum_test.nr b/src/bignum_test.nr index c74875b0..a251119f 100644 --- a/src/bignum_test.nr +++ b/src/bignum_test.nr @@ -16,7 +16,6 @@ struct Test2048Params{} fn silence_warning() { let _ = Test2048Params {}; } - impl RuntimeBigNumParamsTrait<18> for Test2048Params { fn modulus_bits() -> u32 { 2048 @@ -24,79 +23,39 @@ impl RuntimeBigNumParamsTrait<18> for Test2048Params { } impl BigNumParamsTrait<18> for Test2048Params { fn get_instance() -> BigNumInstance<18, Self> { - let 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 - // ]} - let redc_param: [Field; 18] = [ - 0x000000000000000000000000000000000091697def7100cd5cf8d890b4ef2ec3, - 0x00000000000000000000000000000000006765ba8304214dac764d3f4adc3185, - 0x000000000000000000000000000000000048404bd14d927ea230e60d4bebf940, - 0x00000000000000000000000000000000007c4d53a23bacc251ecbfc4b7ba5a0b, - 0x000000000000000000000000000000000093eaf3499474a6f5b2fff83f1259c8, - 0x00000000000000000000000000000000005bff4c737b97281f1a5f2384a8c16d, - 0x000000000000000000000000000000000061b4cf2f55358476b5323782999055, - 0x00000000000000000000000000000000001e7a804e8eacfe3a2a5673bc3885b8, - 0x0000000000000000000000000000000000eabadeae4282906c817adf70eab4ae, - 0x0000000000000000000000000000000000166f7df257fe2bf27f0809aceed9b0, - 0x00000000000000000000000000000000007d90fb7428901b8bed11f6b81e36bf, - 0x0000000000000000000000000000000000f36e6ba885c60b7024c563605df7e0, - 0x000000000000000000000000000000000052b7c58d2fb5d2c8478963ae6d4a44, - 0x000000000000000000000000000000000036ee761de26635f114ccc3f7d74f85, - 0x0000000000000000000000000000000000e3fb726a10cf2220897513f05243de, - 0x0000000000000000000000000000000000f43a26bbd732496eb4d828591b8056, - 0x0000000000000000000000000000000000ff4e42304e60fb3a54fca735499f2c, - 0x0000000000000000000000000000000000000000000000000000000000000162 - ]; - BigNumInstance::new(modulus, redc_param) + test2048_Instance } fn modulus_bits() -> u32 { 2048 } } - +global test2048_Instance: BigNumInstance<18, Test2048Params> = BigNumInstance { + modulus: [ + 0xc0a197a5ae0fcdceb052c9732614fe, 0x656ae034423283422243918ab83be3, 0x6bf590da48a7c1070b7d5aabaac678, 0x0cce39f530238b606f24b296e2bda9, 0x01e1fef9bb9c1c3ead98f226f1bfa0, 0xad8c1c816e12e0ed1379055e373abf, 0xcebe80e474f753aa9d1461c435123d, 0xaee5a18ceedef88d115a8b93c167ad, 0x268ba83c4a65c4307427fc495d9e44, 0xdd2777926848667b7df79f342639d4, 0xf455074c96855ca0068668efe7da3d, 0x5ddba6b30bbc168bfb3a1225f27d65, 0x591fec484f36707524133bcd6f4258, 0x59641b756766aeebe66781dd01d062, 0x58bc5eaff4b165e142bf9e2480eebb, 0x667a3964f08e06df772ce64b229a72, 0x9c1fdb18907711bfe3e3c1cf918395, 0xb8 + ], + double_modulus: [ + 0x0181432f4b5c1f9b9d60a592e64c29fc, 0x01cad5c06884650684448723157077c6, 0x01d7eb21b4914f820e16fab557558cef, 0x01199c73ea604716c0de49652dc57b51, 0x0103c3fdf37738387d5b31e44de37f3f, 0x015b183902dc25c1da26f20abc6e757d, 0x019d7d01c8e9eea7553a28c3886a247a, 0x015dcb4319ddbdf11a22b5172782cf5a, 0x014d17507894cb8860e84ff892bb3c88, 0x01ba4eef24d090ccf6fbef3e684c73a7, 0x01e8aa0e992d0ab9400d0cd1dfcfb47a, 0x01bbb74d6617782d17f674244be4faca, 0x01b23fd8909e6ce0ea4826779ade84af, 0x01b2c836eacecd5dd7cccf03ba03a0c3, 0x01b178bd5fe962cbc2857f3c4901dd75, 0x01ccf472c9e11c0dbeee59cc964534e3, 0x01383fb63120ee237fc7c7839f230729, 0x0170 + ], + modulus_u60: U60Repr { limbs: [ + 0x0eb052c9732614fe, 0x0c0a197a5ae0fcdc, 0x022243918ab83be3, 0x0656ae0344232834, 0x070b7d5aabaac678, 0x06bf590da48a7c10, 0x6f24b296e2bda9, 0xcce39f530238b6, 0x0ead98f226f1bfa0, 0x1e1fef9bb9c1c3, 0x0d1379055e373abf, 0x0ad8c1c816e12e0e, 0x0a9d1461c435123d, 0x0cebe80e474f753a, 0x0d115a8b93c167ad, 0x0aee5a18ceedef88, 0x7427fc495d9e44, 0x0268ba83c4a65c43, 0x0b7df79f342639d4, 0x0dd2777926848667, 0x068668efe7da3d, 0x0f455074c96855ca, 0x0bfb3a1225f27d65, 0x05ddba6b30bbc168, 0x0524133bcd6f4258, 0x0591fec484f36707, 0x0be66781dd01d062, 0x059641b756766aee, 0x0142bf9e2480eebb, 0x058bc5eaff4b165e, 0x0f772ce64b229a72, 0x0667a3964f08e06d, 0x0fe3e3c1cf918395, 0x09c1fdb18907711b, 0xb8, 0x00]}, + modulus_u60_x4: U60Repr { limbs: [ + 0x0eb052c9732614fe, 0x0c0a197a5ae0fcdc, 0x022243918ab83be3, 0x0656ae0344232834, 0x070b7d5aabaac678, 0x06bf590da48a7c10, 0x6f24b296e2bda9, 0xcce39f530238b6, 0x0ead98f226f1bfa0, 0x1e1fef9bb9c1c3, 0x0d1379055e373abf, 0x0ad8c1c816e12e0e, 0x0a9d1461c435123d, 0x0cebe80e474f753a, 0x0d115a8b93c167ad, 0x0aee5a18ceedef88, 0x7427fc495d9e44, 0x0268ba83c4a65c43, 0x0b7df79f342639d4, 0x0dd2777926848667, 0x068668efe7da3d, 0x0f455074c96855ca, 0x0bfb3a1225f27d65, 0x05ddba6b30bbc168, 0x0524133bcd6f4258, 0x0591fec484f36707, 0x0be66781dd01d062, 0x059641b756766aee, 0x0142bf9e2480eebb, 0x058bc5eaff4b165e, 0x0f772ce64b229a72, 0x0667a3964f08e06d, 0x0fe3e3c1cf918395, 0x09c1fdb18907711b, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, + redc_param: [ + 0x1697def7100cd5cf8d890b4ef2ec3f, 0x765ba8304214dac764d3f4adc31859, 0x8404bd14d927ea230e60d4bebf9406, 0xc4d53a23bacc251ecbfc4b7ba5a0b4, 0x3eaf3499474a6f5b2fff83f1259c87, 0xbff4c737b97281f1a5f2384a8c16d9, 0x1b4cf2f55358476b53237829990555, 0xe7a804e8eacfe3a2a5673bc3885b86, 0xabadeae4282906c817adf70eab4ae1, 0x66f7df257fe2bf27f0809aceed9b0e, 0xd90fb7428901b8bed11f6b81e36bf1, 0x36e6ba885c60b7024c563605df7e07, 0x2b7c58d2fb5d2c8478963ae6d4a44f, 0x6ee761de26635f114ccc3f7d74f855, 0x3fb726a10cf2220897513f05243de3, 0x43a26bbd732496eb4d828591b8056e, 0xf4e42304e60fb3a54fca735499f2cf, 0x162f + ] +}; type Fq = BigNum<3, BNParams>; fn test_eq(_: 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]); - let c = BigNum::__derive_from_seed([2, 2, 3, 4]); + let a = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; + let b = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; + let c = unsafe { + BigNum::__derive_from_seed([2, 2, 3, 4]) + }; let modulus = BigNum::modulus(); let t0: U60Repr = (U60Repr::from(modulus.get().as_array())); @@ -128,8 +87,12 @@ fn test_eq(_: BigNum, __: [Field; N]) where BigNum: BigNumTr // // // 99689 // // // 929 gates for a 2048 bit mul fn test_mul(_: BigNum) where BigNum: BigNumTrait + std::ops::Mul + std::ops::Add { - let a: BigNum = BigNum::__derive_from_seed([1, 2, 3, 4]); - let b: BigNum = BigNum::__derive_from_seed([4, 5, 6, 7]); + let a: BigNum = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; + let b: BigNum = unsafe { + BigNum::__derive_from_seed([4, 5, 6, 7]) + }; let c = (a + b) * (a + b); let d = (a * a) + (b * b) + (a * b) + (a * b); @@ -137,8 +100,12 @@ fn test_mul(_: BigNum) where BigNum: BigNumTrait + std::ops::Mul + std:: } fn test_add(_: BigNum) where BigNum: BigNumTrait + std::ops::Add + std::ops::Mul + std::cmp::Eq { - let a = BigNum::__derive_from_seed([1, 2, 3, 4]); - let b: BigNum = BigNum::__derive_from_seed([4, 5, 6, 7]); + let a = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; + let b: BigNum = unsafe { + BigNum::__derive_from_seed([4, 5, 6, 7]) + }; let one = BigNum::one(); a.validate_in_range(); a.validate_in_field(); @@ -162,40 +129,62 @@ fn test_add(_: BigNum) where BigNum: BigNumTrait + std::ops::Add + std:: } fn test_div(_: BigNum) where BigNum: BigNumTrait + std::ops::Div + std::ops::Mul + std::ops::Add + std::cmp::Eq { - let a = BigNum::__derive_from_seed([1, 2, 3, 4]); - let b = BigNum::__derive_from_seed([4, 5, 6, 7]); + let a = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; + let b = unsafe { + BigNum::__derive_from_seed([4, 5, 6, 7]) + }; let c = a / b; assert((b * c) == (a)); } fn test_invmod(_: BigNum) where BigNum: BigNumTrait + std::cmp::Eq { - let u = BigNum::__derive_from_seed([1, 2, 3, 4]); + let u = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; for _ in 0..1 { - let v = u.__invmod(); - let result = u.__mul(v); + let v = unsafe { + u.__invmod() + }; + let result = unsafe { + u.__mul(v) + }; let expected = BigNum::one(); assert(result == expected); } } fn assert_is_not_equal(_: BigNum) where BigNum: BigNumTrait { - let a = BigNum::__derive_from_seed([1, 2, 3, 4]); - let b = BigNum::__derive_from_seed([4, 5, 6, 7]); + let a = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; + let b = unsafe { + BigNum::__derive_from_seed([4, 5, 6, 7]) + }; a.assert_is_not_equal(b); } fn assert_is_not_equal_fail(_: BigNum) where BigNum: BigNumTrait { - let a = BigNum::__derive_from_seed([1, 2, 3, 4]); - let b = BigNum::__derive_from_seed([1, 2, 3, 4]); + let a = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; + let b = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; a.assert_is_not_equal(b); } fn assert_is_not_equal_overloaded_lhs_fail(_: 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]); + let a = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; + let b = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; let modulus = BigNum::modulus(); @@ -206,8 +195,12 @@ fn assert_is_not_equal_overloaded_lhs_fail(_: BigNum, __: [F } fn assert_is_not_equal_overloaded_rhs_fail(_: 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]); + let a = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; + let b = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; let modulus = BigNum::modulus(); @@ -218,8 +211,12 @@ fn assert_is_not_equal_overloaded_rhs_fail(_: BigNum, __: [F } fn assert_is_not_equal_overloaded_fail(_: 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]); + let a = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; + let b = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; let modulus = BigNum::modulus(); @@ -466,13 +463,17 @@ fn test_assert_is_not_equal_overloaded_fail_U256() { type U256 = BigNum<3, U256Params>; #[test] fn test_udiv_mod_U256() { - let a: U256 = BigNum::__derive_from_seed([1, 2, 3, 4]); + let a: U256 = unsafe { + BigNum::__derive_from_seed([1, 2, 3, 4]) + }; let b: U256 = BigNum::from_array([12, 0, 0]); let (q, r) = a.udiv_mod(b); // let qb = q.__mul(b); - let product = q.__mul(b).__add(r); + let product = unsafe { + q.__mul(b).__add(r) + }; assert(product.eq(a)); } @@ -555,7 +556,9 @@ fn test_2048_bit_quadratic_expression() { let a_bn: BigNum<18, Test2048Params> = BigNum { limbs: a }; let b_bn: BigNum<18, Test2048Params> = BigNum { limbs: b }; - let c_bn = a_bn.__mul(b_bn); + let c_bn = unsafe { + a_bn.__mul(b_bn) + }; assert(c_bn.limbs == c_expected); a_bn.validate_in_range(); @@ -584,7 +587,9 @@ fn test_expressions() { 0x0 ] }; - let yy = y.__add(y); + let yy = unsafe { + y.__add(y) + }; assert(yy.limbs == z.limbs); @@ -616,10 +621,18 @@ fn test_expressions() { 0x0000000000000000000000000000000000000000000000000000000000000f93 ] }; - let wx = w.__mul(x); - let uv = uu.__mul(vv); - let y = (uv.__add(wx)).__neg(); - let z = uv.__add(wx); + let wx = unsafe { + w.__mul(x) + }; + let uv = unsafe { + uu.__mul(vv) + }; + let y = unsafe { + (uv.__add(wx)).__neg() + }; + let z = unsafe { + uv.__add(wx) + }; BigNum::evaluate_quadratic_expression( [[uu], [w]], diff --git a/src/fields/U1024.nr b/src/fields/U1024.nr index a024bbbe..8843a68f 100644 --- a/src/fields/U1024.nr +++ b/src/fields/U1024.nr @@ -1,16 +1,13 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct U1024Params {} +pub struct U1024Params {} impl RuntimeBigNumParamsTrait<9> for U1024Params { fn modulus_bits() -> u32 { 1025 } - fn has_multiplicative_inverse() -> bool { - false - } } impl BigNumParamsTrait<9> for U1024Params { fn get_instance() -> BigNumInstance<9, Self> { @@ -19,11 +16,8 @@ impl BigNumParamsTrait<9> for U1024Params { fn modulus_bits() -> u32 { 1025 } - fn has_multiplicative_inverse() -> bool { - false - } } -global U1024_Instance: BigNumInstance<9, U1024Params> = BigNumInstance { +pub global U1024_Instance: BigNumInstance<9, U1024Params> = BigNumInstance { modulus: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x010000000000000000 ], @@ -35,7 +29,6 @@ global U1024_Instance: BigNumInstance<9, U1024Params> = BigNumInstance { modulus_u60_x4: U60Repr { limbs: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x040000000000000000 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x400000000000000000 ] }; - diff --git a/src/fields/U2048.nr b/src/fields/U2048.nr index 3394e44f..1ead8cc5 100644 --- a/src/fields/U2048.nr +++ b/src/fields/U2048.nr @@ -1,16 +1,13 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct U2048Params {} +pub struct U2048Params {} impl RuntimeBigNumParamsTrait<18> for U2048Params { fn modulus_bits() -> u32 { 2049 } - fn has_multiplicative_inverse() -> bool { - false - } } impl BigNumParamsTrait<18> for U2048Params { fn get_instance() -> BigNumInstance<18, Self> { @@ -19,11 +16,8 @@ impl BigNumParamsTrait<18> for U2048Params { fn modulus_bits() -> u32 { 2049 } - fn has_multiplicative_inverse() -> bool { - false - } } -global U2048_Instance: BigNumInstance<18, U2048Params> = BigNumInstance { +pub global U2048_Instance: BigNumInstance<18, U2048Params> = BigNumInstance { modulus: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0100 ], @@ -35,7 +29,6 @@ global U2048_Instance: BigNumInstance<18, U2048Params> = BigNumInstance { modulus_u60_x4: U60Repr { limbs: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0100, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0400 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4000 ] }; - diff --git a/src/fields/U256.nr b/src/fields/U256.nr index 258ef2e5..66c2996f 100644 --- a/src/fields/U256.nr +++ b/src/fields/U256.nr @@ -1,16 +1,13 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct U256Params {} +pub struct U256Params {} impl RuntimeBigNumParamsTrait<3> for U256Params { fn modulus_bits() -> u32 { 257 } - fn has_multiplicative_inverse() -> bool { - false - } } impl BigNumParamsTrait<3> for U256Params { fn get_instance() -> BigNumInstance<3, Self> { @@ -19,11 +16,8 @@ impl BigNumParamsTrait<3> for U256Params { fn modulus_bits() -> u32 { 257 } - fn has_multiplicative_inverse() -> bool { - false - } } -global U256_Instance: BigNumInstance<3, U256Params> = BigNumInstance { +pub global U256_Instance: BigNumInstance<3, U256Params> = BigNumInstance { modulus: [ 0x00, 0x00, 0x010000 ], @@ -35,6 +29,6 @@ global U256_Instance: BigNumInstance<3, U256Params> = BigNumInstance { modulus_u60_x4: U60Repr { limbs: [ 0x00, 0x00, 0x00, 0x00, 0x010000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x00, 0x00, 0x040000 + 0x00, 0x00, 0x400000 ] }; diff --git a/src/fields/U384.nr b/src/fields/U384.nr index b827d680..9723fe64 100644 --- a/src/fields/U384.nr +++ b/src/fields/U384.nr @@ -1,29 +1,23 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct U384_Params {} -impl RuntimeBigNumParamsTrait<4> for U384_Params { +pub struct U384Params {} +impl RuntimeBigNumParamsTrait<4> for U384Params { fn modulus_bits() -> u32 { 385 } - fn has_multiplicative_inverse() -> bool { - false - } } -impl BigNumParamsTrait<4> for U384_Params { +impl BigNumParamsTrait<4> for U384Params { fn get_instance() -> BigNumInstance<4, Self> { U384_Instance } fn modulus_bits() -> u32 { 385 } - fn has_multiplicative_inverse() -> bool { - false - } } -global U384_Instance: BigNumInstance<4, U384_Params> = BigNumInstance { +pub global U384_Instance: BigNumInstance<4, U384Params> = BigNumInstance { modulus: [ 0x00, 0x00, 0x00, 0x01000000 ], @@ -35,7 +29,6 @@ global U384_Instance: BigNumInstance<4, U384_Params> = BigNumInstance { modulus_u60_x4: U60Repr { limbs: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01000000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x00, 0x00, 0x00, 0x04000000 + 0x00, 0x00, 0x00, 0x40000000 ] }; - diff --git a/src/fields/U4096.nr b/src/fields/U4096.nr index 806e2a5d..5273bd5e 100644 --- a/src/fields/U4096.nr +++ b/src/fields/U4096.nr @@ -1,16 +1,13 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct U4096Params {} +pub struct U4096Params {} impl RuntimeBigNumParamsTrait<35> for U4096Params { fn modulus_bits() -> u32 { 4097 } - fn has_multiplicative_inverse() -> bool { - false - } } impl BigNumParamsTrait<35> for U4096Params { fn get_instance() -> BigNumInstance<35, Self> { @@ -19,11 +16,8 @@ impl BigNumParamsTrait<35> for U4096Params { fn modulus_bits() -> u32 { 4097 } - fn has_multiplicative_inverse() -> bool { - false - } } -global U4096_Instance: BigNumInstance<35, U4096Params> = BigNumInstance { +pub global U4096_Instance: BigNumInstance<35, U4096Params> = BigNumInstance { modulus: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x010000 ], @@ -35,7 +29,6 @@ global U4096_Instance: BigNumInstance<35, U4096Params> = BigNumInstance { modulus_u60_x4: U60Repr { limbs: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x010000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x040000 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x400000 ] }; - diff --git a/src/fields/U512.nr b/src/fields/U512.nr index b49c20bc..38646af4 100644 --- a/src/fields/U512.nr +++ b/src/fields/U512.nr @@ -1,19 +1,13 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -// a / b = c - -// b * c + (rem < modulus) = a -pub struct U512Params {} +pub struct U512Params {} impl RuntimeBigNumParamsTrait<5> for U512Params { fn modulus_bits() -> u32 { 513 } - fn has_multiplicative_inverse() -> bool { - false - } } impl BigNumParamsTrait<5> for U512Params { fn get_instance() -> BigNumInstance<5, Self> { @@ -22,11 +16,8 @@ impl BigNumParamsTrait<5> for U512Params { fn modulus_bits() -> u32 { 513 } - fn has_multiplicative_inverse() -> bool { - false - } } -global U512_Instance: BigNumInstance<5, U512Params> = BigNumInstance { +pub global U512_Instance: BigNumInstance<5, U512Params> = BigNumInstance { modulus: [ 0x00, 0x00, 0x00, 0x00, 0x0100000000 ], @@ -38,6 +29,6 @@ global U512_Instance: BigNumInstance<5, U512Params> = BigNumInstance { modulus_u60_x4: U60Repr { limbs: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0100000000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x00, 0x00, 0x00, 0x00, 0x0400000000 + 0x00, 0x00, 0x00, 0x00, 0x4000000000 ] }; diff --git a/src/fields/U768.nr b/src/fields/U768.nr index b5a41ec7..b8828eab 100644 --- a/src/fields/U768.nr +++ b/src/fields/U768.nr @@ -1,41 +1,34 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct U768Params {} -impl RuntimeBigNumParamsTrait<13> for U768Params { +pub struct U768Params {} +impl RuntimeBigNumParamsTrait<7> for U768Params { fn modulus_bits() -> u32 { - 1537 - } - fn has_multiplicative_inverse() -> bool { - false + 769 } } -impl BigNumParamsTrait<13> for U768Params { - fn get_instance() -> BigNumInstance<13, Self> { +impl BigNumParamsTrait<7> for U768Params { + fn get_instance() -> BigNumInstance<7, Self> { U768_Instance } fn modulus_bits() -> u32 { - 1537 - } - fn has_multiplicative_inverse() -> bool { - false + 769 } } -global U768_Instance: BigNumInstance<13, U768Params> = BigNumInstance { +pub global U768_Instance: BigNumInstance<7, U768Params> = BigNumInstance { modulus: [ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01000000000000000000000000 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01000000000000 ], double_modulus: [ - 0x01000000000000000000000000000000, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0x01ffffffffffffffffffffffff + 0x01000000000000000000000000000000, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0xffffffffffffffffffffffffffffff, 0x01ffffffffffff ], modulus_u60: U60Repr { limbs: [ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1000000000]}, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01000000000000, 0x00]}, modulus_u60_x4: U60Repr { limbs: [ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1000000000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01000000000000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04000000000000000000000000 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40000000000000 ] }; - diff --git a/src/fields/U8192.nr b/src/fields/U8192.nr index 089e16bb..c6484126 100644 --- a/src/fields/U8192.nr +++ b/src/fields/U8192.nr @@ -1,16 +1,13 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct U8192Params {} +pub struct U8192Params {} impl RuntimeBigNumParamsTrait<69> for U8192Params { fn modulus_bits() -> u32 { 8193 } - fn has_multiplicative_inverse() -> bool { - false - } } impl BigNumParamsTrait<69> for U8192Params { fn get_instance() -> BigNumInstance<69, Self> { @@ -19,11 +16,8 @@ impl BigNumParamsTrait<69> for U8192Params { fn modulus_bits() -> u32 { 8193 } - fn has_multiplicative_inverse() -> bool { - false - } } -global U8192_Instance: BigNumInstance<69, U8192Params> = BigNumInstance { +pub global U8192_Instance: BigNumInstance<69, U8192Params> = BigNumInstance { modulus: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0100000000 ], @@ -35,7 +29,6 @@ global U8192_Instance: BigNumInstance<69, U8192Params> = BigNumInstance { modulus_u60_x4: U60Repr { limbs: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0100000000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0400000000 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4000000000 ] }; - diff --git a/src/fields/bls12_377Fq.nr b/src/fields/bls12_377Fq.nr index 7c13ca1b..32cb87b7 100644 --- a/src/fields/bls12_377Fq.nr +++ b/src/fields/bls12_377Fq.nr @@ -1,5 +1,5 @@ //! Blurb sourced from https://github.com/arkworks-rs -//! This library implements the base field of the BLS12_377 curve generated in [[BCGMMW20, "Zexe"]](https://eprint.iacr.org/2018/962). +//! This library implements the base field of the BLS12_377 curve generated in [[BCGMMW20, "Zexe"]]((https://eprint.iacr.org/2018/962). //! The name denotes that it is a Barreto--Lynn--Scott curve of embedding degree //! 12, defined over a 377-bit (prime) field. The main feature of this curve is //! that both the scalar field and the base field are highly 2-adic. @@ -20,8 +20,7 @@ use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct BLS12_377_Fq_Params {} +pub struct BLS12_377_Fq_Params {} impl RuntimeBigNumParamsTrait<4> for BLS12_377_Fq_Params { fn modulus_bits() -> u32 { 377 @@ -35,7 +34,7 @@ impl BigNumParamsTrait<4> for BLS12_377_Fq_Params { 377 } } -global BLS12_377_Fq_Instance: BigNumInstance<4, BLS12_377_Fq_Params> = BigNumInstance { +pub global BLS12_377_Fq_Instance: BigNumInstance<4, BLS12_377_Fq_Params> = BigNumInstance { modulus: [ 0x0b5d44300000008508c00000000001, 0xd9f300f5138f1ef3622fba09480017, 0x4617c510eac63b05c06ca1493b1a22, 0x01ae3a ], @@ -47,7 +46,6 @@ global BLS12_377_Fq_Instance: BigNumInstance<4, BLS12_377_Fq_Params> = BigNumIns modulus_u60_x4: U60Repr { limbs: [ 0x0508c00000000001, 0xb5d44300000008, 0x03622fba09480017, 0x0d9f300f5138f1ef, 0x05c06ca1493b1a22, 0x04617c510eac63b0, 0x01ae3a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0xed687789c42a591f9fd58c5e4daffc, 0xd0de6776b1a06af2d488d85a6d02d0, 0x8d0cc4060e976c3ca0582ef4f73bba, 0x026150 + 0xd687789c42a591f9fd58c5e4daffcc, 0x0de6776b1a06af2d488d85a6d02d0e, 0xd0cc4060e976c3ca0582ef4f73bbad, 0x261508 ] }; - diff --git a/src/fields/bls12_377Fr.nr b/src/fields/bls12_377Fr.nr index c44c18cf..ee3ac2ee 100644 --- a/src/fields/bls12_377Fr.nr +++ b/src/fields/bls12_377Fr.nr @@ -16,12 +16,12 @@ //! * G1 curve equation: y^2 = x^3 + 1 //! * G2 curve equation: y^2 = x^3 + B, where //! * B = Fq2(0, 155198655607781456406391640216936120121836107652948796323930557600032281009004493664981332883744016074664192874906) + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct BLS12_377_Fr_Params {} +pub struct BLS12_377_Fr_Params {} impl RuntimeBigNumParamsTrait<3> for BLS12_377_Fr_Params { fn modulus_bits() -> u32 { 253 @@ -35,7 +35,7 @@ impl BigNumParamsTrait<3> for BLS12_377_Fr_Params { 253 } } -global BLS12_377_Fr_Instance: BigNumInstance<3, BLS12_377_Fr_Params> = BigNumInstance { +pub global BLS12_377_Fr_Instance: BigNumInstance<3, BLS12_377_Fr_Params> = BigNumInstance { modulus: [ 0xaa76fed00000010a11800000000001, 0x655e9a2ca55660b44d1e5c37b00159, 0x12ab ], @@ -47,7 +47,6 @@ global BLS12_377_Fr_Instance: BigNumInstance<3, BLS12_377_Fr_Params> = BigNumIns modulus_u60_x4: U60Repr { limbs: [ 0x0a11800000000001, 0x0aa76fed00000010, 0x044d1e5c37b00159, 0x0655e9a2ca55660b, 0x12ab, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x0a180b8d69e258f5204c21151e79ea, 0x491ec40b2c9ee4e51e49faa80548fd, 0x36d9 + 0xa180b8d69e258f5204c21151e79ea1, 0x91ec40b2c9ee4e51e49faa80548fd0, 0x036d94 ] }; - diff --git a/src/fields/bls12_381Fq.nr b/src/fields/bls12_381Fq.nr index f788c44f..c91b4f34 100644 --- a/src/fields/bls12_381Fq.nr +++ b/src/fields/bls12_381Fq.nr @@ -18,8 +18,7 @@ use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct BLS12_381_Fq_Params {} +pub struct BLS12_381_Fq_Params {} impl RuntimeBigNumParamsTrait<4> for BLS12_381_Fq_Params { fn modulus_bits() -> u32 { 381 @@ -33,7 +32,7 @@ impl BigNumParamsTrait<4> for BLS12_381_Fq_Params { 381 } } -global BLS12_381_Fq_Instance: BigNumInstance<4, BLS12_381_Fq_Params> = BigNumInstance { +pub global BLS12_381_Fq_Instance: BigNumInstance<4, BLS12_381_Fq_Params> = BigNumInstance { modulus: [ 0xabfffeb153ffffb9feffffffffaaab, 0x4b84f38512bf6730d2a0f6b0f6241e, 0xea397fe69a4b1ba7b6434bacd76477, 0x1a0111 ], @@ -45,7 +44,6 @@ global BLS12_381_Fq_Instance: BigNumInstance<4, BLS12_381_Fq_Params> = BigNumIns modulus_u60_x4: U60Repr { limbs: [ 0x09feffffffffaaab, 0x0abfffeb153ffffb, 0xd2a0f6b0f6241e, 0x04b84f38512bf673, 0x07b6434bacd76477, 0x0ea397fe69a4b1ba, 0x1a0111, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x65c59e8163c701ec4f881fd59646e8, 0x09d07fda82a52f7d1dc780a19de74e, 0x4bcf32791738a0406c331e9ae8a46e, 0x2760d7 + 0x5c59e8163c701ec4f881fd59646e8b, 0x9d07fda82a52f7d1dc780a19de74e6, 0xbcf32791738a0406c331e9ae8a46e0, 0x02760d74 ] }; - diff --git a/src/fields/bls12_381Fr.nr b/src/fields/bls12_381Fr.nr index 89b4a1dd..913b38a7 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 scalar field of the BLS12_381 curve generated by [Sean Bowe](https://electriccoin.co/blog/new-snark-curve/). +//! This library implements the base 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 @@ -18,8 +18,7 @@ use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct BLS12_381_Fr_Params {} +pub struct BLS12_381_Fr_Params {} impl RuntimeBigNumParamsTrait<3> for BLS12_381_Fr_Params { fn modulus_bits() -> u32 { 255 @@ -33,7 +32,7 @@ impl BigNumParamsTrait<3> for BLS12_381_Fr_Params { 255 } } -global BLS12_381_Fr_Instance: BigNumInstance<3, BLS12_381_Fr_Params> = BigNumInstance { +pub global BLS12_381_Fr_Instance: BigNumInstance<3, BLS12_381_Fr_Params> = BigNumInstance { modulus: [ 0xbda402fffe5bfeffffffff00000001, 0xa753299d7d483339d80809a1d80553, 0x73ed ], @@ -45,6 +44,6 @@ global BLS12_381_Fr_Instance: BigNumInstance<3, BLS12_381_Fr_Params> = BigNumIns modulus_u60_x4: U60Repr { limbs: [ 0x0fffffff00000001, 0x0bda402fffe5bfef, 0x09d80809a1d80553, 0x0a753299d7d48333, 0x73ed, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x410fad2f92eb5c509cde80830358e4, 0x253b7fb78ddf0e2d772dc1f823b4d9, 0x8d54 + 0x10fad2f92eb5c509cde80830358e4c, 0x53b7fb78ddf0e2d772dc1f823b4d94, 0x08d542 ] }; diff --git a/src/fields/bn254Fq.nr b/src/fields/bn254Fq.nr index 0d40eba2..c29850aa 100644 --- a/src/fields/bn254Fq.nr +++ b/src/fields/bn254Fq.nr @@ -2,15 +2,21 @@ use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - pub struct BNParams {} impl RuntimeBigNumParamsTrait<3> for BNParams { fn modulus_bits() -> u32 { 254 } } - -global BN254INSTANCE: BigNumInstance<3, BNParams> = BigNumInstance { +impl BigNumParamsTrait<3> for BNParams { + fn get_instance() -> BigNumInstance<3, Self> { + BN254INSTANCE + } + fn modulus_bits() -> u32 { + 254 + } +} +pub global BN254INSTANCE: BigNumInstance<3, BNParams> = BigNumInstance { modulus: [ 0x816a916871ca8d3c208c16d87cfd47, 0x4e72e131a029b85045b68181585d97, 0x3064 ], @@ -22,15 +28,6 @@ global BN254INSTANCE: BigNumInstance<3, BNParams> = BigNumInstance { modulus_u60_x4: U60Repr { limbs: [ 0x0c208c16d87cfd47, 0x0816a916871ca8d3, 0x45b68181585d97, 0x04e72e131a029b85, 0x3064, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x965e1767cd4c086f3aed8a19bf90e5, 0x7462623a04a7ab074a5868073013ae, 0x54a4 + 0x65e1767cd4c086f3aed8a19bf90e51, 0x462623a04a7ab074a5868073013ae9, 0x054a47 ] }; - -impl BigNumParamsTrait<3> for BNParams { - fn get_instance() -> BigNumInstance<3, Self> { - BN254INSTANCE - } - fn modulus_bits() -> u32 { - 254 - } -} diff --git a/src/fields/ed25519Fq.nr b/src/fields/ed25519Fq.nr index cf7cb667..66b292f5 100644 --- a/src/fields/ed25519Fq.nr +++ b/src/fields/ed25519Fq.nr @@ -1,9 +1,9 @@ + +use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; -use crate::BigNumParamsTrait as BigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct ED25519_Fq_Params {} +pub struct ED25519_Fq_Params {} impl RuntimeBigNumParamsTrait<3> for ED25519_Fq_Params { fn modulus_bits() -> u32 { 255 @@ -17,7 +17,7 @@ impl BigNumParamsTrait<3> for ED25519_Fq_Params { 255 } } -global ED25519_Fq_Instance: BigNumInstance<3, ED25519_Fq_Params> = BigNumInstance { +pub global ED25519_Fq_Instance: BigNumInstance<3, ED25519_Fq_Params> = BigNumInstance { modulus: [ 0xffffffffffffffffffffffffffffed, 0xffffffffffffffffffffffffffffff, 0x7fff ], @@ -29,7 +29,6 @@ global ED25519_Fq_Instance: BigNumInstance<3, ED25519_Fq_Params> = BigNumInstanc modulus_u60_x4: U60Repr { limbs: [ 0x0fffffffffffffed, 0x0fffffffffffffff, 0x0fffffffffffffff, 0x0fffffffffffffff, 0x7fff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x13, 0x00, 0x8000 + 0x0130, 0x00, 0x080000 ] }; - diff --git a/src/fields/ed25519Fr.nr b/src/fields/ed25519Fr.nr index 26734582..fba4d52c 100644 --- a/src/fields/ed25519Fr.nr +++ b/src/fields/ed25519Fr.nr @@ -1,9 +1,9 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct ED25519_Fr_Params {} +pub struct ED25519_Fr_Params {} impl RuntimeBigNumParamsTrait<3> for ED25519_Fr_Params { fn modulus_bits() -> u32 { 253 @@ -17,7 +17,7 @@ impl BigNumParamsTrait<3> for ED25519_Fr_Params { 253 } } -global ED25519_Fr_Instance: BigNumInstance<3, ED25519_Fr_Params> = BigNumInstance { +pub global ED25519_Fr_Instance: BigNumInstance<3, ED25519_Fr_Params> = BigNumInstance { modulus: [ 0xdef9dea2f79cd65812631a5cf5d3ed, 0x14, 0x1000 ], @@ -29,7 +29,6 @@ global ED25519_Fr_Instance: BigNumInstance<3, ED25519_Fr_Params> = BigNumInstanc modulus_u60_x4: U60Repr { limbs: [ 0x0812631a5cf5d3ed, 0x0def9dea2f79cd65, 0x14, 0x00, 0x1000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x84188574218ca69fb673968c28b04c, 0xffffffffffffffffffffffffffffac, 0x3fff + 0x4188574218ca69fb673968c28b04c6, 0xfffffffffffffffffffffffffffac8, 0x03ffff ] }; - diff --git a/src/fields/mnt4_753Fq.nr b/src/fields/mnt4_753Fq.nr index d2b4df2c..e0a85991 100644 --- a/src/fields/mnt4_753Fq.nr +++ b/src/fields/mnt4_753Fq.nr @@ -1,6 +1,6 @@ //! Blurb sourced from https://github.com/arkworks-rs //! This library implements the base field of the MNT4_753 curve generated in -//! [\[BCTV14\]](https://eprint.iacr.org/2014/595). The name denotes that it is a +//! [[BCTV14]](https://eprint.iacr.org/2014/595). The name denotes that it is a //! Miyaji--Nakabayashi--Takano curve of embedding degree 4, defined over a //! 753-bit (prime) field. The main feature of this curve is that its scalar //! field and base field respectively equal the base field and scalar field of @@ -23,8 +23,7 @@ use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct MNT4_753_Fq_Params {} +pub struct MNT4_753_Fq_Params {} impl RuntimeBigNumParamsTrait<7> for MNT4_753_Fq_Params { fn modulus_bits() -> u32 { 753 @@ -38,7 +37,7 @@ impl BigNumParamsTrait<7> for MNT4_753_Fq_Params { 753 } } -global MNT4_753_Fq_Instance: BigNumInstance<7, MNT4_753_Fq_Params> = BigNumInstance { +pub global MNT4_753_Fq_Instance: BigNumInstance<7, MNT4_753_Fq_Params> = BigNumInstance { modulus: [ 0x9d54522cdd119f5e9063de245e8001, 0xcce9767254a4638810719ac425f0e3, 0x76f218059db80f0da5cb537e38685a, 0xe8a0ed8d99d124d9a15af79db117e7, 0x8fafed5eb7e8f96c97d87307fdb925, 0xc41110229022eee2cdadb7f997505b, 0x01c4c62d92 ], @@ -50,7 +49,6 @@ global MNT4_753_Fq_Instance: BigNumInstance<7, MNT4_753_Fq_Params> = BigNumInsta modulus_u60_x4: U60Repr { limbs: [ 0x0e9063de245e8001, 0x09d54522cdd119f5, 0x0810719ac425f0e3, 0x0cce9767254a4638, 0x0da5cb537e38685a, 0x076f218059db80f0, 0x09a15af79db117e7, 0x0e8a0ed8d99d124d, 0x0c97d87307fdb925, 0x08fafed5eb7e8f96, 0x02cdadb7f997505b, 0x0c41110229022eee, 0x01c4c62d92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x45dcc95da918349f4965a2aee8fd75, 0x282372b75580c27c4d1f1f57a96a11, 0xd58326e3c0552419849e3c7171d852, 0xa300e0ede1965cbf72f0aa9bf03479, 0x67fb932cae2aba9a5d17c1ff73538b, 0xa82727c6eaef38056aaa0aaedb0574, 0x0242f916cf + 0x5dcc95da918349f4965a2aee8fd750, 0x82372b75580c27c4d1f1f57a96a114, 0x58326e3c0552419849e3c7171d8522, 0x300e0ede1965cbf72f0aa9bf03479d, 0x7fb932cae2aba9a5d17c1ff73538ba, 0x82727c6eaef38056aaa0aaedb05746, 0x242f916cfa ] }; - diff --git a/src/fields/mnt4_753Fr.nr b/src/fields/mnt4_753Fr.nr index 4142fe87..033fb4dc 100644 --- a/src/fields/mnt4_753Fr.nr +++ b/src/fields/mnt4_753Fr.nr @@ -1,6 +1,6 @@ //! Blurb sourced from https://github.com/arkworks-rs //! This library implements the scalar field of the MNT4_753 curve generated in -//! [\[BCTV14\]](https://eprint.iacr.org/2014/595). The name denotes that it is a +//! [[BCTV14]](https://eprint.iacr.org/2014/595). The name denotes that it is a //! Miyaji--Nakabayashi--Takano curve of embedding degree 4, defined over a //! 753-bit (prime) field. The main feature of this curve is that its scalar //! field and base field respectively equal the base field and scalar field of @@ -23,8 +23,7 @@ use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct MNT4_753_Fr_Params {} +pub struct MNT4_753_Fr_Params {} impl RuntimeBigNumParamsTrait<7> for MNT4_753_Fr_Params { fn modulus_bits() -> u32 { 753 @@ -38,7 +37,7 @@ impl BigNumParamsTrait<7> for MNT4_753_Fr_Params { 753 } } -global MNT4_753_Fr_Instance: BigNumInstance<7, MNT4_753_Fr_Params> = BigNumInstance { +pub global MNT4_753_Fr_Instance: BigNumInstance<7, MNT4_753_Fr_Params> = BigNumInstance { modulus: [ 0xa099170fa13a4fd90776e240000001, 0xf97634993aa4d6c381bc3f0057974e, 0x28c859a99b3eebca9429212636b9df, 0xe8a0ed8d99d124d9a15af79db26c5c, 0x8fafed5eb7e8f96c97d87307fdb925, 0xc41110229022eee2cdadb7f997505b, 0x01c4c62d92 ], @@ -50,7 +49,6 @@ global MNT4_753_Fr_Instance: BigNumInstance<7, MNT4_753_Fr_Params> = BigNumInsta modulus_u60_x4: U60Repr { limbs: [ 0x090776e240000001, 0x0a099170fa13a4fd, 0x0381bc3f0057974e, 0x0f97634993aa4d6c, 0x0a9429212636b9df, 0x028c859a99b3eebc, 0x09a15af79db26c5c, 0x0e8a0ed8d99d124d, 0x0c97d87307fdb925, 0x08fafed5eb7e8f96, 0x02cdadb7f997505b, 0x0c41110229022eee, 0x01c4c62d92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0xa2dae11d15867718ec70f5ff059bba, 0x276d65fe7e00ba391da260f2623ff9, 0x8140a086edaa60c58eb476bdedcb35, 0xa300e0ede1965cbf72f0aa9bee8120, 0x67fb932cae2aba9a5d17c1ff73538b, 0xa82727c6eaef38056aaa0aaedb0574, 0x0242f916cf + 0x2dae11d15867718ec70f5ff059bba2, 0x76d65fe7e00ba391da260f2623ff9a, 0x140a086edaa60c58eb476bdedcb352, 0x300e0ede1965cbf72f0aa9bee81208, 0x7fb932cae2aba9a5d17c1ff73538ba, 0x82727c6eaef38056aaa0aaedb05746, 0x242f916cfa ] }; - diff --git a/src/fields/mnt6_753Fq.nr b/src/fields/mnt6_753Fq.nr index c581c569..41ede6e4 100644 --- a/src/fields/mnt6_753Fq.nr +++ b/src/fields/mnt6_753Fq.nr @@ -1,6 +1,6 @@ //! Blurb sourced from https://github.com/arkworks-rs //! This library implements the base field of the MNT6_753 curve generated in -//! [\[BCTV14\]](https://eprint.iacr.org/2014/595). The name denotes that it is a +//! [[BCTV14]](https://eprint.iacr.org/2014/595). The name denotes that it is a //! Miyaji--Nakabayashi--Takano curve of embedding degree 6, defined over a //! 753-bit (prime) field. The main feature of this curve is that its scalar //! field and base field respectively equal the base field and scalar field of @@ -23,8 +23,7 @@ use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct MNT6_753_Fq_Params {} +pub struct MNT6_753_Fq_Params {} impl RuntimeBigNumParamsTrait<7> for MNT6_753_Fq_Params { fn modulus_bits() -> u32 { 753 @@ -38,7 +37,7 @@ impl BigNumParamsTrait<7> for MNT6_753_Fq_Params { 753 } } -global MNT6_753_Fq_Instance: BigNumInstance<7, MNT6_753_Fq_Params> = BigNumInstance { +pub global MNT6_753_Fq_Instance: BigNumInstance<7, MNT6_753_Fq_Params> = BigNumInstance { modulus: [ 0xa099170fa13a4fd90776e240000001, 0xf97634993aa4d6c381bc3f0057974e, 0x28c859a99b3eebca9429212636b9df, 0xe8a0ed8d99d124d9a15af79db26c5c, 0x8fafed5eb7e8f96c97d87307fdb925, 0xc41110229022eee2cdadb7f997505b, 0x01c4c62d92 ], @@ -50,7 +49,6 @@ global MNT6_753_Fq_Instance: BigNumInstance<7, MNT6_753_Fq_Params> = BigNumInsta modulus_u60_x4: U60Repr { limbs: [ 0x090776e240000001, 0x0a099170fa13a4fd, 0x0381bc3f0057974e, 0x0f97634993aa4d6c, 0x0a9429212636b9df, 0x028c859a99b3eebc, 0x09a15af79db26c5c, 0x0e8a0ed8d99d124d, 0x0c97d87307fdb925, 0x08fafed5eb7e8f96, 0x02cdadb7f997505b, 0x0c41110229022eee, 0x01c4c62d92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0xa2dae11d15867718ec70f5ff059bba, 0x276d65fe7e00ba391da260f2623ff9, 0x8140a086edaa60c58eb476bdedcb35, 0xa300e0ede1965cbf72f0aa9bee8120, 0x67fb932cae2aba9a5d17c1ff73538b, 0xa82727c6eaef38056aaa0aaedb0574, 0x0242f916cf + 0x2dae11d15867718ec70f5ff059bba2, 0x76d65fe7e00ba391da260f2623ff9a, 0x140a086edaa60c58eb476bdedcb352, 0x300e0ede1965cbf72f0aa9bee81208, 0x7fb932cae2aba9a5d17c1ff73538ba, 0x82727c6eaef38056aaa0aaedb05746, 0x242f916cfa ] }; - diff --git a/src/fields/mnt6_753Fr.nr b/src/fields/mnt6_753Fr.nr index cb83e360..9d67e421 100644 --- a/src/fields/mnt6_753Fr.nr +++ b/src/fields/mnt6_753Fr.nr @@ -1,6 +1,6 @@ //! Blurb sourced from https://github.com/arkworks-rs //! This library implements the scalar field of the MNT6_753 curve generated in -//! [\[BCTV14\]](https://eprint.iacr.org/2014/595). The name denotes that it is a +//! [[BCTV14]](https://eprint.iacr.org/2014/595). The name denotes that it is a //! Miyaji--Nakabayashi--Takano curve of embedding degree 6, defined over a //! 753-bit (prime) field. The main feature of this curve is that its scalar //! field and base field respectively equal the base field and scalar field of @@ -23,8 +23,7 @@ use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct MNT6_753_Fr_Params {} +pub struct MNT6_753_Fr_Params {} impl RuntimeBigNumParamsTrait<7> for MNT6_753_Fr_Params { fn modulus_bits() -> u32 { 753 @@ -38,7 +37,7 @@ impl BigNumParamsTrait<7> for MNT6_753_Fr_Params { 753 } } -global MNT6_753_Fr_Instance: BigNumInstance<7, MNT6_753_Fr_Params> = BigNumInstance { +pub global MNT6_753_Fr_Instance: BigNumInstance<7, MNT6_753_Fr_Params> = BigNumInstance { modulus: [ 0x9d54522cdd119f5e9063de245e8001, 0xcce9767254a4638810719ac425f0e3, 0x76f218059db80f0da5cb537e38685a, 0xe8a0ed8d99d124d9a15af79db117e7, 0x8fafed5eb7e8f96c97d87307fdb925, 0xc41110229022eee2cdadb7f997505b, 0x01c4c62d92 ], @@ -50,7 +49,6 @@ global MNT6_753_Fr_Instance: BigNumInstance<7, MNT6_753_Fr_Params> = BigNumInsta modulus_u60_x4: U60Repr { limbs: [ 0x0e9063de245e8001, 0x09d54522cdd119f5, 0x0810719ac425f0e3, 0x0cce9767254a4638, 0x0da5cb537e38685a, 0x076f218059db80f0, 0x09a15af79db117e7, 0x0e8a0ed8d99d124d, 0x0c97d87307fdb925, 0x08fafed5eb7e8f96, 0x02cdadb7f997505b, 0x0c41110229022eee, 0x01c4c62d92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x45dcc95da918349f4965a2aee8fd75, 0x282372b75580c27c4d1f1f57a96a11, 0xd58326e3c0552419849e3c7171d852, 0xa300e0ede1965cbf72f0aa9bf03479, 0x67fb932cae2aba9a5d17c1ff73538b, 0xa82727c6eaef38056aaa0aaedb0574, 0x0242f916cf + 0x5dcc95da918349f4965a2aee8fd750, 0x82372b75580c27c4d1f1f57a96a114, 0x58326e3c0552419849e3c7171d8522, 0x300e0ede1965cbf72f0aa9bf03479d, 0x7fb932cae2aba9a5d17c1ff73538ba, 0x82727c6eaef38056aaa0aaedb05746, 0x242f916cfa ] }; - diff --git a/src/fields/pallasFq.nr b/src/fields/pallasFq.nr index af652727..99d7cb72 100644 --- a/src/fields/pallasFq.nr +++ b/src/fields/pallasFq.nr @@ -1,5 +1,5 @@ //! Blurb sourced from https://github.com/arkworks-rs -//! This library implements the scalar field of the prime-order curve Pallas, generated by +//! This library implements the base field of the prime-order curve Pallas, generated by //! [Daira Hopwood](https://github.com/zcash/pasta). The main feature of this //! curve is that it forms a cycle with Vesta, i.e. its scalar field and base //! field respectively are the base field and scalar field of Vesta. @@ -17,8 +17,7 @@ use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct Pallas_Fq_Params {} +pub struct Pallas_Fq_Params {} impl RuntimeBigNumParamsTrait<3> for Pallas_Fq_Params { fn modulus_bits() -> u32 { 255 @@ -32,7 +31,7 @@ impl BigNumParamsTrait<3> for Pallas_Fq_Params { 255 } } -global Pallas_Fq_Instance: BigNumInstance<3, Pallas_Fq_Params> = BigNumInstance { +pub global Pallas_Fq_Instance: BigNumInstance<3, Pallas_Fq_Params> = BigNumInstance { modulus: [ 0x4698fc094cf91b992d30ed00000001, 0x22, 0x4000 ], @@ -44,7 +43,6 @@ global Pallas_Fq_Instance: BigNumInstance<3, Pallas_Fq_Params> = BigNumInstance modulus_u60_x4: U60Repr { limbs: [ 0x092d30ed00000001, 0x04698fc094cf91b9, 0x22, 0x00, 0x4000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0xe59c0fdacc1b919b4b3c4bfffffffc, 0xffffffffffffffffffffffffffff76, 0xffff + 0x59c0fdacc1b919b4b3c4bfffffffc4, 0xfffffffffffffffffffffffffff76e, 0x0fffff ] }; - diff --git a/src/fields/pallasFr.nr b/src/fields/pallasFr.nr index e3960d94..36ff966e 100644 --- a/src/fields/pallasFr.nr +++ b/src/fields/pallasFr.nr @@ -1,5 +1,5 @@ //! Blurb sourced from https://github.com/arkworks-rs -//! This library implements the base field of the prime-order curve Pallas, generated by +//! This library implements the scalar field of the prime-order curve Pallas, generated by //! [Daira Hopwood](https://github.com/zcash/pasta). The main feature of this //! curve is that it forms a cycle with Vesta, i.e. its scalar field and base //! field respectively are the base field and scalar field of Vesta. @@ -17,8 +17,7 @@ use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct Pallas_Fr_Params {} +pub struct Pallas_Fr_Params {} impl RuntimeBigNumParamsTrait<3> for Pallas_Fr_Params { fn modulus_bits() -> u32 { 255 @@ -32,7 +31,7 @@ impl BigNumParamsTrait<3> for Pallas_Fr_Params { 255 } } -global Pallas_Fr_Instance: BigNumInstance<3, Pallas_Fr_Params> = BigNumInstance { +pub global Pallas_Fr_Instance: BigNumInstance<3, Pallas_Fr_Params> = BigNumInstance { modulus: [ 0x4698fc0994a8dd8c46eb2100000001, 0x22, 0x4000 ], @@ -44,7 +43,6 @@ global Pallas_Fr_Instance: BigNumInstance<3, Pallas_Fr_Params> = BigNumInstance modulus_u60_x4: U60Repr { limbs: [ 0x0c46eb2100000001, 0x04698fc0994a8dd8, 0x22, 0x00, 0x4000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0xe59c0fd9ad5c89cee4537bfffffffc, 0xffffffffffffffffffffffffffff76, 0xffff + 0x59c0fd9ad5c89cee4537bfffffffc4, 0xfffffffffffffffffffffffffff76e, 0x0fffff ] }; - diff --git a/src/fields/secp256k1Fq.nr b/src/fields/secp256k1Fq.nr index 1f3741e3..dbb3b5e2 100644 --- a/src/fields/secp256k1Fq.nr +++ b/src/fields/secp256k1Fq.nr @@ -1,9 +1,9 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct Secp256k1_Fq_Params {} +pub struct Secp256k1_Fq_Params {} impl RuntimeBigNumParamsTrait<3> for Secp256k1_Fq_Params { fn modulus_bits() -> u32 { 256 @@ -17,7 +17,7 @@ impl BigNumParamsTrait<3> for Secp256k1_Fq_Params { 256 } } -global Secp256k1_Fq_Instance: BigNumInstance<3, Secp256k1_Fq_Params> = BigNumInstance { +pub global Secp256k1_Fq_Instance: BigNumInstance<3, Secp256k1_Fq_Params> = BigNumInstance { modulus: [ 0xfffffffffffffffffffffefffffc2f, 0xffffffffffffffffffffffffffffff, 0xffff ], @@ -29,7 +29,6 @@ global Secp256k1_Fq_Instance: BigNumInstance<3, Secp256k1_Fq_Params> = BigNumIns modulus_u60_x4: U60Repr { limbs: [ 0x0ffffffefffffc2f, 0x0fffffffffffffff, 0x0fffffffffffffff, 0x0fffffffffffffff, 0xffff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x01000003d1, 0x00, 0x010000 + 0x1000003d10, 0x00, 0x100000 ] }; - diff --git a/src/fields/secp256k1Fr.nr b/src/fields/secp256k1Fr.nr index 6d604ead..ae0f4398 100644 --- a/src/fields/secp256k1Fr.nr +++ b/src/fields/secp256k1Fr.nr @@ -1,9 +1,9 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct Secp256k1_Fr_Params {} +pub struct Secp256k1_Fr_Params {} impl RuntimeBigNumParamsTrait<3> for Secp256k1_Fr_Params { fn modulus_bits() -> u32 { 256 @@ -17,7 +17,7 @@ impl BigNumParamsTrait<3> for Secp256k1_Fr_Params { 256 } } -global Secp256k1_Fr_Instance: BigNumInstance<3, Secp256k1_Fr_Params> = BigNumInstance { +pub global Secp256k1_Fr_Instance: BigNumInstance<3, Secp256k1_Fr_Params> = BigNumInstance { modulus: [ 0xaedce6af48a03bbfd25e8cd0364141, 0xfffffffffffffffffffffffffffeba, 0xffff ], @@ -29,7 +29,6 @@ global Secp256k1_Fr_Instance: BigNumInstance<3, Secp256k1_Fr_Params> = BigNumIns modulus_u60_x4: U60Repr { limbs: [ 0x0fd25e8cd0364141, 0x0aedce6af48a03bb, 0x0ffffffffffffeba, 0x0fffffffffffffff, 0xffff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x51231950b75fc4402da1732fc9bec0, 0x0145, 0x010000 + 0x1231950b75fc4402da1732fc9bec09, 0x1455, 0x100000 ] }; - diff --git a/src/fields/secp256r1Fq.nr b/src/fields/secp256r1Fq.nr index 2023f04a..cce7c04b 100644 --- a/src/fields/secp256r1Fq.nr +++ b/src/fields/secp256r1Fq.nr @@ -1,9 +1,9 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct Secp256r1_Fq_Params {} +pub struct Secp256r1_Fq_Params {} impl RuntimeBigNumParamsTrait<3> for Secp256r1_Fq_Params { fn modulus_bits() -> u32 { 256 @@ -17,7 +17,7 @@ impl BigNumParamsTrait<3> for Secp256r1_Fq_Params { 256 } } -global Secp256r1_Fq_Instance: BigNumInstance<3, Secp256r1_Fq_Params> = BigNumInstance { +pub global Secp256r1_Fq_Instance: BigNumInstance<3, Secp256r1_Fq_Params> = BigNumInstance { modulus: [ 0xffffffffffffffffffffffff, 0xffff00000001000000000000000000, 0xffff ], @@ -29,7 +29,6 @@ global Secp256r1_Fq_Instance: BigNumInstance<3, Secp256r1_Fq_Params> = BigNumIns modulus_u60_x4: U60Repr { limbs: [ 0x0fffffffffffffff, 0x0fffffffff, 0x00, 0x0ffff00000001000, 0xffff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0xfffffeffffffff0000000000000003, 0xfffffffffffffffefffffffeff, 0x010000 + 0xffffeffffffff00000000000000030, 0x0fffffffffffffffefffffffefff, 0x100000 ] }; - diff --git a/src/fields/secp256r1Fr.nr b/src/fields/secp256r1Fr.nr index e028721b..892be23e 100644 --- a/src/fields/secp256r1Fr.nr +++ b/src/fields/secp256r1Fr.nr @@ -1,9 +1,9 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct Secp256r1_Fr_Params {} +pub struct Secp256r1_Fr_Params {} impl RuntimeBigNumParamsTrait<3> for Secp256r1_Fr_Params { fn modulus_bits() -> u32 { 256 @@ -17,7 +17,7 @@ impl BigNumParamsTrait<3> for Secp256r1_Fr_Params { 256 } } -global Secp256r1_Fr_Instance: BigNumInstance<3, Secp256r1_Fr_Params> = BigNumInstance { +pub global Secp256r1_Fr_Instance: BigNumInstance<3, Secp256r1_Fr_Params> = BigNumInstance { modulus: [ 0xe6faada7179e84f3b9cac2fc632551, 0xffff00000000ffffffffffffffffbc, 0xffff ], @@ -29,7 +29,6 @@ global Secp256r1_Fr_Instance: BigNumInstance<3, Secp256r1_Fr_Params> = BigNumIns modulus_u60_x4: U60Repr { limbs: [ 0x03b9cac2fc632551, 0x0e6faada7179e84f, 0x0fffffffffffffbc, 0x0ffff00000000fff, 0xffff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0x190552df1a6c21012ffd85eedf9bfe, 0xfffffffffffffffeffffffff43, 0x010000 + 0x90552df1a6c21012ffd85eedf9bfe6, 0x0fffffffffffffffeffffffff431, 0x100000 ] }; - diff --git a/src/fields/secp384r1Fq.nr b/src/fields/secp384r1Fq.nr index ccb39ef5..558c5321 100644 --- a/src/fields/secp384r1Fq.nr +++ b/src/fields/secp384r1Fq.nr @@ -1,9 +1,9 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct Secp384r1_Fq_Params {} +pub struct Secp384r1_Fq_Params {} impl RuntimeBigNumParamsTrait<4> for Secp384r1_Fq_Params { fn modulus_bits() -> u32 { 384 @@ -17,7 +17,7 @@ impl BigNumParamsTrait<4> for Secp384r1_Fq_Params { 384 } } -global Secp384r1_Fq_Instance: BigNumInstance<4, Secp384r1_Fq_Params> = BigNumInstance { +pub global Secp384r1_Fq_Instance: BigNumInstance<4, Secp384r1_Fq_Params> = BigNumInstance { modulus: [ 0xffffff0000000000000000ffffffff, 0xfffffffffffffffffffffffffffeff, 0xffffffffffffffffffffffffffffff, 0xffffff ], @@ -29,7 +29,6 @@ global Secp384r1_Fq_Instance: BigNumInstance<4, Secp384r1_Fq_Params> = BigNumIns modulus_u60_x4: U60Repr { limbs: [ 0xffffffff, 0x0ffffff000000000, 0x0ffffffffffffeff, 0x0fffffffffffffff, 0x0fffffffffffffff, 0x0fffffffffffffff, 0xffffff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0xffffffffffffffff00000001, 0x0100, 0x00, 0x01000000 + 0x0ffffffffffffffff000000010, 0x1000, 0x00, 0x10000000 ] }; - diff --git a/src/fields/secp384r1Fr.nr b/src/fields/secp384r1Fr.nr index 6ed9764d..874ef775 100644 --- a/src/fields/secp384r1Fr.nr +++ b/src/fields/secp384r1Fr.nr @@ -1,9 +1,9 @@ + use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct Secp384r1_Fr_Params {} +pub struct Secp384r1_Fr_Params {} impl RuntimeBigNumParamsTrait<4> for Secp384r1_Fr_Params { fn modulus_bits() -> u32 { 384 @@ -17,7 +17,7 @@ impl BigNumParamsTrait<4> for Secp384r1_Fr_Params { 384 } } -global Secp384r1_Fr_Instance: BigNumInstance<4, Secp384r1_Fr_Params> = BigNumInstance { +pub global Secp384r1_Fr_Instance: BigNumInstance<4, Secp384r1_Fr_Params> = BigNumInstance { modulus: [ 0x1a0db248b0a77aecec196accc52973, 0xffffffffffffc7634d81f4372ddf58, 0xffffffffffffffffffffffffffffff, 0xffffff ], @@ -29,7 +29,6 @@ global Secp384r1_Fr_Instance: BigNumInstance<4, Secp384r1_Fr_Params> = BigNumIns modulus_u60_x4: U60Repr { limbs: [ 0x0cec196accc52973, 0x01a0db248b0a77ae, 0x034d81f4372ddf58, 0x0ffffffffffffc76, 0x0fffffffffffffff, 0x0fffffffffffffff, 0xffffff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0xe5f24db74f58851313e695333ad68d, 0x389cb27e0bc8d220a7, 0x00, 0x01000000 + 0x5f24db74f58851313e695333ad68d0, 0x0389cb27e0bc8d220a7e, 0x00, 0x10000000 ] }; - diff --git a/src/fields/vestaFq.nr b/src/fields/vestaFq.nr index fbbefe93..39abb50d 100644 --- a/src/fields/vestaFq.nr +++ b/src/fields/vestaFq.nr @@ -18,8 +18,7 @@ use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct Vesta_Fq_Params {} +pub struct Vesta_Fq_Params {} impl RuntimeBigNumParamsTrait<3> for Vesta_Fq_Params { fn modulus_bits() -> u32 { 255 @@ -33,7 +32,7 @@ impl BigNumParamsTrait<3> for Vesta_Fq_Params { 255 } } -global Vesta_Fq_Instance: BigNumInstance<3, Vesta_Fq_Params> = BigNumInstance { +pub global Vesta_Fq_Instance: BigNumInstance<3, Vesta_Fq_Params> = BigNumInstance { modulus: [ 0x4698fc0994a8dd8c46eb2100000001, 0x22, 0x4000 ], @@ -45,7 +44,6 @@ global Vesta_Fq_Instance: BigNumInstance<3, Vesta_Fq_Params> = BigNumInstance { modulus_u60_x4: U60Repr { limbs: [ 0x0c46eb2100000001, 0x04698fc0994a8dd8, 0x22, 0x00, 0x4000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0xe59c0fd9ad5c89cee4537bfffffffc, 0xffffffffffffffffffffffffffff76, 0xffff + 0x59c0fd9ad5c89cee4537bfffffffc4, 0xfffffffffffffffffffffffffff76e, 0x0fffff ] }; - diff --git a/src/fields/vestaFr.nr b/src/fields/vestaFr.nr index d5ef72b1..b5fccf58 100644 --- a/src/fields/vestaFr.nr +++ b/src/fields/vestaFr.nr @@ -18,8 +18,7 @@ use crate::BigNumParamsTrait; use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait as RuntimeBigNumParamsTrait; use crate::utils::u60_representation::U60Repr; - -pub struct Vesta_Fr_Params {} +pub struct Vesta_Fr_Params {} impl RuntimeBigNumParamsTrait<3> for Vesta_Fr_Params { fn modulus_bits() -> u32 { 255 @@ -33,7 +32,7 @@ impl BigNumParamsTrait<3> for Vesta_Fr_Params { 255 } } -global Vesta_Fr_Instance: BigNumInstance<3, Vesta_Fr_Params> = BigNumInstance { +pub global Vesta_Fr_Instance: BigNumInstance<3, Vesta_Fr_Params> = BigNumInstance { modulus: [ 0x4698fc094cf91b992d30ed00000001, 0x22, 0x4000 ], @@ -45,7 +44,6 @@ global Vesta_Fr_Instance: BigNumInstance<3, Vesta_Fr_Params> = BigNumInstance { modulus_u60_x4: U60Repr { limbs: [ 0x092d30ed00000001, 0x04698fc094cf91b9, 0x22, 0x00, 0x4000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] }, redc_param: [ - 0xe59c0fdacc1b919b4b3c4bfffffffc, 0xffffffffffffffffffffffffffff76, 0xffff + 0x59c0fdacc1b919b4b3c4bfffffffc4, 0xfffffffffffffffffffffffffff76e, 0x0fffff ] }; - diff --git a/src/lib.nr b/src/lib.nr index de450e49..b2587dea 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -41,19 +41,19 @@ trait BigNumTrait where BigNumTrait: std::ops::Add + std::ops::Sub + std::ops::M fn modulus() -> Self; fn modulus_bits(self) -> u32; fn num_limbs(self) -> u32; - fn __derive_from_seed(seed: [u8; SeedBytes]) -> Self; - fn __pow(self, exponent: Self) -> Self; - fn __neg(self) -> Self; - fn __add(self, other: Self) -> Self; - fn __sub(self, other: Self) -> Self; - fn __mul(self, other: Self) -> Self; - fn __div(self, other: Self) -> Self; - fn __udiv_mod(self, divisor: Self) -> (Self, Self); - fn __invmod(self) -> Self; - fn __batch_invert(to_invert: [Self; M]) -> [Self; M]; - fn __is_zero(self) -> bool { RuntimeBigNumTrait::__is_zero(self) } - fn __eq(self, other: Self) -> bool { RuntimeBigNumTrait::__eq(self, other) } - fn __compute_quadratic_expression(lhs: [[Self; LHS_N]; NUM_PRODUCTS], lhs_flags: [[bool; LHS_N]; NUM_PRODUCTS], rhs: [[Self; RHS_N]; NUM_PRODUCTS], rhs_flags: [[bool; RHS_N]; NUM_PRODUCTS], add: [Self; ADD_N], add_flags: [bool; ADD_N]) -> (Self, Self); + unconstrained fn __derive_from_seed(seed: [u8; SeedBytes]) -> Self; + unconstrained fn __pow(self, exponent: Self) -> Self; + unconstrained fn __neg(self) -> Self; + unconstrained fn __add(self, other: Self) -> Self; + unconstrained fn __sub(self, other: Self) -> Self; + unconstrained fn __mul(self, other: Self) -> Self; + unconstrained fn __div(self, other: Self) -> Self; + unconstrained fn __udiv_mod(self, divisor: Self) -> (Self, Self); + unconstrained fn __invmod(self) -> Self; + unconstrained fn __batch_invert(to_invert: [Self; M]) -> [Self; M]; + unconstrained fn __is_zero(self) -> bool { RuntimeBigNumTrait::__is_zero(self) } + unconstrained fn __eq(self, other: Self) -> bool { RuntimeBigNumTrait::__eq(self, other) } + unconstrained fn __compute_quadratic_expression(lhs: [[Self; LHS_N]; NUM_PRODUCTS], lhs_flags: [[bool; LHS_N]; NUM_PRODUCTS], rhs: [[Self; RHS_N]; NUM_PRODUCTS], rhs_flags: [[bool; RHS_N]; NUM_PRODUCTS], add: [Self; ADD_N], add_flags: [bool; ADD_N]) -> (Self, Self); fn evaluate_quadratic_expression(lhs: [[Self; LHS_N]; NUM_PRODUCTS], lhs_flags: [[bool; LHS_N]; NUM_PRODUCTS], rhs: [[Self; RHS_N]; NUM_PRODUCTS], rhs_flags: [[bool; RHS_N]; NUM_PRODUCTS], add: [Self; ADD_N], add_flags: [bool; ADD_N]); fn validate_in_range(self){ RuntimeBigNumTrait::validate_in_range(self) } fn validate_in_field(self); @@ -113,45 +113,45 @@ impl BigNumTrait for BigNum where Params: BigNumP // ### NOTE: these functions call unconstrained internal implementations because trait impl modifiers are not supported // #################################################################################################################### // #################################################################################################################### - fn __derive_from_seed(seed: [u8; SeedBytes]) -> Self { + unconstrained fn __derive_from_seed(seed: [u8; SeedBytes]) -> Self { Params::get_instance().__derive_from_seed(seed) } - fn __neg(self) -> Self { + unconstrained fn __neg(self) -> Self { Params::get_instance().__neg(self) } - fn __add(self, rhs: Self) -> Self { + unconstrained fn __add(self, rhs: Self) -> Self { Params::get_instance().__add(self, rhs) } - fn __sub(self, rhs: Self) -> Self { + unconstrained fn __sub(self, rhs: Self) -> Self { Params::get_instance().__sub(self, rhs) } - fn __mul(self, rhs: Self) -> Self { + unconstrained fn __mul(self, rhs: Self) -> Self { Params::get_instance().__mul(self, rhs) } - fn __div(self, rhs: Self) -> Self { + unconstrained fn __div(self, rhs: Self) -> Self { Params::get_instance().__div(self, rhs) } - fn __batch_invert(x: [Self; M]) -> [Self; M] { + unconstrained fn __batch_invert(x: [Self; M]) -> [Self; M] { assert(Params::has_multiplicative_inverse()); Params::get_instance().__batch_invert(x) } - fn __invmod(self) -> Self { + unconstrained fn __invmod(self) -> Self { assert(Params::has_multiplicative_inverse()); Params::get_instance().__invmod(self) } - fn __pow(self, exponent: Self) -> Self { + unconstrained fn __pow(self, exponent: Self) -> Self { Params::get_instance().__pow(self, exponent) } - fn __compute_quadratic_expression( + unconstrained fn __compute_quadratic_expression( lhs_terms: [[Self; LHS_N]; NUM_PRODUCTS], lhs_flags: [[bool; LHS_N]; NUM_PRODUCTS], rhs_terms: [[Self; RHS_N]; NUM_PRODUCTS], @@ -250,7 +250,7 @@ impl BigNumTrait for BigNum where Params: BigNumP Params::get_instance().neg(self) } - fn __udiv_mod(self, divisor: Self) -> (Self, Self) { + unconstrained fn __udiv_mod(self, divisor: Self) -> (Self, Self) { Params::get_instance().__udiv_mod(self, divisor) } diff --git a/src/runtime_bignum.nr b/src/runtime_bignum.nr index cd7e45e9..27e3fa80 100644 --- a/src/runtime_bignum.nr +++ b/src/runtime_bignum.nr @@ -18,10 +18,16 @@ pub trait BigNumTrait { fn conditional_select(lhs: Self, rhs: Self, predicate: bool) -> Self; fn validate_in_range(self); fn validate_quotient_in_range(self); - fn __is_zero(self) -> bool; - fn __eq(self, rhs: Self) -> bool; + unconstrained fn __is_zero(self) -> bool; + unconstrained fn __eq(self, rhs: Self) -> bool; } +/** + * @brief BARRETT_REDUCTION_OVERFLOW_BITS defines how large an input to barrett reduction can be + * @details maximum value = modulus^2 < where BN: BigNumTrait { fn modulus(self) -> BN; - fn __derive_from_seed(self, seed: [u8; SeedBytes]) -> BN; fn eq(self, lhs: BN, rhs: BN) -> bool; - fn __neg(self, val: BN) -> BN; - fn __add(self, lhs: BN, rhs: BN) -> BN; - fn __sub(self, lhs: BN, rhs: BN) -> BN; - fn __mul(self, lhs: BN, rhs: BN) -> BN; - fn __div(self, lhs: BN, rhs: BN) -> BN; - fn __batch_invert(self, x: [BN; M]) -> [BN; M]; - fn __udiv_mod(self, numerator: BN, divisor: BN) -> (BN, BN); - fn __invmod(self, val: BN) -> BN; - fn __pow(self, val: BN, exponent: BN) -> BN; - fn __compute_quadratic_expression( + unconstrained fn __derive_from_seed(self, seed: [u8; SeedBytes]) -> BN; + unconstrained fn __neg(self, val: BN) -> BN; + unconstrained fn __add(self, lhs: BN, rhs: BN) -> BN; + unconstrained fn __sub(self, lhs: BN, rhs: BN) -> BN; + unconstrained fn __mul(self, lhs: BN, rhs: BN) -> BN; + unconstrained fn __div(self, lhs: BN, rhs: BN) -> BN; + unconstrained fn __batch_invert(self, x: [BN; M]) -> [BN; M]; + unconstrained fn __udiv_mod(self, numerator: BN, divisor: BN) -> (BN, BN); + unconstrained fn __invmod(self, val: BN) -> BN; + unconstrained fn __pow(self, val: BN, exponent: BN) -> BN; + unconstrained fn __compute_quadratic_expression( self, lhs_terms: [[BN; LHS_N]; NUM_PRODUCTS], lhs_flags: [[bool; LHS_N]; NUM_PRODUCTS], @@ -253,11 +259,11 @@ impl BigNumTrait for BigNum where Params: BigNumP self.limbs[N - 1].assert_max_bit_size(final_limb_bits as u32); } - fn __is_zero(self) -> bool { + unconstrained fn __is_zero(self) -> bool { self.__is_zero_impl() } - fn __eq(self, rhs: Self) -> bool { + unconstrained fn __eq(self, rhs: Self) -> bool { self.__eq_impl(rhs) } } @@ -286,7 +292,7 @@ impl BigNumInstanceTrait> for BigNumInstan fn modulus(self) -> BigNum { BigNum { limbs: self.modulus } } - fn __derive_from_seed(self, seed: [u8; SeedBytes]) -> BigNum { + unconstrained fn __derive_from_seed(self, seed: [u8; SeedBytes]) -> BigNum { self.__derive_from_seed_impl(seed) } // #################################################################################################################### @@ -296,39 +302,46 @@ impl BigNumInstanceTrait> for BigNumInstan // #################################################################################################################### // #################################################################################################################### - fn __neg(self, val: BigNum) -> BigNum { + unconstrained fn __neg(self, val: BigNum) -> BigNum { self.__neg_impl(val) } - fn __add(self, lhs: BigNum, rhs: BigNum) -> BigNum { + unconstrained fn __add(self, lhs: BigNum, rhs: BigNum) -> BigNum { self.__add_impl(lhs, rhs) } - fn __sub(self, lhs: BigNum, rhs: BigNum) -> BigNum { + unconstrained fn __sub(self, lhs: BigNum, rhs: BigNum) -> BigNum { self.__sub_impl(lhs, rhs) } - fn __mul(self, lhs: BigNum, rhs: BigNum) -> BigNum { + unconstrained fn __mul(self, lhs: BigNum, rhs: BigNum) -> BigNum { self.__mul_impl(lhs, rhs) } - fn __div(self, lhs: BigNum, rhs: BigNum) -> BigNum { + unconstrained fn __div(self, lhs: BigNum, rhs: BigNum) -> BigNum { self.__div_impl(lhs, rhs) } - fn __batch_invert(self, x: [BigNum; M]) -> [BigNum; M] { + unconstrained fn __batch_invert( + self, + x: [BigNum; M] + ) -> [BigNum; M] { self.batch_invert_impl(x) } - fn __invmod(self, val: BigNum) -> BigNum { + unconstrained fn __invmod(self, val: BigNum) -> BigNum { self.__invmod_impl(val) } - fn __pow(self, val: BigNum, exponent: BigNum) -> BigNum { + unconstrained fn __pow( + self, + val: BigNum, + exponent: BigNum + ) -> BigNum { self.__pow_impl(val, exponent) } - fn __compute_quadratic_expression( + unconstrained fn __compute_quadratic_expression( self, lhs_terms: [[BigNum; LHS_N]; NUM_PRODUCTS], lhs_flags: [[bool; LHS_N]; NUM_PRODUCTS], @@ -720,7 +733,9 @@ impl BigNumInstanceTrait> for BigNumInstan // e.g. performing a sum of multiple multiplications and additions via `evaluate_quadratic_expression` // will create much fewer constraints than calling `mul` and `add` directly fn mul(self, lhs: BigNum, rhs: BigNum) -> BigNum { - let result = self.__mul(lhs, rhs); + let result = unsafe { + self.__mul(lhs, rhs) + }; self.evaluate_quadratic_expression([[lhs]], [[false]], [[rhs]], [[false]], [result], [true]); result } @@ -729,7 +744,9 @@ impl BigNumInstanceTrait> for BigNumInstan assert( Params::has_multiplicative_inverse(), "BigNum has no multiplicative inverse. Use udiv for unsigned integer division" ); - let result = self.__div(lhs, rhs); + let result = unsafe { + self.__div(lhs, rhs) + }; self.evaluate_quadratic_expression([[result]], [[false]], [[rhs]], [[false]], [lhs], [true]); result } @@ -741,7 +758,7 @@ impl BigNumInstanceTrait> for BigNumInstan * 2. numerator % divisor = remainder * 3. divisor * quotient + remainder = numerator **/ - fn __udiv_mod( + unconstrained fn __udiv_mod( self, numerator: BigNum, divisor: BigNum @@ -760,7 +777,9 @@ impl BigNumInstanceTrait> for BigNumInstan numerator: BigNum, divisor: BigNum ) -> (BigNum, BigNum) { - let (quotient, remainder) = BigNumInstance::__udiv_mod_impl(self, numerator, divisor); + let (quotient, remainder) = unsafe { + BigNumInstance::__udiv_mod_impl(self, numerator, divisor) + }; // self / divisor = quotient rounded // quotient * divisor + remainder - self = 0 @@ -1507,7 +1526,21 @@ unconstrained fn __barrett_reduction( } mulout = split_bits::__normalize_limbs(mulout, 3 * N - 1); let mulout_u60: U60Repr = U60Repr::new(mulout); - let mut quotient_u60 = mulout_u60.shr((k + k)); + + // When we apply the barrett reduction, the maximum value of the output will be + // <= p * (1 + x/2^{2k}) + // where p = modulus, + // x = reduction input + // if x > p * p, we need k to be larger than modulus_bits() + // we hardcode k = 4, which means that the maximum value of x is approx. 16 * p * p + // this should be larger than most values put into `evaluate_quadratic_expression` + // TODO: try and detect cases where x might be too large at comptime + // N.B. BARRETT_REDUCTION_OVERFLOW_BITS affects how `redc_param` is generated. + // `redc_param` = 2^{modulus_bits() * 2 + BARRETT_REDUCTION_OVERFLOW_BITS} / modulus + // NOTE: very niche edge case error that we need to be aware of: + // N must be large enough to cover the modulus *plus* BARRETT_REDUCTION_OVERFLOW_BITS + // i.e. a 359-bit prime needs (I think) 4 limbs to represent or we may overflow when calling __barrett_reduction + let mut quotient_u60 = mulout_u60.shr((k + k + BARRETT_REDUCTION_OVERFLOW_BITS)); // N.B. we assume that the shifted quotient cannot exceed 2 times original bit size. // (partial_quotient should be just slightly larger than the modulus, we could probably represent with a size N+1 array) diff --git a/src/runtime_bignum_test.nr b/src/runtime_bignum_test.nr index d60eacb5..2520a8ee 100644 --- a/src/runtime_bignum_test.nr +++ b/src/runtime_bignum_test.nr @@ -3,6 +3,194 @@ use crate::runtime_bignum::BigNumInstance; use crate::runtime_bignum::BigNumParamsTrait; use crate::utils::u60_representation::U60Repr; use crate::fields::bn254Fq::BNParams as BNParams; +use crate::fields::secp256k1Fq::Secp256k1_Fq_Params; +use crate::fields::bls12_381Fq::BLS12_381_Fq_Params; + +// + +#[test] +fn test_bls() { + let X1: BigNum<4, BLS12_381_Fq_Params> = BigNum { + limbs: [ + 0x55e83ff97a1aeffb3af00adb22c6bb, 0x8c4f9774b905a14e3a3f171bac586c, 0xa73197d7942695638c4fa9ac0fc368, 0x17f1d3 + ] + }; + X1.validate_in_field(); + let mut (_, XX_mul_3): (BigNum<4, BLS12_381_Fq_Params>, BigNum<4, BLS12_381_Fq_Params> ) = unsafe { + BigNum::__compute_quadratic_expression( + [[X1, X1, X1]], + [[false, false, false]], + [[X1]], + [[false]], + [], + [] + ) + }; + XX_mul_3.validate_in_field(); +} +comptime fn make_test(f: StructDefinition, N: Quoted, typ: Quoted) -> Quoted { + let k = f.name(); + quote{ +impl $k { +#[test] +fn test_add() { + let bn = $typ ::get_instance(); + let a: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([1, 2, 3, 4]) }; + let b: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([4, 5, 6, 7]) }; + let one: BigNum<$N, $typ> = BigNum::one(); + a.validate_in_range(); + bn.validate_in_field(a); + b.validate_in_range(); + bn.validate_in_field(b); + + let mut c = bn.add(a, b); + c = bn.add(c, c); + let d = bn.mul(bn.add(a, b), bn.add(one, one)); + assert(bn.eq(c, d)); + + let e = bn.add(one, one); + for i in 1..$N { + assert(e.limbs[i] == 0); + } + assert(e.limbs[0] == 2); +} + + +#[test] +fn test_mul() { + let bn = $typ ::get_instance(); + let a: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([1, 2, 3, 4]) }; + let b: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([4, 5, 6, 7]) }; + + let c = bn.mul(bn.add(a, b), bn.add(a, b)); + let d = bn.add( + bn.add( + bn.add(bn.mul(a, a), bn.mul(b, b)), + bn.mul(a, b) + ), + bn.mul(a, b) + ); + assert(bn.eq(c, d)); +} + +#[test] +fn test_quadratic_expression() { + let bn = $typ ::get_instance(); + for i in 0..32 { + let X1: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([i as u8,2,3,4]) }; + let Y1: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([i as u8,i as u8,6,7]) }; + let Z1: BigNum<$N, $typ> = BigNum::one(); + + let (_, YY_mul_2): (BigNum<$N, $typ>, BigNum<$N, $typ> ) = unsafe{BigNum::__compute_quadratic_expression([[Y1]], [[false]], [[Y1, Y1]], [[false, false]], [], [])}; + let mut (_, XX_mul_3): (BigNum<$N, $typ>, BigNum<$N, $typ> ) = unsafe{BigNum::__compute_quadratic_expression( + [[X1]], + [[false]], + [[X1, X1, X1]], + [[false, false, false]], + [], + [] + )}; + // if (CurveParams::a().get_limb(0) != 0) { + // let ZZ = Z1.mul(Z1); + // let AZZZZ = ZZ.mul(ZZ).mul(CurveParams::a()); + // XX_mul_3 = XX_mul_3.add(AZZZZ); + // } + let (_, D): (BigNum<$N, $typ>, BigNum<$N, $typ> ) = unsafe{BigNum::__compute_quadratic_expression([[X1, X1]], [[false, false]], [[YY_mul_2]], [[false]], [], [])}; + let mut (_, X3): (BigNum<$N, $typ>, BigNum<$N, $typ> ) = unsafe{BigNum::__compute_quadratic_expression( + [[XX_mul_3]], + [[false]], + [[XX_mul_3]], + [[false]], + [D, D], + [true, true] + )}; + let (_, Y3): (BigNum<$N, $typ>, BigNum<$N, $typ> ) = unsafe{BigNum::__compute_quadratic_expression( + [[XX_mul_3], [YY_mul_2]], + [[false], [true]], + [[D, X3], [YY_mul_2, YY_mul_2]], + [[false, true], [false, false]], + [], + [] + )}; + // 3XX * (D - X3) - 8YYYY + + let (_, Z3): (BigNum<$N, $typ>, BigNum<$N, $typ> ) = unsafe{ BigNum::__compute_quadratic_expression([[Y1]], [[false]], [[Z1, Z1]], [[false, false]], [], []) }; + + bn.validate_in_field(X3); + bn.validate_in_field(Y3); + bn.validate_in_field(Z3); + } +} + +#[test] +fn assert_is_not_equa() { + let bn = $typ ::get_instance(); + let a: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([1, 2, 3, 4]) }; + let b: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([4, 5, 6, 7]) }; + + bn.assert_is_not_equal(a, b); +} + +#[test(should_fail_with = "asssert_is_not_equal fail")] +fn assert_is_not_equal_fail() { + let bn = $typ ::get_instance(); + let a: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([1, 2, 3, 4]) }; + let b: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([1, 2, 3, 4]) }; + + bn.assert_is_not_equal(a, b); +} + +#[test(should_fail_with = "asssert_is_not_equal fail")] +fn assert_is_not_equal_overloaded_lhs_fail() { + let bn = $typ ::get_instance(); + let a: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([1, 2, 3, 4]) }; + let b: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([1, 2, 3, 4]) }; + + let modulus = bn.modulus(); + + let t0: U60Repr<$N, 2> = U60Repr::from(a.limbs); + let t1: U60Repr<$N, 2> = U60Repr::from(modulus.limbs); + let a_plus_modulus: BigNum<$N, $typ> = BigNum { limbs: U60Repr::into(t0 + t1) }; + bn.assert_is_not_equal(a_plus_modulus, b); +} + +#[test(should_fail_with = "asssert_is_not_equal fail")] +fn assert_is_not_equal_overloaded_rhs_fail() { + let bn = $typ ::get_instance(); + let a: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([1, 2, 3, 4]) }; + let b: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([1, 2, 3, 4]) }; + + let modulus = bn.modulus(); + + let t0: U60Repr<$N, 2> = U60Repr::from(b.limbs); + let t1: U60Repr<$N, 2> = U60Repr::from(modulus.limbs); + let b_plus_modulus: BigNum<$N, $typ> = BigNum { limbs: U60Repr::into(t0 + t1) }; + bn.assert_is_not_equal(a, b_plus_modulus); +} + +#[test(should_fail_with = "asssert_is_not_equal fail")] +fn assert_is_not_equal_overloaded_fail() { + let bn = $typ ::get_instance(); + let a: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([1, 2, 3, 4]) }; + let b: BigNum<$N, $typ> = unsafe{ bn.__derive_from_seed([1, 2, 3, 4]) }; + let modulus = bn.modulus(); + let t0: U60Repr<$N, 2> = U60Repr::from(a.limbs); + let t1: U60Repr<$N, 2> = U60Repr::from(b.limbs); + let t2: U60Repr<$N, 2> = U60Repr::from(modulus.limbs); + let a_plus_modulus: BigNum<$N, $typ> = BigNum { limbs: U60Repr::into(t0 + t2) }; + let b_plus_modulus: BigNum<$N, $typ> = BigNum { limbs: U60Repr::into(t1 + t2) }; + bn.assert_is_not_equal(a_plus_modulus, b_plus_modulus); +} +} +} +} + +#[make_test(quote{3},quote{BNParams})] +pub struct BNTests{} +#[make_test(quote{3},quote{Secp256k1_Fq_Params})] +pub struct Secp256K1FqTests{} +#[make_test(quote{4},quote{BLS12_381_Fq_Params})] +pub struct BLS12_381FqTests{} struct Test2048Params {} @@ -40,24 +228,7 @@ fn get_2048_BN_instance() -> BigNumInstance<18, Test2048Params> { 0x00000000000000000000000000000000000000000000000000000000000000b8 ]; let redc_param: [Field; 18] = [ - 0x000000000000000000000000000000000091697def7100cd5cf8d890b4ef2ec3, - 0x00000000000000000000000000000000006765ba8304214dac764d3f4adc3185, - 0x000000000000000000000000000000000048404bd14d927ea230e60d4bebf940, - 0x00000000000000000000000000000000007c4d53a23bacc251ecbfc4b7ba5a0b, - 0x000000000000000000000000000000000093eaf3499474a6f5b2fff83f1259c8, - 0x00000000000000000000000000000000005bff4c737b97281f1a5f2384a8c16d, - 0x000000000000000000000000000000000061b4cf2f55358476b5323782999055, - 0x00000000000000000000000000000000001e7a804e8eacfe3a2a5673bc3885b8, - 0x0000000000000000000000000000000000eabadeae4282906c817adf70eab4ae, - 0x0000000000000000000000000000000000166f7df257fe2bf27f0809aceed9b0, - 0x00000000000000000000000000000000007d90fb7428901b8bed11f6b81e36bf, - 0x0000000000000000000000000000000000f36e6ba885c60b7024c563605df7e0, - 0x000000000000000000000000000000000052b7c58d2fb5d2c8478963ae6d4a44, - 0x000000000000000000000000000000000036ee761de26635f114ccc3f7d74f85, - 0x0000000000000000000000000000000000e3fb726a10cf2220897513f05243de, - 0x0000000000000000000000000000000000f43a26bbd732496eb4d828591b8056, - 0x0000000000000000000000000000000000ff4e42304e60fb3a54fca735499f2c, - 0x0000000000000000000000000000000000000000000000000000000000000162 + 0x1697def7100cd5cf8d890b4ef2ec3f, 0x765ba8304214dac764d3f4adc31859, 0x8404bd14d927ea230e60d4bebf9406, 0xc4d53a23bacc251ecbfc4b7ba5a0b4, 0x3eaf3499474a6f5b2fff83f1259c87, 0xbff4c737b97281f1a5f2384a8c16d9, 0x1b4cf2f55358476b53237829990555, 0xe7a804e8eacfe3a2a5673bc3885b86, 0xabadeae4282906c817adf70eab4ae1, 0x66f7df257fe2bf27f0809aceed9b0e, 0xd90fb7428901b8bed11f6b81e36bf1, 0x36e6ba885c60b7024c563605df7e07, 0x2b7c58d2fb5d2c8478963ae6d4a44f, 0x6ee761de26635f114ccc3f7d74f855, 0x3fb726a10cf2220897513f05243de3, 0x43a26bbd732496eb4d828591b8056e, 0xf4e42304e60fb3a54fca735499f2cf, 0x162f ]; BigNumInstance::new(modulus, redc_param) } @@ -67,10 +238,27 @@ type Fq = BigNum<3, BNParams>; // type Fqq = BigNum<18, Test2048Params>; // type FqqInstance = BigNumInstance<18, Test2048Params>; +fn test_derive(BNInstance: BigNumInstance) where Params: BigNumParamsTrait { + let a: BigNum = unsafe { + BNInstance.__derive_from_seed("hello".as_bytes()) + }; + let b: BigNum = unsafe { + BNInstance.__derive_from_seed("hello".as_bytes()) + }; + assert(BNInstance.eq(a, b)); + // println(f"derived value = {a}"); +} + fn test_eq(BNInstance: BigNumInstance) where Params: BigNumParamsTrait { - let a: BigNum = BNInstance.__derive_from_seed([1, 2, 3, 4]); - let b: BigNum = BNInstance.__derive_from_seed([1, 2, 3, 4]); - let c: BigNum = BNInstance.__derive_from_seed([2, 2, 3, 4]); + let a: BigNum = unsafe { + BNInstance.__derive_from_seed([1, 2, 3, 4]) + }; + let b: BigNum = unsafe { + BNInstance.__derive_from_seed([1, 2, 3, 4]) + }; + let c: BigNum = unsafe { + BNInstance.__derive_from_seed([2, 2, 3, 4]) + }; let modulus: BigNum = BNInstance.modulus(); let t0: U60Repr = (U60Repr::from(modulus.limbs)); @@ -87,8 +275,12 @@ fn test_eq(BNInstance: BigNumInstance) where Para // 99689 // 929 gates for a 2048 bit mul fn test_mul(BNInstance: BigNumInstance) where Params: BigNumParamsTrait { - let a: BigNum = BNInstance.__derive_from_seed([1, 2, 3, 4]); - let b: BigNum = BNInstance.__derive_from_seed([4, 5, 6, 7]); + let a: BigNum = unsafe { + BNInstance.__derive_from_seed([1, 2, 3, 4]) + }; + let b: BigNum = unsafe { + BNInstance.__derive_from_seed([4, 5, 6, 7]) + }; let c = BNInstance.mul(BNInstance.add(a, b), BNInstance.add(a, b)); let d = BNInstance.add( @@ -102,8 +294,12 @@ fn test_mul(BNInstance: BigNumInstance) where Par } fn test_add(bn: BigNumInstance) where Params: BigNumParamsTrait { - let a: BigNum = bn.__derive_from_seed([1, 2, 3, 4]); - let b: BigNum = bn.__derive_from_seed([4, 5, 6, 7]); + let a: BigNum = unsafe { + bn.__derive_from_seed([1, 2, 3, 4]) + }; + let b: BigNum = unsafe { + bn.__derive_from_seed([4, 5, 6, 7]) + }; let one: BigNum = BigNum::one(); a.validate_in_range(); bn.validate_in_field(a); @@ -123,40 +319,62 @@ fn test_add(bn: BigNumInstance) where Params: Big } fn test_div(bn: BigNumInstance) where Params: BigNumParamsTrait { - let a: BigNum = bn.__derive_from_seed([1, 2, 3, 4]); - let b: BigNum = bn.__derive_from_seed([4, 5, 6, 7]); + let a: BigNum = unsafe { + bn.__derive_from_seed([1, 2, 3, 4]) + }; + let b: BigNum = unsafe { + bn.__derive_from_seed([4, 5, 6, 7]) + }; let c = bn.div(a, b); assert(bn.eq(bn.mul(b, c), a)); } fn test_invmod(bn: BigNumInstance) where Params: BigNumParamsTrait { - let u: BigNum = bn.__derive_from_seed([1, 2, 3, 4]); + let u: BigNum = unsafe { + bn.__derive_from_seed([1, 2, 3, 4]) + }; for _ in 0..1 { - let v = bn.__invmod(u); - let result = bn.__mul(u, v); + let v = unsafe { + bn.__invmod(u) + }; + let result = unsafe { + bn.__mul(u, v) + }; let expected: BigNum = BigNum::one(); assert(result.limbs == expected.limbs); } } fn assert_is_not_equal(bn: BigNumInstance) where Params: BigNumParamsTrait { - let a: BigNum = bn.__derive_from_seed([1, 2, 3, 4]); - let b: BigNum = bn.__derive_from_seed([4, 5, 6, 7]); + let a: BigNum = unsafe { + bn.__derive_from_seed([1, 2, 3, 4]) + }; + let b: BigNum = unsafe { + bn.__derive_from_seed([4, 5, 6, 7]) + }; bn.assert_is_not_equal(a, b); } fn assert_is_not_equal_fail(bn: BigNumInstance) where Params: BigNumParamsTrait { - let a: BigNum = bn.__derive_from_seed([1, 2, 3, 4]); - let b: BigNum = bn.__derive_from_seed([1, 2, 3, 4]); + let a: BigNum = unsafe { + bn.__derive_from_seed([1, 2, 3, 4]) + }; + let b: BigNum = unsafe { + bn.__derive_from_seed([1, 2, 3, 4]) + }; bn.assert_is_not_equal(a, b); } fn assert_is_not_equal_overloaded_lhs_fail(bn: BigNumInstance) where Params: BigNumParamsTrait { - let a: BigNum = bn.__derive_from_seed([1, 2, 3, 4]); - let b: BigNum = bn.__derive_from_seed([1, 2, 3, 4]); + let a: BigNum = unsafe { + bn.__derive_from_seed([1, 2, 3, 4]) + }; + let b: BigNum = unsafe { + bn.__derive_from_seed([1, 2, 3, 4]) + }; let modulus = bn.modulus(); @@ -167,8 +385,12 @@ fn assert_is_not_equal_overloaded_lhs_fail(bn: BigNumInstanc } fn assert_is_not_equal_overloaded_rhs_fail(bn: BigNumInstance) where Params: BigNumParamsTrait { - let a: BigNum = bn.__derive_from_seed([1, 2, 3, 4]); - let b: BigNum = bn.__derive_from_seed([1, 2, 3, 4]); + let a: BigNum = unsafe { + bn.__derive_from_seed([1, 2, 3, 4]) + }; + let b: BigNum = unsafe { + bn.__derive_from_seed([1, 2, 3, 4]) + }; let modulus = bn.modulus(); @@ -179,8 +401,12 @@ fn assert_is_not_equal_overloaded_rhs_fail(bn: BigNumInstanc } fn assert_is_not_equal_overloaded_fail(bn: BigNumInstance) where Params: BigNumParamsTrait { - let a: BigNum = bn.__derive_from_seed([1, 2, 3, 4]); - let b: BigNum = bn.__derive_from_seed([1, 2, 3, 4]); + let a: BigNum = unsafe { + bn.__derive_from_seed([1, 2, 3, 4]) + }; + let b: BigNum = unsafe { + bn.__derive_from_seed([1, 2, 3, 4]) + }; let modulus = bn.modulus(); @@ -192,6 +418,12 @@ fn assert_is_not_equal_overloaded_fail(bn: BigNumInstance = BigNum { limbs: a }; let b_bn: BigNum<18, Test2048Params> = BigNum { limbs: b }; - let c_bn = instance.__mul(a_bn, b_bn); + let c_bn = unsafe { + instance.__mul(a_bn, b_bn) + }; assert(c_bn.limbs == c_expected); a_bn.validate_in_range(); @@ -495,7 +729,9 @@ fn test_expressions() { 0x0 ] }; - let yy = instance.__add(y, y); + let yy = unsafe { + instance.__add(y, y) + }; assert(yy.limbs == z.limbs); @@ -527,10 +763,18 @@ fn test_expressions() { 0x0000000000000000000000000000000000000000000000000000000000000f93 ] }; - let wx = instance.__mul(w, x); - let uv = instance.__mul(uu, vv); - let y = instance.__neg(instance.__add(uv, wx)); - let z = instance.__add(uv, wx); + let wx = unsafe { + instance.__mul(w, x) + }; + let uv = unsafe { + instance.__mul(uu, vv) + }; + let y = unsafe { + instance.__neg(instance.__add(uv, wx)) + }; + let z = unsafe { + instance.__add(uv, wx) + }; instance.evaluate_quadratic_expression( [[uu], [w]], diff --git a/src/utils/u60_representation_test.nr b/src/utils/u60_representation_test.nr index f32c1133..3ac4df07 100644 --- a/src/utils/u60_representation_test.nr +++ b/src/utils/u60_representation_test.nr @@ -4,8 +4,12 @@ use crate::utils::u60_representation::U60Repr; fn test_conversion() { let p = 0xffffffffffffffffffffffffffffff; // 2^120 - 1 let expected: [Field; 3 * 2] = [p, p - 1, p - 2, p - 3, p - 4, p - 5]; - let u60repr: U60Repr<3, 4> = U60Repr::new(expected); - let result = u60repr.into_field_array(); + let u60repr: U60Repr<3, 4> = unsafe { + U60Repr::new(expected) + }; + let result = unsafe { + u60repr.into_field_array() + }; assert(result == expected); } @@ -19,10 +23,14 @@ fn test_shr() { // 120 bits of 01010101 (repeating) let Y = 0x555555555555555555555555555555; let input: [Field; 6] = [X, X, X, X, X, X]; - let u60repr: U60Repr<3, 6> = U60Repr::new(input); + let u60repr: U60Repr<3, 6> = unsafe { + U60Repr::new(input) + }; let result = u60repr.shr(121); - let expected: U60Repr<3, 6> = U60Repr::new([Y, Y, Y, Y, Y, 0]); + let expected: U60Repr<3, 6> = unsafe { + U60Repr::new([Y, Y, Y, Y, Y, 0]) + }; assert(result == expected); let noshift = u60repr.shr(0); @@ -36,10 +44,14 @@ fn test_shl() { // 120 bits of 01010101 (repeating) let Y = 0x555555555555555555555555555555; let input: [Field; 6] = [Y, Y, Y, Y, Y, Y]; - let u60repr: U60Repr<3, 6> = U60Repr::new(input); + let u60repr: U60Repr<3, 6> = unsafe { + U60Repr::new(input) + }; let result = u60repr.shl(121); - let expected: U60Repr<3, 6> = U60Repr::new([0, X, X, X, X, X, X, 0, 0]); + let expected: U60Repr<3, 6> = unsafe { + U60Repr::new([0, X, X, X, X, X, X, 0, 0]) + }; assert(result == expected); let noshift = u60repr.shr(0); @@ -51,7 +63,9 @@ fn test_get_bit() { // 0x8000 = b1000000000000000 (16th bit is high) // 256th bit of input should be high let input: [Field; 6] = [0, 0, 0x8000, 0, 0, 0]; - let u60repr: U60Repr<3, 6> = U60Repr::new(input); + let u60repr: U60Repr<3, 6> = unsafe { + U60Repr::new(input) + }; for i in 0..720 { if i == 255 { assert(u60repr.get_bit(i) == true); @@ -65,22 +79,36 @@ fn test_get_bit() { fn test_gte() { let p = 0xffffffffffffffffffffffffffffff; // 2^120 - 1 let input: [Field; 6] = [p, p - 1, p - 2, p - 3, p - 4, p - 5]; - let lhs: U60Repr<3, 6> = U60Repr::new(input); + let lhs: U60Repr<3, 6> = unsafe { + U60Repr::new(input) + }; { let rhs = lhs; - assert(lhs.gte(rhs)); + assert( + unsafe { + lhs.gte(rhs) + } + ); } { // make rhs smaller by 1 let mut rhs: U60Repr<3, 6> = lhs; rhs.limbs[0] -= 1; - assert(lhs.gte(rhs)); + assert( + unsafe { + lhs.gte(rhs) + } + ); } { // make rhs greater by 1 let mut rhs = lhs; rhs.limbs[0] += 1; - assert(!lhs.gte(rhs)); + assert( + !unsafe { + lhs.gte(rhs) + } + ); } }