Skip to content

Conversation

@salaheldinsoliman
Copy link
Contributor

@salaheldinsoliman salaheldinsoliman commented Sep 13, 2025

This PR is a proof of concept for supporting a counter contract in Miden Assembly (masm).

Some interesting notes:
1- In Miden, each instruction either expects some input on the stack or takes an argument or both. To support this behavior, I added a miden_stack variable in ControlFlowGraph of each function, and updated it accordingly in each Expression or Instr

2- Storage in Miden is not initialized by the contract, the contract only references already existing storage (initialized by the miden client while creating the contract account). In Solang, each storage variable is assgined a slot. now Solang's built in Storage initalizer only emits var names with this format: const.STORAGE_SLOT_0=0 in which this references the first storage index pre-defined by the account.

3- To try this out, create a file named "counter.sol" and insert this code:

contract counter {
    // This references the first storage slot in Miden account storage
    uint32 count=0;

    function increment_count() public {
        count += 1;
    }

    function get_count() public view returns (uint32) {
        return count;
    }
}

Then, compile via: solang compile counter.sol --target miden --release
You then should see the .masm file which would look like:

use.miden::account
use.std::sys
const.STORAGE_SLOT_0=0
export.increment_count
    push.STORAGE_SLOT_0
    exec.account::get_item
    add.1
    push.STORAGE_SLOT_0
    exec.account::set_item
    exec.sys::truncate_stack
end
export.get_count
    push.STORAGE_SLOT_0
    exec.account::get_item
    exec.sys::truncate_stack
end

To deploy and interact with it, the most straightforward way is miden's rust client:
https://github.com/0xMiden/miden-tutorials/blob/main/rust-client/src/bin/counter_contract_increment.rs

Signed-off-by: salaheldinsoliman <salaheldin_sameh@aucegypt.edu>
@salaheldinsoliman salaheldinsoliman requested review from seanyoung and removed request for seanyoung September 13, 2025 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant