Skip to content
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

Rust Bindings: Adding aggregate_iter #199

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
88 changes: 34 additions & 54 deletions bindings/rust/benches/blst_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn bench_verify_multi_aggregate(c: &mut Criterion) {
let seed = [0u8; 32];
let mut rng = ChaCha20Rng::from_seed(seed);

let num_sigs = vec![8, 16, 32, 64, 128];
let num_sigs = [8, 16, 32, 64, 128];
let pks_per_sig = 3;

for n in num_sigs.iter() {
Expand All @@ -81,8 +81,6 @@ fn bench_verify_multi_aggregate(c: &mut Criterion) {
.collect();
let pks_i =
sks_i.iter().map(|sk| sk.sk_to_pk()).collect::<Vec<_>>();
let pks_refs_i: Vec<&PublicKey> =
pks_i.iter().map(|pk| pk).collect();

// Create random message for pks to all sign
let msg_len = (rng.next_u64() & 0x3F) + 1;
Expand All @@ -96,9 +94,7 @@ fn bench_verify_multi_aggregate(c: &mut Criterion) {
.collect::<Vec<Signature>>();

// Aggregate signature
let sig_refs_i =
sigs_i.iter().map(|s| s).collect::<Vec<&Signature>>();
let agg_i = match AggregateSignature::aggregate(&sig_refs_i, false)
let agg_i = match AggregateSignature::aggregate_iter(&sigs_i, false)
{
Ok(agg_i) => agg_i,
Err(err) => panic!("aggregate failure: {:?}", err),
Expand All @@ -107,7 +103,7 @@ fn bench_verify_multi_aggregate(c: &mut Criterion) {

// aggregate public keys and push into vec
let agg_pk_i =
match AggregatePublicKey::aggregate(&pks_refs_i, false) {
match AggregatePublicKey::aggregate_iter(&pks_i, false) {
Ok(agg_pk_i) => agg_pk_i,
Err(err) => panic!("aggregate failure: {:?}", err),
};
Expand Down Expand Up @@ -157,26 +153,18 @@ fn bench_fast_aggregate_verify(c: &mut Criterion) {
let mut msg = vec![0u8; msg_len as usize];
rng.fill_bytes(&mut msg);

let sizes = vec![8, 16, 32, 64, 128];
let sizes = [8, 16, 32, 64, 128];

let bds: Vec<_> = (0..sizes[sizes.len() - 1])
.map(|_| gen_bench_data_for_msg(&mut rng, &msg))
.collect();

for size in sizes.iter() {
let pks_refs = bds
.iter()
.take(*size)
.map(|s| &s.pk)
.collect::<Vec<&PublicKey>>();
for size in sizes {
let pks_refs = bds[..size].iter().map(|s| &s.pk).collect::<Vec<_>>();

let sig_refs = bds
.iter()
.take(*size)
.map(|s| &s.sig)
.collect::<Vec<&Signature>>();
let sig_refs = bds[..size].iter().map(|s| &s.sig);

let agg = match AggregateSignature::aggregate(&sig_refs, false) {
let agg = match AggregateSignature::aggregate_iter(sig_refs, false) {
Ok(agg) => agg,
Err(err) => panic!("aggregate failure: {:?}", err),
};
Expand All @@ -196,7 +184,7 @@ fn bench_fast_aggregate_verify(c: &mut Criterion) {
&agg_ver,
|b, (a, p, m, d)| {
b.iter(|| {
let result = a.fast_aggregate_verify(true, &m, &d, &p);
let result = a.fast_aggregate_verify(true, m, d, p);
assert_eq!(result, BLST_ERROR::BLST_SUCCESS);
});
},
Expand Down Expand Up @@ -224,33 +212,27 @@ fn bench_aggregate_verify(c: &mut Criterion) {
let seed = [0u8; 32];
let mut rng = ChaCha20Rng::from_seed(seed);

let sizes = vec![8, 16, 32, 64, 128];
let sizes = [8, 16, 32, 64, 128];
// [10, 50, 100, 300, 1000, 4000];

let bds: Vec<_> = (0..sizes[sizes.len() - 1])
.map(|_| gen_bench_data(&mut rng))
.collect();

for size in sizes.iter() {
let msgs_refs = bds
for size in sizes {
let msgs_refs = bds[..size]
.iter()
.take(*size)
.map(|s| s.msg.as_slice())
.collect::<Vec<&[u8]>>();

let pks_refs = bds
let pks_refs = bds[..size]
.iter()
.take(*size)
.map(|s| &s.pk)
.collect::<Vec<&PublicKey>>();

let sig_refs = bds
.iter()
.take(*size)
.map(|s| &s.sig)
.collect::<Vec<&Signature>>();
let sig_refs = bds[..size].iter().map(|s| &s.sig);

let agg = match AggregateSignature::aggregate(&sig_refs, false) {
let agg = match AggregateSignature::aggregate_iter(sig_refs, false) {
Ok(agg) => agg,
Err(err) => panic!("aggregate failure: {:?}", err),
};
Expand All @@ -262,7 +244,7 @@ fn bench_aggregate_verify(c: &mut Criterion) {
&agg_ver,
|b, (a, p, m, d)| {
b.iter(|| {
let result = a.aggregate_verify(true, &m, &d, &p, false);
let result = a.aggregate_verify(true, m, d, p, false);
assert_eq!(result, BLST_ERROR::BLST_SUCCESS);
});
},
Expand All @@ -278,36 +260,34 @@ fn bench_aggregate(c: &mut Criterion) {
let seed = [0u8; 32];
let mut rng = ChaCha20Rng::from_seed(seed);

let sizes: [usize; 6] = [10, 50, 100, 300, 1000, 4000];
let sizes = [10, 50, 100, 300, 1000, 4000];

let bds: Vec<_> = (0..4000).map(|_| gen_bench_data(&mut rng)).collect();

for size in sizes.iter() {
let sig_refs = bds
.iter()
.take(*size)
.map(|s| &s.sig)
.collect::<Vec<&Signature>>();

for size in sizes {
group.bench_with_input(
BenchmarkId::new("aggregate_signature", size),
&sig_refs,
|b, s| {
b.iter(|| AggregateSignature::aggregate(&s, false));
&bds,
|b, bds| {
b.iter(|| {
AggregateSignature::aggregate_iter(
bds[..size].iter().map(|s| &s.sig),
false,
)
});
},
);

let pks_refs = bds
.iter()
.take(*size)
.map(|s| &s.pk)
.collect::<Vec<&PublicKey>>();

group.bench_with_input(
BenchmarkId::new("aggregate_public_key", size),
&pks_refs,
|b, p| {
b.iter(|| AggregatePublicKey::aggregate(&p, false));
&bds,
|b, bds| {
b.iter(|| {
AggregatePublicKey::aggregate_iter(
bds[..size].iter().map(|s| &s.pk),
false,
)
});
},
);
}
Expand Down
64 changes: 38 additions & 26 deletions bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,14 +926,22 @@ macro_rules! sig_variant_impl {
pks: &[&PublicKey],
pks_validate: bool,
) -> Result<Self, BLST_ERROR> {
if pks.len() == 0 {
return Err(BLST_ERROR::BLST_AGGR_TYPE_MISMATCH);
}
// Convert Iterator<Item = &&PublicKey> To Iterator<Item = &PublicKey>
Self::aggregate_iter(pks.iter().copied(), pks_validate)
}

pub fn aggregate_iter<'a>(
pks: impl IntoIterator<Item = &'a PublicKey>,
pks_validate: bool,
) -> Result<Self, BLST_ERROR> {
let mut pks = pks.into_iter();
let first =
pks.next().ok_or(BLST_ERROR::BLST_AGGR_TYPE_MISMATCH)?;
if pks_validate {
pks[0].validate()?;
first.validate()?;
}
let mut agg_pk = AggregatePublicKey::from_public_key(pks[0]);
for s in pks.iter().skip(1) {
let mut agg_pk = AggregatePublicKey::from_public_key(first);
for s in pks {
if pks_validate {
s.validate()?;
}
Expand Down Expand Up @@ -1425,17 +1433,25 @@ macro_rules! sig_variant_impl {
sigs: &[&Signature],
sigs_groupcheck: bool,
) -> Result<Self, BLST_ERROR> {
if sigs.len() == 0 {
return Err(BLST_ERROR::BLST_AGGR_TYPE_MISMATCH);
}
// Convert Iterator<Item = &&Signature> To Iterator<Item = &Signature>
Self::aggregate_iter(sigs.iter().copied(), sigs_groupcheck)
}

pub fn aggregate_iter<'a>(
sigs: impl IntoIterator<Item = &'a Signature>,
sigs_groupcheck: bool,
) -> Result<Self, BLST_ERROR> {
let mut sigs = sigs.into_iter();
let first =
sigs.next().ok_or(BLST_ERROR::BLST_AGGR_TYPE_MISMATCH)?;
if sigs_groupcheck {
// We can't actually judge if input is individual or
// aggregated signature, so we can't enforce infinity
// check.
sigs[0].validate(false)?;
first.validate(false)?;
}
let mut agg_sig = AggregateSignature::from_signature(sigs[0]);
for s in sigs.iter().skip(1) {
let mut agg_sig = AggregateSignature::from_signature(first);
for s in sigs {
if sigs_groupcheck {
s.validate(false)?;
}
Expand Down Expand Up @@ -1609,9 +1625,8 @@ macro_rules! sig_variant_impl {
.collect::<Vec<BLST_ERROR>>();
assert_ne!(errs, vec![BLST_ERROR::BLST_SUCCESS; num_msgs]);

let sig_refs =
sigs.iter().map(|s| s).collect::<Vec<&Signature>>();
let agg = match AggregateSignature::aggregate(&sig_refs, true) {
let agg = match AggregateSignature::aggregate_iter(&sigs, true)
{
Ok(agg) => agg,
Err(err) => panic!("aggregate failure: {:?}", err),
};
Expand Down Expand Up @@ -1651,8 +1666,7 @@ macro_rules! sig_variant_impl {
.iter()
.map(|sk| sk.sk_to_pk())
.collect::<Vec<_>>();
let pks_refs_i: Vec<&PublicKey> =
pks_i.iter().map(|pk| pk).collect();
let pks_refs_i: Vec<&PublicKey> = pks_i.iter().collect();

// Create random message for pks to all sign
let msg_len = (rng.next_u64() & 0x3F) + 1;
Expand All @@ -1678,14 +1692,12 @@ macro_rules! sig_variant_impl {
vec![BLST_ERROR::BLST_SUCCESS; num_pks_per_sig]
);

let sig_refs_i =
sigs_i.iter().map(|s| s).collect::<Vec<&Signature>>();
let agg_i =
match AggregateSignature::aggregate(&sig_refs_i, false)
{
Ok(agg_i) => agg_i,
Err(err) => panic!("aggregate failure: {:?}", err),
};
let agg_i = match AggregateSignature::aggregate_iter(
&sigs_i, false,
) {
Ok(agg_i) => agg_i,
Err(err) => panic!("aggregate failure: {:?}", err),
};

// Test current aggregate signature
sigs.push(agg_i.to_signature());
Expand All @@ -1710,7 +1722,7 @@ macro_rules! sig_variant_impl {

// aggregate public keys and push into vec
let agg_pk_i =
match AggregatePublicKey::aggregate(&pks_refs_i, false)
match AggregatePublicKey::aggregate_iter(&pks_i, false)
{
Ok(agg_pk_i) => agg_pk_i,
Err(err) => panic!("aggregate failure: {:?}", err),
Expand Down