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

Commit 89c7cd2

Browse files
committed
cleanup
1 parent 59234ba commit 89c7cd2

File tree

6 files changed

+88
-30
lines changed

6 files changed

+88
-30
lines changed

token/cli/src/clap_app.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2848,11 +2848,20 @@ pub fn app<'a, 'b>(
28482848
.value_name("CONFIDENTIAL_SUPPLY_KEYPAIR")
28492849
.takes_value(true)
28502850
.help(
2851-
"The confidential supply encryption keypair used to decrypt the supply. \
2851+
"The confidential supply encryption keypair used to decrypt ElGamalCiphertext supply. \
28522852
Either the authority or the confidential-supply-keypair have \
28532853
to be specified in order for the supply to be decrypted."
28542854
)
28552855
)
2856+
.arg(
2857+
Arg::with_name("confidential_supply_aes_key")
2858+
.long("confidential-supply-aes-key")
2859+
.value_name("CONFIDENTIAL_SUPPLY_AES_KEY")
2860+
.takes_value(true)
2861+
.help(
2862+
"The aes key used to decrypt the decryptable portion of the confidential supply."
2863+
)
2864+
)
28562865
.nonce_args(true)
28572866
)
28582867
.subcommand(
@@ -2888,6 +2897,16 @@ pub fn app<'a, 'b>(
28882897
"The current confidential supply encryption keypair."
28892898
)
28902899
)
2900+
.arg(
2901+
Arg::with_name("supply_aes_key")
2902+
.long("supply-aes-key")
2903+
.value_name("SUPPLY_AES_KEY")
2904+
.takes_value(true)
2905+
.required(true)
2906+
.help(
2907+
"The aes key to decrypt the decryptable confidential supply."
2908+
)
2909+
)
28912910
.arg(
28922911
Arg::with_name("new_supply_keypair")
28932912
.long("new-supply-keypair")

token/cli/src/command.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4828,10 +4828,18 @@ pub async fn process_command<'a>(
48284828

48294829
ElGamalKeypair::new_from_signer(&*auth_signer, b"").unwrap()
48304830
};
4831+
let aes_key = if arg_matches.is_present("confidential_supply_aes_key") {
4832+
aes_key_of(arg_matches, "confidential_supply_aes_key").unwrap()
4833+
} else {
4834+
let (auth_signer, _auth) =
4835+
config.signer_or_default(arg_matches, "authority", &mut wallet_manager);
4836+
4837+
AeKey::new_from_signer(&*auth_signer, b"").unwrap()
4838+
};
48314839

48324840
let token_cl = token_client_from_config(config, &token, None)?;
48334841
let supply = token_cl
4834-
.confidential_supply(&elgamal_keypair)
4842+
.confidential_supply(&elgamal_keypair, &aes_key)
48354843
.await
48364844
.map_err(|e| format!("Could not fetch confidential supply for {token}: {e}",))?;
48374845

@@ -4843,6 +4851,7 @@ pub async fn process_command<'a>(
48434851
.unwrap();
48444852
let cur_elgamal_keypair =
48454853
elgamal_keypair_of(arg_matches, "current_supply_keypair").unwrap();
4854+
let supply_aes_key = aes_key_of(arg_matches, "supply_aes_key").unwrap();
48464855
let new_elgamal_keypair =
48474856
elgamal_keypair_of(arg_matches, "new_supply_keypair").unwrap();
48484857
let (auth_signer, auth) =
@@ -4855,8 +4864,9 @@ pub async fn process_command<'a>(
48554864
.rotate_supply_elgamal(
48564865
&auth,
48574866
&cur_elgamal_keypair,
4867+
&supply_aes_key,
48584868
&new_elgamal_keypair,
4859-
&[auth_signer],
4869+
&[&auth_signer],
48604870
)
48614871
.await?,
48624872
false,

token/cli/src/encryption_keypair.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
//! NOTE: this module should be remoeved in the next Solana upgrade.
44
55
use {
6-
base64::{prelude::BASE64_STANDARD, Engine},
7-
clap::ArgMatches,
8-
spl_token_2022::solana_zk_sdk::encryption::{
9-
elgamal::{ElGamalKeypair, ElGamalPubkey},
10-
pod::elgamal::PodElGamalPubkey,
11-
},
6+
base64::{prelude::BASE64_STANDARD, Engine}, clap::ArgMatches, solana_sdk::signer::EncodableKey, spl_token_2022::solana_zk_sdk::encryption::{
7+
auth_encryption::AeKey, elgamal::{ElGamalKeypair, ElGamalPubkey}, pod::elgamal::PodElGamalPubkey
8+
}
129
};
1310

