Skip to content

Commit

Permalink
Add Errors, re-organize felts, improve namings
Browse files Browse the repository at this point in the history
  • Loading branch information
phklive committed Oct 7, 2024
1 parent 3a0d64b commit 91dfb28
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 47 deletions.
27 changes: 14 additions & 13 deletions miden-lib/asm/kernels/transaction/lib/account.masm
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ end
#!
#! Stack: []
#! Output: []
export.validate_storage_metadata
export.validate_procedure_metadata
# get number of account procedures and number of storage slots
exec.memory::get_num_account_procedures exec.memory::get_num_storage_slots
# => [num_storage_slots, num_account_procedures]
Expand Down Expand Up @@ -372,15 +372,15 @@ export.validate_storage_metadata
if.true
# clean stack
drop drop
# => [index, num_storage_slots, get_num_account_procedures]
# => [index, num_storage_slots, num_account_procedures]
else
# assert that storage offset is in bounds
dup.2 lt assert.err=ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS
# => [index, num_storage_slots, num_account_procedures]
dup dup.3 lt assert.err=ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS
# => [storage_offset, storage_size, index, num_storage_slots, num_account_procedures]

# assert that storage limit is in bounds
add sub.1 dup.2 lt assert.err=ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS
# => [index, num_storage_slots, get_num_account_procedures]
add dup.2 lte assert.err=ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS
# => [index, num_storage_slots, num_account_procedures]
end

# check if we should continue looping
Expand All @@ -397,16 +397,17 @@ export.validate_storage_metadata
dup dup.4 lt assert.err=ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS
# => [storage_offset, storage_size, index, num_storage_slots, num_account_procedures]

# TODO: Find a way to remove this `if` statement
# assert that if size is 0 then offset is 0
dup.1 eq.0
if.true
dup eq.0 assert.err=ERR_INVALID_STORAGE_OFFSET_FOR_SIZE
end
# => [storage_offset, storage_size, index, num_storage_slots, num_account_procedures]

# assert that the storage limit is in bounds
add sub.1 dup.2 lt assert.err=ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS
# => [index, num_storage_slots, get_num_account_procedures]
# assert that storage limit is in bounds
add dup.2 lte assert.err=ERR_STORAGE_SLOT_INDEX_OUT_OF_BOUNDS
# => [index, num_storage_slots, num_account_procedures]

# check if we should continue looping
add.1 dup dup.3 lt
Expand Down Expand Up @@ -593,8 +594,8 @@ export.get_procedure_info
padw movup.4 mem_loadw padw movup.8 mem_loadw
# => [METADATA, PROC_ROOT]

# keep only relevant data
swap drop swap drop swap movdn.5 movdn.5
# keep relevant data
drop drop swap movdn.5 movdn.5
# => [PROC_ROOT, storage_offset, storage_size]
end

Expand Down Expand Up @@ -753,7 +754,7 @@ proc.get_procedure_metadata
mul.2 exec.memory::get_acct_procedures_section_ptr add add.1
# => [storage_offset_ptr]

# load procedure storage offset from memory and keep relevant data
padw movup.4 mem_loadw swap drop swap drop swap
# load procedure metadata from memory and keep relevant data
padw movup.4 mem_loadw drop drop swap
# => [storage_offset, storage_size]
end
4 changes: 2 additions & 2 deletions miden-lib/asm/kernels/transaction/lib/prologue.masm
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,9 @@ proc.validate_new_account
exec.account::validate_seed
# => []

# Assert the provided storage offsets and sizes satisfy storage requirements
# Assert the provided procedures offsets and sizes satisfy storage requirements
# ---------------------------------------------------------------------------------------------
exec.account::validate_storage_metadata
exec.account::validate_procedure_metadata
# => []
end

