From 985d001ffc26947264c6f7854c3759bdc1c91d93 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Thu, 10 Sep 2020 19:11:58 +0200 Subject: [PATCH] Add rust JubJub & BabyJubJub EdDSA benchmarks --- poseidon-rs-examples/src/lib.rs | 17 +++++++ rust-crypto-benchmarks/.gitignore | 2 + rust-crypto-benchmarks/Cargo.toml | 24 ++++++++++ rust-crypto-benchmarks/README.md | 5 +++ rust-crypto-benchmarks/benches/bench_eddsa.rs | 44 +++++++++++++++++++ rust-crypto-benchmarks/src/lib.rs | 7 +++ 6 files changed, 99 insertions(+) create mode 100644 rust-crypto-benchmarks/.gitignore create mode 100644 rust-crypto-benchmarks/Cargo.toml create mode 100644 rust-crypto-benchmarks/README.md create mode 100644 rust-crypto-benchmarks/benches/bench_eddsa.rs create mode 100644 rust-crypto-benchmarks/src/lib.rs diff --git a/poseidon-rs-examples/src/lib.rs b/poseidon-rs-examples/src/lib.rs index 358bae7..d1465fa 100644 --- a/poseidon-rs-examples/src/lib.rs +++ b/poseidon-rs-examples/src/lib.rs @@ -44,4 +44,21 @@ mod tests { "Fr(0x28410c403c92a9f18d1f27b22218b3649b3be8640dc160ad53bd21cf02f98d81)" ); } + + #[test] + fn test_usage_bytes() { + let msg = "hello"; + let b: BigInt = BigInt::parse_bytes(msg.as_bytes(), 10).unwrap(); + let v: Fr = Fr::from_str(&b.to_string()).unwrap(); + + let mut to_hash: Vec = Vec::new(); + to_hash.push(v); + + let poseidon = Poseidon::new(); + let h = poseidon.hash(to_hash).unwrap(); + assert_eq!( + h.to_string(), + "Fr(0x28410c403c92a9f18d1f27b22218b3649b3be8640dc160ad53bd21cf02f98d81)" + ); + } } diff --git a/rust-crypto-benchmarks/.gitignore b/rust-crypto-benchmarks/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/rust-crypto-benchmarks/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/rust-crypto-benchmarks/Cargo.toml b/rust-crypto-benchmarks/Cargo.toml new file mode 100644 index 0000000..7d6651c --- /dev/null +++ b/rust-crypto-benchmarks/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "rust-crypto-benchmarks" +version = "0.0.1" +authors = ["arnaucube "] +edition = "2018" + + +[dependencies] +eddsa = {git = "https://github.com/dusk-network/EdDSA.git", branch = "master"} +dusk-bls12_381 = "0.1.3" +babyjubjub-rs = {git = "https://github.com/arnaucube/babyjubjub-rs.git", branch = "master"} +ff = {package="ff_ce" , version="0.11", features = ["derive"]} +rand = "0.4" +rand7 = {package="rand", version="0.7.0"} +num = "0.2.0" +num-bigint = {version = "0.2.2", features = ["rand"]} + + +[dev-dependencies] +criterion = "0.3" + +[[bench]] +name = "bench_eddsa" +harness = false diff --git a/rust-crypto-benchmarks/README.md b/rust-crypto-benchmarks/README.md new file mode 100644 index 0000000..3d5c762 --- /dev/null +++ b/rust-crypto-benchmarks/README.md @@ -0,0 +1,5 @@ +# rust-crypto-benchmarks + +``` +cargo bench +``` diff --git a/rust-crypto-benchmarks/benches/bench_eddsa.rs b/rust-crypto-benchmarks/benches/bench_eddsa.rs new file mode 100644 index 0000000..8468fe9 --- /dev/null +++ b/rust-crypto-benchmarks/benches/bench_eddsa.rs @@ -0,0 +1,44 @@ +use criterion::{criterion_group, criterion_main, Criterion}; + +// JubJub +use dusk_bls12_381::Scalar; +use eddsa::{KeyPair, Message, PublicKey}; +extern crate rand7; + +// BabyJubJub +extern crate rand; +#[macro_use] +extern crate ff; +use ff::*; +extern crate num; +extern crate num_bigint; +use babyjubjub_rs::{utils, Point}; +use num_bigint::{BigInt, Sign, ToBigInt}; + +fn criterion_benchmark(c: &mut Criterion) { + let mut m: [u8; 32] = rand::random::<[u8; 32]>(); + m[31] = 0; + println!("m {:?}", m); + + // JubJub + let keypair = KeyPair::new(&mut rand7::thread_rng()).unwrap(); + let message = Message(Scalar::from_bytes(&m).unwrap()); + c.bench_function("JubJub EdDSA sign", |b| b.iter(|| keypair.sign(&message))); + let a = keypair.sign(&message); + c.bench_function("JubJub EdDSA verify", |b| { + b.iter(|| a.verify(&message, &keypair.public_key)) + }); + + // BabyJubJub + let sk = babyjubjub_rs::new_key(); + let pk = sk.public().unwrap(); + let msg = BigInt::from_bytes_le(Sign::Plus, &m); + c.bench_function("BabyJubJub EdDSA sign", |b| b.iter(|| sk.sign(msg.clone()))); + let sig = sk.sign(msg.clone()).unwrap(); + c.bench_function("BabyJubJub EdDSA verify", |b| { + b.iter(|| babyjubjub_rs::verify(pk.clone(), sig.clone(), msg.clone())) + }); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); diff --git a/rust-crypto-benchmarks/src/lib.rs b/rust-crypto-benchmarks/src/lib.rs new file mode 100644 index 0000000..31e1bb2 --- /dev/null +++ b/rust-crypto-benchmarks/src/lib.rs @@ -0,0 +1,7 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +}