Skip to content

Commit

Permalink
add test and bench for ciphertext 3 handles validity proof instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Apr 21, 2024
1 parent 2d1c5a0 commit 5f073fd
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 10 deletions.
145 changes: 144 additions & 1 deletion programs/zk-token-proof-tests/tests/process_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use {
std::mem::size_of,
};

const VERIFY_INSTRUCTION_TYPES: [ProofInstruction; 14] = [
const VERIFY_INSTRUCTION_TYPES: [ProofInstruction; 16] = [
ProofInstruction::VerifyZeroBalance,
ProofInstruction::VerifyWithdraw,
ProofInstruction::VerifyCiphertextCiphertextEquality,
Expand All @@ -40,6 +40,8 @@ const VERIFY_INSTRUCTION_TYPES: [ProofInstruction; 14] = [
ProofInstruction::VerifyGroupedCiphertext2HandlesValidity,
ProofInstruction::VerifyBatchedGroupedCiphertext2HandlesValidity,
ProofInstruction::VerifyFeeSigma,
ProofInstruction::VerifyGroupedCiphertext3HandlesValidity,
ProofInstruction::VerifyBatchedGroupedCiphertext3HandlesValidity,
];

#[tokio::test]
Expand Down Expand Up @@ -887,6 +889,147 @@ async fn test_fee_sigma() {
.await;
}

#[tokio::test]
async fn test_grouped_ciphertext_3_handles_validity() {
let source_keypair = ElGamalKeypair::new_rand();
let source_pubkey = source_keypair.pubkey();

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 amount: u64 = 55;
let opening = PedersenOpening::new_rand();
let grouped_ciphertext = GroupedElGamal::encrypt_with(
[source_pubkey, destination_pubkey, auditor_pubkey],
amount,
&opening,
);

let success_proof_data = GroupedCiphertext3HandlesValidityProofData::new(
source_pubkey,
destination_pubkey,
auditor_pubkey,
&grouped_ciphertext,
amount,
&opening,
)
.unwrap();

let incorrect_opening = PedersenOpening::new_rand();
let fail_proof_data = GroupedCiphertext3HandlesValidityProofData::new(
source_pubkey,
destination_pubkey,
auditor_pubkey,
&grouped_ciphertext,
amount,
&incorrect_opening,
)
.unwrap();

test_verify_proof_without_context(
ProofInstruction::VerifyGroupedCiphertext3HandlesValidity,
&success_proof_data,
&fail_proof_data,
)
.await;

test_verify_proof_with_context(
ProofInstruction::VerifyGroupedCiphertext3HandlesValidity,
size_of::<ProofContextState<GroupedCiphertext3HandlesValidityProofContext>>(),
&success_proof_data,
&fail_proof_data,
)
.await;

test_close_context_state(
ProofInstruction::VerifyGroupedCiphertext3HandlesValidity,
size_of::<ProofContextState<GroupedCiphertext3HandlesValidityProofContext>>(),
&success_proof_data,
)
.await;
}

#[tokio::test]
async fn test_batched_grouped_ciphertext_3_handles_validity() {
let source_keypair = ElGamalKeypair::new_rand();
let source_pubkey = source_keypair.pubkey();

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 amount_lo: u64 = 55;
let amount_hi: u64 = 22;

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

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

let success_proof_data = BatchedGroupedCiphertext3HandlesValidityProofData::new(
source_pubkey,
destination_pubkey,
auditor_pubkey,
&grouped_ciphertext_lo,
&grouped_ciphertext_hi,
amount_lo,
amount_hi,
&opening_lo,
&opening_hi,
)
.unwrap();

let incorrect_opening = PedersenOpening::new_rand();
let fail_proof_data = BatchedGroupedCiphertext3HandlesValidityProofData::new(
source_pubkey,
destination_pubkey,
auditor_pubkey,
&grouped_ciphertext_lo,
&grouped_ciphertext_hi,
amount_lo,
amount_hi,
&incorrect_opening,
&opening_hi,
)
.unwrap();

test_verify_proof_without_context(
ProofInstruction::VerifyBatchedGroupedCiphertext3HandlesValidity,
&success_proof_data,
&fail_proof_data,
)
.await;

test_verify_proof_with_context(
ProofInstruction::VerifyBatchedGroupedCiphertext3HandlesValidity,
size_of::<ProofContextState<BatchedGroupedCiphertext3HandlesValidityProofContext>>(),
&success_proof_data,
&fail_proof_data,
)
.await;

test_close_context_state(
ProofInstruction::VerifyBatchedGroupedCiphertext3HandlesValidity,
size_of::<ProofContextState<BatchedGroupedCiphertext3HandlesValidityProofContext>>(),
&success_proof_data,
)
.await;
}

async fn test_verify_proof_without_context<T, U>(
proof_instruction: ProofInstruction,
success_proof_data: &T,
Expand Down
104 changes: 95 additions & 9 deletions programs/zk-token-proof/benches/verify_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use {
},
instruction::{
transfer::FeeParameters, BatchedGroupedCiphertext2HandlesValidityProofData,
BatchedRangeProofU128Data, BatchedRangeProofU256Data, BatchedRangeProofU64Data,
BatchedGroupedCiphertext3HandlesValidityProofData, BatchedRangeProofU128Data,
BatchedRangeProofU256Data, BatchedRangeProofU64Data,
CiphertextCiphertextEqualityProofData, CiphertextCommitmentEqualityProofData,
FeeSigmaProofData, GroupedCiphertext2HandlesValidityProofData, PubkeyValidityData,
RangeProofU64Data, TransferData, TransferWithFeeData, WithdrawData,
ZeroBalanceProofData, ZkProofData,
FeeSigmaProofData, GroupedCiphertext2HandlesValidityProofData,
GroupedCiphertext3HandlesValidityProofData, PubkeyValidityData, RangeProofU64Data,
TransferData, TransferWithFeeData, WithdrawData, ZeroBalanceProofData, ZkProofData,
},
},
};
Expand Down Expand Up @@ -74,7 +75,7 @@ fn bench_zero_balance(c: &mut Criterion) {
});
}

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

