Muskan Kumari
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Ooty zkVM: Rust version of BrainSTARK This series of blogposts is written by [Muskan Kumari](https://x.com/m_pandey5) and [Soumya Thakur](https://x.com/soumyathakur44). We are undergraduate students at IIT Delhi who spent some time playing around with STARKS and the idea of a zkVM. [Ooty VM](https://github.com/manojkgorle/brainfuckvm) is a toy VM we made from scratch, and it is the rust implementation of [BrainSTARK zkVM](https://aszepieniec.github.io/stark-brainfuck/). We faced some challenges along the way and made changes accordingly to tackle those. This first blog introduces the concepts of ZK, VM and the Brainfuck ISA, while subsequent posts dive into details of our project. j Github repository: [link](https://github.com/manojkgorle/brainfuckvm) (Contributors: [Manoj](https://x.com/ManojKGorle), [Muskan](https://x.com/m_pandey5), [Soumya](https://x.com/soumyathakur44)) # Introduction A **zkVM (Zero-Knowledge Virtual Machine)** is a powerful type of virtual machine that utilizes zero-knowledge proofs (**ZKPs**) to guarantee the integrity and privacy of computations. To understand how a zkVM works, we first need to understand what **zero-knowledge proofs** are, and what **virtual machines** are. --- ## What are ZKPs? **Zero-knowledge proofs** are a cryptographic technique that enables one party to prove the validity of a statement to another party **without revealing any additional information**. In simpler terms, it's like proving you know a secret without actually revealing the secret itself. The reader is assumed to have some background about what ZKPs are and the basics of how they work. > If you are unfamiliar with ZKPs, refer to [this starter cookbook on ZK](https://hackmd.io/UorSwsdPSw2VLMKaJL4e6g#Zero-Knowledge). --- ## What is a Virtual Machine? A **virtual machine (VM)** is a program that runs programs. Commonly shortened to just VM, it is no different than any other physical computer like a laptop, smart phone, or server. It is a program that acts like a computer by simulating a CPU along with few other hardware components, allowing it to: - Perform arithmetic. - Read and write to memory. - Interact with I/O devices. Most importantly, it can understand a machine language which you can use to program it. ### Components of a VM - **Memory**: The RAM that our program accesses, reads, and writes to, plus the program memory. In our case, this memory will be stored in a simple array. - **Registers**: A register is a slot for storing a single value on the CPU. Registers are like the “workbench” of the CPU. For the CPU to work with a piece of data, it has to be in one of the registers. However, since there are just a few registers, only a minimal amount of data can be loaded at any given time. VMs work around this by loading values from memory into registers, calculating values into other registers, and then storing the final results back in memory. The registers of our VM will be discussed later on. - **Instruction Set**: An instruction is a command which tells the CPU to do some fundamental task, such as adding two numbers. Instructions have both an opcode that indicates the kind of task to perform and a set of parameters that provide inputs to the task being performed. Each opcode represents one task that the CPU “knows” how to do. Everything the computer can calculate is some sequence of these simple instructions. --- ## What is a zkVM? A **zkVM** is a VM that provides a **zero-knowledge proof** of the computations it performs. Broadly speaking, most zkVMs implicitly include the compiler toolchains and proof systems appended to the virtual machine that executes the program, and not just the virtual machine itself. ### Components of a zkVM and Their Functions - **Compiler**: Converts ordinary programs written in Rust, C, Solidity, or C++ into machine code (VM-readable code). - **VM**: Executes the compiled program, as machine code, without revealing information about the program and generates an execution trace. - **Prover**: Converts the execution trace into ZKPs with cryptography and shrinks the size of the output into a few kilobytes as a SNARK or STARK proof of program execution. - **Verifier (not part of zkVM)**: Verifies the encrypted results of the executed program using a proof. Thus, the design and implementation of a zkVM are governed by: 1. The **choice of proof** (SNARKs or STARKs). 2. The **instruction set architecture (ISA)** of the zkVM. Traditionally, an ISA specifies what a CPU is capable of (data types, registers, memory, etc.) and the sequence of actions the CPU performs when it executes a program (the instruction set). Essentially, it determines the machine code that is interpretable and executable by the VM. Choosing an ISA can yield radical differences in the accessibility and usability of the zkVM, as well as the speed and efficiency of the proof generation processes, and underpins the construction of any zkVM. The instruction set architecture or ISA used in our zkVM is **Brainfuck ISA**, and the proof system used is **STARKs**. We will cover both of them before we attempt to understand the working of our zkVM (or STARK Engine). --- ## Brainfuck ISA **Brainfuck** is an esoteric programming language created in 1993 by Swiss student Urban Müller. Designed to be extremely minimalistic, the language consists of only eight simple commands, a data pointer, and an instruction pointer. While Brainfuck is fully Turing complete, i.e., it can be used to write any program, it is not intended for practical use. This is because it provides so little abstraction that the programs get very long or complicated. But the simple 8-command structure of this language is the very reason for choosing it to develop a basic zkVM from scratch with all the cryptographic libraries and their implementation. Any Brainfuck program is a sequence of these commands, possibly interspersed with other characters (which are ignored). ### Brainfuck Commands | **Command** | **Description** | |-------------|---------------------------------------------------------------------------------------------------------| | **`>`** | Increment the data pointer by one (move to the next cell on the right). | | **`<`** | Decrement the data pointer by one (move to the previous cell on the left). | | **`+`** | Increment the byte at the data pointer by one. | | **`-`** | Decrement the byte at the data pointer by one. | | **`.`** | Output the byte at the data pointer. | | **`,`** | Accept one byte of input and store its value in the byte at the data pointer. | | **`[`** | If the byte at the data pointer is zero, jump to the command after the matching `]`. | | **`]`** | If the byte at the data pointer is non-zero, jump back to the matching `[` command. | --- ### Key Features of Brainfuck - Uses a simple machine model consisting of: - A program and instruction pointer that begins at the first command, and each command it points to is executed, after which it normally moves forward to the next command. The program terminates when the instruction pointer moves past the last command. - A one-dimensional array of at least 30,000 byte cells initialized to zero. - A movable data pointer (initialized to point to the leftmost byte of the array). - Two streams of bytes for input and output (commonly connected to a keyboard and monitor, and using the **ASCII character encoding**). - Each command is executed sequentially unless redirected by loops (`[` and `]`). #### Loops in Brainfuck - `[ and ]` match like parentheses: - Each `[` matches exactly one `]`, and vice versa. - There can be no unmatched `[ or ]`. --- Let us take a simple example of adding two numbers to see how Brainfuck ISA works. ### Adding Two Values The following code snippet will add the current cell's value to the next cell: Each time the loop is executed, the current cell is decremented, the data pointer moves to the right, that next cell is incremented, and the data pointer moves left again. This sequence is repeated until the starting cell is 0. ```brainfuck ++ Cell c0 = 2 > +++++ Cell c1 = 5 [ Start your loops with your cell pointer on the loop counter (c1 in our case) < + Add 1 to c0 > - Subtract 1 from c1 ] End your loops with the cell pointer on the loop counter At this point, our program has added 5 to 2, leaving 7 in c0 and 0 in c1. However, we cannot output this value to the terminal since it is not ASCII encoded. To display the ASCII character "7," we must add 48 to the value 7. We use a loop to compute 48 = 6 * 8: ++++ ++++ c1 = 8 and this will be our loop counter again [ < +++ +++ Add 6 to c0 > - Subtract 1 from c1 ] < . Print out c0, which has the value 55, translating to "7"! ``` ### Modified Brainfuck Compiler Instructions Brainfuck is not an instruction set architecture on its own because the instructions are not self-contained. They depend on context. Specifically, the [ and ] instructions refer to the locations of their matching partners. To address this, we modify the `[` and `]` instructions as follows: The field element (we will talk about what field elements are and their context in the zkVM, for now just treat this as the immediate next address the instruction pointer will point to) immediately following `[` or `]` contains the destination address of the potential jump. The instruction set with this change is now variable-size but nevertheless defines a machine architecture. This modification necessitates a compiler that translates the Brainfuck program to include these addresses. An exceedingly simple pushdown automata achieves this translation. #### Compiler Logic: The compiler here is almost streaming, meaning that it runs over the input sequence once and starts outputting symbols before it reaches the end. Except when it occasionally outputs placeholder symbols whose concrete value (the needed address of jump) will be set later. The compiler keeps track of a stack that stores the locations (in the output sequence) of the `[` symbols that have not yet been closed by a matching `]` symbol. Let c denote a counter that tracks the total number of symbols sent to output so far, then: - **When `[` is encountered**: a) Push `c` to the stack. b) Push two symbols to the output: `[` and a placeholder `*`. - **When `]` is encountered**: a) Read and pop the location `i` of the matching `[` from the stack. b) Replace the placeholder at location `i+1` with `c+2`. c) Push two symbols to the output: `]` and `i+2`. - **For any other symbol**: Push the symbol to the output with no stack modifications. #### Example (modified and compiled Brainfuck program): For the program discussed [above](https://hackmd.io/CvkubZOBR9CCGu6oR4sEvQ?both#Adding-Two-Values), where we add the value of two cells: `++>+++++[<+>-]++++++++[<++++++>-]<.` The compiled Brainfuck program with corresponding address for jumps will be: |`ip`|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20| |-----|-----|---|------|----|---|----|---|----------|------|----|----|--|----|---|---|----|----|--|----|-----|-----| |`p[ip]`|+|+|>|+|+|+|+|+|[|`16`|<|+|>|-|]|`10`|+|+|+|+|+ |`ip`|20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35|36|37| |---|--|-----|---|------|--------------|------|----|------|----|----------|---|---|----|----|--|----|-----|-----| |`p[ip]`|+|+|+|[|`36`|<|+|+|+|+|+|+|>|-|]|`25`|<|.| The numerical values of `p[ip]` refer to the addresses of jump for `[` and `]`. ### State Transition Function for Modified Brainfuck VM Any virtual machine defines a state transition function. Running the virtual machine consists of repeatedly applying this function to the state, until the termination criterion is met, in this case when the instruction pointer points beyond the length of the program. For Brainfuck, the transition depends on the current instruction `p[ip]`: - `ip`: Instruction pointer pointing to a program instruction. - `dp`: Data pointer pointing to a memory cell. - `p[i]`: Instruction at location `i`. - `d[i]`: Memory cell at location `i`. State Transition Logic: 1. **If `p[ip] == '['`**: - If `d[dp] == 0`, jump: `ip = program[ip+1]`. - If `d[dp] != 0`, skip the placeholder: `ip = ip+2`. 2. **If `p[ip] == ']'`**: - If `d[dp] != 0`, jump: `ip = program[ip+1]`. - If `d[dp] == 0`, skip the placeholder: `ip = ip+2`. 3. **If `p[ip] == '<'`**: - Decrement data pointer: `dp = dp - 1`. - Increment instruction pointer: `ip = ip + 1`. 4. **If `p[ip] == '>'`**: - Increment data pointer: `dp = dp + 1`. - Increment instruction pointer: `ip = ip + 1`. 5. **If `p[ip] == '+'`**: - Increment data element: `d[dp] = d[dp] + 1`. - Increment instruction pointer: `ip = ip + 1`. 6. **If `p[ip] == '-'`**: - Decrement data element: `d[dp] = d[dp] - 1`. - Increment instruction pointer: `ip = ip + 1`. 7. **If `p[ip] == '.'`**: - Output data element: `d[dp]`. - Increment instruction pointer: `ip = ip + 1`. 8. **If `p[ip] == ','`**: - Set data element to input symbol: `d[dp] = input`. - Increment instruction pointer: `ip = ip + 1`. --- We have covered the basics of zkVM and Brainfuck ISA. The [next blogpost](https://hackmd.io/@Muskan05/rJb7hejU1x) will discuss STARKS and its required pre-requisites in detail.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully