Skip to content

Commit

Permalink
feat: Implement AccountId changes in MASM
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter committed Dec 4, 2024
1 parent 41d1221 commit fab8b03
Show file tree
Hide file tree
Showing 47 changed files with 649 additions and 378 deletions.
5 changes: 3 additions & 2 deletions bin/bench-tx/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ use super::{read_to_string, write, Benchmark, Path};
// CONSTANTS
// ================================================================================================

pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN: u64 = 0x200000000000001f; // 2305843009213693983
pub const ACCOUNT_ID_SENDER: u64 = 0x800000000000001f; // 9223372036854775839
// Copied from miden_objects::accounts::account_id::testing.
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN: u128 = 0xaa00000000bb020000cc000000dd00;
pub const ACCOUNT_ID_SENDER: u128 = 0xaa00000000bb080000cc000000dd00;

pub const DEFAULT_AUTH_SCRIPT: &str = "
begin
Expand Down
28 changes: 14 additions & 14 deletions miden-lib/asm/kernels/transaction/api.masm
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ end
#! Returns the account id.
#!
#! Inputs: [pad(16)]
#! Outputs: [acct_id, pad(15)]
#! Outputs: [acct_id_hi, acct_id_lo, pad(14)]
#!
#! Where:
#! - acct_id is the account id.
Expand All @@ -83,11 +83,11 @@ end
export.get_account_id
# get the account id
exec.account::get_id
# => [acct_id, pad(16)]
# => [acct_id_hi, acct_id_lo, pad(16)]

# truncate the stack
swap drop
# => [acct_id, pad(15)]
swapw dropw
# => [acct_id_hi, acct_id_lo, pad(14)]
end

#! Returns the account nonce.
Expand Down Expand Up @@ -371,7 +371,7 @@ end

#! Returns the balance of a fungible asset associated with a faucet_id.
#!
#! Inputs: [faucet_id, pad(15)]
#! Inputs: [faucet_id_hi, faucet_id_lo, pad(14)]
#! Outputs: [balance, pad(15)]
#!
#! Where:
Expand All @@ -384,8 +384,8 @@ end
#! Invocation: dynexec
export.account_vault_get_balance
# get the vault root
exec.memory::get_acct_vault_root_ptr swap
# => [faucet_id, acct_vault_root_ptr, pad(15)]
exec.memory::get_acct_vault_root_ptr movdn.2
# => [faucet_id_hi, faucet_id_lo, acct_vault_root_ptr, pad(13)]

# get the asset balance
exec.asset_vault::get_balance
Expand Down Expand Up @@ -554,22 +554,22 @@ end
#! Returns the sender of the note currently being processed.
#!
#! Inputs: [pad(16)]
#! Outputs: [sender, pad(15)]
#! Outputs: [sender_hi, sender_lo, pad(14)]
#!
#! Where:
#! - sender is the sender of the note currently being processed.
#! - sender is the sender account id of the note currently being processed.
#!
#! Panics if:
#! - a note is not being processed.
#!
#! Invocation: dynexec
export.get_note_sender
exec.note::get_sender
# => [sender, pad(16)]
exec.note::get_sender
# => [sender_hi, sender_lo, pad(16)]

# truncate the stack
swap drop
# => [sender, pad(15)]
swapw dropw
# => [sender_hi, sender_lo, pad(14)]
end

#! Returns the block number of the last known block at the time of transaction execution.
Expand Down Expand Up @@ -818,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::is_fungible_faucet
exec.account::get_id swap drop exec.account::is_fungible_faucet
assert.err=ERR_ACCOUNT_TOTAL_ISSUANCE_PROC_CAN_ONLY_BE_CALLED_ON_FUNGIBLE_FAUCET
# => [pad(16)]

Expand Down
90 changes: 57 additions & 33 deletions miden-lib/asm/kernels/transaction/lib/account.masm
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,25 @@ const.ERR_FOREIGN_ACCOUNT_INVALID=0x00020017
# CONSTANTS
# =================================================================================================

# 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
# Given the least significant half of an account id's first felt, this mask defines the bits used
# to determine the account type.
const.ACCOUNT_TYPE_U32MASK=3 # 0b11

# Bit pattern for an account w/ immutable code, after the account type mask has been applied.
const.REGULAR_ACCOUNT_IMMUTABLE_CODE=0 # 0b00000000_00000000_00000000_00000000
const.REGULAR_ACCOUNT_IMMUTABLE_CODE=0 # 0b00

# Bit pattern for an account w/ updatable code, after the account type mask has been applied.
const.REGULAR_ACCOUNT_UPDATABLE_CODE=268435456 # 0b00010000_00000000_00000000_00000000
const.REGULAR_ACCOUNT_UPDATABLE_CODE=1 # 0b01

# Bit pattern for a fungible faucet w/ immutable code, after the account type mask has been applied.
const.FUNGIBLE_FAUCET_ACCOUNT=536870912 # 0b00100000_00000000_00000000_00000000
const.FUNGIBLE_FAUCET_ACCOUNT=2 # 0b10

# Bit pattern for a non-fungible faucet w/ immutable code, after the account type mask has been
# applied.
const.NON_FUNGIBLE_FAUCET_ACCOUNT=805306368 # 0b00110000_00000000_00000000_00000000
const.NON_FUNGIBLE_FAUCET_ACCOUNT=3 # 0b11

# Bit pattern for a faucet account, after the account type mask has been applied.
const.FAUCET_ACCOUNT=536870912 # 0b00100000_00000000_00000000_00000000
const.FAUCET_ACCOUNT=2 # 0b10

# Specifies a minimum number of ones for a valid account ID.
const.MIN_ACCOUNT_ONES=5
Expand Down Expand Up @@ -253,11 +253,11 @@ export.memory::get_init_acct_hash->get_initial_hash

#! Returns a boolean indicating whether the account is a fungible faucet.
#!
#! Inputs: [acct_id]
#! Inputs: [acct_id_hi]
#! Outputs: [is_fungible_faucet]
#!
#! Where:
#! - acct_id is the account id.
#! - acct_id_hi is the first felt of the account id.
#! - is_fungible_faucet is a boolean indicating whether the account is a fungible faucet.
export.is_fungible_faucet
exec.type push.FUNGIBLE_FAUCET_ACCOUNT eq
Expand All @@ -266,11 +266,11 @@ end

#! Returns a boolean indicating whether the account is a non-fungible faucet.
#!
#! Inputs: [acct_id]
#! Inputs: [acct_id_hi]
#! Outputs: [is_non_fungible_faucet]
#!
#! Where:
#! - acct_id is the account id.
#! - acct_id_hi is the first felt of the account id.
#! - is_non_fungible_faucet is a boolean indicating whether the account is a non-fungible faucet.
export.is_non_fungible_faucet
exec.type push.NON_FUNGIBLE_FAUCET_ACCOUNT eq
Expand All @@ -279,24 +279,24 @@ end

#! Returns a boolean indicating whether the account is a faucet.
#!
#! Inputs: [acct_id]
#! Inputs: [acct_id_hi]
#! Outputs: [is_faucet]
#!
#! Where:
#! - acct_id is the account id.
#! - acct_id_hi is the first felt of the account id.
#! - is_faucet is a boolean indicating whether the account is a faucet.
export.is_faucet
u32split swap drop push.FAUCET_ACCOUNT u32and eq.0 not
u32split drop push.FAUCET_ACCOUNT u32and eq.0 not
# => [is_faucet]
end

#! Returns a boolean indicating whether the account is a regular updatable account.
#!
#! Inputs: [acct_id]
#! Inputs: [acct_id_hi]
#! Outputs: [is_updatable_account]
#!
#! Where:
#! - acct_id is the account id.
#! - acct_id_hi is the first felt of the account id.
#! - is_updatable_account is a boolean indicating whether the account is a regular updatable
#! account.
export.is_updatable_account
Expand All @@ -306,39 +306,62 @@ end

#! Returns a boolean indicating whether the account is a regular immutable account.
#!
#! Inputs: [acct_id]
#! Inputs: [acct_id_hi]
#! Outputs: [is_immutable_account]
#!
#! Where:
#! - acct_id is the account id.
#! - acct_id_hi is the first felt of the account id.
#! - is_immutable_account is a boolean indicating whether the account is a regular immutable
#! account.
export.is_immutable_account
exec.type push.REGULAR_ACCOUNT_IMMUTABLE_CODE eq
# => [is_immutable_account]
end

