Skip to content

Commit

Permalink
Made Amount more generic.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Jun 29, 2023
1 parent 0d7dc07 commit 2397e1e
Show file tree
Hide file tree
Showing 16 changed files with 307 additions and 191 deletions.
26 changes: 13 additions & 13 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::Amount,
transaction::components::amount::{Amount, IAmt},
};
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: Amount,
assets: IAmt,
/// 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 Amount {
fn from(allowed_conversion: AllowedConversion) -> Amount {
impl From<AllowedConversion> for IAmt {
fn from(allowed_conversion: AllowedConversion) -> IAmt {
allowed_conversion.assets
}
}

impl From<Amount> for AllowedConversion {
impl From<IAmt> for AllowedConversion {
/// Produces an asset generator without cofactor cleared
fn from(assets: Amount) -> Self {
fn from(assets: IAmt) -> 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 @@ -199,11 +199,11 @@ mod tests {
#[test]
fn test_homomorphism() {
// Left operand
let a = Amount::from_pair(zec(), 5).unwrap()
+ Amount::from_pair(btc(), 6).unwrap()
+ Amount::from_pair(xan(), 7).unwrap();
let a = Amount::from_pair(zec(), 5i64).unwrap()
+ Amount::from_pair(btc(), 6i64).unwrap()
+ Amount::from_pair(xan(), 7i64).unwrap();
// Right operand
let b = Amount::from_pair(zec(), 2).unwrap() + Amount::from_pair(xan(), 10).unwrap();
let b = Amount::from_pair(zec(), 2i64).unwrap() + Amount::from_pair(xan(), 10i64).unwrap();
// Test homomorphism
assert_eq!(
AllowedConversion::from(a.clone() + b.clone()),
Expand All @@ -213,9 +213,9 @@ mod tests {
#[test]
fn test_serialization() {
// Make conversion
let a: AllowedConversion = (Amount::from_pair(zec(), 5).unwrap()
+ Amount::from_pair(btc(), 6).unwrap()
+ Amount::from_pair(xan(), 7).unwrap())
let a: AllowedConversion = (Amount::from_pair(zec(), 5i64).unwrap()
+ Amount::from_pair(btc(), 6i64).unwrap()
+ Amount::from_pair(xan(), 7i64).unwrap())
.into();
// Serialize conversion
let mut data = Vec::new();
Expand Down
8 changes: 4 additions & 4 deletions masp_primitives/src/sapling/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
redjubjub::{PublicKey, Signature},
Node,
},
transaction::components::{Amount, GROTH_PROOF_SIZE},
transaction::components::{IAmt, GROTH_PROOF_SIZE},
};

use super::{Diversifier, PaymentAddress, ProofGenerationKey, Rseed};
Expand Down Expand Up @@ -73,7 +73,7 @@ pub trait TxProver {
fn binding_sig(
&self,
ctx: &mut Self::SaplingProvingContext,
amount: &Amount,
amount: &IAmt,
sighash: &[u8; 32],
) -> Result<Signature, ()>;
}
Expand All @@ -92,7 +92,7 @@ pub mod mock {
redjubjub::{PublicKey, Signature},
Diversifier, Node, PaymentAddress, ProofGenerationKey, Rseed,
},
transaction::components::{Amount, GROTH_PROOF_SIZE},
transaction::components::{IAmt, GROTH_PROOF_SIZE},
};

use super::TxProver;
Expand Down Expand Up @@ -169,7 +169,7 @@ pub mod mock {
fn binding_sig(
&self,
_ctx: &mut Self::SaplingProvingContext,
_value: &Amount,
_value: &IAmt,
_sighash: &[u8; 32],
) -> Result<Signature, ()> {
Err(())
Expand Down
6 changes: 3 additions & 3 deletions masp_primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{

use self::{
components::{
amount::Amount,
amount::{Amount, IAmt},
sapling::{
self, ConvertDescriptionV5, OutputDescriptionV5, SpendDescription, SpendDescriptionV5,
},
Expand Down Expand Up @@ -269,7 +269,7 @@ impl<A: Authorization> TransactionData<A> {
}

impl<A: Authorization> TransactionData<A> {
pub fn sapling_value_balance(&self) -> Amount {
pub fn sapling_value_balance(&self) -> IAmt {
self.sapling_bundle
.as_ref()
.map_or(Amount::zero(), |b| b.value_balance.clone())
Expand Down Expand Up @@ -355,7 +355,7 @@ impl Transaction {
})
}

fn read_amount<R: Read>(mut reader: R) -> io::Result<Amount> {
fn read_amount<R: Read>(mut reader: R) -> io::Result<IAmt> {
Amount::read(&mut reader).map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidData,
Expand Down
12 changes: 6 additions & 6 deletions masp_primitives/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
sapling::{prover::TxProver, Diversifier, Node, Note, PaymentAddress},
transaction::{
components::{
amount::{Amount, BalanceError, MAX_MONEY},
amount::{Amount, BalanceError, IAmt, MAX_MONEY},
sapling::{
self,
builder::{SaplingBuilder, SaplingMetadata},
Expand All @@ -43,10 +43,10 @@ const DEFAULT_TX_EXPIRY_DELTA: u32 = 20;
pub enum Error<FeeError> {
/// Insufficient funds were provided to the transaction builder; the given
/// additional amount is required in order to construct the transaction.
InsufficientFunds(Amount),
InsufficientFunds(IAmt),
/// The transaction has inputs in excess of outputs and fees; the user must
/// add a change output.
ChangeRequired(Amount),
ChangeRequired(IAmt),
/// An error occurred in computing the fees for a transaction.
Fee(FeeError),
/// An overflow or underflow occurred when computing value balances
Expand Down Expand Up @@ -293,13 +293,13 @@ impl<P: consensus::Parameters, R: RngCore> Builder<P, R> {
}

/// Returns the sum of the transparent, Sapling, and TZE value balances.
pub fn value_balance(&self) -> Result<Amount, BalanceError> {
pub fn value_balance(&self) -> Result<IAmt, BalanceError> {
let value_balances = [
self.transparent_builder.value_balance()?,
self.sapling_builder.value_balance(),
];

Ok(value_balances.into_iter().sum::<Amount>())
Ok(value_balances.into_iter().sum::<IAmt>())
}

/// Builds a transaction from the configured spends and outputs.
Expand All @@ -326,7 +326,7 @@ impl<P: consensus::Parameters, R: RngCore> Builder<P, R> {
fn build_internal<FE>(
self,
prover: &impl TxProver,
fee: Amount,
fee: IAmt,
) -> Result<(Transaction, SaplingMetadata), Error<FE>> {
let consensus_branch_id = BranchId::for_height(&self.params, self.target_height);

Expand Down
2 changes: 1 addition & 1 deletion masp_primitives/src/transaction/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod amount;
pub mod sapling;
pub mod transparent;
pub use self::{
amount::Amount,
amount::{Amount, IAmt},
sapling::{ConvertDescription, OutputDescription, SpendDescription},
transparent::{TxIn, TxOut},
};
Expand Down
Loading

0 comments on commit 2397e1e

Please sign in to comment.