Skip to content

Commit

Permalink
Assert that we don't cast values which are too big
Browse files Browse the repository at this point in the history
  • Loading branch information
vicsn committed May 17, 2023
1 parent d625ed5 commit a1b5201
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions algorithms/src/crypto_hash/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ impl<F: PrimeField, const RATE: usize> PoseidonSponge<F, RATE, 1> {
optimization_type: OptimizationType,
) -> SmallVec<[F; 10]> {
let params = get_params(TargetField::size_in_bits(), F::size_in_bits(), optimization_type);
assert!(params.bits_per_limb <= u32::MAX as usize);

// Push the lower limbs first
let mut limbs: SmallVec<[F; 10]> = SmallVec::new();
Expand Down
3 changes: 3 additions & 0 deletions algorithms/src/msm/variable_base/batched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ pub(super) fn batch_add<G: AffineCurve>(
) -> Vec<G> {
// assert_eq!(bases.len(), bucket_positions.len());
assert!(!bases.is_empty());
assert!(num_buckets <= u32::MAX as usize);

// Fetch the ideal batch size for the number of bases.
let batch_size = batch_size(bases.len());
Expand Down Expand Up @@ -335,6 +336,8 @@ fn batched_window<G: AffineCurve>(
w_start: usize,
c: usize,
) -> (G::Projective, usize) {
assert!(w_start <= u32::MAX as usize);
assert!(c <= u32::MAX as usize);
// We don't need the "zero" bucket, so we only have 2^c - 1 buckets
let window_size = if (w_start % c) != 0 { w_start % c } else { c };
let num_buckets = (1 << window_size) - 1;
Expand Down
2 changes: 2 additions & 0 deletions algorithms/src/msm/variable_base/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn update_buckets<G: AffineCurve>(
c: usize,
buckets: &mut [G::Projective],
) {
assert!(w_start <= u32::MAX as usize);
// We right-shift by w_start, thus getting rid of the lower bits.
scalar.divn(w_start as u32);

Expand All @@ -40,6 +41,7 @@ fn update_buckets<G: AffineCurve>(
// If the scalar is non-zero, we update the corresponding bucket.
// (Recall that `buckets` doesn't have a zero bucket.)
if scalar != 0 {
assert!(scalar - 1 <= usize::MAX as u64);
buckets[(scalar - 1) as usize].add_assign_mixed(base);
}
}
Expand Down

0 comments on commit a1b5201

Please sign in to comment.