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 12f3bf8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 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
46 changes: 29 additions & 17 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use ff::PrimeField;
use ruint::aliases::U256;

/// Assumes little endian
pub fn u256_as_limbs(uint: &U256) -> &[u32; 8] {
let limbs = uint.as_limbs();
pub fn u256_as_limbs(uint: U256) -> [u32; 8] {
let limbs = *uint.as_limbs();
unsafe { transmute(limbs) }
}

Expand All @@ -16,9 +16,9 @@ pub fn limbs_as_u256(limbs: [u32; 8]) -> U256 {
}

/// Assumes little endian
pub fn ff_as_limbs<F: PrimeField>(f: &F) -> &[u32; 8] {
pub fn ff_as_limbs<F: PrimeField>(f: F) -> [u32; 8] {
let binding = f.to_repr();
let repr: &[u8; 32] = binding.as_ref().try_into().unwrap();
let repr: [u8; 32] = binding.as_ref().try_into().unwrap();
// this doesn't work if the platform we're on is not little endian :scream:
unsafe { transmute(repr) }
}
Expand All @@ -35,15 +35,26 @@ pub fn limbs_as_ff<F: PrimeField>(limbs: [u32; 8]) -> F {
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 res = F::ZERO;
// let radix = F::from(0x0001_0000_0000_u64);
// for &val in limbs.iter().rev() {
// res = res * radix + F::from(u64::from(val));
// }
// res
// }

/// Assumes little endian
pub fn u256_as_ff<F: PrimeField>(uint: &U256) -> F {
limbs_as_ff(*u256_as_limbs(uint))
pub fn u256_as_ff<F: PrimeField>(uint: U256) -> F {
limbs_as_ff(u256_as_limbs(uint))
}

#[allow(unused)]
/// Assumes little endian
pub fn ff_as_u256<F: PrimeField>(f: &F) -> U256 {
limbs_as_u256(*ff_as_limbs(f))
pub fn ff_as_u256<F: PrimeField>(f: F) -> U256 {
limbs_as_u256(ff_as_limbs(f))
}

#[cfg(test)]
Expand All @@ -61,8 +72,8 @@ mod tests {

for _ in 0..100 {
let uint = rng.gen::<U256>();
let limbs = u256_as_limbs(&uint);
let other_uint = limbs_as_u256(*limbs);
let limbs = u256_as_limbs(uint);
let other_uint = limbs_as_u256(limbs);
assert_eq!(uint, other_uint)
}
}
Expand All @@ -71,11 +82,12 @@ mod tests {
fn test_ff_limb_roundtrip() {
let mut rng = rand::thread_rng();

for _ in 0..100 {}
let f = pallas::Scalar::random(&mut rng);
let limbs = ff_as_limbs(&f);
let other_f = limbs_as_ff(*limbs);
assert_eq!(f, other_f)
for _ in 0..100 {
let f = pallas::Scalar::random(&mut rng);
let limbs = ff_as_limbs(f);
let other_f = limbs_as_ff(limbs);
assert_eq!(f, other_f)
}
}

#[test]
Expand All @@ -84,8 +96,8 @@ mod tests {

for _ in 0..100 {
let f = pallas::Scalar::random(&mut rng);
let uint = ff_as_u256(&f);
let other_f = u256_as_ff(&uint);
let uint = ff_as_u256(f);
let other_f = u256_as_ff(uint);
assert_eq!(f, other_f)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/witness/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl SafeMemory {

if view[ptr + 7] & 0x80 != 0 {
let num = self.read_big(store, ptr + 8);
u256_as_ff(&num)
u256_as_ff(num)
} else {
F::from(u64::from(self.read_u32(store, ptr)))
}
Expand All @@ -186,7 +186,7 @@ impl SafeMemory {
/// * `ptr` - The memory address where the field element will be written.
/// * `fr` - The [`U256`] field element to write.
fn write_short(&mut self, store: &impl AsStoreRef, ptr: usize, fr: U256) -> Result<()> {
let num = fr.as_limbs()[0] as u32; // wtf is happening
let num = fr.as_limbs()[0] as u32;
self.write_u32(store, ptr, num);
self.write_u32(store, ptr + 4, 0);
Ok(())
Expand Down
5 changes: 2 additions & 3 deletions src/witness/witness_calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl WitnessCalculator {
let (msb, lsb) = fnv(&input.name);

for (i, value) in input.value.into_iter().enumerate() {
let f_arr = ff_as_limbs(&value);
let f_arr = ff_as_limbs(value);
for j in 0..n32 {
self.instance
.write_shared_rw_memory(&mut self.store, j, f_arr[j as usize])?;
Expand All @@ -195,8 +195,7 @@ impl WitnessCalculator {
self.instance.get_witness(&mut self.store, i)?;
let mut arr = [0; 8];
for j in 0..n32 {
arr[(n32 as usize) - 1 - (j as usize)] =
self.instance.read_shared_rw_memory(&mut self.store, j)?;
arr[j as usize] = self.instance.read_shared_rw_memory(&mut self.store, j)?;
}
w.push(limbs_as_ff(arr));
}
Expand Down

0 comments on commit 12f3bf8

Please # to comment.