Expand All @@ -95,7 +96,42 @@ fn bench_grouped_ciphertext_validity(c: &mut Criterion) {
)
.unwrap();

c.bench_function("grouped_ciphertext_validity", |b| {
c.bench_function("grouped_ciphertext_2_handles_validity", |b| {
b.iter(|| {
proof_data.verify_proof().unwrap();
})
});
}

fn bench_grouped_ciphertext_3_handles_validity(c: &mut Criterion) {
let source_keypair = ElGamalKeypair::new_rand();
let source_pubkey = source_keypair.pubkey();

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 amount: u64 = 55;
let opening = PedersenOpening::new_rand();
let grouped_ciphertext = GroupedElGamal::encrypt_with(
[source_pubkey, destination_pubkey, auditor_pubkey],
amount,
&opening,
);

let proof_data = GroupedCiphertext3HandlesValidityProofData::new(
source_pubkey,
destination_pubkey,
auditor_pubkey,
&grouped_ciphertext,
amount,
&opening,
)
.unwrap();

c.bench_function("grouped_ciphertext_3_handles_validity", |b| {
b.iter(|| {
proof_data.verify_proof().unwrap();
})
Expand Down Expand Up @@ -153,7 +189,7 @@ fn bench_ciphertext_ciphertext_equality(c: &mut Criterion) {
});
}

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

Expand Down Expand Up @@ -191,6 +227,54 @@ fn bench_batched_grouped_ciphertext_validity(c: &mut Criterion) {
});
}

fn bench_batched_grouped_ciphertext_3_handles_validity(c: &mut Criterion) {
let source_keypair = ElGamalKeypair::new_rand();
let source_pubkey = source_keypair.pubkey();

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 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(
[source_pubkey, destination_pubkey, auditor_pubkey],
amount_lo,
&opening_lo,
);

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

let proof_data = BatchedGroupedCiphertext3HandlesValidityProofData::new(
source_pubkey,
destination_pubkey,
auditor_pubkey,
&grouped_ciphertext_lo,
&grouped_ciphertext_hi,
amount_lo,
amount_hi,
&opening_lo,
&opening_hi,
)
.unwrap();

c.bench_function("batched_grouped_ciphertext_3_handles_validity", |b| {
b.iter(|| {
proof_data.verify_proof().unwrap();
})
});
}

#[allow(clippy::op_ref)]
fn bench_fee_sigma(c: &mut Criterion) {
let transfer_amount: u64 = 1;
Expand Down Expand Up @@ -447,10 +531,12 @@ criterion_group!(
bench_range_proof_u64,
bench_withdraw,
bench_zero_balance,
bench_grouped_ciphertext_validity,
bench_grouped_ciphertext_2_handles_validity,
bench_grouped_ciphertext_3_handles_validity,
bench_ciphertext_commitment_equality,
bench_ciphertext_ciphertext_equality,
bench_batched_grouped_ciphertext_validity,
bench_batched_grouped_ciphertext_2_handles_validity,
bench_batched_grouped_ciphertext_3_handles_validity,
bench_batched_range_proof_u64,
bench_batched_range_proof_u128,
bench_batched_range_proof_u256,
Expand Down

0 comments on commit 5f073fd

Please sign in to comment.