From 9fc20ec486276f69c4506e5aa08d5ce233b6e544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20P=C3=A9rez?= <37264926+CPerezz@users.noreply.github.com> Date: Sun, 7 Jan 2024 07:34:34 +0100 Subject: [PATCH] Add FieldBits impl for Secp256r1 & remove `utils` module (#123) * chore: Move fe_from_str to integr tests It doesn't make much sense to have a `utils` module which in reality just has one function that only integration tests require. So, chore: Move `fe_from_str` to integration tests file removing code from the lib itself. * fix: Include field_bits! macro impl for Secp256r1/Fq --- src/lib.rs | 2 -- src/secp256r1/fq.rs | 7 ++++++- src/tests/curve.rs | 15 ++++++++++++++- src/utils.rs | 14 -------------- 4 files changed, 20 insertions(+), 18 deletions(-) delete mode 100644 src/utils.rs diff --git a/src/lib.rs b/src/lib.rs index cba3af3a..b2396d7b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,8 +13,6 @@ pub mod secp256k1; pub mod secp256r1; pub mod secq256k1; -pub mod utils; - #[macro_use] mod derive; diff --git a/src/secp256r1/fq.rs b/src/secp256r1/fq.rs index 86005d35..d611097e 100644 --- a/src/secp256r1/fq.rs +++ b/src/secp256r1/fq.rs @@ -118,7 +118,7 @@ const ZETA: Fq = Fq::from_raw([ const DELTA: Fq = Fq::from_raw([0x1e39a5057d81, 0, 0, 0]); use crate::{ - field_arithmetic, field_common, field_specific, impl_add_binop_specify_output, + field_arithmetic, field_bits, field_common, field_specific, impl_add_binop_specify_output, impl_binops_additive, impl_binops_additive_specify_output, impl_binops_multiplicative, impl_binops_multiplicative_mixed, impl_from_u64, impl_sub_binop_specify_output, impl_sum_prod, }; @@ -141,6 +141,11 @@ impl_from_u64!(Fq, R2); field_arithmetic!(Fq, MODULUS, INV, dense); impl_sum_prod!(Fq); +#[cfg(target_pointer_width = "64")] +field_bits!(Fq, MODULUS); +#[cfg(not(target_pointer_width = "64"))] +field_bits!(Fq, MODULUS, MODULUS_LIMBS_32); + impl Fq { pub const fn size() -> usize { 32 diff --git a/src/tests/curve.rs b/src/tests/curve.rs index 32c61aa3..9554e875 100644 --- a/src/tests/curve.rs +++ b/src/tests/curve.rs @@ -3,10 +3,13 @@ use crate::ff::Field; use crate::ff_ext::Legendre; use crate::group::prime::PrimeCurveAffine; -use crate::utils::fe_from_str; use crate::{group::GroupEncoding, serde::SerdeObject}; use crate::{hash_to_curve, CurveAffine, CurveExt}; +use ff::PrimeField; +use num_bigint::BigUint; +use num_traits::Num; use rand_core::{OsRng, RngCore}; +use std::borrow::Cow; use std::iter; #[cfg(feature = "derive_serde")] @@ -352,6 +355,16 @@ pub fn hash_to_curve_test() { } } +fn fe_from_str(string: impl AsRef) -> F { + let string = string.as_ref(); + let oct = if let Some(hex) = string.strip_prefix("0x") { + Cow::Owned(BigUint::from_str_radix(hex, 16).unwrap().to_string()) + } else { + Cow::Borrowed(string) + }; + F::from_str_vartime(&oct).unwrap() +} + pub fn svdw_map_to_curve_test( z: G::Base, precomputed_constants: [&'static str; 4], diff --git a/src/utils.rs b/src/utils.rs deleted file mode 100644 index 3b595672..00000000 --- a/src/utils.rs +++ /dev/null @@ -1,14 +0,0 @@ -use ff::PrimeField; -use num_bigint::BigUint; -use num_traits::Num; -use std::borrow::Cow; - -pub(crate) fn fe_from_str(string: impl AsRef) -> F { - let string = string.as_ref(); - let oct = if let Some(hex) = string.strip_prefix("0x") { - Cow::Owned(BigUint::from_str_radix(hex, 16).unwrap().to_string()) - } else { - Cow::Borrowed(string) - }; - F::from_str_vartime(&oct).unwrap() -}