Skip to content

Commit

Permalink
Add FieldBits impl for Secp256r1 & remove utils module (#123)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
CPerezz authored Jan 7, 2024
1 parent 83d69d5 commit 9fc20ec
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ pub mod secp256k1;
pub mod secp256r1;
pub mod secq256k1;

pub mod utils;

#[macro_use]
mod derive;

Expand Down
7 changes: 6 additions & 1 deletion src/secp256r1/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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
Expand Down
15 changes: 14 additions & 1 deletion src/tests/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -352,6 +355,16 @@ pub fn hash_to_curve_test<G: CurveExt>() {
}
}

fn fe_from_str<F: PrimeField>(string: impl AsRef<str>) -> 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<G: CurveExt>(
z: G::Base,
precomputed_constants: [&'static str; 4],
Expand Down
14 changes: 0 additions & 14 deletions src/utils.rs

This file was deleted.

0 comments on commit 9fc20ec

Please sign in to comment.