Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions crypto/crypto/src/merkle_tree/backends/field_element_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ impl<F, D: Digest, const NUM_BYTES: usize> Default for FieldElementVectorBackend
}
}

impl<F, D: Digest, const NUM_BYTES: usize> FieldElementVectorBackend<F, D, NUM_BYTES>
where
[u8; NUM_BYTES]: From<Output<D>>,
{
/// Hash raw bytes using the same digest (`D`) as this backend's leaf hashing.
/// Enables callers to pre-serialize field elements into a byte buffer and hash
/// once, avoiding per-element allocations while staying consistent with the
/// backend's hash function.
pub fn hash_bytes(data: &[u8]) -> [u8; NUM_BYTES] {
Comment thread
MauroToscano marked this conversation as resolved.
let mut hasher = D::new();
hasher.update(data);
let mut result = [0u8; NUM_BYTES];
result.copy_from_slice(&hasher.finalize());
result
}
}

impl<F, D: Digest, const NUM_BYTES: usize> IsMerkleTreeBackend
for FieldElementVectorBackend<F, D, NUM_BYTES>
where
Expand Down
17 changes: 8 additions & 9 deletions crypto/stark/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::proof::stark::{DeepPolynomialOpenings, PolynomialOpenings};
use crate::table::Table;
use crate::trace::LDETraceTable;

use super::config::{BatchedMerkleTree, Commitment};
use super::config::{BatchedMerkleTree, BatchedMerkleTreeBackend, Commitment};
use super::constraints::evaluator::ConstraintEvaluator;
use super::domain::Domain;
use super::fri::fri_decommit::FriDecommitment;
Expand Down Expand Up @@ -373,7 +373,6 @@ pub trait IsStarkProver<
E: IsField,
{
use math::traits::WriteBytes;
use sha3::Digest;

if columns.is_empty() || columns[0].is_empty() {
return None;
Expand All @@ -383,6 +382,11 @@ pub trait IsStarkProver<
let num_cols = columns.len();
let byte_len = <FieldElement<E> as WriteBytes>::BYTE_LEN;

debug_assert!(
num_rows.is_power_of_two(),
"num_rows must be a power of two for reverse_index"
);

#[cfg(feature = "parallel")]
let iter = (0..num_rows).into_par_iter();
#[cfg(not(feature = "parallel"))]
Expand All @@ -399,9 +403,7 @@ pub trait IsStarkProver<
columns[col_idx][br_idx]
.write_bytes_be(&mut buf[col_idx * byte_len..(col_idx + 1) * byte_len]);
}
let mut hasher = sha3::Keccak256::new();
hasher.update(&buf);
hasher.finalize().into()
BatchedMerkleTreeBackend::<E>::hash_bytes(&buf)
})
.collect();

Expand Down Expand Up @@ -694,7 +696,6 @@ pub trait IsStarkProver<
FieldElement<FieldExtension>: AsBytes + Sync + Send + math::traits::WriteBytes,
{
use math::traits::WriteBytes;
use sha3::Digest;

let num_parts = lde_composition_poly_parts_evaluations.len();
if num_parts == 0 {
Expand Down Expand Up @@ -735,9 +736,7 @@ pub trait IsStarkProver<
part[br_1].write_bytes_be(&mut buf[offset..offset + byte_len]);
offset += byte_len;
}
let mut hasher = sha3::Keccak256::new();
hasher.update(&buf);
hasher.finalize().into()
BatchedMerkleTreeBackend::<FieldExtension>::hash_bytes(&buf)
})
.collect();

Expand Down
Loading