Skip to content

Commit

Permalink
test: Check hashes from byte slice inputs with lower length
Browse files Browse the repository at this point in the history
We allow to pass byte slices with lower length than the one indicated
by the modulus of prime field. To ensure that it works, provide a
test which compares that hashes produced from smaller inputs are
equivalent to hashes produces from bigger inputs, filled up with
zeros.

Such inputs with different sizes are still representing the same
prime field elements.
  • Loading branch information
vadorovsky committed Nov 3, 2023
1 parent 9746e79 commit 7598f97
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions light-poseidon/tests/bn254_fq_x5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,101 @@ fn test_poseidon_bn254_x5_fq_hash_bytes_le() {
);
}

#[test]
fn test_poseidon_bn254_x5_fq_smaller_arrays() {
let mut hasher = Poseidon::<Fr>::new_circom(1).unwrap();

let input1 = vec![1; 1];
let hash1 = hasher.hash_bytes_le(&[input1.as_slice()]).unwrap();

for len in 2..32 {
let input = [vec![1u8], vec![0; len - 1]].concat();
let hash = hasher.hash_bytes_le(&[input.as_slice()]).unwrap();

assert_eq!(hash, hash1);
}

let input1 = vec![1; 1];
let hash1 = hasher.hash_bytes_be(&[input1.as_slice()]).unwrap();

for len in 2..32 {
let input = [vec![0; len - 1], vec![1u8]].concat();
let hash = hasher.hash_bytes_be(&[input.as_slice()]).unwrap();

assert_eq!(hash, hash1);
}
}

#[test]
fn test_poseidon_bn254_x5_fq_hash_bytes_be_smaller_arrays_random() {
for nr_inputs in 1..12 {
let mut hasher = Poseidon::<Fr>::new_circom(nr_inputs).unwrap();
for smaller_arr_len in 1..31 {
let inputs: Vec<Vec<u8>> = (0..nr_inputs)
.map(|_| {
let rng = rand::thread_rng();
rng.sample_iter(rand::distributions::Standard)
.take(smaller_arr_len)
.collect()
})
.collect();
let inputs: Vec<&[u8]> = inputs.iter().map(|v| &v[..]).collect();
let hash1 = hasher.hash_bytes_be(inputs.as_slice()).unwrap();

for greater_arr_len in smaller_arr_len + 1..32 {
let inputs: Vec<Vec<u8>> = inputs
.iter()
.map(|input| {
[vec![0u8; greater_arr_len - smaller_arr_len], input.to_vec()].concat()
})
.collect();
let inputs: Vec<&[u8]> = inputs.iter().map(|v| &v[..]).collect();
let hash = hasher.hash_bytes_be(inputs.as_slice()).unwrap();

assert_eq!(
hash, hash1,
"inputs: {nr_inputs}, smaller array length: {smaller_arr_len}, greater array length: {greater_arr_len}"
);
}
}
}
}

#[test]
fn test_poseidon_bn254_x5_fq_hash_bytes_le_smaller_arrays_random() {
for nr_inputs in 1..12 {
let mut hasher = Poseidon::<Fr>::new_circom(nr_inputs).unwrap();
for smaller_arr_len in 1..31 {
let inputs: Vec<Vec<u8>> = (0..nr_inputs)
.map(|_| {
let rng = rand::thread_rng();
rng.sample_iter(rand::distributions::Standard)
.take(smaller_arr_len)
.collect()
})
.collect();
let inputs: Vec<&[u8]> = inputs.iter().map(|v| &v[..]).collect();
let hash1 = hasher.hash_bytes_le(inputs.as_slice()).unwrap();

for greater_arr_len in smaller_arr_len + 1..32 {
let inputs: Vec<Vec<u8>> = inputs
.iter()
.map(|input| {
[input.to_vec(), vec![0u8; greater_arr_len - smaller_arr_len]].concat()
})
.collect();
let inputs: Vec<&[u8]> = inputs.iter().map(|v| &v[..]).collect();
let hash = hasher.hash_bytes_le(inputs.as_slice()).unwrap();

assert_eq!(
hash, hash1,
"inputs: {nr_inputs}, smaller array length: {smaller_arr_len}, greater array length: {greater_arr_len}"
);
}
}
}
}

macro_rules! test_random_input_same_results {
($name:ident, $method:ident) => {
#[test]
Expand Down

0 comments on commit 7598f97

Please sign in to comment.