From bf36c09eee4004f82786dcac874f65efc13b5bc6 Mon Sep 17 00:00:00 2001 From: GianfrancoBazzani Date: Wed, 15 Feb 2023 16:10:36 +0100 Subject: [PATCH 01/12] fix mul_two_256_bit_integers_works_4() test Test mul_two_256_bit_integers_works_4() fixed after the bug in the * operator patched --- math/src/unsigned_integer/element.rs | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/math/src/unsigned_integer/element.rs b/math/src/unsigned_integer/element.rs index dfab51e21..074420e8a 100644 --- a/math/src/unsigned_integer/element.rs +++ b/math/src/unsigned_integer/element.rs @@ -1609,8 +1609,7 @@ mod tests_u256 { #[test] fn mul_two_256_bit_integers_works_4() { - /* - // Failig + let a = U256::from("15bf61fcf53a3f0ae1e8e555d"); let b = U256::from("cbbc474761bb7995ff54e25fa5d5d0cde405e9f"); let c_expected = U256::from( @@ -1618,22 +1617,7 @@ mod tests_u256 { ); assert_eq!(a * b, c_expected); - */ - - // Passing - let a = U256::from("15bf61fcf53a3f0ae1e8e555d"); - let b = U256::from("cbbc474761bb7995ff54e25fa5d5d0cde405e9f"); - let c_expected = U256::from( - "114ec14db0c80d30b7dcb9c45948ef04cc149e612cb544f447b146553aff2ac3", - ); - - let (overflow, c) = U256::mul(&a, &b); - - - assert_eq!(c, c_expected); - assert_eq!(overflow, U256::from("0")); - } #[test] From 79e317eed966cb3d66251181219be090b397d979 Mon Sep 17 00:00:00 2001 From: GianfrancoBazzani Date: Wed, 15 Feb 2023 16:45:53 +0100 Subject: [PATCH 02/12] Update element.rs --- math/src/unsigned_integer/element.rs | 58 +++++++++++++++++----------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/math/src/unsigned_integer/element.rs b/math/src/unsigned_integer/element.rs index dfab51e21..177e26d53 100644 --- a/math/src/unsigned_integer/element.rs +++ b/math/src/unsigned_integer/element.rs @@ -475,9 +475,9 @@ impl ByteConversion for UnsignedInteger { #[cfg(test)] mod tests_u384 { - use super::*; - const NUM_LIMBS: usize = 6; - type U384 = UnsignedInteger; + + use crate::unsigned_integer::element::U384; + use crate::traits::ByteConversion; #[test] fn construct_new_integer_from_u64_1() { @@ -1153,10 +1153,10 @@ mod tests_u384 { } } +#[cfg(test)] mod tests_u256 { - use super::*; - const NUM_LIMBS: usize = 4; - type U256 = UnsignedInteger; + use crate::unsigned_integer::element::U256; + use crate::traits::ByteConversion; #[test] fn construct_new_integer_from_u64_1() { @@ -1609,8 +1609,7 @@ mod tests_u256 { #[test] fn mul_two_256_bit_integers_works_4() { - /* - // Failig + let a = U256::from("15bf61fcf53a3f0ae1e8e555d"); let b = U256::from("cbbc474761bb7995ff54e25fa5d5d0cde405e9f"); let c_expected = U256::from( @@ -1618,21 +1617,6 @@ mod tests_u256 { ); assert_eq!(a * b, c_expected); - */ - - // Passing - let a = U256::from("15bf61fcf53a3f0ae1e8e555d"); - let b = U256::from("cbbc474761bb7995ff54e25fa5d5d0cde405e9f"); - let c_expected = U256::from( - "114ec14db0c80d30b7dcb9c45948ef04cc149e612cb544f447b146553aff2ac3", - ); - - let (overflow, c) = U256::mul(&a, &b); - - - assert_eq!(c, c_expected); - assert_eq!(overflow, U256::from("0")); - } @@ -1755,4 +1739,32 @@ mod tests_u256 { let b = U256::from("5322c128ec84081b6c376c108ebd7fd3"); assert_eq!(&a >> (64 * 2), b); } + + #[test] + fn to_be_bytes_works() { + let number = U256::from_u64(1); + let expected_bytes = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; + + assert_eq!(number.to_bytes_be(), expected_bytes); + } + + #[test] + fn to_le_bytes_works() { + let number = U256::from_u64(1); + let expected_bytes = vec![1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + + assert_eq!(number.to_bytes_le(), expected_bytes); + } + + #[test] + fn from_bytes_be_works() { + let bytes = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; + let expected_number = U256::from_u64(1); + assert_eq!(U256::from_bytes_be(&bytes).unwrap(), expected_number); + } #[test] + fn from_bytes_le_works() { + let bytes = vec![1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + let expected_number = U256::from_u64(1); + assert_eq!(U256::from_bytes_le(&bytes).unwrap(), expected_number); + } } From b35aea1286cee8d082de87fd2eeffc929d443614 Mon Sep 17 00:00:00 2001 From: GianfrancoBazzani Date: Wed, 15 Feb 2023 17:02:05 +0100 Subject: [PATCH 03/12] Update element.rs --- math/src/unsigned_integer/element.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/math/src/unsigned_integer/element.rs b/math/src/unsigned_integer/element.rs index 177e26d53..3f78dc400 100644 --- a/math/src/unsigned_integer/element.rs +++ b/math/src/unsigned_integer/element.rs @@ -1761,7 +1761,9 @@ mod tests_u256 { let bytes = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; let expected_number = U256::from_u64(1); assert_eq!(U256::from_bytes_be(&bytes).unwrap(), expected_number); - } #[test] + } + + #[test] fn from_bytes_le_works() { let bytes = vec![1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; let expected_number = U256::from_u64(1); From e792ed1fa6841d515c089a928a1892b51de8cb66 Mon Sep 17 00:00:00 2001 From: GianfrancoBazzani Date: Wed, 15 Feb 2023 17:22:13 +0100 Subject: [PATCH 04/12] get rid of space --- math/src/unsigned_integer/element.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/math/src/unsigned_integer/element.rs b/math/src/unsigned_integer/element.rs index 3f78dc400..42693a056 100644 --- a/math/src/unsigned_integer/element.rs +++ b/math/src/unsigned_integer/element.rs @@ -475,7 +475,6 @@ impl ByteConversion for UnsignedInteger { #[cfg(test)] mod tests_u384 { - use crate::unsigned_integer::element::U384; use crate::traits::ByteConversion; From ad6dd9faa3cc8009f51a05b70323a081652b1530 Mon Sep 17 00:00:00 2001 From: GianfrancoBazzani Date: Sat, 18 Feb 2023 17:40:20 +0100 Subject: [PATCH 05/12] Update element.rs --- math/src/unsigned_integer/element.rs | 34 ++++++++++++++++++---------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/math/src/unsigned_integer/element.rs b/math/src/unsigned_integer/element.rs index 8ea4ad87f..b2fe0cbd7 100644 --- a/math/src/unsigned_integer/element.rs +++ b/math/src/unsigned_integer/element.rs @@ -475,8 +475,8 @@ impl ByteConversion for UnsignedInteger { #[cfg(test)] mod tests_u384 { - use crate::unsigned_integer::element::U384; use crate::traits::ByteConversion; + use crate::unsigned_integer::element::U384; #[test] fn construct_new_integer_from_u64_1() { @@ -1152,11 +1152,10 @@ mod tests_u384 { } } -#[cfg(test)] +#[cfg(test)] mod tests_u256 { - use crate::unsigned_integer::element::U256; use crate::traits::ByteConversion; - + use crate::unsigned_integer::element::U256; #[test] fn construct_new_integer_from_u64_1() { @@ -1599,9 +1598,8 @@ mod tests_u256 { fn mul_two_256_bit_integers_works_4() { let a = U256::from("15bf61fcf53a3f0ae1e8e555d"); let b = U256::from("cbbc474761bb7995ff54e25fa5d5d0cde405e9f"); - let c_expected = U256::from( - "114ec14db0c80d30b7dcb9c45948ef04cc149e612cb544f447b146553aff2ac3", - ); + let c_expected = + U256::from("114ec14db0c80d30b7dcb9c45948ef04cc149e612cb544f447b146553aff2ac3"); assert_eq!(a * b, c_expected); } @@ -1726,7 +1724,10 @@ mod tests_u256 { #[test] fn to_be_bytes_works() { let number = U256::from_u64(1); - let expected_bytes = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; + let expected_bytes = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, + ]; assert_eq!(number.to_bytes_be(), expected_bytes); } @@ -1734,21 +1735,30 @@ mod tests_u256 { #[test] fn to_le_bytes_works() { let number = U256::from_u64(1); - let expected_bytes = vec![1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + let expected_bytes = vec![ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; assert_eq!(number.to_bytes_le(), expected_bytes); } #[test] fn from_bytes_be_works() { - let bytes = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]; + let bytes = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, + ]; let expected_number = U256::from_u64(1); assert_eq!(U256::from_bytes_be(&bytes).unwrap(), expected_number); } - + #[test] fn from_bytes_le_works() { - let bytes = vec![1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + let bytes = vec![ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; let expected_number = U256::from_u64(1); assert_eq!(U256::from_bytes_le(&bytes).unwrap(), expected_number); } From 9425c6126097266665fcabbc8f92ec93a6b8ec57 Mon Sep 17 00:00:00 2001 From: GianfrancoBazzani Date: Mon, 27 Feb 2023 17:48:26 +0100 Subject: [PATCH 06/12] Generalize IsMontgomeryConfiguration and MontgomeryBackendPrimeField Solves https://github.com/lambdaclass/lambdaworks/issues/126 --- crypto/src/hash/poseidon/mod.rs | 8 +- .../curves/bls12_377/field_extension.rs | 8 +- .../curves/bls12_381/field_extension.rs | 8 +- .../short_weierstrass/curves/test_curve_2.rs | 6 +- math/src/field/fields/mod.rs | 2 +- ...d.rs => montgomery_backed_prime_fields.rs} | 84 +++++++++++-------- 6 files changed, 69 insertions(+), 47 deletions(-) rename math/src/field/fields/{u384_prime_field.rs => montgomery_backed_prime_fields.rs} (82%) diff --git a/crypto/src/hash/poseidon/mod.rs b/crypto/src/hash/poseidon/mod.rs index ae08793d4..d17568a6c 100644 --- a/crypto/src/hash/poseidon/mod.rs +++ b/crypto/src/hash/poseidon/mod.rs @@ -147,7 +147,9 @@ where #[cfg(test)] mod tests { use lambdaworks_math::{ - field::fields::u384_prime_field::{IsMontgomeryConfiguration, MontgomeryBackendPrimeField}, + field::fields::montgomery_backed_prime_fields::{ + IsMontgomeryConfiguration, U384PrimeField, + }, unsigned_integer::element::U384, }; @@ -155,7 +157,7 @@ mod tests { #[derive(Clone, Debug)] pub struct TestFieldConfig; - impl IsMontgomeryConfiguration for TestFieldConfig { + impl IsMontgomeryConfiguration<6> for TestFieldConfig { const MODULUS: U384 = U384::from("2000000000000080000000000000000000000000000000000000000000000001"); const MP: u64 = 18446744073709551615u64; @@ -163,7 +165,7 @@ mod tests { U384::from("C0000FFFFCFFFF800000000C0001FFFFFFFFBFFFF80000000140001FFFFE00"); } - pub type PoseidonTestField = MontgomeryBackendPrimeField; + pub type PoseidonTestField = U384PrimeField; type TestFieldElement = FieldElement; pub fn load_test_parameters() -> Result, String> { diff --git a/math/src/elliptic_curve/short_weierstrass/curves/bls12_377/field_extension.rs b/math/src/elliptic_curve/short_weierstrass/curves/bls12_377/field_extension.rs index 3181f3414..1a688e80d 100644 --- a/math/src/elliptic_curve/short_weierstrass/curves/bls12_377/field_extension.rs +++ b/math/src/elliptic_curve/short_weierstrass/curves/bls12_377/field_extension.rs @@ -1,6 +1,8 @@ use crate::field::{ element::FieldElement, - fields::u384_prime_field::{IsMontgomeryConfiguration, MontgomeryBackendPrimeField}, + fields::montgomery_backed_prime_fields::{ + IsMontgomeryConfiguration, MontgomeryBackendPrimeField, + }, }; use crate::unsigned_integer::element::U384; @@ -9,13 +11,13 @@ pub const BLS12377_PRIME_FIELD_ORDER: U384 = U384::from("1ae3a4617c510eac63b05c0 // FPBLS12377 #[derive(Clone, Debug)] pub struct BLS12377FieldConfig; -impl IsMontgomeryConfiguration for BLS12377FieldConfig { +impl IsMontgomeryConfiguration<6> for BLS12377FieldConfig { const MODULUS: U384 = BLS12377_PRIME_FIELD_ORDER; const MP: u64 = 9586122913090633727; const R2: U384 = U384::from("6dfccb1e914b88837e92f041790bf9bfdf7d03827dc3ac22a5f11162d6b46d0329fcaab00431b1b786686c9400cd22"); } -pub type BLS12377PrimeField = MontgomeryBackendPrimeField; +pub type BLS12377PrimeField = MontgomeryBackendPrimeField; impl FieldElement { pub fn new_base(a_hex: &str) -> Self { diff --git a/math/src/elliptic_curve/short_weierstrass/curves/bls12_381/field_extension.rs b/math/src/elliptic_curve/short_weierstrass/curves/bls12_381/field_extension.rs index 974c6d6fc..ab5f7d12f 100644 --- a/math/src/elliptic_curve/short_weierstrass/curves/bls12_381/field_extension.rs +++ b/math/src/elliptic_curve/short_weierstrass/curves/bls12_381/field_extension.rs @@ -4,7 +4,9 @@ use crate::field::{ cubic::{CubicExtensionField, HasCubicNonResidue}, quadratic::{HasQuadraticNonResidue, QuadraticExtensionField}, }, - fields::u384_prime_field::{IsMontgomeryConfiguration, MontgomeryBackendPrimeField}, + fields::montgomery_backed_prime_fields::{ + IsMontgomeryConfiguration, MontgomeryBackendPrimeField, + }, }; use crate::unsigned_integer::element::U384; @@ -13,13 +15,13 @@ pub const BLS12381_PRIME_FIELD_ORDER: U384 = U384::from("1a0111ea397fe69a4b1ba7b // FPBLS12381 #[derive(Clone, Debug)] pub struct BLS12381FieldConfig; -impl IsMontgomeryConfiguration for BLS12381FieldConfig { +impl IsMontgomeryConfiguration<6> for BLS12381FieldConfig { const MODULUS: U384 = BLS12381_PRIME_FIELD_ORDER; const MP: u64 = 9940570264628428797; const R2: U384 = U384::from("11988fe592cae3aa9a793e85b519952d67eb88a9939d83c08de5476c4c95b6d50a76e6a609d104f1f4df1f341c341746"); } -pub type BLS12381PrimeField = MontgomeryBackendPrimeField; +pub type BLS12381PrimeField = MontgomeryBackendPrimeField; #[derive(Debug, Clone)] pub struct LevelOneResidue; diff --git a/math/src/elliptic_curve/short_weierstrass/curves/test_curve_2.rs b/math/src/elliptic_curve/short_weierstrass/curves/test_curve_2.rs index d5238d786..d4225ac0e 100644 --- a/math/src/elliptic_curve/short_weierstrass/curves/test_curve_2.rs +++ b/math/src/elliptic_curve/short_weierstrass/curves/test_curve_2.rs @@ -1,6 +1,6 @@ use crate::elliptic_curve::short_weierstrass::point::ShortWeierstrassProjectivePoint; use crate::elliptic_curve::traits::IsEllipticCurve; -use crate::field::fields::u384_prime_field::{ +use crate::field::fields::montgomery_backed_prime_fields::{ IsMontgomeryConfiguration, MontgomeryBackendPrimeField, }; use crate::unsigned_integer::element::U384; @@ -21,13 +21,13 @@ pub const TEST_CURVE_2_MAIN_SUBGROUP_ORDER: U384 = U384::from("40a065fb5a76390de // FPBLS12381 #[derive(Clone, Debug)] pub struct TestCurve2MontgomeryConfig; -impl IsMontgomeryConfiguration for TestCurve2MontgomeryConfig { +impl IsMontgomeryConfiguration<6> for TestCurve2MontgomeryConfig { const MODULUS: U384 = TEST_CURVE_2_PRIME_FIELD_ORDER; const MP: u64 = 1901108026836139985; const R2: U384 = U384::from("f60e53d42ca85ba186067660c4f2daa94"); } -type TestCurve2PrimeField = MontgomeryBackendPrimeField; +type TestCurve2PrimeField = MontgomeryBackendPrimeField; /// In F59 the element -1 is not a square. We use this property /// to construct a Quadratic Field Extension out of it by adding diff --git a/math/src/field/fields/mod.rs b/math/src/field/fields/mod.rs index e79c6b653..61a6de417 100644 --- a/math/src/field/fields/mod.rs +++ b/math/src/field/fields/mod.rs @@ -1,3 +1,3 @@ -pub mod u384_prime_field; +pub mod montgomery_backed_prime_fields; /// Implementation of prime fields over 64 bit unsigned integers. pub mod u64_prime_field; diff --git a/math/src/field/fields/u384_prime_field.rs b/math/src/field/fields/montgomery_backed_prime_fields.rs similarity index 82% rename from math/src/field/fields/u384_prime_field.rs rename to math/src/field/fields/montgomery_backed_prime_fields.rs index ab66de95d..628fce4cc 100644 --- a/math/src/field/fields/u384_prime_field.rs +++ b/math/src/field/fields/montgomery_backed_prime_fields.rs @@ -1,6 +1,5 @@ use crate::field::element::FieldElement; use crate::traits::ByteConversion; -use crate::unsigned_integer::element::U384; use crate::{ field::traits::IsField, unsigned_integer::element::UnsignedInteger, unsigned_integer::montgomery::MontgomeryAlgorithms, @@ -8,32 +7,36 @@ use crate::{ use std::fmt::Debug; use std::marker::PhantomData; +pub type U384PrimeField = MontgomeryBackendPrimeField; +pub type U256PrimeField = MontgomeryBackendPrimeField; + /// This trait is necessary for us to be able to use unsigned integer types bigger than /// `u128` (the biggest native `unit`) as constant generics. /// This trait should be removed when Rust supports this feature. -pub trait IsMontgomeryConfiguration { - const MODULUS: U384; - const R2: U384; + +pub trait IsMontgomeryConfiguration { + const MODULUS: UnsignedInteger; + const R2: UnsignedInteger; const MP: u64; } #[derive(Clone, Debug)] -pub struct MontgomeryBackendPrimeField { +pub struct MontgomeryBackendPrimeField { phantom: PhantomData, } -impl MontgomeryBackendPrimeField +impl MontgomeryBackendPrimeField where - C: IsMontgomeryConfiguration, + C: IsMontgomeryConfiguration, { - const ZERO: U384 = UnsignedInteger::from_u64(0); + const ZERO: UnsignedInteger = UnsignedInteger::from_u64(0); } -impl IsField for MontgomeryBackendPrimeField +impl IsField for MontgomeryBackendPrimeField where - C: IsMontgomeryConfiguration + Clone + Debug, + C: IsMontgomeryConfiguration + Clone + Debug, { - type BaseType = U384; + type BaseType = UnsignedInteger; fn add(a: &Self::BaseType, b: &Self::BaseType) -> Self::BaseType { let (sum, overflow) = UnsignedInteger::add(a, b); @@ -101,51 +104,61 @@ where } } -impl ByteConversion for FieldElement> +impl ByteConversion + for FieldElement> where - C: IsMontgomeryConfiguration + Clone + Debug, + C: IsMontgomeryConfiguration + Clone + Debug, { fn to_bytes_be(&self) -> Vec { - MontgomeryAlgorithms::cios(self.value(), &U384::from_u64(1), &C::MODULUS, &C::MP) - .to_bytes_be() + MontgomeryAlgorithms::cios( + self.value(), + &UnsignedInteger::from_u64(1), + &C::MODULUS, + &C::MP, + ) + .to_bytes_be() } fn to_bytes_le(&self) -> Vec { - MontgomeryAlgorithms::cios(self.value(), &U384::from_u64(1), &C::MODULUS, &C::MP) - .to_bytes_le() + MontgomeryAlgorithms::cios( + self.value(), + &UnsignedInteger::from_u64(1), + &C::MODULUS, + &C::MP, + ) + .to_bytes_le() } fn from_bytes_be(bytes: &[u8]) -> Result { - let value = U384::from_bytes_be(bytes)?; + let value = UnsignedInteger::from_bytes_be(bytes)?; Ok(Self::new(value)) } fn from_bytes_le(bytes: &[u8]) -> Result { - let value = U384::from_bytes_le(bytes)?; + let value = UnsignedInteger::from_bytes_le(bytes)?; Ok(Self::new(value)) } } #[cfg(test)] -mod tests { - use crate::{ - field::element::FieldElement, - traits::ByteConversion, - unsigned_integer::element::{UnsignedInteger, U384}, +mod tests_u384_prime_fields { + use crate::field::element::FieldElement; + use crate::field::fields::montgomery_backed_prime_fields::{ + IsMontgomeryConfiguration, U384PrimeField, }; + use crate::traits::ByteConversion; + use crate::unsigned_integer::element::UnsignedInteger; + use crate::unsigned_integer::element::U384; - use super::{IsMontgomeryConfiguration, MontgomeryBackendPrimeField}; - - // F23 #[derive(Clone, Debug)] - struct MontgomeryConfig23; - impl IsMontgomeryConfiguration for MontgomeryConfig23 { + struct MontgomeryConfiguration23; + impl IsMontgomeryConfiguration<6> for MontgomeryConfiguration23 { const MODULUS: U384 = UnsignedInteger::from_u64(23); const MP: u64 = 3208129404123400281; const R2: U384 = UnsignedInteger::from_u64(6); } - type F23 = MontgomeryBackendPrimeField; + type F23 = U384PrimeField; type F23Element = FieldElement; #[test] @@ -287,7 +300,7 @@ mod tests { // FP1 #[derive(Clone, Debug)] struct MontgomeryConfigP1; - impl IsMontgomeryConfiguration for MontgomeryConfigP1 { + impl IsMontgomeryConfiguration<6> for MontgomeryConfigP1 { const MODULUS: U384 = UnsignedInteger { limbs: [ 0, @@ -318,7 +331,7 @@ mod tests { assert_eq!(x + y, c); } - type FP1 = MontgomeryBackendPrimeField; + type FP1 = U384PrimeField; type FP1Element = FieldElement; #[test] fn montgomery_prime_field_multiplication_works_0() { @@ -337,7 +350,7 @@ mod tests { // FP2 #[derive(Clone, Debug)] struct MontgomeryConfigP2; - impl IsMontgomeryConfiguration for MontgomeryConfigP2 { + impl IsMontgomeryConfiguration<6> for MontgomeryConfigP2 { const MODULUS: U384 = UnsignedInteger { limbs: [ 18446744073709551615, @@ -354,7 +367,7 @@ mod tests { }; } - type FP2 = MontgomeryBackendPrimeField; + type FP2 = U384PrimeField; type FP2Element = FieldElement; #[test] @@ -420,3 +433,6 @@ mod tests { ); } } + +#[cfg(test)] +mod tests_u256_prime_fields {} From e9c24c5ec7456240e997d66eacb5edaa7c02b61e Mon Sep 17 00:00:00 2001 From: GianfrancoBazzani Date: Tue, 28 Feb 2023 09:47:21 +0100 Subject: [PATCH 07/12] Extended U256 Montgomery backed prime fields test --- .../fields/montgomery_backed_prime_fields.rs | 465 +++++++++++++++--- 1 file changed, 395 insertions(+), 70 deletions(-) diff --git a/math/src/field/fields/montgomery_backed_prime_fields.rs b/math/src/field/fields/montgomery_backed_prime_fields.rs index 628fce4cc..afb5c04d0 100644 --- a/math/src/field/fields/montgomery_backed_prime_fields.rs +++ b/math/src/field/fields/montgomery_backed_prime_fields.rs @@ -151,21 +151,21 @@ mod tests_u384_prime_fields { use crate::unsigned_integer::element::U384; #[derive(Clone, Debug)] - struct MontgomeryConfiguration23; - impl IsMontgomeryConfiguration<6> for MontgomeryConfiguration23 { + struct U384MontgomeryConfiguration23; + impl IsMontgomeryConfiguration<6> for U384MontgomeryConfiguration23 { const MODULUS: U384 = UnsignedInteger::from_u64(23); const MP: u64 = 3208129404123400281; const R2: U384 = UnsignedInteger::from_u64(6); } - type F23 = U384PrimeField; - type F23Element = FieldElement; + type U384F23 = U384PrimeField; + type U384F23Element = FieldElement; #[test] fn montgomery_backend_multiplication_works_0() { - let x = F23Element::from(11_u64); - let y = F23Element::from(10_u64); - let c = F23Element::from(110_u64); + let x = U384F23Element::from(11_u64); + let y = U384F23Element::from(10_u64); + let c = U384F23Element::from(110_u64); assert_eq!(x * y, c); } @@ -173,134 +173,137 @@ mod tests_u384_prime_fields { #[test] fn two_plus_one_is_three() { assert_eq!( - F23Element::from(2) + F23Element::from(1), - F23Element::from(3) + U384F23Element::from(2) + U384F23Element::from(1), + U384F23Element::from(3) ); } #[test] fn max_order_plus_1_is_0() { assert_eq!( - F23Element::from((ORDER - 1) as u64) + F23Element::from(1), - F23Element::from(0) + U384F23Element::from((ORDER - 1) as u64) + U384F23Element::from(1), + U384F23Element::from(0) ); } #[test] fn when_comparing_13_and_13_they_are_equal() { - let a: F23Element = F23Element::from(13); - let b: F23Element = F23Element::from(13); + let a: U384F23Element = U384F23Element::from(13); + let b: U384F23Element = U384F23Element::from(13); assert_eq!(a, b); } #[test] fn when_comparing_13_and_8_they_are_different() { - let a: F23Element = F23Element::from(13); - let b: F23Element = F23Element::from(8); + let a: U384F23Element = U384F23Element::from(13); + let b: U384F23Element = U384F23Element::from(8); assert_ne!(a, b); } #[test] fn mul_neutral_element() { - let a: F23Element = F23Element::from(1); - let b: F23Element = F23Element::from(2); - assert_eq!(a * b, F23Element::from(2)); + let a: U384F23Element = U384F23Element::from(1); + let b: U384F23Element = U384F23Element::from(2); + assert_eq!(a * b, U384F23Element::from(2)); } #[test] fn mul_2_3_is_6() { - let a: F23Element = F23Element::from(2); - let b: F23Element = F23Element::from(3); - assert_eq!(a * b, F23Element::from(6)); + let a: U384F23Element = U384F23Element::from(2); + let b: U384F23Element = U384F23Element::from(3); + assert_eq!(a * b, U384F23Element::from(6)); } #[test] fn mul_order_minus_1() { - let a: F23Element = F23Element::from((ORDER - 1) as u64); - let b: F23Element = F23Element::from((ORDER - 1) as u64); - assert_eq!(a * b, F23Element::from(1)); + let a: U384F23Element = U384F23Element::from((ORDER - 1) as u64); + let b: U384F23Element = U384F23Element::from((ORDER - 1) as u64); + assert_eq!(a * b, U384F23Element::from(1)); } #[test] #[should_panic] fn inv_0_error() { - F23Element::from(0).inv(); + U384F23Element::from(0).inv(); } #[test] fn inv_2() { - let a: F23Element = F23Element::from(2); - assert_eq!(&a * a.inv(), F23Element::from(1)); + let a: U384F23Element = U384F23Element::from(2); + assert_eq!(&a * a.inv(), U384F23Element::from(1)); } #[test] fn pow_2_3() { - assert_eq!(F23Element::from(2).pow(3_u64), F23Element::from(8)) + assert_eq!(U384F23Element::from(2).pow(3_u64), U384F23Element::from(8)) } #[test] fn pow_p_minus_1() { - assert_eq!(F23Element::from(2).pow(ORDER - 1), F23Element::from(1)) + assert_eq!( + U384F23Element::from(2).pow(ORDER - 1), + U384F23Element::from(1) + ) } #[test] fn div_1() { assert_eq!( - F23Element::from(2) / F23Element::from(1), - F23Element::from(2) + U384F23Element::from(2) / U384F23Element::from(1), + U384F23Element::from(2) ) } #[test] fn div_4_2() { assert_eq!( - F23Element::from(4) / F23Element::from(2), - F23Element::from(2) + U384F23Element::from(4) / U384F23Element::from(2), + U384F23Element::from(2) ) } #[test] fn div_4_3() { assert_eq!( - F23Element::from(4) / F23Element::from(3) * F23Element::from(3), - F23Element::from(4) + U384F23Element::from(4) / U384F23Element::from(3) * U384F23Element::from(3), + U384F23Element::from(4) ) } #[test] fn two_plus_its_additive_inv_is_0() { - let two = F23Element::from(2); + let two = U384F23Element::from(2); - assert_eq!(&two + (-&two), F23Element::from(0)) + assert_eq!(&two + (-&two), U384F23Element::from(0)) } #[test] fn four_minus_three_is_1() { - let four = F23Element::from(4); - let three = F23Element::from(3); + let four = U384F23Element::from(4); + let three = U384F23Element::from(3); - assert_eq!(four - three, F23Element::from(1)) + assert_eq!(four - three, U384F23Element::from(1)) } #[test] fn zero_minus_1_is_order_minus_1() { - let zero = F23Element::from(0); - let one = F23Element::from(1); + let zero = U384F23Element::from(0); + let one = U384F23Element::from(1); - assert_eq!(zero - one, F23Element::from((ORDER - 1) as u64)) + assert_eq!(zero - one, U384F23Element::from((ORDER - 1) as u64)) } #[test] fn neg_zero_is_zero() { - let zero = F23Element::from(0); + let zero = U384F23Element::from(0); assert_eq!(-&zero, zero); } // FP1 #[derive(Clone, Debug)] - struct MontgomeryConfigP1; - impl IsMontgomeryConfiguration<6> for MontgomeryConfigP1 { + struct U384MontgomeryConfigP1; + impl IsMontgomeryConfiguration<6> for U384MontgomeryConfigP1 { const MODULUS: U384 = UnsignedInteger { limbs: [ 0, @@ -317,31 +320,32 @@ mod tests_u384_prime_fields { }; } + type U384FP1 = U384PrimeField; + type U384FP1Element = FieldElement; + #[test] fn montgomery_prime_field_addition_works_0() { - let x = FP1Element::new(UnsignedInteger::from( + let x = U384FP1Element::new(UnsignedInteger::from( "05ed176deb0e80b4deb7718cdaa075165f149c", )); - let y = FP1Element::new(UnsignedInteger::from( + let y = U384FP1Element::new(UnsignedInteger::from( "5f103b0bd4397d4df560eb559f38353f80eeb6", )); - let c = FP1Element::new(UnsignedInteger::from( + let c = U384FP1Element::new(UnsignedInteger::from( "64fd5279bf47fe02d4185ce279d8aa55e00352", )); assert_eq!(x + y, c); } - type FP1 = U384PrimeField; - type FP1Element = FieldElement; #[test] fn montgomery_prime_field_multiplication_works_0() { - let x = FP1Element::new(UnsignedInteger::from( + let x = U384FP1Element::new(UnsignedInteger::from( "05ed176deb0e80b4deb7718cdaa075165f149c", )); - let y = FP1Element::new(UnsignedInteger::from( + let y = U384FP1Element::new(UnsignedInteger::from( "5f103b0bd4397d4df560eb559f38353f80eeb6", )); - let c = FP1Element::new(UnsignedInteger::from( + let c = U384FP1Element::new(UnsignedInteger::from( "73d23e8d462060dc23d5c15c00fc432d95621a3c", )); assert_eq!(x * y, c); @@ -349,8 +353,8 @@ mod tests_u384_prime_fields { // FP2 #[derive(Clone, Debug)] - struct MontgomeryConfigP2; - impl IsMontgomeryConfiguration<6> for MontgomeryConfigP2 { + struct U384MontgomeryConfigP2; + impl IsMontgomeryConfiguration<6> for U384MontgomeryConfigP2 { const MODULUS: U384 = UnsignedInteger { limbs: [ 18446744073709551615, @@ -367,18 +371,18 @@ mod tests_u384_prime_fields { }; } - type FP2 = U384PrimeField; - type FP2Element = FieldElement; + type U384FP2 = U384PrimeField; + type U384FP2Element = FieldElement; #[test] fn montgomery_prime_field_addition_works_1() { - let x = FP2Element::new(UnsignedInteger::from( + let x = U384FP2Element::new(UnsignedInteger::from( "05ed176deb0e80b4deb7718cdaa075165f149c", )); - let y = FP2Element::new(UnsignedInteger::from( + let y = U384FP2Element::new(UnsignedInteger::from( "5f103b0bd4397d4df560eb559f38353f80eeb6", )); - let c = FP2Element::new(UnsignedInteger::from( + let c = U384FP2Element::new(UnsignedInteger::from( "64fd5279bf47fe02d4185ce279d8aa55e00352", )); assert_eq!(x + y, c); @@ -386,8 +390,8 @@ mod tests_u384_prime_fields { #[test] fn montgomery_prime_field_multiplication_works_1() { - let x = FP2Element::one(); - let y = FP2Element::new(UnsignedInteger::from( + let x = U384FP2Element::one(); + let y = U384FP2Element::new(UnsignedInteger::from( "5f103b0bd4397d4df560eb559f38353f80eeb6", )); assert_eq!(&y * x, y); @@ -395,10 +399,10 @@ mod tests_u384_prime_fields { #[test] fn to_bytes_from_bytes_be_is_the_identity() { - let x = FP2Element::new(UnsignedInteger::from( + let x = U384FP2Element::new(UnsignedInteger::from( "5f103b0bd4397d4df560eb559f38353f80eeb6", )); - assert_eq!(FP2Element::from_bytes_be(&x.to_bytes_be()).unwrap(), x); + assert_eq!(U384FP2Element::from_bytes_be(&x.to_bytes_be()).unwrap(), x); } #[test] @@ -408,17 +412,17 @@ mod tests_u384_prime_fields { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ]; assert_eq!( - FP2Element::from_bytes_be(&bytes).unwrap().to_bytes_be(), + U384FP2Element::from_bytes_be(&bytes).unwrap().to_bytes_be(), bytes ); } #[test] fn to_bytes_from_bytes_le_is_the_identity() { - let x = FP2Element::new(UnsignedInteger::from( + let x = U384FP2Element::new(UnsignedInteger::from( "5f103b0bd4397d4df560eb559f38353f80eeb6", )); - assert_eq!(FP2Element::from_bytes_le(&x.to_bytes_le()).unwrap(), x); + assert_eq!(U384FP2Element::from_bytes_le(&x.to_bytes_le()).unwrap(), x); } #[test] @@ -428,11 +432,332 @@ mod tests_u384_prime_fields { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; assert_eq!( - FP2Element::from_bytes_le(&bytes).unwrap().to_bytes_le(), + U384FP2Element::from_bytes_le(&bytes).unwrap().to_bytes_le(), bytes ); } } #[cfg(test)] -mod tests_u256_prime_fields {} +mod tests_u256_prime_fields { + use crate::field::element::FieldElement; + use crate::field::fields::montgomery_backed_prime_fields::{ + IsMontgomeryConfiguration, U256PrimeField, + }; + use crate::traits::ByteConversion; + use crate::unsigned_integer::element::UnsignedInteger; + use crate::unsigned_integer::element::U256; + + #[derive(Clone, Debug)] + struct U256MontgomeryConfiguration29; + impl IsMontgomeryConfiguration<4> for U256MontgomeryConfiguration29 { + const MODULUS: U256 = UnsignedInteger::from_u64(29); + const MP: u64 = 14630176334321368523; + const R2: U256 = UnsignedInteger::from_u64(24); + } + + type U256F29 = U256PrimeField; + type U256F29Element = FieldElement; + + #[test] + fn montgomery_backend_multiplication_works_0() { + let x = U256F29Element::from(11_u64); + let y = U256F29Element::from(10_u64); + let c = U256F29Element::from(110_u64); + assert_eq!(x * y, c); + } + + const ORDER: usize = 29; + #[test] + fn two_plus_one_is_three() { + assert_eq!( + U256F29Element::from(2) + U256F29Element::from(1), + U256F29Element::from(3) + ); + } + + #[test] + fn max_order_plus_1_is_0() { + assert_eq!( + U256F29Element::from((ORDER - 1) as u64) + U256F29Element::from(1), + U256F29Element::from(0) + ); + } + + #[test] + fn when_comparing_13_and_13_they_are_equal() { + let a: U256F29Element = U256F29Element::from(13); + let b: U256F29Element = U256F29Element::from(13); + assert_eq!(a, b); + } + + #[test] + fn when_comparing_13_and_8_they_are_different() { + let a: U256F29Element = U256F29Element::from(13); + let b: U256F29Element = U256F29Element::from(8); + assert_ne!(a, b); + } + + #[test] + fn mul_neutral_element() { + let a: U256F29Element = U256F29Element::from(1); + let b: U256F29Element = U256F29Element::from(2); + assert_eq!(a * b, U256F29Element::from(2)); + } + + #[test] + fn mul_2_3_is_6() { + let a: U256F29Element = U256F29Element::from(2); + let b: U256F29Element = U256F29Element::from(3); + assert_eq!(a * b, U256F29Element::from(6)); + } + + #[test] + fn mul_order_minus_1() { + let a: U256F29Element = U256F29Element::from((ORDER - 1) as u64); + let b: U256F29Element = U256F29Element::from((ORDER - 1) as u64); + assert_eq!(a * b, U256F29Element::from(1)); + } + + #[test] + #[should_panic] + fn inv_0_error() { + U256F29Element::from(0).inv(); + } + + #[test] + fn inv_2() { + let a: U256F29Element = U256F29Element::from(2); + assert_eq!(&a * a.inv(), U256F29Element::from(1)); + } + + #[test] + fn pow_2_3() { + assert_eq!(U256F29Element::from(2).pow(3_u64), U256F29Element::from(8)) + } + + #[test] + fn pow_p_minus_1() { + assert_eq!( + U256F29Element::from(2).pow(ORDER - 1), + U256F29Element::from(1) + ) + } + + #[test] + fn div_1() { + assert_eq!( + U256F29Element::from(2) / U256F29Element::from(1), + U256F29Element::from(2) + ) + } + + #[test] + fn div_4_2() { + assert_eq!( + U256F29Element::from(4) / U256F29Element::from(2), + U256F29Element::from(2) + ) + } + + #[test] + fn div_4_3() { + assert_eq!( + U256F29Element::from(4) / U256F29Element::from(3) * U256F29Element::from(3), + U256F29Element::from(4) + ) + } + + #[test] + fn two_plus_its_additive_inv_is_0() { + let two = U256F29Element::from(2); + + assert_eq!(&two + (-&two), U256F29Element::from(0)) + } + + #[test] + fn four_minus_three_is_1() { + let four = U256F29Element::from(4); + let three = U256F29Element::from(3); + + assert_eq!(four - three, U256F29Element::from(1)) + } + + #[test] + fn zero_minus_1_is_order_minus_1() { + let zero = U256F29Element::from(0); + let one = U256F29Element::from(1); + + assert_eq!(zero - one, U256F29Element::from((ORDER - 1) as u64)) + } + + #[test] + fn neg_zero_is_zero() { + let zero = U256F29Element::from(0); + + assert_eq!(-&zero, zero); + } + + // FP1 + //TODO, buil a test using SAGE + + /*Python helper to work with limbs + + def from_limbs(limbs): + uint=0 + for i in range(len(limbs)): + index = len(limbs) - i -1 + uint = uint + (limbs[index] << (64*i)) + return uint + + def from_uint(uint,n_limbs): + limbs = [] + for i in range(n_limbs): + index = n_limbs - i -1 + mask = (pow(2,64) -1) << (64*index) + limbs.append((uint & mask) >> 64*index) + return limbs + */ + #[derive(Clone, Debug)] + struct U256MontgomeryConfigP1; + impl IsMontgomeryConfiguration<4> for U256MontgomeryConfigP1 { + const MODULUS: U256 = UnsignedInteger { + limbs: [ + 63, + 3026140964809723730, + 7835788046214001203, + 16962966595042587443, + ], + //396487151739502753262805464559750051512474188475143212544819 + }; + const MP: u64 = 8665272125093150725; + const R2: U256 = UnsignedInteger { + limbs: [ + 7, + 9694576771155704591, + 15518527791430854876, + 15195315649472397251, + ], + //[7, 9694576771155704591, 15518527791430854876, 15195315649472397251] + }; + } + + type U256FP1 = U256PrimeField; + type U256FP1Element = FieldElement; + + #[test] + fn montgomery_prime_field_addition_works_0() { + let x = U256FP1Element::new(UnsignedInteger::from( + "05ed176deb0e80b4deb7718cdaa075165f149c", + )); + let y = U256FP1Element::new(UnsignedInteger::from( + "5f103b0bd4397d4df560eb559f38353f80eeb6", + )); + let c = U256FP1Element::new(UnsignedInteger::from( + "64fd5279bf47fe02d4185ce279d8aa55e00352", + )); + assert_eq!(x + y, c); + } + + /* + MAKE TEST PASS, USE SAGE MATH + #[test] + fn montgomery_prime_field_multiplication_works_0() { + let x = U256FP1Element::new(UnsignedInteger::from( + "05ed176deb0e80b4deb7718cdaa075165f149c", + )); + let y = U256FP1Element::new(UnsignedInteger::from( + "5f103b0bd4397d4df560eb559f38353f80eeb6", + )); + let c = U256FP1Element::new(UnsignedInteger::from( + "73d23e8d462060dc23d5c15c00fc432d95621a3c", + )); + assert_eq!(x * y, c); + } + */ + /* + // FP2 + #[derive(Clone, Debug)] + struct MontgomeryConfigP2; + impl IsMontgomeryConfiguration<4> for MontgomeryConfigP2 { + const MODULUS: U256 = UnsignedInteger { + limbs: [ + 18446744073709551615, + 18446744073709551615, + 18446744073709551615, + 18446744073709551275, + ], + }; + const MP: u64 = 14984598558409225213; + const R2: U256 = UnsignedInteger { + limbs: [0, 0, 0, 116281], + }; + } + + type FP2 = U256PrimeField; + type FP2Element = FieldElement; + + #[test] + fn montgomery_prime_field_addition_works_1() { + let x = FP2Element::new(UnsignedInteger::from( + "05ed176deb0e80b4deb7718cdaa075165f149c", + )); + let y = FP2Element::new(UnsignedInteger::from( + "5f103b0bd4397d4df560eb559f38353f80eeb6", + )); + let c = FP2Element::new(UnsignedInteger::from( + "64fd5279bf47fe02d4185ce279d8aa55e00352", + )); + assert_eq!(x + y, c); + } + + #[test] + fn montgomery_prime_field_multiplication_works_1() { + let x = FP2Element::one(); + let y = FP2Element::new(UnsignedInteger::from( + "5f103b0bd4397d4df560eb559f38353f80eeb6", + )); + assert_eq!(&y * x, y); + } + + #[test] + fn to_bytes_from_bytes_be_is_the_identity() { + let x = FP2Element::new(UnsignedInteger::from( + "5f103b0bd4397d4df560eb559f38353f80eeb6", + )); + assert_eq!(FP2Element::from_bytes_be(&x.to_bytes_be()).unwrap(), x); + } + + #[test] + fn from_bytes_to_bytes_be_is_the_identity_for_one() { + let bytes = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + ]; + assert_eq!( + FP2Element::from_bytes_be(&bytes).unwrap().to_bytes_be(), + bytes + ); + } + + #[test] + fn to_bytes_from_bytes_le_is_the_identity() { + let x = FP2Element::new(UnsignedInteger::from( + "5f103b0bd4397d4df560eb559f38353f80eeb6", + )); + assert_eq!(FP2Element::from_bytes_le(&x.to_bytes_le()).unwrap(), x); + } + + #[test] + fn from_bytes_to_bytes_le_is_the_identity_for_one() { + let bytes = vec![ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; + assert_eq!( + FP2Element::from_bytes_le(&bytes).unwrap().to_bytes_le(), + bytes + ); + } + */ +} From da59dc80ed99433d2978e2cc792e67715d738d29 Mon Sep 17 00:00:00 2001 From: GianfrancoBazzani Date: Thu, 2 Mar 2023 08:59:21 +0100 Subject: [PATCH 08/12] Implemented U256PrimeField solves https://github.com/lambdaclass/lambdaworks/issues/95 --- .../fields/montgomery_backed_prime_fields.rs | 228 ++++++++---------- 1 file changed, 104 insertions(+), 124 deletions(-) diff --git a/math/src/field/fields/montgomery_backed_prime_fields.rs b/math/src/field/fields/montgomery_backed_prime_fields.rs index afb5c04d0..c7b3e313c 100644 --- a/math/src/field/fields/montgomery_backed_prime_fields.rs +++ b/math/src/field/fields/montgomery_backed_prime_fields.rs @@ -599,46 +599,25 @@ mod tests_u256_prime_fields { } // FP1 - //TODO, buil a test using SAGE - - /*Python helper to work with limbs - - def from_limbs(limbs): - uint=0 - for i in range(len(limbs)): - index = len(limbs) - i -1 - uint = uint + (limbs[index] << (64*i)) - return uint - - def from_uint(uint,n_limbs): - limbs = [] - for i in range(n_limbs): - index = n_limbs - i -1 - mask = (pow(2,64) -1) << (64*index) - limbs.append((uint & mask) >> 64*index) - return limbs - */ #[derive(Clone, Debug)] struct U256MontgomeryConfigP1; impl IsMontgomeryConfiguration<4> for U256MontgomeryConfigP1 { const MODULUS: U256 = UnsignedInteger { limbs: [ - 63, - 3026140964809723730, - 7835788046214001203, - 16962966595042587443, + 8366, + 8155137382671976874, + 227688614771682406, + 15723111795979912613, ], - //396487151739502753262805464559750051512474188475143212544819 }; - const MP: u64 = 8665272125093150725; + const MP: u64 = 13870950301186631123; const R2: U256 = UnsignedInteger { limbs: [ - 7, - 9694576771155704591, - 15518527791430854876, - 15195315649472397251, + 3199, + 7378261192024362008, + 10023267389842517467, + 17706296746204571208, ], - //[7, 9694576771155704591, 15518527791430854876, 15195315649472397251] }; } @@ -648,116 +627,117 @@ mod tests_u256_prime_fields { #[test] fn montgomery_prime_field_addition_works_0() { let x = U256FP1Element::new(UnsignedInteger::from( - "05ed176deb0e80b4deb7718cdaa075165f149c", + "93e712950bf3fe589aa030562a44b1cec66b09192c4bcf705a5", )); let y = U256FP1Element::new(UnsignedInteger::from( - "5f103b0bd4397d4df560eb559f38353f80eeb6", + "10a712235c1f6b4172a1e35da6aef1a7ec6b09192c4bb88cfa5", )); let c = U256FP1Element::new(UnsignedInteger::from( - "64fd5279bf47fe02d4185ce279d8aa55e00352", + "a48e24b86813699a0d4213b3d0f3a376b2d61232589787fd54a", )); assert_eq!(x + y, c); } - /* - MAKE TEST PASS, USE SAGE MATH #[test] fn montgomery_prime_field_multiplication_works_0() { let x = U256FP1Element::new(UnsignedInteger::from( - "05ed176deb0e80b4deb7718cdaa075165f149c", + "93e712950bf3fe589aa030562a44b1cec66b09192c4bcf705a5", )); let y = U256FP1Element::new(UnsignedInteger::from( - "5f103b0bd4397d4df560eb559f38353f80eeb6", + "10a712235c1f6b4172a1e35da6aef1a7ec6b09192c4bb88cfa5", )); let c = U256FP1Element::new(UnsignedInteger::from( - "73d23e8d462060dc23d5c15c00fc432d95621a3c", + "7808e74c3208d9a66791ef9cc15a46acc9951ee312102684021", )); assert_eq!(x * y, c); } - */ - /* - // FP2 - #[derive(Clone, Debug)] - struct MontgomeryConfigP2; - impl IsMontgomeryConfiguration<4> for MontgomeryConfigP2 { - const MODULUS: U256 = UnsignedInteger { - limbs: [ - 18446744073709551615, - 18446744073709551615, - 18446744073709551615, - 18446744073709551275, - ], - }; - const MP: u64 = 14984598558409225213; - const R2: U256 = UnsignedInteger { - limbs: [0, 0, 0, 116281], - }; - } - - type FP2 = U256PrimeField; - type FP2Element = FieldElement; - - #[test] - fn montgomery_prime_field_addition_works_1() { - let x = FP2Element::new(UnsignedInteger::from( - "05ed176deb0e80b4deb7718cdaa075165f149c", - )); - let y = FP2Element::new(UnsignedInteger::from( - "5f103b0bd4397d4df560eb559f38353f80eeb6", - )); - let c = FP2Element::new(UnsignedInteger::from( - "64fd5279bf47fe02d4185ce279d8aa55e00352", - )); - assert_eq!(x + y, c); - } - - #[test] - fn montgomery_prime_field_multiplication_works_1() { - let x = FP2Element::one(); - let y = FP2Element::new(UnsignedInteger::from( - "5f103b0bd4397d4df560eb559f38353f80eeb6", - )); - assert_eq!(&y * x, y); - } - - #[test] - fn to_bytes_from_bytes_be_is_the_identity() { - let x = FP2Element::new(UnsignedInteger::from( - "5f103b0bd4397d4df560eb559f38353f80eeb6", - )); - assert_eq!(FP2Element::from_bytes_be(&x.to_bytes_be()).unwrap(), x); - } - - #[test] - fn from_bytes_to_bytes_be_is_the_identity_for_one() { - let bytes = vec![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, - ]; - assert_eq!( - FP2Element::from_bytes_be(&bytes).unwrap().to_bytes_be(), - bytes - ); - } - - #[test] - fn to_bytes_from_bytes_le_is_the_identity() { - let x = FP2Element::new(UnsignedInteger::from( - "5f103b0bd4397d4df560eb559f38353f80eeb6", - )); - assert_eq!(FP2Element::from_bytes_le(&x.to_bytes_le()).unwrap(), x); - } - - #[test] - fn from_bytes_to_bytes_le_is_the_identity_for_one() { - let bytes = vec![ - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - assert_eq!( - FP2Element::from_bytes_le(&bytes).unwrap().to_bytes_le(), - bytes - ); - } - */ + + // FP2 + #[derive(Clone, Debug)] + struct MontgomeryConfigP2; + impl IsMontgomeryConfiguration<4> for MontgomeryConfigP2 { + const MODULUS: U256 = UnsignedInteger { + limbs: [ + 18446744073709551615, + 18446744073709551615, + 18446744073709551615, + 18446744073709551427, + ], + }; + const MP: u64 = 11907422100489763477; + const R2: U256 = UnsignedInteger { + limbs: [0, 0, 0, 35721], + }; + } + + type FP2 = U256PrimeField; + type FP2Element = FieldElement; + + #[test] + fn montgomery_prime_field_addition_works_1() { + let x = FP2Element::new(UnsignedInteger::from( + "acbbb7ca01c65cfffffc72815b397fff9ab130ad53a5ffffffb8f21b207dfedf", + )); + let y = FP2Element::new(UnsignedInteger::from( + "d65ddbe509d3fffff21f494c588cbdbfe43e929b0543e3ffffffffffffffff43", + )); + let c = FP2Element::new(UnsignedInteger::from( + "831993af0b9a5cfff21bbbcdb3c63dbf7eefc34858e9e3ffffb8f21b207dfedf", + )); + assert_eq!(x + y, c); + } + + #[test] + fn montgomery_prime_field_multiplication_works_1() { + let x = FP2Element::new(UnsignedInteger::from( + "acbbb7ca01c65cfffffc72815b397fff9ab130ad53a5ffffffb8f21b207dfedf", + )); + let y = FP2Element::new(UnsignedInteger::from( + "d65ddbe509d3fffff21f494c588cbdbfe43e929b0543e3ffffffffffffffff43", + )); + let c = FP2Element::new(UnsignedInteger::from( + "2b1e80d553ecab2e4d41eb53c4c8ad89ebacac6cf6b91dcf2213f311093aa05d", + )); + assert_eq!(&y * x, c); + } + + #[test] + fn to_bytes_from_bytes_be_is_the_identity() { + let x = FP2Element::new(UnsignedInteger::from( + "5f103b0bd4397d4df560eb559f38353f80eeb6", + )); + assert_eq!(FP2Element::from_bytes_be(&x.to_bytes_be()).unwrap(), x); + } + + #[test] + fn from_bytes_to_bytes_be_is_the_identity_for_one() { + let bytes = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, + ]; + assert_eq!( + FP2Element::from_bytes_be(&bytes).unwrap().to_bytes_be(), + bytes + ); + } + + #[test] + fn to_bytes_from_bytes_le_is_the_identity() { + let x = FP2Element::new(UnsignedInteger::from( + "5f103b0bd4397d4df560eb559f38353f80eeb6", + )); + assert_eq!(FP2Element::from_bytes_le(&x.to_bytes_le()).unwrap(), x); + } + + #[test] + fn from_bytes_to_bytes_le_is_the_identity_for_one() { + let bytes = vec![ + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; + assert_eq!( + FP2Element::from_bytes_le(&bytes).unwrap().to_bytes_le(), + bytes + ); + } } From 7205573d03dfc9956609d15545bc906ac6304b81 Mon Sep 17 00:00:00 2001 From: GianfrancoBazzani Date: Thu, 2 Mar 2023 18:21:27 +0100 Subject: [PATCH 09/12] Conflicts fixed --- .../fields/montgomery_backed_prime_fields.rs | 120 +++++++++++++----- proving-system/stark/src/lib.rs | 6 +- 2 files changed, 91 insertions(+), 35 deletions(-) diff --git a/math/src/field/fields/montgomery_backed_prime_fields.rs b/math/src/field/fields/montgomery_backed_prime_fields.rs index c7b3e313c..30181377c 100644 --- a/math/src/field/fields/montgomery_backed_prime_fields.rs +++ b/math/src/field/fields/montgomery_backed_prime_fields.rs @@ -10,14 +10,65 @@ use std::marker::PhantomData; pub type U384PrimeField = MontgomeryBackendPrimeField; pub type U256PrimeField = MontgomeryBackendPrimeField; + +/// Computes `- modulus^{-1} mod 2^{64}` +/// This algorithm is given by Dussé and Kaliski Jr. in +/// "S. R. Dussé and B. S. Kaliski Jr. A cryptographic library for the Motorola +/// DSP56000. In I. Damgård, editor, Advances in Cryptology – EUROCRYPT’90, +/// volume 473 of Lecture Notes in Computer Science, pages 230–244. Springer, +/// Heidelberg, May 1991." +const fn compute_mu_parameter( modulus: &UnsignedInteger) -> u64 { + let mut y = 1; + let word_size = 64; + let mut i: usize = 2; + while i <= word_size { + let (_, lo) = UnsignedInteger::mul(modulus, &UnsignedInteger::from_u64(y)); + let least_significant_limb = lo.limbs[NUM_LIMBS-1]; + if (least_significant_limb << (word_size - i)) >> (word_size - i) != 1 { + y += 1 << (i - 1); + } + i += 1; + } + y.wrapping_neg() +} + +/// Computes 2^{384 * 2} modulo `modulus` +const fn compute_r2_parameter(modulus: &UnsignedInteger) -> UnsignedInteger { + let word_size = 64; + let mut l: usize = 0; + let zero = UnsignedInteger::from_u64(0); + // Define `c` as the largest power of 2 smaller than `modulus` + while l < NUM_LIMBS * word_size { + if UnsignedInteger::const_ne(&modulus.const_shr(l), &zero) { + break; + } + l += 1; + } + let mut c = UnsignedInteger::from_u64(1).const_shl(l); + + // Double `c` and reduce modulo `modulus` until getting + // `2^{2 * number_limbs * word_size}` mod `modulus` + let mut i: usize = 1; + while i <= 2 * NUM_LIMBS * word_size - l { + let (double_c, overflow) = UnsignedInteger::add(&c, &c); + c = if UnsignedInteger::const_le(modulus, &double_c) || overflow { + UnsignedInteger::sub(&double_c, modulus).0 + } else { + double_c + }; + i += 1; + } + c +} + /// This trait is necessary for us to be able to use unsigned integer types bigger than /// `u128` (the biggest native `unit`) as constant generics. /// This trait should be removed when Rust supports this feature. pub trait IsMontgomeryConfiguration { const MODULUS: UnsignedInteger; - const R2: UnsignedInteger; - const MP: u64; + const R2: UnsignedInteger = compute_r2_parameter(&Self::MODULUS); + const MU: u64 = compute_mu_parameter(&Self::MODULUS); } #[derive(Clone, Debug)] @@ -53,7 +104,7 @@ where } fn mul(a: &Self::BaseType, b: &Self::BaseType) -> Self::BaseType { - MontgomeryAlgorithms::cios(a, b, &C::MODULUS, &C::MP) + MontgomeryAlgorithms::cios(a, b, &C::MODULUS, &C::MU) } fn sub(a: &Self::BaseType, b: &Self::BaseType) -> Self::BaseType { @@ -96,11 +147,16 @@ where } fn from_u64(x: u64) -> Self::BaseType { - MontgomeryAlgorithms::cios(&UnsignedInteger::from_u64(x), &C::R2, &C::MODULUS, &C::MP) + MontgomeryAlgorithms::cios(&UnsignedInteger::from_u64(x), &C::R2, &C::MODULUS, &C::MU) } fn from_base_type(x: Self::BaseType) -> Self::BaseType { - MontgomeryAlgorithms::cios(&x, &C::R2, &C::MODULUS, &C::MP) + MontgomeryAlgorithms::cios(&x, &C::R2, &C::MODULUS, &C::MU) + } + + // TO DO: Add tests for representatives + fn representative(x: Self::BaseType) -> Self::BaseType { + MontgomeryAlgorithms::cios(&x, &UnsignedInteger::from_u64(1), &C::MODULUS, &C::MU) } } @@ -114,7 +170,7 @@ where self.value(), &UnsignedInteger::from_u64(1), &C::MODULUS, - &C::MP, + &C::MU, ) .to_bytes_be() } @@ -124,7 +180,7 @@ where self.value(), &UnsignedInteger::from_u64(1), &C::MODULUS, - &C::MP, + &C::MU, ) .to_bytes_le() } @@ -154,8 +210,8 @@ mod tests_u384_prime_fields { struct U384MontgomeryConfiguration23; impl IsMontgomeryConfiguration<6> for U384MontgomeryConfiguration23 { const MODULUS: U384 = UnsignedInteger::from_u64(23); - const MP: u64 = 3208129404123400281; - const R2: U384 = UnsignedInteger::from_u64(6); + //const MP: u64 = 3208129404123400281; + //const R2: U384 = UnsignedInteger::from_u64(6); } type U384F23 = U384PrimeField; @@ -314,10 +370,10 @@ mod tests_u384_prime_fields { 15923941673896418529, ], }; - const MP: u64 = 16085280245840369887; - const R2: U384 = UnsignedInteger { - limbs: [0, 0, 0, 362264696, 173086217205162856, 7848132598488868435], - }; + //const MP: u64 = 16085280245840369887; + //const R2: U384 = UnsignedInteger { + // limbs: [0, 0, 0, 362264696, 173086217205162856, 7848132598488868435], + //}; } type U384FP1 = U384PrimeField; @@ -365,10 +421,10 @@ mod tests_u384_prime_fields { 18446744073709551275, ], }; - const MP: u64 = 14984598558409225213; - const R2: U384 = UnsignedInteger { - limbs: [0, 0, 0, 0, 0, 116281], - }; + //const MP: u64 = 14984598558409225213; + //const R2: U384 = UnsignedInteger { + // limbs: [0, 0, 0, 0, 0, 116281], + //}; } type U384FP2 = U384PrimeField; @@ -452,8 +508,8 @@ mod tests_u256_prime_fields { struct U256MontgomeryConfiguration29; impl IsMontgomeryConfiguration<4> for U256MontgomeryConfiguration29 { const MODULUS: U256 = UnsignedInteger::from_u64(29); - const MP: u64 = 14630176334321368523; - const R2: U256 = UnsignedInteger::from_u64(24); + //const MP: u64 = 14630176334321368523; + //const R2: U256 = UnsignedInteger::from_u64(24); } type U256F29 = U256PrimeField; @@ -610,15 +666,15 @@ mod tests_u256_prime_fields { 15723111795979912613, ], }; - const MP: u64 = 13870950301186631123; - const R2: U256 = UnsignedInteger { - limbs: [ - 3199, - 7378261192024362008, - 10023267389842517467, - 17706296746204571208, - ], - }; + //const MP: u64 = 13870950301186631123; + //const R2: U256 = UnsignedInteger { + // limbs: [ + // 3199, + // 7378261192024362008, + // 10023267389842517467, + // 17706296746204571208, + // ], + //}; } type U256FP1 = U256PrimeField; @@ -664,10 +720,10 @@ mod tests_u256_prime_fields { 18446744073709551427, ], }; - const MP: u64 = 11907422100489763477; - const R2: U256 = UnsignedInteger { - limbs: [0, 0, 0, 35721], - }; + //const MP: u64 = 11907422100489763477; + //const R2: U256 = UnsignedInteger { + // limbs: [0, 0, 0, 35721], + //}; } type FP2 = U256PrimeField; diff --git a/proving-system/stark/src/lib.rs b/proving-system/stark/src/lib.rs index dd0dea910..7b2f3f572 100644 --- a/proving-system/stark/src/lib.rs +++ b/proving-system/stark/src/lib.rs @@ -7,20 +7,20 @@ use lambdaworks_math::polynomial::{self, Polynomial}; use lambdaworks_math::field::element::FieldElement; use lambdaworks_math::{ - field::fields::u384_prime_field::{IsMontgomeryConfiguration, MontgomeryBackendPrimeField}, + field::fields::montgomery_backed_prime_fields::{IsMontgomeryConfiguration, U384PrimeField}, unsigned_integer::element::U384, }; // DEFINITION OF THE USED FIELD #[derive(Clone, Debug)] pub struct MontgomeryConfig; -impl IsMontgomeryConfiguration for MontgomeryConfig { +impl IsMontgomeryConfiguration<6> for MontgomeryConfig { const MODULUS: U384 = // hex 17 U384::from("800000000000011000000000000000000000000000000000000000000000001"); } -pub type PrimeField = MontgomeryBackendPrimeField; +pub type PrimeField = U384PrimeField; pub type FE = FieldElement; const MODULUS_MINUS_1: U384 = U384::sub(&MontgomeryConfig::MODULUS, &U384::from("1")).0; From d4a89695dec8cb1a0ec0ce787136ec5431dd4675 Mon Sep 17 00:00:00 2001 From: GianfrancoBazzani Date: Thu, 2 Mar 2023 18:22:05 +0100 Subject: [PATCH 10/12] fmt --- .../field/fields/montgomery_backed_prime_fields.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/math/src/field/fields/montgomery_backed_prime_fields.rs b/math/src/field/fields/montgomery_backed_prime_fields.rs index 30181377c..8c5583bed 100644 --- a/math/src/field/fields/montgomery_backed_prime_fields.rs +++ b/math/src/field/fields/montgomery_backed_prime_fields.rs @@ -10,20 +10,19 @@ use std::marker::PhantomData; pub type U384PrimeField = MontgomeryBackendPrimeField; pub type U256PrimeField = MontgomeryBackendPrimeField; - /// Computes `- modulus^{-1} mod 2^{64}` /// This algorithm is given by Dussé and Kaliski Jr. in /// "S. R. Dussé and B. S. Kaliski Jr. A cryptographic library for the Motorola /// DSP56000. In I. Damgård, editor, Advances in Cryptology – EUROCRYPT’90, /// volume 473 of Lecture Notes in Computer Science, pages 230–244. Springer, /// Heidelberg, May 1991." -const fn compute_mu_parameter( modulus: &UnsignedInteger) -> u64 { +const fn compute_mu_parameter(modulus: &UnsignedInteger) -> u64 { let mut y = 1; let word_size = 64; let mut i: usize = 2; while i <= word_size { let (_, lo) = UnsignedInteger::mul(modulus, &UnsignedInteger::from_u64(y)); - let least_significant_limb = lo.limbs[NUM_LIMBS-1]; + let least_significant_limb = lo.limbs[NUM_LIMBS - 1]; if (least_significant_limb << (word_size - i)) >> (word_size - i) != 1 { y += 1 << (i - 1); } @@ -33,7 +32,9 @@ const fn compute_mu_parameter( modulus: &UnsignedInteger } /// Computes 2^{384 * 2} modulo `modulus` -const fn compute_r2_parameter(modulus: &UnsignedInteger) -> UnsignedInteger { +const fn compute_r2_parameter( + modulus: &UnsignedInteger, +) -> UnsignedInteger { let word_size = 64; let mut l: usize = 0; let zero = UnsignedInteger::from_u64(0); @@ -789,7 +790,7 @@ mod tests_u256_prime_fields { fn from_bytes_to_bytes_le_is_the_identity_for_one() { let bytes = vec![ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, + 0, 0, 0, ]; assert_eq!( FP2Element::from_bytes_le(&bytes).unwrap().to_bytes_le(), From b795a20e9b71beb17cde886300605789af1444b3 Mon Sep 17 00:00:00 2001 From: GianfrancoBazzani Date: Thu, 2 Mar 2023 18:32:08 +0100 Subject: [PATCH 11/12] Get rid of unused commented lines --- .../fields/montgomery_backed_prime_fields.rs | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/math/src/field/fields/montgomery_backed_prime_fields.rs b/math/src/field/fields/montgomery_backed_prime_fields.rs index 8c5583bed..9e4940e7c 100644 --- a/math/src/field/fields/montgomery_backed_prime_fields.rs +++ b/math/src/field/fields/montgomery_backed_prime_fields.rs @@ -371,10 +371,6 @@ mod tests_u384_prime_fields { 15923941673896418529, ], }; - //const MP: u64 = 16085280245840369887; - //const R2: U384 = UnsignedInteger { - // limbs: [0, 0, 0, 362264696, 173086217205162856, 7848132598488868435], - //}; } type U384FP1 = U384PrimeField; @@ -422,10 +418,6 @@ mod tests_u384_prime_fields { 18446744073709551275, ], }; - //const MP: u64 = 14984598558409225213; - //const R2: U384 = UnsignedInteger { - // limbs: [0, 0, 0, 0, 0, 116281], - //}; } type U384FP2 = U384PrimeField; @@ -667,15 +659,6 @@ mod tests_u256_prime_fields { 15723111795979912613, ], }; - //const MP: u64 = 13870950301186631123; - //const R2: U256 = UnsignedInteger { - // limbs: [ - // 3199, - // 7378261192024362008, - // 10023267389842517467, - // 17706296746204571208, - // ], - //}; } type U256FP1 = U256PrimeField; @@ -721,10 +704,6 @@ mod tests_u256_prime_fields { 18446744073709551427, ], }; - //const MP: u64 = 11907422100489763477; - //const R2: U256 = UnsignedInteger { - // limbs: [0, 0, 0, 35721], - //}; } type FP2 = U256PrimeField; From 70984d32984c499fd47890214adcca86d37fe463 Mon Sep 17 00:00:00 2001 From: GianfrancoBazzani Date: Thu, 2 Mar 2023 18:33:11 +0100 Subject: [PATCH 12/12] Update montgomery_backed_prime_fields.rs --- math/src/field/fields/montgomery_backed_prime_fields.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/math/src/field/fields/montgomery_backed_prime_fields.rs b/math/src/field/fields/montgomery_backed_prime_fields.rs index 9e4940e7c..16f1d8ee7 100644 --- a/math/src/field/fields/montgomery_backed_prime_fields.rs +++ b/math/src/field/fields/montgomery_backed_prime_fields.rs @@ -211,8 +211,6 @@ mod tests_u384_prime_fields { struct U384MontgomeryConfiguration23; impl IsMontgomeryConfiguration<6> for U384MontgomeryConfiguration23 { const MODULUS: U384 = UnsignedInteger::from_u64(23); - //const MP: u64 = 3208129404123400281; - //const R2: U384 = UnsignedInteger::from_u64(6); } type U384F23 = U384PrimeField; @@ -501,8 +499,6 @@ mod tests_u256_prime_fields { struct U256MontgomeryConfiguration29; impl IsMontgomeryConfiguration<4> for U256MontgomeryConfiguration29 { const MODULUS: U256 = UnsignedInteger::from_u64(29); - //const MP: u64 = 14630176334321368523; - //const R2: U256 = UnsignedInteger::from_u64(24); } type U256F29 = U256PrimeField;