Skip to content

Commit

Permalink
feat: utils module working state
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Dec 18, 2024
1 parent c6e0622 commit 4454352
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 124 deletions.
4 changes: 1 addition & 3 deletions miden-lib/asm/kernels/transaction/api.masm
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use.std::collections::smt
use.std::sys

use.utils::account->account_utils

use.kernel::account
use.kernel::asset_vault
use.kernel::constants
Expand Down Expand Up @@ -820,7 +818,7 @@ end
#! Invocation: dynexec
export.get_fungible_faucet_total_issuance
# assert that we are executing a transaction against a fungible faucet (access checks)
exec.account::get_id exec.account_utils::is_fungible_faucet
exec.account::get_id exec.account::is_fungible_faucet
assert.err=ERR_ACCOUNT_TOTAL_ISSUANCE_PROC_CAN_ONLY_BE_CALLED_ON_FUNGIBLE_FAUCET
# => [pad(16)]

Expand Down
3 changes: 3 additions & 0 deletions miden-lib/asm/kernels/transaction/lib/account.masm
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ export.memory::get_acct_nonce->get_nonce
#! - INIT_HASH is the initial account hash.
export.memory::get_init_acct_hash->get_initial_hash

export.::utils::utils::is_fungible_faucet
export.::utils::utils::is_non_fungible_faucet

#! Returns a boolean indicating whether the account is a faucet.
#!
#! Inputs: [acct_id]
Expand Down
8 changes: 4 additions & 4 deletions miden-lib/asm/kernels/transaction/lib/asset.masm
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use.utils::account->account_utils

use.kernel::account

# ERRORS
Expand Down Expand Up @@ -38,6 +36,8 @@ const.FUNGIBLE_ASSET_MAX_AMOUNT=9223372036854775807
# is used to identify the asset type
const.FUNGIBLE_BITMASK_U32=0x20000000

export.::utils::utils::get_fungible_asset_max_amount

# PROCEDURES
# =================================================================================================

Expand Down Expand Up @@ -65,7 +65,7 @@ export.validate_fungible_asset
# => [ASSET]

# assert that ASSET[3] is a fungible faucet
dup exec.account_utils::is_fungible_faucet
dup exec.account::is_fungible_faucet
assert.err=ERR_FUNGIBLE_ASSET_FORMAT_ELEMENT_THREE_MUST_BE_FUNGIBLE_FAUCET_ID
# => [ASSET]

Expand Down Expand Up @@ -107,7 +107,7 @@ export.validate_non_fungible_asset
# => [ASSET]

# assert that ASSET[1] is a fungible faucet
dup.2 exec.account_utils::is_non_fungible_faucet
dup.2 exec.account::is_non_fungible_faucet
assert.err=ERR_NON_FUNGIBLE_ASSET_FORMAT_ELEMENT_ONE_MUST_BE_FUNGIBLE_FAUCET_ID
# => [ASSET]

Expand Down
7 changes: 2 additions & 5 deletions miden-lib/asm/kernels/transaction/lib/asset_vault.masm
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use.std::collections::smt

use.utils::asset->asset_utils
use.utils::account->account_utils

use.kernel::account
use.kernel::asset
use.kernel::memory
Expand Down Expand Up @@ -51,7 +48,7 @@ const.ERR_VAULT_NON_FUNGIBLE_ASSET_TO_REMOVE_NOT_FOUND=0x0002001F
#! - the asset is not a fungible asset.
export.get_balance
# assert that the faucet id is a fungible faucet
dup exec.account_utils::is_fungible_faucet
dup exec.account::is_fungible_faucet
assert.err=ERR_VAULT_GET_BALANCE_PROC_CAN_ONLY_BE_CALLED_ON_FUNGIBLE_FAUCET
# => [faucet_id, vault_root_ptr]

Expand Down Expand Up @@ -140,7 +137,7 @@ export.add_fungible_asset
# => [amount, amount, cur_amount, faucet_id, 0, 0, VAULT_ROOT, CUR_VAULT_VALUE, vault_root_ptr]

# compute max_amount - cur_amount
exec.asset_utils::get_fungible_asset_max_amount dup.3 sub
exec.asset::get_fungible_asset_max_amount dup.3 sub
# => [(max_amount - cur_amount), amount, amount, cur_amount, faucet_id, 0, 0, VAULT_ROOT,
# CUR_VAULT_VALUE, vault_root_ptr]

Expand Down
2 changes: 2 additions & 0 deletions miden-lib/asm/kernels/transaction/lib/constants.masm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export.get_word_size
push.WORD_SIZE
end

export.::utils::utils::get_max_inputs_per_note

#! Returns the max allowed number of assets per note.
#!
#! Inputs: []
Expand Down
7 changes: 2 additions & 5 deletions miden-lib/asm/kernels/transaction/lib/faucet.masm
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use.std::collections::smt

use.utils::account->account_utils
use.utils::asset->asset_utils

use.kernel::account
use.kernel::asset
use.kernel::asset_vault
Expand Down Expand Up @@ -56,7 +53,7 @@ export.mint_fungible_asset
# => [TOTAL_ISSUANCE, ASSET]

# prepare stack to ensure that minting the asset will not exceed the maximum
dup.7 dup exec.asset_utils::get_fungible_asset_max_amount dup.3
dup.7 dup exec.asset::get_fungible_asset_max_amount dup.3
# => [total_issuance, max_allowed_issuance, amount, amount, TOTAL_ISSUANCE, ASSET]

# compute difference to ensure that the total issuance will not exceed the maximum
Expand Down Expand Up @@ -194,7 +191,7 @@ end
#! transaction via a note or the accounts vault.
proc.burn_non_fungible_asset
# assert that we are executing a transaction against the non-fungible faucet (access checks)
exec.account::get_id exec.account_utils::is_non_fungible_faucet
exec.account::get_id exec.account::is_non_fungible_faucet
assert.err=ERR_FAUCET_BURN_NON_FUNGIBLE_ASSET_CAN_ONLY_BE_CALLED_ON_NON_FUNGIBLE_FAUCET
# => [ASSET]

Expand Down
4 changes: 1 addition & 3 deletions miden-lib/asm/kernels/transaction/lib/prologue.masm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use.std::mem
use.std::collections::mmr
use.std::crypto::hashes::rpo

use.utils::account->account_utils

use.kernel::account
use.kernel::asset_vault
use.kernel::constants
Expand Down Expand Up @@ -348,7 +346,7 @@ proc.validate_new_account
# => [FAUCET_RESERVED_SLOT, acct_id]

# check if the account is a fungible faucet
movup.4 exec.account_utils::is_fungible_faucet
movup.4 exec.account::is_fungible_faucet
# => [is_fungible_faucet, FAUCET_RESERVED_SLOT]

if.true
Expand Down
4 changes: 1 addition & 3 deletions miden-lib/asm/kernels/transaction/lib/tx.masm
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use.utils::asset->asset_utils

use.kernel::account
use.kernel::asset
use.kernel::constants
Expand Down Expand Up @@ -310,7 +308,7 @@ proc.add_fungible_asset_to_note
# num_of_assets, note_idx]

# check that we don't overflow bc we use lte
dup exec.asset_utils::get_fungible_asset_max_amount lte
dup exec.asset::get_fungible_asset_max_amount lte
assert.err=ERR_NOTE_FUNGIBLE_MAX_AMOUNT_EXCEEDED
# => [updated_amount, 0, 0, faucet_id, 0, 0, end_asset_ptr, asset_ptr, note_ptr,
# num_of_assets, note_idx]
Expand Down
6 changes: 6 additions & 0 deletions miden-lib/asm/miden/account.masm
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use.miden::kernel_proc_offsets

# REEXPORTED PROCEDURES
# =================================================================================================

export.::utils::utils::is_fungible_faucet
export.::utils::utils::is_non_fungible_faucet

# NATIVE ACCOUNT PROCEDURES
# =================================================================================================

Expand Down
11 changes: 5 additions & 6 deletions miden-lib/asm/miden/asset.masm
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use.utils::account->account_utils
use.utils::asset->asset_utils

use.miden::account

# ERRORS
Expand All @@ -27,6 +24,8 @@ const.FUNGIBLE_BITMASK_U32=0x20000000
# PROCEDURES
# =================================================================================================

export.::utils::utils::get_fungible_asset_max_amount

