solana_zk_token_sdk/zk_token_elgamal/pod/
mod.rs

1mod auth_encryption;
2mod elgamal;
3mod grouped_elgamal;
4mod instruction;
5mod pedersen;
6mod range_proof;
7mod sigma_proofs;
8
9use {
10    crate::zk_token_proof_instruction::ProofType,
11    num_traits::{FromPrimitive, ToPrimitive},
12    solana_instruction::error::InstructionError,
13    thiserror::Error,
14};
15pub use {
16    auth_encryption::AeCiphertext,
17    bytemuck::{Pod, Zeroable},
18    elgamal::{DecryptHandle, ElGamalCiphertext, ElGamalPubkey},
19    grouped_elgamal::{GroupedElGamalCiphertext2Handles, GroupedElGamalCiphertext3Handles},
20    instruction::{FeeEncryption, FeeParameters, TransferAmountCiphertext},
21    pedersen::PedersenCommitment,
22    range_proof::{RangeProofU128, RangeProofU256, RangeProofU64},
23    sigma_proofs::{
24        BatchedGroupedCiphertext2HandlesValidityProof,
25        BatchedGroupedCiphertext3HandlesValidityProof, CiphertextCiphertextEqualityProof,
26        CiphertextCommitmentEqualityProof, FeeSigmaProof, GroupedCiphertext2HandlesValidityProof,
27        GroupedCiphertext3HandlesValidityProof, PubkeyValidityProof, ZeroBalanceProof,
28    },
29};
30
31#[derive(Error, Debug, Clone, PartialEq, Eq)]
32pub enum ParseError {
33    #[error("String is the wrong size")]
34    WrongSize,
35    #[error("Invalid Base64 string")]
36    Invalid,
37}
38
39#[derive(
40    Clone, Copy, Debug, Default, PartialEq, Eq, bytemuck_derive::Pod, bytemuck_derive::Zeroable,
41)]
42#[repr(transparent)]
43pub struct PodU16([u8; 2]);
44impl From<u16> for PodU16 {
45    fn from(n: u16) -> Self {
46        Self(n.to_le_bytes())
47    }
48}
49impl From<PodU16> for u16 {
50    fn from(pod: PodU16) -> Self {
51        Self::from_le_bytes(pod.0)
52    }
53}
54
55#[derive(
56    Clone, Copy, Debug, Default, PartialEq, Eq, bytemuck_derive::Pod, bytemuck_derive::Zeroable,
57)]
58#[repr(transparent)]
59pub struct PodU64([u8; 8]);
60impl From<u64> for PodU64 {
61    fn from(n: u64) -> Self {
62        Self(n.to_le_bytes())
63    }
64}
65impl From<PodU64> for u64 {
66    fn from(pod: PodU64) -> Self {
67        Self::from_le_bytes(pod.0)
68    }
69}
70
71#[derive(
72    Clone, Copy, Debug, Default, PartialEq, Eq, bytemuck_derive::Pod, bytemuck_derive::Zeroable,
73)]
74#[repr(transparent)]
75pub struct PodProofType(u8);
76impl From<ProofType> for PodProofType {
77    fn from(proof_type: ProofType) -> Self {
78        Self(ToPrimitive::to_u8(&proof_type).unwrap())
79    }
80}
81impl TryFrom<PodProofType> for ProofType {
82    type Error = InstructionError;
83
84    fn try_from(pod: PodProofType) -> Result<Self, Self::Error> {
85        FromPrimitive::from_u8(pod.0).ok_or(Self::Error::InvalidAccountData)
86    }
87}
88
89#[derive(Clone, Copy, bytemuck_derive::Pod, bytemuck_derive::Zeroable, PartialEq, Eq)]
90#[repr(transparent)]
91pub struct CompressedRistretto(pub [u8; 32]);
92
93macro_rules! impl_from_str {
94    (TYPE = $type:ident, BYTES_LEN = $bytes_len:expr, BASE64_LEN = $base64_len:expr) => {
95        impl std::str::FromStr for $type {
96            type Err = crate::zk_token_elgamal::pod::ParseError;
97
98            fn from_str(s: &str) -> Result<Self, Self::Err> {
99                if s.len() > $base64_len {
100                    return Err(Self::Err::WrongSize);
101                }
102                let mut bytes = [0u8; $bytes_len];
103                let decoded_len = BASE64_STANDARD
104                    .decode_slice(s, &mut bytes)
105                    .map_err(|_| Self::Err::Invalid)?;
106                if decoded_len != $bytes_len {
107                    Err(Self::Err::WrongSize)
108                } else {
109                    Ok($type(bytes))
110                }
111            }
112        }
113    };
114}
115pub(crate) use impl_from_str;