Merge pull request #478 from mir-protocol/from_partial_use_slice

`from_partial` now takes in a slice
This commit is contained in:
BGluth 2022-02-10 07:48:55 -08:00 committed by GitHub
commit 387ce463fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 20 deletions

View File

@ -31,14 +31,10 @@ impl<F: Field> HashOut<F> {
}
}
pub fn from_partial(mut elements: Vec<F>) -> 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<R: Rng>(rng: &mut R) -> Self {
@ -104,14 +100,10 @@ impl HashOutTarget {
}
}
pub fn from_partial(mut elements: Vec<Target>, 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 }
}
}

View File

@ -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<F: RichField, P: PlonkyPermutation<F>>(inputs: Vec<F>) -> HashOut<F> {
pub fn hash_or_noop<F: RichField, P: PlonkyPermutation<F>>(inputs: &[F]) -> HashOut<F> {
if inputs.len() <= 4 {
HashOut::from_partial(inputs)
} else {
hash_n_to_hash_no_pad::<F, P>(&inputs)
hash_n_to_hash_no_pad::<F, P>(inputs)
}
}
@ -26,7 +26,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
pub fn hash_or_noop<H: AlgebraicHasher<F>>(&mut self, inputs: Vec<Target>) -> 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::<H>(inputs)
}