masp_primitives 3.0.5

Rust implementations of the experimental MASP primitives (derived from zcash_primitives)
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
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
use crate::asset_type::AssetType;
use borsh::BorshSchema;
use borsh::schema::Fields;
use borsh::schema::add_definition;
use borsh::schema::{Declaration, Definition};
use borsh::{BorshDeserialize, BorshSerialize};
use num_traits::{CheckedAdd, CheckedMul, CheckedNeg, CheckedSub, ConstZero, One, Zero};
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::collections::btree_map::Keys;
use std::collections::btree_map::{IntoIter, Iter};
use std::hash::Hash;
use std::io::{Read, Write};
use std::iter::Sum;
use std::ops::{Add, AddAssign, Index, Mul, MulAssign, Neg, Sub, SubAssign};
use zcash_encoding::Vector;

pub const MAX_MONEY: u64 = u64::MAX;
lazy_static::lazy_static! {
pub static ref DEFAULT_FEE: U64Sum = ValueSum::from_pair(zec(), 1000);
}
pub type I8Sum = ValueSum<AssetType, i8>;

pub type U8Sum = ValueSum<AssetType, u8>;

pub type I16Sum = ValueSum<AssetType, i16>;

pub type U16Sum = ValueSum<AssetType, u16>;

pub type I32Sum = ValueSum<AssetType, i32>;

pub type U32Sum = ValueSum<AssetType, u32>;

pub type I64Sum = ValueSum<AssetType, i64>;

pub type U64Sum = ValueSum<AssetType, u64>;

pub type I128Sum = ValueSum<AssetType, i128>;

pub type U128Sum = ValueSum<AssetType, u128>;

/// A type-safe representation of some quantity of Zcash.
///
/// An ValueSum can only be constructed from an integer that is within the valid monetary
/// range of `{-MAX_MONEY..MAX_MONEY}` (where `MAX_MONEY` = i64::MAX).
/// However, this range is not preserved as an invariant internally; it is possible to
/// add two valid ValueSums together to obtain an invalid ValueSum. It is the user's
/// responsibility to handle the result of serializing potentially-invalid ValueSums. In
/// particular, a `Transaction` containing serialized invalid ValueSums will be rejected
/// by the network consensus rules.
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Default, Debug, PartialEq, Eq, Hash)]
pub struct ValueSum<
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize,
    Value: BorshSerialize + BorshDeserialize + PartialEq + Eq,
>(BTreeMap<Unit, Value>);

impl<Unit, Value> memuse::DynamicUsage for ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Default + PartialOrd,
{
    #[inline(always)]
    fn dynamic_usage(&self) -> usize {
        unimplemented!()
        //self.0.dynamic_usage()
    }

    #[inline(always)]
    fn dynamic_usage_bounds(&self) -> (usize, Option<usize>) {
        unimplemented!()
        //self.0.dynamic_usage_bounds()
    }
}

impl<Unit, Value> ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + PartialOrd,
{
    /// Creates a non-negative ValueSum from a Value.
    pub fn from_nonnegative(atype: Unit, amount: Value) -> Result<Self, ()> {
        if amount.is_zero() {
            Ok(Self::zero())
        } else if Value::zero() <= amount {
            let mut ret = BTreeMap::new();
            ret.insert(atype, amount);
            Ok(ValueSum(ret))
        } else {
            Err(())
        }
    }

    /// Compute the infimum of two ValueSums
    pub fn inf(&self, rhs: &Self) -> Self {
        let mut comps = BTreeMap::new();
        for (atype, rhs_amount) in rhs.components() {
            let lhs_amount = self.get(atype);
            if lhs_amount <= *rhs_amount && !lhs_amount.is_zero() {
                comps.insert(atype.clone(), lhs_amount);
            } else if lhs_amount > *rhs_amount && !rhs_amount.is_zero() {
                comps.insert(atype.clone(), *rhs_amount);
            }
        }
        ValueSum(comps)
    }

