Skip to content

Commit

Permalink
single macro for PrimeFieldBits
Browse files Browse the repository at this point in the history
  • Loading branch information
kilic committed Apr 6, 2024
1 parent 37d9b47 commit 0287a7d
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 67 deletions.
6 changes: 1 addition & 5 deletions src/bn256/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,14 @@ extend_field_legendre!(Fq);
impl_from_uniform_bytes!(Fq, 64);
impl_from_uniform_bytes!(Fq, 48);
impl_from_u64!(Fq);
field_bits!(Fq);

const_montgomery_4!(Fq);
#[cfg(not(feature = "asm"))]
field_arithmetic_4!(Fq, sparse);
#[cfg(feature = "asm")]
field_arithmetic_asm!(Fq, MODULUS, INV);

#[cfg(target_pointer_width = "64")]
field_bits!(Fq);
#[cfg(not(target_pointer_width = "64"))]
field_bits!(Fq);

#[cfg(feature = "derive_serde")]
crate::serialize_deserialize_primefield!(Fq, [u8; 32]);

Expand Down
6 changes: 1 addition & 5 deletions src/bn256/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,14 @@ impl_prime_field!(Fr, [u8; 32], le);
impl_sum_prod!(Fr);
extend_field_legendre!(Fr);
impl_from_uniform_bytes!(Fr, 64);
field_bits!(Fr);

const_montgomery_4!(Fr);
#[cfg(not(feature = "asm"))]
field_arithmetic_4!(Fr, sparse);
#[cfg(feature = "asm")]
field_arithmetic_asm!(Fr, MODULUS, INV);

#[cfg(target_pointer_width = "64")]
field_bits!(Fr);
#[cfg(not(target_pointer_width = "64"))]
field_bits!(Fr);

#[cfg(not(feature = "bn256-table"))]
impl_from_u64!(Fr);
#[cfg(feature = "bn256-table")]
Expand Down
42 changes: 15 additions & 27 deletions src/derive/field/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,51 +424,39 @@ macro_rules! impl_serde_object {

#[macro_export]
macro_rules! field_bits {
// For #[cfg(target_pointer_width = "64")]
($field:ident) => {
#[cfg(feature = "bits")]
#[cfg_attr(docsrs, doc(cfg(feature = "bits")))]
impl ff::PrimeFieldBits for $field {
#[cfg(target_pointer_width = "64")]
type ReprBits = [u64; NUM_LIMBS];
#[cfg(not(target_pointer_width = "64"))]
type ReprBits = [u32; NUM_LIMBS * 2];

fn to_le_bits(&self) -> ff::FieldBits<Self::ReprBits> {
let bytes: [u8; Self::SIZE] = self.to_repr().into();

let limbs = (0..NUM_LIMBS)
.map(|off| {
u64::from_le_bytes(bytes[off * 8..(off + 1) * 8].try_into().unwrap())
})
.collect::<Vec<_>>();

ff::FieldBits::new(limbs.try_into().unwrap())
}

fn char_le_bits() -> ff::FieldBits<Self::ReprBits> {
ff::FieldBits::new(MODULUS.0)
}
}
};
// For #[cfg(not(target_pointer_width = "64"))]
($field:ident) => {
#[cfg(feature = "bits")]
#[cfg_attr(docsrs, doc(cfg(feature = "bits")))]
impl ff::PrimeFieldBits for $field {
type ReprBits = [u32; NUM_LIMBS * 2];

fn to_le_bits(&self) -> ff::FieldBits<Self::ReprBits> {
let bytes = self.to_repr();
#[cfg(target_pointer_width = "64")]
const STEP: usize = 8;
#[cfg(not(target_pointer_width = "64"))]
const STEP: usize = 4;

let limbs = (0..NUM_LIMBS * 2)
let limbs = (0..NUM_LIMBS * 8 / STEP)
.map(|off| {
u64::from_le_bytes(bytes[off * 4..(off + 1) * 4].try_into().unwrap())
u64::from_le_bytes(bytes[off * STEP..(off + 1) * STEP].try_into().unwrap())
})
.collect::<Vec<_>>();

ff::FieldBits::new(limbs.try_into().unwrap())
}

fn char_le_bits() -> ff::FieldBits<Self::ReprBits> {
ff::FieldBits::new(MODULUS_LIMBS_32)
#[cfg(target_pointer_width = "64")]
let bits = ff::FieldBits::new(MODULUS.0);
#[cfg(not(target_pointer_width = "64"))]
let bits = ff::FieldBits::new(MODULUS_LIMBS_32.0);

bits
}
}
};
Expand Down
6 changes: 1 addition & 5 deletions src/pluto_eris/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,10 @@ impl_from_u64!(Fp);
impl_from_uniform_bytes!(Fp, 64);
impl_from_uniform_bytes!(Fp, 72);
impl_from_uniform_bytes!(Fp, 112);
field_bits!(Fp);

field_arithmetic_7!(Fp);

#[cfg(target_pointer_width = "64")]
field_bits!(Fp);
#[cfg(not(target_pointer_width = "64"))]
field_bits!(Fp);

#[cfg(feature = "derive_serde")]
crate::serialize_deserialize_primefield!(Fp, ReprFp);

Expand Down
6 changes: 1 addition & 5 deletions src/pluto_eris/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,10 @@ impl_from_u64!(Fq);
impl_from_uniform_bytes!(Fq, 64);
impl_from_uniform_bytes!(Fq, 72);
impl_from_uniform_bytes!(Fq, 112);
field_bits!(Fq);

field_arithmetic_7!(Fq);

#[cfg(target_pointer_width = "64")]
field_bits!(Fq);
#[cfg(not(target_pointer_width = "64"))]
field_bits!(Fq);

#[cfg(feature = "derive_serde")]
crate::serialize_deserialize_primefield!(Fq, ReprFq);

Expand Down
6 changes: 1 addition & 5 deletions src/secp256k1/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,11 @@ extend_field_legendre!(Fp);
impl_from_uniform_bytes!(Fp, 64);
impl_from_uniform_bytes!(Fp, 48);
impl_from_u64!(Fp);
field_bits!(Fp);

const_montgomery_4!(Fp);
field_arithmetic_4!(Fp, dense);

#[cfg(target_pointer_width = "64")]
field_bits!(Fp);
#[cfg(not(target_pointer_width = "64"))]
field_bits!(Fp);

#[cfg(feature = "derive_serde")]
crate::serialize_deserialize_primefield!(Fp, [u8; 32]);

Expand Down
6 changes: 1 addition & 5 deletions src/secp256k1/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,11 @@ extend_field_legendre!(Fq);
impl_from_uniform_bytes!(Fq, 64);
impl_from_uniform_bytes!(Fq, 48);
impl_from_u64!(Fq);
field_bits!(Fq);

const_montgomery_4!(Fq);
field_arithmetic_4!(Fq, dense);

#[cfg(target_pointer_width = "64")]
field_bits!(Fq);
#[cfg(not(target_pointer_width = "64"))]
field_bits!(Fq);

#[cfg(feature = "derive_serde")]
crate::serialize_deserialize_primefield!(Fq, [u8; 32]);

Expand Down
6 changes: 1 addition & 5 deletions src/secp256r1/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,11 @@ extend_field_legendre!(Fp);
impl_from_uniform_bytes!(Fp, 64);
impl_from_uniform_bytes!(Fp, 48);
impl_from_u64!(Fp);
field_bits!(Fp);

const_montgomery_4!(Fp);
field_arithmetic_4!(Fp, dense);

#[cfg(target_pointer_width = "64")]
field_bits!(Fp);
#[cfg(not(target_pointer_width = "64"))]
field_bits!(Fp);

#[cfg(feature = "derive_serde")]
crate::serialize_deserialize_primefield!(Fp, [u8; 32]);

Expand Down
6 changes: 1 addition & 5 deletions src/secp256r1/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,11 @@ extend_field_legendre!(Fq);
impl_from_uniform_bytes!(Fq, 64);
impl_from_uniform_bytes!(Fq, 48);
impl_from_u64!(Fq);
field_bits!(Fq);

const_montgomery_4!(Fq);
field_arithmetic_4!(Fq, dense);

#[cfg(target_pointer_width = "64")]
field_bits!(Fq);
#[cfg(not(target_pointer_width = "64"))]
field_bits!(Fq);

#[cfg(feature = "derive_serde")]
crate::serialize_deserialize_primefield!(Fq, [u8; 32]);

Expand Down

0 comments on commit 0287a7d

Please sign in to comment.