1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
pub use target_arch::*;
use {super::pod, crate::curve25519::ristretto::PodRistrettoPoint};
impl From<(pod::PedersenCommitment, pod::DecryptHandle)> for pod::ElGamalCiphertext {
fn from((commitment, handle): (pod::PedersenCommitment, pod::DecryptHandle)) -> Self {
let mut buf = [0_u8; 64];
buf[..32].copy_from_slice(&commitment.0);
buf[32..].copy_from_slice(&handle.0);
pod::ElGamalCiphertext(buf)
}
}
impl From<pod::ElGamalCiphertext> for (pod::PedersenCommitment, pod::DecryptHandle) {
fn from(ciphertext: pod::ElGamalCiphertext) -> Self {
let commitment: [u8; 32] = ciphertext.0[..32].try_into().unwrap();
let handle: [u8; 32] = ciphertext.0[32..].try_into().unwrap();
(
pod::PedersenCommitment(commitment),
pod::DecryptHandle(handle),
)
}
}
impl From<pod::PedersenCommitment> for PodRistrettoPoint {
fn from(commitment: pod::PedersenCommitment) -> Self {
PodRistrettoPoint(commitment.0)
}
}
impl From<PodRistrettoPoint> for pod::PedersenCommitment {
fn from(point: PodRistrettoPoint) -> Self {
pod::PedersenCommitment(point.0)
}
}
impl From<pod::DecryptHandle> for PodRistrettoPoint {
fn from(handle: pod::DecryptHandle) -> Self {
PodRistrettoPoint(handle.0)
}
}
impl From<PodRistrettoPoint> for pod::DecryptHandle {
fn from(point: PodRistrettoPoint) -> Self {
pod::DecryptHandle(point.0)
}
}
#[cfg(not(target_os = "solana"))]
mod target_arch {
use {
super::pod,
crate::{
curve25519::scalar::PodScalar,
encryption::{
auth_encryption::AeCiphertext,
elgamal::{DecryptHandle, ElGamalCiphertext, ElGamalPubkey},
pedersen::PedersenCommitment,
},
errors::{ProofError, ProofVerificationError},
instruction::{
transfer::{TransferAmountEncryption, TransferPubkeys},
transfer_with_fee::{FeeEncryption, FeeParameters, TransferWithFeePubkeys},
},
range_proof::{errors::RangeProofError, RangeProof},
sigma_proofs::{
equality_proof::{CtxtCommEqualityProof, CtxtCtxtEqualityProof},
errors::*,
fee_proof::FeeSigmaProof,
pubkey_proof::PubkeySigmaProof,
validity_proof::{AggregatedValidityProof, ValidityProof},
zero_balance_proof::ZeroBalanceProof,
},
},
curve25519_dalek::{ristretto::CompressedRistretto, scalar::Scalar},
std::convert::TryFrom,
};
impl From<Scalar> for PodScalar {
fn from(scalar: Scalar) -> Self {
Self(scalar.to_bytes())
}
}
impl TryFrom<PodScalar> for Scalar {
type Error = ProofError;
fn try_from(pod: PodScalar) -> Result<Self, Self::Error> {
Scalar::from_canonical_bytes(pod.0).ok_or(ProofError::CiphertextDeserialization)
}
}
impl From<ElGamalCiphertext> for pod::ElGamalCiphertext {
fn from(ct: ElGamalCiphertext) -> Self {
Self(ct.to_bytes())
}
}
impl TryFrom<pod::ElGamalCiphertext> for ElGamalCiphertext {
type Error = ProofError;
fn try_from(ct: pod::ElGamalCiphertext) -> Result<Self, Self::Error> {
Self::from_bytes(&ct.0).ok_or(ProofError::CiphertextDeserialization)
}
}
impl From<ElGamalPubkey> for pod::ElGamalPubkey {
fn from(pk: ElGamalPubkey) -> Self {
Self(pk.to_bytes())
}
}
impl TryFrom<pod::ElGamalPubkey> for ElGamalPubkey {
type Error = ProofError;
fn try_from(pk: pod::ElGamalPubkey) -> Result<Self, Self::Error> {
Self::from_bytes(&pk.0).ok_or(ProofError::CiphertextDeserialization)
}
}
impl From<CompressedRistretto> for pod::CompressedRistretto {
fn from(cr: CompressedRistretto) -> Self {
Self(cr.to_bytes())
}
}
impl From<pod::CompressedRistretto> for CompressedRistretto {
fn from(pod: pod::CompressedRistretto) -> Self {
Self(pod.0)
}
}
impl From<PedersenCommitment> for pod::PedersenCommitment {
fn from(comm: PedersenCommitment) -> Self {
Self(comm.to_bytes())
}
}
#[cfg(not(target_os = "solana"))]
impl From<pod::PedersenCommitment> for CompressedRistretto {
fn from(pod: pod::PedersenCommitment) -> Self {
Self(pod.0)
}
}
#[cfg(not(target_os = "solana"))]
impl TryFrom<pod::PedersenCommitment> for PedersenCommitment {
type Error = ProofError;
fn try_from(pod: pod::PedersenCommitment) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0).ok_or(ProofError::CiphertextDeserialization)
}
}
#[cfg(not(target_os = "solana"))]
impl From<DecryptHandle> for pod::DecryptHandle {
fn from(handle: DecryptHandle) -> Self {
Self(handle.to_bytes())
}
}
#[cfg(not(target_os = "solana"))]
impl From<pod::DecryptHandle> for CompressedRistretto {
fn from(pod: pod::DecryptHandle) -> Self {
Self(pod.0)
}
}
#[cfg(not(target_os = "solana"))]
impl TryFrom<pod::DecryptHandle> for DecryptHandle {
type Error = ProofError;
fn try_from(pod: pod::DecryptHandle) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0).ok_or(ProofError::CiphertextDeserialization)
}
}
impl From<AeCiphertext> for pod::AeCiphertext {
fn from(ct: AeCiphertext) -> Self {
Self(ct.to_bytes())
}
}
impl TryFrom<pod::AeCiphertext> for AeCiphertext {
type Error = ProofError;
fn try_from(ct: pod::AeCiphertext) -> Result<Self, Self::Error> {
Self::from_bytes(&ct.0).ok_or(ProofError::CiphertextDeserialization)
}
}
impl From<CtxtCommEqualityProof> for pod::CtxtCommEqualityProof {
fn from(proof: CtxtCommEqualityProof) -> Self {
Self(proof.to_bytes())
}
}
impl TryFrom<pod::CtxtCommEqualityProof> for CtxtCommEqualityProof {
type Error = EqualityProofError;
fn try_from(pod: pod::CtxtCommEqualityProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}
impl From<CtxtCtxtEqualityProof> for pod::CtxtCtxtEqualityProof {
fn from(proof: CtxtCtxtEqualityProof) -> Self {
Self(proof.to_bytes())
}
}
impl TryFrom<pod::CtxtCtxtEqualityProof> for CtxtCtxtEqualityProof {
type Error = EqualityProofError;
fn try_from(pod: pod::CtxtCtxtEqualityProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}
impl From<ValidityProof> for pod::ValidityProof {
fn from(proof: ValidityProof) -> Self {
Self(proof.to_bytes())
}
}
impl TryFrom<pod::ValidityProof> for ValidityProof {
type Error = ValidityProofError;
fn try_from(pod: pod::ValidityProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}
impl From<AggregatedValidityProof> for pod::AggregatedValidityProof {
fn from(proof: AggregatedValidityProof) -> Self {
Self(proof.to_bytes())
}
}
impl TryFrom<pod::AggregatedValidityProof> for AggregatedValidityProof {
type Error = ValidityProofError;
fn try_from(pod: pod::AggregatedValidityProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}
impl From<ZeroBalanceProof> for pod::ZeroBalanceProof {
fn from(proof: ZeroBalanceProof) -> Self {
Self(proof.to_bytes())
}
}
impl TryFrom<pod::ZeroBalanceProof> for ZeroBalanceProof {
type Error = ZeroBalanceProofError;
fn try_from(pod: pod::ZeroBalanceProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}
impl From<FeeSigmaProof> for pod::FeeSigmaProof {
fn from(proof: FeeSigmaProof) -> Self {
Self(proof.to_bytes())
}
}
impl TryFrom<pod::FeeSigmaProof> for FeeSigmaProof {
type Error = FeeSigmaProofError;
fn try_from(pod: pod::FeeSigmaProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}
impl From<PubkeySigmaProof> for pod::PubkeySigmaProof {
fn from(proof: PubkeySigmaProof) -> Self {
Self(proof.to_bytes())
}
}
impl TryFrom<pod::PubkeySigmaProof> for PubkeySigmaProof {
type Error = PubkeyValidityProofError;
fn try_from(pod: pod::PubkeySigmaProof) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}
impl TryFrom<RangeProof> for pod::RangeProof64 {
type Error = RangeProofError;
fn try_from(proof: RangeProof) -> Result<Self, Self::Error> {
if proof.ipp_proof.serialized_size() != 448 {
return Err(ProofVerificationError::Deserialization.into());
}
let mut buf = [0_u8; 672];
buf[..32].copy_from_slice(proof.A.as_bytes());
buf[32..64].copy_from_slice(proof.S.as_bytes());
buf[64..96].copy_from_slice(proof.T_1.as_bytes());
buf[96..128].copy_from_slice(proof.T_2.as_bytes());
buf[128..160].copy_from_slice(proof.t_x.as_bytes());
buf[160..192].copy_from_slice(proof.t_x_blinding.as_bytes());
buf[192..224].copy_from_slice(proof.e_blinding.as_bytes());
buf[224..672].copy_from_slice(&proof.ipp_proof.to_bytes());
Ok(pod::RangeProof64(buf))
}
}
impl TryFrom<pod::RangeProof64> for RangeProof {
type Error = RangeProofError;
fn try_from(pod: pod::RangeProof64) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}
#[cfg(not(target_os = "solana"))]
impl TryFrom<RangeProof> for pod::RangeProof128 {
type Error = RangeProofError;
fn try_from(proof: RangeProof) -> Result<Self, Self::Error> {
if proof.ipp_proof.serialized_size() != 512 {
return Err(ProofVerificationError::Deserialization.into());
}
let mut buf = [0_u8; 736];
buf[..32].copy_from_slice(proof.A.as_bytes());
buf[32..64].copy_from_slice(proof.S.as_bytes());
buf[64..96].copy_from_slice(proof.T_1.as_bytes());
buf[96..128].copy_from_slice(proof.T_2.as_bytes());
buf[128..160].copy_from_slice(proof.t_x.as_bytes());
buf[160..192].copy_from_slice(proof.t_x_blinding.as_bytes());
buf[192..224].copy_from_slice(proof.e_blinding.as_bytes());
buf[224..736].copy_from_slice(&proof.ipp_proof.to_bytes());
Ok(pod::RangeProof128(buf))
}
}
impl TryFrom<pod::RangeProof128> for RangeProof {
type Error = RangeProofError;
fn try_from(pod: pod::RangeProof128) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}
#[cfg(not(target_os = "solana"))]
impl TryFrom<RangeProof> for pod::RangeProof256 {
type Error = RangeProofError;
fn try_from(proof: RangeProof) -> Result<Self, Self::Error> {
if proof.ipp_proof.serialized_size() != 576 {
return Err(ProofVerificationError::Deserialization.into());
}
let mut buf = [0_u8; 800];
buf[..32].copy_from_slice(proof.A.as_bytes());
buf[32..64].copy_from_slice(proof.S.as_bytes());
buf[64..96].copy_from_slice(proof.T_1.as_bytes());
buf[96..128].copy_from_slice(proof.T_2.as_bytes());
buf[128..160].copy_from_slice(proof.t_x.as_bytes());
buf[160..192].copy_from_slice(proof.t_x_blinding.as_bytes());
buf[192..224].copy_from_slice(proof.e_blinding.as_bytes());
buf[224..800].copy_from_slice(&proof.ipp_proof.to_bytes());
Ok(pod::RangeProof256(buf))
}
}
impl TryFrom<pod::RangeProof256> for RangeProof {
type Error = RangeProofError;
fn try_from(pod: pod::RangeProof256) -> Result<Self, Self::Error> {
Self::from_bytes(&pod.0)
}
}
impl From<TransferPubkeys> for pod::TransferPubkeys {
fn from(keys: TransferPubkeys) -> Self {
Self {
source_pubkey: keys.source_pubkey.into(),
destination_pubkey: keys.destination_pubkey.into(),
auditor_pubkey: keys.auditor_pubkey.into(),
}
}
}
impl TryFrom<pod::TransferPubkeys> for TransferPubkeys {
type Error = ProofError;
fn try_from(pod: pod::TransferPubkeys) -> Result<Self, Self::Error> {
Ok(Self {
source_pubkey: pod.source_pubkey.try_into()?,
destination_pubkey: pod.destination_pubkey.try_into()?,
auditor_pubkey: pod.auditor_pubkey.try_into()?,
})
}
}
impl From<TransferWithFeePubkeys> for pod::TransferWithFeePubkeys {
fn from(keys: TransferWithFeePubkeys) -> Self {
Self {
source_pubkey: keys.source_pubkey.into(),
destination_pubkey: keys.destination_pubkey.into(),
auditor_pubkey: keys.auditor_pubkey.into(),
withdraw_withheld_authority_pubkey: keys.withdraw_withheld_authority_pubkey.into(),
}
}
}
impl TryFrom<pod::TransferWithFeePubkeys> for TransferWithFeePubkeys {
type Error = ProofError;
fn try_from(pod: pod::TransferWithFeePubkeys) -> Result<Self, Self::Error> {
Ok(Self {
source_pubkey: pod.source_pubkey.try_into()?,
destination_pubkey: pod.destination_pubkey.try_into()?,
auditor_pubkey: pod.auditor_pubkey.try_into()?,
withdraw_withheld_authority_pubkey: pod
.withdraw_withheld_authority_pubkey
.try_into()?,
})
}
}
impl From<TransferAmountEncryption> for pod::TransferAmountEncryption {
fn from(ciphertext: TransferAmountEncryption) -> Self {
Self {
commitment: ciphertext.commitment.into(),
source_handle: ciphertext.source_handle.into(),
destination_handle: ciphertext.destination_handle.into(),
auditor_handle: ciphertext.auditor_handle.into(),
}
}
}
impl TryFrom<pod::TransferAmountEncryption> for TransferAmountEncryption {
type Error = ProofError;
fn try_from(pod: pod::TransferAmountEncryption) -> Result<Self, Self::Error> {
Ok(Self {
commitment: pod.commitment.try_into()?,
source_handle: pod.source_handle.try_into()?,
destination_handle: pod.destination_handle.try_into()?,
auditor_handle: pod.auditor_handle.try_into()?,
})
}
}
impl From<FeeEncryption> for pod::FeeEncryption {
fn from(ciphertext: FeeEncryption) -> Self {
Self {
commitment: ciphertext.commitment.into(),
destination_handle: ciphertext.destination_handle.into(),
withdraw_withheld_authority_handle: ciphertext
.withdraw_withheld_authority_handle
.into(),
}
}
}
impl TryFrom<pod::FeeEncryption> for FeeEncryption {
type Error = ProofError;
fn try_from(pod: pod::FeeEncryption) -> Result<Self, Self::Error> {
Ok(Self {
commitment: pod.commitment.try_into()?,
destination_handle: pod.destination_handle.try_into()?,
withdraw_withheld_authority_handle: pod
.withdraw_withheld_authority_handle
.try_into()?,
})
}
}
impl From<FeeParameters> for pod::FeeParameters {
fn from(parameters: FeeParameters) -> Self {
Self {
fee_rate_basis_points: parameters.fee_rate_basis_points.into(),
maximum_fee: parameters.maximum_fee.into(),
}
}
}
impl From<pod::FeeParameters> for FeeParameters {
fn from(pod: pod::FeeParameters) -> Self {
Self {
fee_rate_basis_points: pod.fee_rate_basis_points.into(),
maximum_fee: pod.maximum_fee.into(),
}
}
}
}
#[cfg(target_os = "solana")]
#[allow(unused_variables)]
mod target_arch {}
#[cfg(test)]
mod tests {
use {
super::*,
crate::{encryption::pedersen::Pedersen, range_proof::RangeProof},
merlin::Transcript,
std::convert::TryInto,
};
#[test]
fn test_pod_range_proof_64() {
let (comm, open) = Pedersen::new(55_u64);
let mut transcript_create = Transcript::new(b"Test");
let mut transcript_verify = Transcript::new(b"Test");
let proof = RangeProof::new(vec![55], vec![64], vec![&open], &mut transcript_create);
let proof_serialized: pod::RangeProof64 = proof.try_into().unwrap();
let proof_deserialized: RangeProof = proof_serialized.try_into().unwrap();
assert!(proof_deserialized
.verify(vec![&comm], vec![64], &mut transcript_verify)
.is_ok());
let proof = RangeProof::new(vec![55], vec![64], vec![&open], &mut transcript_create);
assert!(TryInto::<pod::RangeProof128>::try_into(proof).is_err());
}
#[test]
fn test_pod_range_proof_128() {
let (comm_1, open_1) = Pedersen::new(55_u64);
let (comm_2, open_2) = Pedersen::new(77_u64);
let (comm_3, open_3) = Pedersen::new(99_u64);
let mut transcript_create = Transcript::new(b"Test");
let mut transcript_verify = Transcript::new(b"Test");
let proof = RangeProof::new(
vec![55, 77, 99],
vec![64, 32, 32],
vec![&open_1, &open_2, &open_3],
&mut transcript_create,
);
let proof_serialized: pod::RangeProof128 = proof.try_into().unwrap();
let proof_deserialized: RangeProof = proof_serialized.try_into().unwrap();
assert!(proof_deserialized
.verify(
vec![&comm_1, &comm_2, &comm_3],
vec![64, 32, 32],
&mut transcript_verify,
)
.is_ok());
let proof = RangeProof::new(
vec![55, 77, 99],
vec![64, 32, 32],
vec![&open_1, &open_2, &open_3],
&mut transcript_create,
);
assert!(TryInto::<pod::RangeProof64>::try_into(proof).is_err());
}
}