    /// Compute the supremum of two ValueSums
    pub fn sup(&self, rhs: &Self) -> Self {
        let mut comps = BTreeMap::new();
        for (atype, rhs_amount) in rhs.components() {
            let lhs_amount = self.get(atype);
            if lhs_amount <= *rhs_amount && !rhs_amount.is_zero() {
                comps.insert(atype.clone(), *rhs_amount);
            } else if lhs_amount > *rhs_amount && !lhs_amount.is_zero() {
                comps.insert(atype.clone(), lhs_amount);
            }
        }
        ValueSum(comps)
    }
}

impl<Unit, Value> ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
{
    /// Creates an ValueSum from a Value.
    pub fn from_pair(atype: Unit, amount: Value) -> Self {
        if amount.is_zero() {
            Self::zero()
        } else {
            let mut ret = BTreeMap::new();
            ret.insert(atype, amount);
            ValueSum(ret)
        }
    }

    /// Filters out everything but the given AssetType from this ValueSum
    pub fn project(&self, index: Unit) -> Self {
        let val = self.0.get(&index).copied().unwrap_or(Value::zero());
        Self::from_pair(index, val)
    }

    /// Get the given AssetType within this ValueSum
    pub fn get(&self, index: &Unit) -> Value {
        *self.0.get(index).unwrap_or(&Value::zero())
    }
}

impl<Unit, Value> ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy,
{
    /// Returns a zero-valued ValueSum.
    pub fn zero() -> Self {
        ValueSum(BTreeMap::new())
    }

    /// Check if ValueSum is zero
    pub fn is_zero(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns an iterator over the amount's non-zero asset-types
    pub fn asset_types(&self) -> Keys<'_, Unit, Value> {
        self.0.keys()
    }

    /// Returns an iterator over the amount's non-zero components
    pub fn components(&self) -> Iter<'_, Unit, Value> {
        self.0.iter()
    }

    /// Returns an iterator over the amount's non-zero components
    pub fn into_components(self) -> IntoIter<Unit, Value> {
        self.0.into_iter()
    }

    /// Filters out the given AssetType from this ValueSum
    pub fn reject(&self, index: Unit) -> Self {
        let mut val = self.clone();
        val.0.remove(&index);
        val
    }
}

impl<Unit, Value> Zero for ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy,
{
    fn zero() -> Self {
        Self(BTreeMap::new())
    }

    fn is_zero(&self) -> bool {
        self.0.is_empty()
    }
}

impl<Unit, Value> BorshSerialize for ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy,
{
    fn serialize<W: Write>(&self, writer: &mut W) -> std::io::Result<()> {
        let vec: Vec<_> = self.components().collect();
        Vector::write(writer, vec.as_ref(), |writer, elt| {
            elt.0.serialize(writer)?;
            elt.1.serialize(writer)?;
            Ok(())
        })
    }
}

impl<Unit, Value> BorshDeserialize for ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize,
    Value: BorshSerialize + BorshDeserialize + PartialEq + Eq,
{
    fn deserialize_reader<R: Read>(reader: &mut R) -> std::io::Result<Self> {
        let vec = Vector::read(reader, |reader| {
            let atype = Unit::deserialize_reader(reader)?;
            let value = Value::deserialize_reader(reader)?;
            Ok((atype, value))
        })?;
        Ok(Self(vec.into_iter().collect()))
    }
}

impl<Unit, Value> BorshSchema for ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + BorshSchema,
    Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + BorshSchema,
{
    fn add_definitions_recursively(definitions: &mut BTreeMap<Declaration, Definition>) {
        let definition = Definition::Enum {
            tag_width: 1,
            variants: vec![
                (253, "u16".into(), u16::declaration()),
                (254, "u32".into(), u32::declaration()),
                (255, "u64".into(), u64::declaration()),
            ],
        };
        add_definition(
            format!("{}::CompactSize", Self::declaration()),
            definition,
            definitions,
        );
        let definition = Definition::Sequence {
            length_width: 0,
            length_range: u64::MIN..=u64::MAX,
            elements: <(Unit, Value)>::declaration(),
        };
        add_definition(
            format!("{}::Sequence", Self::declaration()),
            definition,
            definitions,
        );
        let definition = Definition::Struct {
            fields: Fields::UnnamedFields(vec![
                format!("{}::CompactSize", Self::declaration()),
                format!("{}::Sequence", Self::declaration()),
            ]),
        };
        add_definition(Self::declaration(), definition, definitions);
        u16::add_definitions_recursively(definitions);
        u32::add_definitions_recursively(definitions);
        u64::add_definitions_recursively(definitions);
        <(Unit, Value)>::add_definitions_recursively(definitions);
    }

    fn declaration() -> Declaration {
        format!(
            r#"ValueSum<{}, {}>"#,
            Unit::declaration(),
            Value::declaration()
        )
    }
}

