Browse Source

Add benchmarks

main
arnaucube 4 years ago
parent
commit
30218dc1f3
3 changed files with 133 additions and 65 deletions
  1. +8
    -0
      Cargo.toml
  2. +59
    -0
      benches/bench_babyjubjub.rs
  3. +66
    -65
      src/lib.rs

+ 8
- 0
Cargo.toml

@ -21,3 +21,11 @@ mimc-rs = "0.0.2"
poseidon-rs = "0.0.1"
arrayref = "0.3.5"
lazy_static = "1.4.0"
[dev-dependencies]
criterion = "0.3"
[[bench]]
name = "bench_babyjubjub"
harness = false

+ 59
- 0
benches/bench_babyjubjub.rs

@ -0,0 +1,59 @@
use criterion::{criterion_group, criterion_main, Criterion};
extern crate num;
extern crate num_bigint;
extern crate num_traits;
use num_bigint::{BigInt, ToBigInt};
use babyjubjub_rs::{utils, Point};
fn criterion_benchmark(c: &mut Criterion) {
let x: BigInt = BigInt::parse_bytes(
b"17777552123799933955779906779655732241715742912184938656739573121738514868268",
10,
)
.unwrap();
c.bench_function("modulus", |b| {
b.iter(|| utils::modulus(&x, &babyjubjub_rs::Q))
});
let p: Point = Point {
x: BigInt::parse_bytes(
b"17777552123799933955779906779655732241715742912184938656739573121738514868268",
10,
)
.unwrap(),
y: BigInt::parse_bytes(
b"2626589144620713026669568689430873010625803728049924121243784502389097019475",
10,
)
.unwrap(),
};
let q = p.clone();
c.bench_function("add", |b| b.iter(|| p.add(&q)));
c.bench_function("mul_scalar_small", |b| {
b.iter(|| p.mul_scalar(&3.to_bigint().unwrap()))
});
let r: BigInt = BigInt::parse_bytes(
b"2626589144620713026669568689430873010625803728049924121243784502389097019475",
10,
)
.unwrap();
c.bench_function("mul_scalar", |b| b.iter(|| p.mul_scalar(&r)));
let sk = babyjubjub_rs::new_key();
let pk = sk.public().unwrap();
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_main!(benches);

+ 66
- 65
src/lib.rs

@ -17,7 +17,7 @@ use num_traits::{One, Zero};
use generic_array::GenericArray;
mod utils;
pub mod utils;
#[macro_use]
extern crate lazy_static;
@ -25,7 +25,7 @@ extern crate lazy_static;
lazy_static! {
static ref D: BigInt = BigInt::parse_bytes(b"168696", 10).unwrap();
static ref A: BigInt = BigInt::parse_bytes(b"168700", 10).unwrap();
static ref Q: BigInt = BigInt::parse_bytes(
pub static ref Q: BigInt = BigInt::parse_bytes(
b"21888242871839275222246405745257275088548364400416034343698204186575808495617",
10,
)
@ -156,6 +156,7 @@ pub fn decompress_point(bb: [u8; 32]) -> Result {
Ok(Point { x: x, y: y })
}
#[derive(Debug, Clone)]
pub struct Signature {
r_b8: Point,
s: BigInt,
@ -497,25 +498,25 @@ mod tests {
);
}
#[test]
fn test_new_key_sign_verify_mimc_0() {
let sk = new_key();
let pk = sk.public().unwrap();
let msg = 5.to_bigint().unwrap();
let sig = sk.sign_mimc(msg.clone()).unwrap();
let v = verify_mimc(pk, sig, msg);
assert_eq!(v, true);
}
#[test]
fn test_new_key_sign_verify_mimc_1() {
let sk = new_key();
let pk = sk.public().unwrap();
let msg = BigInt::parse_bytes(b"123456789012345678901234567890", 10).unwrap();
let sig = sk.sign_mimc(msg.clone()).unwrap();
let v = verify_mimc(pk, sig, msg);
assert_eq!(v, true);
}
// #[test]
// fn test_new_key_sign_verify_mimc_0() {
// let sk = new_key();
// let pk = sk.public().unwrap();
// let msg = 5.to_bigint().unwrap();
// let sig = sk.sign_mimc(msg.clone()).unwrap();
// let v = verify_mimc(pk, sig, msg);
// assert_eq!(v, true);
// }
//
// #[test]
// fn test_new_key_sign_verify_mimc_1() {
// let sk = new_key();
// let pk = sk.public().unwrap();
// let msg = BigInt::parse_bytes(b"123456789012345678901234567890", 10).unwrap();
// let sig = sk.sign_mimc(msg.clone()).unwrap();
// let v = verify_mimc(pk, sig, msg);
// assert_eq!(v, true);
// }
#[test]
fn test_new_key_sign_verify_poseidon_0() {
let sk = new_key();
@ -596,50 +597,50 @@ mod tests {
assert_eq!(&p.x, &expected_px);
}
#[test]
fn test_point_decompress_loop() {
for _ in 0..5 {
let mut rng = rand::thread_rng();
let sk_raw = rng.gen_biguint(1024).to_bigint().unwrap();
let mut hasher = Blake2b::new();
let (_, sk_raw_bytes) = sk_raw.to_bytes_be();
hasher.input(sk_raw_bytes);
let mut h = hasher.result();
h[0] = h[0] & 0xF8;
h[31] = h[31] & 0x7F;
h[31] = h[31] | 0x40;
let sk = BigInt::from_bytes_le(Sign::Plus, &h[..]);
let point = B8.mul_scalar(&sk).unwrap();
let cmp_point = point.compress();
let dcmp_point = decompress_point(cmp_point).unwrap();
assert_eq!(&point.x, &dcmp_point.x);
assert_eq!(&point.y, &dcmp_point.y);
}
}
#[test]
fn test_signature_compress_decompress() {
let sk = new_key();
let pk = sk.public().unwrap();
for i in 0..5 {
let msg_raw = "123456".to_owned() + &i.to_string();
let msg = BigInt::parse_bytes(msg_raw.as_bytes(), 10).unwrap();
let sig = sk.sign_mimc(msg.clone()).unwrap();
let compressed_sig = sig.compress();
let decompressed_sig = decompress_signature(&compressed_sig).unwrap();
assert_eq!(&sig.r_b8.x, &decompressed_sig.r_b8.x);
assert_eq!(&sig.r_b8.y, &decompressed_sig.r_b8.y);
assert_eq!(&sig.s, &decompressed_sig.s);
let v = verify_mimc(pk.clone(), decompressed_sig, msg);
assert_eq!(v, true);
}
}
// #[test]
// fn test_point_decompress_loop() {
// for _ in 0..5 {
// let mut rng = rand::thread_rng();
// let sk_raw = rng.gen_biguint(1024).to_bigint().unwrap();
// let mut hasher = Blake2b::new();
// let (_, sk_raw_bytes) = sk_raw.to_bytes_be();
// hasher.input(sk_raw_bytes);
// let mut h = hasher.result();
//
// h[0] = h[0] & 0xF8;
// h[31] = h[31] & 0x7F;
// h[31] = h[31] | 0x40;
//
// let sk = BigInt::from_bytes_le(Sign::Plus, &h[..]);
// let point = B8.mul_scalar(&sk).unwrap();
// let cmp_point = point.compress();
// let dcmp_point = decompress_point(cmp_point).unwrap();
//
// assert_eq!(&point.x, &dcmp_point.x);
// assert_eq!(&point.y, &dcmp_point.y);
// }
// }
// #[test]
// fn test_signature_compress_decompress() {
// let sk = new_key();
// let pk = sk.public().unwrap();
//
// for i in 0..5 {
// let msg_raw = "123456".to_owned() + &i.to_string();
// let msg = BigInt::parse_bytes(msg_raw.as_bytes(), 10).unwrap();
// let sig = sk.sign_mimc(msg.clone()).unwrap();
//
// let compressed_sig = sig.compress();
// let decompressed_sig = decompress_signature(&compressed_sig).unwrap();
// assert_eq!(&sig.r_b8.x, &decompressed_sig.r_b8.x);
// assert_eq!(&sig.r_b8.y, &decompressed_sig.r_b8.y);
// assert_eq!(&sig.s, &decompressed_sig.s);
//
// let v = verify_mimc(pk.clone(), decompressed_sig, msg);
// assert_eq!(v, true);
// }
// }
#[test]
fn test_schnorr_signature() {

Loading…
Cancel
Save