From ba1505cdd350a26a850295472ea6eb1ecd5fb089 Mon Sep 17 00:00:00 2001 From: lonerapier Date: Thu, 9 May 2024 13:40:34 +0530 Subject: [PATCH 1/2] fix mul, generator and add hardcoded tests --- math/field.sage | 7 +++--- src/field/gf_101_2.rs | 50 +++++++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/math/field.sage b/math/field.sage index 3cdd83e4..ad696947 100644 --- a/math/field.sage +++ b/math/field.sage @@ -54,8 +54,8 @@ F_2 = GF(101 ^ 2, name="t", modulus=P) print("extension field:", F_2, "of order:", F_2.order()) # Primitive element -f_2_primitive_element = F_2.primitive_element() -print("Primitive element of F_2:", f_2_primitive_element, f_2_primitive_element.order()) +f_2_primitive_element = F_2([2, 1]) +print("Primitive element of F_2:", f_2_primitive_element, f_2_primitive_element.multiplicative_order()) # 100th root of unity F_2_order = F_2.order() @@ -65,5 +65,4 @@ quotient = (F_2_order-1)//root_of_unity_order f_2_omega_n = f_2_primitive_element ^ quotient print("The", root_of_unity_order, "th root of unity of extension field is: ", f_2_omega_n) -###################################################################### - +###################################################################### \ No newline at end of file diff --git a/src/field/gf_101_2.rs b/src/field/gf_101_2.rs index ea8bb893..c6ecd16c 100644 --- a/src/field/gf_101_2.rs +++ b/src/field/gf_101_2.rs @@ -30,11 +30,9 @@ impl QuadraticPlutoField { /// F[X]/(X^2-2) fn irreducible() -> F { F::from_canonical_u32(2) } - // const fn from_base(b: F) -> Self { Self { value: [b, F::zero()] } } + // const fn from_base(b: F) -> Self { Self { value: [b, F::zero()] } } - pub const fn new(a: F, b: F) -> Self { - Self { value: [a, b] } - } + pub const fn new(a: F, b: F) -> Self { Self { value: [a, b] } } } impl ExtensionField for QuadraticPlutoField { @@ -52,12 +50,13 @@ impl From for QuadraticPlutoField { impl FiniteField for QuadraticPlutoField { type Storage = u32; + const NEG_ONE: Self = Self::new(F::NEG_ONE, F::ZERO); + // TODO: This is wrong + const ONE: Self = Self::new(F::ONE, F::ZERO); const ORDER: Self::Storage = QUADRATIC_EXTENSION_FIELD_ORDER; - // fn zero() -> Self { Self { value: [F::zero(); EXT_DEGREE] } } - const ZERO: Self = Self::new(F::ZERO, F::TWO); // TODO: This is wrong - const ONE: Self = Self::new(F::ONE, F::ZERO); const TWO: Self = Self::new(F::TWO, F::ZERO); - const NEG_ONE: Self = Self::new(F::NEG_ONE, F::ZERO); + // fn zero() -> Self { Self { value: [F::zero(); EXT_DEGREE] } } + const ZERO: Self = Self::new(F::ZERO, F::ZERO); // field generator: can be verified using sage script // ```sage @@ -65,11 +64,12 @@ impl FiniteField for QuadraticPlutoField { // Ft. = F[] // P = Ft(t ^ 2 - 2) // F_2 = GF(101 ^ 2, name="t", modulus=P) - // f_2_primitive_element = F_2.primitive_element() + // f_2_primitive_element = F_2([2, 1]) + // assert f_2_primitive_element.multiplicative_order() == 101^2-1 // ``` fn generator() -> Self { // TODO: unsure if this is correct or not, research more - Self { value: [F::from_canonical_u32(15), F::from_canonical_u32(20)] } + Self { value: [F::from_canonical_u32(2), F::from_canonical_u32(1)] } } /// Computes the multiplicative inverse of `a`, i.e. 1 / (a0 + a1 * t). @@ -176,7 +176,7 @@ impl Mul for QuadraticPlutoField { let a = self.value; let b = rhs.value; let mut res = Self::default(); - res.value[0] = a[0].clone() * b[0].clone() + a[1].clone() * a[1].clone() * Self::irreducible(); + res.value[0] = a[0].clone() * b[0].clone() + a[1].clone() * b[1].clone() * Self::irreducible(); res.value[1] = a[0].clone() * b[1].clone() + a[1].clone() * b[0].clone(); res } @@ -251,6 +251,27 @@ mod tests { assert_eq!(x_2.value[1], F::new(0)); } + #[test] + fn test_add() { + let a = F2::new(F::new(10), F::new(20)); + let b = F2::new(F::new(20), F::new(10)); + assert_eq!(a + b, F2::new(F::new(30), F::new(30))); + } + + #[test] + fn test_sub() { + let a = F2::new(F::new(10), F::new(20)); + let b = F2::new(F::new(20), F::new(10)); + assert_eq!(a - b, F2::new(F::new(91), F::new(10))); + } + + #[test] + fn test_mul() { + let a = F2::new(F::new(10), F::new(20)); + let b = F2::new(F::new(20), F::new(10)); + assert_eq!(a * b, F2::new(F::new(95), F::new(96))); + } + #[test] fn test_add_sub_neg_mul() { let mut rng = rand::thread_rng(); @@ -289,10 +310,7 @@ mod tests { let z = F2::from_base(rng.gen::()); assert_eq!(x * x.inverse().unwrap(), F2::ONE); assert_eq!(x.inverse().unwrap_or(F2::ONE) * x, F2::ONE); - assert_eq!( - x.square().inverse().unwrap_or(F2::ONE), - x.inverse().unwrap_or(F2::ONE).square() - ); + assert_eq!(x.square().inverse().unwrap_or(F2::ONE), x.inverse().unwrap_or(F2::ONE).square()); assert_eq!((x / y) * y, x); assert_eq!(x / (y * z), (x / y) / z); assert_eq!((x * y) / z, x * (y / z)); @@ -322,7 +340,7 @@ mod tests { #[test] fn test_generator_order() { - let generator = F2::generator(); + let generator = F2::ONE; let mut x = generator; for _ in 1..F2::ORDER { x *= generator; From 13c59bc77c901f25401922544284d358173db8c2 Mon Sep 17 00:00:00 2001 From: lonerapier Date: Thu, 9 May 2024 21:15:51 +0530 Subject: [PATCH 2/2] fix generator test --- src/field/gf_101_2.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/field/gf_101_2.rs b/src/field/gf_101_2.rs index c6ecd16c..afc32d35 100644 --- a/src/field/gf_101_2.rs +++ b/src/field/gf_101_2.rs @@ -67,10 +67,7 @@ impl FiniteField for QuadraticPlutoField { // f_2_primitive_element = F_2([2, 1]) // assert f_2_primitive_element.multiplicative_order() == 101^2-1 // ``` - fn generator() -> Self { - // TODO: unsure if this is correct or not, research more - Self { value: [F::from_canonical_u32(2), F::from_canonical_u32(1)] } - } + fn generator() -> Self { Self { value: [F::from_canonical_u32(2), F::from_canonical_u32(1)] } } /// Computes the multiplicative inverse of `a`, i.e. 1 / (a0 + a1 * t). /// Multiply by `a0 - a1 * t` in numerator and denominator. @@ -340,8 +337,8 @@ mod tests { #[test] fn test_generator_order() { - let generator = F2::ONE; - let mut x = generator; + let generator = F2::generator(); + let mut x = F2::ONE; for _ in 1..F2::ORDER { x *= generator; }