impl ValueSum<AssetType, i32> {
    /// Deserialize an ValueSum object from a list of amounts denominated by
    /// different assets
    pub fn read<R: Read>(reader: &mut R) -> std::io::Result<Self> {
        let vec = Vector::read(reader, |reader| {
            let atype = AssetType::read(reader)?;
            let mut value = [0; 4];
            reader.read_exact(&mut value)?;
            Ok((atype, i32::from_le_bytes(value)))
        })?;
        let mut ret = Self::zero();
        for (atype, amt) in vec {
            ret += Self::from_pair(atype, amt);
        }
        Ok(ret)
    }

    /// Serialize an ValueSum object into a list of amounts denominated by
    /// distinct asset types
    pub fn write<W: Write>(&self, writer: &mut W) -> std::io::Result<()> {
        let vec: Vec<_> = self.components().collect();
        Vector::write(writer, vec.as_ref(), |writer, elt| {
            writer.write_all(elt.0.get_identifier())?;
            writer.write_all(elt.1.to_le_bytes().as_ref())?;
            Ok(())
        })
    }
}

impl ValueSum<AssetType, i64> {
    /// Deserialize an ValueSum object from a list of amounts denominated by
    /// different assets
    pub fn read<R: Read>(reader: &mut R) -> std::io::Result<Self> {
        let vec = Vector::read(reader, |reader| {
            let atype = AssetType::read(reader)?;
            let mut value = [0; 8];
            reader.read_exact(&mut value)?;
            Ok((atype, i64::from_le_bytes(value)))
        })?;
        let mut ret = Self::zero();
        for (atype, amt) in vec {
            ret += Self::from_pair(atype, amt);
        }
        Ok(ret)
    }

    /// Serialize an ValueSum object into a list of amounts denominated by
    /// distinct asset types
    pub fn write<W: Write>(&self, writer: &mut W) -> std::io::Result<()> {
        let vec: Vec<_> = self.components().collect();
        Vector::write(writer, vec.as_ref(), |writer, elt| {
            writer.write_all(elt.0.get_identifier())?;
            writer.write_all(elt.1.to_le_bytes().as_ref())?;
            Ok(())
        })
    }
}

impl ValueSum<AssetType, i128> {
    /// Deserialize an ValueSum object from a list of amounts denominated by
    /// different assets
    pub fn read<R: Read>(reader: &mut R) -> std::io::Result<Self> {
        let vec = Vector::read(reader, |reader| {
            let atype = AssetType::read(reader)?;
            let mut value = [0; 16];
            reader.read_exact(&mut value)?;
            Ok((atype, i128::from_le_bytes(value)))
        })?;
        let mut ret = Self::zero();
        for (atype, amt) in vec {
            ret += Self::from_pair(atype, amt);
        }
        Ok(ret)
    }

    /// Serialize an ValueSum object into a list of amounts denominated by
    /// distinct asset types
    pub fn write<W: Write>(&self, writer: &mut W) -> std::io::Result<()> {
        let vec: Vec<_> = self.components().collect();
        Vector::write(writer, vec.as_ref(), |writer, elt| {
            writer.write_all(elt.0.get_identifier())?;
            writer.write_all(elt.1.to_le_bytes().as_ref())?;
            Ok(())
        })
    }
}

impl<Unit, Value> From<Unit> for ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize,
    Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + One,
{
    fn from(atype: Unit) -> Self {
        let mut ret = BTreeMap::new();
        ret.insert(atype, Value::one());
        ValueSum(ret)
    }
}

