Skip to content

Commit 3bbeb53

Browse files
samkim-cryptowen-coding
authored andcommitted
[zk-token-sdk] Make ElGamalKeypair fields private (solana-labs#32190)
* make `ElGamalKeypair` fields private * update the rest of `zk-token-sdk` for the visibility update * update `zk-token-proof-tests` for the visibility update * update `zk-keygen` for the visibility update * update `zk-token-proof` benches for the updated visibility * cargo fmt * rename `ElGamalKeypair::new` to `ElGamalKeypair::new_for_tests`
1 parent 900282d commit 3bbeb53

22 files changed

+368
-288
lines changed

programs/zk-token-proof-tests/tests/process_transaction.rs

Lines changed: 78 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use {
1010
},
1111
solana_zk_token_sdk::{
1212
encryption::{
13-
elgamal::ElGamalKeypair,
13+
elgamal::{ElGamalKeypair, ElGamalSecretKey},
1414
grouped_elgamal::GroupedElGamal,
1515
pedersen::{Pedersen, PedersenOpening},
1616
},
@@ -42,13 +42,13 @@ const VERIFY_INSTRUCTION_TYPES: [ProofInstruction; 13] = [
4242
async fn test_zero_balance() {
4343
let elgamal_keypair = ElGamalKeypair::new_rand();
4444

45-
let zero_ciphertext = elgamal_keypair.public.encrypt(0_u64);
45+
let zero_ciphertext = elgamal_keypair.pubkey().encrypt(0_u64);
4646
let success_proof_data = ZeroBalanceProofData::new(&elgamal_keypair, &zero_ciphertext).unwrap();
4747

48-
let incorrect_keypair = ElGamalKeypair {
49-
public: ElGamalKeypair::new_rand().public,
50-
secret: ElGamalKeypair::new_rand().secret,
51-
};
48+
let incorrect_pubkey = elgamal_keypair.pubkey();
49+
let incorrect_secret = ElGamalSecretKey::new_rand();
50+
let incorrect_keypair = ElGamalKeypair::new_for_tests(*incorrect_pubkey, incorrect_secret);
51+
5252
let fail_proof_data = ZeroBalanceProofData::new(&incorrect_keypair, &zero_ciphertext).unwrap();
5353

5454
test_verify_proof_without_context(
@@ -80,30 +80,30 @@ async fn test_ciphertext_ciphertext_equality() {
8080
let destination_keypair = ElGamalKeypair::new_rand();
8181

8282
let amount: u64 = 0;
83-
let source_ciphertext = source_keypair.public.encrypt(amount);
83+
let source_ciphertext = source_keypair.pubkey().encrypt(amount);
8484

8585
let destination_opening = PedersenOpening::new_rand();
8686
let destination_ciphertext = destination_keypair
87-
.public
87+
.pubkey()
8888
.encrypt_with(amount, &destination_opening);
8989

9090
let success_proof_data = CiphertextCiphertextEqualityProofData::new(
9191
&source_keypair,
92-
&destination_keypair.public,
92+
destination_keypair.pubkey(),
9393
&source_ciphertext,
9494
&destination_ciphertext,
9595
&destination_opening,
9696
amount,
9797
)
9898
.unwrap();
9999

100-
let incorrect_keypair = ElGamalKeypair {
101-
public: ElGamalKeypair::new_rand().public,
102-
secret: ElGamalKeypair::new_rand().secret,
103-
};
100+
let incorrect_pubkey = source_keypair.pubkey();
101+
let incorrect_secret = ElGamalSecretKey::new_rand();
102+
let incorrect_keypair = ElGamalKeypair::new_for_tests(*incorrect_pubkey, incorrect_secret);
103+
104104
let fail_proof_data = CiphertextCiphertextEqualityProofData::new(
105105
&incorrect_keypair,
106-
&destination_keypair.public,
106+
destination_keypair.pubkey(),
107107
&source_ciphertext,
108108
&destination_ciphertext,
109109
&destination_opening,
@@ -137,32 +137,35 @@ async fn test_ciphertext_ciphertext_equality() {
137137
#[tokio::test]
138138
async fn test_transfer() {
139139
let source_keypair = ElGamalKeypair::new_rand();
140-
let dest_pubkey = ElGamalKeypair::new_rand().public;
141-
let auditor_pubkey = ElGamalKeypair::new_rand().public;
140+
141+
let destination_keypair = ElGamalKeypair::new_rand();
142+
let destination_pubkey = destination_keypair.pubkey();
143+
144+
let auditor_keypair = ElGamalKeypair::new_rand();
145+
let auditor_pubkey = auditor_keypair.pubkey();
142146

143147
let spendable_balance: u64 = 0;
144-
let spendable_ciphertext = source_keypair.public.encrypt(spendable_balance);
148+
let spendable_ciphertext = source_keypair.pubkey().encrypt(spendable_balance);
145149

146150
let transfer_amount: u64 = 0;
147151

148152
let success_proof_data = TransferData::new(
149153
transfer_amount,
150154
(spendable_balance, &spendable_ciphertext),
151155
&source_keypair,
152-
(&dest_pubkey, &auditor_pubkey),
156+
(destination_pubkey, auditor_pubkey),
153157
)
154158
.unwrap();
155159

156-
let incorrect_keypair = ElGamalKeypair {
157-
public: ElGamalKeypair::new_rand().public,
158-
secret: ElGamalKeypair::new_rand().secret,
159-
};
160+
let incorrect_pubkey = source_keypair.pubkey();
161+
let incorrect_secret = ElGamalSecretKey::new_rand();
162+
let incorrect_keypair = ElGamalKeypair::new_for_tests(*incorrect_pubkey, incorrect_secret);
160163

161164
let fail_proof_data = TransferData::new(
162165
transfer_amount,
163166
(spendable_balance, &spendable_ciphertext),
164167
&incorrect_keypair,
165-
(&dest_pubkey, &auditor_pubkey),
168+
(destination_pubkey, auditor_pubkey),
166169
)
167170
.unwrap();
168171

@@ -192,12 +195,18 @@ async fn test_transfer() {
192195
#[tokio::test]
193196
async fn test_transfer_with_fee() {
194197
let source_keypair = ElGamalKeypair::new_rand();
195-
let destination_pubkey = ElGamalKeypair::new_rand().public;
196-
let auditor_pubkey = ElGamalKeypair::new_rand().public;
197-
let withdraw_withheld_authority_pubkey = ElGamalKeypair::new_rand().public;
198+
199+
let destination_keypair = ElGamalKeypair::new_rand();
200+
let destination_pubkey = destination_keypair.pubkey();
201+
202+
let auditor_keypair = ElGamalKeypair::new_rand();
203+
let auditor_pubkey = auditor_keypair.pubkey();
204+
205+
let withdraw_withheld_authority_keypair = ElGamalKeypair::new_rand();
206+
let withdraw_withheld_authority_pubkey = withdraw_withheld_authority_keypair.pubkey();
198207

199208
let spendable_balance: u64 = 120;
200-
let spendable_ciphertext = source_keypair.public.encrypt(spendable_balance);
209+
let spendable_ciphertext = source_keypair.pubkey().encrypt(spendable_balance);
201210

202211
let transfer_amount: u64 = 0;
203212

@@ -210,24 +219,23 @@ async fn test_transfer_with_fee() {
210219
transfer_amount,
211220
(spendable_balance, &spendable_ciphertext),
212221
&source_keypair,
213-
(&destination_pubkey, &auditor_pubkey),
222+
(destination_pubkey, auditor_pubkey),
214223
fee_parameters,
215-
&withdraw_withheld_authority_pubkey,
224+
withdraw_withheld_authority_pubkey,
216225
)
217226
.unwrap();
218227

219-
let incorrect_keypair = ElGamalKeypair {
220-
public: ElGamalKeypair::new_rand().public,
221-
secret: ElGamalKeypair::new_rand().secret,
222-
};
228+
let incorrect_pubkey = source_keypair.pubkey();
229+
let incorrect_secret = ElGamalSecretKey::new_rand();
230+
let incorrect_keypair = ElGamalKeypair::new_for_tests(*incorrect_pubkey, incorrect_secret);
223231

224232
let fail_proof_data = TransferWithFeeData::new(
225233
transfer_amount,
226234
(spendable_balance, &spendable_ciphertext),
227235
&incorrect_keypair,
228-
(&destination_pubkey, &auditor_pubkey),
236+
(destination_pubkey, auditor_pubkey),
229237
fee_parameters,
230-
&withdraw_withheld_authority_pubkey,
238+
withdraw_withheld_authority_pubkey,
231239
)
232240
.unwrap();
233241

@@ -259,7 +267,7 @@ async fn test_withdraw() {
259267
let elgamal_keypair = ElGamalKeypair::new_rand();
260268

261269
let current_balance: u64 = 77;
262-
let current_ciphertext = elgamal_keypair.public.encrypt(current_balance);
270+
let current_ciphertext = elgamal_keypair.pubkey().encrypt(current_balance);
263271
let withdraw_amount: u64 = 55;
264272

265273
let success_proof_data = WithdrawData::new(
@@ -270,10 +278,10 @@ async fn test_withdraw() {
270278
)
271279
.unwrap();
272280

273-
let incorrect_keypair = ElGamalKeypair {
274-
public: ElGamalKeypair::new_rand().public,
275-
secret: ElGamalKeypair::new_rand().secret,
276-
};
281+
let incorrect_pubkey = elgamal_keypair.pubkey();
282+
let incorrect_secret = ElGamalSecretKey::new_rand();
283+
let incorrect_keypair = ElGamalKeypair::new_for_tests(*incorrect_pubkey, incorrect_secret);
284+
277285
let fail_proof_data = WithdrawData::new(
278286
withdraw_amount,
279287
&incorrect_keypair,
@@ -311,10 +319,9 @@ async fn test_pubkey_validity() {
311319

312320
let success_proof_data = PubkeyValidityData::new(&elgamal_keypair).unwrap();
313321

314-
let incorrect_keypair = ElGamalKeypair {
315-
public: ElGamalKeypair::new_rand().public,
316-
secret: ElGamalKeypair::new_rand().secret,
317-
};
322+
let incorrect_pubkey = elgamal_keypair.pubkey();
323+
let incorrect_secret = ElGamalSecretKey::new_rand();
324+
let incorrect_keypair = ElGamalKeypair::new_for_tests(*incorrect_pubkey, incorrect_secret);
318325

319326
let fail_proof_data = PubkeyValidityData::new(&incorrect_keypair).unwrap();
320327

@@ -526,7 +533,7 @@ async fn test_batched_range_proof_u256() {
526533
async fn test_ciphertext_commitment_equality() {
527534
let keypair = ElGamalKeypair::new_rand();
528535
let amount: u64 = 55;
529-
let ciphertext = keypair.public.encrypt(amount);
536+
let ciphertext = keypair.pubkey().encrypt(amount);
530537
let (commitment, opening) = Pedersen::new(amount);
531538

532539
let success_proof_data = CiphertextCommitmentEqualityProofData::new(
@@ -538,10 +545,9 @@ async fn test_ciphertext_commitment_equality() {
538545
)
539546
.unwrap();
540547

541-
let incorrect_keypair = ElGamalKeypair {
542-
public: ElGamalKeypair::new_rand().public,
543-
secret: ElGamalKeypair::new_rand().secret,
544-
};
548+
let incorrect_pubkey = keypair.pubkey();
549+
let incorrect_secret = ElGamalSecretKey::new_rand();
550+
let incorrect_keypair = ElGamalKeypair::new_for_tests(*incorrect_pubkey, incorrect_secret);
545551

546552
let fail_proof_data = CiphertextCommitmentEqualityProofData::new(
547553
&incorrect_keypair,
@@ -577,17 +583,20 @@ async fn test_ciphertext_commitment_equality() {
577583

578584
#[tokio::test]
579585
async fn test_grouped_ciphertext_2_handles_validity() {
580-
let destination_pubkey = ElGamalKeypair::new_rand().public;
581-
let auditor_pubkey = ElGamalKeypair::new_rand().public;
586+
let destination_keypair = ElGamalKeypair::new_rand();
587+
let destination_pubkey = destination_keypair.pubkey();
588+
589+
let auditor_keypair = ElGamalKeypair::new_rand();
590+
let auditor_pubkey = auditor_keypair.pubkey();
582591

583592
let amount: u64 = 55;
584593
let opening = PedersenOpening::new_rand();
585594
let grouped_ciphertext =
586-
GroupedElGamal::encrypt_with([&destination_pubkey, &auditor_pubkey], amount, &opening);
595+
GroupedElGamal::encrypt_with([destination_pubkey, auditor_pubkey], amount, &opening);
587596

588597
let success_proof_data = GroupedCiphertext2HandlesValidityProofData::new(
589-
&destination_pubkey,
590-
&auditor_pubkey,
598+
destination_pubkey,
599+
auditor_pubkey,
591600
&grouped_ciphertext,
592601
amount,
593602
&opening,
@@ -596,8 +605,8 @@ async fn test_grouped_ciphertext_2_handles_validity() {
596605

597606
let incorrect_opening = PedersenOpening::new_rand();
598607
let fail_proof_data = GroupedCiphertext2HandlesValidityProofData::new(
599-
&destination_pubkey,
600-
&auditor_pubkey,
608+
destination_pubkey,
609+
auditor_pubkey,
601610
&grouped_ciphertext,
602611
amount,
603612
&incorrect_opening,
@@ -629,29 +638,26 @@ async fn test_grouped_ciphertext_2_handles_validity() {
629638

630639
#[tokio::test]
631640
async fn test_batched_grouped_ciphertext_2_handles_validity() {
632-
let destination_pubkey = ElGamalKeypair::new_rand().public;
633-
let auditor_pubkey = ElGamalKeypair::new_rand().public;
641+
let destination_keypair = ElGamalKeypair::new_rand();
642+
let destination_pubkey = destination_keypair.pubkey();
643+
644+
let auditor_keypair = ElGamalKeypair::new_rand();
645+
let auditor_pubkey = auditor_keypair.pubkey();
634646

635647
let amount_lo: u64 = 55;
636648
let amount_hi: u64 = 22;
637649

638650
let opening_lo = PedersenOpening::new_rand();
639651
let opening_hi = PedersenOpening::new_rand();
640652

641-
let grouped_ciphertext_lo = GroupedElGamal::encrypt_with(
642-
[&destination_pubkey, &auditor_pubkey],
643-
amount_lo,
644-
&opening_lo,
645-
);
646-
let grouped_ciphertext_hi = GroupedElGamal::encrypt_with(
647-
[&destination_pubkey, &auditor_pubkey],
648-
amount_hi,
649-
&opening_hi,
650-
);
653+
let grouped_ciphertext_lo =
654+
GroupedElGamal::encrypt_with([destination_pubkey, auditor_pubkey], amount_lo, &opening_lo);
655+
let grouped_ciphertext_hi =
656+
GroupedElGamal::encrypt_with([destination_pubkey, auditor_pubkey], amount_hi, &opening_hi);
651657

652658
let success_proof_data = BatchedGroupedCiphertext2HandlesValidityProofData::new(
653-
&destination_pubkey,
654-
&auditor_pubkey,
659+
destination_pubkey,
660+
auditor_pubkey,
655661
&grouped_ciphertext_lo,
656662
&grouped_ciphertext_hi,
657663
amount_lo,
@@ -663,8 +669,8 @@ async fn test_batched_grouped_ciphertext_2_handles_validity() {
663669

664670
let incorrect_opening = PedersenOpening::new_rand();
665671
let fail_proof_data = BatchedGroupedCiphertext2HandlesValidityProofData::new(
666-
&destination_pubkey,
667-
&auditor_pubkey,
672+
destination_pubkey,
673+
auditor_pubkey,
668674
&grouped_ciphertext_lo,
669675
&grouped_ciphertext_hi,
670676
amount_lo,

0 commit comments

Comments
 (0)