Skip to content

Commit

Permalink
add differential testing
Browse files Browse the repository at this point in the history
  • Loading branch information
iammadab committed Sep 5, 2024
1 parent 206170d commit fa3c707
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions poseidon/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate core;

pub(crate) mod constants;
pub(crate) mod digest;
pub(crate) mod poseidon;
Expand Down
44 changes: 40 additions & 4 deletions poseidon/src/poseidon_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ impl PoseidonHash {
compress(left, right)
}

fn hash_or_noop<F: Poseidon + AdaptedField>(inputs: Vec<F>) -> Digest<F> {
fn hash_or_noop<F: Poseidon + AdaptedField>(inputs: &[F]) -> Digest<F> {
if inputs.len() <= DIGEST_WIDTH {
Digest::from_partial(inputs.as_slice())
Digest::from_partial(inputs)
} else {
hash_n_to_hash_no_pad(inputs.as_slice())
hash_n_to_hash_no_pad(inputs)
}
}
}
Expand Down Expand Up @@ -86,6 +86,8 @@ mod tests {
type PlonkyFieldElements = Vec<GoldilocksField>;
type CenoFieldElements = Vec<Goldilocks>;

const N_ITERATIONS: usize = 100;

fn ceno_goldy_from_plonky_goldy(values: &[GoldilocksField]) -> Vec<Goldilocks> {
values
.iter()
Expand All @@ -99,6 +101,16 @@ mod tests {
(plonky_elems, ceno_elems)
}

fn random_hash_pair() -> (HashOut<GoldilocksField>, Digest<Goldilocks>) {
let plonky_random_hash = HashOut::<GoldilocksField>::rand();
let ceno_equivalent_hash = Digest(
ceno_goldy_from_plonky_goldy(plonky_random_hash.elements.as_slice())
.try_into()
.unwrap(),
);
(plonky_random_hash, ceno_equivalent_hash)
}

fn compare_hash_output(
plonky_hash: HashOut<GoldilocksField>,
ceno_hash: Digest<Goldilocks>,
Expand All @@ -112,12 +124,36 @@ mod tests {
fn compare_hash_no_pad() {
let mut rng = thread_rng();

for i in 0..100 {
for i in 0..N_ITERATIONS {
let n: usize = rng.gen_range(0..=100);
let (plonky_elems, ceno_elems) = n_test_vectors(n);
let plonky_out = PlonkyPoseidonHash::hash_no_pad(plonky_elems.as_slice());
let ceno_out = PoseidonHash::hash_no_pad(ceno_elems.as_slice());
assert!(compare_hash_output(plonky_out, ceno_out));
}
}

#[test]
fn compare_hash_or_noop() {
let mut rng = thread_rng();
for i in 0..N_ITERATIONS {
let n = rng.gen_range(0..=100);
let (plonky_elems, ceno_elems) = n_test_vectors(n);
let plonky_out = PlonkyPoseidonHash::hash_or_noop(plonky_elems.as_slice());
let ceno_out = PoseidonHash::hash_or_noop(ceno_elems.as_slice());
assert!(compare_hash_output(plonky_out, ceno_out));
}
}

#[test]
fn compare_two_to_one() {
let mut rng = thread_rng();
for i in 0..N_ITERATIONS {
let (plonky_hash_a, ceno_hash_a) = random_hash_pair();
let (plonky_hash_b, ceno_hash_b) = random_hash_pair();
let plonky_combined = PlonkyPoseidonHash::two_to_one(plonky_hash_a, plonky_hash_b);
let ceno_combined = PoseidonHash::two_to_one(ceno_hash_a, ceno_hash_b);
assert!(compare_hash_output(plonky_combined, ceno_combined));
}
}
}

0 comments on commit fa3c707

Please sign in to comment.