diff --git a/ec/src/scalar_mul/variable_base/mod.rs b/ec/src/scalar_mul/variable_base/mod.rs index 6e485c9a3..c65722a7c 100644 --- a/ec/src/scalar_mul/variable_base/mod.rs +++ b/ec/src/scalar_mul/variable_base/mod.rs @@ -246,7 +246,7 @@ fn msm_bigint( } // From: https://github.com/arkworks-rs/gemini/blob/main/src/kzg/msm/variable_base.rs#L20 -fn make_digits(a: &impl BigInteger, w: usize, num_bits: usize) -> Vec { +fn make_digits(a: &impl BigInteger, w: usize, num_bits: usize) -> impl Iterator + '_ { let scalar = a.as_ref(); let radix: u64 = 1 << w; let window_mask: u64 = radix - 1; @@ -258,8 +258,8 @@ fn make_digits(a: &impl BigInteger, w: usize, num_bits: usize) -> Vec { num_bits }; let digits_count = (num_bits + w - 1) / w; - let mut digits = vec![0i64; digits_count]; - for (i, digit) in digits.iter_mut().enumerate() { + + (0..digits_count).into_iter().map(move |i| { // Construct a buffer of bits of the scalar, starting at `bit_offset`. let bit_offset = i * w; let u64_idx = bit_offset / 64; @@ -279,10 +279,11 @@ fn make_digits(a: &impl BigInteger, w: usize, num_bits: usize) -> Vec { // Recenter coefficients from [0,2^w) to [-2^w/2, 2^w/2) carry = (coef + radix / 2) >> w; - *digit = (coef as i64) - (carry << w) as i64; - } + let mut digit = (coef as i64) - (carry << w) as i64; - digits[digits_count - 1] += (carry << w) as i64; - - digits + if i == digits_count - 1 { + digit += (carry << w) as i64; + } + digit + }) }