Skip to content

Commit

Permalink
fix benches and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ordian committed Apr 6, 2021
1 parent 421d884 commit 10c640b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 72 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Expand Up @@ -29,8 +29,9 @@ script:
- cargo clippy --version
- cargo clippy --all

# build
- cargo build --all
# check
- cargo check --no-default-features --features "u64_backend" # no_std
- cargo check --tests --benches # compilation

# test
- cargo $TEST_COMMAND $FEATURES
55 changes: 22 additions & 33 deletions benches/schnorr_benchmarks.rs
Expand Up @@ -9,75 +9,64 @@
// - isis agora lovecruft <isis@patternsinthevoid.net>
// - Jeff Burdges <jeff@web3.foundation>

#[macro_use]
extern crate criterion;
extern crate schnorrkel;
extern crate rand;
// extern crate sha2;

use criterion::Criterion;
use criterion::{BenchmarkId, Criterion, criterion_main, criterion_group};

mod schnorr_benches {
use super::*;
use schnorrkel::{Keypair, PublicKey, Signature, verify_batch, signing_context}; // SecretKey
use rand::prelude::*; // ThreadRng,thread_rng
use schnorrkel::{signing_context, verify_batch, Keypair, PublicKey, Signature}; // SecretKey

// TODO: fn sign_mini(c: &mut Criterion)

fn sign(c: &mut Criterion) {
let mut csprng: ThreadRng = thread_rng();
let keypair: Keypair = Keypair::generate();
let msg: &[u8] = b"";

let ctx = signing_context(b"this signature does this thing");
let ctx = signing_context(b"this signature does this thing");
c.bench_function("Schnorr signing", move |b| {
b.iter(| | keypair.sign(ctx.bytes(msg)))
b.iter(|| keypair.sign(ctx.bytes(msg)))
});
}

fn verify(c: &mut Criterion) {
let mut csprng: ThreadRng = thread_rng();
let keypair: Keypair = Keypair::generate();
let msg: &[u8] = b"";
let ctx = signing_context(b"this signature does this thing");
let ctx = signing_context(b"this signature does this thing");
let sig: Signature = keypair.sign(ctx.bytes(msg));

c.bench_function("Schnorr signature verification", move |b| {
b.iter(| | keypair.verify(ctx.bytes(msg), &sig))
b.iter(|| keypair.verify(ctx.bytes(msg), &sig))
});
}

fn verify_batch_signatures(c: &mut Criterion) {
static BATCH_SIZES: [usize; 8] = [4, 8, 16, 32, 64, 96, 128, 256];
const BATCH_SIZES: [usize; 8] = [4, 8, 16, 32, 64, 96, 128, 256];

c.bench_function_over_inputs(
"Schnorr batch signature verification",
|b, &&size| {
let mut csprng: ThreadRng = thread_rng();
let mut group = c.benchmark_group("Schnorr batch signature verification");
for size in &BATCH_SIZES {
group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| {
let keypairs: Vec<Keypair> = (0..size).map(|_| Keypair::generate()).collect();
let msg: &[u8] = b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
let ctx = signing_context(b"this signature does this thing");
let signatures: Vec<Signature> = keypairs.iter().map(|key| key.sign(ctx.bytes(msg))).collect();
let ctx = signing_context(b"this signature does this thing");
let signatures: Vec<Signature> = keypairs
.iter()
.map(|key| key.sign(ctx.bytes(msg)))
.collect();
let public_keys: Vec<PublicKey> = keypairs.iter().map(|key| key.public).collect();

b.iter(|| {
let transcripts = ::std::iter::once(ctx.bytes(msg)).cycle().take(size);
verify_batch(transcripts, &signatures[..], &public_keys[..])
});
},
&BATCH_SIZES,
);
let transcripts = ::std::iter::once(ctx.bytes(msg)).cycle().take(size);
let _ = verify_batch(transcripts, &signatures[..], &public_keys[..], true);
});
});
}
}

fn key_generation(c: &mut Criterion) {
let mut csprng: ThreadRng = thread_rng();

c.bench_function("Schnorr keypair generation", move |b| {
b.iter(| | Keypair::generate())
b.iter(|| Keypair::generate())
});
}

criterion_group!{
criterion_group! {
name = schnorr_benches;
config = Criterion::default();
targets =
Expand Down
24 changes: 0 additions & 24 deletions src/context.rs
Expand Up @@ -390,30 +390,6 @@ where T: SigningTranscript, R: RngCore+CryptoRng
}
}

