diff --git a/plonky2/src/hash/hash_types.rs b/plonky2/src/hash/hash_types.rs index 0a1cedd0..01062960 100644 --- a/plonky2/src/hash/hash_types.rs +++ b/plonky2/src/hash/hash_types.rs @@ -31,14 +31,10 @@ impl HashOut { } } - pub fn from_partial(mut elements: Vec) -> Self { - debug_assert!(elements.len() <= 4); - while elements.len() < 4 { - elements.push(F::ZERO); - } - Self { - elements: [elements[0], elements[1], elements[2], elements[3]], - } + pub fn from_partial(elements_in: &[F]) -> Self { + let mut elements = [F::ZERO; 4]; + elements[0..elements_in.len()].copy_from_slice(elements_in); + Self { elements } } pub fn rand_from_rng(rng: &mut R) -> Self { @@ -104,14 +100,10 @@ impl HashOutTarget { } } - pub fn from_partial(mut elements: Vec, zero: Target) -> Self { - debug_assert!(elements.len() <= 4); - while elements.len() < 4 { - elements.push(zero); - } - Self { - elements: [elements[0], elements[1], elements[2], elements[3]], - } + pub fn from_partial(elements_in: &[Target], zero: Target) -> Self { + let mut elements = [zero; 4]; + elements[0..elements_in.len()].copy_from_slice(elements_in); + Self { elements } } } diff --git a/plonky2/src/hash/hashing.rs b/plonky2/src/hash/hashing.rs index ea205654..468bd1b8 100644 --- a/plonky2/src/hash/hashing.rs +++ b/plonky2/src/hash/hashing.rs @@ -12,13 +12,13 @@ pub(crate) const SPONGE_RATE: usize = 8; pub(crate) const SPONGE_CAPACITY: usize = 4; pub const SPONGE_WIDTH: usize = SPONGE_RATE + SPONGE_CAPACITY; -/// Hash the vector if necessary to reduce its length to ~256 bits. If it already fits, this is a +/// Hash the slice if necessary to reduce its length to ~256 bits. If it already fits, this is a /// no-op. -pub fn hash_or_noop>(inputs: Vec) -> HashOut { +pub fn hash_or_noop>(inputs: &[F]) -> HashOut { if inputs.len() <= 4 { HashOut::from_partial(inputs) } else { - hash_n_to_hash_no_pad::(&inputs) + hash_n_to_hash_no_pad::(inputs) } } @@ -26,7 +26,7 @@ impl, const D: usize> CircuitBuilder { pub fn hash_or_noop>(&mut self, inputs: Vec) -> HashOutTarget { let zero = self.zero(); if inputs.len() <= 4 { - HashOutTarget::from_partial(inputs, zero) + HashOutTarget::from_partial(&inputs, zero) } else { self.hash_n_to_hash_no_pad::(inputs) }