Skip to content

Instantly share code, notes, and snippets.

@partylikeits1983
Last active August 27, 2024 13:19
Show Gist options
  • Save partylikeits1983/d503df805ce0e6f869d6675115c52e40 to your computer and use it in GitHub Desktop.
Save partylikeits1983/d503df805ce0e6f869d6675115c52e40 to your computer and use it in GitHub Desktop.

Basic usable compiler:

Level 1: Math & Pure functions:

For simplicity it would be sufficient to have a pure Rust to MASM compiler.

There's probably already a way to run the compiler, but this is just an example of how it would work.

midenc some_rust_file.rs masm_output.masm

Consider a simple Rust function:

fn divide_sum(a: u32, b: u32, c: u32) -> u32 {
    if c == 0 {
        panic!("Division by zero");
    } else {
        (a + b) / c
    }
}

The compiler would convert the Rust function into Miden Assembly:

# inputs => [a, b, c]
# outputs => [result]
proc.divide_sum
  swap.2
  dup
  push.0 
  eq

  if.true
    push.0
    assert
  else
    swap.2
    add
    swap
    div
  end
end

(This could probably be optimized; this is just an illustrative example.)

Ideally, the compiler would automatically use u32mul and u32div and would, by default, ensure no underflow or overflow occurs.

Here is an example of fixed point arithmetic done in MASM that would be nice to be able to do in rust: https://github.com/compolabs/spark-miden-v1/blob/56587a07732fa8e40cd7b577e60d7874c608ea00/src/notes/SWAPp.masm#L77

Would also be cool, although probably more difficult to be able to hash things in Rust exactly like how hashing occurs in MASM: https://github.com/compolabs/spark-miden-v1/blob/56587a07732fa8e40cd7b577e60d7874c608ea00/src/notes/SWAPp.masm#L204

Level 2: Miden notes and accounts written in pure rust:

Although not a requirement at the beginning, it would be very useful to be able to access Miden kernel functions using Rust. This would enable writing Miden notes and accounts in pure Rust.

Imagine if you could write notes and accounts in pure rust. Something like this:

use miden::note;
use miden::wallet;

// Miden Note Example
fn divide_sum(a: u32, b: u32, c: u32) -> u32 {
    if c == 0 {
        panic!("Division by zero");
    } else {
        (a + b) / c
    }
}


fn main() {
    let res = divide_sum(5,5,2);
    
    let serial_num = note::get_serial_number();
    
    # ... more logic ...
}

I know this is a primitive example, but it's very cool to think about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment