Skip to content

More benchmarks #52

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

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@ documentation = "https://ip-v8.github.io/rust-ipv8/ipv8/"
repository = "https://github.com/ip-v8/rust-ipv8"
readme = "readme.md"
license-file = "license.md"

[lib]
crate-type = ["lib", "cdylib"]

exclude = [
".editorconfig",
".travis.yml",
".codeclimate.yml"
]

[lib]
crate-type = ["lib", "cdylib"]

[badges]
travis-ci = { repository = "ip-v8/rust-ipv8", branch = "develop" }
maintenance = { status = "experimental" }
Expand All @@ -42,6 +41,10 @@ simulacrum = "0.3.1"
name = "bench_crypto"
harness = false

[[bench]]
name = "networking_throughput"
harness = false

[profile.release]
lto = true # Enables link time optimization (allows for inlining cross-crate)
opt-level = 3 # Ensures optimization level is set to the maximum
Expand Down
57 changes: 51 additions & 6 deletions benches/bench_crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use ipv8::crypto::keytypes::{PrivateKey, PublicKey};
use ipv8::crypto::signature::Signature;
use rust_sodium::crypto::sign::ed25519;

fn e25519_benchmark(c: &mut Criterion) {
c.bench_function("bench: ed25519", |b| {
fn create_sign_verify(c: &mut Criterion) {
c.bench_function("ed25519: creation + sign + verify", |b| {
b.iter(|| {
let seed = ed25519::Seed::from_slice(&[
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
Expand All @@ -26,8 +26,7 @@ fn e25519_benchmark(c: &mut Criterion) {
assert_ne!(e_pkey, pkey);
assert_ne!(e_skey, skey);

let sig =
Signature::from_bytes(&[42, 43, 44], PrivateKey::Ed25519(e_skey, skey)).unwrap();
let sig = Signature::from_bytes(&[42, 43, 44], &PrivateKey(e_skey, skey)).unwrap();
assert_eq!(
vec![
31, 14, 50, 234, 129, 186, 124, 84, 223, 67, 233, 173, 116, 95, 218, 136, 149,
Expand All @@ -38,10 +37,56 @@ fn e25519_benchmark(c: &mut Criterion) {
sig.signature
);

assert!(sig.verify(&[42, 43, 44], PublicKey::Ed25519(pkey, pkey)));
assert!(sig.verify(&[42, 43, 44], PublicKey(pkey, pkey)));
})
});
}

criterion_group!(benches, e25519_benchmark,);
fn sign(c: &mut Criterion) {
let seed = ed25519::Seed::from_slice(&[
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31,
])
.unwrap();
let (_, s_skey) = ed25519::keypair_from_seed(&seed);

let seed = ed25519::Seed::from_slice(&[
1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31,
])
.unwrap();
let (_, e_skey) = ed25519::keypair_from_seed(&seed);

let p = PrivateKey(e_skey, s_skey);

// Only bench the actual verification
c.bench_function("ed25519 sign", move |b| {
b.iter(|| Signature::from_bytes(&[42, 43, 44], &p).unwrap())
});
}

fn verify(c: &mut Criterion) {
let seed = ed25519::Seed::from_slice(&[
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31,
])
.unwrap();
let (s_pkey, s_skey) = ed25519::keypair_from_seed(&seed);

let seed = ed25519::Seed::from_slice(&[
1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31,
])
.unwrap();
let (e_pkey, e_skey) = ed25519::keypair_from_seed(&seed);

let sig = Signature::from_bytes(&[42, 43, 44], &PrivateKey(e_skey, s_skey)).unwrap();

// Only bench the actual verification
c.bench_function("ed25519 verify", move |b| {
b.iter(|| sig.verify(&[42, 43, 44], PublicKey(s_pkey, e_pkey)))
});
}

criterion_group!(benches, create_sign_verify, verify, sign);
criterion_main!(benches);
69 changes: 69 additions & 0 deletions benches/networking_throughput.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use criterion::*;
use ipv8::serialization::Packet;
use ipv8::serialization::header::Header;
use ipv8::payloads::timedistributionpayload::TimeDistributionPayload;
use ipv8::payloads::introductionresponsepayload::IntroductionResponsePayload;

fn throughput(c: &mut Criterion) {
static BYTES: [u8; 208] = [
0x00, 0x02, 0xba, 0xf3, 0x0e, 0xd9, 0x19, 0x2b, 0xa3, 0x54, 0xcd, 0xd7, 0xb1, 0x73, 0xe0,
0xef, 0x2c, 0x32, 0x80, 0x27, 0xf1, 0xd3, 0xf5, 0x00, 0x4a, 0x4c, 0x69, 0x62, 0x4e, 0x61,
0x43, 0x4c, 0x50, 0x4b, 0x3a, 0x51, 0xe7, 0x12, 0xc4, 0xeb, 0x8a, 0xc2, 0x5a, 0xe3, 0xa5,
0x68, 0x24, 0x08, 0xb2, 0xad, 0xbd, 0x6b, 0x78, 0xa4, 0x25, 0x54, 0x7f, 0x26, 0x85, 0xcf,
0xdf, 0x1e, 0xe9, 0x27, 0x0c, 0xbe, 0x7e, 0xc3, 0x36, 0xc4, 0x16, 0x0f, 0xf5, 0x72, 0x05,
0x4c, 0x87, 0x78, 0x42, 0xbe, 0x37, 0x73, 0x50, 0x45, 0xa9, 0x3b, 0xc4, 0xe2, 0x04, 0x15,
0x31, 0x6f, 0xdb, 0x14, 0x71, 0x61, 0xa2, 0xd7, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x51, 0xab, 0x1b, 0xc2, 0x2b, 0x67, 0xc0, 0xa8, 0x01, 0x4b, 0x1f, 0x9a, 0xc0,
0xa8, 0x01, 0x4b, 0x1f, 0x9a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xd2, 0x0e, 0x00, 0x00, 0x00, 0x00, 0xce, 0xe9, 0x32, 0x6b, 0x9d, 0xd4,
0xbb, 0x8a, 0xaf, 0x8d, 0xc0, 0x39, 0x28, 0x8e, 0xbf, 0xc2, 0x4a, 0x10, 0xad, 0xc3, 0x7a,
0xf1, 0xd9, 0xc8, 0x04, 0x17, 0x72, 0x5d, 0x2d, 0x3e, 0x5e, 0x07, 0x52, 0x4d, 0xab, 0x6e,
0xa7, 0x1b, 0x17, 0x5a, 0x77, 0x5d, 0xb5, 0xd8, 0x91, 0x0c, 0x2b, 0x4b, 0xc8, 0xbb, 0x03,
0xd3, 0x55, 0xed, 0x10, 0x26, 0xdd, 0xbb, 0xd8, 0xb2, 0x3b, 0xfd, 0xfc, 0x01,
];

c.bench(
"throughput",
Benchmark::new("simple-deserialize", |b| {
b.iter(|| {
let data = Packet(BYTES.to_vec());
let mut de = data.start_deserialize();
let _: Header = de.pop_header().unwrap();
// de.verify();
let _: TimeDistributionPayload = de.next_payload().unwrap();
let _: IntroductionResponsePayload = de.next_payload().unwrap();
})
})
.throughput(Throughput::Bytes(BYTES.len() as u32)),
);

c.bench(
"throughput",
Benchmark::new("only-verify", |b| {
b.iter(|| {
let data = Packet(BYTES.to_vec());
let de = data.start_deserialize();
de.skip_header().unwrap().verify();
})
})
.throughput(Throughput::Bytes(BYTES.len() as u32)),
);

c.bench(
"throughput",
Benchmark::new("deserialize+verify", |b| {
b.iter(|| {
let data = Packet(BYTES.to_vec());
let mut de = data.start_deserialize();
let _: Header = de.pop_header().unwrap();
de.verify();
let _: TimeDistributionPayload = de.next_payload().unwrap();
let _: IntroductionResponsePayload = de.next_payload().unwrap();
})
})
.throughput(Throughput::Bytes(BYTES.len() as u32)),
);
}

criterion_group!(benches, throughput);
criterion_main!(benches);
2 changes: 1 addition & 1 deletion src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ create_error!(
/// wrapper function for signing data using ed25519
pub fn create_signature_ed25519(
data: &[u8],
skey: ed25519::SecretKey,
skey: &ed25519::SecretKey,
) -> Result<ed25519::Signature, Box<dyn Error>> {
Ok(ed25519::sign_detached(data, &skey))
}
Expand Down
6 changes: 3 additions & 3 deletions src/crypto/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ impl Serialize for Signature {

impl Signature {
/// Signature can be created from its binary string (bytes)
pub fn from_bytes(data: &[u8], skey: PrivateKey) -> Result<Self, Box<dyn Error>> {
pub fn from_bytes(data: &[u8], skey: &PrivateKey) -> Result<Self, Box<dyn Error>> {
// skey.1 is the verification key
let signature: Vec<u8> = create_signature_ed25519(data, skey.1)?.as_ref().to_owned();
let signature: Vec<u8> = create_signature_ed25519(data, &skey.1)?.as_ref().to_owned();
Ok(Self { signature })
}

Expand Down Expand Up @@ -85,7 +85,7 @@ pub mod tests {
assert_ne!(e_pkey, pkey);
assert_ne!(e_skey, skey);

let sig = Signature::from_bytes(&[42, 43, 44], PrivateKey(e_skey, skey)).unwrap();
let sig = Signature::from_bytes(&[42, 43, 44], &PrivateKey(e_skey, skey)).unwrap();
assert_eq!(
vec![
31, 14, 50, 234, 129, 186, 124, 84, 223, 67, 233, 173, 116, 95, 218, 136, 149, 223,
Expand Down
4 changes: 2 additions & 2 deletions src/serialization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Packet {
///
/// To verify signatures first transform the Packet into a PacketIterator with Packet.deserialize_multiple and then use the PacketIterator.verify() or
/// PacketIterator.verify_with() method.
pub fn sign(mut self, skey: PrivateKey) -> Result<Self, Box<dyn Error>> {
pub fn sign(mut self, skey: &PrivateKey) -> Result<Self, Box<dyn Error>> {
let signature = Signature::from_bytes(&*self.0, skey)?;
self.add(&signature)?;
// now this packet *must not* be modified anymore
Expand Down Expand Up @@ -276,7 +276,7 @@ mod tests {
let skey = PrivateKey(e_skey_tmp, skey_tmp);
let pkey = PublicKey(e_pkey_tmp, pkey_tmp);

let signed = packet.sign(skey).unwrap();
let signed = packet.sign(&skey).unwrap();

let mut deser_iterator = signed.start_deserialize();
let valid = deser_iterator.verify_with(pkey);
Expand Down