Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test): Fuzz test poseidon2 hash equivalence #6265

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions acvm-repo/bn254_blackbox_solver/src/poseidon2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,23 @@ impl<'a> Poseidon2<'a> {
}

/// Performs a poseidon hash with a sponge construction equivalent to the one in poseidon2.nr
pub fn poseidon_hash(inputs: &[FieldElement]) -> Result<FieldElement, BlackBoxResolutionError> {
///
/// The `is_variable_length` parameter is there to so we can produce an equivalent hash with
/// the Barretenberg implementation which distinguishes between variable and fixed length inputs.
/// Set it to true if the input length matches the static size expected by the Noir function.
pub fn poseidon_hash(
inputs: &[FieldElement],
is_variable_length: bool,
) -> Result<FieldElement, BlackBoxResolutionError> {
let two_pow_64 = 18446744073709551616_u128.into();
let iv = FieldElement::from(inputs.len()) * two_pow_64;
let mut sponge = Poseidon2Sponge::new(iv, 3);
for input in inputs.iter() {
sponge.absorb(*input)?;
}
if is_variable_length {
sponge.absorb(FieldElement::from(1u32))?;
}
sponge.squeeze()
}

Expand Down Expand Up @@ -640,7 +650,7 @@ mod test {
FieldElement::from(3u128),
FieldElement::from(4u128),
];
let result = super::poseidon_hash(&fields).expect("should hash successfully");
let result = super::poseidon_hash(&fields, false).expect("should hash successfully");
assert_eq!(
result,
field_from_hex("130bf204a32cac1f0ace56c78b731aa3809f06df2731ebcf6b3464a15788b1b9"),
Expand Down
44 changes: 43 additions & 1 deletion tooling/nargo_cli/tests/stdlib-props.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{cell::RefCell, collections::BTreeMap, path::Path};

use acvm::{acir::native_types::WitnessStack, FieldElement};
use acvm::{acir::native_types::WitnessStack, AcirField, FieldElement};
use iter_extended::vecmap;
use nargo::{
ops::{execute_program, DefaultForeignCallExecutor},
parse_all,
Expand Down Expand Up @@ -254,6 +255,47 @@ fn fuzz_sha512_equivalence() {
);
}

#[test]
fn fuzz_poseidon2_equivalence() {
use bn254_blackbox_solver::poseidon_hash;

for max_len in [0, 1, 3, 4, 511, 512] {
let source = format!(
"fn main(input: [Field; {max_len}], message_size: u32) -> pub Field {{
std::hash::poseidon2::Poseidon2::hash(input, message_size)
}}"
);

let strategy = (0..=max_len)
.prop_flat_map(|len: usize| {
// Generate Field elements from random 32 byte vectors.
let field = prop::collection::vec(any::<u8>(), 32)
.prop_map(|bytes| FieldElement::from_be_bytes_reduce(&bytes));

prop::collection::vec(field, len)
})
.prop_map(move |mut msg| {
// The output hash is a single field element.
let output = poseidon_hash(&msg, msg.len() < max_len).expect("failed to hash");

// The input has to be padded to the maximum length.
let msg_size = msg.len();
msg.resize(max_len, FieldElement::from(0u64));

let inputs = vec![
("input", InputValue::Vec(vecmap(msg, InputValue::Field))),
("message_size", InputValue::Field(FieldElement::from(msg_size))),
];

SnippetInputOutput::new(inputs, InputValue::Field(output))
.with_description(format!("max_len = {max_len}"))
})
.boxed();

run_snippet_proptest(source.clone(), false, strategy);
}
}

fn bytes_input(bytes: &[u8]) -> InputValue {
InputValue::Vec(
bytes.iter().map(|b| InputValue::Field(FieldElement::from(*b as u32))).collect(),
Expand Down
Loading