1411
const ELGAMAL_PUBKEY_MAX_BASE64_LEN: usize = 44;
@@ -68,6 +65,14 @@ pub(crate) fn elgamal_keypair_of(
6865
ElGamalKeypair::read_json_file(path).map_err(|e| e.to_string())
6966
}
7067

68+
pub(crate) fn aes_key_of(
69+
matches: &ArgMatches,
70+
name: &str,
71+
) -> Result<AeKey, String> {
72+
let path = matches.value_of(name).unwrap();
73+
AeKey::read_from_file(path).map_err(|e| e.to_string())
74+
}
75+
7176
fn elgamal_pubkey_from_str(s: &str) -> Option<PodElGamalPubkey> {
7277
if s.len() > ELGAMAL_PUBKEY_MAX_BASE64_LEN {
7378
return None;

token/client/src/token.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use {
5959
encryption::{
6060
auth_encryption::{AeCiphertext, AeKey},
6161
elgamal::{ElGamalCiphertext, ElGamalKeypair, ElGamalPubkey, ElGamalSecretKey},
62-
pod::elgamal::PodElGamalPubkey,
62+
pod::{auth_encryption::PodAeCiphertext, elgamal::PodElGamalPubkey},
6363
},
6464
zk_elgamal_proof_program::{
6565
self,
@@ -3598,23 +3598,46 @@ where
35983598
pub async fn confidential_supply(
35993599
&self,
36003600
supply_elgamal_keypair: &ElGamalKeypair,
3601+
supply_aes_key: &AeKey,
36013602
) -> Result<u64, TokenError> {
36023603
let mint = self.get_mint_info().await?;
36033604
let confidential_mint_burn_ext = mint.get_extension::<ConfidentialMintBurn>()?;
36043605

3605-
// Currently only correctly displays supplies until u32::MAX,
3606-
// the supply ciphertext will still be correct after, but it
3607-
// can't be decrypted here until a corresponding decryption
3608-
// method is added to the solana-zk-token-sdk
3609-
Ok(supply_elgamal_keypair
3606+
let current_decyptable_supply =
3607+
if confidential_mint_burn_ext.decryptable_supply != PodAeCiphertext::default() {
3608+
// decrypt the current supply
3609+
let decryptable_supply: AeCiphertext = confidential_mint_burn_ext
3610+
.decryptable_supply
3611+
.try_into()
3612+
.map_err(|_| TokenError::AccountDecryption)?;
3613+
decryptable_supply
3614+
.decrypt(supply_aes_key)
3615+
.ok_or(TokenError::AccountDecryption)?
3616+
} else {
3617+
0
3618+
};
3619+
3620+
// get the difference between the supply ciphertext and the decryptable supply
3621+
// explanation see https://github.com/solana-labs/solana-program-library/pull/6881#issuecomment-2385579058
3622+
let decryptable_supply_ciphertext = supply_elgamal_keypair
3623+
.pubkey()
3624+
.encrypt(current_decyptable_supply);
3625+
let current_supply: ElGamalCiphertext = confidential_mint_burn_ext
3626+
.confidential_supply
3627+
.try_into()
3628+
.map_err(|_| TokenError::AccountDecryption)?;
3629+
let ct_decryptable_to_current_diff = decryptable_supply_ciphertext - current_supply;
3630+
let decryptable_to_current_diff = supply_elgamal_keypair
36103631
.secret()
3611-
.decrypt_u32(
3612-
&TryInto::<ElGamalCiphertext>::try_into(
3613-
confidential_mint_burn_ext.confidential_supply,
3614-
)
3615-
.map_err(|_| TokenError::Program(ProgramError::InvalidAccountData))?,
3616-
)
3617-
.unwrap_or_default())
3632+
.decrypt_u32(&ct_decryptable_to_current_diff)
3633+
.ok_or(TokenError::AccountDecryption)?;
3634+
3635+
// compute the total supply
3636+
current_decyptable_supply
3637+
.checked_sub(decryptable_to_current_diff)
3638+
.ok_or(TokenError::TokenProgramError(String::from(
3639+
"Confidential supply underflow",
3640+
)))
36183641
}
36193642

36203643
pub async fn supply_elgamal_pubkey(&self) -> TokenResult<Option<ElGamalPubkey>> {
@@ -3633,6 +3656,7 @@ where
36333656
&self,
36343657
authority: &Pubkey,
36353658
supply_elgamal_keypair: &ElGamalKeypair,
3659+
supply_aes_key: &AeKey,
36363660
new_supply_elgamal_keypair: &ElGamalKeypair,
36373661
signing_keypairs: &S,
36383662
) -> TokenResult<T::Output> {
@@ -3641,7 +3665,7 @@ where
36413665

36423666
let mint = self.get_mint_info().await?;
36433667
let extension_state = mint.get_extension::<ConfidentialMintBurn>()?;
3644-
let current_supply = self.confidential_supply(supply_elgamal_keypair).await?;
3668+
let current_supply = self.confidential_supply(supply_elgamal_keypair, supply_aes_key).await?;
36453669

36463670
self.process_ixs(
36473671
&confidential_mint_burn::instruction::rotate_supply_elgamal(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub fn mint_split_proof_data(
104104

105105
// compute the new supply
106106
let new_supply = current_decyptable_supply
107-
.checked_add(decryptable_to_current_diff)
107+
.checked_sub(decryptable_to_current_diff)
108108
.ok_or(TokenProofGenerationError::IllegalAmountBitLength)?
109109
.checked_add(mint_amount)
110110
.ok_or(TokenProofGenerationError::IllegalAmountBitLength)?;

token/program-2022-test/tests/confidential_mint_burn.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use {
1616
},
1717
BaseStateWithExtensions,
1818
},
19-
proof::ProofLocation,
2019
solana_zk_sdk::encryption::{
2120
auth_encryption::AeKey, elgamal::*, pod::elgamal::PodElGamalPubkey,
2221
},
@@ -112,7 +111,7 @@ async fn test_confidential_mint() {
112111

113112
assert_eq!(
114113
token
115-
.confidential_supply(&auditor_elgamal_keypair,)
114+
.confidential_supply(&auditor_elgamal_keypair, &supply_aes_key)
116115
.await
117116
.unwrap(),
118117
MINT_AMOUNT
@@ -159,7 +158,7 @@ async fn test_confidential_burn() {
159158

160159
assert_eq!(
161160
token
162-
.confidential_supply(&auditor_elgamal_keypair,)
161+
.confidential_supply(&auditor_elgamal_keypair, &supply_aes_key)
163162
.await
164163
.unwrap(),
165164
MINT_AMOUNT
@@ -313,7 +312,7 @@ async fn test_confidential_burn() {
313312

314313
assert_eq!(
315314
token
316-
.confidential_supply(&auditor_elgamal_keypair,)
315+
.confidential_supply(&auditor_elgamal_keypair, &supply_aes_key)
317316
.await
318317
.unwrap(),
319318
MINT_AMOUNT - BURN_AMOUNT,
@@ -360,7 +359,7 @@ async fn test_rotate_supply_elgamal() {
360359

361360
assert_eq!(
362361
token
363-
.confidential_supply(&auditor_elgamal_keypair,)
362+
.confidential_supply(&auditor_elgamal_keypair, &supply_aes_key)
364363
.await
365364
.unwrap(),
366365
MINT_AMOUNT
@@ -371,6 +370,7 @@ async fn test_rotate_supply_elgamal() {
371370
.rotate_supply_elgamal(
372371
&authority.pubkey(),
373372
&auditor_elgamal_keypair,
373+
&supply_aes_key,
374374
&new_supply_elgamal_keypair,
375375
&[authority],
376376
)
@@ -379,7 +379,7 @@ async fn test_rotate_supply_elgamal() {
379379

380380
assert_eq!(
381381
token
382-
.confidential_supply(&new_supply_elgamal_keypair)
382+
.confidential_supply(&new_supply_elgamal_keypair, &supply_aes_key)
383383
.await
384384
.unwrap(),
385385
MINT_AMOUNT

0 commit comments

Comments
 (0)