From 9862b247d2098d2f2e699870252f08d58d16fa2f Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 24 Nov 2022 05:11:46 +0000 Subject: [PATCH] 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());