Skip to content

Commit

Permalink
[zk-token-proof] Add benches for proof instructions (solana-labs#32071)
Browse files Browse the repository at this point in the history
* add bench test for `VerifyPubkeyValidity`

* add bench test for `VerifyRangeProofU64`

* add bench test for `VerifyWithdraw`

* add bench test for `VerifyZeroBalance`

* add bench test for `VerifyGroupedCiphertextValidity`

* add bench test for `VerifyCiphertextCommitmentEquality`

* add bench test for `VerifyCiphertextCiphertextEquality`

* add bench test for `VerifyBatchedGroupedCiphertextValidity`

* add bench test for `VerifyBatchedRangeProofu64`, `VerifyBatchedRangeProofU128`, `VerifyBatchedRangeProofU256`

* add bench test for `VerifyTransfer` and `VerifyTransferWithFee`

* use add `criterion` to workspace cargo

* add bench to ci
  • Loading branch information
samkim-crypto authored and wen-coding committed Aug 15, 2023
1 parent 5e3b9b1 commit 4c72a5e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 35 additions & 45 deletions programs/zk-token-proof/benches/verify_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn bench_range_proof_u64(c: &mut Criterion) {
fn bench_withdraw(c: &mut Criterion) {
let keypair = ElGamalKeypair::new_rand();
let current_balance: u64 = 77;
let current_ciphertext = keypair.pubkey().encrypt(current_balance);
let current_ciphertext = keypair.public.encrypt(current_balance);
let withdraw_amount: u64 = 55;
let proof_data = WithdrawData::new(
withdraw_amount,
Expand All @@ -61,7 +61,7 @@ fn bench_withdraw(c: &mut Criterion) {

fn bench_zero_balance(c: &mut Criterion) {
let keypair = ElGamalKeypair::new_rand();
let ciphertext = keypair.pubkey().encrypt(0_u64);
let ciphertext = keypair.public.encrypt(0_u64);
let proof_data = ZeroBalanceProofData::new(&keypair, &ciphertext).unwrap();

c.bench_function("zero_balance", |b| {
Expand All @@ -72,20 +72,17 @@ fn bench_zero_balance(c: &mut Criterion) {
}

fn bench_grouped_ciphertext_validity(c: &mut Criterion) {
let destination_keypair = ElGamalKeypair::new_rand();
let destination_pubkey = destination_keypair.pubkey();

let auditor_keypair = ElGamalKeypair::new_rand();
let auditor_pubkey = auditor_keypair.pubkey();
let destination_pubkey = ElGamalKeypair::new_rand().public;
let auditor_pubkey = ElGamalKeypair::new_rand().public;

let amount: u64 = 55;
let opening = PedersenOpening::new_rand();
let grouped_ciphertext =
GroupedElGamal::encrypt_with([destination_pubkey, auditor_pubkey], amount, &opening);
GroupedElGamal::encrypt_with([&destination_pubkey, &auditor_pubkey], amount, &opening);

let proof_data = GroupedCiphertext2HandlesValidityProofData::new(
destination_pubkey,
auditor_pubkey,
&destination_pubkey,
&auditor_pubkey,
&grouped_ciphertext,
amount,
&opening,
Expand All @@ -102,7 +99,7 @@ fn bench_grouped_ciphertext_validity(c: &mut Criterion) {
fn bench_ciphertext_commitment_equality(c: &mut Criterion) {
let keypair = ElGamalKeypair::new_rand();
let amount: u64 = 55;
let ciphertext = keypair.pubkey().encrypt(amount);
let ciphertext = keypair.public.encrypt(amount);
let (commitment, opening) = Pedersen::new(amount);

let proof_data = CiphertextCommitmentEqualityProofData::new(
Expand All @@ -126,16 +123,16 @@ fn bench_ciphertext_ciphertext_equality(c: &mut Criterion) {
let destination_keypair = ElGamalKeypair::new_rand();

let amount: u64 = 0;
let source_ciphertext = source_keypair.pubkey().encrypt(amount);
let source_ciphertext = source_keypair.public.encrypt(amount);

let destination_opening = PedersenOpening::new_rand();
let destination_ciphertext = destination_keypair
.pubkey()
.public
.encrypt_with(amount, &destination_opening);

let proof_data = CiphertextCiphertextEqualityProofData::new(
&source_keypair,
destination_keypair.pubkey(),
&destination_keypair.public,
&source_ciphertext,
&destination_ciphertext,
&destination_opening,
Expand All @@ -151,27 +148,30 @@ fn bench_ciphertext_ciphertext_equality(c: &mut Criterion) {
}

fn bench_batched_grouped_ciphertext_validity(c: &mut Criterion) {
let destination_keypair = ElGamalKeypair::new_rand();
let destination_pubkey = destination_keypair.pubkey();

let auditor_keypair = ElGamalKeypair::new_rand();
let auditor_pubkey = auditor_keypair.pubkey();
let destination_pubkey = ElGamalKeypair::new_rand().public;
let auditor_pubkey = ElGamalKeypair::new_rand().public;

let amount_lo: u64 = 11;
let amount_hi: u64 = 22;

let opening_lo = PedersenOpening::new_rand();
let opening_hi = PedersenOpening::new_rand();

let grouped_ciphertext_lo =
GroupedElGamal::encrypt_with([destination_pubkey, auditor_pubkey], amount_lo, &opening_lo);
let grouped_ciphertext_lo = GroupedElGamal::encrypt_with(
[&destination_pubkey, &auditor_pubkey],
amount_lo,
&opening_lo,
);

let grouped_ciphertext_hi =
GroupedElGamal::encrypt_with([destination_pubkey, auditor_pubkey], amount_hi, &opening_hi);
let grouped_ciphertext_hi = GroupedElGamal::encrypt_with(
[&destination_pubkey, &auditor_pubkey],
amount_hi,
&opening_hi,
);

let proof_data = BatchedGroupedCiphertext2HandlesValidityProofData::new(
destination_pubkey,
auditor_pubkey,
&destination_pubkey,
&auditor_pubkey,
&grouped_ciphertext_lo,
&grouped_ciphertext_hi,
amount_lo,
Expand Down Expand Up @@ -334,22 +334,18 @@ fn bench_batched_range_proof_u256(c: &mut Criterion) {

fn bench_transfer(c: &mut Criterion) {
let source_keypair = ElGamalKeypair::new_rand();

let destination_keypair = ElGamalKeypair::new_rand();
let destination_pubkey = destination_keypair.pubkey();

let auditor_keypair = ElGamalKeypair::new_rand();
let auditor_pubkey = auditor_keypair.pubkey();
let destination_pubkey = ElGamalKeypair::new_rand().public;
let auditor_pubkey = ElGamalKeypair::new_rand().public;

let spendable_balance: u64 = 77;
let spendable_ciphertext = source_keypair.pubkey().encrypt(spendable_balance);
let spendable_ciphertext = source_keypair.public.encrypt(spendable_balance);
let transfer_amount: u64 = 55;

let proof_data = TransferData::new(
transfer_amount,
(spendable_balance, &spendable_ciphertext),
&source_keypair,
(destination_pubkey, auditor_pubkey),
(&destination_pubkey, &auditor_pubkey),
)
.unwrap();

Expand All @@ -362,18 +358,12 @@ fn bench_transfer(c: &mut Criterion) {

fn bench_transfer_with_fee(c: &mut Criterion) {
let source_keypair = ElGamalKeypair::new_rand();

let destination_keypair = ElGamalKeypair::new_rand();
let destination_pubkey = destination_keypair.pubkey();

let auditor_keypair = ElGamalKeypair::new_rand();
let auditor_pubkey = auditor_keypair.pubkey();

let withdraw_withheld_authority_keypair = ElGamalKeypair::new_rand();
let withdraw_withheld_authority_pubkey = withdraw_withheld_authority_keypair.pubkey();
let destination_pubkey = ElGamalKeypair::new_rand().public;
let auditor_pubkey = ElGamalKeypair::new_rand().public;
let withdraw_withheld_authority_pubkey = ElGamalKeypair::new_rand().public;

let spendable_balance: u64 = 120;
let spendable_ciphertext = source_keypair.pubkey().encrypt(spendable_balance);
let spendable_ciphertext = source_keypair.public.encrypt(spendable_balance);

let transfer_amount: u64 = 100;

Expand All @@ -386,9 +376,9 @@ fn bench_transfer_with_fee(c: &mut Criterion) {
transfer_amount,
(spendable_balance, &spendable_ciphertext),
&source_keypair,
(destination_pubkey, auditor_pubkey),
(&destination_pubkey, &auditor_pubkey),
fee_parameters,
withdraw_withheld_authority_pubkey,
&withdraw_withheld_authority_pubkey,
)
.unwrap();

Expand Down

0 comments on commit 4c72a5e

Please sign in to comment.