Skip to content

Commit

Permalink
Merge pull request #2175 from ljedrz/perf/fewer_verify_allocs
Browse files Browse the repository at this point in the history
Fewer prove/verify allocations
  • Loading branch information
howardwu authored Nov 24, 2023
2 parents 6e14400 + ad69aea commit 052cbd9
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion algorithms/src/crypto_hash/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl<F: PrimeField, const RATE: usize> PoseidonSponge<F, RATE, 1> {
};
let bits = self.get_bits(num_bits_per_nonnative * num_elements);

let mut lookup_table = Vec::<TargetField>::new();
let mut lookup_table = Vec::<TargetField>::with_capacity(num_bits_per_nonnative);
let mut cur = TargetField::one();
for _ in 0..num_bits_per_nonnative {
lookup_table.push(cur);
Expand Down
4 changes: 2 additions & 2 deletions algorithms/src/fft/polynomial/multiplier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<'a, F: PrimeField> PolyMultiplier<'a, F> {
}
let fft_pc = &self.fft_precomputation.unwrap();
let ifft_pc = &self.ifft_precomputation.unwrap();
let mut pool = ExecutionPool::new();
let mut pool = ExecutionPool::with_capacity(self.polynomials.len() + self.evaluations.len());
for (_, p) in self.polynomials {
pool.add_job(move || {
let mut p = p.clone().into_owned().coeffs;
Expand Down Expand Up @@ -146,7 +146,7 @@ impl<'a, F: PrimeField> PolyMultiplier<'a, F> {
Some(Cow::Owned(self.fft_precomputation.as_ref().unwrap().to_ifft_precomputation()));
}
let fft_pc = self.fft_precomputation.as_ref().unwrap();
let mut pool = ExecutionPool::new();
let mut pool = ExecutionPool::with_capacity(self.polynomials.len() + self.evaluations.len());
for (l, p) in self.polynomials {
pool.add_job(move || {
let mut p = p.clone().into_owned().coeffs;
Expand Down
2 changes: 1 addition & 1 deletion algorithms/src/msm/variable_base/batched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub(super) fn batch_add<G: AffineCurve>(
let mut number_of_bases_in_batch = 0;

let mut instr = Vec::<(u32, u32)>::with_capacity(batch_size);
let mut new_bases = Vec::with_capacity(bases.len() * 3 / 8);
let mut new_bases = Vec::with_capacity(bases.len());
let mut scratch_space = Vec::with_capacity(batch_size / 2);

// In the first loop, copy the results of the first in-place addition tree to the vector `new_bases`.
Expand Down
6 changes: 4 additions & 2 deletions algorithms/src/snark/varuna/ahp/ahp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use snarkvm_fields::{Field, PrimeField};

use core::{borrow::Borrow, marker::PhantomData};
use itertools::Itertools;
use std::collections::BTreeMap;
use std::{collections::BTreeMap, fmt::Write};

/// The algebraic holographic proof defined in [CHMMVW19](https://eprint.iacr.org/2019/1047).
/// Currently, this AHP only supports inputs of size one
Expand All @@ -43,7 +43,9 @@ pub struct AHPForR1CS<F: Field, SM: SNARKMode> {
}

pub(crate) fn witness_label(circuit_id: CircuitId, poly: &str, i: usize) -> String {
format!("circuit_{circuit_id}_{poly}_{i:0>8}")
let mut label = String::with_capacity(82 + poly.len());
let _ = write!(&mut label, "circuit_{circuit_id}_{poly}_{i:0>8}");
label
}

pub(crate) struct NonZeroDomains<F: PrimeField> {
Expand Down
2 changes: 1 addition & 1 deletion curves/src/templates/bls12/g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl<P: Bls12Parameters> G2Prepared<P> {
let mut r = G2HomProjective { x: q.x, y: q.y, z: Fp2::one() };

let bit_iterator = BitIteratorBE::new(P::X);
let mut ell_coeffs = Vec::with_capacity(bit_iterator.len());
let mut ell_coeffs = Vec::with_capacity(bit_iterator.len() * 3 / 2);

// `one_half` = 1/2 in the field.
let one_half = P::Fp::half();
Expand Down
12 changes: 11 additions & 1 deletion fields/src/traits/poseidon_grain_lfsr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ impl PoseidonGrainLFSR {
let mut output = Vec::with_capacity(num_elems);
for _ in 0..num_elems {
// Obtain `n` bits and make it most-significant-bit first.
let mut bits = self.get_bits(self.field_size_in_bits as usize).collect::<Vec<_>>();
let bits_iter = self.get_bits(self.field_size_in_bits as usize);
let mut bits = Vec::with_capacity(bits_iter.len());
for bit in bits_iter {
bits.push(bit);
}
bits.reverse();

let bytes = bits
Expand Down Expand Up @@ -199,3 +203,9 @@ impl<'a> Iterator for LFSRIter<'a> {
}
}
}

impl<'a> ExactSizeIterator for LFSRIter<'a> {
fn len(&self) -> usize {
self.num_bits
}
}
1 change: 1 addition & 0 deletions utilities/src/serialize/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ impl<T: CanonicalDeserialize> CanonicalDeserialize for Vec<T> {
) -> Result<Self, SerializationError> {
let len = u64::deserialize_with_mode(&mut reader, compress, validate)?;
let mut values = Vec::new();
let _ = values.try_reserve(len as usize);
for _ in 0..len {
values.push(T::deserialize_with_mode(&mut reader, compress, Validate::No)?);
}
Expand Down

0 comments on commit 052cbd9

Please sign in to comment.