-
Notifications
You must be signed in to change notification settings - Fork 39
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
Showing
30 changed files
with
1,016 additions
and
78 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,48 @@ | ||
#![allow(clippy::missing_docs_in_private_items)] | ||
#![allow(missing_docs)] | ||
|
||
extern crate crypto; | ||
|
||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use rand::Rng; | ||
use std::iter::repeat_with; | ||
|
||
fn bench_bn254(c: &mut Criterion) { | ||
use crypto::bn254::{AggregateSignature, PublicKey, SecretKey, Signature}; | ||
let mut rng = rand::thread_rng(); | ||
let mut group = c.benchmark_group("bn254"); | ||
group.bench_function("100 sig aggregation", |b| { | ||
b.iter(|| { | ||
let sks: Vec<SecretKey> = repeat_with(|| rng.gen::<SecretKey>()).take(100).collect(); | ||
let pks: Vec<PublicKey> = sks.iter().map(|k| k.public()).collect(); | ||
let msg = rng.gen::<[u8; 32]>(); | ||
let sigs: Vec<Signature> = sks.iter().map(|k| k.sign(&msg)).collect(); | ||
let agg = AggregateSignature::aggregate(&sigs); | ||
agg.verify(pks.iter().map(|pk| (&msg[..], pk))).unwrap() | ||
}); | ||
}); | ||
|
||
group.finish(); | ||
} | ||
|
||
#[allow(missing_docs)] | ||
fn bench_bls12_381(c: &mut Criterion) { | ||
use crypto::bls12_381::{AggregateSignature, PublicKey, SecretKey, Signature}; | ||
let mut rng = rand::thread_rng(); | ||
let mut group = c.benchmark_group("bls12_381"); | ||
group.bench_function("100 sig aggregation", |b| { | ||
b.iter(|| { | ||
let sks: Vec<SecretKey> = repeat_with(|| rng.gen::<SecretKey>()).take(100).collect(); | ||
let pks: Vec<PublicKey> = sks.iter().map(|k| k.public()).collect(); | ||
let msg = rng.gen::<[u8; 32]>(); | ||
let sigs: Vec<Signature> = sks.iter().map(|k| k.sign(&msg)).collect(); | ||
let agg = AggregateSignature::aggregate(&sigs)?; | ||
agg.verify(pks.iter().map(|pk| (&msg[..], pk))) | ||
}); | ||
}); | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, bench_bls12_381, bench_bn254); | ||
criterion_main!(benches); |
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,9 @@ | ||
/// Error type for generating and interacting with bn254. | ||
#[derive(Debug, thiserror::Error)] | ||
#[non_exhaustive] | ||
pub enum Error { | ||
#[error("Signature verification failure")] | ||
SignatureVerificationFailure, | ||
#[error("Aggregate signature verification failure")] | ||
AggregateSignatureVerificationFailure, | ||
} |
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,26 @@ | ||
//! Hash operations. | ||
use ark_bn254::{G1Affine, G1Projective}; | ||
use ark_ec::AffineRepr as _; | ||
use sha2::Digest as _; | ||
|
||
/// Hashes an arbitrary message and maps it to an elliptic curve point in G1. | ||
pub(crate) fn hash_to_g1(msg: &[u8]) -> G1Projective { | ||
for i in 0..256 { | ||
// Hash the message with the index as suffix. | ||
let bytes: [u8; 32] = sha2::Sha256::new() | ||
.chain_update(msg) | ||
.chain_update((i as u32).to_be_bytes()) | ||
.finalize() | ||
.into(); | ||
|
||
// Try to get a G1 point from the hash. The probability that this works is around 1/8. | ||
let p = G1Affine::from_random_bytes(&bytes); | ||
|
||
if let Some(p) = p { | ||
return p.into(); | ||
} | ||
} | ||
// It should be statistically infeasible to finish the loop without finding a point. | ||
unreachable!() | ||
} |
Oops, something went wrong.