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

chore: do not differentiate fixed-length vs variable-length in Poseidon2 #7284

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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 @@ -78,8 +78,8 @@
Err(e) => panic!("failed to compile program; brillig = {force_brillig}:\n{source}\n{e:?}"),
};

let pedandic_solving = true;

Check warning on line 81 in tooling/nargo_cli/tests/stdlib-props.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (pedandic)
let blackbox_solver = bn254_blackbox_solver::Bn254BlackBoxSolver(pedandic_solving);

Check warning on line 82 in tooling/nargo_cli/tests/stdlib-props.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (pedandic)
let foreign_call_executor = RefCell::new(DefaultForeignCallBuilder::default().build());

// Generate multiple input/output
Expand Down Expand Up @@ -268,7 +268,7 @@
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 All @@ -294,7 +294,7 @@
use light_poseidon::{Poseidon, PoseidonHasher};

let poseidon_hash = |inputs: &[FieldElement]| {
let mut poseidon = Poseidon::<ark_bn254_v04::Fr>::new_circom(inputs.len()).unwrap();

Check warning on line 297 in tooling/nargo_cli/tests/stdlib-props.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (circom)
let frs: Vec<ark_bn254_v04::Fr> = inputs
.iter()
.map(|f| ark_bn254_v04::Fr::from_be_bytes_mod_order(&f.to_be_bytes()))
Expand Down
Loading