Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
winston-h-zhang committed Mar 4, 2024
1 parent 362d327 commit 3502f38
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,13 @@ fn read_header<R: Read>(
.read_exact(&mut prime_size)
.map_err(|err| ReadBytesError { source: err.into() })?;
let prime = U256::from_le_slice(&prime_size);
let prime = &prime.to_string().to_ascii_lowercase();

if prime != &expected_prime[2..] {
// get rid of '0x' in the front
let expected_prime =
U256::from_str_radix(&expected_prime[2..], 16).map_err(|_err| NonMatchingPrime {
expected: expected_prime.to_string(),
value: prime.to_string(),
})?;
if prime != expected_prime {
return Err(NonMatchingPrime {
expected: expected_prime.to_string(),
value: prime.to_string(),
Expand Down
25 changes: 18 additions & 7 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,27 @@ pub fn ff_as_limbs<F: PrimeField>(f: &F) -> &[u32; 8] {
unsafe { transmute(repr) }
}

// /// Assumes little endian
// pub fn limbs_as_ff<F: PrimeField>(limbs: [u32; 8]) -> F {
// let mut repr = F::ZERO.to_repr();
// let limbs: [u8; 32] = unsafe { transmute(limbs) };
// for (i, digit) in repr.as_mut().iter_mut().enumerate() {
// // this doesn't work if the platform we're on is not little endian :scream:
// *digit = limbs[i];
// }

// F::from_repr(repr).unwrap()
// }

// TODO(winston): This is slower than the above, but does not fail
/// Assumes little endian
pub fn limbs_as_ff<F: PrimeField>(limbs: [u32; 8]) -> F {
let mut repr = F::ZERO.to_repr();
let limbs: [u8; 32] = unsafe { transmute(limbs) };
for (i, digit) in repr.as_mut().iter_mut().enumerate() {
// this doesn't work if the platform we're on is not little endian :scream:
*digit = limbs[i];
let mut res = F::ZERO;
let radix = F::from(0x0001_0000_0000_u64);
for val in limbs {
res = res * radix + F::from(u64::from(val));
}

F::from_repr(repr).unwrap()
res
}

/// Assumes little endian
Expand Down

0 comments on commit 3502f38

Please # to comment.