Skip to content

Commit

Permalink
Account code_root to code_commitment second pass
Browse files Browse the repository at this point in the history
  • Loading branch information
phklive committed Jul 23, 2024
1 parent 3bd86a7 commit cc7303d
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 155 deletions.
89 changes: 43 additions & 46 deletions docs/architecture/transactions/procedures.md

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions miden-lib/asm/kernels/transaction/api.masm
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,18 @@ end
#! Sets the code of the account the transaction is being executed against. This procedure can only
#! executed on regular accounts with updatable code. Otherwise, this procedure fails.
#!
#! Stack: [CODE_ROOT]
#! Stack: [CODE_COMMITMENT]
#! Output: [0, 0, 0, 0]
#!
#! - CODE_ROOT is the hash of the code to set.
#! - CODE_COMMITMENT is the hash of the code to set.
export.set_account_code
# authenticate that the procedure invocation originates from the account context
exec.authenticate_account_origin
# => [CODE_ROOT]
# => [CODE_COMMITMENT]

# arrange stack
padw swapw
# => [CODE_ROOT, 0, 0, 0, 0]
# => [CODE_COMMITMENT, 0, 0, 0, 0]

# set the account code
exec.account::set_code
Expand Down
4 changes: 2 additions & 2 deletions miden-lib/asm/miden/account.masm
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ end
#! Sets the code of the account the transaction is being executed against. This procedure can only
#! executed on regular accounts with updatable code. Otherwise, this procedure fails.
#!
#! Stack: [CODE_ROOT]
#! Stack: [CODE_COMMITMENT]
#! Output: []
#!
#! - CODE_ROOT is the hash of the code to set.
#! - CODE_COMMITMENT is the hash of the code to set.
export.set_code
syscall.set_account_code
# => [0, 0, 0, 0]
Expand Down
22 changes: 11 additions & 11 deletions miden-lib/asm/miden/kernels/tx/account.masm
Original file line number Diff line number Diff line change
Expand Up @@ -299,21 +299,21 @@ end
#! Sets the code of the account the transaction is being executed against. This procedure can only
#! executed on regular accounts with updatable code. Otherwise, this procedure fails.
#!
#! Stack: [CODE_ROOT]
#! Stack: [CODE_COMMITMENT]
#! Output: []
#!
#! - CODE_ROOT is the hash of the code to set.
#! - CODE_COMMITMENT is the hash of the code to set.
export.set_code
# get the account id
exec.memory::get_acct_id
# => [acct_id, CODE_ROOT]
# => [acct_id, CODE_COMMITMENT]

# assert the account is an updatable regular account
exec.is_updatable_account assert.err=ERR_ACCOUNT_SET_CODE_ACCOUNT_MUST_BE_UPDATABLE
# => [CODE_ROOT]
# => [CODE_COMMITMENT]

# set the code root
exec.memory::set_new_acct_code_root dropw
# set the code commitment
exec.memory::set_new_acct_code_commitment dropw
# => []
end

Expand Down Expand Up @@ -515,7 +515,7 @@ end
#! Validates that the account seed, provided via the advice map, satisfies the seed requirements.
#!
#! Validation is performed via the following steps:
#! 1. compute the hash of (SEED, CODE_ROOT, STORAGE_ROOT, 0, 0, 0, 0)
#! 1. compute the hash of (SEED, CODE_commitment, STORAGE_ROOT, 0, 0, 0, 0)
#! 2. Assert the least significant element of the digest is equal to the account id of the account
#! the transaction is being executed against.
#! 3. Assert the most significant element has sufficient proof of work (trailing zeros) for the account
Expand All @@ -528,11 +528,11 @@ export.validate_seed
padw exec.memory::get_acct_id push.0.0.0 adv.push_mapval adv_loadw
# => [SEED, 0, 0, 0, 0]

# populate last four elements of the hasher rate with the code root
exec.memory::get_acct_code_root
# => [CODE_ROOT, SEED, 0, 0, 0, 0]
# populate last four elements of the hasher rate with the code commitment
exec.memory::get_acct_code_commitment
# => [CODE_COMMITMENT, SEED, 0, 0, 0, 0]

# perform first permutation of seed and code_root (from advice stack) perm(seed, code_root)
# perform first permutation of seed and code_commitment (from advice stack) perm(seed, code_commitment)
hperm
# => [RATE, RATE, PERM]

Expand Down
16 changes: 8 additions & 8 deletions miden-lib/asm/miden/kernels/tx/epilogue.masm
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,20 @@ end
# ACCOUNT CODE UPDATE
# =================================================================================================

