mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 04:39:24 +00:00
This commit is contained in:
11
crypto/multiexp/Cargo.toml
Normal file
11
crypto/multiexp/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "multiexp"
|
||||
version = "0.1.0"
|
||||
description = "Multiexponentation algorithms for ff/group"
|
||||
license = "MIT"
|
||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
ff = "0.11"
|
||||
group = "0.11"
|
||||
21
crypto/multiexp/LICENSE
Normal file
21
crypto/multiexp/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Luke Parker
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
55
crypto/multiexp/src/lib.rs
Normal file
55
crypto/multiexp/src/lib.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use ff::PrimeField;
|
||||
use group::{Group, GroupEncoding, ScalarMul};
|
||||
|
||||
// An implementation of Straus, with a extremely minimal API that lets us add other algorithms in
|
||||
// the future. Takes in a list of scalars and points with a boolean for if the scalars are little
|
||||
// endian encoded or not
|
||||
pub fn multiexp_vartime<F: PrimeField, G: Group + GroupEncoding + ScalarMul<F>>(
|
||||
scalars: &[F],
|
||||
points: &[G],
|
||||
little: bool
|
||||
) -> G {
|
||||
let mut tables = vec![];
|
||||
// dalek uses 8 in their impl, along with a carry scheme where values are [-8, 8)
|
||||
// Moving to a similar system here did save a marginal amount, yet not one significant enough for
|
||||
// its pain (as some fields do have scalars which can have their top bit set, a scenario dalek
|
||||
// assumes is never true)
|
||||
tables.resize(points.len(), [G::identity(); 16]);
|
||||
for p in 0 .. points.len() {
|
||||
let mut accum = G::identity();
|
||||
for i in 1 .. 16 {
|
||||
accum += points[p];
|
||||
tables[p][i] = accum;
|
||||
}
|
||||
}
|
||||
|
||||
let mut nibbles = vec![];
|
||||
nibbles.resize(scalars.len(), vec![]);
|
||||
for s in 0 .. scalars.len() {
|
||||
let mut repr = scalars[s].to_repr();
|
||||
let bytes = repr.as_mut();
|
||||
if !little {
|
||||
bytes.reverse();
|
||||
}
|
||||
|
||||
nibbles[s].resize(bytes.len() * 2, 0);
|
||||
for i in 0 .. bytes.len() {
|
||||
nibbles[s][i * 2] = bytes[i] & 0b1111;
|
||||
nibbles[s][(i * 2) + 1] = (bytes[i] >> 4) & 0b1111;
|
||||
}
|
||||
}
|
||||
|
||||
let mut res = G::identity();
|
||||
for b in (0 .. nibbles[0].len()).rev() {
|
||||
for _ in 0 .. 4 {
|
||||
res = res.double();
|
||||
}
|
||||
|
||||
for s in 0 .. scalars.len() {
|
||||
if nibbles[s][b] != 0 {
|
||||
res += tables[s][nibbles[s][b] as usize];
|
||||
}
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
Reference in New Issue
Block a user