zkMIPS / zkm

The universal zkVM empowering Ethereum as the Global Settlement Layer
https://zkm.io
MIT License
105 stars 50 forks source link

Support public input #125

Closed eigmax closed 6 months ago

eigmax commented 6 months ago

The program needs a way to check the consistency of its state transition. For example, when we run EVM on zkVM, we need to check the Account MPT's root(pre root and post root) is changed correctly.

eigmax commented 6 months ago

Background

There are 2 methods to set up the external input to program. The first is patch_stack and the second is to use syscall getpid.

Generally speaking, we has hardcoded the syscall getpid with load_preimage to read the previous memory mirror of MIPS VM.

Design

Since we get the input from os.Argv, we can define the os.Argv[1] as the public input, and os.Argv[2] as the private input. Notice that os.Argv[0] is the ELF itself.

Since the size of public input for a circuit should be fixed when we initiate the zkVM, we fixed the public input maximum size to 1024 bytes currently, but will change it to 32 bytes in production environment. This is controlled by global variable here.

Another consideration is that we have split the program into segments, and each segment actually digests same public input. We add a new field userdata to PublicValues.

For example, we take Hello!!! as the user data, we have public values as below.

PublicValues {
    roots_before: MemRoots {
        root: [337687913, 2183073662, 2468577829, 3342129701, 3593151075, 3158445574, 1218541304, 763871345],
        userdata: [104, 101, 108, 108, 111, 33, 33, 33]
    },
    roots_after: MemRoots {
        root: [2173857488, 1724529098, 275865231, 3939080808, 538191105, 3929294015, 1653038589, 1891891347],
        userdata: [104, 101, 108, 108, 111, 33, 33, 33]
    }
}

A full example is as below.

ARGS="hello!!! world!!!" BASEDIR=test-vectors RUST_LOG=debug ELF_PATH=test-vectors/hello SEG_OUTPUT=/tmp/output SEG_SIZE=$1    cargo run --release --example zkmips split

BASEDIR=test-vectors RUST_LOG=debug BLOCK_NO=13284491 SEG_FILE_DIR="/tmp/output" SEG_FILE_NUM=2 SEG_SIZE=$1 \
    cargo run --release --example zkmips aggregate_proof_all