Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
abcalphabet committed Oct 24, 2024
1 parent 534861e commit b01ac24
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 180 deletions.
10 changes: 1 addition & 9 deletions libraries/pod/src/optional_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<'de> Visitor<'de> for OptionalNonZeroPubkeyVisitor {
where
E: Error,
{
let pkey = Pubkey::from_str(&v)
let pkey = Pubkey::from_str(v)
.map_err(|_| Error::invalid_value(Unexpected::Str(v), &"value string"))?;

OptionalNonZeroPubkey::try_from(Some(pkey))
Expand Down Expand Up @@ -162,14 +162,6 @@ impl From<OptionalNonZeroElGamalPubkey> for Option<PodElGamalPubkey> {
}
}
}
impl OptionalNonZeroElGamalPubkey {
pub fn is_none(&self) -> bool {
self.0 == PodElGamalPubkey::default()
}
pub fn is_some(&self) -> bool {
self.0 != PodElGamalPubkey::default()
}
}

#[cfg(feature = "serde-traits")]
impl Serialize for OptionalNonZeroElGamalPubkey {
Expand Down
9 changes: 1 addition & 8 deletions token/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,7 @@ use {
},
spl_token_group_interface::state::TokenGroup,
spl_token_metadata_interface::state::{Field, TokenMetadata},
std::{
collections::HashMap,
fmt::Display,
process::exit,
rc::Rc,
str::{self, FromStr},
sync::Arc,
},
std::{collections::HashMap, fmt::Display, process::exit, rc::Rc, str::FromStr, sync::Arc},
};

fn print_error_and_exit<T, E: Display>(e: E) -> T {
Expand Down
4 changes: 2 additions & 2 deletions token/client/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use {
ApplyPendingBalanceAccountInfo, EmptyAccountAccountInfo, TransferAccountInfo,
WithdrawAccountInfo,
},
instruction::{ProofContextState, ZkProofData},
ConfidentialTransferAccount, DecryptableBalance,
},
confidential_transfer_fee::{
Expand All @@ -58,6 +57,8 @@ use {
zk_elgamal_proof_program::{
self,
instruction::{close_context_state, ContextStateInfo},
proof_data::*,
state::ProofContextState,
},
},
state::{Account, AccountState, Mint, Multisig},
Expand Down Expand Up @@ -110,7 +111,6 @@ pub enum TokenError {
#[error("decimals specified, but incorrect")]
InvalidDecimals,
}

impl PartialEq for TokenError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
Expand Down
2 changes: 0 additions & 2 deletions token/confidential-transfer/proof-generation/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ pub enum TokenProofGenerationError {
IllegalAmountBitLength,
#[error("fee calculation failed")]
FeeCalculation,
#[error("supply decryption failed")]
SupplyDecryption,
}
7 changes: 0 additions & 7 deletions token/program-2022/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,6 @@ pub enum TokenError {
/// Withdraw / Deposit not allowed for confidential-mint-burn
#[error("Withdraw / Deposit not allowed for confidential-mint-burn")]
IllegalMintBurnConversion,
/// Undecryptable supply when trying to generate confidential-mint proofs
#[error("Could not decrypt difference between current supply and decryptable supply when generating mint proofs")]
SupplyDecryption,
}
impl From<TokenError> for ProgramError {
fn from(e: TokenError) -> Self {
Expand Down Expand Up @@ -456,9 +453,6 @@ impl PrintProgramError for TokenError {
TokenError::IllegalMintBurnConversion => {
msg!("Conversions from normal to confidential token balance and vice versa are illegal if the confidential-mint-burn extension is enabled")
}
TokenError::SupplyDecryption => {
msg!("Could not decrypt difference between current supply and decryptable supply when generating mint proofs")
}
}
}
}
Expand All @@ -471,7 +465,6 @@ impl From<TokenProofGenerationError> for TokenError {
TokenProofGenerationError::NotEnoughFunds => TokenError::InsufficientFunds,
TokenProofGenerationError::IllegalAmountBitLength => TokenError::IllegalBitLength,
TokenProofGenerationError::FeeCalculation => TokenError::FeeCalculation,
TokenProofGenerationError::SupplyDecryption => TokenError::SupplyDecryption,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use {
auth_encryption::{AeCiphertext, AeKey},
elgamal::{ElGamalCiphertext, ElGamalKeypair},
pedersen::PedersenOpening,
pod::{auth_encryption::PodAeCiphertext, elgamal::PodElGamalCiphertext},
pod::{
auth_encryption::PodAeCiphertext,
elgamal::{PodElGamalCiphertext, PodElGamalPubkey},
},
},
zk_elgamal_proof_program::proof_data::CiphertextCiphertextEqualityProofData,
},
spl_pod::optional_keys::OptionalNonZeroElGamalPubkey,
};

