Skip to content

Commit

Permalink
refactor: change note_ptr to note_idx for create_note and add_asset_t…
Browse files Browse the repository at this point in the history
…o_note
  • Loading branch information
Dominik1999 committed Jun 12, 2024
1 parent 3fb65fb commit 97016d5
Showing 1 changed file with 30 additions and 36 deletions.
66 changes: 30 additions & 36 deletions miden-lib/asm/miden/kernels/tx/tx.masm
Original file line number Diff line number Diff line change
Expand Up @@ -130,49 +130,49 @@ end
#! Returns the pointer to the note the asset was stored at.
#! Panics if the non-fungible asset already exists in the note.
#!
#! Inputs: [ASSET, note_ptr, num_of_assets]
#! Outputs: [note_ptr]
#! Inputs: [ASSET, note_ptr, num_of_assets, note_idx]
#! Outputs: [note_ptr, note_idx]
proc.add_non_fungible_asset_to_note
dup.4 exec.memory::get_created_note_asset_data_ptr
# => [asset_ptr, ASSET, note_ptr, num_of_assets]
# => [asset_ptr, ASSET, note_ptr, num_of_assets, note_idx]

# compute the pointer at which we should stop iterating
dup dup.7 add
# => [end_asset_ptr, asset_ptr, ASSET, note_ptr, num_of_assets]
# => [end_asset_ptr, asset_ptr, ASSET, note_ptr, num_of_assets, note_idx]

# reorganize and pad the stack, prepare for the loop
movdn.5 movdn.5 padw dup.9
# => [asset_ptr, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [asset_ptr, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

# compute the loop latch
dup dup.10 neq
# => [latch, asset_ptr, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [latch, asset_ptr, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

while.true
# load the asset and compare
mem_loadw eqw assertz.err=ERR_NON_FUNGIBLE_ASSET_ALREADY_EXISTS
# => [ASSET', ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [ASSET', ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

# drop ASSET' and increment the asset pointer
dropw movup.5 add.1 dup movdn.6 padw movup.4
# => [asset_ptr + 1, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr + 1, note_ptr, num_of_assets]
# => [asset_ptr + 1, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr + 1, note_ptr, num_of_assets, note_idx]

# check if we reached the end of the loop
dup dup.10 neq
end
# => [asset_ptr, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [asset_ptr, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

# prepare stack for storing the ASSET
movdn.4 dropw
# => [asset_ptr, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [asset_ptr, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

# end of the loop reached, no error so we can store the non-fungible asset
mem_storew dropw drop drop
# => [note_ptr, num_of_assets]
# => [note_ptr, num_of_assets, note_idx]

# increase the number of assets in the note
swap add.1 dup.1 exec.memory::set_created_note_num_assets
# => [note_ptr]
# => [note_ptr, note_idx]
end

#! Adds a fungible asset to a note. If the note already holds an asset issued by the
Expand All @@ -181,74 +181,74 @@ end
#! position. Returns the pointer to the note the asset was stored at.
#! Panics if the combined quantity exceeds the maximum for fungible assets (~overflow).
#!
#! Inputs: [ASSET, note_ptr, num_of_assets]
#! Inputs: [ASSET, note_ptr, num_of_assets, note_idx]
#! Outputs: [note_ptr]
proc.add_fungible_asset_to_note
dup.4 exec.memory::get_created_note_asset_data_ptr
# => [asset_ptr, ASSET, note_ptr, num_of_assets]
# => [asset_ptr, ASSET, note_ptr, num_of_assets, note_idx]

# compute the pointer at which we should stop iterating
dup dup.7 add
# => [end_asset_ptr, asset_ptr, ASSET, note_ptr, num_of_assets]
# => [end_asset_ptr, asset_ptr, ASSET, note_ptr, num_of_assets, note_idx]

# reorganize and pad the stack, prepare for the loop
movdn.5 movdn.5 padw dup.9
# => [asset_ptr, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [asset_ptr, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

# compute the loop latch
dup dup.10 neq
# => [latch, asset_ptr, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [latch, asset_ptr, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

while.true
mem_loadw
# => [STORED_ASSET, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [STORED_ASSET, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

dup.4 eq
# => [are_equal, 0, 0, stored_amount, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [are_equal, 0, 0, stored_amount, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

if.true
# add the asset quantity, we don't overflow here, bc both ASSETs are valid.
movup.2 movup.6 add
# => [updated_amount, 0, 0, faucet_id, 0, 0, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [updated_amount, 0, 0, faucet_id, 0, 0, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

# check that we don't overflow bc we use 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]
# => [updated_amount, 0, 0, faucet_id, 0, 0, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

# prepare stack to store the "updated" ASSET'' with the new quantity
movdn.5
# => [0, 0, ASSET'', end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [0, 0, ASSET'', end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

# decrease num_of_assets by 1 to offset incrementing it later
movup.9 sub.1 movdn.9
# => [0, 0, ASSET'', end_asset_ptr, asset_ptr, note_ptr, num_of_assets - 1]
# => [0, 0, ASSET'', end_asset_ptr, asset_ptr, note_ptr, num_of_assets - 1, note_idx]

# end the loop we add 0's to the stack to have the correct number of elements
push.0.0 dup.9 push.0
# => [0, asset_ptr, 0, 0, 0, 0, ASSET'', end_asset_ptr, asset_ptr, note_ptr, num_of_assets - 1]
# => [0, asset_ptr, 0, 0, 0, 0, ASSET'', end_asset_ptr, asset_ptr, note_ptr, num_of_assets - 1, note_idx]
else
# => [0, 0, stored_amount, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [0, 0, stored_amount, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

# drop ASSETs and increment the asset pointer
movup.2 drop push.0.0 movup.9 add.1 dup movdn.10
# => [asset_ptr + 1, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr + 1, note_ptr, num_of_assets]
# => [asset_ptr + 1, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr + 1, note_ptr, num_of_assets, note_idx]

# check if we reached the end of the loop
dup dup.10 neq
end
end
# => [asset_ptr, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [asset_ptr, 0, 0, 0, 0, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]
# prepare stack for storing the ASSET
movdn.4 dropw
# => [asset_ptr, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets]
# => [asset_ptr, ASSET, end_asset_ptr, asset_ptr, note_ptr, num_of_assets, note_idx]

# Store the fungible asset, either the combined ASSET or the new ASSET
mem_storew dropw drop drop
# => [note_ptr, num_of_assets]
# => [note_ptr, num_of_assets, note_idx]

# increase the number of assets in the note
swap add.1 dup.1 exec.memory::set_created_note_num_assets
# => [note_ptr]
# => [note_ptr, note_idx]
end

#! Creates a new note and returns the index of the created note.
Expand Down Expand Up @@ -363,12 +363,6 @@ export.add_asset_to_note
end
# => [note_ptr, note_idx]

push.0 drop # TODO: remove line, see miden-vm/#1122
# emit event to signal that a new asset was added to the note.
emit.NOTE_AFTER_ADD_ASSET_EVENT



push.0 drop # TODO: remove line, see miden-vm/#1122
# emit event to signal that a new asset was added to the note.
emit.NOTE_AFTER_ADD_ASSET_EVENT
Expand Down

0 comments on commit 97016d5

Please sign in to comment.