|
| 1 | +use { |
| 2 | + crate::{encryption::BurnAmountCiphertext, errors::TokenProofGenerationError}, |
| 3 | + solana_zk_sdk::{ |
| 4 | + encryption::{ |
| 5 | + elgamal::{ElGamalCiphertext, ElGamalKeypair, ElGamalPubkey}, |
| 6 | + pedersen::Pedersen, |
| 7 | + }, |
| 8 | + zk_elgamal_proof_program::proof_data::{ |
| 9 | + BatchedRangeProofU64Data, CiphertextCommitmentEqualityProofData, |
| 10 | + GroupedCiphertext2HandlesValidityProofData, |
| 11 | + }, |
| 12 | + }, |
| 13 | +}; |
| 14 | + |
| 15 | +const BURN_AMOUNT_BIT_LENGTH: usize = 64; |
| 16 | + |
| 17 | +pub fn burn_split_proof_data( |
| 18 | + burn_amount: u64, |
| 19 | + source_elgamal_keypair: &ElGamalKeypair, |
| 20 | + auditor_elgamal_pubkey: &ElGamalPubkey, |
| 21 | + current_spendable_balance: u64, |
| 22 | + current_spendable_balance_ciphertext: &ElGamalCiphertext, |
| 23 | +) -> Result< |
| 24 | + ( |
| 25 | + CiphertextCommitmentEqualityProofData, |
| 26 | + GroupedCiphertext2HandlesValidityProofData, |
| 27 | + BatchedRangeProofU64Data, |
| 28 | + ), |
| 29 | + TokenProofGenerationError, |
| 30 | +> { |
| 31 | + // Encrypt the burn amount under the source and auditor's ElGamal public key |
| 32 | + let (burn_amount_ciphertext, burn_amount_opening) = BurnAmountCiphertext::new( |
| 33 | + burn_amount, |
| 34 | + source_elgamal_keypair.pubkey(), |
| 35 | + auditor_elgamal_pubkey, |
| 36 | + ); |
| 37 | + |
| 38 | + // Copmute the remaining balance ciphertext |
| 39 | + let burn_amount_ciphertext_source = burn_amount_ciphertext.0.to_elgamal_ciphertext(0).unwrap(); |
| 40 | + |
| 41 | + #[allow(clippy::arithmetic_side_effects)] |
| 42 | + let remaining_balance_ciphertext = |
| 43 | + current_spendable_balance_ciphertext - burn_amount_ciphertext_source; |
| 44 | + |
| 45 | + // Compute the remaining balance at the source |
| 46 | + let remaining_balance = current_spendable_balance |
| 47 | + .checked_sub(burn_amount) |
| 48 | + .ok_or(TokenProofGenerationError::NotEnoughFunds)?; |
| 49 | + |
| 50 | + let (remaining_balance_commitment, remaining_balance_opening) = |
| 51 | + Pedersen::new(remaining_balance); |
| 52 | + |
| 53 | + let equality_proof_data = CiphertextCommitmentEqualityProofData::new( |
| 54 | + source_elgamal_keypair, |
| 55 | + &remaining_balance_ciphertext, |
| 56 | + &remaining_balance_commitment, |
| 57 | + &remaining_balance_opening, |
| 58 | + remaining_balance, |
| 59 | + ) |
| 60 | + .map_err(TokenProofGenerationError::from)?; |
| 61 | + |
| 62 | + let ciphertext_validity_proof_data = GroupedCiphertext2HandlesValidityProofData::new( |
| 63 | + source_elgamal_keypair.pubkey(), |
| 64 | + auditor_elgamal_pubkey, |
| 65 | + &burn_amount_ciphertext.0, |
| 66 | + burn_amount, |
| 67 | + &burn_amount_opening, |
| 68 | + ) |
| 69 | + .map_err(TokenProofGenerationError::from)?; |
| 70 | + |
| 71 | + let range_proof_data = BatchedRangeProofU64Data::new( |
| 72 | + vec![&remaining_balance_commitment], |
| 73 | + vec![remaining_balance], |
| 74 | + vec![BURN_AMOUNT_BIT_LENGTH], |
| 75 | + vec![&remaining_balance_opening], |
| 76 | + ) |
| 77 | + .map_err(TokenProofGenerationError::from)?; |
| 78 | + |
| 79 | + Ok(( |
| 80 | + equality_proof_data, |
| 81 | + ciphertext_validity_proof_data, |
| 82 | + range_proof_data, |
| 83 | + )) |
| 84 | +} |
0 commit comments