From 1a68887b118781f31960dc983d779c2bc6cce971 Mon Sep 17 00:00:00 2001 From: "Raymond E. Pasco" Date: Thu, 7 Sep 2023 14:19:32 -0400 Subject: [PATCH 1/4] i128 conversions --- masp_primitives/src/convert.rs | 14 +++++++------- .../src/transaction/components/sapling/builder.rs | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/masp_primitives/src/convert.rs b/masp_primitives/src/convert.rs index 5a345cf5..effde97c 100644 --- a/masp_primitives/src/convert.rs +++ b/masp_primitives/src/convert.rs @@ -3,7 +3,7 @@ use crate::{ pedersen_hash::{pedersen_hash, Personalization}, Node, ValueCommitment, }, - transaction::components::amount::{I32Sum, ValueSum}, + transaction::components::amount::{I128Sum, ValueSum}, }; use borsh::{BorshDeserialize, BorshSerialize}; use group::{Curve, GroupEncoding}; @@ -16,7 +16,7 @@ use std::{ #[derive(Clone, Debug, PartialEq, Eq)] pub struct AllowedConversion { /// The asset type that the note represents - assets: I32Sum, + assets: I128Sum, /// Memorize generator because it's expensive to recompute generator: jubjub::ExtendedPoint, } @@ -71,15 +71,15 @@ impl AllowedConversion { } } -impl From for I32Sum { - fn from(allowed_conversion: AllowedConversion) -> I32Sum { +impl From for I128Sum { + fn from(allowed_conversion: AllowedConversion) -> I128Sum { allowed_conversion.assets } } -impl From for AllowedConversion { +impl From for AllowedConversion { /// Produces an asset generator without cofactor cleared - fn from(assets: I32Sum) -> Self { + fn from(assets: I128Sum) -> Self { let mut asset_generator = jubjub::ExtendedPoint::identity(); for (asset, value) in assets.components() { // Compute the absolute value (failing if -i64::MAX is @@ -123,7 +123,7 @@ impl BorshDeserialize for AllowedConversion { /// computation of checking whether the asset generator corresponds to the /// deserialized amount. fn deserialize(buf: &mut &[u8]) -> borsh::maybestd::io::Result { - let assets = I32Sum::read(buf)?; + let assets = I128Sum::read(buf)?; let gen_bytes = <::Repr as BorshDeserialize>::deserialize(buf)?; let generator = Option::from(jubjub::ExtendedPoint::from_bytes(&gen_bytes)) diff --git a/masp_primitives/src/transaction/components/sapling/builder.rs b/masp_primitives/src/transaction/components/sapling/builder.rs index 9d5594ca..5f952cde 100644 --- a/masp_primitives/src/transaction/components/sapling/builder.rs +++ b/masp_primitives/src/transaction/components/sapling/builder.rs @@ -25,7 +25,7 @@ use crate::{ transaction::{ builder::Progress, components::{ - amount::{I128Sum, I32Sum, ValueSum, MAX_MONEY}, + amount::{I128Sum, ValueSum, MAX_MONEY}, sapling::{ fees, Authorization, Authorized, Bundle, ConvertDescription, GrothProofBytes, OutputDescription, SpendDescription, @@ -437,7 +437,7 @@ impl SaplingBuilder

{ self.convert_anchor = Some(merkle_path.root(node).into()) } - let allowed_amt: I32Sum = allowed.clone().into(); + let allowed_amt: I128Sum = allowed.clone().into(); self.value_balance += I128Sum::from_sum(allowed_amt) * (value as i128); self.converts.push(ConvertDescriptionInfo { From 6ad27c15e3796de4a3c059d24602f18a84a4015e Mon Sep 17 00:00:00 2001 From: joe bebel <55120843+joebebel@users.noreply.github.com> Date: Fri, 22 Sep 2023 01:16:16 -0700 Subject: [PATCH 2/4] fix tests i32->i128 --- masp_primitives/src/convert.rs | 14 +++++++------- masp_proofs/src/circuit/convert.rs | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/masp_primitives/src/convert.rs b/masp_primitives/src/convert.rs index 27e3ba65..ae047749 100644 --- a/masp_primitives/src/convert.rs +++ b/masp_primitives/src/convert.rs @@ -199,12 +199,12 @@ mod tests { #[test] fn test_homomorphism() { // Left operand - let a = ValueSum::from_pair(zec(), 5i32).unwrap() - + ValueSum::from_pair(btc(), 6i32).unwrap() - + ValueSum::from_pair(xan(), 7i32).unwrap(); + let a = ValueSum::from_pair(zec(), 5i128).unwrap() + + ValueSum::from_pair(btc(), 6i128).unwrap() + + ValueSum::from_pair(xan(), 7i128).unwrap(); // Right operand let b = - ValueSum::from_pair(zec(), 2i32).unwrap() + ValueSum::from_pair(xan(), 10i32).unwrap(); + ValueSum::from_pair(zec(), 2i128).unwrap() + ValueSum::from_pair(xan(), 10i128).unwrap(); // Test homomorphism assert_eq!( AllowedConversion::from(a.clone() + b.clone()), @@ -214,9 +214,9 @@ mod tests { #[test] fn test_serialization() { // Make conversion - let a: AllowedConversion = (ValueSum::from_pair(zec(), 5i32).unwrap() - + ValueSum::from_pair(btc(), 6i32).unwrap() - + ValueSum::from_pair(xan(), 7i32).unwrap()) + let a: AllowedConversion = (ValueSum::from_pair(zec(), 5i128).unwrap() + + ValueSum::from_pair(btc(), 6i128).unwrap() + + ValueSum::from_pair(xan(), 7i128).unwrap()) .into(); // Serialize conversion let mut data = Vec::new(); diff --git a/masp_proofs/src/circuit/convert.rs b/masp_proofs/src/circuit/convert.rs index c8bcb300..7fbcc88b 100644 --- a/masp_proofs/src/circuit/convert.rs +++ b/masp_proofs/src/circuit/convert.rs @@ -150,9 +150,9 @@ fn test_convert_circuit_with_bls12_381() { let output_asset = AssetType::new(format!("asset {}", i + 1).as_bytes()).unwrap(); let mint_asset = AssetType::new(b"reward").unwrap(); - let spend_value = -(i as i32 + 1); - let output_value = i as i32 + 1; - let mint_value = i as i32 + 1; + let spend_value = -(i as i128 + 1); + let output_value = i as i128 + 1; + let mint_value = i as i128 + 1; let allowed_conversion: AllowedConversion = (ValueSum::from_pair(spend_asset, spend_value) .unwrap() From d2c99e49408fe0ec02a9737046f11fda2c558f08 Mon Sep 17 00:00:00 2001 From: joe bebel <55120843+joebebel@users.noreply.github.com> Date: Fri, 22 Sep 2023 01:18:10 -0700 Subject: [PATCH 3/4] cargo fmt --- masp_primitives/src/convert.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/masp_primitives/src/convert.rs b/masp_primitives/src/convert.rs index ae047749..b5aa63ac 100644 --- a/masp_primitives/src/convert.rs +++ b/masp_primitives/src/convert.rs @@ -203,8 +203,8 @@ mod tests { + ValueSum::from_pair(btc(), 6i128).unwrap() + ValueSum::from_pair(xan(), 7i128).unwrap(); // Right operand - let b = - ValueSum::from_pair(zec(), 2i128).unwrap() + ValueSum::from_pair(xan(), 10i128).unwrap(); + let b = ValueSum::from_pair(zec(), 2i128).unwrap() + + ValueSum::from_pair(xan(), 10i128).unwrap(); // Test homomorphism assert_eq!( AllowedConversion::from(a.clone() + b.clone()), From 397f693099bbdc48031e390f8b30a870e66761a9 Mon Sep 17 00:00:00 2001 From: joe bebel <55120843+joebebel@users.noreply.github.com> Date: Fri, 22 Sep 2023 01:21:48 -0700 Subject: [PATCH 4/4] update bench --- masp_proofs/benches/convert.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/masp_proofs/benches/convert.rs b/masp_proofs/benches/convert.rs index 60fbbc74..0ac849fe 100644 --- a/masp_proofs/benches/convert.rs +++ b/masp_proofs/benches/convert.rs @@ -34,9 +34,9 @@ fn criterion_benchmark(c: &mut Criterion) { let output_asset = AssetType::new(format!("asset {}", i + 1).as_bytes()).unwrap(); let mint_asset = AssetType::new(b"reward").unwrap(); - let spend_value = -(i as i32 + 1); - let output_value = i as i32 + 1; - let mint_value = i as i32 + 1; + let spend_value = -(i as i128 + 1); + let output_value = i as i128 + 1; + let mint_value = i as i128 + 1; let allowed_conversion: AllowedConversion = (ValueSum::from_pair(spend_asset, spend_value) .unwrap()