impl<Unit, Value> PartialOrd for ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + PartialOrd,
{
    /// One ValueSum is more than or equal to another if each corresponding
    /// coordinate is more than or equal to the other's.
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        let zero = Value::zero();
        let mut ordering = Some(Ordering::Equal);
        for k in self.0.keys().chain(other.0.keys()) {
            let v1 = self.0.get(k).unwrap_or(&zero);
            let v2 = other.0.get(k).unwrap_or(&zero);
            match (v1.partial_cmp(v2), ordering) {
                // Sums cannot be compared if even a single coordinate cannot be
                // compared
                (None, _) => ordering = None,
                // If sums are uncomparable, less, greater, or equal, another
                // equal coordinate will not change that
                (Some(Ordering::Equal), _) => {}
                // A lesser coordinate is inconsistent with the sum being
                // greater, and vice-versa
                (Some(Ordering::Less), Some(Ordering::Greater) | None) => ordering = None,
                (Some(Ordering::Greater), Some(Ordering::Less) | None) => ordering = None,
                // It only takes one lesser coordinate, to make a sum that
                // otherwise would have been equal, to be lesser
                (Some(Ordering::Less), Some(Ordering::Less | Ordering::Equal)) => {
                    ordering = Some(Ordering::Less)
                }
                (Some(Ordering::Greater), Some(Ordering::Greater | Ordering::Equal)) => {
                    ordering = Some(Ordering::Greater)
                }
            }
        }
        ordering
    }
}

macro_rules! impl_index {
    ($struct_type:ty) => {
        impl<Unit> Index<&Unit> for ValueSum<Unit, $struct_type>
        where
            Unit: Hash + Ord + BorshSerialize + BorshDeserialize,
        {
            type Output = $struct_type;
            /// Query how much of the given asset this amount contains
            fn index(&self, index: &Unit) -> &Self::Output {
                self.0
                    .get(index)
                    .unwrap_or(&<$struct_type as ConstZero>::ZERO)
            }
        }
    };
}

impl_index!(i8);

impl_index!(u8);

impl_index!(i16);

impl_index!(u16);

impl_index!(i32);

impl_index!(u32);

impl_index!(i64);

impl_index!(u64);

impl_index!(i128);

impl_index!(u128);

impl<Unit, Lhs, Rhs> MulAssign<Rhs> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize
        + BorshDeserialize
        + PartialEq
        + Eq
        + Copy
        + Zero
        + CheckedMul<Rhs, Output = Lhs>,
    Rhs: Copy,
{
    fn mul_assign(&mut self, rhs: Rhs) {
        *self = self.clone() * &rhs;
    }
}

impl<Unit, Lhs, Rhs> Mul<&Rhs> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + CheckedMul<Rhs>,
    Rhs: Copy,
    <Lhs as CheckedMul<Rhs>>::Output: Zero + BorshSerialize + BorshDeserialize + Eq,
{
    type Output = ValueSum<Unit, <Lhs as CheckedMul<Rhs>>::Output>;

    fn mul(self, rhs: &Rhs) -> Self::Output {
        self.checked_mul(rhs).expect("overflow detected")
    }
}

impl<Unit, Lhs, Rhs> CheckedMul<&Rhs> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + CheckedMul<Rhs>,
    Rhs: Copy,
    <Lhs as CheckedMul<Rhs>>::Output: Zero + BorshSerialize + BorshDeserialize + Eq,
{
    type Output = ValueSum<Unit, <Lhs as CheckedMul<Rhs>>::Output>;

    fn checked_mul(self, rhs: &Rhs) -> Option<Self::Output> {
        let mut comps = BTreeMap::new();
        for (atype, amount) in self.0.iter() {
            comps.insert(atype.clone(), amount.checked_mul(*rhs)?);
        }
        comps.retain(|_, v| *v != <Lhs as CheckedMul<Rhs>>::Output::zero());
        Some(ValueSum(comps))
    }
}

impl<Unit, Lhs, Rhs> AddAssign<&ValueSum<Unit, Rhs>> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize
        + BorshDeserialize
        + PartialEq
        + Eq
        + Copy
        + Zero
        + CheckedAdd<Rhs, Output = Lhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
{
    fn add_assign(&mut self, rhs: &ValueSum<Unit, Rhs>) {
        *self = self.clone() + rhs;
    }
}

