Skip to content

Commit

Permalink
Merge pull request #68 from anoma/ray/i128-conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
joe committed Sep 22, 2023
2 parents 449a729 + 397f693 commit 77e0096
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
30 changes: 15 additions & 15 deletions masp_primitives/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
}
Expand Down Expand Up @@ -71,15 +71,15 @@ impl AllowedConversion {
}
}

impl From<AllowedConversion> for I32Sum {
fn from(allowed_conversion: AllowedConversion) -> I32Sum {
impl From<AllowedConversion> for I128Sum {
fn from(allowed_conversion: AllowedConversion) -> I128Sum {
allowed_conversion.assets
}
}

impl From<I32Sum> for AllowedConversion {
impl From<I128Sum> 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
Expand Down Expand Up @@ -123,7 +123,7 @@ impl BorshDeserialize for AllowedConversion {
/// computation of checking whether the asset generator corresponds to the
/// deserialized amount.
fn deserialize_reader<R: io::Read>(reader: &mut R) -> io::Result<Self> {
let assets = I32Sum::read(reader)?;
let assets = I128Sum::read(reader)?;
let gen_bytes =
<<jubjub::ExtendedPoint as GroupEncoding>::Repr as BorshDeserialize>::deserialize_reader(reader)?;
let generator = Option::from(jubjub::ExtendedPoint::from_bytes(&gen_bytes))
Expand Down Expand Up @@ -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();
let b = ValueSum::from_pair(zec(), 2i128).unwrap()
+ ValueSum::from_pair(xan(), 10i128).unwrap();
// Test homomorphism
assert_eq!(
AllowedConversion::from(a.clone() + b.clone()),
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions masp_primitives/src/transaction/components/sapling/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -438,7 +438,7 @@ impl<P: consensus::Parameters> SaplingBuilder<P> {
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 {
Expand Down
6 changes: 3 additions & 3 deletions masp_proofs/benches/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions masp_proofs/src/circuit/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 77e0096

Please sign in to comment.