Expand Down
24 changes: 12 additions & 12 deletions miden-lib/src/transaction/procedures/kernel_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ use miden_objects::{digest, Digest, Felt};
/// Hashes of all dynamically executed procedures from the kernel 0.
pub const KERNEL0_PROCEDURES: [Digest; 32] = [
// account_vault_add_asset
digest!(0x7215093ef8c739ae, 0xa2744b83f88eb2e, 0x4bca2840de404d50, 0x267ca24ec412e967),
digest!(0x77365035d901b352, 0x85d8042000096df, 0xa8531ec691f24d17, 0xc67a8fd2677bf558),
// account_vault_get_balance
digest!(0x92b81d20684fa47, 0x4920ee53425609b9, 0x2f8c32c56898141c, 0x9e4542839e34452f),
// account_vault_has_non_fungible_asset
digest!(0x1b1e6ec92fabca80, 0xbb3847ce15f98cac, 0x7152391739b5e0b3, 0x696aaf2c879c4fde),
// account_vault_remove_asset
digest!(0x82c1762488a5fa06, 0x6d64bc2b046147ae, 0x89dec46bcea59bbd, 0xe0f03be7ffc4dcc3),
digest!(0xdf93ea4374fe098f, 0x63df56e7578d9661, 0xc5d3b1958456cc5, 0xbfeec68c1c6b4ca9),
// get_account_id
digest!(0x386549d4435f79c1, 0x4a7add2e3b9f1b9e, 0x91c0af1138c14e77, 0xee8a5630e31bc74d),
// get_account_item
digest!(0x614250d8c36af706, 0x46d39fb65480d1f3, 0xe0ebb7d5f46a6f32, 0x2bc18e17712bbbc5),
digest!(0x83380522a33f8c7e, 0x1653bbd634d31107, 0x868fac07b1cb4005, 0x39bee294dac7fdc9),
// get_account_map_item
digest!(0xe055cca34d15fc7f, 0x815734bce550acd4, 0x50a827f81176640b, 0xb426738c7e29fb23),
digest!(0xdf739f276157cf90, 0x4c94a55654d426b, 0xff2528216462fa83, 0x45797577ddc9a224),
// get_account_nonce
digest!(0x64d14d80f9eff37a, 0x7587e273b2d8a416, 0x3c041064332c03d3, 0xc327341072f4f1e8),
// get_account_vault_commitment
Expand All @@ -30,23 +30,23 @@ pub const KERNEL0_PROCEDURES: [Digest; 32] = [
// get_initial_account_hash
digest!(0xe239391d2c860c53, 0x7a9d09c3015d7417, 0x111e9be3640d3848, 0xf2d442cf1e685a89),
// incr_account_nonce
digest!(0xaa2904a4bec929ca, 0xc66e25b357383da8, 0x959f72ae1af83a30, 0x479718e0d289c3e),
digest!(0x6d75402ead2fe81c, 0x6e66c9ec980ec9cd, 0xe82e007b0eda78f1, 0xea9de83af0fc2634),
// set_account_code
digest!(0x90bc1f541f7adc63, 0xffa3daf2197fe496, 0xc72c5cedeb3482b, 0x5d6eac8e22abda40),
digest!(0x62110f0b57e49ee5, 0xd961174262cd614a, 0x3459572bcf110091, 0x319291c6c18ad0db),
// set_account_item
digest!(0xe77cd2a1c02ad66a, 0xa18d96ecd20c7ca8, 0x7114ec61e4db0bea, 0xe6b97475f1f4dcbc),
digest!(0xc279aa203249464, 0x464f69a21be47e7a, 0xb9161aaee45f0ff5, 0xbca81ff227c9ca03),
// set_account_map_item
digest!(0x49092f6ea0d561f, 0x11528bb53882af83, 0x228c1352560481a, 0x79667f86e9a32dd),
digest!(0x85c7e78d8e33f81, 0x2392bd80e65f27a7, 0x69d4d656a994dd2c, 0xcb9be97522be5cf4),
// burn_asset
digest!(0x58e53cf050c1218e, 0x498f9b3f9904c03f, 0xbc341b7737247115, 0x3ea366d3bc90fe32),
digest!(0x3c71836eaa5fba1b, 0xee719bcada360cd1, 0xad55420b925fd10d, 0x4d32e15e121e5e3e),
// get_fungible_faucet_total_issuance
digest!(0xd9310aaf087d0dc4, 0xdc834fff6ea325d2, 0x2c9d90a33b9a6d8a, 0xa381c27e49c538a8),
// mint_asset
digest!(0xf1f416bd8fa21c94, 0x35c470f8c7a1eb1b, 0xc8c0b0b497dfe7a7, 0xea434f9afeb44bc1),
digest!(0x715eae96f4068cf1, 0x84ee32a7c64a85dd, 0x9b4d5a63fbd97064, 0xef0e81abf63aa2be),
// add_asset_to_note
digest!(0x88f847f7b2f8f5f6, 0x301fa80970aea476, 0x1a09aa5379870f6, 0x76f121f8d5db2a0d),
digest!(0x9fbed6f52f2cc62d, 0xda9c2f699fac16fb, 0xeb6b8827beac6c95, 0xe27fc6900c673e2d),
// create_note
digest!(0xd6ac7c95ba08b35e, 0x1ad1759445a51a95, 0x7cd321aa88e80729, 0x7804e81109ddc342),
digest!(0xa9e52dd343a6fa1d, 0xa54d666e10f34357, 0x7c53cc941096bd84, 0xe601314453890dfc),
// get_input_notes_commitment
digest!(0x1c078486abf976f5, 0xfce31a9f4b9687cd, 0xb1edb2edc115a619, 0xf1bb8c1bd9c7148b),
// get_note_assets_info
Expand Down
7 changes: 4 additions & 3 deletions objects/src/accounts/code/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ impl AccountCode {
/// location 0.
///
/// # Errors
/// - If the number of procedures exported from the provided library is smaller than 1 or
/// greater than 256.
/// - If the number of procedures exported from the provided library is 0.
/// - If the number of procedures exported from the provided library is greater than 256.
/// - If the creation of a new `AccountProcedureInfo` fails.
pub fn new(library: Library, is_faucet: bool) -> Result<Self, AccountError> {
// extract procedure information from the library exports
// TODO: currently, offsets for all regular account procedures are set to 0
// and offsets for faucet accounts procedures are set to 1. Furthermore sizes
// are set to 1 for all accounts. Instead they should be read from the Library metadata.
let mut procedures: Vec<AccountProcedureInfo> = Vec::new();
let mut procedures = Vec::new();
let storage_offset = if is_faucet { 1 } else { 0 };
let storage_size = 1;
for module in library.module_infos() {
Expand Down
34 changes: 19 additions & 15 deletions objects/src/accounts/code/procedure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use vm_core::{
};
use vm_processor::DeserializationError;

use super::{AccountCode, Digest, Felt};
use crate::AccountError;
use super::{Digest, Felt};
use crate::{accounts::AccountStorage, AccountError};

// ACCOUNT PROCEDURE INFO
// ================================================================================================
Expand Down Expand Up @@ -43,19 +43,23 @@ impl AccountProcedureInfo {

/// Returns a new instance of an [AccountProcedureInfo].
///
/// # Panics
/// Panics if `storage_size` is 0 and `storage_offset` is not 0.
/// # Errors
/// - If `storage_size` is 0 and `storage_offset` is not 0.
/// - If `storage_size + storage_offset` is greater than `MAX_NUM_STORAGE_SLOTS`.
pub fn new(
mast_root: Digest,
storage_offset: u8,
storage_size: u8,
) -> Result<Self, AccountError> {
if storage_size == 0 && storage_offset != 0 {
return Err(AccountError::ProcedureNotAccessingStorageHasOffsets);
return Err(AccountError::PureProcedureWithStorageOffset);
}

if (storage_offset + storage_size) as usize > AccountCode::MAX_NUM_PROCEDURES {
return Err(AccountError::StorageLimitOutOfBounds);
if (storage_offset + storage_size) as usize > AccountStorage::MAX_NUM_STORAGE_SLOTS {
return Err(AccountError::StorageOffsetOutOfBounds {
max: AccountStorage::MAX_NUM_STORAGE_SLOTS as u8,
actual: storage_offset + storage_size,
});
}

Ok(Self { mast_root, storage_offset, storage_size })
Expand Down Expand Up @@ -91,7 +95,7 @@ impl From<AccountProcedureInfo> for [Felt; 8] {
result[4] = Felt::from(value.storage_offset);

// copy the storage size into value[7]
result[7] = Felt::from(value.storage_size);
result[5] = Felt::from(value.storage_size);

result
}
Expand All @@ -109,16 +113,16 @@ impl TryFrom<[Felt; 8]> for AccountProcedureInfo {
.try_into()
.map_err(|_| AccountError::AccountCodeProcedureInvalidStorageOffset)?;

// Check if the next two elements are zero
if value[5] != Felt::ZERO || value[6] != Felt::ZERO {
return Err(AccountError::AccountCodeProcedureInvalidPadding);
}

// get storage_size form value[7]
let storage_size: u8 = value[7]
// get storage_size form value[5]
let storage_size: u8 = value[5]
.try_into()
.map_err(|_| AccountError::AccountCodeProcedureInvalidStorageSize)?;

// Check if the remaining values are 0
if value[6] != Felt::ZERO || value[7] != Felt::ZERO {
return Err(AccountError::AccountCodeProcedureInvalidPadding);
}

Ok(Self { mast_root, storage_offset, storage_size })
}
}
Expand Down
4 changes: 2 additions & 2 deletions objects/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ pub enum AccountError {
StorageSlotNotValue(u8),
StorageIndexOutOfBounds { max: u8, actual: u8 },
StorageTooManySlots(u64),
StorageLimitOutOfBounds,
ProcedureNotAccessingStorageHasOffsets,
StorageOffsetOutOfBounds { max: u8, actual: u8 },
PureProcedureWithStorageOffset,
}

impl fmt::Display for AccountError {
Expand Down

0 comments on commit 91dfb28

Please sign in to comment.