-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
Crandall primes #445
base: master
Are you sure you want to change the base?
Crandall primes #445
Conversation
… Prime fast reduction - closes #11
…t, renaming of lazy reduction both in Montgomery and Crandall to lazyReduction
…k1, failing edwards25519
Bench vs RustCrypto/elliptic-curveshttps://github.com/RustCrypto/elliptic-curves/ is the current record holder of https://programming-language-benchmarks.vercel.app/problem/secp256k1 We modify it to bench some of the internals Field implementation
with an extra fn bench_field_element_10adds<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let x = test_field_element_x();
let y = test_field_element_y();
group.bench_function("10 adds", |b| b.iter(
|| {
&black_box(x) + &black_box(y);
&black_box(x) + &black_box(y);
&black_box(x) + &black_box(y);
&black_box(x) + &black_box(y);
&black_box(x) + &black_box(y);
&black_box(x) + &black_box(y);
&black_box(x) + &black_box(y);
&black_box(x) + &black_box(y);
&black_box(x) + &black_box(y);
&black_box(x) + &black_box(y)
}
));
}
EC implementation (projective with Renes2015 formulae)use criterion::{
black_box, criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, Criterion,
};
use k256::ProjectivePoint;
use elliptic_curve::{
rand_core::SeedableRng,
group::Group,
};
use rand_xorshift::XorShiftRng;
fn bench_ec_add<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let mut rng = XorShiftRng::seed_from_u64(1234u64);
let p = ProjectivePoint::random(&mut rng);
let q = ProjectivePoint::random(&mut rng);
group.bench_function("EC Add", |b| {
b.iter(|| &black_box(p) + &black_box(q))
});
}
fn bench_ec_dbl<'a, M: Measurement>(group: &mut BenchmarkGroup<'a, M>) {
let mut rng = XorShiftRng::seed_from_u64(1234u64);
let p = ProjectivePoint::random(&mut rng);
group.bench_function("EC Dbl", |b| {
b.iter(|| black_box(p).double())
});
}
fn bench_ec(c: &mut Criterion) {
let mut group = c.benchmark_group("EC operations");
bench_ec_add(&mut group);
bench_ec_dbl(&mut group);
group.finish();
}
criterion_group!(benches, bench_ec);
criterion_main!(benches);
AnalysisThe fact that field operations are 1.7x to 2x faster BUT the elliptic curve operations are 0.85x slower is extremely suspicious. Especially when we implement the same formulae from Renes2015 paper. There might be useless copies or parameter passing overhead similar to #21 and #146 |
This closes #11 for primes of form 2ᵐ-c (Crandall primes / pseudo-Mersenne primes), such as the one used for Curve25519 and secp256kq (Ethereum/ Bitcoin).
Bench Fp vs Constantine master
Previous
Current
Analysis
Bench EC vs Constantine master
Previous
Current
Analysis
Bench vs bitcoin/secp256k1
Analysis
The fact that field operations are 1.5x faster BUT the elliptic curve operations are sometimes slower is suspicious. We probably need to check the EC formulae
TODO
indicates in Theorem 4 that their partial reduction may grow by 1 bit if 256-bit.