/// Attach a fake `Rng` that returns all zeros, only for use in test vectors.
/// You must never deploy this because some protocols like MuSig become insecure.
#[cfg(test)]
pub(crate) fn attach_test_vector_rng<T>(t: T) -> SigningTranscriptWithRng<T,impl RngCore+CryptoRng>
where T: SigningTranscript
{
// Very insecure hack except this fn only exists in tests
struct ZeroFakeRng;
impl ::rand::RngCore for ZeroFakeRng {
fn next_u32(&mut self) -> u32 { panic!() }
fn next_u64(&mut self) -> u64 { panic!() }
fn fill_bytes(&mut self, dest: &mut [u8]) {
for i in dest.iter_mut() { *i = 0; }
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), ::rand_core::Error> {
self.fill_bytes(dest);
Ok(())
}
}
impl ::rand::CryptoRng for ZeroFakeRng {}
attach_rng(t, ZeroFakeRng)
}


#[cfg(feature = "rand_chacha")]
use rand_chacha::ChaChaRng;

Expand Down
20 changes: 8 additions & 12 deletions src/lib.rs
Expand Up @@ -218,7 +218,6 @@
extern crate std;

#[cfg(feature = "alloc")]
#[macro_use]
extern crate alloc;

use rand_core::{RngCore,CryptoRng};
Expand All @@ -236,28 +235,25 @@ use rand_core::{RngCore,CryptoRng};
// #[cfg(all(feature = "getrandom", not(feature = "rand")))]
#[cfg(feature = "getrandom")]
fn rand_hack() -> impl RngCore+CryptoRng {
::rand_core::OsRng
rand_core::OsRng
}

#[cfg(not(feature = "getrandom"))]
fn rand_hack() -> impl RngCore+CryptoRng {
const PRM : &'static str = "Attempted to use functionality that requires system randomness!!";
const PRM: &'static str = "Attempted to use functionality that requires system randomness!!";

struct PanicRng;
impl ::rand_core::RngCore for PanicRng {
fn next_u32(&mut self) -> u32 { panic!(&PRM) }
fn next_u64(&mut self) -> u64 { panic!(&PRM) }
fn fill_bytes(&mut self, _dest: &mut [u8]) { panic!(&PRM) }
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), ::rand_core::Error> { panic!(&PRM) }
impl rand_core::RngCore for PanicRng {
fn next_u32(&mut self) -> u32 { panic!("{}", PRM) }
fn next_u64(&mut self) -> u64 { panic!("{}", PRM) }
fn fill_bytes(&mut self, _dest: &mut [u8]) { panic!("{}", PRM) }
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), ::rand_core::Error> { panic!("{}", PRM) }
}
impl ::rand_core::CryptoRng for PanicRng {}
impl rand_core::CryptoRng for PanicRng {}

PanicRng
}

#[macro_use]
extern crate arrayref;

#[macro_use]
mod serdey;

Expand Down
1 change: 1 addition & 0 deletions src/musig.rs
Expand Up @@ -38,6 +38,7 @@ use alloc::{collections::btree_map::{BTreeMap, Entry}};
#[cfg(feature = "std")]
use std::{collections::btree_map::{BTreeMap, Entry}};

use arrayref::array_ref;
use arrayvec::ArrayVec;

use merlin::Transcript;
Expand Down
5 changes: 4 additions & 1 deletion src/vrf.rs
Expand Up @@ -89,7 +89,9 @@ use std::{boxed::Box, vec::Vec};
use curve25519_dalek::constants;
use curve25519_dalek::ristretto::{CompressedRistretto, RistrettoPoint};
use curve25519_dalek::scalar::Scalar;
use curve25519_dalek::traits::{IsIdentity,MultiscalarMul,VartimeMultiscalarMul}; // Identity
use curve25519_dalek::traits::{IsIdentity}; // Identity
#[cfg(any(feature = "alloc", feature = "std"))]
use curve25519_dalek::traits::{MultiscalarMul,VartimeMultiscalarMul};

use merlin::Transcript;

Expand Down Expand Up @@ -465,6 +467,7 @@ impl PublicKey {
};
#[cfg(not(any(feature = "alloc", feature = "std")))]
let go = |io: fn(p: &VRFInOut) -> &RistrettoPoint| {
let _ = vartime; // ignore unused variable
use curve25519_dalek::traits::Identity;
let mut acc = RistrettoPoint::identity();
for (z,p) in zf().zip(ps) {
Expand Down

0 comments on commit 10c640b

Please sign in to comment.