From adf5444f3fbd7e5011c3b0cd7f5f9a0efdbdb764 Mon Sep 17 00:00:00 2001 From: BGluth Date: Wed, 9 Feb 2022 18:31:58 -0700 Subject: [PATCH 1/2] `from_partial` (non-target) now takes in a slice - Doesn't need to take in a `Vec`. --- plonky2/src/hash/hash_types.rs | 14 ++++++-------- plonky2/src/hash/hashing.rs | 4 ++-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/plonky2/src/hash/hash_types.rs b/plonky2/src/hash/hash_types.rs index ed6fca43..8187979b 100644 --- a/plonky2/src/hash/hash_types.rs +++ b/plonky2/src/hash/hash_types.rs @@ -31,14 +31,12 @@ 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 { + debug_assert!(elements_in.len() <= 4); + + 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 { diff --git a/plonky2/src/hash/hashing.rs b/plonky2/src/hash/hashing.rs index ea205654..eb238e51 100644 --- a/plonky2/src/hash/hashing.rs +++ b/plonky2/src/hash/hashing.rs @@ -14,11 +14,11 @@ 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 /// 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) } } From b2c747b171df954757d64a0fad4482d235e22a1d Mon Sep 17 00:00:00 2001 From: BGluth Date: Wed, 9 Feb 2022 23:34:26 -0700 Subject: [PATCH 2/2] Also did the same to the circuit version - And removed the `debug_assert!`. --- plonky2/src/hash/hash_types.rs | 14 ++++---------- plonky2/src/hash/hashing.rs | 4 ++-- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/plonky2/src/hash/hash_types.rs b/plonky2/src/hash/hash_types.rs index 8187979b..f7605306 100644 --- a/plonky2/src/hash/hash_types.rs +++ b/plonky2/src/hash/hash_types.rs @@ -32,8 +32,6 @@ impl HashOut { } pub fn from_partial(elements_in: &[F]) -> Self { - debug_assert!(elements_in.len() <= 4); - let mut elements = [F::ZERO; 4]; elements[0..elements_in.len()].copy_from_slice(elements_in); Self { elements } @@ -102,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 eb238e51..468bd1b8 100644 --- a/plonky2/src/hash/hashing.rs +++ b/plonky2/src/hash/hashing.rs @@ -12,7 +12,7 @@ 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: &[F]) -> HashOut { if inputs.len() <= 4 { @@ -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) }