#! Updates the account code root if the account code has changed. `NEW_ACCT_CODE_ROOT` is set to
#! the initial account code root in the prologue and as such this procedure will not result in a
#! change to the account code root if the `account::set_code` procedure has not been invoked in
#! Updates the account code commitment if the account code has changed. `NEW_ACCT_CODE_COMMITMENT` is set to
#! the initial account code commitment in the prologue and as such this procedure will not result in a
#! change to the account code commitment if the `account::set_code` procedure has not been invoked in
#! this transaction.
#!
#! Stack: []
#! Output: []
proc.update_account_code
# check if the account code root has been updated
exec.memory::get_new_acct_code_root
# => [NEW_ACCT_CODE_ROOT]
# check if the account code commitment has been updated
exec.memory::get_new_acct_code_commitment
# => [NEW_ACCT_CODE_COMMITMENT]

# set the account code root to the new account code root (may not have changed)
exec.memory::set_acct_code_root dropw
# set the account code commitment to the new account code commitment (may not have changed)
exec.memory::set_acct_code_commitment dropw
# => []
end

Expand Down
52 changes: 26 additions & 26 deletions miden-lib/asm/miden/kernels/tx/memory.masm
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ const.ACCT_VAULT_ROOT_PTR=401
# The memory address at which the account storage root is stored
const.ACCT_STORAGE_ROOT_PTR=402

# The memory address at which the account code root is stored
const.ACCT_CODE_ROOT_PTR=403
# The memory address at which the account code commitment is stored
const.ACCT_CODE_COMMITMENT_PTR=403

# The memory address at which the new account code root is stored
const.ACCT_NEW_CODE_ROOT_PTR=404
# The memory address at which the new account code commitment is stored
const.ACCT_NEW_CODE_COMMITMENT_PTR=404

# The memory offset at which the account data section ends (exclusive)
const.ACCT_CORE_DATA_SECTION_END_OFFSET=404
Expand Down Expand Up @@ -658,50 +658,50 @@ export.get_account_procedures_section_offset
push.ACCT_PROCEDURES_SECTION_OFFSET
end

#! Sets the code root of the account.
#! Sets the code commitment of the account.
#!
#! Stack: [CODE_ROOT]
#! Output: [CODE_ROOT]
#! Stack: [CODE_COMMITMENT]
#! Output: [CODE_COMMITMENT]
#!
#! Where:
#! - CODE_ROOT is the code root to be set.
export.set_acct_code_root
push.ACCT_CODE_ROOT_PTR
#! - CODE_COMMITMENT is the code commitment to be set.
export.set_acct_code_commitment
push.ACCT_CODE_COMMITMENT_PTR
mem_storew
end

#! Returns the code root of the account.
#! Returns the code commitment of the account.
#!
#! Stack: []
#! Output: [CODE_ROOT]
#! Output: [CODE_COMMITMENT]
#!
#! Where:
#! - CODE_ROOT is the code root of the account.
export.get_acct_code_root
padw push.ACCT_CODE_ROOT_PTR mem_loadw
#! - CODE_COMMITMENT is the code commitment of the account.
export.get_acct_code_commitment
padw push.ACCT_CODE_COMMITMENT_PTR mem_loadw
end

#! Stores the new account code root in memory.
#! Stores the new account code commitment in memory.
#!
#! Stack: [CODE_ROOT]
#! Output: [CODE_ROOT]
#! Stack: [CODE_COMMITMENT]
#! Output: [CODE_COMMITMENT]
#!
#! Where:
#! - CODE_ROOT is the new account code root.
export.set_new_acct_code_root
push.ACCT_NEW_CODE_ROOT_PTR
#! - CODE_COMMITMENT is the new account code commitment.
export.set_new_acct_code_commitment
push.ACCT_NEW_CODE_COMMITMENT_PTR
mem_storew
end

#! Returns the new account code root.
#! Returns the new account code commitment.
#!
#! Stack: []
#! Output: [CODE_ROOT]
#! Output: [CODE_COMMITMENT]
#!
#! Where:
#! - CODE_ROOT is the new account code root.
export.get_new_acct_code_root
padw push.ACCT_NEW_CODE_ROOT_PTR mem_loadw
#! - CODE_COMMITMENT is the new account code commitment.
export.get_new_acct_code_commitment
padw push.ACCT_NEW_CODE_COMMITMENT_PTR mem_loadw
end

