Skip to content

Commit 8f36036

Browse files
authored
Merge pull request #2300 from ljedrz/fix/avoid_sum_overflow
Avoid an overflow when summing batch sizes
2 parents 37d8c62 + 50886d5 commit 8f36036

File tree

1 file changed

+6
-2
lines changed
  • algorithms/src/snark/varuna/data_structures

1 file changed

+6
-2
lines changed

algorithms/src/snark/varuna/data_structures/proof.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<E: PairingEngine> Commitments<E> {
8888
compress: Compress,
8989
validate: Validate,
9090
) -> Result<Self, snarkvm_utilities::SerializationError> {
91-
let mut w = Vec::with_capacity(batch_sizes.iter().sum());
91+
let mut w = Vec::new();
9292
for batch_size in batch_sizes {
9393
w.extend(deserialize_vec_without_len(&mut reader, compress, validate, *batch_size)?);
9494
}
@@ -261,7 +261,11 @@ impl<E: PairingEngine> Proof<E> {
261261

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

0 commit comments

Comments
 (0)