Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

[zk-token-sdk] Make ElGamalKeypair fields private #32190

Merged
merged 7 commits into from
Jun 22, 2023
Merged

[zk-token-sdk] Make ElGamalKeypair fields private #32190

merged 7 commits into from
Jun 22, 2023

Conversation

samkim-crypto
Copy link
Contributor

@samkim-crypto samkim-crypto commented Jun 19, 2023

Problem

Currently, the fields public and secret in the type ElGamalKeypair are all public. This is somewhat unnatural and could also cause security issues since the secret component implements Copy allowing downstream to copy/clone the secret key in memory under the hood.

Summary of Changes

  • Changed the public and secret fields in ElGamalKeypair to be private
  • Added constructor for ElGamalKeypair and getters for its private fields
  • Updated the rest of the repo for the new visibility

I referenced the interface for Keypair in the sdk. There, we have the interface

pub fn secret(&self) -> &SecretKey { ... } // returns reference to `SecretKey`

and

pub fn pubkey(&self) -> Pubkey { ...} // returns a copy of `Pubkey`

In this PR, I made both getters to return references to ElGamalSecretKey and ElGamalPubkey. For secret key, it obviously makes sense to return a reference. For the pubkey part, I thought returning a refernce is more flexible than returning a fully owned ElGamalPubkey since ElGamalPubkey implements Copy and hence, the returned variable from the getter can always be dereferenced immediately.

But when I was updating the rest of the crates, encountered some occurrences in tests like

let random_elgamal_pubkey = ElGamalKeypair::new_rand().pubkey();

which does not compile because the output of ElGamalKeypair::new_rand() is freed immediately after its call and random_elgamal_pubkey becomes a dangling pointer. The snippet would work if the getter returned a fully-owned ElGamalPubkey.

With returning a reference, the snippet above has to become

let random_elgamal_keypair = ElGamalKeypair::new_rand(); // must save keypair in a variable
let random_elgamal_pubkey = random_elgamal_keypair::pubkey();

I think generating a random ElGamalPubkey without generating a corresponding ElGamalSecretKey will be very rare in practice other than in tests, so I think this is more minor for downstream.

But one other example is

let keypair = ElGamalKeypair::new_rand();

let pod_elgamal_pubkey: PodElGamalPubkey = keypair.pubkey().into(); // does not work
let pod_elgamal_pubkey: PodElGamalPubkey = (*keypair.pubkey()).into(); // must be dereferenced first

There are some issues like these that make the code slightly less ergonomic. I thought about this a bit and I thought going with returning &ElGamalPubkey is still the way to go, but I'd be happy to discuss.

This is a follow-up to #31468 (comment).

Fixes #

@samkim-crypto samkim-crypto added the work in progress This isn't quite right yet label Jun 19, 2023
@codecov
Copy link

codecov bot commented Jun 19, 2023

Codecov Report

Merging #32190 (af36808) into master (c142ba1) will decrease coverage by 0.1%.
The diff coverage is 98.9%.

@@            Coverage Diff            @@
##           master   #32190     +/-   ##
=========================================
- Coverage    82.0%    82.0%   -0.1%     
=========================================
  Files         769      769             
  Lines      209137   209208     +71     
=========================================
+ Hits       171587   171633     +46     
- Misses      37550    37575     +25     

@samkim-crypto samkim-crypto added v1.16 PRs that should be backported to v1.16 and removed work in progress This isn't quite right yet labels Jun 20, 2023
@samkim-crypto samkim-crypto merged commit 1452ed7 into solana-labs:master Jun 22, 2023
mergify bot pushed a commit that referenced this pull request Jun 22, 2023
* 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`

(cherry picked from commit 1452ed7)

# Conflicts:
#	programs/zk-token-proof/benches/verify_proofs.rs
HaoranYi pushed a commit to HaoranYi/solana that referenced this pull request Jun 22, 2023
* 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`
samkim-crypto added a commit that referenced this pull request Jun 24, 2023
* 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`

(cherry picked from commit 1452ed7)

# Conflicts:
#	programs/zk-token-proof/benches/verify_proofs.rs
samkim-crypto added a commit that referenced this pull request Jun 25, 2023
* 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`

(cherry picked from commit 1452ed7)

# Conflicts:
#	programs/zk-token-proof/benches/verify_proofs.rs
wen-coding pushed a commit to wen-coding/solana that referenced this pull request Aug 15, 2023
* 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`
wen-coding pushed a commit to wen-coding/solana that referenced this pull request Aug 15, 2023
* 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`
wen-coding pushed a commit to wen-coding/solana that referenced this pull request Aug 15, 2023
* 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`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
v1.16 PRs that should be backported to v1.16
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants