Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Fix kernel codehash discrepancy (0xPolygonZero#1400)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare authored Dec 1, 2023
1 parent 5572da3 commit 32d0096
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
35 changes: 21 additions & 14 deletions evm/src/cpu/kernel/asm/mpt/hex_prefix.asm
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ remaining_bytes:
SWAP1 SUB DUP1
// stack: num_nibbles - parity, num_nibbles - parity, U256_MAX, packed_nibbles, rlp_pos, ret_dest
%div_const(2)
// stack: remaining_bytes, num_nibbles - parity, U256_MAX, packed_nibbles, rlp_pos, ret_dest
// stack: rem_bytes, num_nibbles - parity, U256_MAX, packed_nibbles, rlp_pos, ret_dest
SWAP2 SWAP1
// stack: num_nibbles - parity, U256_MAX, remaining_bytes, packed_nibbles, rlp_pos, ret_dest
// stack: num_nibbles - parity, U256_MAX, rem_bytes, packed_nibbles, rlp_pos, ret_dest
%mul_const(4)
// stack: 4*(num_nibbles - parity), U256_MAX, remaining_bytes, packed_nibbles, rlp_pos, ret_dest
// stack: 4*(num_nibbles - parity), U256_MAX, rem_bytes, packed_nibbles, rlp_pos, ret_dest
PUSH 256 SUB
// stack: 256 - 4*(num_nibbles - parity), U256_MAX, remaining_bytes, packed_nibbles, rlp_pos, ret_dest
// stack: 256 - 4*(num_nibbles - parity), U256_MAX, rem_bytes, packed_nibbles, rlp_pos, ret_dest
SHR
// stack: mask, remaining_bytes, packed_nibbles, rlp_pos, ret_dest
// stack: mask, rem_bytes, packed_nibbles, rlp_pos, ret_dest
SWAP1 SWAP2
AND
%stack
(remaining_nibbles, remaining_bytes, rlp_pos) ->
(rlp_pos, remaining_nibbles, remaining_bytes)
(remaining_nibbles, rem_bytes, rlp_pos) ->
(rlp_pos, remaining_nibbles, rem_bytes)
%mstore_unpacking_rlp
SWAP1
JUMP
Expand All @@ -95,9 +95,13 @@ rlp_header_medium:
// rlp_pos += 1
%increment

%stack
(rlp_pos, num_nibbles, packed_nibbles, terminated, retdest) ->
(rlp_pos, num_nibbles, packed_nibbles, terminated, remaining_bytes, num_nibbles, packed_nibbles, retdest)
// stack: rlp_pos, num_nibbles, packed_nibbles, terminated, retdest
SWAP3 DUP3 DUP3
// stack: num_nibbles, packed_nibbles, terminated, num_nibbles, packed_nibbles, rlp_pos, retdest
PUSH remaining_bytes
// stack: remaining_bytes, num_nibbles, packed_nibbles, terminated, num_nibbles, packed_nibbles, rlp_pos, retdest
SWAP4 SWAP5 SWAP6
// stack: rlp_pos, num_nibbles, packed_nibbles, terminated, remaining_bytes, num_nibbles, packed_nibbles, retdest

%jump(first_byte)

Expand All @@ -118,9 +122,12 @@ rlp_header_large:
// rlp_pos += 2
%add_const(2)

%stack
(rlp_pos, num_nibbles, packed_nibbles, terminated, retdest) ->
(rlp_pos, num_nibbles, packed_nibbles, terminated, remaining_bytes, num_nibbles, packed_nibbles, retdest)
// stack: rlp_pos, num_nibbles, packed_nibbles, terminated, retdest
SWAP3 DUP3 DUP3
// stack: num_nibbles, packed_nibbles, terminated, num_nibbles, packed_nibbles, rlp_pos, retdest
PUSH remaining_bytes
// stack: remaining_bytes, num_nibbles, packed_nibbles, terminated, num_nibbles, packed_nibbles, rlp_pos, retdest
SWAP4 SWAP5 SWAP6
// stack: rlp_pos, num_nibbles, packed_nibbles, terminated, remaining_bytes, num_nibbles, packed_nibbles, retdest

%jump(first_byte)

16 changes: 16 additions & 0 deletions evm/src/cpu/kernel/tests/kernel_consistency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use anyhow::Result;
use ethereum_types::U256;

use crate::cpu::kernel::aggregator::{combined_kernel, KERNEL};
use crate::cpu::kernel::interpreter::Interpreter;
use crate::memory::segments::Segment;

#[test]
fn test_kernel_code_hash_consistency() -> Result<()> {
for _ in 0..10 {
let kernel2 = combined_kernel();
assert_eq!(kernel2.code_hash, KERNEL.code_hash);
}

Ok(())
}
1 change: 1 addition & 0 deletions evm/src/cpu/kernel/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod core;
mod ecc;
mod exp;
mod hash;
mod kernel_consistency;
mod log;
mod mpt;
mod packing;
Expand Down
15 changes: 0 additions & 15 deletions evm/src/generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,6 @@ fn apply_metadata_and_tries_memops<F: RichField + Extendable<D>, const D: usize>
state.traces.memory_ops.extend(ops);
}

fn initialize_kernel_code<F: RichField + Extendable<D>, const D: usize>(
state: &mut GenerationState<F>,
) {
for (i, &byte) in enumerate(KERNEL.code.iter()) {
let address = MemoryAddress {
context: 0,
segment: Segment::Code as usize,
virt: i,
};
state.memory.set(address, byte.into());
}
}

pub fn generate_traces<F: RichField + Extendable<D>, const D: usize>(
all_stark: &AllStark<F, D>,
inputs: GenerationInputs,
Expand All @@ -219,8 +206,6 @@ pub fn generate_traces<F: RichField + Extendable<D>, const D: usize>(

apply_metadata_and_tries_memops(&mut state, &inputs);

initialize_kernel_code(&mut state);

timed!(timing, "simulate CPU", simulate_cpu(&mut state)?);

assert!(
Expand Down

0 comments on commit 32d0096

Please sign in to comment.