From c92be0e8815875460e736086a6b02fed9e4273ff Mon Sep 17 00:00:00 2001 From: Chris Sosnin <48099298+slumber@users.noreply.github.com> Date: Sat, 23 Sep 2023 15:22:38 +0300 Subject: [PATCH] make_digits without allocation (#684) --- ec/src/scalar_mul/variable_base/mod.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) 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 + }) }