Skip to content

Commit

Permalink
make_digits without allocation (arkworks-rs#684)
Browse files Browse the repository at this point in the history
  • Loading branch information
slumber authored and aleasims committed Oct 18, 2023
1 parent 820e48f commit d0f219c
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions ec/src/scalar_mul/variable_base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ fn msm_bigint<V: VariableBaseMSM>(
}

// 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<i64> {
fn make_digits(a: &impl BigInteger, w: usize, num_bits: usize) -> impl Iterator<Item = i64> + '_ {
let scalar = a.as_ref();
let radix: u64 = 1 << w;
let window_mask: u64 = radix - 1;
Expand All @@ -258,8 +258,8 @@ fn make_digits(a: &impl BigInteger, w: usize, num_bits: usize) -> Vec<i64> {
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;
Expand All @@ -279,10 +279,11 @@ fn make_digits(a: &impl BigInteger, w: usize, num_bits: usize) -> Vec<i64> {

// 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
})
}

0 comments on commit d0f219c

Please sign in to comment.