#! Returns the account storage root.
Expand Down
42 changes: 21 additions & 21 deletions miden-lib/asm/miden/kernels/tx/prologue.masm
Original file line number Diff line number Diff line change
Expand Up @@ -384,50 +384,50 @@ proc.validate_new_account
# => []
end

#! Validates that account procedures match account code root.
#! Validates that account procedures match account code commitment.
#!
#! This is achieved by reading account procedures from AdviceMap and sequentially hashing them
#! before comparing the final hash to the account code root. This procedure also populates memory
#! before comparing the final hash to the account code commitment. This procedure also populates memory
#! with these account procedures.
#!
#! Stack: []
#! Output: []
proc.validate_account_procedures
# get account code root to query elements from AdviceMap
exec.memory::get_acct_code_root
# => [ACCT_CODE_ROOT]
# get account code commitment to query elements from AdviceMap
exec.memory::get_acct_code_commitment
# => [ACCT_CODE_COMMITMENT]

# query elements from AdviceMap and get num of procedures to pipe
adv.push_mapval adv_push.1
# => [len, ACCT_CODE_ROOT]
# => [len, ACCT_CODE_COMMITMENT]

# get counter and account procedures memory location
push.0 exec.memory::get_account_procedures_section_offset
# => [location, counter, len, ACCT_CODE_ROOT]
# => [location, counter, len, ACCT_CODE_COMMITMENT]

# prepare stack for looping
padw padw padw push.1
# => [PAD, PAD, PAD, 1, location, counter, len, ACCT_CODE_ROOT]
# => [PAD, PAD, PAD, 1, location, counter, len, ACCT_CODE_COMMITMENT]

while.true
# pipe elements from advice map to stack and memory and hash them
adv_pipe hperm
# => [HASH, HASH, HASH, location, counter, len, ACCT_CODE_ROOT]
# => [HASH, HASH, HASH, location, counter, len, ACCT_CODE_COMMITMENT]

# check if all account procedures have been piped
movup.13 add.1 dup movdn.14 dup.15 neq
# => [should_loop, HASH, HASH, HASH, location, counter, len, ACCT_CODE_ROOT]
# => [should_loop, HASH, HASH, HASH, location, counter, len, ACCT_CODE_COMMITMENT]
end

# keep relevant hash
exec.native::state_to_digest
# => [HASH, location, counter, len, ACCT_CODE_ROOT]
# => [HASH, location, counter, len, ACCT_CODE_COMMITMENT]

# drop memory location, counter and number of procedures
movup.4 movup.5 movup.6 drop drop drop
# => [HASH, ACCT_CODE_ROOT]
# => [HASH, ACCT_CODE_COMMITMENT]

# verify hashed account procedures match account code root
# verify hashed account procedures match account code commitment
assert_eqw
# => []
end
Expand All @@ -444,15 +444,15 @@ end
#! and the account nonce is not zero
#!
#! Stack: []
#! Advice stack: [[account_id, 0, 0, account_nonce], ACCOUNT_VAULT_ROOT, ACCOUNT_STORAGE_ROOT, ACCOUNT_CODE_ROOT]
#! Advice stack: [[account_id, 0, 0, account_nonce], ACCOUNT_VAULT_ROOT, ACCOUNT_STORAGE_ROOT, ACCOUNT_CODE_COMMITMENT]
#! Output: []
#!
#! Where:
#! - account_id, the account that the transaction is being executed against.
#! - account_nonce, account's nonce.
#! - ACCOUNT_VAULT_ROOT, account's vault root.
#! - ACCOUNT_STORAGE_ROOT, account's storage root.
#! - ACCOUNT_CODE_ROOT, account's code root.
#! - ACCOUNT_CODE_COMMITMENT, account's code commitment.
proc.process_account_data
# Copy the account data from the advice stack to memory and hash it
# ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -520,10 +520,10 @@ proc.process_account_data
exec.memory::set_init_nonce
# => []

# set the new account code root to the initial account code root this is used for managing
# code root updates, validates and stores account procedures in memory.
exec.memory::get_acct_code_root
exec.memory::set_new_acct_code_root dropw
# set the new account code commitment to the initial account code commitment this is used for managing
# code commitment updates, validates and stores account procedures in memory.
exec.memory::get_acct_code_commitment
exec.memory::set_new_acct_code_commitment dropw
exec.validate_account_procedures
# => []

