-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4e5616
commit 0bdfaa4
Showing
7 changed files
with
105 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use example_crypto::openssl::sign::Ed25519Signer; | ||
use rand::{thread_rng, RngCore}; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
|
||
fn bench_sign(c: &mut Criterion) { | ||
// eddsa | ||
c.bench_function("openssl::sign::ed25519", |b| { | ||
const KEY_COUNT: usize = 256; | ||
const DATA_COUNT: usize = 512; | ||
|
||
// build 256 signers | ||
let keys: Vec<Ed25519Signer> = (0..KEY_COUNT) | ||
.into_iter() | ||
.map(|_| Ed25519Signer::random()) | ||
.collect(); | ||
|
||
let data: Vec<[u8; 32]> = (0..DATA_COUNT) | ||
.into_iter() | ||
.map(|_| { | ||
let mut d = [0; 32]; | ||
thread_rng().fill_bytes(&mut d); | ||
d | ||
}) | ||
.collect(); | ||
|
||
let (signer_index, data_index) = (AtomicUsize::new(0), AtomicUsize::new(0)); | ||
|
||
b.iter(|| { | ||
let (current_signer, current_data) = ( | ||
signer_index.fetch_add(1, Ordering::AcqRel) % KEY_COUNT, | ||
data_index.fetch_add(1, Ordering::AcqRel) % DATA_COUNT, | ||
); | ||
|
||
// get em fast | ||
let _sig = unsafe { | ||
keys.get_unchecked(current_signer) | ||
.sign(data.get_unchecked(current_data)) | ||
}; | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group! { | ||
name = sign; | ||
config = Criterion::default(); | ||
targets = bench_sign | ||
} | ||
|
||
criterion_main!(sign); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod keygen; | ||
pub mod sign; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use openssl::pkey::{Id, PKey, Private}; | ||
use openssl::sign::Signer; | ||
use rand::{thread_rng, RngCore}; | ||
|
||
pub struct Ed25519Signer { | ||
key: PKey<Private>, | ||
} | ||
|
||
impl Ed25519Signer { | ||
/// Generate a new signer with a randomly generated key. | ||
/// | ||
/// All values are legal in EdDSA keys, so it's simply generating 32 bytes from the CSPNG. | ||
pub fn random() -> Self { | ||
// generate 32 bytes of random data on the stack | ||
let key_bytes = { | ||
let mut k = [0; 32]; | ||
thread_rng().fill_bytes(&mut k); | ||
k | ||
}; | ||
|
||
// construct a pkey | ||
let key = PKey::private_key_from_raw_bytes(&key_bytes, Id::ED25519) | ||
.expect("unable to create ed25519 private key"); | ||
|
||
Self { key } | ||
} | ||
|
||
pub fn sign(&self, data: &[u8]) -> [u8; 64] { | ||
let mut sig = [0; 64]; | ||
|
||
let _signature_length = Signer::new_without_digest(&self.key) | ||
.expect("unable to create signer") | ||
.sign_oneshot(&mut sig, data) | ||
.expect("unable to sign data"); | ||
|
||
sig | ||
} | ||
} |