orbinum-encrypted-memo 0.2.0

Encrypted memo primitives for Orbinum shielded transactions
Documentation
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
//! Selective Disclosure
//!
//! Structures for selective disclosure of encrypted memo fields.
//! Allows revealing specific fields while maintaining privacy.

use crate::domain::entities::error::MemoError;
use alloc::vec::Vec;

#[cfg(feature = "parity-scale-codec")]
use parity_scale_codec::{Decode, Encode};
#[cfg(feature = "scale-info")]
use scale_info::TypeInfo;

/// Disclosure mask defining which memo fields to reveal
///
/// Controls which fields are revealed. `blinding` must always be false.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
	all(feature = "parity-scale-codec", feature = "scale-info"),
	derive(Encode, Decode, TypeInfo)
)]
pub struct DisclosureMask {
	/// Reveal the value/amount of the memo
	pub disclose_value: bool,

	/// Reveal the owner public key (or hash of it)
	pub disclose_owner: bool,

	/// Reveal the blinding factor (SHOULD ALWAYS BE FALSE)
	pub disclose_blinding: bool,

	/// Reveal the asset ID
	pub disclose_asset_id: bool,
}

impl DisclosureMask {
	/// Creates mask revealing all fields except blinding
	pub fn all() -> Self {
		Self {
			disclose_value: true,
			disclose_owner: true,
			disclose_blinding: false, // NEVER reveal blinding
			disclose_asset_id: true,
		}
	}

	/// Creates mask revealing only value
	pub fn only_value() -> Self {
		Self {
			disclose_value: true,
			disclose_owner: false,
			disclose_blinding: false,
			disclose_asset_id: false,
		}
	}

	/// Creates mask revealing value and asset ID
	pub fn value_and_asset() -> Self {
		Self {
			disclose_value: true,
			disclose_owner: false,
			disclose_blinding: false,
			disclose_asset_id: true,
		}
	}

	/// Creates mask revealing nothing
	pub fn none() -> Self {
		Self {
			disclose_value: false,
			disclose_owner: false,
			disclose_blinding: false,
			disclose_asset_id: false,
		}
	}

	/// Converts mask to 4-bit bitmap for circuit encoding
	pub fn to_bitmap(&self) -> u8 {
		(self.disclose_value as u8)
			| (self.disclose_owner as u8) << 1
			| (self.disclose_blinding as u8) << 2
			| (self.disclose_asset_id as u8) << 3
	}

	/// Creates mask from bitmap (inverse of `to_bitmap`)
	pub fn from_bitmap(bits: u8) -> Self {
		Self {
			disclose_value: (bits & 0b0001) != 0,
			disclose_owner: (bits & 0b0010) != 0,
			disclose_blinding: (bits & 0b0100) != 0,
			disclose_asset_id: (bits & 0b1000) != 0,
		}
	}

	/// Validates mask safety: no blinding, at least one field
	pub fn validate(&self) -> Result<(), MemoError> {
		// RULE 1: NEVER reveal blinding
		if self.disclose_blinding {
			return Err(MemoError::InvalidDisclosureMask(
				"Cannot disclose blinding factor - compromises commitment privacy",
			));
		}

		// RULE 2: At least ONE field must be revealed
		if !self.disclose_value && !self.disclose_owner && !self.disclose_asset_id {
			return Err(MemoError::InvalidDisclosureMask(
				"Must disclose at least one field (value, owner, or asset_id)",
			));
		}

		Ok(())
	}

	/// Counts how many fields will be revealed
	pub fn disclosed_field_count(&self) -> usize {
		let mut count = 0;
		if self.disclose_value {
			count += 1;
		}
		if self.disclose_owner {
			count += 1;
		}
		if self.disclose_blinding {
			count += 1;
		}
		if self.disclose_asset_id {
			count += 1;
		}
		count
	}
}

