|
| 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