serai/crypto/multiexp/src/straus.rs

50 lines
1 KiB
Rust
Raw Normal View History

use ff::PrimeFieldBits;
use group::Group;
2022-06-07 04:02:10 +00:00
use crate::{prep_bits, prep_tables};
2022-06-07 04:02:10 +00:00
pub(crate) fn straus<G: Group>(
pairs: &[(G::Scalar, G)],
window: u8
) -> G where G::Scalar: PrimeFieldBits {
let groupings = prep_bits(pairs, window);
let tables = prep_tables(pairs, window);
2022-06-07 04:02:10 +00:00
let mut res = G::identity();
for b in (0 .. groupings[0].len()).rev() {
for _ in 0 .. window {
2022-06-07 04:02:10 +00:00
res = res.double();
}
for s in 0 .. tables.len() {
res += tables[s][usize::from(groupings[s][b])];
2022-06-07 04:02:10 +00:00
}
}
res
}
pub(crate) fn straus_vartime<G: Group>(
pairs: &[(G::Scalar, G)],
window: u8
) -> G where G::Scalar: PrimeFieldBits {
let groupings = prep_bits(pairs, window);
let tables = prep_tables(pairs, window);
2022-06-07 04:02:10 +00:00
let mut res = G::identity();
for b in (0 .. groupings[0].len()).rev() {
if b != (groupings[0].len() - 1) {
for _ in 0 .. window {
2022-06-07 04:02:10 +00:00
res = res.double();
}
}
for s in 0 .. tables.len() {
if groupings[s][b] != 0 {
res += tables[s][usize::from(groupings[s][b])];
2022-06-07 04:02:10 +00:00
}
}
}
res
}