/// Partially revealed memo data
///
/// Only disclosed fields are Some, others are None.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
	all(feature = "parity-scale-codec", feature = "scale-info"),
	derive(Encode, Decode, TypeInfo)
)]
pub struct PartialMemoData {
	/// Revealed value (None if not disclosed)
	pub value: Option<u64>,

	/// Revealed owner public key (None if not disclosed)
	pub owner_pk: Option<[u8; 32]>,

	/// Revealed blinding factor (None if not disclosed)
	/// This field should normally be None to preserve privacy
	pub blinding: Option<[u8; 32]>,

	/// Revealed asset ID (None if not disclosed)
	pub asset_id: Option<u32>,
}

impl PartialMemoData {
	/// Creates empty partial memo (nothing revealed)
	pub fn empty() -> Self {
		Self {
			value: None,
			owner_pk: None,
			blinding: None,
			asset_id: None,
		}
	}

	/// Creates partial memo by applying disclosure mask to complete memo data
	pub fn from_disclosure(
		memo: &crate::domain::entities::types::MemoData,
		mask: &DisclosureMask,
	) -> Self {
		Self {
			value: if mask.disclose_value {
				Some(memo.value)
			} else {
				None
			},
			owner_pk: if mask.disclose_owner {
				Some(memo.owner_pk)
			} else {
				None
			},
			blinding: if mask.disclose_blinding {
				Some(memo.blinding)
			} else {
				None
			},
			asset_id: if mask.disclose_asset_id {
				Some(memo.asset_id)
			} else {
				None
			},
		}
	}

	/// Checks if no fields are revealed
	pub fn is_empty(&self) -> bool {
		self.value.is_none()
			&& self.owner_pk.is_none()
			&& self.blinding.is_none()
			&& self.asset_id.is_none()
	}

	/// Serializes to bytes: [flags(1)] || [optional fields...]
	pub fn to_bytes(&self) -> Vec<u8> {
		let mut bytes = Vec::new();

		// Flags byte: bitmap of present fields
		let flags = (self.value.is_some() as u8)
			| (self.owner_pk.is_some() as u8) << 1
			| (self.blinding.is_some() as u8) << 2
			| (self.asset_id.is_some() as u8) << 3;
		bytes.push(flags);

		// Append present fields in order
		if let Some(v) = self.value {
			bytes.extend_from_slice(&v.to_le_bytes());
		}
		if let Some(pk) = self.owner_pk {
			bytes.extend_from_slice(&pk);
		}
		if let Some(b) = self.blinding {
			bytes.extend_from_slice(&b);
		}
		if let Some(aid) = self.asset_id {
			bytes.extend_from_slice(&aid.to_le_bytes());
		}

		bytes
	}

	/// Deserializes from bytes
	pub fn from_bytes(bytes: &[u8]) -> Result<Self, MemoError> {
		if bytes.is_empty() {
			return Err(MemoError::InvalidDisclosureData);
		}

		let flags = bytes[0];
		let mut offset = 1;

		// Parse value if present
		let value = if (flags & 0b0001) != 0 {
			if bytes.len() < offset + 8 {
				return Err(MemoError::InvalidDisclosureData);
			}
			let v = u64::from_le_bytes(
				bytes[offset..offset + 8]
					.try_into()
					.map_err(|_| MemoError::InvalidDisclosureData)?,
			);
			offset += 8;
			Some(v)
		} else {
			None
		};

		// Parse owner_pk if present
		let owner_pk = if (flags & 0b0010) != 0 {
			if bytes.len() < offset + 32 {
				return Err(MemoError::InvalidDisclosureData);
			}
			let mut pk = [0u8; 32];
			pk.copy_from_slice(&bytes[offset..offset + 32]);
			offset += 32;
			Some(pk)
		} else {
			None
		};

		// Parse blinding if present
		let blinding = if (flags & 0b0100) != 0 {
			if bytes.len() < offset + 32 {
				return Err(MemoError::InvalidDisclosureData);
			}
			let mut b = [0u8; 32];
			b.copy_from_slice(&bytes[offset..offset + 32]);
			offset += 32;
			Some(b)
		} else {
			None
		};

		// Parse asset_id if present
		let asset_id = if (flags & 0b1000) != 0 {
			if bytes.len() < offset + 4 {
				return Err(MemoError::InvalidDisclosureData);
			}
			let aid = u32::from_le_bytes(
				bytes[offset..offset + 4]
					.try_into()
					.map_err(|_| MemoError::InvalidDisclosureData)?,
			);
			Some(aid)
		} else {
			None
		};

		Ok(Self {
			value,
			owner_pk,
			blinding,
			asset_id,
		})
	}

