Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test for SyscallReadPreimage #2305

Merged
merged 11 commits into from
Jun 17, 2024

Conversation

querolita
Copy link
Member

This uses @dannywillems's framework to emulate the preimage oracle in unit tests for the Keccak syscall.

This PR does not only include a unit test using the sample file, but it also proposes two main fixes:

  1. The get_preimage() function of the oracle trait was not reading the right bytes from the file, but in ASCII. Thus, I decode them as hexadecimal bytes to store them correctly.
  2. The interpreter for the SyscallReadPreimage was not updating the address with the offset correctly. Thus, the addresses where the preimage bytes were being stored in memory were mis-aligned. The unit test checks that now the bytes are stored correctly where desired.

@querolita
Copy link
Member Author

Merging #2311 into this branch to avoid underflow errors when accessing registers/memory.

@querolita querolita marked this pull request as ready for review June 11, 2024 17:15
@querolita
Copy link
Member Author

querolita commented Jun 14, 2024

Making it a draft because despite constraints are satisfied after this PR, the demo halts 🤦🏻 Investigating.

Running the block 13000378 with config

export L1_HEAD=0x83e91f7d09781527978f05405fd8a1156e66163e4d06631d753a160bc2b6ce24
export L2_HEAD=0x78d20c72952c7c85ff9fa55e1990a3d65ddba1808f84159bfc9e86cb9c4cc054
export L2_BLOCK_NUMBER=13000378
export STARTING_OUTPUT_ROOT=0x4b007f45b63fdf5fe07472cff8e9d4b44548e1d55f11fca03c893fe9279ab8fe
export L2_CLAIM=0x8fd0c1696c19d4d4e3eccc4e97572f302f3e523fcf0bff0f237469a1bbc45142
export OP_PROGRAM_DATA_DIR=/proof-systems/o1vm/op-program-db-for-l2-block-13000378

With command

ZKVM_STATE_FILENAME=snapshot-state-60000000.json RUST_LOG=debug INFO_AT="%100000" FILENAME=env-for-l2-block-13000378.sh bash run-code.sh

Gives me

[2024-06-14T11:37:02Z DEBUG o1vm] Instruction 61394906 -> IType(Load32)
[2024-06-14T11:37:02Z DEBUG o1vm] Instruction 61394907
[2024-06-14T11:37:02Z DEBUG o1vm::mips::witness] Executing instruction IType(AddImmediateUnsigned)
[2024-06-14T11:37:02Z DEBUG o1vm] Instruction 61394907 -> IType(AddImmediateUnsigned)
[2024-06-14T11:37:02Z DEBUG o1vm] Instruction 61394908
[2024-06-14T11:37:02Z DEBUG o1vm::mips::witness] Executing instruction RType(SyscallExitGroup)
Exited with code 2 at step 61394907
Halted at step=61394908 instruction=RType(SyscallExitGroup)

whereas in the parent branch (and master) it executes a different set of instructions

[2024-06-14T11:41:10Z DEBUG o1vm] Instruction 61394906
[2024-06-14T11:41:10Z DEBUG o1vm::mips::witness] Executing instruction RType(ShiftLeftLogical)
[2024-06-14T11:41:10Z DEBUG o1vm] Instruction 61394906 -> RType(ShiftLeftLogical)
[2024-06-14T11:41:10Z DEBUG o1vm] Instruction 61394907
[2024-06-14T11:41:10Z DEBUG o1vm::mips::witness] Executing instruction RType(Or)
[2024-06-14T11:41:10Z DEBUG o1vm] Instruction 61394907 -> RType(Or)
[2024-06-14T11:41:10Z DEBUG o1vm] Instruction 61394908
[2024-06-14T11:41:10Z DEBUG o1vm::mips::witness] Executing instruction RType(Or)
[2024-06-14T11:41:10Z DEBUG o1vm] Instruction 61394908 -> RType(Or)
[2024-06-14T11:41:10Z DEBUG o1vm::mips::witness] Executing instruction IType(Store32)
[2024-06-14T11:41:26Z DEBUG o1vm] Checking MIPS circuit RType(SyscallReadPreimage)
[2024-06-14T11:41:30Z DEBUG o1vm] Generated a MIPS RType(SyscallReadPreimage) proof:

@querolita querolita marked this pull request as ready for review June 14, 2024 15:14
@querolita
Copy link
Member Author

There was no issue with the interpreter code. Meaning, the interpreter did not have to compute the effective address. Instead, the instruction itself updates the address with the preimage offset at every call. Now the unit test behaves like the real interpreter.

// We read at most 4 bytes, ensuring that we respect word alignment.
// Here, if the address is not aligned, the first call will read < 4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this comment is true. The code could be asking to write into the address 4 the first 4 bytes, and at the address 42 the next 3 bytes, after that the next 2 bytes at the address 82, etc (just using random values here for the example)

Copy link
Member Author

@querolita querolita Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, that comment is outdated. I wrote that back when I thought that was the case, but after talking to you I learnt that that was not necessarily the case. I will delete it in the next commit.

use crate::mips::{interpreter::interpret_rtype, RTypeInstruction};

#[test]
fn test_unit_syscall_read_preimage() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is interesting to verify if the syscall works with writing into a contiguous piece of memory (addr/reg 5 is always increased by the (random) number of bytes that the user asks to read). However, what about this example? It doesn't need to be part of this PR, but it is a use case that might happen.

Copy link
Member

@dannywillems dannywillems left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test seems to make sense and map the expected behavior. I would implement more "random read access" in the future (not blocking for the PR) as explained in the comments.

LGTM, modulo my comments.
I'm moving on the actual implementation in #2274, and I might have other properties I would like to check in this test, but still not blocking for this PR.

Base automatically changed from zkvm/mips/fix-syscall-preimage to master June 17, 2024 18:21
@dannywillems
Copy link
Member

Merging.

@dannywillems dannywillems merged commit 5ae0a2f into master Jun 17, 2024
6 checks passed
@dannywillems dannywillems deleted the zkvm/mips/test-syscall-read-preimage branch June 17, 2024 18:25
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.

2 participants