Skip to content

Commit

Permalink
Merge pull request #2300 from ljedrz/fix/avoid_sum_overflow
Browse files Browse the repository at this point in the history
Avoid an overflow when summing batch sizes
  • Loading branch information
howardwu authored Jan 20, 2024
2 parents 37d8c62 + 50886d5 commit 8f36036
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions algorithms/src/snark/varuna/data_structures/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<E: PairingEngine> Commitments<E> {
compress: Compress,
validate: Validate,
) -> Result<Self, snarkvm_utilities::SerializationError> {
let mut w = Vec::with_capacity(batch_sizes.iter().sum());
let mut w = Vec::new();
for batch_size in batch_sizes {
w.extend(deserialize_vec_without_len(&mut reader, compress, validate, *batch_size)?);
}
Expand Down Expand Up @@ -261,7 +261,11 @@ impl<E: PairingEngine> Proof<E> {

/// Check that the number of messages is consistent with our batch size
pub fn check_batch_sizes(&self) -> Result<(), SNARKError> {
let total_instances = self.batch_sizes.iter().sum::<usize>();
let total_instances = self
.batch_sizes
.iter()
.try_fold(0usize, |acc, &size| acc.checked_add(size))
.ok_or(SNARKError::BatchSizeMismatch)?;
if self.commitments.witness_commitments.len() != total_instances {
return Err(SNARKError::BatchSizeMismatch);
}
Expand Down

0 comments on commit 8f36036

Please sign in to comment.