	/// Validates data consistency
	pub fn validate(&self) -> Result<(), MemoError> {
		// At least one field must be present
		if self.is_empty() {
			return Err(MemoError::InvalidDisclosureData);
		}

		Ok(())
	}
}

impl Default for PartialMemoData {
	fn default() -> Self {
		Self::empty()
	}
}

/// Selective disclosure proof
///
/// Contains Groth16 proof and public signals verified on-chain.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
	all(feature = "parity-scale-codec", feature = "scale-info"),
	derive(Encode, Decode, TypeInfo)
)]
pub struct DisclosureProof {
	/// Groth16 proof (points a, b, c in BN254)
	/// Format: 2*G1 (64 bytes) + 1*G2 (128 bytes) = 192 bytes compressed
	pub proof: Vec<u8>,

	/// Public signals of the circuit (commitment, vk_hash, mask, revealed_owner_hash)
	pub public_signals: DisclosurePublicSignals,

	/// Original mask (for reference and validation)
	pub mask: DisclosureMask,
}

impl DisclosureProof {
	/// Creates new proof
	pub fn new(
		proof: Vec<u8>,
		public_signals: DisclosurePublicSignals,
		mask: DisclosureMask,
	) -> Self {
		Self {
			proof,
			public_signals,
			mask,
		}
	}

	/// Serializes complete proof for on-chain storage
	pub fn to_bytes(&self) -> Vec<u8> {
		let mut bytes = Vec::new();

		// 1. Proof bytes (variable size, prefixed with length)
		let proof_len = self.proof.len() as u16;
		bytes.extend_from_slice(&proof_len.to_le_bytes());
		bytes.extend_from_slice(&self.proof);

		// 2. Public signals
		bytes.extend_from_slice(&self.public_signals.to_bytes());

		// 3. Mask bitmap (1 byte)
		bytes.push(self.mask.to_bitmap());

		bytes
	}

	/// Deserializes proof from bytes
	pub fn from_bytes(bytes: &[u8]) -> Result<Self, MemoError> {
		if bytes.len() < 2 {
			return Err(MemoError::InvalidProof("Proof too short"));
		}

		let mut offset = 0;

		// 1. Parse proof length
		let proof_len = u16::from_le_bytes(
			bytes[offset..offset + 2]
				.try_into()
				.map_err(|_| MemoError::InvalidProof("Invalid proof length"))?,
		) as usize;
		offset += 2;

		// 2. Parse proof bytes
		if bytes.len() < offset + proof_len {
			return Err(MemoError::InvalidProof("Proof bytes truncated"));
		}
		let proof = bytes[offset..offset + proof_len].to_vec();
		offset += proof_len;

		// 3. Parse public signals (76 bytes: 32+8+4+32)
		if bytes.len() < offset + 76 {
			return Err(MemoError::InvalidProof("Public signals truncated"));
		}
		let public_signals = DisclosurePublicSignals::from_bytes(&bytes[offset..offset + 76])?;
		offset += 76;

		// 4. Parse mask bitmap
		if bytes.len() < offset + 1 {
			return Err(MemoError::InvalidProof("Mask bitmap missing"));
		}
		let mask = DisclosureMask::from_bitmap(bytes[offset]);

		Ok(Self {
			proof,
			public_signals,
			mask,
		})
	}

