Skip to content
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

Cut verification/signing/proofs etc. time by half for 64bit machines #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix benchmarks and add new ones
elichai committed Apr 19, 2020
commit 4fcaccf217b8d507bf451d607231765556c1802f
3 changes: 1 addition & 2 deletions src/ecdh.rs
Original file line number Diff line number Diff line change
@@ -120,10 +120,9 @@ mod benches {

#[bench]
pub fn bench_ecdh(bh: &mut Bencher) {
let s = Secp256k1::with_caps(::ContextFlag::SignOnly);
let s = Secp256k1::new();
let (sk, pk) = s.generate_keypair(&mut thread_rng()).unwrap();

let s = Secp256k1::new();
bh.iter( || {
let res = SharedSecret::new(&s, &pk, &sk);
black_box(res);
49 changes: 27 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1050,20 +1050,40 @@ mod tests {
}
}


#[cfg(all(test, feature = "unstable"))]
mod benches {
use rand::{Rng, thread_rng};
use rand::{thread_rng, RngCore};
use test::{Bencher, black_box};

use super::{Secp256k1, Message};

#[bench]
pub fn generate(bh: &mut Bencher) {
struct CounterRng(u32);
impl Rng for CounterRng {
fn next_u32(&mut self) -> u32 { self.0 += 1; self.0 }
struct CounterRng(u64);
impl RngCore for CounterRng {
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}

fn next_u64(&mut self) -> u64 {
self.0 += 1;
self.0
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
for chunk in dest.chunks_mut(64/8) {
let rand: [u8; 64/8] = unsafe {std::mem::transmute(self.next_u64())};
chunk.copy_from_slice(&rand[..chunk.len()]);
}
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
Ok(self.fill_bytes(dest))
}
}


let s = Secp256k1::new();
let mut r = CounterRng(0);
bh.iter( || {
@@ -1077,12 +1097,12 @@ mod benches {
pub fn bench_sign(bh: &mut Bencher) {
let s = Secp256k1::new();
let mut msg = [0u8; 32];
thread_rng().fill(&mut msg);
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, _) = s.generate_keypair(&mut thread_rng()).unwrap();

bh.iter(|| {
let sig = s.sign(&msg, &sk).unwrap();
let sig = s.sign(&msg, &sk);
black_box(sig);
});
}
@@ -1091,7 +1111,7 @@ mod benches {
pub fn bench_verify(bh: &mut Bencher) {
let s = Secp256k1::new();
let mut msg = [0u8; 32];
thread_rng().fill(&mut msg);
thread_rng().fill_bytes(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, pk) = s.generate_keypair(&mut thread_rng()).unwrap();
let sig = s.sign(&msg, &sk).unwrap();
@@ -1101,19 +1121,4 @@ mod benches {
black_box(res);
});
}

#[bench]
pub fn bench_recover(bh: &mut Bencher) {
let s = Secp256k1::new();
let mut msg = [0u8; 32];
thread_rng().fill(&mut msg);
let msg = Message::from_slice(&msg).unwrap();
let (sk, _) = s.generate_keypair(&mut thread_rng()).unwrap();
let sig = s.sign_recoverable(&msg, &sk).unwrap();

bh.iter(|| {
let res = s.recover(&msg, &sig).unwrap();
black_box(res);
});
}
}
28 changes: 28 additions & 0 deletions src/pedersen.rs
Original file line number Diff line number Diff line change
@@ -2026,3 +2026,31 @@ mod tests {
assert_eq!(errs, 0);
}
}




#[cfg(all(test, feature = "unstable"))]
mod benches {
use rand::thread_rng;
use test::{Bencher, black_box};
use super::{ContextFlag, SecretKey, Secp256k1};

#[bench]
pub fn bench_bullet_proof_verification(bh: &mut Bencher) {
// Test Bulletproofs without message
let secp = Secp256k1::with_caps(ContextFlag::Commit);
let blinding = SecretKey::new(&secp, &mut thread_rng());
let value = 12345678;
let commit = secp.commit(value, blinding.clone()).unwrap();
let bullet_proof = secp.bullet_proof(value, blinding.clone(), blinding.clone(), blinding.clone(), None, None);
bh.iter( || {
let proof_range = secp.verify_bullet_proof(commit, bullet_proof, None).unwrap();
assert_eq!(proof_range.min, 0);
});
}
}