Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove redundant allocation in msm #684

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
})
}