Skip to content

Tx script and note args felt order on the stack #844

@greenhat

Description

@greenhat

This would probably be fixed in 0xMiden/miden-vm#2393. @bitwalker or is it?

Problem

The Word arg passed to TransactionRequestBuilder::script_arg ends up in the reversed order in arg at run(arg: Word,...) tx script entrypoint (see the entrypoint example for a tx script at

fn run(arg: Word, account: &mut Account) {
let num_felts = adv_push_mapvaln(arg.clone());
let num_felts_u64 = num_felts.as_u64();
assert_eq(Felt::from_u32((num_felts_u64 % 4) as u32), felt!(0));
let num_words = Felt::from_u64_unchecked(num_felts_u64 / 4);
let commitment = arg;
let input = adv_load_preimage(num_words, commitment);
let tag = input[TAG_INDEX];
let aux = input[AUX_INDEX];
let note_type = input[NOTE_TYPE_INDEX];
let execution_hint = input[EXECUTION_HINT_INDEX];
let recipient: [Felt; 4] = input[RECIPIENT_START..RECIPIENT_END].try_into().unwrap();
let note_idx =
output_note::create(tag.into(), aux, note_type.into(), execution_hint, recipient.into());
let asset: [Felt; 4] = input[ASSET_START..ASSET_END].try_into().unwrap();
account.move_asset_to_note(asset.into(), note_idx);
}
).

Since tx and note script args are passed on the stack there is not much we can do in the compiler to convert it to the expected order. The run function is compiled to Wasm expecting 4 values on the stack and they are pushed on the stack by whoever calls the tx script program.

The current workaround is to reverse the Word before passing it in TransactionRequestBuilder::script_arg. However, it's unintuitive to the user and I have this issue reported by our pioneer.

Solutions

  1. Emit a wrapper for the VM program's entrypoint that reverses the first 4 elements on the stack before calling the entrypoint.
  2. Reverse the order of the tx script and note script args when they are put on the stack by the client/base. But that will be a breaking change for all the existing MASM code that uses args.
  3. Do nothing and document the need to pass args reversed in TransactionRequestBuilder::script_arg and note script args docs.

I prefer 1 or 2.

/cc @bobbinth

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    blockerThis issue is one of our top prioritiescodegenIssues or features related to lowering IR into MASMrustRelated to the Rust and SDK ergonomicssdkRust SDK related functionality

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions