Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set to 1 all faucet account storage offsets #875

Merged
merged 16 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
- [BREAKING] `AccountStorageType` enum was renamed to `AccountStorageMode` along with its variants (#854).
- [BREAKING] `AccountStub` structure was renamed to `AccountHeader` (#855).
- [BREAKING] Kernel procedures now have to be invoked using `dynexec` instruction (#803).
- Refactored `AccountStorage` from `Smt` to `sequential hash` (#846)
- Refactored `AccountStorage` from `Smt` to `sequential hash` (#846).
- Set all procedures storage offsets of faucet accounts to `1` (#875).

## 0.5.1 (2024-08-28) - `miden-objects` crate only

Expand Down
78 changes: 39 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bench-tx/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn get_account_with_default_account_code(
let account_code_src = DEFAULT_ACCOUNT_CODE;
let assembler = TransactionKernel::assembler();

let account_code = AccountCode::compile(account_code_src, assembler).unwrap();
let account_code = AccountCode::compile(account_code_src, assembler, false).unwrap();
let account_storage = AccountStorage::new(vec![StorageSlot::Value(public_key)]).unwrap();

let account_vault = match assets {
Expand Down
111 changes: 106 additions & 5 deletions miden-lib/asm/kernels/transaction/lib/account.masm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const.ERR_PROC_INDEX_OUT_OF_BOUNDS=0x0002004B
# Provided storage slot index is out of bounds
const.ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS=0x0002004E

# Storage offset is invalid for a faucet account (0 is prohibited being the faucet reserved data slot)
const.ERR_INVALID_FAUCET_STORAGE_OFFSET=0x0002004F

# CONSTANTS
# =================================================================================================

Expand Down Expand Up @@ -341,6 +344,81 @@ export.apply_storage_offset
# => [offset_slot_index]
end

#! Validates all account procedures storage offsets
phklive marked this conversation as resolved.
Show resolved Hide resolved
#!
#! Notes:
#! - For faucets checks that no storage offset is 0 (preventing reserved storage slot access)
#!
#! Stack: []
#! Output: []
export.validate_storage_offsets
# get number of account procedures
exec.memory::get_num_procedures
# => [num_account_procedures]

# prepare stack for looping
push.0.1
# => [start_loop, index, num_account_procedures]

# check if the account is a faucet
exec.get_id exec.is_faucet
# => [is_faucet, start_loop, index, num_account_procedures]

# we do not check if num_account_procedures == 0 here because a valid
# account has between 1 and 256 procedures with associated offsets
if.true
while.true
# get storage offset from memory
dup exec.get_procedure_storage_offset
# => [storage_offset, index, num_account_procedures]

# assert that storage offset is not 0
dup push.0 neq assert.err=ERR_INVALID_FAUCET_STORAGE_OFFSET
# => [storage_offset, index, num_account_procedures]

# TODO: This is a temporary check for faucets. We add 1 to all faucet offsets
# to prevent access to the reserved faucet data slot. When the assembler
# will support storage offsets remove this check.
# check if the storage offset and the number of storage slots are 1
dup exec.memory::get_num_storage_slots eq.1 swap eq.1 and
# => [not_both_1, storage_offset, index, num_account_procedures]
phklive marked this conversation as resolved.
Show resolved Hide resolved

if.false
# assert that storage offset is in bounds
exec.memory::get_num_storage_slots lt assert.err=ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS
# => [index, num_account_procedures]
else
# TODO: Remove this drop with the check above
# skip bound check and drop storage_offset
drop
# => [index, num_account_procedures]
end

# check if we should continue looping
add.1 dup dup.2 lt
# => [should_loop, index, num_account_procedures]
end
else
while.true
# get storage offset from memory
dup exec.get_procedure_storage_offset
# => [storage_offset, index, num_account_procedures]

# assert that storage offset is in bounds
exec.memory::get_num_storage_slots lt assert.err=ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS
phklive marked this conversation as resolved.
Show resolved Hide resolved
# => [index, num_account_procedures]

# check if we should continue looping
add.1 dup dup.2 lt
# => [should_loop, index, num_account_procedures]
end
end

# clean stack
drop drop
# => []
end

#! Gets an item from the account storage
#!
#! Note:
Expand Down Expand Up @@ -533,17 +611,40 @@ end
#! - index is out of bounds
export.get_procedure_info
# check that index < number of procedures contained in the account code
dup exec.memory::get_num_account_procedures lt assert.err=ERR_PROC_INDEX_OUT_OF_BOUNDS
dup exec.memory::get_num_procedures lt assert.err=ERR_PROC_INDEX_OUT_OF_BOUNDS
# => [index]

# get procedure section ptr
push.2 mul exec.memory::get_acct_procedures_section_offset add dup push.1 add
# => [proc_ptr, offset_ptr]
# get procedure pointer
mul.2 exec.memory::get_acct_procedures_section_offset add dup add.1
# => [offset_ptr, proc_ptr]

# load procedure information from memory
mem_load swap padw movup.4 mem_loadw
# => [PROC_ROOT, storage_offset]
end
end

#! Returns the procedure storage offset
#!
#! Stack: [index, ...]
#! Output: [storage_offset, ...]
#!
#! - storage_offset is the procedure storage offset.
#!
#! Panics if
#! - index is out of bounds
export.get_procedure_storage_offset
# check that index < number of procedures contained in the account code
dup exec.memory::get_num_procedures lt assert.err=ERR_PROC_INDEX_OUT_OF_BOUNDS
# => [index]

# get procedure storage offset pointer
mul.2 exec.memory::get_acct_procedures_section_offset add add.1
# => [storage_offset_ptr]

# load procedure storage offset from memory
mem_load
# => [storage_offset]
end
phklive marked this conversation as resolved.
Show resolved Hide resolved

#! Verifies that the procedure root is part of the account code
#!
Expand Down
2 changes: 1 addition & 1 deletion miden-lib/asm/kernels/transaction/lib/memory.masm
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ end
#!
#! Where:
#! - num_procedures is the number of procedures contained in the account code.
export.get_num_account_procedures
export.get_num_procedures
push.NUM_ACCT_PROCEDURES_PTR mem_load
end
phklive marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Loading
Loading