|
10 | 10 |
|
11 | 11 | use std::str::FromStr; |
12 | 12 |
|
| 13 | +use async_std::task::block_on; |
| 14 | +use criterion::async_executor::AsyncStdExecutor; |
13 | 15 | use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
14 | 16 | use ethereum_types::Address; |
15 | | -use k256::ecdsa::{SigningKey, VerifyingKey}; |
| 17 | +use ethers::signers::{LocalWallet, Signer, Wallet}; |
| 18 | +use ethers_core::k256::ecdsa::SigningKey; |
16 | 19 | use rand_core::OsRng; |
17 | 20 | use tap_core::{ |
18 | 21 | eip_712_signed_message::EIP712SignedMessage, |
19 | 22 | receipt_aggregate_voucher::ReceiptAggregateVoucher, tap_receipt::Receipt, |
20 | 23 | }; |
21 | 24 |
|
22 | | -pub fn create_and_sign_receipt( |
| 25 | +pub async fn create_and_sign_receipt( |
23 | 26 | allocation_id: Address, |
24 | 27 | value: u128, |
25 | | - signing_key: &SigningKey, |
| 28 | + wallet: &Wallet<SigningKey>, |
26 | 29 | ) -> 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() |
28 | 33 | } |
29 | 34 |
|
30 | 35 | 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); |
33 | 37 |
|
34 | 38 | // 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; |
38 | 41 |
|
39 | 42 | 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 | + }) |
41 | 50 | }); |
42 | 51 |
|
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)); |
44 | 53 |
|
45 | 54 | 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 | + }) |
47 | 60 | }); |
48 | 61 |
|
49 | 62 | let mut rav_group = c.benchmark_group("Create RAV with varying input sizes"); |
50 | 63 |
|
51 | 64 | 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<_>>(); |
57 | 68 |
|
58 | 69 | rav_group.bench_function( |
59 | 70 | &format!("Create RAV w/ 2^{} receipt's", log_number_of_receipts), |
60 | 71 | |b| { |
61 | 72 | 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 | + ) |
63 | 78 | }) |
64 | 79 | }, |
65 | 80 | ); |
66 | | - let signed_rav = black_box(EIP712SignedMessage::new( |
| 81 | + |
| 82 | + let signed_rav = block_on(EIP712SignedMessage::new( |
67 | 83 | ReceiptAggregateVoucher::aggregate_receipts(allocation_id, &receipts, None).unwrap(), |
68 | | - &signing_key, |
| 84 | + &wallet, |
69 | 85 | )) |
70 | 86 | .unwrap(); |
| 87 | + |
71 | 88 | rav_group.bench_function( |
72 | 89 | &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()))), |
74 | 91 | ); |
75 | 92 | } |
76 | 93 | } |
|
0 commit comments