Inspiration

As smart contract developers, we look forward to the arrival of zk-rollups to scale up the adoption of bockchain technology. However, all the ZK VMs are still fairly early in their development. While all of them will eventually support some sort of (high-level) Solidity compiler, currently developers who want an early glimpse of this technology have to deal with low-level coding. To speed up our learning, we decided to build a compiler with a simple grammar that will allow us to rapidly write different Miden programs.

By focusing on simple arithmetic expressions, our compiler also help demonstrate the power of zero knowledge proofs to developers not familiar with smart contract development. That is, our compiler will help a developer see that she can write a math formula and prove to another user that it was correctly executed on his inputs without revealing the formula to him.

What it does

The compiler takes an arithmetic expression (such as 2 + 3 * 4) and compiles an assembly program that can be executed by the Miden VM. It has a special syntax (in[0]) to access user inputs, so we can write more useful formulas such as in[0] + in[1] * in[2] - 2 * in[3].

How we built it

We use ohm.js, which is a JavaScript library for building parsers, interpreters, compilers, and more. We started with their math example and adapt it to output Miden assembly code. Plus we added support for user inputs.

Challenges we ran into

It's the first time we've written a compiler, and also the first time we've used ohm.js, so there's a learning curve. We had also run into an issue reported in Miden's Github, namely the Miden prover expects a stack size of 16 at the end of execution.

Accomplishments that we're proud of

We take pride in building a viable project to be of educational value.

What we learned

We learned how to write a compiler :) And also many of the assembly codes supported by the Miden VM. We only use a subset of them, but look forward to incorporating more.

What's next for An Experimental Compiler for Miden VM

Unfortunately we didn't have enough time to add support for comparison functions (greater than, less than, etc.) and conditional execution (if-then). Definitely want to add those next. We also want to add support for statements and temporary variables so simple programs can be written.

Built With

Share this project:

Updates