Skip to content

Commit

Permalink
Merge pull request AleoNet#2240 from ljedrz/perf/verification_mem
Browse files Browse the repository at this point in the history
Reduce max RAM when verifying deployment transactions
  • Loading branch information
howardwu authored Dec 27, 2023
2 parents 6c90b7a + ed22804 commit 1191ccc
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 49 deletions.
4 changes: 2 additions & 2 deletions algorithms/src/fft/polynomial/multiplier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ impl<'a, F: PrimeField> PolyMultiplier<'a, F> {
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;
let mut p = p.into_owned().coeffs;
p.resize(domain.size(), F::zero());
domain.out_order_fft_in_place_with_pc(&mut p, fft_pc);
p
})
}
for (_, e) in self.evaluations {
pool.add_job(move || {
let mut e = e.clone().into_owned().evaluations;
let mut e = e.into_owned().evaluations;
e.resize(domain.size(), F::zero());
crate::fft::domain::derange(&mut e);
e
Expand Down
23 changes: 1 addition & 22 deletions algorithms/src/snark/varuna/ahp/indexer/constraint_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{
r1cs::{errors::SynthesisError, ConstraintSystem as CS, Index as VarIndex, LinearCombination, Variable},
snark::varuna::ahp::matrices::to_matrix_helper,
};
use crate::r1cs::{errors::SynthesisError, ConstraintSystem as CS, Index as VarIndex, LinearCombination, Variable};
use snarkvm_fields::Field;
use snarkvm_utilities::serialize::*;

Expand Down Expand Up @@ -44,24 +41,6 @@ impl<F: Field> ConstraintSystem<F> {
}
}

#[inline]
/// Returns the sparse A matrix as Vec of rows, where each row is a Vec of assigned value and variable index
pub(crate) fn a_matrix(&self) -> Result<Vec<Vec<(F, usize)>>> {
to_matrix_helper(&self.a, self.num_public_variables)
}

#[inline]
/// Returns the sparse B matrix as Vec of rows, where each row is a Vec of assigned value and variable index
pub(crate) fn b_matrix(&self) -> Result<Vec<Vec<(F, usize)>>> {
to_matrix_helper(&self.b, self.num_public_variables)
}

#[inline]
/// Returns the sparse C matrix as Vec of rows, where each row is a Vec of assigned value and variable index
pub(crate) fn c_matrix(&self) -> Result<Vec<Vec<(F, usize)>>> {
to_matrix_helper(&self.c, self.num_public_variables)
}

#[inline]
fn make_row(l: &LinearCombination<F>) -> Vec<(F, VarIndex)> {
l.as_ref().iter().map(|(var, coeff)| (*coeff, var.get_unchecked())).collect()
Expand Down
16 changes: 8 additions & 8 deletions algorithms/src/snark/varuna/ahp/indexer/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
use crate::{
fft::EvaluationDomain,
polycommit::sonic_pc::{LinearCombination, PolynomialInfo, PolynomialLabel},
r1cs::{errors::SynthesisError, ConstraintSynthesizer, ConstraintSystem},
r1cs::{errors::SynthesisError, ConstraintSynthesizer},
snark::varuna::{
ahp::{
indexer::{Circuit, CircuitId, CircuitInfo, ConstraintSystem as IndexerConstraintSystem},
AHPForR1CS,
},
matrices::{matrix_evals, MatrixEvals},
matrices::{into_matrix_helper, matrix_evals, MatrixEvals},
num_non_zero,
SNARKMode,
},
Expand Down Expand Up @@ -140,15 +140,15 @@ impl<F: PrimeField, SM: SNARKMode> AHPForR1CS<F, SM> {

crate::snark::varuna::ahp::matrices::pad_input_for_indexer_and_prover(&mut ics)?;

let a = ics.a_matrix()?;
let b = ics.b_matrix()?;
let c = ics.c_matrix()?;
let IndexerConstraintSystem { a, b, c, num_public_variables, num_private_variables, num_constraints } = ics;

let a = into_matrix_helper(a, num_public_variables)?;
let b = into_matrix_helper(b, num_public_variables)?;
let c = into_matrix_helper(c, num_public_variables)?;

end_timer!(padding_time);

let num_padded_public_variables = ics.num_public_variables();
let num_private_variables = ics.num_private_variables();
let num_constraints = ics.num_constraints();
let num_padded_public_variables = num_public_variables;
let num_non_zero_a = num_non_zero(&a);
let num_non_zero_b = num_non_zero(&b);
let num_non_zero_c = num_non_zero(&c);
Expand Down
26 changes: 15 additions & 11 deletions algorithms/src/snark/varuna/ahp/matrices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ use crate::{
},
};
use snarkvm_fields::{Field, PrimeField};
use snarkvm_utilities::{cfg_iter, cfg_iter_mut, serialize::*};
use snarkvm_utilities::{cfg_into_iter, cfg_iter, cfg_iter_mut, serialize::*};

use anyhow::{anyhow, ensure, Result};
use std::collections::BTreeMap;

#[cfg(feature = "serial")]
use itertools::Itertools;
Expand All @@ -36,22 +35,27 @@ use rayon::prelude::*;

// This function converts a matrix output by Zexe's constraint infrastructure
// to the one used in this crate.
pub(crate) fn to_matrix_helper<F: Field>(
matrix: &[Vec<(F, VarIndex)>],
pub(crate) fn into_matrix_helper<F: Field>(
matrix: Vec<Vec<(F, VarIndex)>>,
num_input_variables: usize,
) -> Result<Matrix<F>> {
cfg_iter!(matrix)
cfg_into_iter!(matrix)
.map(|row| {
let mut row_map = BTreeMap::new();
for (val, column) in row.iter() {
ensure!(*val != F::zero(), "matrix entries should be non-zero");
let mut row_map = Vec::with_capacity(row.len());
for (val, column) in row {
ensure!(val != F::zero(), "matrix entries should be non-zero");
let column = match column {
VarIndex::Public(i) => *i,
VarIndex::Public(i) => i,
VarIndex::Private(i) => num_input_variables + i,
};
*row_map.entry(column).or_insert_with(F::zero) += *val;
match row_map.binary_search_by_key(&column, |(_, c)| *c) {
Ok(idx) => row_map[idx].0 += val,
Err(idx) => {
row_map.insert(idx, (val, column));
}
}
}
Ok(row_map.into_iter().map(|(column, coeff)| (coeff, column)).collect())
Ok(row_map)
})
.collect()
}
Expand Down
13 changes: 7 additions & 6 deletions circuit/environment/src/helpers/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::Index;
use snarkvm_fields::PrimeField;

use indexmap::IndexMap;
use std::sync::Arc;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum AssignmentVariable<F: PrimeField> {
Expand Down Expand Up @@ -84,9 +85,9 @@ impl<F: PrimeField> AssignmentLC<F> {
/// and constraint assignments.
#[derive(Clone, Debug)]
pub struct Assignment<F: PrimeField> {
public: Vec<(Index, F)>,
private: Vec<(Index, F)>,
constraints: Vec<(AssignmentLC<F>, AssignmentLC<F>, AssignmentLC<F>)>,
public: Arc<[(Index, F)]>,
private: Arc<[(Index, F)]>,
constraints: Arc<[(AssignmentLC<F>, AssignmentLC<F>, AssignmentLC<F>)]>,
}

impl<F: PrimeField> From<crate::R1CS<F>> for Assignment<F> {
Expand All @@ -109,17 +110,17 @@ impl<F: PrimeField> From<crate::R1CS<F>> for Assignment<F> {

impl<F: PrimeField> Assignment<F> {
/// Returns the public inputs of the assignment.
pub const fn public_inputs(&self) -> &Vec<(Index, F)> {
pub const fn public_inputs(&self) -> &Arc<[(Index, F)]> {
&self.public
}

/// Returns the private inputs of the assignment.
pub const fn private_inputs(&self) -> &Vec<(Index, F)> {
pub const fn private_inputs(&self) -> &Arc<[(Index, F)]> {
&self.private
}

/// Returns the constraints of the assignment.
pub const fn constraints(&self) -> &Vec<(AssignmentLC<F>, AssignmentLC<F>, AssignmentLC<F>)> {
pub const fn constraints(&self) -> &Arc<[(AssignmentLC<F>, AssignmentLC<F>, AssignmentLC<F>)]> {
&self.constraints
}

Expand Down

0 comments on commit 1191ccc

Please sign in to comment.