impl<Unit, Lhs, Rhs> AddAssign<ValueSum<Unit, Rhs>> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize
        + BorshDeserialize
        + PartialEq
        + Eq
        + Copy
        + Zero
        + CheckedAdd<Rhs, Output = Lhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
{
    fn add_assign(&mut self, rhs: ValueSum<Unit, Rhs>) {
        *self += &rhs
    }
}

impl<Unit, Lhs, Rhs> Add<&ValueSum<Unit, Rhs>> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + CheckedAdd<Rhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
    <Lhs as CheckedAdd<Rhs>>::Output: BorshSerialize + BorshDeserialize + Eq + Zero,
{
    type Output = ValueSum<Unit, <Lhs as CheckedAdd<Rhs>>::Output>;

    fn add(self, rhs: &ValueSum<Unit, Rhs>) -> Self::Output {
        self.checked_add(rhs).expect("overflow detected")
    }
}

impl<Unit, Lhs, Rhs> Add<ValueSum<Unit, Rhs>> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + CheckedAdd<Rhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
    <Lhs as CheckedAdd<Rhs>>::Output: BorshSerialize + BorshDeserialize + Eq + Zero,
{
    type Output = ValueSum<Unit, <Lhs as CheckedAdd<Rhs>>::Output>;

    fn add(self, rhs: ValueSum<Unit, Rhs>) -> Self::Output {
        self + &rhs
    }
}

impl<Unit, Lhs, Rhs> CheckedAdd<&ValueSum<Unit, Rhs>> for &ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + CheckedAdd<Rhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
    <Lhs as CheckedAdd<Rhs>>::Output: BorshSerialize + BorshDeserialize + Eq + Zero,
{
    type Output = ValueSum<Unit, <Lhs as CheckedAdd<Rhs>>::Output>;

    fn checked_add(self, v: &ValueSum<Unit, Rhs>) -> Option<Self::Output> {
        let mut comps = BTreeMap::new();
        for (atype, amount) in self.components() {
            comps.insert(atype.clone(), amount.checked_add(Rhs::zero())?);
        }
        for (atype, amount) in v.components() {
            comps.insert(atype.clone(), self.get(atype).checked_add(*amount)?);
        }
        comps.retain(|_, v| *v != <Lhs as CheckedAdd<Rhs>>::Output::zero());
        Some(ValueSum(comps))
    }
}

impl<Unit, Lhs, Rhs> SubAssign<&ValueSum<Unit, Rhs>> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize
        + BorshDeserialize
        + PartialEq
        + Eq
        + Copy
        + Zero
        + CheckedSub<Rhs, Output = Lhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
{
    fn sub_assign(&mut self, rhs: &ValueSum<Unit, Rhs>) {
        *self = self.clone() - rhs
    }
}

impl<Unit, Lhs, Rhs> SubAssign<ValueSum<Unit, Rhs>> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize
        + BorshDeserialize
        + PartialEq
        + Eq
        + Copy
        + Zero
        + CheckedSub<Rhs, Output = Lhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
{
    fn sub_assign(&mut self, rhs: ValueSum<Unit, Rhs>) {
        *self -= &rhs
    }
}

impl<Unit, Value> Neg for ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Value:
        BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + PartialOrd + CheckedNeg,
    <Value as CheckedNeg>::Output: BorshSerialize + BorshDeserialize + Eq + Zero,
{
    type Output = ValueSum<Unit, <Value as CheckedNeg>::Output>;

    fn neg(self) -> Self::Output {
        self.checked_neg().expect("overflow detected")
    }
}

impl<Unit, Value> CheckedNeg for ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Value:
        BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + PartialOrd + CheckedNeg,
    <Value as CheckedNeg>::Output: BorshSerialize + BorshDeserialize + Eq + Zero,
{
    type Output = ValueSum<Unit, <Value as CheckedNeg>::Output>;

    fn checked_neg(mut self) -> Option<Self::Output> {
        let mut comps = BTreeMap::new();
        for (atype, amount) in self.0.iter_mut() {
            comps.insert(atype.clone(), amount.checked_neg()?);
        }
        comps.retain(|_, v| *v != <Value as CheckedNeg>::Output::zero());
        Some(ValueSum(comps))
    }
}

impl<Unit, Lhs, Rhs> Sub<&ValueSum<Unit, Rhs>> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + CheckedSub<Rhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
    <Lhs as CheckedSub<Rhs>>::Output: BorshSerialize + BorshDeserialize + Eq + Zero,
{
    type Output = ValueSum<Unit, <Lhs as CheckedSub<Rhs>>::Output>;

    fn sub(self, rhs: &ValueSum<Unit, Rhs>) -> Self::Output {
        self.checked_sub(rhs).expect("underflow detected")
    }
}

impl<Unit, Lhs, Rhs> Sub<ValueSum<Unit, Rhs>> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + CheckedSub<Rhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
    <Lhs as CheckedSub<Rhs>>::Output: BorshSerialize + BorshDeserialize + Eq + Zero,
{
    type Output = ValueSum<Unit, <Lhs as CheckedSub<Rhs>>::Output>;

    fn sub(self, rhs: ValueSum<Unit, Rhs>) -> Self::Output {
        self - &rhs
    }
}

impl<Unit, Lhs, Rhs> CheckedSub<&ValueSum<Unit, Rhs>> for &ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + CheckedSub<Rhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
    <Lhs as CheckedSub<Rhs>>::Output: BorshSerialize + BorshDeserialize + Eq + Zero,
{
    type Output = ValueSum<Unit, <Lhs as CheckedSub<Rhs>>::Output>;

    fn checked_sub(self, v: &ValueSum<Unit, Rhs>) -> Option<Self::Output> {
        let mut comps = BTreeMap::new();
        for (atype, amount) in self.components() {
            comps.insert(atype.clone(), amount.checked_sub(Rhs::zero())?);
        }
        for (atype, amount) in v.components() {
            comps.insert(atype.clone(), self.get(atype).checked_sub(*amount)?);
        }
        comps.retain(|_, v| *v != <Lhs as CheckedSub<Rhs>>::Output::zero());
        Some(ValueSum(comps))
    }
}

impl<Unit, Value> Sum for ValueSum<Unit, Value>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + PartialOrd,
    Self: Add<Output = Self>,
{
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self::zero(), Add::add)
    }
}

impl<Unit, Output> ValueSum<Unit, Output>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Output: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
{
    pub fn try_from_sum<Value>(
        x: ValueSum<Unit, Value>,
    ) -> Result<Self, <Output as TryFrom<Value>>::Error>
    where
        Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy,
        Output: TryFrom<Value>,
    {
        let mut comps = BTreeMap::new();
        for (atype, amount) in x.0 {
            comps.insert(atype, amount.try_into()?);
        }
        comps.retain(|_, v| *v != Output::zero());
        Ok(Self(comps))
    }

    pub fn from_sum<Value>(x: ValueSum<Unit, Value>) -> Self
    where
        Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy,
        Output: From<Value>,
    {
        let mut comps = BTreeMap::new();
        for (atype, amount) in x.0 {
            comps.insert(atype, amount.into());
        }
        comps.retain(|_, v| *v != Output::zero());
        Self(comps)
    }
}

impl<Unit, Lhs, Rhs> CheckedMul<&ValueSum<Unit, Rhs>> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + CheckedMul<Rhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
    <Lhs as CheckedMul<Rhs>>::Output: Zero
        + BorshSerialize
        + BorshDeserialize
        + Eq
        + CheckedAdd<Output = <Lhs as CheckedMul<Rhs>>::Output>,
{
    type Output = <Lhs as CheckedMul<Rhs>>::Output;

    fn checked_mul(self, rhs: &ValueSum<Unit, Rhs>) -> Option<Self::Output> {
        let mut product = Self::Output::zero();
        for (atype, amount) in rhs.components() {
            product = product.checked_add(self.get(atype).checked_mul(*amount)?)?;
        }
        Some(product)
    }
}

impl<Unit, Lhs, Rhs> Mul<&ValueSum<Unit, Rhs>> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + CheckedMul<Rhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
    <Lhs as CheckedMul<Rhs>>::Output: Zero
        + BorshSerialize
        + BorshDeserialize
        + Eq
        + CheckedAdd<Output = <Lhs as CheckedMul<Rhs>>::Output>,
{
    type Output = <Lhs as CheckedMul<Rhs>>::Output;

    fn mul(self, rhs: &ValueSum<Unit, Rhs>) -> Self::Output {
        self.checked_mul(rhs).expect("overflow detected")
    }
}

impl<Unit, Lhs, Rhs> Mul<ValueSum<Unit, Rhs>> for ValueSum<Unit, Lhs>
where
    Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
    Lhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero + CheckedMul<Rhs>,
    Rhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Zero,
    <Lhs as CheckedMul<Rhs>>::Output: Zero
        + BorshSerialize
        + BorshDeserialize
        + Eq
        + CheckedAdd<Output = <Lhs as CheckedMul<Rhs>>::Output>,
{
    type Output = <Lhs as CheckedMul<Rhs>>::Output;

    fn mul(self, rhs: ValueSum<Unit, Rhs>) -> Self::Output {
        self * &rhs
    }
}

/// A type for balance violations in amount addition and subtraction
/// (overflow and underflow of allowed ranges)
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum BalanceError {
    Overflow,
    Underflow,
}

impl std::fmt::Display for BalanceError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match &self {
            BalanceError::Overflow => {
                write!(
                    f,
                    "ValueSum addition resulted in a value outside the valid range."
                )
            }
            BalanceError::Underflow => write!(
                f,
                "ValueSum subtraction resulted in a value outside the valid range."
            ),
        }
    }
}

pub fn zec() -> AssetType {
    AssetType::new(b"ZEC").unwrap()
}

pub fn default_fee() -> ValueSum<AssetType, i64> {
    ValueSum::from_pair(zec(), 10000)
}

#[cfg(any(test, feature = "test-dependencies"))]
pub mod testing {
    use proptest::prelude::prop_compose;

    use super::{I64Sum, I128Sum, MAX_MONEY, U64Sum, ValueSum};
    use crate::asset_type::testing::arb_asset_type;

    prop_compose! {
        pub fn arb_i64_sum()(asset_type in arb_asset_type(), amt in i64::MIN..i64::MAX) -> I64Sum {
            ValueSum::from_pair(asset_type, amt)
        }
    }

    prop_compose! {
        pub fn arb_i128_sum()(asset_type in arb_asset_type(), amt in i128::MIN..i128::MAX) -> I128Sum {
            ValueSum::from_pair(asset_type, amt)
        }
    }

    prop_compose! {
        pub fn arb_nonnegative_amount()(asset_type in arb_asset_type(), amt in 0u64..MAX_MONEY) -> U64Sum {
            ValueSum::from_pair(asset_type, amt)
        }
    }

    prop_compose! {
        pub fn arb_positive_amount()(asset_type in arb_asset_type(), amt in 1u64..MAX_MONEY) -> U64Sum {
            ValueSum::from_pair(asset_type, amt)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{I32Sum, I64Sum, I128Sum, MAX_MONEY, ValueSum, zec};

    #[test]
    fn amount_in_range() {
        let mut bytes = Vec::with_capacity(100);

        macro_rules! test_vector {
            ($amount:ident) => {{
                bytes.clear();
                $amount.write(&mut bytes).unwrap();
                format!(
                    "b\"{}\",\n",
                    std::str::from_utf8(
                        &bytes
                            .iter()
                            .flat_map(|b| std::ascii::escape_default(*b))
                            .collect::<Vec<_>>(),
                    )
                    .unwrap()
                )
            }};
        }
        macro_rules! value_amount {
            ($t:ty, $val:expr) => {{
                let mut amount = <$t>::from_pair(zec(), 1);
                *amount.0.get_mut(&zec()).unwrap() = $val;
                amount
            }};
        }

        let test_amounts_i32 = [
            value_amount!(I32Sum, 0), // zec() asset with value 0
            I32Sum::from_pair(zec(), -1),
            I32Sum::from_pair(zec(), i32::MAX),
            I32Sum::from_pair(zec(), -i32::MAX),
        ];

        let test_amounts_i64 = [
            value_amount!(I64Sum, 0), // zec() asset with value 0
            I64Sum::from_pair(zec(), -1),
            I64Sum::from_pair(zec(), i64::MAX),
            I64Sum::from_pair(zec(), -i64::MAX),
        ];

        let test_amounts_i128 = [
            value_amount!(I128Sum, 0), // zec() asset with value 0
            I128Sum::from_pair(zec(), MAX_MONEY as i128),
            value_amount!(I128Sum, MAX_MONEY as i128 + 1),
            I128Sum::from_pair(zec(), -(MAX_MONEY as i128)),
            value_amount!(I128Sum, -(MAX_MONEY as i128 - 1)),
        ];

        println!(
            "let test_vectors_i32 = [{}];",
            test_amounts_i32
                .iter()
                .map(|a| test_vector!(a))
                .collect::<String>()
        );
        println!(
            "let test_vectors_i64 = [{}];",
            test_amounts_i64
                .iter()
                .map(|a| test_vector!(a))
                .collect::<String>()
        );

        println!(
            "let test_vectors_i128 = [{}];",
            test_amounts_i128
                .iter()
                .map(|a| test_vector!(a))
                .collect::<String>()
        );

        let zero = b"\x00";
        let test_vectors_i32 = [b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\x00\x00\x00\x00",
        b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\xff\xff\xff\xff",
        b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\xff\xff\xff\x7f",
        b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\x01\x00\x00\x80",
        ];
        let test_vectors_i64 = [b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\x00\x00\x00\x00\x00\x00\x00\x00",
        b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\xff\xff\xff\xff\xff\xff\xff\xff",
        b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\xff\xff\xff\xff\xff\xff\xff\x7f",
        b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\x01\x00\x00\x00\x00\x00\x00\x80",
        ];
        let test_vectors_i128 = [b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
        b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00",
        b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00",
        b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\x01\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff",
        b"\x01\x94\xf3O\xfdd\xef\n\xc3i\x08\xfd\xdf\xec\x05hX\x06)\xc4Vq\x0f\xa1\x86\x83\x12\xa8\x7f\xbf\n\xa5\t\x02\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff",
        ];

        assert_eq!(
            I32Sum::read(&mut test_vectors_i32[0].as_ref()).unwrap(),
            ValueSum::zero()
        );
        assert_eq!(
            I64Sum::read(&mut test_vectors_i64[0].as_ref()).unwrap(),
            ValueSum::zero()
        );
        assert_eq!(
            I128Sum::read(&mut test_vectors_i128[0].as_ref()).unwrap(),
            ValueSum::zero()
        );

        assert_eq!(I32Sum::read(&mut zero.as_ref()).unwrap(), ValueSum::zero());
        assert_eq!(I64Sum::read(&mut zero.as_ref()).unwrap(), ValueSum::zero());
        assert_eq!(I128Sum::read(&mut zero.as_ref()).unwrap(), ValueSum::zero());

        test_vectors_i32
            .iter()
            .skip(1)
            .zip(test_amounts_i32.iter().skip(1))
            .for_each(|(tv, ta)| assert_eq!(I32Sum::read(&mut tv.as_ref()).unwrap(), *ta));
        test_vectors_i64
            .iter()
            .skip(1)
            .zip(test_amounts_i64.iter().skip(1))
            .for_each(|(tv, ta)| assert_eq!(I64Sum::read(&mut tv.as_ref()).unwrap(), *ta));
        test_vectors_i128
            .iter()
            .skip(1)
            .zip(test_amounts_i128.iter().skip(1))
            .for_each(|(tv, ta)| assert_eq!(I128Sum::read(&mut tv.as_ref()).unwrap(), *ta));
    }

    #[test]
    #[should_panic]
    fn add_panics_on_overflow() {
        let v = ValueSum::from_pair(zec(), MAX_MONEY);
        let _sum = v + ValueSum::from_pair(zec(), 1);
    }

    #[test]
    #[should_panic]
    fn add_assign_panics_on_overflow() {
        let mut a = ValueSum::from_pair(zec(), MAX_MONEY);
        a += ValueSum::from_pair(zec(), 1);
    }

    #[test]
    #[should_panic]
    fn sub_panics_on_underflow() {
        let v = ValueSum::from_pair(zec(), 0u64);
        let _diff = v - ValueSum::from_pair(zec(), 1);
    }

    #[test]
    #[should_panic]
    fn sub_assign_panics_on_underflow() {
        let mut a = ValueSum::from_pair(zec(), 0u64);
        a -= ValueSum::from_pair(zec(), 1);
    }
}