#! Builds a fungible asset for the specified fungible faucet and amount.
#!
#! Inputs: [faucet_id, amount]
Expand All @@ -40,11 +39,11 @@ const.FUNGIBLE_BITMASK_U32=0x20000000
#! Annotation hint: is not used anywhere except this file
export.build_fungible_asset
# assert the faucet is a fungible faucet
dup exec.account_utils::is_fungible_faucet assert.err=ERR_FUNGIBLE_ASSET_PROVIDED_FAUCET_ID_IS_INVALID
dup exec.account::is_fungible_faucet assert.err=ERR_FUNGIBLE_ASSET_PROVIDED_FAUCET_ID_IS_INVALID
# => [faucet_id, amount]

# assert the amount is valid
dup.1 exec.asset_utils::get_fungible_asset_max_amount lte
dup.1 exec.get_fungible_asset_max_amount lte
assert.err=ERR_FUNGIBLE_ASSET_AMOUNT_EXCEEDS_MAX_ALLOWED_AMOUNT
# => [faucet_id, amount]

Expand Down Expand Up @@ -86,7 +85,7 @@ end
#! Annotation hint: is not used anywhere except this file
export.build_non_fungible_asset
# assert the faucet is a non-fungible faucet
dup exec.account_utils::is_non_fungible_faucet
dup exec.account::is_non_fungible_faucet
assert.err=ERR_NON_FUNGIBLE_ASSET_PROVIDED_FAUCET_ID_IS_INVALID
# => [faucet_id, DATA_HASH]

Expand Down
10 changes: 7 additions & 3 deletions miden-lib/asm/miden/note.masm
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use.std::crypto::hashes::rpo
use.std::mem

use.utils::note->note_utils

use.miden::kernel_proc_offsets

# ERRORS
Expand All @@ -14,6 +12,12 @@ const.ERR_NOTE_DATA_DOES_NOT_MATCH_COMMITMENT=0x0002004E
# Number of note inputs exceeded the maximum limit of 128
const.ERR_PROLOGUE_NUMBER_OF_NOTE_INPUTS_EXCEEDED_LIMIT=0x0002004F


# PROCEDURES
# =================================================================================================

export.::utils::utils::get_max_inputs_per_note

#! Writes the data currently on the advice stack into the memory at the specified location and
#! verifies that the hash of the written data is equal to the provided hash.
#!
Expand Down Expand Up @@ -123,7 +127,7 @@ export.get_inputs
# => [num_inputs, INPUTS_HASH, dest_ptr]

# validate the input length
dup exec.note_utils::get_max_inputs_per_note lte
dup exec.get_max_inputs_per_note lte
assert.err=ERR_PROLOGUE_NUMBER_OF_NOTE_INPUTS_EXCEEDED_LIMIT
# => [num_inputs, INPUTS_HASH, dest_ptr]

Expand Down
12 changes: 0 additions & 12 deletions miden-lib/asm/utils/asset.masm

This file was deleted.

12 changes: 0 additions & 12 deletions miden-lib/asm/utils/note.masm

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
### ACCOUNT #########################################################

# Given the most significant half of an account id, this mask defines the bits used to determine the
# account type.
const.ACCOUNT_TYPE_U32MASK=805306368 # 0b00110000_00000000_00000000_00000000
Expand Down Expand Up @@ -51,3 +53,33 @@ export.is_non_fungible_faucet
exec.type push.NON_FUNGIBLE_FAUCET_ACCOUNT eq
# => [is_non_fungible_faucet]
end

### ASSET ###########################################################

const.FUNGIBLE_ASSET_MAX_AMOUNT=9223372036854775807

#! Returns the maximum amount of a fungible asset.
#!
#! Stack: []
#! Outputs: [fungible_asset_max_amount]
#!
#! fungible_asset_max_amount is the maximum amount of a fungible asset.
export.get_fungible_asset_max_amount
push.FUNGIBLE_ASSET_MAX_AMOUNT
# => [fungible_asset_max_amount]
end

### NOTE ############################################################

# The maximum number of input values associated with a single note.
const.MAX_INPUTS_PER_NOTE=128

#! Returns the max allowed number of input values per note.
#!
#! Stack: []
#! Output: [max_inputs_per_note]
#!
#! - max_inputs_per_note is the max inputs per note.
export.get_max_inputs_per_note
push.MAX_INPUTS_PER_NOTE
end
Loading

0 comments on commit 4454352

Please sign in to comment.