From 3f474f1919f5d1d28ea20bf8d0d540cb0b991c10 Mon Sep 17 00:00:00 2001 From: benesjan Date: Fri, 10 Jan 2025 00:56:37 +0000 Subject: [PATCH] refactor: using U128 in contract function args --- .../contracts/amm_contract/src/main.nr | 181 +++++++----------- .../app_subscription_contract/src/main.nr | 4 +- .../contracts/claim_contract/src/main.nr | 7 +- .../crowdfunding_contract/src/main.nr | 15 +- .../contracts/escrow_contract/src/main.nr | 2 +- .../contracts/fpc_contract/src/main.nr | 22 ++- .../contracts/lending_contract/src/main.nr | 78 ++++---- .../lending_contract/src/position.nr | 13 +- .../contracts/token_contract/src/main.nr | 122 ++++++------ .../token_contract/src/test/burn_private.nr | 14 +- .../token_contract/src/test/burn_public.nr | 12 +- .../token_contract/src/test/mint_to_public.nr | 16 +- .../token_contract/src/test/refunds.nr | 6 +- .../token_contract/src/test/transfer.nr | 8 +- .../src/test/transfer_in_private.nr | 10 +- .../src/test/transfer_in_public.nr | 16 +- .../src/test/transfer_to_public.nr | 14 +- .../token_contract/src/test/utils.nr | 25 +-- .../crates/types/src/type_serialization.nr | 7 +- 19 files changed, 266 insertions(+), 306 deletions(-) diff --git a/noir-projects/noir-contracts/contracts/amm_contract/src/main.nr b/noir-projects/noir-contracts/contracts/amm_contract/src/main.nr index a70d9ebeada..efcacf0705d 100644 --- a/noir-projects/noir-contracts/contracts/amm_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/amm_contract/src/main.nr @@ -72,21 +72,24 @@ contract AMM { /// The identity of the liquidity provider is not revealed, but the action and amounts are. #[private] fn add_liquidity( - amount0_max: Field, - amount1_max: Field, - amount0_min: Field, - amount1_min: Field, + amount0_max: U128, + amount1_max: U128, + amount0_min: U128, + amount1_min: U128, nonce: Field, ) { assert( - amount0_min.lt(amount0_max) | (amount0_min == amount0_max), + (amount0_min < amount0_max) | (amount0_min == amount0_max), "INCORRECT_TOKEN0_LIMITS", ); assert( - amount1_min.lt(amount1_max) | (amount1_min == amount1_max), + (amount1_min < amount1_max) | (amount1_min == amount1_max), "INCORRECT_TOKEN1_LIMITS", ); - assert(0.lt(amount0_max) & 0.lt(amount1_max), "INSUFFICIENT_INPUT_AMOUNTS"); + assert( + (U128::zero() < amount0_max) & (U128::zero() < amount1_max), + "INSUFFICIENT_INPUT_AMOUNTS", + ); let config = storage.config.read(); @@ -142,17 +145,11 @@ contract AMM { refund_token0_hiding_point_slot: Field, refund_token1_hiding_point_slot: Field, liquidity_hiding_point_slot: Field, - amount0_max: Field, - amount1_max: Field, - amount0_min: Field, - amount1_min: Field, + amount0_max: U128, + amount1_max: U128, + amount0_min: U128, + amount1_min: U128, ) { - // TODO(#8271): Type the args as U128 and nuke these ugly casts - let amount0_max = U128::from_integer(amount0_max); - let amount1_max = U128::from_integer(amount1_max); - let amount0_min = U128::from_integer(amount0_min); - let amount1_min = U128::from_integer(amount1_min); - let token0 = Token::at(config.token0); let token1 = Token::at(config.token1); let liquidity_token = Token::at(config.liquidity_token); @@ -160,14 +157,12 @@ contract AMM { // We read the current AMM balance of both tokens. Note that by the time this function is called the token // transfers have already been completed (since those calls were enqueued before this call), and so we need to // substract the transfer amount to get the pre-deposit balance. - let balance0_plus_amount0_max = U128::from_integer(token0 - .balance_of_public(context.this_address()) - .view(&mut context)); + let balance0_plus_amount0_max = + token0.balance_of_public(context.this_address()).view(&mut context); let balance0 = balance0_plus_amount0_max - amount0_max; - let balance1_plus_amount1_max = U128::from_integer(token1 - .balance_of_public(context.this_address()) - .view(&mut context)); + let balance1_plus_amount1_max = + token1.balance_of_public(context.this_address()).view(&mut context); let balance1 = balance1_plus_amount1_max - amount1_max; // With the current balances known, we can calculate the token amounts to the pool, respecting the user's @@ -189,24 +184,18 @@ contract AMM { // simply stay in public storage and not be completed, but this is not an issue. if (refund_amount_token0 > U128::zero()) { token0 - .finalize_transfer_to_private( - refund_amount_token0.to_integer(), - refund_token0_hiding_point_slot, - ) + .finalize_transfer_to_private(refund_amount_token0, refund_token0_hiding_point_slot) .call(&mut context); } if (refund_amount_token1 > U128::zero()) { token1 - .finalize_transfer_to_private( - refund_amount_token1.to_integer(), - refund_token1_hiding_point_slot, - ) + .finalize_transfer_to_private(refund_amount_token1, refund_token1_hiding_point_slot) .call(&mut context); } // With the deposit amounts known, we can compute the number of liquidity tokens to mint and finalize the // depositor's partial note. - let total_supply = U128::from_integer(liquidity_token.total_supply().view(&mut context)); + let total_supply = liquidity_token.total_supply().view(&mut context); let liquidity_amount = if total_supply != U128::zero() { // The liquidity token supply increases by the same ratio as the balances. In case one of the token balances // increased with a ratio different from the other one, we simply take the smallest value. @@ -223,16 +212,16 @@ contract AMM { // As part of initialization, we mint some tokens to the zero address to 'lock' them (i.e. make them // impossible to redeem), guaranteeing total supply will never be zero again. - liquidity_token - .mint_to_public(AztecAddress::zero(), MINIMUM_LIQUIDITY.to_integer()) - .call(&mut context); + liquidity_token.mint_to_public(AztecAddress::zero(), MINIMUM_LIQUIDITY).call( + &mut context, + ); INITIAL_LIQUIDITY }; assert(liquidity_amount > U128::zero(), "INSUFFICIENT_LIQUIDITY_MINTED"); liquidity_token - .finalize_mint_to_private(liquidity_amount.to_integer(), liquidity_hiding_point_slot) + .finalize_mint_to_private(liquidity_amount, liquidity_hiding_point_slot) .call(&mut context); } @@ -243,7 +232,7 @@ contract AMM { /// /// The identity of the liquidity provider is not revealed, but the action and amounts are. #[private] - fn remove_liquidity(liquidity: Field, amount0_min: Field, amount1_min: Field, nonce: Field) { + fn remove_liquidity(liquidity: U128, amount0_min: U128, amount1_min: U128, nonce: Field) { let config = storage.config.read(); let liquidity_token = Token::at(config.liquidity_token); @@ -286,30 +275,21 @@ contract AMM { #[internal] fn _remove_liquidity( config: Config, // We could read this in public, but it's cheaper to receive from private - liquidity: Field, + liquidity: U128, token0_hiding_point_slot: Field, token1_hiding_point_slot: Field, - amount0_min: Field, - amount1_min: Field, + amount0_min: U128, + amount1_min: U128, ) { - // TODO(#8271): Type the args as U128 and nuke these ugly casts - let liquidity = U128::from_integer(liquidity); - let amount0_min = U128::from_integer(amount0_min); - let amount1_min = U128::from_integer(amount1_min); - let token0 = Token::at(config.token0); let token1 = Token::at(config.token1); let liquidity_token = Token::at(config.liquidity_token); // We need the current balance of both tokens as well as the liquidity token total supply in order to compute // the amounts to send the user. - let balance0 = U128::from_integer(token0.balance_of_public(context.this_address()).view( - &mut context, - )); - let balance1 = U128::from_integer(token1.balance_of_public(context.this_address()).view( - &mut context, - )); - let total_supply = U128::from_integer(liquidity_token.total_supply().view(&mut context)); + let balance0 = token0.balance_of_public(context.this_address()).view(&mut context); + let balance1 = token1.balance_of_public(context.this_address()).view(&mut context); + let total_supply = liquidity_token.total_supply().view(&mut context); // We calculate the amounts of token0 and token1 the user is entitled to based on the amount of liquidity they // are removing, and check that they are above the minimum amounts they requested. @@ -319,15 +299,9 @@ contract AMM { // We can now burn the liquidity tokens that had been privately transferred into the AMM, as well as complete // both partial notes. - liquidity_token.burn_public(context.this_address(), liquidity.to_integer(), 0).call( - &mut context, - ); - token0.finalize_transfer_to_private(amount0.to_integer(), token0_hiding_point_slot).call( - &mut context, - ); - token1.finalize_transfer_to_private(amount1.to_integer(), token1_hiding_point_slot).call( - &mut context, - ); + liquidity_token.burn_public(context.this_address(), liquidity, 0).call(&mut context); + token0.finalize_transfer_to_private(amount0, token0_hiding_point_slot).call(&mut context); + token1.finalize_transfer_to_private(amount1, token1_hiding_point_slot).call(&mut context); } /// Privately swaps `amount_in` `token_in` tokens for at least `amount_out_mint` `token_out` tokens with the pool. @@ -339,8 +313,8 @@ contract AMM { fn swap_exact_tokens_for_tokens( token_in: AztecAddress, token_out: AztecAddress, - amount_in: Field, - amount_out_min: Field, + amount_in: U128, + amount_out_min: U128, nonce: Field, ) { let config = storage.config.read(); @@ -377,32 +351,26 @@ contract AMM { fn _swap_exact_tokens_for_tokens( token_in: AztecAddress, token_out: AztecAddress, - amount_in: Field, - amount_out_min: Field, + amount_in: U128, + amount_out_min: U128, token_out_hiding_point_slot: Field, ) { - // TODO(#8271): Type the args as U128 and nuke these ugly casts - let amount_in = U128::from_integer(amount_in); - let amount_out_min = U128::from_integer(amount_out_min); - // In order to compute the amount to swap we need the live token balances. Note that at this state the token in // transfer has already been completed as that function call was enqueued before this one. We therefore need to // subtract the amount in to get the pre-swap balances. - let balance_in_plus_amount_in = U128::from_integer(Token::at(token_in) - .balance_of_public(context.this_address()) - .view(&mut context)); + let balance_in_plus_amount_in = + Token::at(token_in).balance_of_public(context.this_address()).view(&mut context); let balance_in = balance_in_plus_amount_in - amount_in; - let balance_out = U128::from_integer(Token::at(token_out) - .balance_of_public(context.this_address()) - .view(&mut context)); + let balance_out = + Token::at(token_out).balance_of_public(context.this_address()).view(&mut context); // We can now compute the number of tokens to transfer and complete the partial note. let amount_out = get_amount_out(amount_in, balance_in, balance_out); assert(amount_out >= amount_out_min, "INSUFFICIENT_OUTPUT_AMOUNT"); Token::at(token_out) - .finalize_transfer_to_private(amount_out.to_integer(), token_out_hiding_point_slot) + .finalize_transfer_to_private(amount_out, token_out_hiding_point_slot) .call(&mut context); } @@ -415,8 +383,8 @@ contract AMM { fn swap_tokens_for_exact_tokens( token_in: AztecAddress, token_out: AztecAddress, - amount_out: Field, - amount_in_max: Field, + amount_out: U128, + amount_in_max: U128, nonce: Field, ) { let config = storage.config.read(); @@ -431,7 +399,7 @@ contract AMM { // public execution as it depends on the live balances. We therefore transfer the full maximum amount and // prepare partial notes both for the token out and the refund. // Technically the token out note does not need to be partial, since we do know the amount out, but we do want - // to wait until the swap has been completed before commiting the note to the tree to avoid it being spent too + // to wait until the swap has been completed before committing the note to the tree to avoid it being spent too // early. // TODO(#10286): consider merging these two calls Token::at(token_in) @@ -461,26 +429,20 @@ contract AMM { fn _swap_tokens_for_exact_tokens( token_in: AztecAddress, token_out: AztecAddress, - amount_in_max: Field, - amount_out: Field, + amount_in_max: U128, + amount_out: U128, change_token_in_hiding_point_slot: Field, token_out_hiding_point_slot: Field, ) { - // TODO(#8271): Type the args as U128 and nuke these ugly casts - let amount_out = U128::from_integer(amount_out); - let amount_in_max = U128::from_integer(amount_in_max); - // In order to compute the amount to swap we need the live token balances. Note that at this state the token in // transfer has already been completed as that function call was enqueued before this one. We therefore need to // subtract the amount in to get the pre-swap balances. - let balance_in_plus_amount_in_max = U128::from_integer(Token::at(token_in) - .balance_of_public(context.this_address()) - .view(&mut context)); + let balance_in_plus_amount_in_max = + Token::at(token_in).balance_of_public(context.this_address()).view(&mut context); let balance_in = balance_in_plus_amount_in_max - amount_in_max; - let balance_out = U128::from_integer(Token::at(token_out) - .balance_of_public(context.this_address()) - .view(&mut context)); + let balance_out = + Token::at(token_out).balance_of_public(context.this_address()).view(&mut context); // We can now compute the number of tokens we need to receive and complete the partial note with the change. let amount_in = get_amount_in(amount_out, balance_in, balance_out); @@ -489,43 +451,32 @@ contract AMM { let change = amount_in_max - amount_in; if (change > U128::zero()) { Token::at(token_in) - .finalize_transfer_to_private(change.to_integer(), change_token_in_hiding_point_slot - ) - .call(&mut context); + .finalize_transfer_to_private(change, change_token_in_hiding_point_slot) + .call(&mut context); } // Note again that we already knew the amount out, but for consistency we want to only commit this note once // all other steps have been performed. Token::at(token_out) - .finalize_transfer_to_private(amount_out.to_integer(), token_out_hiding_point_slot) + .finalize_transfer_to_private(amount_out, token_out_hiding_point_slot) .call(&mut context); } unconstrained fn get_amount_out_for_exact_in( - balance_in: Field, - balance_out: Field, - amount_in: Field, - ) -> Field { + balance_in: U128, + balance_out: U128, + amount_in: U128, + ) -> U128 { // Ideally we'd call the token contract in order to read the current balance, but we can't due to #7524. - get_amount_out( - U128::from_integer(amount_in), - U128::from_integer(balance_in), - U128::from_integer(balance_out), - ) - .to_integer() + get_amount_out(amount_in, balance_in, balance_out) } unconstrained fn get_amount_in_for_exact_out( - balance_in: Field, - balance_out: Field, - amount_out: Field, - ) -> Field { + balance_in: U128, + balance_out: U128, + amount_out: U128, + ) -> U128 { // Ideally we'd call the token contract in order to read the current balance, but we can't due to #7524. - get_amount_in( - U128::from_integer(amount_out), - U128::from_integer(balance_in), - U128::from_integer(balance_out), - ) - .to_integer() + get_amount_in(amount_out, balance_in, balance_out) } } diff --git a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/main.nr b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/main.nr index 8428117427a..f5d8d03bfb3 100644 --- a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/main.nr @@ -23,7 +23,7 @@ contract AppSubscription { target_address: PublicImmutable, subscription_token_address: PublicImmutable, subscription_recipient_address: PublicImmutable, - subscription_price: PublicImmutable, + subscription_price: PublicImmutable, subscriptions: Map, Context>, fee_juice_limit_per_tx: PublicImmutable, } @@ -68,7 +68,7 @@ contract AppSubscription { target_address: AztecAddress, subscription_recipient_address: AztecAddress, subscription_token_address: AztecAddress, - subscription_price: Field, + subscription_price: U128, fee_juice_limit_per_tx: Field, ) { storage.target_address.initialize(target_address); diff --git a/noir-projects/noir-contracts/contracts/claim_contract/src/main.nr b/noir-projects/noir-contracts/contracts/claim_contract/src/main.nr index e2a9e487756..50feeca54f3 100644 --- a/noir-projects/noir-contracts/contracts/claim_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/claim_contract/src/main.nr @@ -51,8 +51,9 @@ contract Claim { context.push_nullifier(nullifier); // 4) Finally we mint the reward token to the sender of the transaction - Token::at(storage.reward_token.read()).mint_to_public(recipient, proof_note.value).enqueue( - &mut context, - ); + // TODO(benesjan): Instead of ValueNote use UintNote to avoid the conversion to U128 below. + Token::at(storage.reward_token.read()) + .mint_to_public(recipient, U128::from_integer(proof_note.value)) + .enqueue(&mut context); } } diff --git a/noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr b/noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr index 7644487cba7..a165d1ed4d3 100644 --- a/noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr @@ -30,7 +30,7 @@ contract Crowdfunding { #[event] struct WithdrawalProcessed { who: AztecAddress, - amount: u64, + amount: U128, } // docs:start:storage @@ -65,7 +65,7 @@ contract Crowdfunding { // docs:start:donate #[private] - fn donate(amount: u64) { + fn donate(amount: U128) { // 1) Check that the deadline has not passed --> we do that via the router contract to conceal which contract // is performing the check. // docs:start:call-check-deadline @@ -76,13 +76,14 @@ contract Crowdfunding { // 2) Transfer the donation tokens from donor to this contract let donor = context.msg_sender(); Token::at(storage.donation_token.read()) - .transfer_in_private(donor, context.this_address(), amount as Field, 0) + .transfer_in_private(donor, context.this_address(), amount, 0) .call(&mut context); // docs:end:do-transfer // 3) Create a value note for the donor so that he can later on claim a rewards token in the Claim // contract by proving that the hash of this note exists in the note hash tree. // docs:start:valuenote_new - let mut note = ValueNote::new(amount as Field, donor); + // TODO(benesjan): Instead of ValueNote use UintNote to avoid the conversion to a Field below. + let mut note = ValueNote::new(amount.to_field(), donor); // docs:end:valuenote_new storage.donation_receipts.insert(&mut note).emit(encode_and_encrypt_note( @@ -96,13 +97,13 @@ contract Crowdfunding { // docs:start:operator-withdrawals // Withdraws balance to the operator. Requires that msg_sender() is the operator. #[private] - fn withdraw(amount: u64) { + fn withdraw(amount: U128) { // 1) Check that msg_sender() is the operator let operator_address = storage.operator.read(); assert(context.msg_sender() == operator_address, "Not an operator"); // 2) Transfer the donation tokens from this contract to the operator - Token::at(storage.donation_token.read()).transfer(operator_address, amount as Field).call( + Token::at(storage.donation_token.read()).transfer(operator_address, amount).call( &mut context, ); // 3) Emit an unencrypted event so that anyone can audit how much the operator has withdrawn @@ -114,7 +115,7 @@ contract Crowdfunding { #[public] #[internal] - fn _publish_donation_receipts(amount: u64, to: AztecAddress) { + fn _publish_donation_receipts(amount: U128, to: AztecAddress) { WithdrawalProcessed { amount, who: to }.emit(encode_event(&mut context)); } } diff --git a/noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr b/noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr index f5683b01ed0..59a6dd5a274 100644 --- a/noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr @@ -35,7 +35,7 @@ contract Escrow { // Withdraws balance. Requires that msg.sender is the owner. #[private] - fn withdraw(token: AztecAddress, amount: Field, recipient: AztecAddress) { + fn withdraw(token: AztecAddress, amount: U128, recipient: AztecAddress) { let sender = context.msg_sender(); let note = storage.owner.get_note(); diff --git a/noir-projects/noir-contracts/contracts/fpc_contract/src/main.nr b/noir-projects/noir-contracts/contracts/fpc_contract/src/main.nr index d61d242a1d7..480358c2310 100644 --- a/noir-projects/noir-contracts/contracts/fpc_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/fpc_contract/src/main.nr @@ -79,7 +79,7 @@ contract FPC { /// - which FPC has been used to make the payment; /// - the asset which was used to make the payment. #[private] - fn fee_entrypoint_private(max_fee: Field, nonce: Field) { + fn fee_entrypoint_private(max_fee: U128, nonce: Field) { // TODO(PR #8022): Once PublicImmutable performs only 1 merkle proof here, we'll save ~4k gates let config = storage.config.read(); @@ -110,7 +110,7 @@ contract FPC { /// Protocol-enshrined fee-payment phase: /// 4. The protocol deducts the actual fee denominated in fee juice from the FPC's balance. #[private] - fn fee_entrypoint_public(max_fee: Field, nonce: Field) { + fn fee_entrypoint_public(max_fee: U128, nonce: Field) { // TODO(PR #8022): Once PublicImmutable performs only 1 merkle proof here, we'll save ~4k gates let config = storage.config.read(); @@ -124,10 +124,18 @@ contract FPC { context.set_as_fee_payer(); // TODO(#6277) for improving interface: // FPC::at(context.this_address()).pay_refund(...).set_public_teardown_function(&mut context); + let max_fee_serialized = max_fee.serialize(); context.set_public_teardown_function( context.this_address(), - comptime { FunctionSelector::from_signature("pay_refund((Field),Field,(Field))") }, - [context.msg_sender().to_field(), max_fee, config.accepted_asset.to_field()], + comptime { + FunctionSelector::from_signature("pay_refund((Field),(Field,Field),(Field))") + }, + [ + context.msg_sender().to_field(), + max_fee_serialized[0], + max_fee_serialized[1], + config.accepted_asset.to_field(), + ], ); } @@ -136,9 +144,9 @@ contract FPC { /// to avoid the need for another read from public storage. #[public] #[internal] - fn pay_refund(refund_recipient: AztecAddress, max_fee: Field, accepted_asset: AztecAddress) { - let actual_fee = context.transaction_fee(); - assert(!max_fee.lt(actual_fee), "Max fee paid to the paymaster does not cover actual fee"); + fn pay_refund(refund_recipient: AztecAddress, max_fee: U128, accepted_asset: AztecAddress) { + let actual_fee = U128::from_integer(context.transaction_fee()); + assert(actual_fee <= max_fee, "Max fee paid to the paymaster does not cover actual fee"); // TODO(#10805): Introduce a real exchange rate let refund = max_fee - actual_fee; diff --git a/noir-projects/noir-contracts/contracts/lending_contract/src/main.nr b/noir-projects/noir-contracts/contracts/lending_contract/src/main.nr index 10c46f41891..01ff29ae8d2 100644 --- a/noir-projects/noir-contracts/contracts/lending_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/lending_contract/src/main.nr @@ -34,8 +34,8 @@ contract Lending { collateral_asset: PublicMutable, stable_coin: PublicMutable, assets: Map, Context>, - collateral: Map, Context>, - static_debt: Map, Context>, // abusing keys very heavily + collateral: Map, Context>, + static_debt: Map, Context>, // abusing keys very heavily } // Constructs the contract. @@ -46,18 +46,18 @@ contract Lending { #[public] fn init( oracle: AztecAddress, - loan_to_value: Field, + loan_to_value: U128, collateral_asset: AztecAddress, stable_coin: AztecAddress, ) { let asset_loc = storage.assets.at(0); let asset: Asset = asset_loc.read(); - let loan_to_value = U128::from_integer(loan_to_value); + let loan_to_value = loan_to_value; assert(loan_to_value <= U128::from_integer(10000)); assert(asset.last_updated_ts == 0); - assert(asset.interest_accumulator == U128::from_integer(0)); + assert(asset.interest_accumulator == U128::zero()); let last_updated_ts = context.timestamp(); @@ -103,7 +103,7 @@ contract Lending { #[private] fn deposit_private( from: AztecAddress, - amount: Field, + amount: U128, nonce: Field, secret: Field, on_behalf_of: Field, @@ -123,7 +123,7 @@ contract Lending { #[public] fn deposit_public( - amount: Field, + amount: U128, nonce: Field, on_behalf_of: Field, collateral_asset: AztecAddress, @@ -140,7 +140,7 @@ contract Lending { #[public] #[internal] - fn _deposit(owner: AztecAddress, amount: Field, collateral_asset: AztecAddress) { + fn _deposit(owner: AztecAddress, amount: U128, collateral_asset: AztecAddress) { let _asset = Lending::at(context.this_address()).update_accumulator().call(&mut context); let coll_asset = storage.collateral_asset.read(); @@ -152,7 +152,7 @@ contract Lending { } #[private] - fn withdraw_private(secret: Field, to: AztecAddress, amount: Field) { + fn withdraw_private(secret: Field, to: AztecAddress, amount: U128) { let on_behalf_of = compute_identifier(secret, 0, context.msg_sender().to_field()); Lending::at(context.this_address()) ._withdraw(AztecAddress::from_field(on_behalf_of), to, amount) @@ -160,7 +160,7 @@ contract Lending { } #[public] - fn withdraw_public(to: AztecAddress, amount: Field) { + fn withdraw_public(to: AztecAddress, amount: U128) { let _ = Lending::at(context.this_address()) ._withdraw(context.msg_sender(), to, amount) .call(&mut context); @@ -168,30 +168,25 @@ contract Lending { #[public] #[internal] - fn _withdraw(owner: AztecAddress, recipient: AztecAddress, amount: Field) { + fn _withdraw(owner: AztecAddress, recipient: AztecAddress, amount: U128) { let asset = Lending::at(context.this_address()).update_accumulator().call(&mut context); let price = PriceFeed::at(asset.oracle).get_price(0).view(&mut context).price; let coll_loc = storage.collateral.at(owner); - let collateral: Field = coll_loc.read(); + let collateral = coll_loc.read(); let debt_loc = storage.static_debt.at(owner); - let static_debt: Field = debt_loc.read(); + let static_debt = debt_loc.read(); // debt_covered will revert if decrease would leave insufficient collateral to cover debt. // or trying to remove more collateral than available - let debt_covered = covered_by_collateral( - price, - asset.loan_to_value, - U128::from_integer(collateral), - U128::from_integer(0), - U128::from_integer(amount), - ); + let debt_covered = + covered_by_collateral(price, asset.loan_to_value, collateral, U128::zero(), amount); let debt_returns = debt_updates( asset.interest_accumulator, - U128::from_integer(static_debt), - U128::from_integer(0), - U128::from_integer(0), + static_debt, + U128::zero(), + U128::zero(), ); assert(debt_returns.debt_value < debt_covered); @@ -206,7 +201,7 @@ contract Lending { } #[private] - fn borrow_private(secret: Field, to: AztecAddress, amount: Field) { + fn borrow_private(secret: Field, to: AztecAddress, amount: U128) { let on_behalf_of = compute_identifier(secret, 0, context.msg_sender().to_field()); let _ = Lending::at(context.this_address()) ._borrow(AztecAddress::from_field(on_behalf_of), to, amount) @@ -214,7 +209,7 @@ contract Lending { } #[public] - fn borrow_public(to: AztecAddress, amount: Field) { + fn borrow_public(to: AztecAddress, amount: U128) { let _ = Lending::at(context.this_address())._borrow(context.msg_sender(), to, amount).call( &mut context, ); @@ -222,31 +217,31 @@ contract Lending { #[public] #[internal] - fn _borrow(owner: AztecAddress, to: AztecAddress, amount: Field) { + fn _borrow(owner: AztecAddress, to: AztecAddress, amount: U128) { let asset = Lending::at(context.this_address()).update_accumulator().call(&mut context); let price = PriceFeed::at(asset.oracle).get_price(0).view(&mut context).price; // Fetch collateral and static_debt, compute health of current position - let collateral = U128::from_integer(storage.collateral.at(owner).read()); - let static_debt = U128::from_integer(storage.static_debt.at(owner).read()); + let collateral = storage.collateral.at(owner).read(); + let static_debt = storage.static_debt.at(owner).read(); let debt_covered = covered_by_collateral( price, asset.loan_to_value, collateral, - U128::from_integer(0), - U128::from_integer(0), + U128::zero(), + U128::zero(), ); let debt_returns = debt_updates( asset.interest_accumulator, static_debt, - U128::from_integer(amount), - U128::from_integer(0), + amount, + U128::zero(), ); assert(debt_returns.debt_value < debt_covered); - storage.static_debt.at(owner).write(debt_returns.static_debt.to_integer()); + storage.static_debt.at(owner).write(debt_returns.static_debt); // @todo @LHerskind Need to support both private and public minting. let stable_coin = storage.stable_coin.read(); @@ -256,7 +251,7 @@ contract Lending { #[private] fn repay_private( from: AztecAddress, - amount: Field, + amount: U128, nonce: Field, secret: Field, on_behalf_of: Field, @@ -273,7 +268,7 @@ contract Lending { } #[public] - fn repay_public(amount: Field, nonce: Field, owner: AztecAddress, stable_coin: AztecAddress) { + fn repay_public(amount: U128, nonce: Field, owner: AztecAddress, stable_coin: AztecAddress) { let _ = Token::at(stable_coin).burn_public(context.msg_sender(), amount, nonce).call( &mut context, ); @@ -284,21 +279,21 @@ contract Lending { #[public] #[internal] - fn _repay(owner: AztecAddress, amount: Field, stable_coin: AztecAddress) { + fn _repay(owner: AztecAddress, amount: U128, stable_coin: AztecAddress) { let asset = Lending::at(context.this_address()).update_accumulator().call(&mut context); // To ensure that private is using the correct token. assert(stable_coin.eq(storage.stable_coin.read())); - let static_debt = U128::from_integer(storage.static_debt.at(owner).read()); + let static_debt = storage.static_debt.at(owner).read(); let debt_returns = debt_updates( asset.interest_accumulator, static_debt, - U128::from_integer(0), - U128::from_integer(amount), + U128::zero(), + amount, ); - storage.static_debt.at(owner).write(debt_returns.static_debt.to_integer()); + storage.static_debt.at(owner).write(debt_returns.static_debt); } #[public] @@ -313,8 +308,7 @@ contract Lending { let collateral = storage.collateral.at(owner).read(); let static_debt = storage.static_debt.at(owner).read(); let asset: Asset = storage.assets.at(0).read(); - let debt = - debt_value(U128::from_integer(static_debt), asset.interest_accumulator).to_integer(); + let debt = debt_value(static_debt, asset.interest_accumulator); Position { collateral, static_debt, debt } } diff --git a/noir-projects/noir-contracts/contracts/lending_contract/src/position.nr b/noir-projects/noir-contracts/contracts/lending_contract/src/position.nr index 15144b6e722..ded1abb2d2d 100644 --- a/noir-projects/noir-contracts/contracts/lending_contract/src/position.nr +++ b/noir-projects/noir-contracts/contracts/lending_contract/src/position.nr @@ -1,13 +1,14 @@ use dep::aztec::protocol_types::traits::{Deserialize, Serialize}; pub struct Position { - collateral: Field, - static_debt: Field, - debt: Field, + collateral: U128, + static_debt: U128, + debt: U128, } global POSITION_SERIALIZED_LEN: u32 = 3; +// TODO(benesjan): either don't call to_field below or make this Packable trait instead. impl Serialize for Position { fn serialize(position: Position) -> [Field; POSITION_SERIALIZED_LEN] { [position.collateral.to_field(), position.static_debt.to_field(), position.debt.to_field()] @@ -16,6 +17,10 @@ impl Serialize for Position { impl Deserialize for Position { fn deserialize(fields: [Field; POSITION_SERIALIZED_LEN]) -> Position { - Position { collateral: fields[0], static_debt: fields[1], debt: fields[2] } + Position { + collateral: U128::from_integer(fields[0]), + static_debt: U128::from_integer(fields[1]), + debt: U128::from_integer(fields[2]), + } } } diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr index f1ec6266f7e..cd9d827d0fa 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr @@ -63,7 +63,7 @@ contract Token { struct Transfer { from: AztecAddress, to: AztecAddress, - amount: Field, + amount: U128, } // docs:start:storage_struct @@ -168,16 +168,16 @@ contract Token { // docs:start:total_supply #[public] #[view] - fn total_supply() -> Field { - storage.total_supply.read().to_integer() + fn total_supply() -> U128 { + storage.total_supply.read() } // docs:end:total_supply // docs:start:balance_of_public #[public] #[view] - fn balance_of_public(owner: AztecAddress) -> Field { - storage.public_balances.at(owner).read().to_integer() + fn balance_of_public(owner: AztecAddress) -> U128 { + storage.public_balances.at(owner).read() } // docs:end:balance_of_public @@ -195,11 +195,10 @@ contract Token { // docs:start:mint_to_public #[public] - fn mint_to_public(to: AztecAddress, amount: Field) { + fn mint_to_public(to: AztecAddress, amount: U128) { // docs:start:read_minter assert(storage.minters.at(context.msg_sender()).read(), "caller is not minter"); // docs:end:read_minter - let amount = U128::from_integer(amount); let new_balance = storage.public_balances.at(to).read().add(amount); let supply = storage.total_supply.read().add(amount); storage.public_balances.at(to).write(new_balance); @@ -209,13 +208,12 @@ contract Token { // docs:start:transfer_in_public #[public] - fn transfer_in_public(from: AztecAddress, to: AztecAddress, amount: Field, nonce: Field) { + fn transfer_in_public(from: AztecAddress, to: AztecAddress, amount: U128, nonce: Field) { if (!from.eq(context.msg_sender())) { assert_current_call_valid_authwit_public(&mut context, from); } else { assert(nonce == 0, "invalid nonce"); } - let amount = U128::from_integer(amount); let from_balance = storage.public_balances.at(from).read().sub(amount); storage.public_balances.at(from).write(from_balance); let to_balance = storage.public_balances.at(to).read().add(amount); @@ -225,7 +223,7 @@ contract Token { // docs:start:burn_public #[public] - fn burn_public(from: AztecAddress, amount: Field, nonce: Field) { + fn burn_public(from: AztecAddress, amount: U128, nonce: Field) { // docs:start:assert_current_call_valid_authwit_public if (!from.eq(context.msg_sender())) { assert_current_call_valid_authwit_public(&mut context, from); @@ -233,7 +231,6 @@ contract Token { assert(nonce == 0, "invalid nonce"); } // docs:end:assert_current_call_valid_authwit_public - let amount = U128::from_integer(amount); let from_balance = storage.public_balances.at(from).read().sub(amount); storage.public_balances.at(from).write(from_balance); let new_supply = storage.total_supply.read().sub(amount); @@ -243,7 +240,7 @@ contract Token { // docs:start:transfer_to_public #[private] - fn transfer_to_public(from: AztecAddress, to: AztecAddress, amount: Field, nonce: Field) { + fn transfer_to_public(from: AztecAddress, to: AztecAddress, amount: U128, nonce: Field) { if (!from.eq(context.msg_sender())) { assert_current_call_valid_authwit(&mut context, from); } else { @@ -252,19 +249,20 @@ contract Token { // TODO: constrain encryption below - we are using unconstrained here only because of the following Noir issue // https://github.com/noir-lang/noir/issues/5771 - storage.balances.at(from).sub(from, U128::from_integer(amount)).emit( - encode_and_encrypt_note_unconstrained(&mut context, from, from), - ); + storage.balances.at(from).sub(from, amount).emit(encode_and_encrypt_note_unconstrained( + &mut context, + from, + from, + )); Token::at(context.this_address())._increase_public_balance(to, amount).enqueue(&mut context); } // docs:end:transfer_to_public // docs:start:transfer #[private] - fn transfer(to: AztecAddress, amount: Field) { + fn transfer(to: AztecAddress, amount: U128) { let from = context.msg_sender(); - let amount = U128::from_integer(amount); // We reduce `from`'s balance by amount by recursively removing notes over potentially multiple calls. This // method keeps the gate count for each individual call low - reading too many notes at once could result in // circuits in which proving is not feasible. @@ -292,9 +290,11 @@ contract Token { // function is only designed to be used in situations where the event is not strictly necessary (e.g. payment to // another person where the payment is considered to be successful when the other party successfully decrypts a // note). - Transfer { from, to, amount: amount.to_field() }.emit( - encode_and_encrypt_event_unconstrained(&mut context, to, from), - ); + Transfer { from, to, amount }.emit(encode_and_encrypt_event_unconstrained( + &mut context, + to, + from, + )); } // docs:end:transfer @@ -311,7 +311,7 @@ contract Token { // We could in some cases fail early inside try_sub if we detected that fewer notes than the maximum were // returned and we were still unable to reach the target amount, but that'd make the code more complicated, and // optimizing for the failure scenario is not as important. - assert(subtracted > U128::from_integer(0), "Balance too low"); + assert(subtracted > U128::zero(), "Balance too low"); if subtracted >= amount { // We have achieved our goal of nullifying notes that add up to more than amount, so we return the change subtracted - amount @@ -332,19 +332,17 @@ contract Token { account: AztecAddress, remaining: U128, ) -> PrivateCallInterface<25, U128> { - Token::at(context.this_address())._recurse_subtract_balance(account, remaining.to_field()) + Token::at(context.this_address())._recurse_subtract_balance(account, remaining) } - // TODO(#7728): even though the amount should be a U128, we can't have that type in a contract interface due to - // serialization issues. #[internal] #[private] - fn _recurse_subtract_balance(account: AztecAddress, amount: Field) -> U128 { + fn _recurse_subtract_balance(account: AztecAddress, amount: U128) -> U128 { subtract_balance( &mut context, storage, account, - U128::from_integer(amount), + amount, RECURSIVE_TRANSFER_CALL_MAX_NOTES, ) } @@ -364,7 +362,7 @@ contract Token { // docs:start:transfer_in_private #[private] - fn transfer_in_private(from: AztecAddress, to: AztecAddress, amount: Field, nonce: Field) { + fn transfer_in_private(from: AztecAddress, to: AztecAddress, amount: U128, nonce: Field) { // docs:start:assert_current_call_valid_authwit if (!from.eq(context.msg_sender())) { assert_current_call_valid_authwit(&mut context, from); @@ -373,7 +371,6 @@ contract Token { } // docs:end:assert_current_call_valid_authwit - let amount = U128::from_integer(amount); // docs:start:increase_private_balance // docs:start:encrypted // TODO: constrain encryption below - we are using unconstrained here only becuase of the following Noir issue @@ -397,7 +394,7 @@ contract Token { // docs:start:burn_private #[private] - fn burn_private(from: AztecAddress, amount: Field, nonce: Field) { + fn burn_private(from: AztecAddress, amount: U128, nonce: Field) { if (!from.eq(context.msg_sender())) { assert_current_call_valid_authwit(&mut context, from); } else { @@ -405,9 +402,11 @@ contract Token { } // TODO: constrain encryption below - we are using unconstrained here only becuase of the following Noir issue // https://github.com/noir-lang/noir/issues/5771 - storage.balances.at(from).sub(from, U128::from_integer(amount)).emit( - encode_and_encrypt_note_unconstrained(&mut context, from, from), - ); + storage.balances.at(from).sub(from, amount).emit(encode_and_encrypt_note_unconstrained( + &mut context, + from, + from, + )); Token::at(context.this_address())._reduce_total_supply(amount).enqueue(&mut context); } // docs:end:burn_private @@ -415,7 +414,7 @@ contract Token { // docs:start:transfer_to_private // Transfers token `amount` from public balance of message sender to a private balance of `to`. #[private] - fn transfer_to_private(to: AztecAddress, amount: Field) { + fn transfer_to_private(to: AztecAddress, amount: U128) { // `from` is the owner of the public balance from which we'll subtract the `amount`. let from = context.msg_sender(); let token = Token::at(context.this_address()); @@ -498,7 +497,7 @@ contract Token { /// The transfer must be prepared by calling `prepare_private_balance_increase` first and the resulting /// `hiding_point_slot` must be passed as an argument to this function. #[public] - fn finalize_transfer_to_private(amount: Field, hiding_point_slot: Field) { + fn finalize_transfer_to_private(amount: U128, hiding_point_slot: Field) { let from = context.msg_sender(); _finalize_transfer_to_private(from, amount, hiding_point_slot, &mut context, storage); } @@ -512,7 +511,7 @@ contract Token { #[internal] fn _finalize_transfer_to_private_unsafe( from: AztecAddress, - amount: Field, + amount: U128, hiding_point_slot: Field, ) { _finalize_transfer_to_private(from, amount, hiding_point_slot, &mut context, storage); @@ -522,14 +521,11 @@ contract Token { #[contract_library_method] fn _finalize_transfer_to_private( from: AztecAddress, - amount: Field, + amount: U128, hiding_point_slot: Field, context: &mut PublicContext, storage: Storage<&mut PublicContext>, ) { - // TODO(#8271): Type the amount as U128 and nuke the ugly cast - let amount = U128::from_integer(amount); - // First we subtract the `amount` from the public balance of `from` let from_balance = storage.public_balances.at(from).read().sub(amount); storage.public_balances.at(from).write(from_balance); @@ -549,7 +545,7 @@ contract Token { fn mint_to_private( from: AztecAddress, // sender of the tag: TODO(#9887): this is not great? to: AztecAddress, - amount: Field, + amount: U128, ) { let token = Token::at(context.this_address()); @@ -574,7 +570,7 @@ contract Token { /// and `finalize_transfer_to_private`. It is however used very commonly so it makes sense to optimize it /// (e.g. used during token bridging, in AMM liquidity token etc.). #[public] - fn finalize_mint_to_private(amount: Field, hiding_point_slot: Field) { + fn finalize_mint_to_private(amount: U128, hiding_point_slot: Field) { assert(storage.minters.at(context.msg_sender()).read(), "caller is not minter"); _finalize_mint_to_private(amount, hiding_point_slot, &mut context, storage); @@ -586,7 +582,7 @@ contract Token { #[internal] fn _finalize_mint_to_private_unsafe( from: AztecAddress, - amount: Field, + amount: U128, hiding_point_slot: Field, ) { // We check the minter permissions as it was not done in `mint_to_private` function. @@ -597,13 +593,11 @@ contract Token { #[contract_library_method] fn _finalize_mint_to_private( - amount: Field, + amount: U128, hiding_point_slot: Field, context: &mut PublicContext, storage: Storage<&mut PublicContext>, ) { - let amount = U128::from_integer(amount); - // First we increase the total supply by the `amount` let supply = storage.total_supply.read().add(amount); storage.total_supply.write(supply); @@ -621,7 +615,7 @@ contract Token { #[private] fn setup_refund( user: AztecAddress, // A user for which we are setting up the fee refund. - max_fee: Field, // The maximum fee a user is willing to pay for the tx. + max_fee: U128, // The maximum fee a user is willing to pay for the tx. nonce: Field, // A nonce to make authwitness unique. ) { // 1. This function is called by FPC when setting up a refund so we need to support the authwit flow here @@ -634,7 +628,7 @@ contract Token { &mut context, storage, user, - U128::from_integer(max_fee), + max_fee, INITIAL_TRANSFER_CALL_MAX_NOTES, ); // Emit the change note. @@ -650,10 +644,18 @@ contract Token { // 4. Set the public teardown function to `complete_refund(...)`. Public teardown is the only time when a public // function has access to the final transaction fee, which is needed to compute the actual refund amount. let fee_recipient = context.msg_sender(); // FPC is the fee recipient. + let max_fee_serialized = max_fee.serialize(); context.set_public_teardown_function( context.this_address(), - comptime { FunctionSelector::from_signature("complete_refund((Field),Field,Field)") }, - [fee_recipient.to_field(), user_point_slot, max_fee], + comptime { + FunctionSelector::from_signature("complete_refund((Field),Field,(Field,Field))") + }, + [ + fee_recipient.to_field(), + user_point_slot, + max_fee_serialized[0], + max_fee_serialized[1], + ], ); } // docs:end:setup_refund @@ -674,16 +676,12 @@ contract Token { context.storage_write(slot + aztec::protocol_types::point::POINT_LENGTH as Field, setup_log); } - // TODO(#7728): even though the max_fee should be a U128, we can't have that type in a contract interface due - // to serialization issues. // docs:start:complete_refund /// Executed as a public teardown function and is responsible for completing the refund in a private fee payment /// flow. #[public] #[internal] - fn complete_refund(fee_recipient: AztecAddress, user_slot: Field, max_fee: Field) { - // TODO(#7728): Remove the next line - let max_fee = U128::from_integer(max_fee); + fn complete_refund(fee_recipient: AztecAddress, user_slot: Field, max_fee: U128) { let tx_fee = U128::from_integer(context.transaction_fee()); // 1. We check that user funded the fee payer contract with at least the transaction fee. @@ -695,7 +693,7 @@ contract Token { let refund_amount = max_fee - tx_fee; // 3. We send the tx fee to the fee recipient in public. - _increase_public_balance_inner(fee_recipient, tx_fee.to_field(), storage); + _increase_public_balance_inner(fee_recipient, tx_fee, storage); // 4. We construct the user note finalization payload with the refund amount. let user_finalization_payload = @@ -713,7 +711,7 @@ contract Token { /// function. #[public] #[internal] - fn _increase_public_balance(to: AztecAddress, amount: Field) { + fn _increase_public_balance(to: AztecAddress, amount: U128) { _increase_public_balance_inner(to, amount, storage); } // docs:end:increase_public_balance @@ -721,27 +719,27 @@ contract Token { #[contract_library_method] fn _increase_public_balance_inner( to: AztecAddress, - amount: Field, + amount: U128, storage: Storage<&mut PublicContext>, ) { - let new_balance = storage.public_balances.at(to).read().add(U128::from_integer(amount)); + let new_balance = storage.public_balances.at(to).read().add(amount); storage.public_balances.at(to).write(new_balance); } // docs:start:reduce_total_supply #[public] #[internal] - fn _reduce_total_supply(amount: Field) { + fn _reduce_total_supply(amount: U128) { // Only to be called from burn. - let new_supply = storage.total_supply.read().sub(U128::from_integer(amount)); + let new_supply = storage.total_supply.read().sub(amount); storage.total_supply.write(new_supply); } // docs:end:reduce_total_supply /// Unconstrained /// // docs:start:balance_of_private - pub(crate) unconstrained fn balance_of_private(owner: AztecAddress) -> pub Field { - storage.balances.at(owner).balance_of().to_field() + pub(crate) unconstrained fn balance_of_private(owner: AztecAddress) -> pub U128 { + storage.balances.at(owner).balance_of() } // docs:end:balance_of_private } diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/burn_private.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/burn_private.nr index a3ac58f79a1..3559c851cc4 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/burn_private.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/burn_private.nr @@ -7,7 +7,7 @@ use dep::aztec::oracle::random::random; unconstrained fn burn_private_on_behalf_of_self() { let (env, token_contract_address, owner, _, mint_amount) = utils::setup_and_mint_to_private(/* with_account_contracts */ false); - let burn_amount = mint_amount / 10; + let burn_amount = mint_amount / U128::from_integer(10); // Burn less than balance Token::at(token_contract_address).burn_private(owner, burn_amount, 0).call(&mut env.private()); @@ -18,7 +18,7 @@ unconstrained fn burn_private_on_behalf_of_self() { unconstrained fn burn_private_on_behalf_of_other() { let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_private(/* with_account_contracts */ true); - let burn_amount = mint_amount / 10; + let burn_amount = mint_amount / U128::from_integer(10); // Burn on behalf of other let burn_call_interface = @@ -41,7 +41,7 @@ unconstrained fn burn_private_failure_more_than_balance() { utils::setup_and_mint_to_public(/* with_account_contracts */ false); // Burn more than balance - let burn_amount = mint_amount * 10; + let burn_amount = mint_amount * U128::from_integer(10); Token::at(token_contract_address).burn_private(owner, burn_amount, 0).call(&mut env.private()); } @@ -51,7 +51,7 @@ unconstrained fn burn_private_failure_on_behalf_of_self_non_zero_nonce() { utils::setup_and_mint_to_public(/* with_account_contracts */ false); // Burn more than balance - let burn_amount = mint_amount / 10; + let burn_amount = mint_amount / U128::from_integer(10); Token::at(token_contract_address).burn_private(owner, burn_amount, random()).call( &mut env.private(), ); @@ -63,7 +63,7 @@ unconstrained fn burn_private_failure_on_behalf_of_other_more_than_balance() { utils::setup_and_mint_to_public(/* with_account_contracts */ true); // Burn more than balance - let burn_amount = mint_amount * 10; + let burn_amount = mint_amount * U128::from_integer(10); // Burn on behalf of other let burn_call_interface = Token::at(token_contract_address).burn_private(owner, burn_amount, random()); @@ -83,7 +83,7 @@ unconstrained fn burn_private_failure_on_behalf_of_other_without_approval() { utils::setup_and_mint_to_public(/* with_account_contracts */ true); // Burn more than balance - let burn_amount = mint_amount / 10; + let burn_amount = mint_amount / U128::from_integer(10); // Burn on behalf of other let burn_call_interface = Token::at(token_contract_address).burn_private(owner, burn_amount, 3); // Impersonate recipient to perform the call @@ -97,7 +97,7 @@ unconstrained fn burn_private_failure_on_behalf_of_other_wrong_designated_caller utils::setup_and_mint_to_public(/* with_account_contracts */ true); // Burn more than balance - let burn_amount = mint_amount / 10; + let burn_amount = mint_amount / U128::from_integer(10); // Burn on behalf of other let burn_call_interface = Token::at(token_contract_address).burn_private(owner, burn_amount, 3); authwit_cheatcodes::add_private_authwit_from_call_interface(owner, owner, burn_call_interface); diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/burn_public.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/burn_public.nr index 1d427ff30ff..075007fdc3b 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/burn_public.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/burn_public.nr @@ -7,7 +7,7 @@ use dep::aztec::oracle::random::random; unconstrained fn burn_public_success() { let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_public(/* with_account_contracts */ false); - let burn_amount = mint_amount / 10; + let burn_amount = mint_amount / U128::from_integer(10); // Burn less than balance Token::at(token_contract_address).burn_public(owner, burn_amount, 0).call(&mut env.public()); @@ -18,7 +18,7 @@ unconstrained fn burn_public_success() { unconstrained fn burn_public_on_behalf_of_other() { let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_public(/* with_account_contracts */ true); - let burn_amount = mint_amount / 10; + let burn_amount = mint_amount / U128::from_integer(10); // Burn on behalf of other let burn_call_interface = @@ -41,7 +41,7 @@ unconstrained fn burn_public_failure_more_than_balance() { utils::setup_and_mint_to_public(/* with_account_contracts */ false); // Burn more than balance - let burn_amount = mint_amount * 10; + let burn_amount = mint_amount * U128::from_integer(10); // Try to burn Token::at(token_contract_address).burn_public(owner, burn_amount, 0).call(&mut env.public()); } @@ -52,7 +52,7 @@ unconstrained fn burn_public_failure_on_behalf_of_self_non_zero_nonce() { utils::setup_and_mint_to_public(/* with_account_contracts */ false); // Burn on behalf of self with non-zero nonce - let burn_amount = mint_amount / 10; + let burn_amount = mint_amount / U128::from_integer(10); // Try to burn Token::at(token_contract_address).burn_public(owner, burn_amount, random()).call( &mut env.public(), @@ -65,7 +65,7 @@ unconstrained fn burn_public_failure_on_behalf_of_other_without_approval() { utils::setup_and_mint_to_public(/* with_account_contracts */ true); // Burn on behalf of other without approval - let burn_amount = mint_amount / 10; + let burn_amount = mint_amount / U128::from_integer(10); let burn_call_interface = Token::at(token_contract_address).burn_public(owner, burn_amount, random()); // Impersonate recipient to perform the call @@ -79,7 +79,7 @@ unconstrained fn burn_public_failure_on_behalf_of_other_wrong_caller() { utils::setup_and_mint_to_public(/* with_account_contracts */ true); // Burn on behalf of other, wrong designated caller - let burn_amount = mint_amount / 10; + let burn_amount = mint_amount / U128::from_integer(10); let burn_call_interface = Token::at(token_contract_address).burn_public(owner, burn_amount, random()); authwit_cheatcodes::add_public_authwit_from_call_interface(owner, owner, burn_call_interface); diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/mint_to_public.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/mint_to_public.nr index c4cb9055ac0..b762de7856b 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/mint_to_public.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/mint_to_public.nr @@ -5,7 +5,7 @@ unconstrained fn mint_to_public_success() { // Setup without account contracts. We are not using authwits here, so dummy accounts are enough let (env, token_contract_address, owner, _) = utils::setup(/* with_account_contracts */ false); - let mint_amount = 10000; + let mint_amount = U128::from_integer(10_000); Token::at(token_contract_address).mint_to_public(owner, mint_amount).call(&mut env.public()); utils::check_public_balance(token_contract_address, owner, mint_amount); @@ -21,36 +21,36 @@ unconstrained fn mint_to_public_failures() { utils::setup(/* with_account_contracts */ false); // As non-minter - let mint_amount = 10000; + let mint_amount = U128::from_integer(10_000); env.impersonate(recipient); let mint_to_public_call_interface = Token::at(token_contract_address).mint_to_public(owner, mint_amount); env.assert_public_call_fails(mint_to_public_call_interface); - utils::check_public_balance(token_contract_address, owner, 0); + utils::check_public_balance(token_contract_address, owner, U128::zero()); env.impersonate(owner); // Overflow recipient - let mint_amount = 2.pow_32(128); + let mint_amount = U128::from_integer(2.pow_32(128)); let mint_to_public_call_interface = Token::at(token_contract_address).mint_to_public(owner, mint_amount); env.assert_public_call_fails(mint_to_public_call_interface); - utils::check_public_balance(token_contract_address, owner, 0); + utils::check_public_balance(token_contract_address, owner, U128::zero()); // Overflow total supply - let mint_for_recipient_amount = 1000; + let mint_for_recipient_amount = U128::from_integer(1_000); Token::at(token_contract_address).mint_to_public(recipient, mint_for_recipient_amount).call( &mut env.public(), ); - let mint_amount = 2.pow_32(128) - mint_for_recipient_amount; + let mint_amount = U128::from_integer(2.pow_32(128)) - mint_for_recipient_amount; let mint_to_public_call_interface = Token::at(token_contract_address).mint_to_public(owner, mint_amount); env.assert_public_call_fails(mint_to_public_call_interface); utils::check_public_balance(token_contract_address, recipient, mint_for_recipient_amount); - utils::check_public_balance(token_contract_address, owner, 0); + utils::check_public_balance(token_contract_address, owner, U128::zero()); } diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/refunds.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/refunds.nr index 5e01965e8a2..49c5b7bc373 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/refunds.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/refunds.nr @@ -13,10 +13,10 @@ unconstrained fn setup_refund_success() { let txe_expected_gas_used = Gas::new(1, 1); // TXE oracle uses gas fees of (1, 1) let txe_gas_fees = GasFees::new(1, 1); - let expected_tx_fee = txe_expected_gas_used.compute_fee(txe_gas_fees); + let expected_tx_fee = U128::from_integer(txe_expected_gas_used.compute_fee(txe_gas_fees)); // Fund account with enough to cover tx fee plus some - let funded_amount = 1_000 + expected_tx_fee; + let funded_amount = U128::from_integer(1_000) + expected_tx_fee; let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_amount_to_private(true, funded_amount); @@ -82,7 +82,7 @@ unconstrained fn setup_refund_insufficient_funded_amount() { let fee_payer = recipient; // We set funded amount to 0 to make the transaction fee higher than the funded amount - let funded_amount = 0; + let funded_amount = U128::zero(); let nonce = random(); let setup_refund_from_call_interface = diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer.nr index 24015869d66..87d42b88c94 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer.nr @@ -11,7 +11,7 @@ unconstrained fn transfer_private() { // docs:start:txe_test_transfer_private // Transfer tokens - let transfer_amount = 1000; + let transfer_amount = U128::from_integer(1000); Token::at(token_contract_address).transfer(recipient, transfer_amount).call(&mut env.private()); // docs:end:txe_test_transfer_private // Check balances @@ -25,7 +25,7 @@ unconstrained fn transfer_private_to_self() { let (env, token_contract_address, owner, _, mint_amount) = utils::setup_and_mint_to_private(/* with_account_contracts */ false); // Transfer tokens - let transfer_amount = 1000; + let transfer_amount = U128::from_integer(1000); Token::at(token_contract_address).transfer(owner, transfer_amount).call(&mut env.private()); // Check balances @@ -39,7 +39,7 @@ unconstrained fn transfer_private_to_non_deployed_account() { utils::setup_and_mint_to_private(/* with_account_contracts */ false); let not_deployed = cheatcodes::create_account(); // Transfer tokens - let transfer_amount = 1000; + let transfer_amount = U128::from_integer(1000); Token::at(token_contract_address).transfer(not_deployed.address, transfer_amount).call( &mut env.private(), ); @@ -59,6 +59,6 @@ unconstrained fn transfer_private_failure_more_than_balance() { let (env, token_contract_address, _, recipient, mint_amount) = utils::setup_and_mint_to_private(/* with_account_contracts */ false); // Transfer tokens - let transfer_amount = mint_amount + 1; + let transfer_amount = mint_amount + U128::from_integer(1); Token::at(token_contract_address).transfer(recipient, transfer_amount).call(&mut env.private()); } diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer_in_private.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer_in_private.nr index e7113a7aa44..cfb01d97ab1 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer_in_private.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer_in_private.nr @@ -9,7 +9,7 @@ unconstrained fn transfer_private_on_behalf_of_other() { utils::setup_and_mint_to_private(/* with_account_contracts */ true); // Add authwit // docs:start:private_authwit - let transfer_amount = 1000; + let transfer_amount = U128::from_integer(1000); let transfer_private_from_call_interface = Token::at(token_contract_address).transfer_in_private(owner, recipient, transfer_amount, 1); authwit_cheatcodes::add_private_authwit_from_call_interface( @@ -34,7 +34,7 @@ unconstrained fn transfer_private_failure_on_behalf_of_self_non_zero_nonce() { let (env, token_contract_address, owner, recipient, _) = utils::setup_and_mint_to_private(/* with_account_contracts */ false); // Add authwit - let transfer_amount = 1000; + let transfer_amount = U128::from_integer(1000); let transfer_private_from_call_interface = Token::at(token_contract_address).transfer_in_private(owner, recipient, transfer_amount, 1); authwit_cheatcodes::add_private_authwit_from_call_interface( @@ -53,7 +53,7 @@ unconstrained fn transfer_private_failure_on_behalf_of_more_than_balance() { let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_private(/* with_account_contracts */ true); // Add authwit - let transfer_amount = mint_amount + 1; + let transfer_amount = mint_amount + U128::from_integer(1); let transfer_private_from_call_interface = Token::at(token_contract_address).transfer_in_private(owner, recipient, transfer_amount, 1); authwit_cheatcodes::add_private_authwit_from_call_interface( @@ -73,7 +73,7 @@ unconstrained fn transfer_private_failure_on_behalf_of_other_without_approval() let (env, token_contract_address, owner, recipient, _) = utils::setup_and_mint_to_private(/* with_account_contracts */ true); // Add authwit - let transfer_amount = 1000; + let transfer_amount = U128::from_integer(1000); let transfer_private_from_call_interface = Token::at(token_contract_address).transfer_in_private(owner, recipient, transfer_amount, 1); // Impersonate recipient to perform the call @@ -88,7 +88,7 @@ unconstrained fn transfer_private_failure_on_behalf_of_other_wrong_caller() { let (env, token_contract_address, owner, recipient, _) = utils::setup_and_mint_to_private(/* with_account_contracts */ true); // Add authwit - let transfer_amount = 1000; + let transfer_amount = U128::from_integer(1000); let transfer_private_from_call_interface = Token::at(token_contract_address).transfer_in_private(owner, recipient, transfer_amount, 1); authwit_cheatcodes::add_private_authwit_from_call_interface( diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer_in_public.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer_in_public.nr index aa8ba0376fb..5b60b28f8e9 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer_in_public.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer_in_public.nr @@ -9,7 +9,7 @@ unconstrained fn public_transfer() { let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_public(/* with_account_contracts */ false); // Transfer tokens - let transfer_amount = mint_amount / 10; + let transfer_amount = mint_amount / U128::from_integer(10); Token::at(token_contract_address).transfer_in_public(owner, recipient, transfer_amount, 0).call( &mut env.public(), ); @@ -25,7 +25,7 @@ unconstrained fn public_transfer_to_self() { let (env, token_contract_address, owner, _, mint_amount) = utils::setup_and_mint_to_public(/* with_account_contracts */ false); // Transfer tokens - let transfer_amount = mint_amount / 10; + let transfer_amount = mint_amount / U128::from_integer(10); // docs:start:call_public Token::at(token_contract_address).transfer_in_public(owner, owner, transfer_amount, 0).call( &mut env.public(), @@ -40,7 +40,7 @@ unconstrained fn public_transfer_on_behalf_of_other() { // Setup with account contracts. Slower since we actually deploy them, but needed for authwits. let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_public(/* with_account_contracts */ true); - let transfer_amount = mint_amount / 10; + let transfer_amount = mint_amount / U128::from_integer(10); let public_transfer_in_private_call_interface = Token::at(token_contract_address).transfer_in_public(owner, recipient, transfer_amount, 1); authwit_cheatcodes::add_public_authwit_from_call_interface( @@ -63,7 +63,7 @@ unconstrained fn public_transfer_failure_more_than_balance() { let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_public(/* with_account_contracts */ false); // Transfer tokens - let transfer_amount = mint_amount + 1; + let transfer_amount = mint_amount + U128::from_integer(1); let public_transfer_call_interface = Token::at(token_contract_address).transfer_in_public(owner, recipient, transfer_amount, 0); // Try to transfer tokens @@ -76,7 +76,7 @@ unconstrained fn public_transfer_failure_on_behalf_of_self_non_zero_nonce() { let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_public(/* with_account_contracts */ false); // Transfer tokens - let transfer_amount = mint_amount / 10; + let transfer_amount = mint_amount / U128::from_integer(10); let public_transfer_call_interface = Token::at(token_contract_address).transfer_in_public( owner, recipient, @@ -97,7 +97,7 @@ unconstrained fn public_transfer_failure_on_behalf_of_other_without_approval() { // Setup with account contracts. Slower since we actually deploy them, but needed for authwits. let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_public(/* with_account_contracts */ true); - let transfer_amount = mint_amount / 10; + let transfer_amount = mint_amount / U128::from_integer(10); let public_transfer_in_private_call_interface = Token::at(token_contract_address).transfer_in_public(owner, recipient, transfer_amount, 1); // Impersonate recipient to perform the call @@ -111,7 +111,7 @@ unconstrained fn public_transfer_failure_on_behalf_of_other_more_than_balance() // Setup with account contracts. Slower since we actually deploy them, but needed for authwits. let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_public(/* with_account_contracts */ true); - let transfer_amount = mint_amount + 1; + let transfer_amount = mint_amount + U128::from_integer(1); // docs:start:public_authwit let public_transfer_in_private_call_interface = Token::at(token_contract_address).transfer_in_public(owner, recipient, transfer_amount, 1); @@ -132,7 +132,7 @@ unconstrained fn public_transfer_failure_on_behalf_of_other_wrong_caller() { // Setup with account contracts. Slower since we actually deploy them, but needed for authwits. let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_public(/* with_account_contracts */ true); - let transfer_amount = mint_amount / 10; + let transfer_amount = mint_amount / U128::from_integer(10); let public_transfer_in_private_call_interface = Token::at(token_contract_address).transfer_in_public(owner, recipient, transfer_amount, 1); authwit_cheatcodes::add_public_authwit_from_call_interface( diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer_to_public.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer_to_public.nr index 7789bf8aeb4..8d958d29dcf 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer_to_public.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/transfer_to_public.nr @@ -9,7 +9,7 @@ unconstrained fn transfer_to_public_on_behalf_of_self() { let (env, token_contract_address, owner, _, mint_amount) = utils::setup_and_mint_to_private(/* with_account_contracts */ false); - let transfer_to_public_amount = mint_amount / 10; + let transfer_to_public_amount = mint_amount / U128::from_integer(10); Token::at(token_contract_address) .transfer_to_public(owner, owner, transfer_to_public_amount, 0) .call(&mut env.private()); @@ -26,7 +26,7 @@ unconstrained fn transfer_to_public_on_behalf_of_other() { let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_private(/* with_account_contracts */ true); - let transfer_to_public_amount = mint_amount / 10; + let transfer_to_public_amount = mint_amount / U128::from_integer(10); let transfer_to_public_call_interface = Token::at(token_contract_address).transfer_to_public( owner, recipient, @@ -56,7 +56,7 @@ unconstrained fn transfer_to_public_failure_more_than_balance() { let (env, token_contract_address, owner, _, mint_amount) = utils::setup_and_mint_to_private(/* with_account_contracts */ false); - let transfer_to_public_amount = mint_amount + 1; + let transfer_to_public_amount = mint_amount + U128::one(); Token::at(token_contract_address) .transfer_to_public(owner, owner, transfer_to_public_amount, 0) .call(&mut env.private()); @@ -68,7 +68,7 @@ unconstrained fn transfer_to_public_failure_on_behalf_of_self_non_zero_nonce() { let (env, token_contract_address, owner, _, mint_amount) = utils::setup_and_mint_to_private(/* with_account_contracts */ false); - let transfer_to_public_amount = mint_amount + 1; + let transfer_to_public_amount = mint_amount + U128::one(); Token::at(token_contract_address) .transfer_to_public(owner, owner, transfer_to_public_amount, random()) .call(&mut env.private()); @@ -79,7 +79,7 @@ unconstrained fn transfer_to_public_failure_on_behalf_of_other_more_than_balance let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_private(/* with_account_contracts */ true); - let transfer_to_public_amount = mint_amount + 1; + let transfer_to_public_amount = mint_amount + U128::one(); let transfer_to_public_call_interface = Token::at(token_contract_address).transfer_to_public( owner, recipient, @@ -102,7 +102,7 @@ unconstrained fn transfer_to_public_failure_on_behalf_of_other_invalid_designate let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_private(/* with_account_contracts */ true); - let transfer_to_public_amount = mint_amount + 1; + let transfer_to_public_amount = mint_amount + U128::one(); let transfer_to_public_call_interface = Token::at(token_contract_address).transfer_to_public( owner, recipient, @@ -125,7 +125,7 @@ unconstrained fn transfer_to_public_failure_on_behalf_of_other_no_approval() { let (env, token_contract_address, owner, recipient, mint_amount) = utils::setup_and_mint_to_private(/* with_account_contracts */ true); - let transfer_to_public_amount = mint_amount + 1; + let transfer_to_public_amount = mint_amount + U128::one(); let transfer_to_public_call_interface = Token::at(token_contract_address).transfer_to_public( owner, recipient, diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr index 838399225bf..a4244656c7f 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/utils.nr @@ -46,10 +46,10 @@ pub unconstrained fn setup( pub unconstrained fn setup_and_mint_to_public( with_account_contracts: bool, -) -> (&mut TestEnvironment, AztecAddress, AztecAddress, AztecAddress, Field) { +) -> (&mut TestEnvironment, AztecAddress, AztecAddress, AztecAddress, U128) { // Setup let (env, token_contract_address, owner, recipient) = setup(with_account_contracts); - let mint_amount = 10000; + let mint_amount: U128 = U128::from_integer(10000); // Mint some tokens Token::at(token_contract_address).mint_to_public(owner, mint_amount).call(&mut env.public()); @@ -58,8 +58,8 @@ pub unconstrained fn setup_and_mint_to_public( pub unconstrained fn setup_and_mint_amount_to_private( with_account_contracts: bool, - mint_amount: Field, -) -> (&mut TestEnvironment, AztecAddress, AztecAddress, AztecAddress, Field) { + mint_amount: U128, +) -> (&mut TestEnvironment, AztecAddress, AztecAddress, AztecAddress, U128) { // Setup the tokens and mint public balance let (env, token_contract_address, owner, recipient) = setup(with_account_contracts); @@ -71,15 +71,16 @@ pub unconstrained fn setup_and_mint_amount_to_private( pub unconstrained fn setup_and_mint_to_private( with_account_contracts: bool, -) -> (&mut TestEnvironment, AztecAddress, AztecAddress, AztecAddress, Field) { - setup_and_mint_amount_to_private(with_account_contracts, 10000) +) -> (&mut TestEnvironment, AztecAddress, AztecAddress, AztecAddress, U128) { + let mint_amount: U128 = U128::from_integer(10000); + setup_and_mint_amount_to_private(with_account_contracts, mint_amount) } pub unconstrained fn mint_to_private( env: &mut TestEnvironment, token_contract_address: AztecAddress, recipient: AztecAddress, - amount: Field, + amount: U128, ) { let note_randomness = random(); let _ = OracleMock::mock("getRandomField").returns(note_randomness); @@ -102,7 +103,7 @@ pub unconstrained fn mint_to_private( pub unconstrained fn check_public_balance( token_contract_address: AztecAddress, address: AztecAddress, - address_amount: Field, + address_amount: U128, ) { let current_contract_address = get_contract_address(); cheatcodes::set_contract_address(token_contract_address); @@ -111,7 +112,7 @@ pub unconstrained fn check_public_balance( let balances_slot = Token::storage_layout().public_balances.slot; let address_slot = derive_storage_slot_in_map(balances_slot, address); let amount: U128 = storage_read(token_contract_address, address_slot, block_number); - assert(amount.to_field() == address_amount, "Public balance is not correct"); + assert(amount == address_amount, "Public balance is not correct"); cheatcodes::set_contract_address(current_contract_address); } // docs:end:txe_test_read_public @@ -120,7 +121,7 @@ pub unconstrained fn check_public_balance( pub unconstrained fn check_private_balance( token_contract_address: AztecAddress, address: AztecAddress, - address_amount: Field, + address_amount: U128, ) { let current_contract_address = get_contract_address(); cheatcodes::set_contract_address(token_contract_address); @@ -137,7 +138,7 @@ pub unconstrained fn add_token_note( env: &mut TestEnvironment, token_contract_address: AztecAddress, owner: AztecAddress, - amount: Field, + amount: U128, note_randomness: Field, ) { // docs:start:txe_test_add_note @@ -146,7 +147,7 @@ pub unconstrained fn add_token_note( env.add_note( &mut UintNote { - value: U128::from_integer(amount), + value: amount, owner: owner, randomness: note_randomness, header: NoteHeader::empty(), diff --git a/noir-projects/noir-protocol-circuits/crates/types/src/type_serialization.nr b/noir-projects/noir-protocol-circuits/crates/types/src/type_serialization.nr index a509d6b9eef..1c0516e9e5a 100644 --- a/noir-projects/noir-protocol-circuits/crates/types/src/type_serialization.nr +++ b/noir-projects/noir-protocol-circuits/crates/types/src/type_serialization.nr @@ -5,7 +5,7 @@ global U8_SERIALIZED_LEN: u32 = 1; global U16_SERIALIZED_LEN: u32 = 1; global U32_SERIALIZED_LEN: u32 = 1; global U64_SERIALIZED_LEN: u32 = 1; -global U128_SERIALIZED_LEN: u32 = 1; +global U128_SERIALIZED_LEN: u32 = 2; global FIELD_SERIALIZED_LEN: u32 = 1; global I8_SERIALIZED_LEN: u32 = 1; global I16_SERIALIZED_LEN: u32 = 1; @@ -74,13 +74,14 @@ impl Deserialize for u64 { impl Serialize for U128 { fn serialize(self) -> [Field; U128_SERIALIZED_LEN] { - [self.to_integer()] + // We follow big endian so hi comes first + [self.hi, self.lo] } } impl Deserialize for U128 { fn deserialize(fields: [Field; U128_SERIALIZED_LEN]) -> Self { - U128::from_integer(fields[0]) + U128::from_u64s_be(fields[0] as u64, fields[1] as u64) } }