spl_token_2022/extension/confidential_mint_burn/
verify_proof.rs

1use crate::error::TokenError;
2#[cfg(feature = "zk-ops")]
3use {
4    solana_program::{
5        account_info::{next_account_info, AccountInfo},
6        program_error::ProgramError,
7    },
8    solana_zk_sdk::zk_elgamal_proof_program::proof_data::{
9        BatchedGroupedCiphertext3HandlesValidityProofContext,
10        BatchedGroupedCiphertext3HandlesValidityProofData, BatchedRangeProofContext,
11        BatchedRangeProofU128Data, CiphertextCommitmentEqualityProofContext,
12        CiphertextCommitmentEqualityProofData,
13    },
14    spl_token_confidential_transfer_proof_extraction::{
15        burn::BurnProofContext, instruction::verify_and_extract_context, mint::MintProofContext,
16    },
17    std::slice::Iter,
18};
19
20/// Verify zero-knowledge proofs needed for a [`ConfidentialMint`] instruction
21/// and return the corresponding proof context information.
22#[cfg(feature = "zk-ops")]
23pub fn verify_mint_proof(
24    account_info_iter: &mut Iter<'_, AccountInfo<'_>>,
25    equality_proof_instruction_offset: i8,
26    ciphertext_validity_proof_instruction_offset: i8,
27    range_proof_instruction_offset: i8,
28) -> Result<MintProofContext, ProgramError> {
29    let sysvar_account_info = if equality_proof_instruction_offset != 0 {
30        Some(next_account_info(account_info_iter)?)
31    } else {
32        None
33    };
34
35    let equality_proof_context = verify_and_extract_context::<
36        CiphertextCommitmentEqualityProofData,
37        CiphertextCommitmentEqualityProofContext,
38    >(
39        account_info_iter,
40        equality_proof_instruction_offset as i64,
41        sysvar_account_info,
42    )?;
43
44    let ciphertext_validity_proof_context = verify_and_extract_context::<
45        BatchedGroupedCiphertext3HandlesValidityProofData,
46        BatchedGroupedCiphertext3HandlesValidityProofContext,
47    >(
48        account_info_iter,
49        ciphertext_validity_proof_instruction_offset as i64,
50        sysvar_account_info,
51    )?;
52
53    let range_proof_context =
54        verify_and_extract_context::<BatchedRangeProofU128Data, BatchedRangeProofContext>(
55            account_info_iter,
56            range_proof_instruction_offset as i64,
57            sysvar_account_info,
58        )?;
59
60    Ok(MintProofContext::verify_and_extract(
61        &equality_proof_context,
62        &ciphertext_validity_proof_context,
63        &range_proof_context,
64    )
65    .map_err(|e| -> TokenError { e.into() })?)
66}
67
68/// Verify zero-knowledge proofs needed for a [`ConfidentialBurn`] instruction
69/// and return the corresponding proof context information.
70#[cfg(feature = "zk-ops")]
71pub fn verify_burn_proof(
72    account_info_iter: &mut Iter<'_, AccountInfo<'_>>,
73    equality_proof_instruction_offset: i8,
74    ciphertext_validity_proof_instruction_offset: i8,
75    range_proof_instruction_offset: i8,
76) -> Result<BurnProofContext, ProgramError> {
77    let sysvar_account_info = if equality_proof_instruction_offset != 0 {
78        Some(next_account_info(account_info_iter)?)
79    } else {
80        None
81    };
82
83    let equality_proof_context = verify_and_extract_context::<
84        CiphertextCommitmentEqualityProofData,
85        CiphertextCommitmentEqualityProofContext,
86    >(
87        account_info_iter,
88        equality_proof_instruction_offset as i64,
89        sysvar_account_info,
90    )?;
91
92    let ciphertext_validity_proof_context = verify_and_extract_context::<
93        BatchedGroupedCiphertext3HandlesValidityProofData,
94        BatchedGroupedCiphertext3HandlesValidityProofContext,
95    >(
96        account_info_iter,
97        ciphertext_validity_proof_instruction_offset as i64,
98        sysvar_account_info,
99    )?;
100
101    let range_proof_context =
102        verify_and_extract_context::<BatchedRangeProofU128Data, BatchedRangeProofContext>(
103            account_info_iter,
104            range_proof_instruction_offset as i64,
105            sysvar_account_info,
106        )?;
107
108    Ok(BurnProofContext::verify_and_extract(
109        &equality_proof_context,
110        &ciphertext_validity_proof_context,
111        &range_proof_context,
112    )
113    .map_err(|e| -> TokenError { e.into() })?)
114}