PR feedback

This commit is contained in:
wborgeaud 2022-02-12 15:18:20 +01:00
parent f7256a6efc
commit 736b65b0a7
2 changed files with 7 additions and 12 deletions

View File

@ -12,16 +12,6 @@ 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 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: &[F]) -> HashOut<F> {
if inputs.len() <= 4 {
HashOut::from_partial(inputs)
} else {
hash_n_to_hash_no_pad::<F, P>(inputs)
}
}
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();

View File

@ -3,6 +3,7 @@ use std::fmt::Debug;
use plonky2_field::extension_field::quadratic::QuadraticExtension;
use plonky2_field::extension_field::{Extendable, FieldExtension};
use plonky2_field::goldilocks_field::GoldilocksField;
use plonky2_util::ceil_div_usize;
use serde::{de::DeserializeOwned, Serialize};
use crate::hash::hash_types::HashOut;
@ -49,8 +50,12 @@ pub trait Hasher<F: RichField>: Sized + Clone + Debug + Eq + PartialEq {
/// 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();
if inputs.len() * ceil_div_usize(F::BITS, 8) <= Self::HASH_SIZE {
let mut inputs_bytes = inputs
.iter()
.flat_map(|x| x.to_canonical_u64().to_le_bytes())
.collect::<Vec<_>>();
inputs_bytes.resize(Self::HASH_SIZE, 0);
Self::Hash::from_bytes(&inputs_bytes)
} else {
Self::hash_no_pad(inputs)