/// Confidential Mint Burn extension information needed to construct a
Expand All @@ -24,7 +26,7 @@ pub struct SupplyAccountInfo {
/// The decryptable supply
pub decryptable_supply: PodAeCiphertext,
/// The supply's elgamal pubkey
pub supply_elgamal_pubkey: OptionalNonZeroElGamalPubkey,
pub supply_elgamal_pubkey: PodElGamalPubkey,
}

impl SupplyAccountInfo {
Expand All @@ -46,11 +48,8 @@ impl SupplyAccountInfo {
aes_key: &AeKey,
elgamal_keypair: &ElGamalKeypair,
) -> Result<u64, TokenError> {
if self.supply_elgamal_pubkey.is_none() {
return Err(TokenError::InvalidState);
}
// decrypt the decryptable supply
let current_decyptable_supply = TryInto::<AeCiphertext>::try_into(self.decryptable_supply)
let current_decyptable_supply = AeCiphertext::try_from(self.decryptable_supply)
.map_err(|_| TokenError::MalformedCiphertext)?
.decrypt(aes_key)
.ok_or(TokenError::MalformedCiphertext)?;
Expand All @@ -61,8 +60,8 @@ impl SupplyAccountInfo {
elgamal_keypair.pubkey().encrypt(current_decyptable_supply);
#[allow(clippy::arithmetic_side_effects)]
let supply_delta_ciphertext = decryptable_supply_ciphertext
- (TryInto::<ElGamalCiphertext>::try_into(self.current_supply)
.map_err(|_| TokenError::MalformedCiphertext)?);
- ElGamalCiphertext::try_from(self.current_supply)
.map_err(|_| TokenError::MalformedCiphertext)?;
let decryptable_to_current_diff = elgamal_keypair
.secret()
.decrypt_u32(&supply_delta_ciphertext)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#[cfg(not(target_os = "solana"))]
use crate::proof::{process_proof_location, ProofLocation};
#[cfg(not(target_os = "solana"))]
use solana_zk_sdk::encryption::{auth_encryption::AeCiphertext, elgamal::ElGamalPubkey};
#[cfg(not(target_os = "solana"))]
use solana_zk_sdk::zk_elgamal_proof_program::{
instruction::ProofInstruction,
proof_data::{
BatchedGroupedCiphertext3HandlesValidityProofData, BatchedRangeProofU128Data,
CiphertextCiphertextEqualityProofData, CiphertextCommitmentEqualityProofData,
use {
crate::proof::{process_proof_location, ProofLocation},
solana_zk_sdk::{
encryption::{auth_encryption::AeCiphertext, elgamal::ElGamalPubkey},
zk_elgamal_proof_program::{
instruction::ProofInstruction,
proof_data::{
BatchedGroupedCiphertext3HandlesValidityProofData, BatchedRangeProofU128Data,
CiphertextCiphertextEqualityProofData, CiphertextCommitmentEqualityProofData,
},
},
},
};
#[cfg(feature = "serde-traits")]
use {
crate::serialization::aeciphertext_fromstr,
crate::serialization::{aeciphertext_fromstr, elgamalpubkey_fromstr},
serde::{Deserialize, Serialize},
};
use {
Expand All @@ -29,7 +31,7 @@ use {
pubkey::Pubkey,
},
solana_zk_sdk::encryption::pod::{auth_encryption::PodAeCiphertext, elgamal::PodElGamalPubkey},
spl_pod::optional_keys::{OptionalNonZeroElGamalPubkey, OptionalNonZeroPubkey},
spl_pod::optional_keys::OptionalNonZeroPubkey,
};

/// Confidential Transfer extension instructions
Expand Down Expand Up @@ -98,7 +100,6 @@ pub enum ConfidentialMintBurnInstruction {
/// Mints tokens to confidential balance
///
/// Fails if the destination account is frozen.
/// Fails if the associated mint is extended as `NonTransferable`.
///
/// Accounts expected by this instruction:
///
Expand Down Expand Up @@ -135,11 +136,10 @@ pub enum ConfidentialMintBurnInstruction {
///
/// Data expected by this instruction:
/// `MintInstructionData`
ConfidentialMint,
Mint,
/// Burn tokens from confidential balance
///
/// Fails if the destination account is frozen.
/// Fails if the associated mint is extended as `NonTransferable`.
///
/// Accounts expected by this instruction:
///
Expand Down Expand Up @@ -176,7 +176,7 @@ pub enum ConfidentialMintBurnInstruction {
///
/// Data expected by this instruction:
/// `BurnInstructionData`
ConfidentialBurn,
Burn,
}

/// Data expected by `ConfidentialMintBurnInstruction::InitializeMint`
Expand All @@ -189,7 +189,8 @@ pub struct InitializeMintData {
/// configuration and mint new tokens
pub authority: OptionalNonZeroPubkey,
/// The ElGamal pubkey used to encrypt the confidential supply
pub supply_elgamal_pubkey: OptionalNonZeroElGamalPubkey,
#[cfg_attr(feature = "serde-traits", serde(with = "elgamalpubkey_fromstr"))]
pub supply_elgamal_pubkey: PodElGamalPubkey,
/// The initial 0 supply ecrypted with the supply aes key
#[cfg_attr(feature = "serde-traits", serde(with = "aeciphertext_fromstr"))]
pub decryptable_supply: PodAeCiphertext,
Expand All @@ -202,7 +203,8 @@ pub struct InitializeMintData {
#[repr(C)]
pub struct RotateSupplyElGamalPubkeyData {
/// The new ElGamal pubkey for supply encryption
pub new_supply_elgamal_pubkey: OptionalNonZeroElGamalPubkey,
#[cfg_attr(feature = "serde-traits", serde(with = "elgamalpubkey_fromstr"))]
pub new_supply_elgamal_pubkey: PodElGamalPubkey,
/// The location of the
/// `ProofInstruction::VerifyCiphertextCiphertextEquality` instruction
/// relative to the `RotateSupplyElGamal` instruction in the transaction
Expand Down Expand Up @@ -275,27 +277,21 @@ pub fn initialize_mint(
token_program_id: &Pubkey,
mint: &Pubkey,
authority: &Pubkey,
confidential_supply_pubkey: Option<PodElGamalPubkey>,
decryptable_supply: Option<PodAeCiphertext>,
supply_elgamal_pubkey: PodElGamalPubkey,
decryptable_supply: PodAeCiphertext,
) -> Result<Instruction, ProgramError> {
check_program_account(token_program_id)?;
let accounts = vec![AccountMeta::new(*mint, false)];

let decryptable_supply = if confidential_supply_pubkey.is_some() {
decryptable_supply.ok_or(ProgramError::InvalidInstructionData)?
} else {
PodAeCiphertext::zeroed()
};

let authority = Some(authority);
let authority = Some(*authority);
Ok(encode_instruction(
token_program_id,
accounts,
TokenInstruction::ConfidentialMintBurnExtension,
ConfidentialMintBurnInstruction::InitializeMint,
&InitializeMintData {
authority: authority.try_into()?,
supply_elgamal_pubkey: confidential_supply_pubkey.try_into()?,
supply_elgamal_pubkey,
decryptable_supply,
},
))
Expand Down Expand Up @@ -341,10 +337,7 @@ pub fn rotate_supply_elgamal_pubkey(
TokenInstruction::ConfidentialMintBurnExtension,
ConfidentialMintBurnInstruction::RotateSupplyElGamalPubkey,
&RotateSupplyElGamalPubkeyData {
new_supply_elgamal_pubkey: Some(Into::<PodElGamalPubkey>::into(
new_supply_elgamal_pubkey,
))
.try_into()?,
new_supply_elgamal_pubkey: PodElGamalPubkey::from(new_supply_elgamal_pubkey),
proof_instruction_offset,
},
)];
Expand Down Expand Up @@ -464,7 +457,7 @@ pub fn confidential_mint_with_split_proofs(
token_program_id,
accounts,
TokenInstruction::ConfidentialMintBurnExtension,
ConfidentialMintBurnInstruction::ConfidentialMint,
ConfidentialMintBurnInstruction::Mint,
&MintInstructionData {
new_decryptable_supply: new_decryptable_supply.into(),
equality_proof_instruction_offset,
Expand Down Expand Up @@ -546,7 +539,7 @@ pub fn confidential_burn_with_split_proofs(
token_program_id,
accounts,
TokenInstruction::ConfidentialMintBurnExtension,
ConfidentialMintBurnInstruction::ConfidentialBurn,
ConfidentialMintBurnInstruction::Burn,
&BurnInstructionData {
new_decryptable_available_balance,
equality_proof_instruction_offset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use {
crate::extension::{Extension, ExtensionType},
bytemuck::{Pod, Zeroable},
solana_zk_sdk::encryption::pod::{
auth_encryption::PodAeCiphertext, elgamal::PodElGamalCiphertext,
auth_encryption::PodAeCiphertext,
elgamal::{PodElGamalCiphertext, PodElGamalPubkey},
},
spl_pod::optional_keys::{OptionalNonZeroElGamalPubkey, OptionalNonZeroPubkey},
spl_pod::optional_keys::OptionalNonZeroPubkey,
};

/// Maximum bit length of any mint or burn amount
Expand Down Expand Up @@ -40,7 +41,7 @@ pub struct ConfidentialMintBurn {
/// The decryptable confidential supply of the mint
pub decryptable_supply: PodAeCiphertext,
/// The ElGamal pubkey used to encrypt the confidential supply
pub supply_elgamal_pubkey: OptionalNonZeroElGamalPubkey,
pub supply_elgamal_pubkey: PodElGamalPubkey,
}

impl Extension for ConfidentialMintBurn {
Expand Down
Loading

0 comments on commit b01ac24

Please sign in to comment.