From 253c2d70d1310d883db985ae17d7785303286c64 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Sat, 7 Dec 2024 11:55:12 +0900 Subject: [PATCH 1/5] make instruction constructor function parameters consistent --- .../confidential_mint_burn/instruction.rs | 26 ++++++++-------- .../confidential_transfer/instruction.rs | 30 +++++++++---------- .../confidential_transfer_fee/instruction.rs | 4 +-- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/token/program-2022/src/extension/confidential_mint_burn/instruction.rs b/token/program-2022/src/extension/confidential_mint_burn/instruction.rs index 6ecd112433d..065e7ff9aeb 100644 --- a/token/program-2022/src/extension/confidential_mint_burn/instruction.rs +++ b/token/program-2022/src/extension/confidential_mint_burn/instruction.rs @@ -26,7 +26,7 @@ use { #[cfg(not(target_os = "solana"))] use { solana_zk_sdk::{ - encryption::{auth_encryption::AeCiphertext, elgamal::ElGamalPubkey}, + encryption::elgamal::ElGamalPubkey, zk_elgamal_proof_program::{ instruction::ProofInstruction, proof_data::{ @@ -291,8 +291,8 @@ pub struct BurnInstructionData { pub fn initialize_mint( token_program_id: &Pubkey, mint: &Pubkey, - supply_elgamal_pubkey: PodElGamalPubkey, - decryptable_supply: PodAeCiphertext, + supply_elgamal_pubkey: &PodElGamalPubkey, + decryptable_supply: &DecryptableBalance, ) -> Result { check_program_account(token_program_id)?; let accounts = vec![AccountMeta::new(*mint, false)]; @@ -303,8 +303,8 @@ pub fn initialize_mint( TokenInstruction::ConfidentialMintBurnExtension, ConfidentialMintBurnInstruction::InitializeMint, &InitializeMintData { - supply_elgamal_pubkey, - decryptable_supply, + supply_elgamal_pubkey: *supply_elgamal_pubkey, + decryptable_supply: *decryptable_supply, }, )) } @@ -317,7 +317,7 @@ pub fn rotate_supply_elgamal_pubkey( mint: &Pubkey, authority: &Pubkey, multisig_signers: &[&Pubkey], - new_supply_elgamal_pubkey: ElGamalPubkey, + new_supply_elgamal_pubkey: &PodElGamalPubkey, ciphertext_equality_proof: ProofLocation, ) -> Result, ProgramError> { check_program_account(token_program_id)?; @@ -349,7 +349,7 @@ pub fn rotate_supply_elgamal_pubkey( TokenInstruction::ConfidentialMintBurnExtension, ConfidentialMintBurnInstruction::RotateSupplyElGamalPubkey, &RotateSupplyElGamalPubkeyData { - new_supply_elgamal_pubkey: PodElGamalPubkey::from(new_supply_elgamal_pubkey), + new_supply_elgamal_pubkey: *new_supply_elgamal_pubkey, proof_instruction_offset, }, )]; @@ -366,7 +366,7 @@ pub fn update_decryptable_supply( mint: &Pubkey, authority: &Pubkey, multisig_signers: &[&Pubkey], - new_decryptable_supply: AeCiphertext, + new_decryptable_supply: &DecryptableBalance, ) -> Result { check_program_account(token_program_id)?; let mut accounts = vec![ @@ -382,7 +382,7 @@ pub fn update_decryptable_supply( TokenInstruction::ConfidentialMintBurnExtension, ConfidentialMintBurnInstruction::UpdateDecryptableSupply, &UpdateDecryptableSupplyData { - new_decryptable_supply: new_decryptable_supply.into(), + new_decryptable_supply: *new_decryptable_supply, }, )) } @@ -417,7 +417,7 @@ pub fn confidential_mint_with_split_proofs( BatchedGroupedCiphertext3HandlesValidityProofData, >, range_proof_location: ProofLocation, - new_decryptable_supply: AeCiphertext, + new_decryptable_supply: &DecryptableBalance, ) -> Result, ProgramError> { check_program_account(token_program_id)?; let mut accounts = vec![AccountMeta::new(*token_account, false)]; @@ -473,7 +473,7 @@ pub fn confidential_mint_with_split_proofs( TokenInstruction::ConfidentialMintBurnExtension, ConfidentialMintBurnInstruction::Mint, &MintInstructionData { - new_decryptable_supply: new_decryptable_supply.into(), + new_decryptable_supply: *new_decryptable_supply, mint_amount_auditor_ciphertext_lo: *mint_amount_auditor_ciphertext_lo, mint_amount_auditor_ciphertext_hi: *mint_amount_auditor_ciphertext_hi, equality_proof_instruction_offset, @@ -495,7 +495,7 @@ pub fn confidential_burn_with_split_proofs( token_account: &Pubkey, mint: &Pubkey, supply_elgamal_pubkey: Option, - new_decryptable_available_balance: DecryptableBalance, + new_decryptable_available_balance: &DecryptableBalance, burn_amount_auditor_ciphertext_lo: &PodElGamalCiphertext, burn_amount_auditor_ciphertext_hi: &PodElGamalCiphertext, authority: &Pubkey, @@ -559,7 +559,7 @@ pub fn confidential_burn_with_split_proofs( TokenInstruction::ConfidentialMintBurnExtension, ConfidentialMintBurnInstruction::Burn, &BurnInstructionData { - new_decryptable_available_balance, + new_decryptable_available_balance: *new_decryptable_available_balance, burn_amount_auditor_ciphertext_lo: *burn_amount_auditor_ciphertext_lo, burn_amount_auditor_ciphertext_hi: *burn_amount_auditor_ciphertext_hi, equality_proof_instruction_offset, diff --git a/token/program-2022/src/extension/confidential_transfer/instruction.rs b/token/program-2022/src/extension/confidential_transfer/instruction.rs index 1419f08e38f..548eabeca17 100644 --- a/token/program-2022/src/extension/confidential_transfer/instruction.rs +++ b/token/program-2022/src/extension/confidential_transfer/instruction.rs @@ -760,7 +760,7 @@ pub fn inner_configure_account( token_program_id: &Pubkey, token_account: &Pubkey, mint: &Pubkey, - decryptable_zero_balance: PodAeCiphertext, + decryptable_zero_balance: &DecryptableBalance, maximum_pending_balance_credit_counter: u64, authority: &Pubkey, multisig_signers: &[&Pubkey], @@ -802,7 +802,7 @@ pub fn inner_configure_account( TokenInstruction::ConfidentialTransferExtension, ConfidentialTransferInstruction::ConfigureAccount, &ConfigureAccountInstructionData { - decryptable_zero_balance, + decryptable_zero_balance: *decryptable_zero_balance, maximum_pending_balance_credit_counter: maximum_pending_balance_credit_counter.into(), proof_instruction_offset, }, @@ -815,7 +815,7 @@ pub fn configure_account( token_program_id: &Pubkey, token_account: &Pubkey, mint: &Pubkey, - decryptable_zero_balance: PodAeCiphertext, + decryptable_zero_balance: &DecryptableBalance, maximum_pending_balance_credit_counter: u64, authority: &Pubkey, multisig_signers: &[&Pubkey], @@ -1013,7 +1013,7 @@ pub fn inner_withdraw( mint: &Pubkey, amount: u64, decimals: u8, - new_decryptable_available_balance: DecryptableBalance, + new_decryptable_available_balance: &DecryptableBalance, authority: &Pubkey, multisig_signers: &[&Pubkey], equality_proof_data_location: ProofLocation, @@ -1076,7 +1076,7 @@ pub fn inner_withdraw( &WithdrawInstructionData { amount: amount.into(), decimals, - new_decryptable_available_balance, + new_decryptable_available_balance: *new_decryptable_available_balance, equality_proof_instruction_offset, range_proof_instruction_offset, }, @@ -1091,7 +1091,7 @@ pub fn withdraw( mint: &Pubkey, amount: u64, decimals: u8, - new_decryptable_available_balance: PodAeCiphertext, + new_decryptable_available_balance: &DecryptableBalance, authority: &Pubkey, multisig_signers: &[&Pubkey], equality_proof_data_location: ProofLocation, @@ -1162,7 +1162,7 @@ pub fn inner_transfer( source_token_account: &Pubkey, mint: &Pubkey, destination_token_account: &Pubkey, - new_source_decryptable_available_balance: DecryptableBalance, + new_source_decryptable_available_balance: &DecryptableBalance, transfer_amount_auditor_ciphertext_lo: &PodElGamalCiphertext, transfer_amount_auditor_ciphertext_hi: &PodElGamalCiphertext, authority: &Pubkey, @@ -1244,7 +1244,7 @@ pub fn inner_transfer( TokenInstruction::ConfidentialTransferExtension, ConfidentialTransferInstruction::Transfer, &TransferInstructionData { - new_source_decryptable_available_balance, + new_source_decryptable_available_balance: *new_source_decryptable_available_balance, transfer_amount_auditor_ciphertext_lo: *transfer_amount_auditor_ciphertext_lo, transfer_amount_auditor_ciphertext_hi: *transfer_amount_auditor_ciphertext_hi, equality_proof_instruction_offset, @@ -1261,7 +1261,7 @@ pub fn transfer( source_token_account: &Pubkey, mint: &Pubkey, destination_token_account: &Pubkey, - new_source_decryptable_available_balance: DecryptableBalance, + new_source_decryptable_available_balance: &DecryptableBalance, transfer_amount_auditor_ciphertext_lo: &PodElGamalCiphertext, transfer_amount_auditor_ciphertext_hi: &PodElGamalCiphertext, authority: &Pubkey, @@ -1359,7 +1359,7 @@ pub fn inner_apply_pending_balance( token_program_id: &Pubkey, token_account: &Pubkey, expected_pending_balance_credit_counter: u64, - new_decryptable_available_balance: DecryptableBalance, + new_decryptable_available_balance: &DecryptableBalance, authority: &Pubkey, multisig_signers: &[&Pubkey], ) -> Result { @@ -1380,7 +1380,7 @@ pub fn inner_apply_pending_balance( ConfidentialTransferInstruction::ApplyPendingBalance, &ApplyPendingBalanceData { expected_pending_balance_credit_counter: expected_pending_balance_credit_counter.into(), - new_decryptable_available_balance, + new_decryptable_available_balance: *new_decryptable_available_balance, }, )) } @@ -1390,7 +1390,7 @@ pub fn apply_pending_balance( token_program_id: &Pubkey, token_account: &Pubkey, pending_balance_instructions: u64, - new_decryptable_available_balance: PodAeCiphertext, + new_decryptable_available_balance: &DecryptableBalance, authority: &Pubkey, multisig_signers: &[&Pubkey], ) -> Result { @@ -1503,7 +1503,7 @@ pub fn inner_transfer_with_fee( source_token_account: &Pubkey, mint: &Pubkey, destination_token_account: &Pubkey, - new_source_decryptable_available_balance: DecryptableBalance, + new_source_decryptable_available_balance: &DecryptableBalance, transfer_amount_auditor_ciphertext_lo: &PodElGamalCiphertext, transfer_amount_auditor_ciphertext_hi: &PodElGamalCiphertext, authority: &Pubkey, @@ -1618,7 +1618,7 @@ pub fn inner_transfer_with_fee( TokenInstruction::ConfidentialTransferExtension, ConfidentialTransferInstruction::TransferWithFee, &TransferWithFeeInstructionData { - new_source_decryptable_available_balance, + new_source_decryptable_available_balance: *new_source_decryptable_available_balance, transfer_amount_auditor_ciphertext_lo: *transfer_amount_auditor_ciphertext_lo, transfer_amount_auditor_ciphertext_hi: *transfer_amount_auditor_ciphertext_hi, equality_proof_instruction_offset, @@ -1637,7 +1637,7 @@ pub fn transfer_with_fee( source_token_account: &Pubkey, mint: &Pubkey, destination_token_account: &Pubkey, - new_source_decryptable_available_balance: DecryptableBalance, + new_source_decryptable_available_balance: &DecryptableBalance, transfer_amount_auditor_ciphertext_lo: &PodElGamalCiphertext, transfer_amount_auditor_ciphertext_hi: &PodElGamalCiphertext, authority: &Pubkey, diff --git a/token/program-2022/src/extension/confidential_transfer_fee/instruction.rs b/token/program-2022/src/extension/confidential_transfer_fee/instruction.rs index b27c82acb61..1d140e5af01 100644 --- a/token/program-2022/src/extension/confidential_transfer_fee/instruction.rs +++ b/token/program-2022/src/extension/confidential_transfer_fee/instruction.rs @@ -275,7 +275,7 @@ pub fn initialize_confidential_transfer_fee_config( token_program_id: &Pubkey, mint: &Pubkey, authority: Option, - withdraw_withheld_authority_elgamal_pubkey: PodElGamalPubkey, + withdraw_withheld_authority_elgamal_pubkey: &PodElGamalPubkey, ) -> Result { check_program_account(token_program_id)?; let accounts = vec![AccountMeta::new(*mint, false)]; @@ -287,7 +287,7 @@ pub fn initialize_confidential_transfer_fee_config( ConfidentialTransferFeeInstruction::InitializeConfidentialTransferFeeConfig, &InitializeConfidentialTransferFeeConfigData { authority: authority.try_into()?, - withdraw_withheld_authority_elgamal_pubkey, + withdraw_withheld_authority_elgamal_pubkey: *withdraw_withheld_authority_elgamal_pubkey, }, )) } From 3ace5565f2d57a8123b5e87537f3f0187aed24d9 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 10 Dec 2024 09:57:10 +0900 Subject: [PATCH 2/5] fix typo --- .../src/extension/confidential_transfer/account_info.rs | 4 ++-- token/program-2022/src/extension/confidential_transfer/mod.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/token/program-2022/src/extension/confidential_transfer/account_info.rs b/token/program-2022/src/extension/confidential_transfer/account_info.rs index 39b819b9a70..672a8028904 100644 --- a/token/program-2022/src/extension/confidential_transfer/account_info.rs +++ b/token/program-2022/src/extension/confidential_transfer/account_info.rs @@ -147,7 +147,7 @@ impl ApplyPendingBalanceAccountInfo { #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)] pub struct WithdrawAccountInfo { - /// The available balance (encrypted by `encrypiton_pubkey`) + /// The available balance (encrypted by `encryption_pubkey`) pub available_balance: EncryptedBalance, /// The decryptable available balance pub decryptable_available_balance: DecryptableBalance, @@ -214,7 +214,7 @@ impl WithdrawAccountInfo { #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)] pub struct TransferAccountInfo { - /// The available balance (encrypted by `encrypiton_pubkey`) + /// The available balance (encrypted by `encryption_pubkey`) pub available_balance: EncryptedBalance, /// The decryptable available balance pub decryptable_available_balance: DecryptableBalance, diff --git a/token/program-2022/src/extension/confidential_transfer/mod.rs b/token/program-2022/src/extension/confidential_transfer/mod.rs index ab1bb71e162..baa371cf89f 100644 --- a/token/program-2022/src/extension/confidential_transfer/mod.rs +++ b/token/program-2022/src/extension/confidential_transfer/mod.rs @@ -90,7 +90,7 @@ pub struct ConfidentialTransferAccount { /// The high 48 bits of the pending balance (encrypted by `elgamal_pubkey`) pub pending_balance_hi: EncryptedBalance, - /// The available balance (encrypted by `encrypiton_pubkey`) + /// The available balance (encrypted by `encryption_pubkey`) pub available_balance: EncryptedBalance, /// The decryptable available balance From 76d1881504d99e939ab6d0a19e725b91dc1207a9 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 10 Dec 2024 10:08:01 +0900 Subject: [PATCH 3/5] make auditor pubkey optional in mint and burn proof generation --- token/confidential-transfer/proof-generation/src/burn.rs | 5 ++++- token/confidential-transfer/proof-generation/src/mint.rs | 5 ++++- token/confidential-transfer/proof-tests/tests/proof_test.rs | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/token/confidential-transfer/proof-generation/src/burn.rs b/token/confidential-transfer/proof-generation/src/burn.rs index ae127d9e9a6..7d7b788fb11 100644 --- a/token/confidential-transfer/proof-generation/src/burn.rs +++ b/token/confidential-transfer/proof-generation/src/burn.rs @@ -36,9 +36,12 @@ pub fn burn_split_proof_data( burn_amount: u64, source_elgamal_keypair: &ElGamalKeypair, source_aes_key: &AeKey, - auditor_elgamal_pubkey: &ElGamalPubkey, + auditor_elgamal_pubkey: Option<&ElGamalPubkey>, supply_elgamal_pubkey: &ElGamalPubkey, ) -> Result { + let default_auditor_pubkey = ElGamalPubkey::default(); + let auditor_elgamal_pubkey = auditor_elgamal_pubkey.unwrap_or(&default_auditor_pubkey); + // split the burn amount into low and high bits let (burn_amount_lo, burn_amount_hi) = try_split_u64(burn_amount, BURN_AMOUNT_LO_BIT_LENGTH) .ok_or(TokenProofGenerationError::IllegalAmountBitLength)?; diff --git a/token/confidential-transfer/proof-generation/src/mint.rs b/token/confidential-transfer/proof-generation/src/mint.rs index 1f06a1c0156..a670a26ee27 100644 --- a/token/confidential-transfer/proof-generation/src/mint.rs +++ b/token/confidential-transfer/proof-generation/src/mint.rs @@ -38,8 +38,11 @@ pub fn mint_split_proof_data( supply_elgamal_keypair: &ElGamalKeypair, supply_aes_key: &AeKey, destination_elgamal_pubkey: &ElGamalPubkey, - auditor_elgamal_pubkey: &ElGamalPubkey, + auditor_elgamal_pubkey: Option<&ElGamalPubkey>, ) -> Result { + let default_auditor_pubkey = ElGamalPubkey::default(); + let auditor_elgamal_pubkey = auditor_elgamal_pubkey.unwrap_or(&default_auditor_pubkey); + // split the mint amount into low and high bits let (mint_amount_lo, mint_amount_hi) = try_split_u64(mint_amount, MINT_AMOUNT_LO_BIT_LENGTH) .ok_or(TokenProofGenerationError::IllegalAmountBitLength)?; diff --git a/token/confidential-transfer/proof-tests/tests/proof_test.rs b/token/confidential-transfer/proof-tests/tests/proof_test.rs index d5e0110e0af..9bcbfa25951 100644 --- a/token/confidential-transfer/proof-tests/tests/proof_test.rs +++ b/token/confidential-transfer/proof-tests/tests/proof_test.rs @@ -238,7 +238,7 @@ fn test_mint_validity(mint_amount: u64, supply: u64) { &supply_keypair, &supply_aes_key, destination_pubkey, - auditor_pubkey, + Some(auditor_pubkey), ) .unwrap(); @@ -291,7 +291,7 @@ fn test_burn_validity(spendable_balance: u64, burn_amount: u64) { burn_amount, &source_keypair, &aes_key, - auditor_pubkey, + Some(auditor_pubkey), supply_pubkey, ) .unwrap(); From 834fc023310e2cc3fe3bb2106b092482fece4c98 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 10 Dec 2024 10:15:47 +0900 Subject: [PATCH 4/5] update token client --- token/client/src/token.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/token/client/src/token.rs b/token/client/src/token.rs index 20cae87c1fe..93e2486280d 100644 --- a/token/client/src/token.rs +++ b/token/client/src/token.rs @@ -301,7 +301,7 @@ impl ExtensionInitializationParams { token_program_id, mint, authority, - withdraw_withheld_authority_elgamal_pubkey, + &withdraw_withheld_authority_elgamal_pubkey, ) } Self::GroupPointer { @@ -2002,14 +2002,14 @@ where ) .unwrap(); - let decryptable_balance = aes_key.encrypt(0); + let decryptable_balance = aes_key.encrypt(0).into(); self.process_ixs( &confidential_transfer::instruction::configure_account( &self.program_id, account, &self.pubkey, - decryptable_balance.into(), + &decryptable_balance, maximum_pending_balance_credit_counter, authority, &multisig_signers, @@ -2216,7 +2216,8 @@ where let new_decryptable_available_balance = account_info .new_decryptable_available_balance(withdraw_amount, aes_key) - .map_err(|_| TokenError::AccountDecryption)?; + .map_err(|_| TokenError::AccountDecryption)? + .into(); self.process_ixs( &confidential_transfer::instruction::withdraw( @@ -2225,7 +2226,7 @@ where &self.pubkey, withdraw_amount, decimals, - new_decryptable_available_balance.into(), + &new_decryptable_available_balance, authority, &multisig_signers, equality_proof_location, @@ -2351,14 +2352,15 @@ where let new_decryptable_available_balance = account_info .new_decryptable_available_balance(transfer_amount, source_aes_key) - .map_err(|_| TokenError::AccountDecryption)?; + .map_err(|_| TokenError::AccountDecryption)? + .into(); let mut instructions = confidential_transfer::instruction::transfer( &self.program_id, source_account, self.get_address(), destination_account, - new_decryptable_available_balance.into(), + &new_decryptable_available_balance, &transfer_amount_auditor_ciphertext_lo, &transfer_amount_auditor_ciphertext_hi, source_authority, @@ -2745,14 +2747,15 @@ where let new_decryptable_available_balance = account_info .new_decryptable_available_balance(transfer_amount, source_aes_key) - .map_err(|_| TokenError::AccountDecryption)?; + .map_err(|_| TokenError::AccountDecryption)? + .into(); let mut instructions = confidential_transfer::instruction::transfer_with_fee( &self.program_id, source_account, self.get_address(), destination_account, - new_decryptable_available_balance.into(), + &new_decryptable_available_balance, &transfer_amount_auditor_ciphertext_lo, &transfer_amount_auditor_ciphertext_hi, source_authority, @@ -2807,14 +2810,15 @@ where let expected_pending_balance_credit_counter = account_info.pending_balance_credit_counter(); let new_decryptable_available_balance = account_info .new_decryptable_available_balance(elgamal_secret_key, aes_key) - .map_err(|_| TokenError::AccountDecryption)?; + .map_err(|_| TokenError::AccountDecryption)? + .into(); self.process_ixs( &[confidential_transfer::instruction::apply_pending_balance( &self.program_id, account, expected_pending_balance_credit_counter, - new_decryptable_available_balance.into(), + &new_decryptable_available_balance, authority, &multisig_signers, )?], From 309e269b868838752f2560efe18bb4c518fad46f Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 10 Dec 2024 10:21:03 +0900 Subject: [PATCH 5/5] update program-2022 tests --- token/program-2022-test/tests/initialize_mint.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/token/program-2022-test/tests/initialize_mint.rs b/token/program-2022-test/tests/initialize_mint.rs index 9152d2942aa..a14641f211d 100644 --- a/token/program-2022-test/tests/initialize_mint.rs +++ b/token/program-2022-test/tests/initialize_mint.rs @@ -523,7 +523,7 @@ async fn fail_invalid_extensions_combination() { &spl_token_2022::id(), &mint_account.pubkey(), Some(Pubkey::new_unique()), - PodElGamalPubkey::default(), + &PodElGamalPubkey::default(), ) .unwrap();