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

Commit 4fea36d

Browse files
committed
review fixes
1 parent 32e06db commit 4fea36d

File tree

10 files changed

+232
-210
lines changed

10 files changed

+232
-210
lines changed

token/confidential-transfer/proof-generation/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub mod burn;
1010
pub mod encryption;
1111
pub mod errors;
1212
pub mod mint;
13-
pub mod supply;
1413
pub mod transfer;
1514
pub mod transfer_with_fee;
1615
pub mod withdraw;

token/confidential-transfer/proof-generation/src/mint.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use {
88
auth_encryption::{AeCiphertext, AeKey},
99
elgamal::{ElGamalCiphertext, ElGamalKeypair, ElGamalPubkey},
1010
pedersen::Pedersen,
11-
pod::auth_encryption::PodAeCiphertext,
1211
},
1312
zk_elgamal_proof_program::proof_data::{
1413
BatchedGroupedCiphertext3HandlesValidityProofData, BatchedRangeProofU128Data,
@@ -33,8 +32,8 @@ pub struct MintProofData {
3332

3433
pub fn mint_split_proof_data(
3534
current_supply_ciphertext: &ElGamalCiphertext,
36-
current_decryptable_supply: &AeCiphertext,
3735
mint_amount: u64,
36+
current_supply: u64,
3837
supply_elgamal_keypair: &ElGamalKeypair,
3938
supply_aes_key: &AeKey,
4039
destination_elgamal_pubkey: &ElGamalPubkey,
@@ -79,34 +78,8 @@ pub fn mint_split_proof_data(
7978
)
8079
.ok_or(TokenProofGenerationError::IllegalAmountBitLength)?;
8180

82-
// fresh mints are initialized with a zeroed decryptable_supply
83-
// TODO: don't clone here once AeCiphertext implement Copy in the zk-sdk
84-
let pod_decryptable_supply: PodAeCiphertext = current_decryptable_supply.clone().into();
85-
let current_decyptable_supply = if pod_decryptable_supply != PodAeCiphertext::default() {
86-
// decrypt the current supply
87-
current_decryptable_supply
88-
.decrypt(supply_aes_key)
89-
.ok_or(TokenProofGenerationError::IllegalAmountBitLength)?
90-
} else {
91-
0
92-
};
93-
94-
// get the difference between the supply ciphertext and the decryptable supply
95-
// explanation see https://github.com/solana-labs/solana-program-library/pull/6881#issuecomment-2385579058
96-
let decryptable_supply_ciphertext = supply_elgamal_keypair
97-
.pubkey()
98-
.encrypt(current_decyptable_supply);
99-
#[allow(clippy::arithmetic_side_effects)]
100-
let ct_decryptable_to_current_diff = decryptable_supply_ciphertext - current_supply_ciphertext;
101-
let decryptable_to_current_diff = supply_elgamal_keypair
102-
.secret()
103-
.decrypt_u32(&ct_decryptable_to_current_diff)
104-
.ok_or(TokenProofGenerationError::SupplyDecryption)?;
105-
10681
// compute the new supply
107-
let new_supply = current_decyptable_supply
108-
.checked_sub(decryptable_to_current_diff)
109-
.ok_or(TokenProofGenerationError::IllegalAmountBitLength)?
82+
let new_supply = current_supply
11083
.checked_add(mint_amount)
11184
.ok_or(TokenProofGenerationError::IllegalAmountBitLength)?;
11285

token/confidential-transfer/proof-generation/src/supply.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ fn test_mint_validity(mint_amount: u64, supply: u64) {
217217
let supply_aes_key = AeKey::new_rand();
218218

219219
let supply_ciphertext = supply_keypair.pubkey().encrypt(supply);
220-
let decryptable_supply = supply_aes_key.encrypt(supply);
221220

222221
let MintProofData {
223222
equality_proof_data,
@@ -226,8 +225,8 @@ fn test_mint_validity(mint_amount: u64, supply: u64) {
226225
new_decryptable_supply: _,
227226
} = mint_split_proof_data(
228227
&supply_ciphertext,
229-
&decryptable_supply,
230228
mint_amount,
229+
supply,
231230
&supply_keypair,
232231
&supply_aes_key,
233232
destination_pubkey,
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use {
2+
super::ConfidentialMintBurn,
3+
crate::error::TokenError,
4+
bytemuck::{Pod, Zeroable},
5+
solana_zk_sdk::{
6+
encryption::{
7+
auth_encryption::{AeCiphertext, AeKey},
8+
elgamal::{ElGamalCiphertext, ElGamalKeypair},
9+
pedersen::PedersenOpening,
10+
pod::{auth_encryption::PodAeCiphertext, elgamal::PodElGamalCiphertext},
11+
},
12+
zk_elgamal_proof_program::proof_data::CiphertextCiphertextEqualityProofData,
13+
},
14+
spl_pod::optional_keys::OptionalNonZeroElGamalPubkey,
15+
};
16+
17+
/// Confidential Mint Burn extension information needed to construct a
18+
/// `RotateSupplyElgamalPubkey` instruction.
19+
#[repr(C)]
20+
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
21+
pub struct SupplyAccountInfo {
22+
/// The available balance (encrypted by `encrypiton_pubkey`)
23+
pub current_supply: PodElGamalCiphertext,
24+
/// The decryptable supply
25+
pub decryptable_supply: PodAeCiphertext,
26+
/// The supply's elgamal pubkey
27+
pub supply_elgamal_pubkey: OptionalNonZeroElGamalPubkey,
28+
}
29+
30+
impl SupplyAccountInfo {
31+
/// Creates a SupplyAccountInfo from ConfidentialMintBurn extension account
32+
/// data
33+
pub fn new(extension: ConfidentialMintBurn) -> Self {
34+
Self {
35+
current_supply: extension.confidential_supply,
36+
decryptable_supply: extension.decryptable_supply,
37+
supply_elgamal_pubkey: extension.supply_elgamal_pubkey,
38+
}
39+
}
40+
41+
/// Computes the current supply from the decryptable supply and the
42+
/// difference between the decryptable supply and the elgamal encrypted
43+
/// supply ciphertext
44+
pub fn decrypt_current_supply(
45+
&self,
46+
aes_key: &AeKey,
47+
elgamal_keypair: &ElGamalKeypair,
48+
) -> Result<u64, TokenError> {
49+
if self.supply_elgamal_pubkey.is_none() {
50+
return Err(TokenError::InvalidState);
51+
}
52+
// fresh mints are initialized with a zeroed decryptable_supply
53+
// TODO: include decryptable supply in InitMint instruction
54+
let current_decyptable_supply = if self.decryptable_supply != PodAeCiphertext::default() {
55+
// decrypt the current supply
56+
TryInto::<AeCiphertext>::try_into(self.decryptable_supply)
57+
.map_err(|_| TokenError::MalformedCiphertext)?
58+
.decrypt(aes_key)
59+
.ok_or(TokenError::MalformedCiphertext)?
60+
} else {
61+
0
62+
};
63+
64+
// get the difference between the supply ciphertext and the decryptable supply
65+
// explanation see https://github.com/solana-labs/solana-program-library/pull/6881#issuecomment-2385579058
66+
let decryptable_supply_ciphertext =
67+
elgamal_keypair.pubkey().encrypt(current_decyptable_supply);
68+
#[allow(clippy::arithmetic_side_effects)]
69+
let supply_delta_ciphertext = decryptable_supply_ciphertext
70+
- (TryInto::<ElGamalCiphertext>::try_into(self.current_supply)
71+
.map_err(|_| TokenError::MalformedCiphertext)?);
72+
let decryptable_to_current_diff = elgamal_keypair
73+
.secret()
74+
.decrypt_u32(&supply_delta_ciphertext)
75+
.ok_or(TokenError::MalformedCiphertext)?;
76+
77+
// compute the current supply
78+
current_decyptable_supply
79+
.checked_sub(decryptable_to_current_diff)
80+
.ok_or(TokenError::Overflow)
81+
}
82+
83+
/// Generates the `CiphertextCiphertextEqualityProofData` needed for a
84+
/// `RotateSupplyElgamalPubkey` instruction
85+
pub fn generate_rotate_supply_elgamal_pubkey_proof(
86+
&self,
87+
aes_key: &AeKey,
88+
current_supply_elgamal_keypair: &ElGamalKeypair,
89+
new_supply_elgamal_keypair: &ElGamalKeypair,
90+
) -> Result<CiphertextCiphertextEqualityProofData, TokenError> {
91+
let current_supply =
92+
self.decrypt_current_supply(aes_key, current_supply_elgamal_keypair)?;
93+
94+
let new_supply_opening = PedersenOpening::new_rand();
95+
let new_supply_ciphertext = new_supply_elgamal_keypair
96+
.pubkey()
97+
.encrypt_with(current_supply, &new_supply_opening);
98+
99+
CiphertextCiphertextEqualityProofData::new(
100+
current_supply_elgamal_keypair,
101+
new_supply_elgamal_keypair.pubkey(),
102+
&self
103+
.current_supply
104+
.try_into()
105+
.map_err(|_| TokenError::MalformedCiphertext)?,
106+
&new_supply_ciphertext,
107+
&new_supply_opening,
108+
current_supply,
109+
)
110+
.map_err(|_| TokenError::ProofGeneration)
111+
}
112+
}

0 commit comments

Comments
 (0)