Skip to content

Commit

Permalink
use non asm mul in FromUniforomBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
kilic committed May 13, 2024
1 parent 84edae1 commit bb5d509
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
4 changes: 3 additions & 1 deletion derive/src/field/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,9 @@ pub(crate) fn impl_field(input: TokenStream) -> TokenStream {
.unwrap();
let a1 = #field(a1);

a0 * Self::R2 + a1 * Self::R3
// enforce non assmebly impl since asm is likely to be optimized for sparse fields
a0.mul_const(&Self::R2) + a1.mul_const(&Self::R3)

}
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/tests/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,18 +725,18 @@ where
use rand_core::OsRng;
use rand_core::RngCore;

let mut uniform_bytes = [0u8; L];
OsRng.fill_bytes(&mut uniform_bytes[..]);
let uniform_bytes = [0u8; L];
assert_eq!(F::from_uniform_bytes(&uniform_bytes), F::ZERO);

let e0 = {
let mut uniform_bytes = [u8::MAX; L];

for _ in 0..10000 {
let e0 = BigUint::from_bytes_le(&uniform_bytes);
let e0 = e0 % crate::tests::modulus::<F>();
let bytes = e0.to_bytes_le();
let mut e0 = F::Repr::default();
e0.as_mut()[..bytes.len()].copy_from_slice(&bytes);
F::from_repr(e0).unwrap()
};
let e0: F = crate::tests::big_to_fe(&e0);

let e1 = F::from_uniform_bytes(&uniform_bytes);
assert_eq!(e0, e1);

let e1 = F::from_uniform_bytes(&uniform_bytes);
assert_eq!(e0, e1);
OsRng.fill_bytes(&mut uniform_bytes[..]);
}
}
8 changes: 8 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ pub(crate) fn fe_to_big<F: PrimeField>(fe: &F) -> BigUint {
BigUint::from_bytes_le(fe.to_repr().as_ref())
}

pub fn big_to_fe<F: PrimeField>(e: &BigUint) -> F {
let e = e % modulus::<F>();
let bytes = e.to_bytes_le();
let mut repr = F::Repr::default();
repr.as_mut()[..bytes.len()].copy_from_slice(&bytes[..]);
F::from_repr(repr).unwrap()
}

pub(crate) fn modulus<F: PrimeField>() -> BigUint {
fe_to_big(&-F::ONE) + 1usize
}

0 comments on commit bb5d509

Please sign in to comment.