diff --git a/CHANGELOG.md b/CHANGELOG.md index 81b50f7..d5df23a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,8 +15,8 @@ and this project adheres to Rust's notion of - `pasta_curves::arithmetic`: - `Field` re-export (`pasta_curves::group::ff::Field` is equivalent). - `FieldExt::ROOT_OF_UNITY` (use `ff::PrimeField::root_of_unity` instead). - - `FieldExt::{T_MINUS1_OVER2, pow_by_t_minus1_over2, sqrt_alt, sqrt_ratio}` - (moved to `SqrtRatio` trait). + - `FieldExt::{T_MINUS1_OVER2, pow_by_t_minus1_over2, get_lower_32, sqrt_alt,` + `sqrt_ratio}` (moved to `SqrtRatio` trait). - `FieldExt::{RESCUE_ALPHA, RESCUE_INVALPHA}` - `FieldExt::from_u64` (use `From for ff::PrimeField` instead). - `FieldExt::{from_bytes, read, to_bytes, write}` diff --git a/src/arithmetic/fields.rs b/src/arithmetic/fields.rs index fbe0498..485a1cf 100644 --- a/src/arithmetic/fields.rs +++ b/src/arithmetic/fields.rs @@ -31,6 +31,10 @@ pub trait SqrtRatio: ff::PrimeField { ff::Field::pow_vartime(&self, &Self::T_MINUS1_OVER2) } + /// Gets the lower 32 bits of this field element when expressed + /// canonically. + fn get_lower_32(&self) -> u32; + /// Computes: /// /// - $(\textsf{true}, \sqrt{\textsf{num}/\textsf{div}})$, if $\textsf{num}$ and @@ -103,10 +107,6 @@ pub trait FieldExt: SqrtRatio + From + Ord + Group { /// Gets the lower 128 bits of this field element when expressed /// canonically. fn get_lower_128(&self) -> u128; - - /// Gets the lower 32 bits of this field element when expressed - /// canonically. - fn get_lower_32(&self) -> u32; } /// Tonelli–Shanks' square-root algorithm for `p mod 16 = 1`. diff --git a/src/fields/fp.rs b/src/fields/fp.rs index 0a5ae71..d27928f 100644 --- a/src/fields/fp.rs +++ b/src/fields/fp.rs @@ -704,6 +704,13 @@ impl SqrtRatio for Fp { rs.square() // rt } + fn get_lower_32(&self) -> u32 { + // TODO: don't reduce, just hash the Montgomery form. (Requires rebuilding perfect hash table.) + let tmp = Fp::montgomery_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0); + + tmp.0[0] as u32 + } + fn sqrt_ratio(num: &Self, div: &Self) -> (Choice, Self) { FP_TABLES.sqrt_ratio(num, div) } @@ -761,13 +768,6 @@ impl FieldExt for Fp { u128::from(tmp.0[0]) | (u128::from(tmp.0[1]) << 64) } - - fn get_lower_32(&self) -> u32 { - // TODO: don't reduce, just hash the Montgomery form. (Requires rebuilding perfect hash table.) - let tmp = Fp::montgomery_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0); - - tmp.0[0] as u32 - } } #[cfg(all(test, feature = "std"))] diff --git a/src/fields/fq.rs b/src/fields/fq.rs index 567b99a..f7111be 100644 --- a/src/fields/fq.rs +++ b/src/fields/fq.rs @@ -704,6 +704,13 @@ impl SqrtRatio for Fq { sqr(ss, 4) // st } + fn get_lower_32(&self) -> u32 { + // TODO: don't reduce, just hash the Montgomery form. (Requires rebuilding perfect hash table.) + let tmp = Fq::montgomery_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0); + + tmp.0[0] as u32 + } + fn sqrt_ratio(num: &Self, div: &Self) -> (Choice, Self) { FQ_TABLES.sqrt_ratio(num, div) } @@ -761,13 +768,6 @@ impl FieldExt for Fq { u128::from(tmp.0[0]) | (u128::from(tmp.0[1]) << 64) } - - fn get_lower_32(&self) -> u32 { - // TODO: don't reduce, just hash the Montgomery form. (Requires rebuilding perfect hash table.) - let tmp = Fq::montgomery_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0); - - tmp.0[0] as u32 - } } #[cfg(all(test, feature = "std"))] diff --git a/src/hashtocurve.rs b/src/hashtocurve.rs index e09f3e7..3661265 100644 --- a/src/hashtocurve.rs +++ b/src/hashtocurve.rs @@ -171,11 +171,7 @@ pub fn map_to_curve_simple_swu, I: CurveExt