Skip to content

Commit

Permalink
refactor: miscellaneous tidying up
Browse files Browse the repository at this point in the history
* Miscellaneous tidying up
* Replace failure with thiserror
* Update dependencies
* Fix benchmarks

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
  • Loading branch information
koushiro authored Feb 24, 2020
1 parent c3c924d commit 53e522c
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 418 deletions.
584 changes: 205 additions & 379 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ pro-release-commit-message = "chore(release): starting development cycle for {{n

[dependencies]
ff = { version = "0.2", package = "fff" }
rayon = "1"
byteorder = "1.2"
failure = "0.1"
paired = "0.17.0"
groupy = "0.3.0"
paired = "0.17.0"
rayon = "1"
rand_core = "0.5.1"
thiserror = "1.0"

[workspace]
members = [
Expand Down
34 changes: 20 additions & 14 deletions benches/serialize.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,73 @@
#![feature(test)]
extern crate test;

use test::{black_box, Bencher};

use bls_signatures::*;
use rand::{Rng, SeedableRng, XorShiftRng};
use test::Bencher;
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

const SEED: [u8; 16] = [
0x3d, 0xbe, 0x62, 0x59, 0x8d, 0x31, 0x3d, 0x76, 0x32, 0x37, 0xdb, 0x17, 0xe5, 0xbc, 0x06, 0x54,
];

#[bench]
fn bench_serialize_private_key_as_bytes(b: &mut Bencher) {
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let rng = &mut XorShiftRng::from_seed(SEED);

let private_key = PrivateKey::generate(rng);

b.iter(|| test::black_box(private_key.as_bytes()));
b.iter(|| black_box(private_key.as_bytes()));
}

#[bench]
fn bench_serialize_private_key_from_bytes(b: &mut Bencher) {
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let rng = &mut XorShiftRng::from_seed(SEED);

let private_key = PrivateKey::generate(rng);
let bytes = private_key.as_bytes();

b.iter(|| test::black_box(PrivateKey::from_bytes(&bytes).unwrap()));
b.iter(|| black_box(PrivateKey::from_bytes(&bytes).unwrap()));
}

#[bench]
fn bench_serialize_public_key_as_bytes(b: &mut Bencher) {
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let rng = &mut XorShiftRng::from_seed(SEED);

let public_key = PrivateKey::generate(rng).public_key();

b.iter(|| test::black_box(public_key.as_bytes()));
b.iter(|| black_box(public_key.as_bytes()));
}

#[bench]
fn bench_serialize_public_key_from_bytes(b: &mut Bencher) {
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let rng = &mut XorShiftRng::from_seed(SEED);

let public_key = PrivateKey::generate(rng).public_key();
let bytes = public_key.as_bytes();

b.iter(|| test::black_box(PublicKey::from_bytes(&bytes).unwrap()));
b.iter(|| black_box(PublicKey::from_bytes(&bytes).unwrap()));
}

#[bench]
fn bench_serialize_signature_as_bytes(b: &mut Bencher) {
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let rng = &mut XorShiftRng::from_seed(SEED);

let private_key = PrivateKey::generate(rng);
let msg = (0..64).map(|_| rng.gen()).collect::<Vec<u8>>();
let signature = private_key.sign(&msg);

b.iter(|| test::black_box(signature.as_bytes()));
b.iter(|| black_box(signature.as_bytes()));
}

#[bench]
fn bench_serialize_signature_from_bytes(b: &mut Bencher) {
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let rng = &mut XorShiftRng::from_seed(SEED);

let private_key = PrivateKey::generate(rng);
let msg = (0..64).map(|_| rng.gen()).collect::<Vec<u8>>();
let signature = private_key.sign(&msg);
let bytes = signature.as_bytes();

b.iter(|| test::black_box(Signature::from_bytes(&bytes).unwrap()));
b.iter(|| black_box(Signature::from_bytes(&bytes).unwrap()));
}
14 changes: 10 additions & 4 deletions benches/sign.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#![feature(test)]
extern crate test;

use test::{black_box, Bencher};

use bls_signatures::*;
use rand::{Rng, SeedableRng, XorShiftRng};
use test::Bencher;
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

const SEED: [u8; 16] = [
0x3d, 0xbe, 0x62, 0x59, 0x8d, 0x31, 0x3d, 0x76, 0x32, 0x37, 0xdb, 0x17, 0xe5, 0xbc, 0x06, 0x54,
];

#[bench]
fn sign_64b(b: &mut Bencher) {
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let rng = &mut XorShiftRng::from_seed(SEED);

let private_key = PrivateKey::generate(rng);
let msg: Vec<u8> = (0..64).map(|_| rng.gen()).collect();

b.iter(|| test::black_box(private_key.sign(&msg)))
b.iter(|| black_box(private_key.sign(&msg)))
}
14 changes: 10 additions & 4 deletions benches/verify.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#![feature(test)]
extern crate test;

use test::{black_box, Bencher};

use bls_signatures::*;
use rand::{Rng, SeedableRng, XorShiftRng};
use test::Bencher;
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

const SEED: [u8; 16] = [
0x3d, 0xbe, 0x62, 0x59, 0x8d, 0x31, 0x3d, 0x76, 0x32, 0x37, 0xdb, 0x17, 0xe5, 0xbc, 0x06, 0x54,
];

macro_rules! bench_verify {
($name:ident, $num:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
let rng = &mut XorShiftRng::from_seed(SEED);
// generate private keys
let private_keys: Vec<_> = (0..$num).map(|_| PrivateKey::generate(rng)).collect();

Expand All @@ -36,7 +42,7 @@ macro_rules! bench_verify {
.map(|pk| pk.public_key())
.collect::<Vec<_>>();

b.iter(|| test::black_box(verify(&aggregated_signature, &hashes, &public_keys)))
b.iter(|| black_box(verify(&aggregated_signature, &hashes, &public_keys)))
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion bls-signatures-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ libc = "0.2"
rayon = "1.2.0"

[build-dependencies]
cbindgen = "0.6.8"
cbindgen = { version = "0.13.1", default-features = false }
2 changes: 1 addition & 1 deletion bls-signatures-ffi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::Write;
use std::path::PathBuf;
use std::process::Command;

const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const VERSION: &str = env!("CARGO_PKG_VERSION");

fn main() {
let crate_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
Expand Down
4 changes: 2 additions & 2 deletions bls-signatures-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ mod tests {
unsafe {
let private_key = (*private_key_generate()).private_key;
let public_key = (*private_key_public_key(&private_key[0])).public_key;
let message = "hello world".as_bytes();
let message = b"hello world";
let digest = (*hash(&message[0], message.len())).digest;
let signature =
(*private_key_sign(&private_key[0], &message[0], message.len())).signature;
Expand All @@ -260,7 +260,7 @@ mod tests {

assert_eq!(1, verified);

let different_message = "bye world".as_bytes();
let different_message = b"bye world";
let different_digest = (*hash(&different_message[0], different_message.len())).digest;
let not_verified = verify(
&signature[0],
Expand Down
13 changes: 13 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("Size mismatch")]
SizeMismatch,
#[error("Io error: {0}")]
Io(#[from] std::io::Error),
#[error("Group decode error: {0}")]
GroupDecode(#[from] groupy::GroupDecodingError),
#[error("Prime field decode error: {0}")]
FieldDecode(#[from] ff::PrimeFieldDecodingError),
}
15 changes: 8 additions & 7 deletions src/key.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::io::{self, Cursor};
use std::io::{self, Cursor, Read};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use failure::{format_err, Error};
use ff::{Field, PrimeField};
use groupy::{CurveProjective, EncodedPoint, Wnaf};
use paired::bls12_381::{Fr, FrRepr, G1Affine, G1Compressed, G1};
use rand_core::RngCore;

use super::signature::*;
use crate::error::Error;
use crate::signature::*;

#[derive(Debug, Clone, PartialEq)]
pub struct PublicKey(G1Affine);
Expand Down Expand Up @@ -87,7 +86,7 @@ impl PrivateKey {
impl Serialize for PrivateKey {
fn write_bytes(&self, dest: &mut impl io::Write) -> io::Result<()> {
for digit in self.0.into_repr().as_ref().iter() {
dest.write_u64::<LittleEndian>(*digit)?;
dest.write_all(&digit.to_le_bytes())?;
}

Ok(())
Expand All @@ -97,7 +96,9 @@ impl Serialize for PrivateKey {
let mut res = FrRepr::default();
let mut reader = Cursor::new(raw);
for digit in res.0.as_mut().iter_mut() {
*digit = reader.read_u64::<LittleEndian>()?;
let mut buf = [0; 8];
reader.read_exact(&mut buf)?;
*digit = u64::from_le_bytes(buf);
}

Ok(Fr::from_repr(res)?.into())
Expand All @@ -119,7 +120,7 @@ impl Serialize for PublicKey {

fn from_bytes(raw: &[u8]) -> Result<Self, Error> {
if raw.len() != G1Compressed::size() {
return Err(format_err!("size missmatch"));
return Err(Error::SizeMismatch);
}

let mut res = G1Compressed::empty();
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod error;
mod key;
mod signature;

pub use self::error::Error;
pub use self::key::{PrivateKey, PublicKey, Serialize};
pub use self::signature::{aggregate, hash, verify, Signature};

Expand Down
4 changes: 2 additions & 2 deletions src/signature.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::io;

use failure::{format_err, Error};
use ff::Field;
use groupy::{CurveAffine, CurveProjective, EncodedPoint};
use paired::bls12_381::{Bls12, Fq12, G1Affine, G2Affine, G2Compressed, G2};
use paired::{Engine, PairingCurveAffine};
use rayon::prelude::*;

use crate::error::Error;
use crate::key::*;

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -33,7 +33,7 @@ impl Serialize for Signature {

fn from_bytes(raw: &[u8]) -> Result<Self, Error> {
if raw.len() != G2Compressed::size() {
return Err(format_err!("size missmatch"));
return Err(Error::SizeMismatch);
}

let mut res = G2Compressed::empty();
Expand Down

0 comments on commit 53e522c

Please sign in to comment.