Skip to content

Commit

Permalink
Now use i32s in AllowedConversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Jul 27, 2023
1 parent 55e24dd commit 438369a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 22 deletions.
28 changes: 14 additions & 14 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::{I64Sum, ValueSum},
transaction::components::amount::{I32Sum, 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: I64Sum,
assets: I32Sum,
/// 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 I64Sum {
fn from(allowed_conversion: AllowedConversion) -> I64Sum {
impl From<AllowedConversion> for I32Sum {
fn from(allowed_conversion: AllowedConversion) -> I32Sum {
allowed_conversion.assets
}
}

impl From<I64Sum> for AllowedConversion {
impl From<I32Sum> for AllowedConversion {
/// Produces an asset generator without cofactor cleared
fn from(assets: I64Sum) -> Self {
fn from(assets: I32Sum) -> 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(buf: &mut &[u8]) -> borsh::maybestd::io::Result<Self> {
let assets = I64Sum::read(buf)?;
let assets = I32Sum::read(buf)?;
let gen_bytes =
<<jubjub::ExtendedPoint as GroupEncoding>::Repr as BorshDeserialize>::deserialize(buf)?;
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(), 5i64).unwrap()
+ ValueSum::from_pair(btc(), 6i64).unwrap()
+ ValueSum::from_pair(xan(), 7i64).unwrap();
let a = ValueSum::from_pair(zec(), 5i32).unwrap()
+ ValueSum::from_pair(btc(), 6i32).unwrap()
+ ValueSum::from_pair(xan(), 7i32).unwrap();
// Right operand
let b =
ValueSum::from_pair(zec(), 2i64).unwrap() + ValueSum::from_pair(xan(), 10i64).unwrap();
ValueSum::from_pair(zec(), 2i32).unwrap() + ValueSum::from_pair(xan(), 10i32).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(), 5i64).unwrap()
+ ValueSum::from_pair(btc(), 6i64).unwrap()
+ ValueSum::from_pair(xan(), 7i64).unwrap())
let a: AllowedConversion = (ValueSum::from_pair(zec(), 5i32).unwrap()
+ ValueSum::from_pair(btc(), 6i32).unwrap()
+ ValueSum::from_pair(xan(), 7i32).unwrap())
.into();
// Serialize conversion
let mut data = Vec::new();
Expand Down
35 changes: 35 additions & 0 deletions masp_primitives/src/transaction/components/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,41 @@ where
}
}

impl ValueSum<AssetType, i32> {
/// Deserialize an Amount 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 mut atype = [0; 32];
let mut value = [0; 4];
reader.read_exact(&mut atype)?;
reader.read_exact(&mut value)?;
let atype = AssetType::from_identifier(&atype).ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid asset type")
})?;
Ok((atype, i32::from_le_bytes(value)))
})?;
let mut ret = Self::zero();
for (atype, amt) in vec {
ret += Self::from_pair(atype, amt).map_err(|_| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "amount out of range")
})?;
}
Ok(ret)
}

/// Serialize an Amount 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 Amount object from a list of amounts denominated by
/// different assets
Expand Down
10 changes: 5 additions & 5 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::{FromNt, I128Sum, I64Sum, ValueSum, MAX_MONEY},
amount::{FromNt, I128Sum, I32Sum, ValueSum, MAX_MONEY},
sapling::{
fees, Authorization, Authorized, Bundle, ConvertDescription, GrothProofBytes,
OutputDescription, SpendDescription,
Expand Down Expand Up @@ -401,8 +401,8 @@ impl<P: consensus::Parameters> SaplingBuilder<P> {

let alpha = jubjub::Fr::random(&mut rng);

self.value_balance +=
ValueSum::from_pair(note.asset_type, note.value.into()).map_err(|_| Error::InvalidAmount)?;
self.value_balance += ValueSum::from_pair(note.asset_type, note.value.into())
.map_err(|_| Error::InvalidAmount)?;

self.spends.push(SpendDescriptionInfo {
extsk,
Expand Down Expand Up @@ -437,8 +437,8 @@ impl<P: consensus::Parameters> SaplingBuilder<P> {
self.convert_anchor = Some(merkle_path.root(node).into())
}

let allowed_amt: I64Sum = allowed.clone().into();
self.value_balance += I128Sum::from(FromNt(allowed_amt * (value as i64)));
let allowed_amt: I32Sum = allowed.clone().into();
self.value_balance += I128Sum::from(FromNt(allowed_amt)) * (value as i128);

self.converts.push(ConvertDescriptionInfo {
allowed,
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 i64 + 1);
let output_value = i as i64 + 1;
let mint_value = i as i64 + 1;
let spend_value = -(i as i32 + 1);
let output_value = i as i32 + 1;
let mint_value = i as i32 + 1;

let allowed_conversion: AllowedConversion = (ValueSum::from_pair(spend_asset, spend_value)
.unwrap()
Expand Down

0 comments on commit 438369a

Please sign in to comment.