Expand Down Expand Up @@ -1089,7 +1089,7 @@ end
#! [account_id, 0, 0, account_nonce],
#! ACCOUNT_VAULT_ROOT,
#! ACCOUNT_STORAGE_ROOT,
#! ACCOUNT_CODE_ROOT,
#! ACCOUNT_CODE_COMMITMENT,
#! number_of_input_notes,
#! TX_SCRIPT_ROOT,
#! ]
Expand Down Expand Up @@ -1117,7 +1117,7 @@ end
#! - account_nonce, account's nonce.
#! - ACCOUNT_VAULT_ROOT, account's vault root.
#! - ACCOUNT_STORAGE_ROOT, account's storage root.
#! - ACCOUNT_CODE_ROOT, account's code root.
#! - ACCOUNT_CODE_COMMITMENT, account's code commitment.
#! - number_of_input_notes, number of input notes.
#! - TX_SCRIPT_ROOT, the transaction's script root.
#! - MMR_PEAKS, is the MMR peak data, see process_chain_data
Expand Down
2 changes: 1 addition & 1 deletion miden-lib/src/transaction/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn extend_advice_inputs(
/// [account_id, 0, 0, account_nonce],
/// ACCOUNT_VAULT_ROOT,
/// ACCOUNT_STORAGE_ROOT,
/// ACCOUNT_CODE_ROOT,
/// ACCOUNT_CODE_COMMITMENT,
/// number_of_input_notes,
/// TX_SCRIPT_ROOT,
/// ]
Expand Down
6 changes: 3 additions & 3 deletions miden-lib/src/transaction/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub const FINAL_ACCOUNT_HASH_WORD_IDX: usize = 1;
// ================================================================================================

/// Parses the stub account data returned by the VM into individual account component commitments.
/// Returns a tuple of account ID, vault root, storage root, code root, and nonce.
/// Returns a tuple of account ID, vault root, storage root, code commitment, and nonce.
pub fn parse_final_account_stub(elements: &[Word]) -> Result<AccountStub, AccountError> {
if elements.len() != ACCT_DATA_MEM_SIZE {
return Err(AccountError::StubDataIncorrectLength(elements.len(), ACCT_DATA_MEM_SIZE));
Expand All @@ -31,7 +31,7 @@ pub fn parse_final_account_stub(elements: &[Word]) -> Result<AccountStub, Accoun
let nonce = elements[ACCT_ID_AND_NONCE_OFFSET as usize][ACCT_NONCE_IDX];
let vault_root = elements[ACCT_VAULT_ROOT_OFFSET as usize].into();
let storage_root = elements[ACCT_STORAGE_ROOT_OFFSET as usize].into();
let code_root = elements[ACCT_CODE_COMMITMENT_OFFSET as usize].into();
let code_commitment = elements[ACCT_CODE_COMMITMENT_OFFSET as usize].into();

Ok(AccountStub::new(id, nonce, vault_root, storage_root, code_root))
Ok(AccountStub::new(id, nonce, vault_root, storage_root, code_commitment))
}
4 changes: 2 additions & 2 deletions miden-tx/src/host/account_procs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ impl AccountProcedureIndexMap {
/// Returns a new [AccountProcedureIndexMap] instantiated with account procedures present in
/// the provided advice provider.
pub fn new<A: AdviceProvider>(
account_code_root: Digest,
account_code_commitment: Digest,
adv_provider: &A,
) -> Result<Self, TransactionHostError> {
// get the account procedures from the advice_map
let procs = adv_provider.get_mapped_values(&account_code_root).ok_or_else(|| {
let procs = adv_provider.get_mapped_values(&account_code_commitment).ok_or_else(|| {
TransactionHostError::AccountProcedureIndexMapError(
"Failed to get mapped values from the AdviceProvider".to_string(),
)
Expand Down
4 changes: 2 additions & 2 deletions miden-tx/src/testing/account_procs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub struct AccountProcedureIndexMap(BTreeMap<Digest, u8>);
impl AccountProcedureIndexMap {
/// Returns a new [AccountProcedureIndexMap] instantiated with account procedures present in
/// the provided advice provider.
pub fn new<A: AdviceProvider>(account_code_root: Digest, adv_provider: &A) -> Self {
pub fn new<A: AdviceProvider>(account_code_commitment: Digest, adv_provider: &A) -> Self {
// get the account procedures from the advice_map
let procs = adv_provider.get_mapped_values(&account_code_root).unwrap();
let procs = adv_provider.get_mapped_values(&account_code_commitment).unwrap();

let mut result = BTreeMap::new();

Expand Down
Loading

0 comments on commit cc7303d

Please sign in to comment.