mirror of
https://github.com/saymrwulf/pasta_curves-source.git
synced 2026-09-03 19:53:40 +00:00
Move FieldExt::get_lower_32 to SqrtRatio trait
It is only used internally by the table-based square root impl, and we should probably refactor this further, but for now it can live in the sqrt extension trait.
This commit is contained in:
parent
3a6f71d2f0
commit
32cc10db46
5 changed files with 21 additions and 25 deletions
|
|
@ -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<u64> for ff::PrimeField` instead).
|
||||
- `FieldExt::{from_bytes, read, to_bytes, write}`
|
||||
|
|
|
|||
|
|
@ -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<bool> + Ord + Group<Scalar = Self> {
|
|||
/// 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`.
|
||||
|
|
|
|||
|
|
@ -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"))]
|
||||
|
|
|
|||
|
|
@ -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"))]
|
||||
|
|
|
|||
|
|
@ -171,11 +171,7 @@ pub fn map_to_curve_simple_swu<F: FieldExt, C: CurveExt<Base = F>, I: CurveExt<B
|
|||
let y = F::conditional_select(&y2, &y1, gx1_square);
|
||||
|
||||
// 9. If sgn0(u) != sgn0(y), set y = -y
|
||||
let y = F::conditional_select(
|
||||
&(-y),
|
||||
&y,
|
||||
(u.get_lower_32() % 2).ct_eq(&(y.get_lower_32() % 2)),
|
||||
);
|
||||
let y = F::conditional_select(&(-y), &y, u.is_odd().ct_eq(&y.is_odd()));
|
||||
|
||||
I::new_jacobian(num_x * div, y * div3, div).unwrap()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue