mirror of
https://github.com/serai-dex/serai.git
synced 2025-01-24 11:36:18 +00:00
3.7.6, 3.7.7 Optimize multiexp implementations
This commit is contained in:
parent
e5329b42e6
commit
1d2ebdca62
2 changed files with 35 additions and 13 deletions
|
@ -15,9 +15,11 @@ where
|
||||||
|
|
||||||
let mut res = G::identity();
|
let mut res = G::identity();
|
||||||
for n in (0 .. bits[0].len()).rev() {
|
for n in (0 .. bits[0].len()).rev() {
|
||||||
|
if n != (bits[0].len() - 1) {
|
||||||
for _ in 0 .. window {
|
for _ in 0 .. window {
|
||||||
res = res.double();
|
res = res.double();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut buckets = vec![G::identity(); 2_usize.pow(window.into())];
|
let mut buckets = vec![G::identity(); 2_usize.pow(window.into())];
|
||||||
for p in 0 .. bits.len() {
|
for p in 0 .. bits.len() {
|
||||||
|
@ -49,20 +51,34 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut buckets = vec![G::identity(); 2_usize.pow(window.into())];
|
// Use None to represent identity since is_none is likely faster than is_identity
|
||||||
|
let mut buckets = vec![None; 2_usize.pow(window.into())];
|
||||||
for p in 0 .. bits.len() {
|
for p in 0 .. bits.len() {
|
||||||
let nibble = usize::from(bits[p][n]);
|
let nibble = usize::from(bits[p][n]);
|
||||||
if nibble != 0 {
|
if nibble != 0 {
|
||||||
buckets[nibble] += pairs[p].1;
|
if let Some(bucket) = buckets[nibble].as_mut() {
|
||||||
|
*bucket += pairs[p].1;
|
||||||
|
} else {
|
||||||
|
buckets[nibble] = Some(pairs[p].1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut intermediate_sum = G::identity();
|
let mut intermediate_sum = None;
|
||||||
for b in (1 .. buckets.len()).rev() {
|
for b in (1 .. buckets.len()).rev() {
|
||||||
intermediate_sum += buckets[b];
|
if let Some(bucket) = buckets[b].as_ref() {
|
||||||
|
if let Some(intermediate_sum) = intermediate_sum.as_mut() {
|
||||||
|
*intermediate_sum += bucket;
|
||||||
|
} else {
|
||||||
|
intermediate_sum = Some(*bucket);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(intermediate_sum) = intermediate_sum.as_ref() {
|
||||||
res += intermediate_sum;
|
res += intermediate_sum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,9 +31,11 @@ where
|
||||||
|
|
||||||
let mut res = G::identity();
|
let mut res = G::identity();
|
||||||
for b in (0 .. groupings[0].len()).rev() {
|
for b in (0 .. groupings[0].len()).rev() {
|
||||||
|
if b != (groupings[0].len() - 1) {
|
||||||
for _ in 0 .. window {
|
for _ in 0 .. window {
|
||||||
res = res.double();
|
res = res.double();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for s in 0 .. tables.len() {
|
for s in 0 .. tables.len() {
|
||||||
res += tables[s][usize::from(groupings[s][b])];
|
res += tables[s][usize::from(groupings[s][b])];
|
||||||
|
@ -51,20 +53,24 @@ where
|
||||||
let groupings = prep_bits(pairs, window);
|
let groupings = prep_bits(pairs, window);
|
||||||
let tables = prep_tables(pairs, window);
|
let tables = prep_tables(pairs, window);
|
||||||
|
|
||||||
let mut res = G::identity();
|
let mut res: Option<G> = None;
|
||||||
for b in (0 .. groupings[0].len()).rev() {
|
for b in (0 .. groupings[0].len()).rev() {
|
||||||
if b != (groupings[0].len() - 1) {
|
if b != (groupings[0].len() - 1) {
|
||||||
for _ in 0 .. window {
|
for _ in 0 .. window {
|
||||||
res = res.double();
|
res = res.map(|res| res.double());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for s in 0 .. tables.len() {
|
for s in 0 .. tables.len() {
|
||||||
if groupings[s][b] != 0 {
|
if groupings[s][b] != 0 {
|
||||||
res += tables[s][usize::from(groupings[s][b])];
|
if let Some(res) = res.as_mut() {
|
||||||
|
*res += tables[s][usize::from(groupings[s][b])];
|
||||||
|
} else {
|
||||||
|
res = Some(tables[s][usize::from(groupings[s][b])]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
res.unwrap_or_else(G::identity)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue