diff --git a/Cargo.lock b/Cargo.lock index eb6072d4..a295ddcc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -436,7 +436,7 @@ dependencies = [ "criterion-plot", "itertools 0.10.5", "lazy_static", - "num-traits", + "num-traits 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", "oorandom", "plotters", "rayon", @@ -617,7 +617,7 @@ dependencies = [ "libm", "num-bigint", "num-integer", - "num-traits", + "num-traits 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -920,7 +920,8 @@ dependencies = [ "masp_note_encryption", "memuse", "nonempty", - "num-traits", + "num-traits 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.19 (git+https://github.com/heliaxdev/num-traits?rev=e3e712fc4ecbf9d95399b0b98ee9f3d6b9973e38)", "proptest", "rand", "rand_core", @@ -1021,7 +1022,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c165a9ab64cf766f73521c0dd2cfdff64f488b8f0b3e621face3462d3db536d7" dependencies = [ "num-integer", - "num-traits", + "num-traits 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1040,7 +1041,7 @@ version = "0.1.46" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" dependencies = [ - "num-traits", + "num-traits 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1053,6 +1054,14 @@ dependencies = [ "libm", ] +[[package]] +name = "num-traits" +version = "0.2.19" +source = "git+https://github.com/heliaxdev/num-traits?rev=e3e712fc4ecbf9d95399b0b98ee9f3d6b9973e38#e3e712fc4ecbf9d95399b0b98ee9f3d6b9973e38" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -1174,7 +1183,7 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45" dependencies = [ - "num-traits", + "num-traits 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", "plotters-backend", "plotters-svg", "wasm-bindgen", @@ -1286,7 +1295,7 @@ dependencies = [ "bit-vec", "bitflags 2.5.0", "lazy_static", - "num-traits", + "num-traits 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)", "rand", "rand_chacha", "rand_xorshift", diff --git a/masp_primitives/Cargo.toml b/masp_primitives/Cargo.toml index e78dbf41..186e380a 100644 --- a/masp_primitives/Cargo.toml +++ b/masp_primitives/Cargo.toml @@ -38,6 +38,7 @@ memuse = "0.2.1" # - Checked arithmetic num-traits = "0.2.14" +num-traits-decoupled = { package = "num-traits", version = "0.2.19", git = "https://github.com/heliaxdev/num-traits", rev = "e3e712fc4ecbf9d95399b0b98ee9f3d6b9973e38" } # - Secret management subtle = "2.2.3" diff --git a/masp_primitives/src/transaction/components/amount.rs b/masp_primitives/src/transaction/components/amount.rs index cf9888ae..f35daf92 100644 --- a/masp_primitives/src/transaction/components/amount.rs +++ b/masp_primitives/src/transaction/components/amount.rs @@ -4,7 +4,7 @@ use borsh::schema::Fields; use borsh::schema::{Declaration, Definition}; use borsh::BorshSchema; use borsh::{BorshDeserialize, BorshSerialize}; -use num_traits::{CheckedAdd, CheckedMul, CheckedNeg, CheckedSub, One}; +use num_traits_decoupled::{CheckedAdd, CheckedMul, CheckedNeg, CheckedSub, One}; use std::cmp::Ordering; use std::collections::btree_map::Keys; use std::collections::btree_map::{IntoIter, Iter}; @@ -422,7 +422,7 @@ where + Copy + Default + PartialOrd - + CheckedMul, + + CheckedMul, { fn mul_assign(&mut self, rhs: Value) { *self = self.clone() * rhs; @@ -440,18 +440,19 @@ where + Default + PartialOrd + CheckedMul, + ::Output : Default + BorshSerialize + BorshDeserialize + Eq, { - type Output = ValueSum; + type Output = ValueSum::Output>; fn mul(self, rhs: Value) -> Self::Output { let mut comps = BTreeMap::new(); for (atype, amount) in self.0.iter() { comps.insert( atype.clone(), - amount.checked_mul(&rhs).expect("overflow detected"), + amount.checked_mul(rhs).expect("overflow detected"), ); } - comps.retain(|_, v| *v != Value::default()); + comps.retain(|_, v| *v != ::Output::default()); ValueSum(comps) } } @@ -466,7 +467,7 @@ where + Copy + Default + PartialOrd - + CheckedAdd, + + CheckedAdd, { fn add_assign(&mut self, rhs: &ValueSum) { *self = self.clone() + rhs; @@ -483,7 +484,7 @@ where + Copy + Default + PartialOrd - + CheckedAdd, + + CheckedAdd, { fn add_assign(&mut self, rhs: ValueSum) { *self += &rhs @@ -500,7 +501,7 @@ where + Copy + Default + PartialOrd - + CheckedAdd, + + CheckedAdd, { type Output = ValueSum; @@ -519,7 +520,7 @@ where + Copy + Default + PartialOrd - + CheckedAdd, + + CheckedAdd, { type Output = ValueSum; @@ -528,7 +529,7 @@ where } } -impl CheckedAdd for ValueSum +impl CheckedAdd for &ValueSum where Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone, Value: BorshSerialize @@ -538,12 +539,14 @@ where + Copy + Default + PartialOrd - + CheckedAdd, + + CheckedAdd, { - fn checked_add(&self, v: &Self) -> Option { + type Output = ValueSum; + + fn checked_add(self, v: Self) -> Option { let mut comps = self.0.clone(); for (atype, amount) in v.components() { - comps.insert(atype.clone(), self.get(atype).checked_add(amount)?); + comps.insert(atype.clone(), self.get(atype).checked_add(*amount)?); } comps.retain(|_, v| *v != Value::default()); Some(ValueSum(comps)) @@ -560,7 +563,7 @@ where + Copy + Default + PartialOrd - + CheckedSub, + + CheckedSub, { fn sub_assign(&mut self, rhs: &ValueSum) { *self = self.clone() - rhs @@ -577,7 +580,7 @@ where + Copy + Default + PartialOrd - + CheckedSub, + + CheckedSub, { fn sub_assign(&mut self, rhs: ValueSum) { *self -= &rhs @@ -595,8 +598,9 @@ where + Default + PartialOrd + CheckedNeg, + ::Output: BorshSerialize + BorshDeserialize + Eq + Default, { - type Output = ValueSum; + type Output = ValueSum::Output>; fn neg(mut self) -> Self::Output { let mut comps = BTreeMap::new(); @@ -606,7 +610,7 @@ where amount.checked_neg().expect("overflow detected"), ); } - comps.retain(|_, v| *v != Value::default()); + comps.retain(|_, v| *v != ::Output::default()); ValueSum(comps) } } @@ -614,7 +618,7 @@ where impl Sub<&ValueSum> for ValueSum where Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone, - Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Default + CheckedSub, + Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Default + CheckedSub, { type Output = ValueSum; @@ -626,7 +630,7 @@ where impl Sub> for ValueSum where Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone, - Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Default + CheckedSub, + Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Default + CheckedSub, { type Output = ValueSum; @@ -635,15 +639,17 @@ where } } -impl CheckedSub for ValueSum +impl CheckedSub for &ValueSum where Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone, - Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Default + CheckedSub, + Value: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Default + CheckedSub, { - fn checked_sub(&self, v: &Self) -> Option { + type Output = ValueSum; + + fn checked_sub(self, v: Self) -> Option { let mut comps = self.0.clone(); for (atype, amount) in v.components() { - comps.insert(atype.clone(), self.get(atype).checked_sub(amount)?); + comps.insert(atype.clone(), self.get(atype).checked_sub(*amount)?); } comps.retain(|_, v| *v != Value::default()); Some(ValueSum(comps))