From 055916395346af5e75a26c8f6890a5f43b404fa9 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 24 Nov 2022 09:15:48 +0000 Subject: [PATCH 1/7] Remove unused bounds and methods from `FieldExt` --- src/arithmetic/fields.rs | 6 +----- src/fields/fp.rs | 6 ------ src/fields/fq.rs | 6 ------ 3 files changed, 1 insertion(+), 17 deletions(-) diff --git a/src/arithmetic/fields.rs b/src/arithmetic/fields.rs index 6d638c9..46b9a85 100644 --- a/src/arithmetic/fields.rs +++ b/src/arithmetic/fields.rs @@ -32,7 +32,7 @@ pub(crate) trait SqrtTableHelpers: ff::PrimeField { /// This trait is a common interface for dealing with elements of a finite /// field. -pub trait FieldExt: ff::PrimeField + From + Ord + Group { +pub trait FieldExt: ff::PrimeField + Ord + Group { /// Modulus of the field written as a string for display purposes const MODULUS: &'static str; @@ -54,10 +54,6 @@ pub trait FieldExt: ff::PrimeField + From + Ord + Group { /// Obtains a field element that is congruent to the provided little endian /// byte representation of an integer. fn from_bytes_wide(bytes: &[u8; 64]) -> Self; - - /// Gets the lower 128 bits of this field element when expressed - /// canonically. - fn get_lower_128(&self) -> u128; } /// Parameters for a perfect hash function used in square root computation. diff --git a/src/fields/fp.rs b/src/fields/fp.rs index f781cff..b505c99 100644 --- a/src/fields/fp.rs +++ b/src/fields/fp.rs @@ -767,12 +767,6 @@ impl FieldExt for Fp { u64::from_le_bytes(bytes[56..64].try_into().unwrap()), ]) } - - fn get_lower_128(&self) -> u128 { - let tmp = Fp::montgomery_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0); - - u128::from(tmp.0[0]) | (u128::from(tmp.0[1]) << 64) - } } #[cfg(feature = "gpu")] diff --git a/src/fields/fq.rs b/src/fields/fq.rs index 2046530..8f70cb5 100644 --- a/src/fields/fq.rs +++ b/src/fields/fq.rs @@ -766,12 +766,6 @@ impl FieldExt for Fq { u64::from_le_bytes(bytes[56..64].try_into().unwrap()), ]) } - - fn get_lower_128(&self) -> u128 { - let tmp = Fq::montgomery_reduce(self.0[0], self.0[1], self.0[2], self.0[3], 0, 0, 0, 0); - - u128::from(tmp.0[0]) | (u128::from(tmp.0[1]) << 64) - } } #[cfg(feature = "gpu")] From 4c86de5e1007e2ad32c28351432a5ca0a0fb3b10 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 24 Nov 2022 09:33:21 +0000 Subject: [PATCH 2/7] Remove `FieldExt` bound from `SqrtHasher` generic argument --- src/arithmetic/fields.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/arithmetic/fields.rs b/src/arithmetic/fields.rs index 46b9a85..52ec2b8 100644 --- a/src/arithmetic/fields.rs +++ b/src/arithmetic/fields.rs @@ -60,14 +60,14 @@ pub trait FieldExt: ff::PrimeField + Ord + Group { #[cfg(feature = "sqrt-table")] #[cfg_attr(docsrs, doc(cfg(feature = "sqrt-table")))] #[derive(Debug)] -struct SqrtHasher { +struct SqrtHasher { hash_xor: u32, hash_mod: usize, marker: PhantomData, } #[cfg(feature = "sqrt-table")] -impl SqrtHasher { +impl SqrtHasher { /// Returns a perfect hash of x for use with SqrtTables::inv. fn hash(&self, x: &F) -> usize { // This is just the simplest constant-time perfect hash construction that could @@ -82,7 +82,7 @@ impl SqrtHasher { #[cfg(feature = "sqrt-table")] #[cfg_attr(docsrs, doc(cfg(feature = "sqrt-table")))] #[derive(Debug)] -pub(crate) struct SqrtTables { +pub(crate) struct SqrtTables { hasher: SqrtHasher, inv: Vec, g0: Box<[F; 256]>, @@ -92,7 +92,7 @@ pub(crate) struct SqrtTables { } #[cfg(feature = "sqrt-table")] -impl SqrtTables { +impl SqrtTables { /// Build tables given parameters for the perfect hash. pub fn new(hash_xor: u32, hash_mod: usize) -> Self { use alloc::vec; From fbce21598d9c53ec7645b69f6b32ef714ae865cf Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 24 Nov 2022 09:41:35 +0000 Subject: [PATCH 3/7] Remove `crate::arithmetic::Group` trait It was only present to enable an FFT implementation in `halo2_proofs` that is generic over fields and groups, but we can replace it with an equivalent trait in `halo2_proofs` that can have a blanket impl. --- CHANGELOG.md | 8 ++++---- src/arithmetic.rs | 21 --------------------- src/arithmetic/curves.rs | 3 +-- src/arithmetic/fields.rs | 4 +--- src/curves.rs | 18 ------------------ src/fields/fp.rs | 19 +------------------ src/fields/fq.rs | 19 +------------------ 7 files changed, 8 insertions(+), 84 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a561cc1..fe33e88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,10 +10,10 @@ and this project adheres to Rust's notion of - Migrated to `ff 0.13`, `group 0.13`. ### Removed -- `pasta_curves::arithmetic::SqrtRatio` (use `ff::Field::{sqrt_ratio, sqrt_alt}` - instead). -- `pasta_curves::arithmetic::SqrtTables` (from public API, as it isn't suitable - for generic usage). +- `pasta_curves::arithmetic`: + - `Group` + - `SqrtRatio` (use `ff::Field::{sqrt_ratio, sqrt_alt}` instead). + - `SqrtTables` (from public API, as it isn't suitable for generic usage). ## [0.4.1] - 2022-10-13 ### Added diff --git a/src/arithmetic.rs b/src/arithmetic.rs index 0de6600..39613a6 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -9,24 +9,3 @@ mod fields; pub use curves::*; pub use fields::*; - -/// This represents an element of a group with basic operations that can be -/// performed. This allows an FFT implementation (for example) to operate -/// generically over either a field or elliptic curve group. -pub trait Group: Copy + Clone + Send + Sync + 'static { - /// The group is assumed to be of prime order $p$. `Scalar` is the - /// associated scalar field of size $p$. - type Scalar: FieldExt; - - /// Returns the additive identity of the group. - fn group_zero() -> Self; - - /// Adds `rhs` to this group element. - fn group_add(&mut self, rhs: &Self); - - /// Subtracts `rhs` from this group element. - fn group_sub(&mut self, rhs: &Self); - - /// Scales this group element by a scalar. - fn group_scale(&mut self, by: &Self::Scalar); -} diff --git a/src/arithmetic/curves.rs b/src/arithmetic/curves.rs index 44eced6..f9b2ef4 100644 --- a/src/arithmetic/curves.rs +++ b/src/arithmetic/curves.rs @@ -7,7 +7,7 @@ use group::prime::{PrimeCurve, PrimeCurveAffine}; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; #[cfg(feature = "alloc")] -use super::{FieldExt, Group}; +use super::FieldExt; #[cfg(feature = "alloc")] use alloc::boxed::Box; @@ -28,7 +28,6 @@ pub trait CurveExt: + ConditionallySelectable + ConstantTimeEq + From<::Affine> - + Group::Scalar> { /// The scalar field of this elliptic curve. type ScalarExt: FieldExt; diff --git a/src/arithmetic/fields.rs b/src/arithmetic/fields.rs index 52ec2b8..e80d6b5 100644 --- a/src/arithmetic/fields.rs +++ b/src/arithmetic/fields.rs @@ -5,8 +5,6 @@ use core::mem::size_of; use static_assertions::const_assert; -use super::Group; - #[cfg(feature = "sqrt-table")] use alloc::{boxed::Box, vec::Vec}; #[cfg(feature = "sqrt-table")] @@ -32,7 +30,7 @@ pub(crate) trait SqrtTableHelpers: ff::PrimeField { /// This trait is a common interface for dealing with elements of a finite /// field. -pub trait FieldExt: ff::PrimeField + Ord + Group { +pub trait FieldExt: ff::PrimeField + Ord { /// Modulus of the field written as a string for display purposes const MODULUS: &'static str; diff --git a/src/curves.rs b/src/curves.rs index 52712f3..45ab4c1 100644 --- a/src/curves.rs +++ b/src/curves.rs @@ -19,7 +19,6 @@ use rand::RngCore; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; use super::{Fp, Fq}; -use crate::arithmetic::Group; #[cfg(feature = "alloc")] use crate::arithmetic::{Coordinates, CurveAffine, CurveExt, FieldExt}; @@ -783,23 +782,6 @@ macro_rules! new_curve_impl { impl_binops_multiplicative!($name, $scalar); impl_binops_multiplicative_mixed!($name_affine, $scalar, $name); - impl Group for $name { - type Scalar = $scalar; - - fn group_zero() -> Self { - Self::identity() - } - fn group_add(&mut self, rhs: &Self) { - *self += *rhs; - } - fn group_sub(&mut self, rhs: &Self) { - *self -= *rhs; - } - fn group_scale(&mut self, by: &Self::Scalar) { - *self *= *by; - } - } - #[cfg(feature = "gpu")] impl ec_gpu::GpuName for $name_affine { fn name() -> alloc::string::String { diff --git a/src/fields/fp.rs b/src/fields/fp.rs index b505c99..dbc0317 100644 --- a/src/fields/fp.rs +++ b/src/fields/fp.rs @@ -11,7 +11,7 @@ use lazy_static::lazy_static; #[cfg(feature = "bits")] use ff::{FieldBits, PrimeFieldBits}; -use crate::arithmetic::{adc, mac, sbb, FieldExt, Group, SqrtTableHelpers}; +use crate::arithmetic::{adc, mac, sbb, FieldExt, SqrtTableHelpers}; #[cfg(feature = "sqrt-table")] use crate::arithmetic::SqrtTables; @@ -475,23 +475,6 @@ impl<'a> From<&'a Fp> for [u8; 32] { } } -impl Group for Fp { - type Scalar = Fp; - - fn group_zero() -> Self { - Self::zero() - } - fn group_add(&mut self, rhs: &Self) { - *self += *rhs; - } - fn group_sub(&mut self, rhs: &Self) { - *self -= *rhs; - } - fn group_scale(&mut self, by: &Self::Scalar) { - *self *= *by; - } -} - impl ff::Field for Fp { const ZERO: Self = Self::zero(); const ONE: Self = Self::one(); diff --git a/src/fields/fq.rs b/src/fields/fq.rs index 8f70cb5..7a3495f 100644 --- a/src/fields/fq.rs +++ b/src/fields/fq.rs @@ -11,7 +11,7 @@ use lazy_static::lazy_static; #[cfg(feature = "bits")] use ff::{FieldBits, PrimeFieldBits}; -use crate::arithmetic::{adc, mac, sbb, FieldExt, Group, SqrtTableHelpers}; +use crate::arithmetic::{adc, mac, sbb, FieldExt, SqrtTableHelpers}; #[cfg(feature = "sqrt-table")] use crate::arithmetic::SqrtTables; @@ -475,23 +475,6 @@ impl<'a> From<&'a Fq> for [u8; 32] { } } -impl Group for Fq { - type Scalar = Fq; - - fn group_zero() -> Self { - Self::zero() - } - fn group_add(&mut self, rhs: &Self) { - *self += *rhs; - } - fn group_sub(&mut self, rhs: &Self) { - *self -= *rhs; - } - fn group_scale(&mut self, by: &Self::Scalar) { - *self *= *by; - } -} - impl ff::Field for Fq { const ZERO: Self = Self::zero(); const ONE: Self = Self::one(); From bedaa0055c6a7c55087b946b73f2dd6149d3b52a Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 24 Nov 2022 12:14:59 +0000 Subject: [PATCH 4/7] Remove unnecessary `FieldExt` bounds in `crate::hashtocurve` --- src/hashtocurve.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/hashtocurve.rs b/src/hashtocurve.rs index c5b9a45..1f8fd90 100644 --- a/src/hashtocurve.rs +++ b/src/hashtocurve.rs @@ -1,6 +1,7 @@ //! This module implements "simplified SWU" hashing to short Weierstrass curves //! with a = 0. +use ff::{Field, PrimeField}; use static_assertions::const_assert; use subtle::ConstantTimeEq; @@ -77,7 +78,7 @@ pub fn hash_to_field( } /// Implements a degree 3 isogeny map. -pub fn iso_map, I: CurveExt>( +pub fn iso_map, I: CurveExt>( p: &I, iso: &[C::Base; 13], ) -> C { @@ -105,7 +106,7 @@ pub fn iso_map, I: CurveExt>( } #[allow(clippy::many_single_char_names)] -pub fn map_to_curve_simple_swu, I: CurveExt>( +pub fn map_to_curve_simple_swu, I: CurveExt>( u: &F, theta: F, z: F, From 9862b247d2098d2f2e699870252f08d58d16fa2f Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 24 Nov 2022 05:11:46 +0000 Subject: [PATCH 5/7] Migrate remaining `FieldExt` constants and methods into `ff` --- Cargo.toml | 2 +- src/arithmetic/fields.rs | 24 +------------------- src/curves.rs | 5 ++++- src/fields/fp.rs | 48 ++++++++++++++++++++++------------------ src/fields/fq.rs | 48 ++++++++++++++++++++++------------------ src/hashtocurve.rs | 8 +++---- src/lib.rs | 4 ++-- 7 files changed, 64 insertions(+), 75 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9cfc9da..33f3614 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,4 +74,4 @@ uninline-portable = [] serde = ["hex", "serde_crate"] [patch.crates-io] -ff = { git = "https://github.com/zkcrypto/ff.git", rev = "c070ffbaea8cb17e57f817a91ed0e364ff679b7c" } +ff = { git = "https://github.com/zkcrypto/ff.git", rev = "054a4d2daf9a9540d4c436fa51f0222e997ad15c" } diff --git a/src/arithmetic/fields.rs b/src/arithmetic/fields.rs index e80d6b5..ef45018 100644 --- a/src/arithmetic/fields.rs +++ b/src/arithmetic/fields.rs @@ -30,29 +30,7 @@ pub(crate) trait SqrtTableHelpers: ff::PrimeField { /// This trait is a common interface for dealing with elements of a finite /// field. -pub trait FieldExt: ff::PrimeField + Ord { - /// Modulus of the field written as a string for display purposes - const MODULUS: &'static str; - - /// Inverse of `PrimeField::ROOT_OF_UNITY` - const ROOT_OF_UNITY_INV: Self; - - /// Generator of the $t-order$ multiplicative subgroup - const DELTA: Self; - - /// Inverse of $2$ in the field. - const TWO_INV: Self; - - /// Element of multiplicative order $3$. - const ZETA: Self; - - /// Obtains a field element congruent to the integer `v`. - fn from_u128(v: u128) -> Self; - - /// Obtains a field element that is congruent to the provided little endian - /// byte representation of an integer. - fn from_bytes_wide(bytes: &[u8; 64]) -> Self; -} +pub trait FieldExt: ff::WithSmallOrderMulGroup<3> + Ord {} /// Parameters for a perfect hash function used in square root computation. #[cfg(feature = "sqrt-table")] diff --git a/src/curves.rs b/src/curves.rs index 45ab4c1..41ccb9a 100644 --- a/src/curves.rs +++ b/src/curves.rs @@ -18,10 +18,13 @@ use group::{ use rand::RngCore; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; +#[cfg(feature = "alloc")] +use ff::WithSmallOrderMulGroup; + use super::{Fp, Fq}; #[cfg(feature = "alloc")] -use crate::arithmetic::{Coordinates, CurveAffine, CurveExt, FieldExt}; +use crate::arithmetic::{Coordinates, CurveAffine, CurveExt}; macro_rules! new_curve_impl { (($($privacy:tt)*), $name:ident, $name_affine:ident, $iso:ident, $base:ident, $scalar:ident, diff --git a/src/fields/fp.rs b/src/fields/fp.rs index dbc0317..e97ae2c 100644 --- a/src/fields/fp.rs +++ b/src/fields/fp.rs @@ -1,7 +1,7 @@ use core::fmt; use core::ops::{Add, Mul, Neg, Sub}; -use ff::{Field, PrimeField}; +use ff::{Field, FromUniformBytes, PrimeField, WithSmallOrderMulGroup}; use rand::RngCore; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; @@ -563,11 +563,30 @@ impl ff::Field for Fp { impl ff::PrimeField for Fp { type Repr = [u8; 32]; + const MODULUS: &'static str = + "0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001"; + const TWO_INV: Self = Fp::from_raw([ + 0xcc96987680000001, + 0x11234c7e04a67c8d, + 0x0000000000000000, + 0x2000000000000000, + ]); const NUM_BITS: u32 = 255; const CAPACITY: u32 = 254; const MULTIPLICATIVE_GENERATOR: Self = GENERATOR; const S: u32 = S; const ROOT_OF_UNITY: Self = ROOT_OF_UNITY; + const ROOT_OF_UNITY_INV: Self = Fp::from_raw([ + 0xf0b87c7db2ce91f6, + 0x84a0a1d8859f066f, + 0xb4ed8e647196dad1, + 0x2cd5282c53116b5c, + ]); + const DELTA: Self = DELTA; + + fn from_u128(v: u128) -> Self { + Fp::from_raw([v as u64, (v >> 64) as u64, 0, 0]) + } fn from_repr(repr: Self::Repr) -> CtOption { let mut tmp = Fp([0, 0, 0, 0]); @@ -709,36 +728,21 @@ impl SqrtTableHelpers for Fp { } } -impl FieldExt for Fp { - const MODULUS: &'static str = - "0x40000000000000000000000000000000224698fc094cf91b992d30ed00000001"; - const ROOT_OF_UNITY_INV: Self = Fp::from_raw([ - 0xf0b87c7db2ce91f6, - 0x84a0a1d8859f066f, - 0xb4ed8e647196dad1, - 0x2cd5282c53116b5c, - ]); - const DELTA: Self = DELTA; - const TWO_INV: Self = Fp::from_raw([ - 0xcc96987680000001, - 0x11234c7e04a67c8d, - 0x0000000000000000, - 0x2000000000000000, - ]); +impl FieldExt for Fp {} + +impl WithSmallOrderMulGroup<3> for Fp { const ZETA: Self = Fp::from_raw([ 0x1dad5ebdfdfe4ab9, 0x1d1f8bd237ad3149, 0x2caad5dc57aab1b0, 0x12ccca834acdba71, ]); +} - fn from_u128(v: u128) -> Self { - Fp::from_raw([v as u64, (v >> 64) as u64, 0, 0]) - } - +impl FromUniformBytes<64> for Fp { /// Converts a 512-bit little endian integer into /// a `Fp` by reducing by the modulus. - fn from_bytes_wide(bytes: &[u8; 64]) -> Fp { + fn from_uniform_bytes(bytes: &[u8; 64]) -> Fp { Fp::from_u512([ u64::from_le_bytes(bytes[0..8].try_into().unwrap()), u64::from_le_bytes(bytes[8..16].try_into().unwrap()), diff --git a/src/fields/fq.rs b/src/fields/fq.rs index 7a3495f..8a3ac47 100644 --- a/src/fields/fq.rs +++ b/src/fields/fq.rs @@ -1,7 +1,7 @@ use core::fmt; use core::ops::{Add, Mul, Neg, Sub}; -use ff::{Field, PrimeField}; +use ff::{Field, FromUniformBytes, PrimeField, WithSmallOrderMulGroup}; use rand::RngCore; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; @@ -563,11 +563,30 @@ impl ff::Field for Fq { impl ff::PrimeField for Fq { type Repr = [u8; 32]; + const MODULUS: &'static str = + "0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001"; const NUM_BITS: u32 = 255; const CAPACITY: u32 = 254; + const TWO_INV: Self = Fq::from_raw([ + 0xc623759080000001, + 0x11234c7e04ca546e, + 0x0000000000000000, + 0x2000000000000000, + ]); const MULTIPLICATIVE_GENERATOR: Self = GENERATOR; const S: u32 = S; const ROOT_OF_UNITY: Self = ROOT_OF_UNITY; + const ROOT_OF_UNITY_INV: Self = Fq::from_raw([ + 0x57eecda0a84b6836, + 0x4ad38b9084b8a80c, + 0xf4c8f353124086c1, + 0x2235e1a7415bf936, + ]); + const DELTA: Self = DELTA; + + fn from_u128(v: u128) -> Self { + Fq::from_raw([v as u64, (v >> 64) as u64, 0, 0]) + } fn from_repr(repr: Self::Repr) -> CtOption { let mut tmp = Fq([0, 0, 0, 0]); @@ -708,36 +727,21 @@ impl SqrtTableHelpers for Fq { } } -impl FieldExt for Fq { - const MODULUS: &'static str = - "0x40000000000000000000000000000000224698fc0994a8dd8c46eb2100000001"; - const ROOT_OF_UNITY_INV: Self = Fq::from_raw([ - 0x57eecda0a84b6836, - 0x4ad38b9084b8a80c, - 0xf4c8f353124086c1, - 0x2235e1a7415bf936, - ]); - const DELTA: Self = DELTA; - const TWO_INV: Self = Fq::from_raw([ - 0xc623759080000001, - 0x11234c7e04ca546e, - 0x0000000000000000, - 0x2000000000000000, - ]); +impl FieldExt for Fq {} + +impl WithSmallOrderMulGroup<3> for Fq { const ZETA: Self = Fq::from_raw([ 0x2aa9d2e050aa0e4f, 0x0fed467d47c033af, 0x511db4d81cf70f5a, 0x06819a58283e528e, ]); +} - fn from_u128(v: u128) -> Self { - Fq::from_raw([v as u64, (v >> 64) as u64, 0, 0]) - } - +impl FromUniformBytes<64> for Fq { /// Converts a 512-bit little endian integer into /// a `Fq` by reducing by the modulus. - fn from_bytes_wide(bytes: &[u8; 64]) -> Fq { + fn from_uniform_bytes(bytes: &[u8; 64]) -> Fq { Fq::from_u512([ u64::from_le_bytes(bytes[0..8].try_into().unwrap()), u64::from_le_bytes(bytes[8..16].try_into().unwrap()), diff --git a/src/hashtocurve.rs b/src/hashtocurve.rs index 1f8fd90..3691f24 100644 --- a/src/hashtocurve.rs +++ b/src/hashtocurve.rs @@ -1,14 +1,14 @@ //! This module implements "simplified SWU" hashing to short Weierstrass curves //! with a = 0. -use ff::{Field, PrimeField}; +use ff::{Field, FromUniformBytes, PrimeField}; use static_assertions::const_assert; use subtle::ConstantTimeEq; -use crate::arithmetic::{CurveExt, FieldExt}; +use crate::arithmetic::CurveExt; /// Hashes over a message and writes the output to all of `buf`. -pub fn hash_to_field( +pub fn hash_to_field>( curve_id: &str, domain_prefix: &str, message: &[u8], @@ -73,7 +73,7 @@ pub fn hash_to_field( let mut little = [0u8; CHUNKLEN]; little.copy_from_slice(big.as_array()); little.reverse(); - *buf = F::from_bytes_wide(&little); + *buf = F::from_uniform_bytes(&little); } } diff --git a/src/lib.rs b/src/lib.rs index 4d22495..188134b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,8 +39,8 @@ pub extern crate group; #[cfg(feature = "alloc")] #[test] fn test_endo_consistency() { - use crate::arithmetic::{CurveExt, FieldExt}; - use group::Group; + use crate::arithmetic::CurveExt; + use group::{ff::WithSmallOrderMulGroup, Group}; let a = pallas::Point::generator(); assert_eq!(a * pallas::Scalar::ZETA, a.endo()); From 56b73e32ce81acc50d0eca79f0c43825e48862c3 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 24 Nov 2022 20:20:27 +0000 Subject: [PATCH 6/7] Move `Ord` bound from `FieldExt` to `CurveAffine::{Base, Scalar}` This helps with the `FieldExt` removal while deferring the decision on whether to add an `Ord` bound to `ff::PrimeField`. --- src/arithmetic/curves.rs | 4 ++-- src/arithmetic/fields.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/arithmetic/curves.rs b/src/arithmetic/curves.rs index f9b2ef4..ade9dbf 100644 --- a/src/arithmetic/curves.rs +++ b/src/arithmetic/curves.rs @@ -102,9 +102,9 @@ pub trait CurveAffine: + From<::Curve> { /// The scalar field of this elliptic curve. - type ScalarExt: FieldExt; + type ScalarExt: FieldExt + Ord; /// The base field over which this elliptic curve is constructed. - type Base: FieldExt; + type Base: FieldExt + Ord; /// The projective form of the curve type CurveExt: CurveExt::ScalarExt>; diff --git a/src/arithmetic/fields.rs b/src/arithmetic/fields.rs index ef45018..d42419d 100644 --- a/src/arithmetic/fields.rs +++ b/src/arithmetic/fields.rs @@ -30,7 +30,7 @@ pub(crate) trait SqrtTableHelpers: ff::PrimeField { /// This trait is a common interface for dealing with elements of a finite /// field. -pub trait FieldExt: ff::WithSmallOrderMulGroup<3> + Ord {} +pub trait FieldExt: ff::WithSmallOrderMulGroup<3> {} /// Parameters for a perfect hash function used in square root computation. #[cfg(feature = "sqrt-table")] From 825cb944decfab4b1a64380d15a43eb93899dfad Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 24 Nov 2022 20:51:46 +0000 Subject: [PATCH 7/7] Remove `FieldExt` trait MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One, two! One, two! And through and through     The vorpal blade went snicker-snack! He left it dead, and with its head     He went galumphing back. Closes zcash/pasta_curves#42. --- CHANGELOG.md | 1 + src/arithmetic.rs | 2 +- src/arithmetic/curves.rs | 11 ++++------- src/arithmetic/fields.rs | 4 ---- src/fields/fp.rs | 4 +--- src/fields/fq.rs | 4 +--- 6 files changed, 8 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe33e88..3c611da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to Rust's notion of ### Removed - `pasta_curves::arithmetic`: + - `FieldExt` (use `ff::PrimeField` or `ff::WithSmallOrderMulGroup` instead). - `Group` - `SqrtRatio` (use `ff::Field::{sqrt_ratio, sqrt_alt}` instead). - `SqrtTables` (from public API, as it isn't suitable for generic usage). diff --git a/src/arithmetic.rs b/src/arithmetic.rs index 39613a6..faf5872 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -8,4 +8,4 @@ mod curves; mod fields; pub use curves::*; -pub use fields::*; +pub(crate) use fields::*; diff --git a/src/arithmetic/curves.rs b/src/arithmetic/curves.rs index ade9dbf..636f080 100644 --- a/src/arithmetic/curves.rs +++ b/src/arithmetic/curves.rs @@ -6,9 +6,6 @@ use group::prime::{PrimeCurve, PrimeCurveAffine}; #[cfg(feature = "alloc")] use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; -#[cfg(feature = "alloc")] -use super::FieldExt; - #[cfg(feature = "alloc")] use alloc::boxed::Box; #[cfg(feature = "alloc")] @@ -30,9 +27,9 @@ pub trait CurveExt: + From<::Affine> { /// The scalar field of this elliptic curve. - type ScalarExt: FieldExt; + type ScalarExt: ff::WithSmallOrderMulGroup<3>; /// The base field over which this elliptic curve is constructed. - type Base: FieldExt; + type Base: ff::WithSmallOrderMulGroup<3>; /// The affine version of the curve type AffineExt: CurveAffine::ScalarExt> + Mul @@ -102,9 +99,9 @@ pub trait CurveAffine: + From<::Curve> { /// The scalar field of this elliptic curve. - type ScalarExt: FieldExt + Ord; + type ScalarExt: ff::WithSmallOrderMulGroup<3> + Ord; /// The base field over which this elliptic curve is constructed. - type Base: FieldExt + Ord; + type Base: ff::WithSmallOrderMulGroup<3> + Ord; /// The projective form of the curve type CurveExt: CurveExt::ScalarExt>; diff --git a/src/arithmetic/fields.rs b/src/arithmetic/fields.rs index d42419d..bd356a2 100644 --- a/src/arithmetic/fields.rs +++ b/src/arithmetic/fields.rs @@ -28,10 +28,6 @@ pub(crate) trait SqrtTableHelpers: ff::PrimeField { fn get_lower_32(&self) -> u32; } -/// This trait is a common interface for dealing with elements of a finite -/// field. -pub trait FieldExt: ff::WithSmallOrderMulGroup<3> {} - /// Parameters for a perfect hash function used in square root computation. #[cfg(feature = "sqrt-table")] #[cfg_attr(docsrs, doc(cfg(feature = "sqrt-table")))] diff --git a/src/fields/fp.rs b/src/fields/fp.rs index e97ae2c..2c2c9b5 100644 --- a/src/fields/fp.rs +++ b/src/fields/fp.rs @@ -11,7 +11,7 @@ use lazy_static::lazy_static; #[cfg(feature = "bits")] use ff::{FieldBits, PrimeFieldBits}; -use crate::arithmetic::{adc, mac, sbb, FieldExt, SqrtTableHelpers}; +use crate::arithmetic::{adc, mac, sbb, SqrtTableHelpers}; #[cfg(feature = "sqrt-table")] use crate::arithmetic::SqrtTables; @@ -728,8 +728,6 @@ impl SqrtTableHelpers for Fp { } } -impl FieldExt for Fp {} - impl WithSmallOrderMulGroup<3> for Fp { const ZETA: Self = Fp::from_raw([ 0x1dad5ebdfdfe4ab9, diff --git a/src/fields/fq.rs b/src/fields/fq.rs index 8a3ac47..9417d2e 100644 --- a/src/fields/fq.rs +++ b/src/fields/fq.rs @@ -11,7 +11,7 @@ use lazy_static::lazy_static; #[cfg(feature = "bits")] use ff::{FieldBits, PrimeFieldBits}; -use crate::arithmetic::{adc, mac, sbb, FieldExt, SqrtTableHelpers}; +use crate::arithmetic::{adc, mac, sbb, SqrtTableHelpers}; #[cfg(feature = "sqrt-table")] use crate::arithmetic::SqrtTables; @@ -727,8 +727,6 @@ impl SqrtTableHelpers for Fq { } } -impl FieldExt for Fq {} - impl WithSmallOrderMulGroup<3> for Fq { const ZETA: Self = Fq::from_raw([ 0x2aa9d2e050aa0e4f,