	/// Validates proof consistency before on-chain verification
	pub fn validate(&self) -> Result<(), MemoError> {
		// Validate mask
		self.mask.validate()?;

		// Validate proof not empty
		if self.proof.is_empty() {
			return Err(MemoError::InvalidProof("Proof is empty"));
		}

		// Validate public signals consistency
		self.public_signals.validate(&self.mask)?;

		Ok(())
	}
}

/// Public signals verified on-chain
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
	all(feature = "parity-scale-codec", feature = "scale-info"),
	derive(Encode, Decode, TypeInfo)
)]
pub struct DisclosurePublicSignals {
	/// Commitment of the memo (binds proof to a specific commitment)
	pub commitment: [u8; 32],

	/// Revealed value (value if disclosed, 0 otherwise)
	pub revealed_value: u64,

	/// Revealed asset ID (asset_id if disclosed, 0 otherwise)
	pub revealed_asset_id: u32,

	/// Hash of owner public key (if disclosed), zero otherwise
	pub revealed_owner_hash: [u8; 32],
}

impl DisclosurePublicSignals {
	/// Creates new public signals
	pub fn new(
		commitment: [u8; 32],
		revealed_value: u64,
		revealed_asset_id: u32,
		revealed_owner_hash: [u8; 32],
	) -> Self {
		Self {
			commitment,
			revealed_value,
			revealed_asset_id,
			revealed_owner_hash,
		}
	}

	/// Serializes public signals (76 bytes)
	pub fn to_bytes(&self) -> Vec<u8> {
		let mut bytes = Vec::with_capacity(76);
		bytes.extend_from_slice(&self.commitment);
		bytes.extend_from_slice(&self.revealed_value.to_le_bytes());
		bytes.extend_from_slice(&self.revealed_asset_id.to_le_bytes());
		bytes.extend_from_slice(&self.revealed_owner_hash);
		bytes
	}

	/// Deserializes public signals
	pub fn from_bytes(bytes: &[u8]) -> Result<Self, MemoError> {
		if bytes.len() != 76 {
			return Err(MemoError::InvalidProof("Invalid public signals length"));
		}

		let mut commitment = [0u8; 32];
		commitment.copy_from_slice(&bytes[0..32]);

		let revealed_value = u64::from_le_bytes(
			bytes[32..40]
				.try_into()
				.map_err(|_| MemoError::InvalidProof("Invalid revealed_value"))?,
		);

		let revealed_asset_id = u32::from_le_bytes(
			bytes[40..44]
				.try_into()
				.map_err(|_| MemoError::InvalidProof("Invalid revealed_asset_id"))?,
		);

		let mut revealed_owner_hash = [0u8; 32];
		revealed_owner_hash.copy_from_slice(&bytes[44..76]);

		Ok(Self {
			commitment,
			revealed_value,
			revealed_asset_id,
			revealed_owner_hash,
		})
	}

	/// Validates consistency with the mask
	pub fn validate(&self, mask: &DisclosureMask) -> Result<(), MemoError> {
		// If value revealed, it must be non-zero (or we can't tell)
		if mask.disclose_value && self.revealed_value == 0 {
			// Note: This could also happen if the real value is 0.
		}

		// If owner NOT revealed, hash must be zero
		if !mask.disclose_owner && self.revealed_owner_hash != [0u8; 32] {
			return Err(MemoError::InvalidProof(
				"Owner hash should be zero when owner not disclosed",
			));
		}

		Ok(())
	}

	/// Helper for prover logic
	pub fn disclose_value(&self) -> bool {
		self.revealed_value != 0
	}

	/// Helper for prover logic
	pub fn disclose_asset_id(&self) -> bool {
		self.revealed_asset_id != 0
	}

	/// Helper for prover logic
	pub fn disclose_owner(&self) -> bool {
		self.revealed_owner_hash != [0u8; 32]
	}
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
	use super::*;
	use crate::domain::entities::types::MemoData;
	extern crate alloc;
	use alloc::vec;

	// ===== DisclosureMask Tests =====

