From aeda766c3495f79808c736be2dba0963a8a41c26 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 20 Sep 2021 16:46:54 +0100 Subject: [PATCH] Remove `FieldExt::ROOT_OF_UNITY` We can use the `ff::PrimeField::root_of_unity` method everywhere we currently use this associated constant. If there is a more general need for accessing this as an associated constant, we should consider that for `ff::PrimeField`. --- CHANGELOG.md | 3 +++ src/arithmetic/fields.rs | 11 ++++------- src/curves.rs | 4 ++-- src/fields/fp.rs | 9 ++++----- src/fields/fq.rs | 9 ++++----- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cde56d0..36a0d3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to Rust's notion of [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Removed +- `pasta_curves::arithmetic`: + - `FieldExt::ROOT_OF_UNITY` (use `ff::PrimeField::root_of_unity` instead). ## [0.2.1] - 2021-09-17 ### Changed diff --git a/src/arithmetic/fields.rs b/src/arithmetic/fields.rs index 53554fc..cb1befe 100644 --- a/src/arithmetic/fields.rs +++ b/src/arithmetic/fields.rs @@ -28,10 +28,7 @@ pub trait FieldExt: ff::PrimeField + From + Ord + Group { /// Modulus of the field written as a string for display purposes const MODULUS: &'static str; - /// Generator of the $2^S$ multiplicative subgroup - const ROOT_OF_UNITY: Self; - - /// Inverse of `ROOT_OF_UNITY` + /// Inverse of `PrimeField::root_of_unity()` const ROOT_OF_UNITY_INV: Self; /// The value $(T-1)/2$ such that $2^S \cdot T = p - 1$ with $T$ odd. @@ -233,7 +230,7 @@ impl SqrtTables { marker: PhantomData, }; - let mut gtab = (0..4).scan(F::ROOT_OF_UNITY, |gi, _| { + let mut gtab = (0..4).scan(F::root_of_unity(), |gi, _| { // gi == ROOT_OF_UNITY^(256^i) let gtab_i: Vec = (0..256) .scan(F::one(), |acc, _| { @@ -331,7 +328,7 @@ impl SqrtTables { let sqdiv = res.square() * div; let is_square = (sqdiv - num).is_zero(); - let is_nonsquare = (sqdiv - F::ROOT_OF_UNITY * num).is_zero(); + let is_nonsquare = (sqdiv - F::root_of_unity() * num).is_zero(); assert!(bool::from( num.is_zero() | div.is_zero() | (is_square ^ is_nonsquare) )); @@ -348,7 +345,7 @@ impl SqrtTables { let sq = res.square(); let is_square = (sq - u).is_zero(); - let is_nonsquare = (sq - F::ROOT_OF_UNITY * u).is_zero(); + let is_nonsquare = (sq - F::root_of_unity() * u).is_zero(); assert!(bool::from(u.is_zero() | (is_square ^ is_nonsquare))); (is_square, res) diff --git a/src/curves.rs b/src/curves.rs index d402de5..8661033 100644 --- a/src/curves.rs +++ b/src/curves.rs @@ -1100,7 +1100,7 @@ impl Ep { 0x4000000000000000, ]); - /// `(F::ROOT_OF_UNITY.invert().unwrap() * z).sqrt().unwrap()` + /// `(F::root_of_unity().invert().unwrap() * z).sqrt().unwrap()` pub const THETA: Fp = Fp::from_raw([ 0xca330bcc09ac318e, 0x51f64fc4dc888857, @@ -1200,7 +1200,7 @@ impl Eq { 0x4000000000000000, ]); - /// `(F::ROOT_OF_UNITY.invert().unwrap() * z).sqrt().unwrap()` + /// `(F::root_of_unity().invert().unwrap() * z).sqrt().unwrap()` pub const THETA: Fq = Fq::from_raw([ 0x632cae9872df1b5d, 0x38578ccadf03ac27, diff --git a/src/fields/fp.rs b/src/fields/fp.rs index 0650751..6097f94 100644 --- a/src/fields/fp.rs +++ b/src/fields/fp.rs @@ -673,7 +673,6 @@ lazy_static! { impl FieldExt for Fp { const MODULUS: &'static str = "0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001"; - const ROOT_OF_UNITY: Self = ROOT_OF_UNITY; const ROOT_OF_UNITY_INV: Self = Fp::from_raw([ 0xf0b87c7db2ce91f6, 0x84a0a1d8859f066f, @@ -854,8 +853,8 @@ fn test_sqrt_ratio_and_alt() { assert!(v_alt == v); // (false, sqrt(ROOT_OF_UNITY * num/div)), if num and div are nonzero and num/div is a nonsquare in the field - let num = num * Fp::ROOT_OF_UNITY; - let expected = Fp::TWO_INV * Fp::ROOT_OF_UNITY * Fp::from_u64(5).invert().unwrap(); + let num = num * Fp::root_of_unity(); + let expected = Fp::TWO_INV * Fp::root_of_unity() * Fp::from_u64(5).invert().unwrap(); let (is_square, v) = Fp::sqrt_ratio(&num, &div); assert!(!bool::from(is_square)); assert!(v == expected || (-v) == expected); @@ -904,7 +903,7 @@ fn test_zeta() { #[test] fn test_root_of_unity() { assert_eq!( - Fp::ROOT_OF_UNITY.pow_vartime(&[1 << Fp::S, 0, 0, 0]), + Fp::root_of_unity().pow_vartime(&[1 << Fp::S, 0, 0, 0]), Fp::one() ); } @@ -912,7 +911,7 @@ fn test_root_of_unity() { #[cfg(feature = "std")] #[test] fn test_inv_root_of_unity() { - assert_eq!(Fp::ROOT_OF_UNITY_INV, Fp::ROOT_OF_UNITY.invert().unwrap()); + assert_eq!(Fp::ROOT_OF_UNITY_INV, Fp::root_of_unity().invert().unwrap()); } #[cfg(feature = "std")] diff --git a/src/fields/fq.rs b/src/fields/fq.rs index 1a137fc..d72ac36 100644 --- a/src/fields/fq.rs +++ b/src/fields/fq.rs @@ -673,7 +673,6 @@ lazy_static! { impl FieldExt for Fq { const MODULUS: &'static str = "0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001"; - const ROOT_OF_UNITY: Self = ROOT_OF_UNITY; const ROOT_OF_UNITY_INV: Self = Fq::from_raw([ 0x57eecda0a84b6836, 0x4ad38b9084b8a80c, @@ -854,8 +853,8 @@ fn test_sqrt_ratio_and_alt() { assert!(v_alt == v); // (false, sqrt(ROOT_OF_UNITY * num/div)), if num and div are nonzero and num/div is a nonsquare in the field - let num = num * Fq::ROOT_OF_UNITY; - let expected = Fq::TWO_INV * Fq::ROOT_OF_UNITY * Fq::from_u64(5).invert().unwrap(); + let num = num * Fq::root_of_unity(); + let expected = Fq::TWO_INV * Fq::root_of_unity() * Fq::from_u64(5).invert().unwrap(); let (is_square, v) = Fq::sqrt_ratio(&num, &div); assert!(!bool::from(is_square)); assert!(v == expected || (-v) == expected); @@ -903,7 +902,7 @@ fn test_zeta() { #[test] fn test_root_of_unity() { assert_eq!( - Fq::ROOT_OF_UNITY.pow_vartime(&[1 << Fq::S, 0, 0, 0]), + Fq::root_of_unity().pow_vartime(&[1 << Fq::S, 0, 0, 0]), Fq::one() ); } @@ -911,7 +910,7 @@ fn test_root_of_unity() { #[cfg(feature = "std")] #[test] fn test_inv_root_of_unity() { - assert_eq!(Fq::ROOT_OF_UNITY_INV, Fq::ROOT_OF_UNITY.invert().unwrap()); + assert_eq!(Fq::ROOT_OF_UNITY_INV, Fq::root_of_unity().invert().unwrap()); } #[cfg(feature = "std")]