Skip to content
Closed
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
10 changes: 2 additions & 8 deletions acvm-repo/bn254_blackbox_solver/src/poseidon2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,19 +548,13 @@ impl<'a> Poseidon2<'a> {
/// 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> {
pub fn poseidon_hash(inputs: &[FieldElement]) -> 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 @@ -650,7 +644,7 @@ mod test {
FieldElement::from(3u128),
FieldElement::from(4u128),
];
let result = super::poseidon_hash(&fields, false).expect("should hash successfully");
let result = super::poseidon_hash(&fields).expect("should hash successfully");
assert_eq!(
result,
field_from_hex("130bf204a32cac1f0ace56c78b731aa3809f06df2731ebcf6b3464a15788b1b9"),
Expand Down
14 changes: 2 additions & 12 deletions noir_stdlib/src/hash/poseidon2.nr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Poseidon2 {
impl Poseidon2 {
#[no_predicates]
pub fn hash<let N: u32>(input: [Field; N], message_size: u32) -> Field {
Poseidon2::hash_internal(input, message_size, message_size != N)
Poseidon2::hash_internal(input, message_size)
}

pub fn new(iv: Field) -> Poseidon2 {
Expand Down Expand Up @@ -59,11 +59,7 @@ impl Poseidon2 {
self.state[0]
}

fn hash_internal<let N: u32>(
input: [Field; N],
in_len: u32,
is_variable_length: bool,
) -> Field {
fn hash_internal<let N: u32>(input: [Field; N], in_len: u32) -> Field {
let two_pow_64 = 18446744073709551616;
let iv: Field = (in_len as Field) * two_pow_64;
let mut sponge = Poseidon2::new(iv);
Expand All @@ -73,12 +69,6 @@ impl Poseidon2 {
}
}

// In the case where the hash preimage is variable-length, we append `1` to the end of the input, to distinguish
// from fixed-length hashes. (the combination of this additional field element + the hash IV ensures
// fixed-length and variable-length hashes do not collide)
if is_variable_length {
sponge.absorb(1);
}
sponge.squeeze()
}
}
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo_cli/tests/stdlib-props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn fuzz_poseidon2_equivalence() {
let strategy = (0..=max_len)
.prop_flat_map(field_vec_strategy)
.prop_map(move |mut msg| {
let output = poseidon_hash(&msg, msg.len() < max_len).expect("failed to hash");
let output = poseidon_hash(&msg).expect("failed to hash");

// The input has to be padded to the maximum length.
let msg_size = msg.len();
Expand Down