	#[test]
	fn test_mask_all() {
		let mask = DisclosureMask::all();
		assert!(mask.disclose_value);
		assert!(mask.disclose_owner);
		assert!(!mask.disclose_blinding); // Never reveal
		assert!(mask.disclose_asset_id);
	}

	#[test]
	fn test_mask_only_value() {
		let mask = DisclosureMask::only_value();
		assert!(mask.disclose_value);
		assert!(!mask.disclose_owner);
		assert!(!mask.disclose_blinding);
		assert!(!mask.disclose_asset_id);
	}

	#[test]
	fn test_mask_value_and_asset() {
		let mask = DisclosureMask::value_and_asset();
		assert!(mask.disclose_value);
		assert!(!mask.disclose_owner);
		assert!(!mask.disclose_blinding);
		assert!(mask.disclose_asset_id);
	}

	#[test]
	fn test_mask_none() {
		let mask = DisclosureMask::none();
		assert!(!mask.disclose_value);
		assert!(!mask.disclose_owner);
		assert!(!mask.disclose_blinding);
		assert!(!mask.disclose_asset_id);
	}

	#[test]
	fn test_mask_to_bitmap() {
		assert_eq!(DisclosureMask::only_value().to_bitmap(), 0b0001);
		assert_eq!(DisclosureMask::value_and_asset().to_bitmap(), 0b1001);
		assert_eq!(DisclosureMask::all().to_bitmap(), 0b1011);
		assert_eq!(DisclosureMask::none().to_bitmap(), 0b0000);
	}

	#[test]
	fn test_mask_from_bitmap() {
		let mask = DisclosureMask::from_bitmap(0b1001);
		assert!(mask.disclose_value);
		assert!(!mask.disclose_owner);
		assert!(!mask.disclose_blinding);
		assert!(mask.disclose_asset_id);
	}

	#[test]
	fn test_mask_bitmap_roundtrip() {
		let original = DisclosureMask::value_and_asset();
		let bitmap = original.to_bitmap();
		let recovered = DisclosureMask::from_bitmap(bitmap);
		assert_eq!(original, recovered);
	}

	#[test]
	fn test_mask_validate_ok() {
		assert!(DisclosureMask::only_value().validate().is_ok());
		assert!(DisclosureMask::all().validate().is_ok());
		assert!(DisclosureMask::value_and_asset().validate().is_ok());
	}

	#[test]
	fn test_mask_validate_blinding_error() {
		let mask = DisclosureMask {
			disclose_value: true,
			disclose_owner: false,
			disclose_blinding: true, // Invalid!
			disclose_asset_id: false,
		};
		assert!(mask.validate().is_err());
	}

	#[test]
	fn test_mask_validate_empty_error() {
		let mask = DisclosureMask::none();
		assert!(mask.validate().is_err());
	}

	#[test]
	fn test_mask_disclosed_field_count() {
		assert_eq!(DisclosureMask::only_value().disclosed_field_count(), 1);
		assert_eq!(DisclosureMask::value_and_asset().disclosed_field_count(), 2);
		assert_eq!(DisclosureMask::all().disclosed_field_count(), 3);
		assert_eq!(DisclosureMask::none().disclosed_field_count(), 0);
	}

	#[test]
	fn test_mask_clone() {
		let mask1 = DisclosureMask::only_value();
		let mask2 = mask1.clone();
		assert_eq!(mask1, mask2);
	}

	// ===== PartialMemoData Tests =====

	#[test]
	fn test_partial_empty() {
		let partial = PartialMemoData::empty();
		assert!(partial.is_empty());
		assert_eq!(partial.value, None);
		assert_eq!(partial.owner_pk, None);
		assert_eq!(partial.blinding, None);
		assert_eq!(partial.asset_id, None);
	}

	#[test]
	fn test_partial_from_disclosure_value_only() {
		let memo = MemoData::new(1000, [1u8; 32], [2u8; 32], 0);
		let mask = DisclosureMask::only_value();
		let partial = PartialMemoData::from_disclosure(&memo, &mask);

		assert_eq!(partial.value, Some(1000));
		assert_eq!(partial.owner_pk, None);
		assert_eq!(partial.blinding, None);
		assert_eq!(partial.asset_id, None);
	}

