Skip to content

Commit

Permalink
Prevent addition overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
vicsn committed Sep 13, 2023
1 parent 11efdfb commit bf5619b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
17 changes: 8 additions & 9 deletions algorithms/src/snark/varuna/ahp/indexer/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,14 @@ impl<F: PrimeField, MM: SNARKMode> CanonicalSerialize for Circuit<F, MM> {

#[allow(unused_mut, unused_variables)]
fn serialized_size(&self, mode: Compress) -> usize {
let mut size = 0;
size += self.index_info.serialized_size(mode);
size += self.a.serialized_size(mode);
size += self.b.serialized_size(mode);
size += self.c.serialized_size(mode);
size += self.a_arith.serialized_size(mode);
size += self.b_arith.serialized_size(mode);
size += self.c_arith.serialized_size(mode);
size
0usize
.saturating_add(self.index_info.serialized_size(mode))
.saturating_add(self.a.serialized_size(mode))
.saturating_add(self.b.serialized_size(mode))
.saturating_add(self.c.serialized_size(mode))
.saturating_add(self.a_arith.serialized_size(mode))
.saturating_add(self.b_arith.serialized_size(mode))
.saturating_add(self.c_arith.serialized_size(mode))
}
}

Expand Down
5 changes: 3 additions & 2 deletions algorithms/src/snark/varuna/ahp/prover/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl<'a, F: PrimeField, MM: SNARKMode> State<'a, F, MM> {
let mut max_non_zero_domain: Option<EvaluationDomain<F>> = None;
let mut max_num_constraints = 0;
let mut max_num_variables = 0;
let mut total_instances = 0;
let mut total_instances = 0usize;
let circuit_specific_states = indices_and_assignments
.into_iter()
.map(|(circuit, variable_assignments)| {
Expand All @@ -124,7 +124,8 @@ impl<'a, F: PrimeField, MM: SNARKMode> State<'a, F, MM> {
let first_padded_public_inputs = &variable_assignments[0].0;
let input_domain = EvaluationDomain::new(first_padded_public_inputs.len()).unwrap();
let batch_size = variable_assignments.len();
total_instances += batch_size;
total_instances =
total_instances.checked_add(batch_size).ok_or_else(|| anyhow::anyhow!("Batch size too large"))?;
let mut z_as = Vec::with_capacity(batch_size);
let mut z_bs = Vec::with_capacity(batch_size);
let mut z_cs = Vec::with_capacity(batch_size);
Expand Down
30 changes: 13 additions & 17 deletions algorithms/src/snark/varuna/data_structures/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,15 @@ impl<E: PairingEngine> Commitments<E> {
}

fn serialized_size(&self, compress: Compress) -> usize {
let mut size = 0;
size += serialized_vec_size_without_len(&self.witness_commitments, compress);
size += CanonicalSerialize::serialized_size(&self.mask_poly, compress);
size += CanonicalSerialize::serialized_size(&self.h_0, compress);
size += CanonicalSerialize::serialized_size(&self.g_1, compress);
size += CanonicalSerialize::serialized_size(&self.h_1, compress);
size += serialized_vec_size_without_len(&self.g_a_commitments, compress);
size += serialized_vec_size_without_len(&self.g_b_commitments, compress);
size += serialized_vec_size_without_len(&self.g_c_commitments, compress);
size += CanonicalSerialize::serialized_size(&self.h_2, compress);
size
serialized_vec_size_without_len(&self.witness_commitments, compress)
.saturating_add(CanonicalSerialize::serialized_size(&self.mask_poly, compress))
.saturating_add(CanonicalSerialize::serialized_size(&self.h_0, compress))
.saturating_add(CanonicalSerialize::serialized_size(&self.g_1, compress))
.saturating_add(CanonicalSerialize::serialized_size(&self.h_1, compress))
.saturating_add(serialized_vec_size_without_len(&self.g_a_commitments, compress))
.saturating_add(serialized_vec_size_without_len(&self.g_b_commitments, compress))
.saturating_add(serialized_vec_size_without_len(&self.g_c_commitments, compress))
.saturating_add(CanonicalSerialize::serialized_size(&self.h_2, compress))
}

fn deserialize_with_mode<R: snarkvm_utilities::Read>(
Expand Down Expand Up @@ -140,12 +138,10 @@ impl<F: PrimeField> Evaluations<F> {
}

fn serialized_size(&self, compress: Compress) -> usize {
let mut size = 0;
size += CanonicalSerialize::serialized_size(&self.g_1_eval, compress);
size += serialized_vec_size_without_len(&self.g_a_evals, compress);
size += serialized_vec_size_without_len(&self.g_b_evals, compress);
size += serialized_vec_size_without_len(&self.g_c_evals, compress);
size
CanonicalSerialize::serialized_size(&self.g_1_eval, compress)
.saturating_add(serialized_vec_size_without_len(&self.g_a_evals, compress))
.saturating_add(serialized_vec_size_without_len(&self.g_b_evals, compress))
.saturating_add(serialized_vec_size_without_len(&self.g_c_evals, compress))
}

fn deserialize_with_mode<R: snarkvm_utilities::Read>(
Expand Down
5 changes: 3 additions & 2 deletions algorithms/src/snark/varuna/varuna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ where
let mut batch_sizes = BTreeMap::new();
let mut circuit_infos = BTreeMap::new();
let mut inputs_and_batch_sizes = BTreeMap::new();
let mut total_instances = 0;
let mut total_instances = 0usize;
let mut public_inputs = BTreeMap::new(); // inputs need to live longer than the rest of prover_state
let num_unique_circuits = keys_to_constraints.len();
let mut circuit_ids = Vec::with_capacity(num_unique_circuits);
Expand All @@ -371,8 +371,9 @@ where
batch_sizes.insert(circuit_id, batch_size);
circuit_infos.insert(circuit_id, &pk.circuit_verifying_key.circuit_info);
inputs_and_batch_sizes.insert(circuit_id, (batch_size, padded_public_input));
total_instances += batch_size;
public_inputs.insert(circuit_id, public_input);
total_instances = total_instances.saturating_add(batch_size);

circuit_ids.push(circuit_id);
}
assert_eq!(prover_state.total_instances, total_instances);
Expand Down

0 comments on commit bf5619b

Please sign in to comment.