Skip to content

Commit 96cdf24

Browse files
authored
fix: verification benchmarks (#114)
Signed-off-by: Ozgur Akkurt <[email protected]>
1 parent c57f6d7 commit 96cdf24

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

tap_core/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ ethers = "2.0.0"
1717
ethers-core = "2.0.0"
1818
ethers-contract = "2.0.0"
1919
ethers-contract-derive = "2.0.0"
20-
criterion = "0.4.0"
20+
2121
strum = "0.24.1"
2222
strum_macros = "0.24.3"
2323
futures = "0.3.17"
2424

25+
[dev-dependencies]
26+
criterion = { version = "0.5", features = ["async_std"] }
2527

2628
[[bench]]
2729
name = 'timeline_aggretion_protocol_benchmark'

tap_core/benches/timeline_aggretion_protocol_benchmark.rs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,84 @@
1010
1111
use std::str::FromStr;
1212

13+
use async_std::task::block_on;
14+
use criterion::async_executor::AsyncStdExecutor;
1315
use criterion::{black_box, criterion_group, criterion_main, Criterion};
1416
use ethereum_types::Address;
15-
use k256::ecdsa::{SigningKey, VerifyingKey};
17+
use ethers::signers::{LocalWallet, Signer, Wallet};
18+
use ethers_core::k256::ecdsa::SigningKey;
1619
use rand_core::OsRng;
1720
use tap_core::{
1821
eip_712_signed_message::EIP712SignedMessage,
1922
receipt_aggregate_voucher::ReceiptAggregateVoucher, tap_receipt::Receipt,
2023
};
2124

22-
pub fn create_and_sign_receipt(
25+
pub async fn create_and_sign_receipt(
2326
allocation_id: Address,
2427
value: u128,
25-
signing_key: &SigningKey,
28+
wallet: &Wallet<SigningKey>,
2629
) -> EIP712SignedMessage<Receipt> {
27-
EIP712SignedMessage::new(Receipt::new(allocation_id, value).unwrap(), signing_key).unwrap()
30+
EIP712SignedMessage::new(Receipt::new(allocation_id, value).unwrap(), wallet)
31+
.await
32+
.unwrap()
2833
}
2934

3035
pub fn criterion_benchmark(c: &mut Criterion) {
31-
let signing_key = SigningKey::random(&mut OsRng);
32-
let verifying_key = VerifyingKey::from(&signing_key);
36+
let wallet = LocalWallet::new(&mut OsRng);
3337

3438
// Arbitrary values wrapped in black box to avoid compiler optimizing them out
35-
let allocation_id =
36-
black_box(Address::from_str("0xabababababababababababababababababababab").unwrap());
37-
let value = black_box(12345u128);
39+
let allocation_id = Address::from_str("0xabababababababababababababababababababab").unwrap();
40+
let value = 12345u128;
3841

3942
c.bench_function("Create Receipt", |b| {
40-
b.iter(|| create_and_sign_receipt(allocation_id, value, &signing_key))
43+
b.to_async(AsyncStdExecutor).iter(|| {
44+
create_and_sign_receipt(
45+
black_box(allocation_id),
46+
black_box(value),
47+
black_box(&wallet),
48+
)
49+
})
4150
});
4251

43-
let receipt = black_box(create_and_sign_receipt(allocation_id, value, &signing_key));
52+
let receipt = block_on(create_and_sign_receipt(allocation_id, value, &wallet));
4453

4554
c.bench_function("Validate Receipt", |b| {
46-
b.iter(|| receipt.check_signature(verifying_key))
55+
b.iter(|| {
56+
black_box(&receipt)
57+
.verify(black_box(wallet.address()))
58+
.unwrap()
59+
})
4760
});
4861

4962
let mut rav_group = c.benchmark_group("Create RAV with varying input sizes");
5063

5164
for log_number_of_receipts in 10..30 {
52-
let receipts = black_box(
53-
(0..2 ^ log_number_of_receipts)
54-
.map(|_| create_and_sign_receipt(allocation_id, value, &signing_key))
55-
.collect::<Vec<_>>(),
56-
);
65+
let receipts = (0..2 ^ log_number_of_receipts)
66+
.map(|_| block_on(create_and_sign_receipt(allocation_id, value, &wallet)))
67+
.collect::<Vec<_>>();
5768

5869
rav_group.bench_function(
5970
&format!("Create RAV w/ 2^{} receipt's", log_number_of_receipts),
6071
|b| {
6172
b.iter(|| {
62-
ReceiptAggregateVoucher::aggregate_receipts(allocation_id, &receipts, None)
73+
ReceiptAggregateVoucher::aggregate_receipts(
74+
black_box(allocation_id),
75+
black_box(&receipts),
76+
black_box(None),
77+
)
6378
})
6479
},
6580
);
66-
let signed_rav = black_box(EIP712SignedMessage::new(
81+
82+
let signed_rav = block_on(EIP712SignedMessage::new(
6783
ReceiptAggregateVoucher::aggregate_receipts(allocation_id, &receipts, None).unwrap(),
68-
&signing_key,
84+
&wallet,
6985
))
7086
.unwrap();
87+
7188
rav_group.bench_function(
7289
&format!("Validate RAV w/ 2^{} receipt's", log_number_of_receipts),
73-
|b| b.iter(|| signed_rav.check_signature(verifying_key)),
90+
|b| b.iter(|| black_box(&signed_rav).verify(black_box(wallet.address()))),
7491
);
7592
}
7693
}

0 commit comments

Comments
 (0)