	#[test]
	fn test_partial_from_disclosure_all() {
		let memo = MemoData::new(500, [5u8; 32], [10u8; 32], 2);
		let mask = DisclosureMask::all();
		let partial = PartialMemoData::from_disclosure(&memo, &mask);

		assert_eq!(partial.value, Some(500));
		assert_eq!(partial.owner_pk, Some([5u8; 32]));
		assert_eq!(partial.blinding, None); // Never disclosed by all()
		assert_eq!(partial.asset_id, Some(2));
	}

	#[test]
	fn test_partial_is_empty() {
		let empty = PartialMemoData::empty();
		assert!(empty.is_empty());

		let not_empty = PartialMemoData {
			value: Some(100),
			owner_pk: None,
			blinding: None,
			asset_id: None,
		};
		assert!(!not_empty.is_empty());
	}

	#[test]
	fn test_partial_to_bytes_value_only() {
		let partial = PartialMemoData {
			value: Some(1000),
			owner_pk: None,
			blinding: None,
			asset_id: None,
		};

		let bytes = partial.to_bytes();
		assert_eq!(bytes[0], 0b0001); // Only value flag
		assert_eq!(bytes.len(), 9); // 1 flag + 8 value
	}

	#[test]
	fn test_partial_to_bytes_all() {
		let partial = PartialMemoData {
			value: Some(500),
			owner_pk: Some([5u8; 32]),
			blinding: Some([10u8; 32]),
			asset_id: Some(2),
		};

		let bytes = partial.to_bytes();
		assert_eq!(bytes[0], 0b1111); // All flags
		assert_eq!(bytes.len(), 77); // 1 + 8 + 32 + 32 + 4
	}

	#[test]
	fn test_partial_from_bytes_value_only() {
		let original = PartialMemoData {
			value: Some(1000),
			owner_pk: None,
			blinding: None,
			asset_id: None,
		};

		let bytes = original.to_bytes();
		let recovered = PartialMemoData::from_bytes(&bytes).unwrap();

		assert_eq!(recovered, original);
	}

	#[test]
	fn test_partial_from_bytes_all() {
		let original = PartialMemoData {
			value: Some(500),
			owner_pk: Some([5u8; 32]),
			blinding: Some([10u8; 32]),
			asset_id: Some(2),
		};

		let bytes = original.to_bytes();
		let recovered = PartialMemoData::from_bytes(&bytes).unwrap();

		assert_eq!(recovered, original);
	}

	#[test]
	fn test_partial_from_bytes_empty_error() {
		let result = PartialMemoData::from_bytes(&[]);
		assert!(result.is_err());
	}

	#[test]
	fn test_partial_from_bytes_truncated_error() {
		let bytes = vec![0b0001]; // Flag says value present, but no value bytes
		let result = PartialMemoData::from_bytes(&bytes);
		assert!(result.is_err());
	}

	#[test]
	fn test_partial_validate_ok() {
		let partial = PartialMemoData {
			value: Some(100),
			owner_pk: None,
			blinding: None,
			asset_id: None,
		};
		assert!(partial.validate().is_ok());
	}

	#[test]
	fn test_partial_validate_empty_error() {
		let partial = PartialMemoData::empty();
		assert!(partial.validate().is_err());
	}

	#[test]
	fn test_partial_default() {
		let partial = PartialMemoData::default();
		assert!(partial.is_empty());
	}

	// ===== DisclosureProof Tests =====

	#[test]
	fn test_proof_new() {
		let proof_bytes = vec![1u8; 192];
		let signals = DisclosurePublicSignals::new([0u8; 32], 1000, 0, [0u8; 32]);
		let mask = DisclosureMask::only_value();

		let proof = DisclosureProof::new(proof_bytes.clone(), signals.clone(), mask.clone());

		assert_eq!(proof.proof, proof_bytes);
		assert_eq!(proof.public_signals, signals);
		assert_eq!(proof.mask, mask);
	}

