diff --git a/plonky2/src/hash/merkle_proofs.rs b/plonky2/src/hash/merkle_proofs.rs index f90f0657..7ef81570 100644 --- a/plonky2/src/hash/merkle_proofs.rs +++ b/plonky2/src/hash/merkle_proofs.rs @@ -32,7 +32,7 @@ pub(crate) fn verify_merkle_proof>( proof: &MerkleProof, ) -> Result<()> { let mut index = leaf_index; - let mut current_digest = H::hash_no_pad(&leaf_data); + let mut current_digest = H::hash_or_noop(&leaf_data); for &sibling_digest in proof.siblings.iter() { let bit = index & 1; index >>= 1; diff --git a/plonky2/src/plonk/config.rs b/plonky2/src/plonk/config.rs index fdca7037..76891240 100644 --- a/plonky2/src/plonk/config.rs +++ b/plonky2/src/plonk/config.rs @@ -46,6 +46,17 @@ pub trait Hasher: Sized + Clone + Debug + Eq + PartialEq { Self::hash_no_pad(&padded_input) } + /// Hash the slice if necessary to reduce its length to ~256 bits. If it already fits, this is a + /// no-op. + fn hash_or_noop(inputs: &[F]) -> Self::Hash { + if inputs.len() <= 4 { + let inputs_bytes = HashOut::from_partial(inputs).to_bytes(); + Self::Hash::from_bytes(&inputs_bytes) + } else { + Self::hash_no_pad(inputs) + } + } + fn two_to_one(left: Self::Hash, right: Self::Hash) -> Self::Hash; }