solana_zk_token_sdk/zk_token_elgamal/pod/
sigma_proofs.rs

1//! Plain Old Data types for sigma proofs.
2
3#[cfg(not(target_os = "solana"))]
4use crate::sigma_proofs::{
5    batched_grouped_ciphertext_validity_proof::BatchedGroupedCiphertext2HandlesValidityProof as DecodedBatchedGroupedCiphertext2HandlesValidityProof,
6    batched_grouped_ciphertext_validity_proof::BatchedGroupedCiphertext3HandlesValidityProof as DecodedBatchedGroupedCiphertext3HandlesValidityProof,
7    ciphertext_ciphertext_equality_proof::CiphertextCiphertextEqualityProof as DecodedCiphertextCiphertextEqualityProof,
8    ciphertext_commitment_equality_proof::CiphertextCommitmentEqualityProof as DecodedCiphertextCommitmentEqualityProof,
9    errors::*, fee_proof::FeeSigmaProof as DecodedFeeSigmaProof,
10    grouped_ciphertext_validity_proof::GroupedCiphertext2HandlesValidityProof as DecodedGroupedCiphertext2HandlesValidityProof,
11    grouped_ciphertext_validity_proof::GroupedCiphertext3HandlesValidityProof as DecodedGroupedCiphertext3HandlesValidityProof,
12    pubkey_proof::PubkeyValidityProof as DecodedPubkeyValidityProof,
13    zero_balance_proof::ZeroBalanceProof as DecodedZeroBalanceProof,
14};
15use bytemuck::{Pod, Zeroable};
16
17/// Byte length of a ciphertext-commitment equality proof
18const CIPHERTEXT_COMMITMENT_EQUALITY_PROOF_LEN: usize = 192;
19
20/// Byte length of a ciphertext-ciphertext equality proof
21const CIPHERTEXT_CIPHERTEXT_EQUALITY_PROOF_LEN: usize = 224;
22
23/// Byte length of a grouped ciphertext for 2 handles validity proof
24const GROUPED_CIPHERTEXT_2_HANDLES_VALIDITY_PROOF_LEN: usize = 160;
25
26/// Byte length of a grouped ciphertext for 3 handles validity proof
27const GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_PROOF_LEN: usize = 192;
28
29/// Byte length of a batched grouped ciphertext for 2 handles validity proof
30const BATCHED_GROUPED_CIPHERTEXT_2_HANDLES_VALIDITY_PROOF_LEN: usize = 160;
31
32/// Byte length of a batched grouped ciphertext for 3 handles validity proof
33const BATCHED_GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_PROOF_LEN: usize = 192;
34
35/// Byte length of a zero-balance proof
36const ZERO_BALANCE_PROOF_LEN: usize = 96;
37
38/// Byte length of a fee sigma proof
39const FEE_SIGMA_PROOF_LEN: usize = 256;
40
41/// Byte length of a public key validity proof
42const PUBKEY_VALIDITY_PROOF_LEN: usize = 64;
43
44/// The `CiphertextCommitmentEqualityProof` type as a `Pod`.
45#[derive(Clone, Copy)]
46#[repr(transparent)]
47pub struct CiphertextCommitmentEqualityProof(pub [u8; CIPHERTEXT_COMMITMENT_EQUALITY_PROOF_LEN]);
48
49#[cfg(not(target_os = "solana"))]
50impl From<DecodedCiphertextCommitmentEqualityProof> for CiphertextCommitmentEqualityProof {
51    fn from(decoded_proof: DecodedCiphertextCommitmentEqualityProof) -> Self {
52        Self(decoded_proof.to_bytes())
53    }
54}
55
56#[cfg(not(target_os = "solana"))]
57impl TryFrom<CiphertextCommitmentEqualityProof> for DecodedCiphertextCommitmentEqualityProof {
58    type Error = EqualityProofVerificationError;
59
60    fn try_from(pod_proof: CiphertextCommitmentEqualityProof) -> Result<Self, Self::Error> {
61        Self::from_bytes(&pod_proof.0)
62    }
63}
64
65/// The `CiphertextCiphertextEqualityProof` type as a `Pod`.
66#[derive(Clone, Copy)]
67#[repr(transparent)]
68pub struct CiphertextCiphertextEqualityProof(pub [u8; CIPHERTEXT_CIPHERTEXT_EQUALITY_PROOF_LEN]);
69
70#[cfg(not(target_os = "solana"))]
71impl From<DecodedCiphertextCiphertextEqualityProof> for CiphertextCiphertextEqualityProof {
72    fn from(decoded_proof: DecodedCiphertextCiphertextEqualityProof) -> Self {
73        Self(decoded_proof.to_bytes())
74    }
75}
76
77#[cfg(not(target_os = "solana"))]
78impl TryFrom<CiphertextCiphertextEqualityProof> for DecodedCiphertextCiphertextEqualityProof {
79    type Error = EqualityProofVerificationError;
80
81    fn try_from(pod_proof: CiphertextCiphertextEqualityProof) -> Result<Self, Self::Error> {
82        Self::from_bytes(&pod_proof.0)
83    }
84}
85
86/// The `GroupedCiphertext2HandlesValidityProof` type as a `Pod`.
87#[derive(Clone, Copy)]
88#[repr(transparent)]
89pub struct GroupedCiphertext2HandlesValidityProof(
90    pub [u8; GROUPED_CIPHERTEXT_2_HANDLES_VALIDITY_PROOF_LEN],
91);
92
93#[cfg(not(target_os = "solana"))]
94impl From<DecodedGroupedCiphertext2HandlesValidityProof>
95    for GroupedCiphertext2HandlesValidityProof
96{
97    fn from(decoded_proof: DecodedGroupedCiphertext2HandlesValidityProof) -> Self {
98        Self(decoded_proof.to_bytes())
99    }
100}
101
102#[cfg(not(target_os = "solana"))]
103impl TryFrom<GroupedCiphertext2HandlesValidityProof>
104    for DecodedGroupedCiphertext2HandlesValidityProof
105{
106    type Error = ValidityProofVerificationError;
107
108    fn try_from(pod_proof: GroupedCiphertext2HandlesValidityProof) -> Result<Self, Self::Error> {
109        Self::from_bytes(&pod_proof.0)
110    }
111}
112
113/// The `GroupedCiphertext3HandlesValidityProof` type as a `Pod`.
114#[derive(Clone, Copy)]
115#[repr(transparent)]
116pub struct GroupedCiphertext3HandlesValidityProof(
117    pub [u8; GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_PROOF_LEN],
118);
119
120#[cfg(not(target_os = "solana"))]
121impl From<DecodedGroupedCiphertext3HandlesValidityProof>
122    for GroupedCiphertext3HandlesValidityProof
123{
124    fn from(decoded_proof: DecodedGroupedCiphertext3HandlesValidityProof) -> Self {
125        Self(decoded_proof.to_bytes())
126    }
127}
128
129#[cfg(not(target_os = "solana"))]
130impl TryFrom<GroupedCiphertext3HandlesValidityProof>
131    for DecodedGroupedCiphertext3HandlesValidityProof
132{
133    type Error = ValidityProofVerificationError;
134
135    fn try_from(pod_proof: GroupedCiphertext3HandlesValidityProof) -> Result<Self, Self::Error> {
136        Self::from_bytes(&pod_proof.0)
137    }
138}
139
140/// The `BatchedGroupedCiphertext2HandlesValidityProof` type as a `Pod`.
141#[derive(Clone, Copy)]
142#[repr(transparent)]
143pub struct BatchedGroupedCiphertext2HandlesValidityProof(
144    pub [u8; BATCHED_GROUPED_CIPHERTEXT_2_HANDLES_VALIDITY_PROOF_LEN],
145);
146
147#[cfg(not(target_os = "solana"))]
148impl From<DecodedBatchedGroupedCiphertext2HandlesValidityProof>
149    for BatchedGroupedCiphertext2HandlesValidityProof
150{
151    fn from(decoded_proof: DecodedBatchedGroupedCiphertext2HandlesValidityProof) -> Self {
152        Self(decoded_proof.to_bytes())
153    }
154}
155
156#[cfg(not(target_os = "solana"))]
157impl TryFrom<BatchedGroupedCiphertext2HandlesValidityProof>
158    for DecodedBatchedGroupedCiphertext2HandlesValidityProof
159{
160    type Error = ValidityProofVerificationError;
161
162    fn try_from(
163        pod_proof: BatchedGroupedCiphertext2HandlesValidityProof,
164    ) -> Result<Self, Self::Error> {
165        Self::from_bytes(&pod_proof.0)
166    }
167}
168
169/// The `BatchedGroupedCiphertext3HandlesValidityProof` type as a `Pod`.
170#[derive(Clone, Copy)]
171#[repr(transparent)]
172pub struct BatchedGroupedCiphertext3HandlesValidityProof(
173    pub [u8; BATCHED_GROUPED_CIPHERTEXT_3_HANDLES_VALIDITY_PROOF_LEN],
174);
175
176#[cfg(not(target_os = "solana"))]
177impl From<DecodedBatchedGroupedCiphertext3HandlesValidityProof>
178    for BatchedGroupedCiphertext3HandlesValidityProof
179{
180    fn from(decoded_proof: DecodedBatchedGroupedCiphertext3HandlesValidityProof) -> Self {
181        Self(decoded_proof.to_bytes())
182    }
183}
184
185#[cfg(not(target_os = "solana"))]
186impl TryFrom<BatchedGroupedCiphertext3HandlesValidityProof>
187    for DecodedBatchedGroupedCiphertext3HandlesValidityProof
188{
189    type Error = ValidityProofVerificationError;
190
191    fn try_from(
192        pod_proof: BatchedGroupedCiphertext3HandlesValidityProof,
193    ) -> Result<Self, Self::Error> {
194        Self::from_bytes(&pod_proof.0)
195    }
196}
197
198/// The `ZeroBalanceProof` type as a `Pod`.
199#[derive(Clone, Copy)]
200#[repr(transparent)]
201pub struct ZeroBalanceProof(pub [u8; ZERO_BALANCE_PROOF_LEN]);
202
203#[cfg(not(target_os = "solana"))]
204impl From<DecodedZeroBalanceProof> for ZeroBalanceProof {
205    fn from(decoded_proof: DecodedZeroBalanceProof) -> Self {
206        Self(decoded_proof.to_bytes())
207    }
208}
209
210#[cfg(not(target_os = "solana"))]
211impl TryFrom<ZeroBalanceProof> for DecodedZeroBalanceProof {
212    type Error = ZeroBalanceProofVerificationError;
213
214    fn try_from(pod_proof: ZeroBalanceProof) -> Result<Self, Self::Error> {
215        Self::from_bytes(&pod_proof.0)
216    }
217}
218
219/// The `FeeSigmaProof` type as a `Pod`.
220#[derive(Clone, Copy, bytemuck_derive::Pod, bytemuck_derive::Zeroable)]
221#[repr(transparent)]
222pub struct FeeSigmaProof(pub [u8; FEE_SIGMA_PROOF_LEN]);
223
224#[cfg(not(target_os = "solana"))]
225impl From<DecodedFeeSigmaProof> for FeeSigmaProof {
226    fn from(decoded_proof: DecodedFeeSigmaProof) -> Self {
227        Self(decoded_proof.to_bytes())
228    }
229}
230
231#[cfg(not(target_os = "solana"))]
232impl TryFrom<FeeSigmaProof> for DecodedFeeSigmaProof {
233    type Error = FeeSigmaProofVerificationError;
234
235    fn try_from(pod_proof: FeeSigmaProof) -> Result<Self, Self::Error> {
236        Self::from_bytes(&pod_proof.0)
237    }
238}
239
240/// The `PubkeyValidityProof` type as a `Pod`.
241#[derive(Clone, Copy, bytemuck_derive::Pod, bytemuck_derive::Zeroable)]
242#[repr(transparent)]
243pub struct PubkeyValidityProof(pub [u8; PUBKEY_VALIDITY_PROOF_LEN]);
244
245#[cfg(not(target_os = "solana"))]
246impl From<DecodedPubkeyValidityProof> for PubkeyValidityProof {
247    fn from(decoded_proof: DecodedPubkeyValidityProof) -> Self {
248        Self(decoded_proof.to_bytes())
249    }
250}
251
252#[cfg(not(target_os = "solana"))]
253impl TryFrom<PubkeyValidityProof> for DecodedPubkeyValidityProof {
254    type Error = PubkeyValidityProofVerificationError;
255
256    fn try_from(pod_proof: PubkeyValidityProof) -> Result<Self, Self::Error> {
257        Self::from_bytes(&pod_proof.0)
258    }
259}
260
261// The sigma proof pod types are wrappers for byte arrays, which are both `Pod` and `Zeroable`. However,
262// the marker traits `bytemuck::Pod` and `bytemuck::Zeroable` can only be derived for power-of-two
263// length byte arrays. Directly implement these traits for the sigma proof pod types.
264unsafe impl Zeroable for CiphertextCommitmentEqualityProof {}
265unsafe impl Pod for CiphertextCommitmentEqualityProof {}
266
267unsafe impl Zeroable for CiphertextCiphertextEqualityProof {}
268unsafe impl Pod for CiphertextCiphertextEqualityProof {}
269
270unsafe impl Zeroable for GroupedCiphertext2HandlesValidityProof {}
271unsafe impl Pod for GroupedCiphertext2HandlesValidityProof {}
272
273unsafe impl Zeroable for GroupedCiphertext3HandlesValidityProof {}
274unsafe impl Pod for GroupedCiphertext3HandlesValidityProof {}
275
276unsafe impl Zeroable for BatchedGroupedCiphertext2HandlesValidityProof {}
277unsafe impl Pod for BatchedGroupedCiphertext2HandlesValidityProof {}
278
279unsafe impl Zeroable for BatchedGroupedCiphertext3HandlesValidityProof {}
280unsafe impl Pod for BatchedGroupedCiphertext3HandlesValidityProof {}
281
282unsafe impl Zeroable for ZeroBalanceProof {}
283unsafe impl Pod for ZeroBalanceProof {}