Skip to content

Commit

Permalink
implement test to compare hash_no_pad behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
iammadab committed Sep 5, 2024
1 parent 1f4fabd commit 206170d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions poseidon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ license.workspace = true
goldilocks.workspace = true
serde.workspace = true
unroll = "0.1.5"

[dev-dependencies]
plonky2 = "0.2.2"
rand = "0.8.5"
1 change: 1 addition & 0 deletions poseidon/src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::constants::DIGEST_WIDTH;
use goldilocks::SmallField;
use serde::Serialize;

#[derive(Debug)]
pub struct Digest<F: SmallField + Serialize>(pub [F; DIGEST_WIDTH]);

impl<F: SmallField> TryFrom<Vec<F>> for Digest<F> {
Expand Down
53 changes: 53 additions & 0 deletions poseidon/src/poseidon_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,56 @@ pub fn compress<F: Poseidon>(x: Digest<F>, y: Digest<F>) -> Digest<F> {

Digest(perm.squeeze()[..DIGEST_WIDTH].try_into().unwrap())
}

#[cfg(test)]
mod tests {
use crate::{digest::Digest, poseidon_hash::PoseidonHash};
use goldilocks::Goldilocks;
use plonky2::{
field::{
goldilocks_field::GoldilocksField,
types::{PrimeField64, Sample},
},
hash::{hash_types::HashOut, poseidon::PoseidonHash as PlonkyPoseidonHash},
plonk::config::{GenericHashOut, Hasher},
};
use rand::{thread_rng, Rng};

type PlonkyFieldElements = Vec<GoldilocksField>;
type CenoFieldElements = Vec<Goldilocks>;

fn ceno_goldy_from_plonky_goldy(values: &[GoldilocksField]) -> Vec<Goldilocks> {
values
.iter()
.map(|value| Goldilocks(value.to_canonical_u64()))
.collect()
}

fn n_test_vectors(n: usize) -> (PlonkyFieldElements, CenoFieldElements) {
let plonky_elems = GoldilocksField::rand_vec(n);
let ceno_elems = ceno_goldy_from_plonky_goldy(plonky_elems.as_slice());
(plonky_elems, ceno_elems)
}

fn compare_hash_output(
plonky_hash: HashOut<GoldilocksField>,
ceno_hash: Digest<Goldilocks>,
) -> bool {
let plonky_elems = plonky_hash.to_vec();
let plonky_in_ceno_field = ceno_goldy_from_plonky_goldy(plonky_elems.as_slice());
plonky_in_ceno_field == ceno_hash.elements()
}

#[test]
fn compare_hash_no_pad() {
let mut rng = thread_rng();

for i in 0..100 {
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));
}
}
}

0 comments on commit 206170d

Please sign in to comment.