Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit de97337

Browse files
committed
add test cases
1 parent ad35e70 commit de97337

File tree

1 file changed

+87
-9
lines changed

1 file changed

+87
-9
lines changed

token/confidential-transfer/proof-tests/tests/proof_test.rs

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ use {
33
encryption::{auth_encryption::AeKey, elgamal::ElGamalKeypair},
44
zk_elgamal_proof_program::proof_data::ZkProofData,
55
},
6-
spl_token_confidential_transfer_proof_extraction::transfer::TransferProofContext,
7-
spl_token_confidential_transfer_proof_generation::transfer::transfer_split_proof_data,
6+
spl_token_confidential_transfer_proof_extraction::{
7+
transfer::TransferProofContext, transfer_with_fee::TransferWithFeeProofContext,
8+
},
9+
spl_token_confidential_transfer_proof_generation::{
10+
transfer::transfer_split_proof_data, transfer_with_fee::transfer_with_fee_split_proof_data,
11+
},
812
};
913

1014
#[test]
1115
fn test_transfer_correctness() {
12-
test_proof_validity(0, 0);
13-
test_proof_validity(1, 0);
14-
test_proof_validity(1, 1);
15-
test_proof_validity(65535, 65535); // 2^16 - 1
16-
test_proof_validity(65536, 65536); // 2^16
17-
test_proof_validity(281474976710655, 281474976710655); // 2^48 - 1
16+
test_transfer_proof_validity(0, 0);
17+
test_transfer_proof_validity(1, 0);
18+
test_transfer_proof_validity(1, 1);
19+
test_transfer_proof_validity(65535, 65535); // 2^16 - 1
20+
test_transfer_proof_validity(65536, 65536); // 2^16
21+
test_transfer_proof_validity(281474976710655, 281474976710655); // 2^48 - 1
1822
}
1923

20-
fn test_proof_validity(spendable_balance: u64, transfer_amount: u64) {
24+
fn test_transfer_proof_validity(spendable_balance: u64, transfer_amount: u64) {
2125
let source_keypair = ElGamalKeypair::new_rand();
2226

2327
let aes_key = AeKey::new_rand();
@@ -53,3 +57,77 @@ fn test_proof_validity(spendable_balance: u64, transfer_amount: u64) {
5357
)
5458
.unwrap();
5559
}
60+
61+
#[test]
62+
fn test_transfer_with_fee_correctness() {
63+
test_transfer_with_fee_proof_validity(0, 0, 0, 0);
64+
test_transfer_with_fee_proof_validity(0, 0, 0, 1);
65+
test_transfer_with_fee_proof_validity(0, 0, 1, 0);
66+
test_transfer_with_fee_proof_validity(0, 0, 1, 1);
67+
test_transfer_with_fee_proof_validity(1, 0, 0, 0);
68+
test_transfer_with_fee_proof_validity(1, 1, 0, 0);
69+
70+
test_transfer_with_fee_proof_validity(100, 100, 5, 10);
71+
test_transfer_with_fee_proof_validity(100, 100, 5, 1);
72+
}
73+
74+
fn test_transfer_with_fee_proof_validity(
75+
spendable_balance: u64,
76+
transfer_amount: u64,
77+
fee_rate_basis_points: u16,
78+
maximum_fee: u64,
79+
) {
80+
let source_keypair = ElGamalKeypair::new_rand();
81+
let aes_key = AeKey::new_rand();
82+
83+
let destination_keypair = ElGamalKeypair::new_rand();
84+
let destination_pubkey = destination_keypair.pubkey();
85+
86+
let auditor_keypair = ElGamalKeypair::new_rand();
87+
let auditor_pubkey = auditor_keypair.pubkey();
88+
89+
let withdraw_withheld_authority_keyupair = ElGamalKeypair::new_rand();
90+
let withdraw_withheld_authority_pubkey = withdraw_withheld_authority_keyupair.pubkey();
91+
92+
let spendable_ciphertext = source_keypair.pubkey().encrypt(spendable_balance);
93+
let decryptable_balance = aes_key.encrypt(spendable_balance);
94+
95+
let (
96+
equality_proof_data,
97+
transfer_amount_ciphertext_validity_proof_data,
98+
percentage_with_cap_proof_data,
99+
fee_ciphertext_validity_proof_data,
100+
range_proof_data,
101+
) = transfer_with_fee_split_proof_data(
102+
&spendable_ciphertext,
103+
&decryptable_balance,
104+
transfer_amount,
105+
&source_keypair,
106+
&aes_key,
107+
destination_pubkey,
108+
Some(auditor_pubkey),
109+
withdraw_withheld_authority_pubkey,
110+
fee_rate_basis_points,
111+
maximum_fee,
112+
)
113+
.unwrap();
114+
115+
equality_proof_data.verify_proof().unwrap();
116+
transfer_amount_ciphertext_validity_proof_data
117+
.verify_proof()
118+
.unwrap();
119+
percentage_with_cap_proof_data.verify_proof().unwrap();
120+
fee_ciphertext_validity_proof_data.verify_proof().unwrap();
121+
range_proof_data.verify_proof().unwrap();
122+
123+
TransferWithFeeProofContext::verify_and_extract(
124+
equality_proof_data.context_data(),
125+
transfer_amount_ciphertext_validity_proof_data.context_data(),
126+
percentage_with_cap_proof_data.context_data(),
127+
fee_ciphertext_validity_proof_data.context_data(),
128+
range_proof_data.context_data(),
129+
fee_rate_basis_points,
130+
maximum_fee,
131+
)
132+
.unwrap();
133+
}

0 commit comments

Comments
 (0)