mirror of
https://github.com/arnaucube/babyjubjub-ark.git
synced 2026-01-13 17:21:29 +01:00
Optimize point add & mul_scalar methods
(On a Intel(R) Core(TM) i7-8705G CPU @ 3.10GHz, with 32 GB of RAM) - before: ``` add time: [53.447 us 53.467 us 53.492 us] mul_scalar time: [121.19 ms 121.22 ms 121.25 ms] ``` - current: ``` add time: [317.34 ns 317.44 ns 317.54 ns] mul_scalar time: [131.05 us 131.28 us 131.58 us] ``` Which is `168x` improvement for `add`, and `923x` improvement for `mul_scalar`.
This commit is contained in:
@@ -9,10 +9,11 @@ repository = "https://github.com/arnaucube/babyjubjub-rs"
|
|||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
ff = {package="ff_ce" , version="0.11", features = ["derive"]}
|
||||||
|
rand = "0.4"
|
||||||
num = "0.2.0"
|
num = "0.2.0"
|
||||||
num-bigint = {version = "0.2.2", features = ["rand"]}
|
num-bigint = {version = "0.2.2", features = ["rand"]}
|
||||||
num-traits = "0.2.8"
|
num-traits = "0.2.8"
|
||||||
rand = "0.6.5"
|
|
||||||
blake2 = "0.8"
|
blake2 = "0.8"
|
||||||
generic-array = "0.13.2"
|
generic-array = "0.13.2"
|
||||||
tiny-keccak = "1.5"
|
tiny-keccak = "1.5"
|
||||||
|
|||||||
@@ -1,41 +1,42 @@
|
|||||||
use criterion::{criterion_group, criterion_main, Criterion};
|
use criterion::{criterion_group, criterion_main, Criterion};
|
||||||
|
|
||||||
|
extern crate rand;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate ff;
|
||||||
|
use ff::*;
|
||||||
|
|
||||||
extern crate num;
|
extern crate num;
|
||||||
extern crate num_bigint;
|
extern crate num_bigint;
|
||||||
extern crate num_traits;
|
use num_bigint::BigInt;
|
||||||
use num_bigint::{BigInt, ToBigInt};
|
|
||||||
|
|
||||||
use babyjubjub_rs::{utils, Point};
|
use babyjubjub_rs::{utils, Point};
|
||||||
|
|
||||||
fn criterion_benchmark(c: &mut Criterion) {
|
fn criterion_benchmark(c: &mut Criterion) {
|
||||||
let x: BigInt = BigInt::parse_bytes(
|
// let x: BigInt = BigInt::parse_bytes(
|
||||||
b"17777552123799933955779906779655732241715742912184938656739573121738514868268",
|
// b"17777552123799933955779906779655732241715742912184938656739573121738514868268",
|
||||||
10,
|
// 10,
|
||||||
)
|
// )
|
||||||
.unwrap();
|
// .unwrap();
|
||||||
c.bench_function("modulus", |b| {
|
// c.bench_function("modulus", |b| {
|
||||||
b.iter(|| utils::modulus(&x, &babyjubjub_rs::Q))
|
// b.iter(|| utils::modulus(&x, &babyjubjub_rs::Q))
|
||||||
});
|
// });
|
||||||
|
|
||||||
let p: Point = Point {
|
let p: Point = Point {
|
||||||
x: BigInt::parse_bytes(
|
x: babyjubjub_rs::Fr::from_str(
|
||||||
b"17777552123799933955779906779655732241715742912184938656739573121738514868268",
|
"17777552123799933955779906779655732241715742912184938656739573121738514868268",
|
||||||
10,
|
|
||||||
)
|
)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
y: BigInt::parse_bytes(
|
y: babyjubjub_rs::Fr::from_str(
|
||||||
b"2626589144620713026669568689430873010625803728049924121243784502389097019475",
|
"2626589144620713026669568689430873010625803728049924121243784502389097019475",
|
||||||
10,
|
|
||||||
)
|
)
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
z: babyjubjub_rs::Fr::one(),
|
||||||
};
|
};
|
||||||
let q = p.clone();
|
let q = p.clone();
|
||||||
|
|
||||||
c.bench_function("add", |b| b.iter(|| p.add(&q)));
|
c.bench_function("add", |b| b.iter(|| p.add(&q)));
|
||||||
|
let r: BigInt = BigInt::parse_bytes(b"3", 10).unwrap();
|
||||||
c.bench_function("mul_scalar_small", |b| {
|
c.bench_function("mul_scalar_small", |b| b.iter(|| p.mul_scalar(&r)));
|
||||||
b.iter(|| p.mul_scalar(&3.to_bigint().unwrap()))
|
|
||||||
});
|
|
||||||
let r: BigInt = BigInt::parse_bytes(
|
let r: BigInt = BigInt::parse_bytes(
|
||||||
b"2626589144620713026669568689430873010625803728049924121243784502389097019475",
|
b"2626589144620713026669568689430873010625803728049924121243784502389097019475",
|
||||||
10,
|
10,
|
||||||
@@ -43,16 +44,22 @@ fn criterion_benchmark(c: &mut Criterion) {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
c.bench_function("mul_scalar", |b| b.iter(|| p.mul_scalar(&r)));
|
c.bench_function("mul_scalar", |b| b.iter(|| p.mul_scalar(&r)));
|
||||||
|
|
||||||
let sk = babyjubjub_rs::new_key();
|
// c.bench_function("compress", |b| b.iter(|| p.compress()));
|
||||||
let pk = sk.public().unwrap();
|
// let p_comp = p.compress();
|
||||||
let msg = 5.to_bigint().unwrap();
|
// c.bench_function("decompress", |b| {
|
||||||
c.bench_function("sign_poseidon", |b| {
|
// b.iter(|| babyjubjub_rs::decompress_point(p_comp))
|
||||||
b.iter(|| sk.sign_poseidon(msg.clone()))
|
// });
|
||||||
});
|
|
||||||
let sig = sk.sign_poseidon(msg.clone()).unwrap();
|
// let sk = babyjubjub_rs::new_key();
|
||||||
c.bench_function("verify_poseidon", |b| {
|
// let pk = sk.public().unwrap();
|
||||||
b.iter(|| babyjubjub_rs::verify_poseidon(pk.clone(), sig.clone(), msg.clone()))
|
// let msg = 5.to_bigint().unwrap();
|
||||||
});
|
// c.bench_function("sign_poseidon", |b| {
|
||||||
|
// b.iter(|| sk.sign_poseidon(msg.clone()))
|
||||||
|
// });
|
||||||
|
// let sig = sk.sign_poseidon(msg.clone()).unwrap();
|
||||||
|
// c.bench_function("verify_poseidon", |b| {
|
||||||
|
// b.iter(|| babyjubjub_rs::verify_poseidon(pk.clone(), sig.clone(), msg.clone()))
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
criterion_group!(benches, criterion_benchmark);
|
criterion_group!(benches, criterion_benchmark);
|
||||||
|
|||||||
826
src/lib.rs
826
src/lib.rs
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user