	#[test]
	fn test_proof_to_bytes() {
		let proof_bytes = vec![1u8; 192];
		let signals = DisclosurePublicSignals::new([0u8; 32], 1000, 0, [0u8; 32]);
		let mask = DisclosureMask::only_value();
		let proof = DisclosureProof::new(proof_bytes, signals, mask);

		let bytes = proof.to_bytes();
		assert!(bytes.len() > 100); // Should have proof + signals + mask
	}

	#[test]
	fn test_proof_from_bytes_roundtrip() {
		let proof_bytes = vec![2u8; 192];
		let signals = DisclosurePublicSignals::new([3u8; 32], 500, 1, [0u8; 32]);
		let mask = DisclosureMask::value_and_asset();
		let original = DisclosureProof::new(proof_bytes, signals, mask);

		let bytes = original.to_bytes();
		let recovered = DisclosureProof::from_bytes(&bytes).unwrap();

		assert_eq!(recovered.proof, original.proof);
		assert_eq!(recovered.public_signals, original.public_signals);
		assert_eq!(recovered.mask, original.mask);
	}

	#[test]
	fn test_proof_from_bytes_too_short() {
		let result = DisclosureProof::from_bytes(&[0u8]);
		assert!(result.is_err());
	}

	#[test]
	fn test_proof_validate_ok() {
		let proof_bytes = vec![1u8; 192];
		let signals = DisclosurePublicSignals::new([0u8; 32], 1000, 0, [0u8; 32]);
		let mask = DisclosureMask::only_value();
		let proof = DisclosureProof::new(proof_bytes, signals, mask);

		assert!(proof.validate().is_ok());
	}

	#[test]
	fn test_proof_validate_empty_proof_error() {
		let signals = DisclosurePublicSignals::new([0u8; 32], 1000, 0, [0u8; 32]);
		let mask = DisclosureMask::only_value();
		let proof = DisclosureProof::new(vec![], signals, mask);

		assert!(proof.validate().is_err());
	}

	#[test]
	fn test_proof_validate_invalid_mask() {
		let proof_bytes = vec![1u8; 192];
		let signals = DisclosurePublicSignals::new([0u8; 32], 0, 0, [0u8; 32]);
		let mask = DisclosureMask::none(); // Invalid: reveals nothing
		let proof = DisclosureProof::new(proof_bytes, signals, mask);

		assert!(proof.validate().is_err());
	}

	#[test]
	fn test_proof_clone() {
		let proof_bytes = vec![1u8; 192];
		let signals = DisclosurePublicSignals::new([0u8; 32], 1000, 0, [0u8; 32]);
		let mask = DisclosureMask::only_value();
		let proof1 = DisclosureProof::new(proof_bytes, signals, mask);
		let proof2 = proof1.clone();

		assert_eq!(proof1, proof2);
	}

	// ===== DisclosurePublicSignals Tests =====

	#[test]
	fn test_signals_new() {
		let signals = DisclosurePublicSignals::new([1u8; 32], 1000, 5, [2u8; 32]);

		assert_eq!(signals.commitment, [1u8; 32]);
		assert_eq!(signals.revealed_value, 1000);
		assert_eq!(signals.revealed_asset_id, 5);
		assert_eq!(signals.revealed_owner_hash, [2u8; 32]);
	}

	#[test]
	fn test_signals_to_bytes() {
		let signals = DisclosurePublicSignals::new([1u8; 32], 500, 2, [3u8; 32]);
		let bytes = signals.to_bytes();

		assert_eq!(bytes.len(), 76); // Fixed size
	}

	#[test]
	fn test_signals_from_bytes_roundtrip() {
		let original = DisclosurePublicSignals::new([10u8; 32], 1234, 56, [20u8; 32]);
		let bytes = original.to_bytes();
		let recovered = DisclosurePublicSignals::from_bytes(&bytes).unwrap();

		assert_eq!(recovered, original);
	}

	#[test]
	fn test_signals_from_bytes_invalid_length() {
		let result = DisclosurePublicSignals::from_bytes(&[0u8; 50]);
		assert!(result.is_err());
	}

	#[test]
	fn test_signals_validate_ok() {
		let signals = DisclosurePublicSignals::new([0u8; 32], 1000, 0, [0u8; 32]);
		let mask = DisclosureMask::only_value();
		assert!(signals.validate(&mask).is_ok());
	}

	#[test]
	fn test_signals_validate_owner_hash_error() {
		let signals = DisclosurePublicSignals::new([0u8; 32], 1000, 0, [1u8; 32]); // Non-zero hash
		let mask = DisclosureMask::only_value(); // Owner not disclosed
		assert!(signals.validate(&mask).is_err());
	}

	#[test]
	fn test_signals_disclose_value() {
		let signals1 = DisclosurePublicSignals::new([0u8; 32], 1000, 0, [0u8; 32]);
		assert!(signals1.disclose_value());

		let signals2 = DisclosurePublicSignals::new([0u8; 32], 0, 0, [0u8; 32]);
		assert!(!signals2.disclose_value());
	}

	#[test]
	fn test_signals_disclose_asset_id() {
		let signals1 = DisclosurePublicSignals::new([0u8; 32], 0, 5, [0u8; 32]);
		assert!(signals1.disclose_asset_id());

		let signals2 = DisclosurePublicSignals::new([0u8; 32], 0, 0, [0u8; 32]);
		assert!(!signals2.disclose_asset_id());
	}

	#[test]
	fn test_signals_disclose_owner() {
		let signals1 = DisclosurePublicSignals::new([0u8; 32], 0, 0, [1u8; 32]);
		assert!(signals1.disclose_owner());

		let signals2 = DisclosurePublicSignals::new([0u8; 32], 0, 0, [0u8; 32]);
		assert!(!signals2.disclose_owner());
	}

	#[test]
	fn test_signals_clone() {
		let signals1 = DisclosurePublicSignals::new([5u8; 32], 999, 7, [10u8; 32]);
		let signals2 = signals1.clone();
		assert_eq!(signals1, signals2);
	}

	// ===== Integration Tests =====

	#[test]
	fn test_full_disclosure_flow() {
		let memo = MemoData::new(1000, [1u8; 32], [2u8; 32], 0);
		let mask = DisclosureMask::only_value();

		// Create partial data
		let partial = PartialMemoData::from_disclosure(&memo, &mask);
		assert_eq!(partial.value, Some(1000));
		assert!(partial.owner_pk.is_none());

		// Serialize and deserialize
		let bytes = partial.to_bytes();
		let recovered = PartialMemoData::from_bytes(&bytes).unwrap();
		assert_eq!(recovered, partial);
	}

	#[test]
	fn test_mask_bitmap_all_combinations() {
		for i in 0..16u8 {
			let mask = DisclosureMask::from_bitmap(i);
			let bitmap = mask.to_bitmap();
			assert_eq!(bitmap, i);
		}
	}

	#[test]
	fn test_partial_memo_different_masks() {
		let memo = MemoData::new(500, [10u8; 32], [20u8; 32], 3);

		let partial1 = PartialMemoData::from_disclosure(&memo, &DisclosureMask::only_value());
		let partial2 = PartialMemoData::from_disclosure(&memo, &DisclosureMask::value_and_asset());
		let partial3 = PartialMemoData::from_disclosure(&memo, &DisclosureMask::all());

		assert_eq!(partial1.value, Some(500));
		assert_eq!(partial1.asset_id, None);

		assert_eq!(partial2.value, Some(500));
		assert_eq!(partial2.asset_id, Some(3));

		assert_eq!(partial3.value, Some(500));
		assert_eq!(partial3.owner_pk, Some([10u8; 32]));
		assert_eq!(partial3.asset_id, Some(3));
	}
}