#! Returns a boolean indicating whether the given account_ids are equal.
#!
#! Inputs: [acct_id_hi, acct_id_lo, other_acct_id_hi, other_acct_id_lo]
#! Outputs: [is_id_equal]
#!
#! Where:
#! - acct_id is an account id.
#! - other_acct_id is the other account id to compare against.
#! - is_id_equal is a boolean indicating whether the account ids are equal.
export.is_id_eq
movup.2 eq
# => [is_hi_equal, acct_id_lo, other_acct_id_lo]
swap movup.2 eq
# => [is_lo_equal, is_hi_equal]
and
# => [is_id_equal]
end

#! Validates an account id.
#!
#! Inputs: [acct_id]
#! Inputs: [acct_id_hi, acct_id_lo]
#! Outputs: []
#!
#! Where:
#! - acct_id is the account id.
#! - acct_id_hi is the first felt of the account id.
#! - acct_id_lo is the second felt of the account id.
#!
#! Panics if:
#! - the account id is invalid: account id must have at least `MIN_ACCOUNT_ONES` ones.
#! - account_id_hi does not have at least MIN_ACCOUNT_ONES ones.
export.validate_id
# split felt into 32 bit limbs
u32split
# => [l_1, l_0]
# => [l_1, l_0, acct_id_lo]

# count the number of 1 bits
u32popcnt swap u32popcnt add
# => [ones]
# => [ones, acct_id_lo]

# check if the number of ones is at least MIN_ACCOUNT_ONES ones.
push.MIN_ACCOUNT_ONES u32gte assert.err=ERR_ACCOUNT_INSUFFICIENT_NUMBER_OF_ONES
# => [acct_id_lo]

drop
# => []
end

#! Sets the code of the account the transaction is being executed against.
Expand All @@ -353,8 +376,8 @@ end
#! - this procedure is executed on an account whose type differs from `regular mutable`.
export.set_code
# get the account id
exec.memory::get_account_id
# => [acct_id, CODE_COMMITMENT]
exec.memory::get_account_id swap drop
# => [acct_id_hi, CODE_COMMITMENT]

# assert the account is an updatable regular account
exec.is_updatable_account assert.err=ERR_ACCOUNT_CODE_IS_NOT_UPDATABLE
Expand Down Expand Up @@ -904,19 +927,19 @@ end
#! Returns the most significant half with the account type bits masked out.
#!
#! The account type can be defined by comparing this value with the following constants:
#! - REGULAR_ACCOUNT_UPDATABLE_CODE # 0b00010000_00000000_00000000_00000000
#! - REGULAR_ACCOUNT_IMMUTABLE_CODE # 0b00000000_00000000_00000000_00000000
#! - FUNGIBLE_FAUCET_ACCOUNT # 0b00100000_00000000_00000000_00000000
#! - NON_FUNGIBLE_FAUCET_ACCOUNT # 0b00110000_00000000_00000000_00000000
#! - REGULAR_ACCOUNT_UPDATABLE_CODE
#! - REGULAR_ACCOUNT_IMMUTABLE_CODE
#! - FUNGIBLE_FAUCET_ACCOUNT
#! - NON_FUNGIBLE_FAUCET_ACCOUNT
#!
#! Inputs: [acct_id]
#! Inputs: [acct_id_hi]
#! Outputs: [acct_type]
#!
#! Where:
#! - acct_id is the account id.
#! - acct_id_hi is the first felt of the account id.
#! - acct_type is the account type.
proc.type
u32split swap drop push.ACCOUNT_TYPE_U32MASK u32and
u32split drop push.ACCOUNT_TYPE_U32MASK u32and
# => [acct_type]
end

Expand Down Expand Up @@ -1056,6 +1079,7 @@ export.validate_current_foreign_account
exec.memory::get_acct_db_root
# => [ACCOUNT_DB_ROOT]

# TODO: Update when account tree was updated.
# get the current account ID
exec.memory::get_account_id
# => [account_id, ACCOUNT_DB_ROOT]
Expand Down
Loading

0 comments on commit fab8b03

Please sign in to comment.