Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare committed Feb 26, 2024
1 parent bb7f20c commit 08b32da
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 55 deletions.
5 changes: 3 additions & 2 deletions evm_arithmetization/src/cpu/kernel/asm/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ global main:
// Initialize accessed addresses and storage keys lists
%init_access_lists

// Initialize the RLP DATA pointer to its initial position (ctx == virt == 0, segment = RLP)
PUSH @SEGMENT_RLP_RAW
// Initialize the RLP DATA pointer to its initial position,
// skipping over the preinitialized empty node.
PUSH @INITIAL_TXN_RLP_ADDR
%mstore_global_metadata(@GLOBAL_METADATA_RLP_DATA_SIZE)

// Encode constant nodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub(crate) enum GlobalMetadata {
/// The size of the `TrieData` segment, in bytes. In other words, the next
/// address available for appending additional trie data.
TrieDataSize,
/// The size of the `TrieData` segment, in bytes, represented as a whole
/// The size of the `RLP` segment, in bytes, represented as a whole
/// address. In other words, the next address available for appending
/// additional trie data.
/// additional RLP data.
RlpDataSize,
/// A pointer to the root of the state trie within the `TrieData` buffer.
StateTrieRoot,
Expand Down
8 changes: 4 additions & 4 deletions evm_arithmetization/src/cpu/kernel/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,23 @@ const MISC_CONSTANTS: [(&str, [u8; 32]); 4] = [
hex!("0000000000000000000000000000000100000000000000000000000000000000"),
),
// Address where the empty node encoding is stored.
// It is the offset 1 within SEGMENT_RLP_RAW.
// It is at the offset 0 within SEGMENT_RLP_RAW.
// *Note*: Changing this will break some tests.
(
"ENCODED_EMPTY_NODE_ADDR",
hex!("0000000000000000000000000000000000000000000000000000000c00000001"),
hex!("0000000000000000000000000000000000000000000000000000000c00000000"),
),
// 0x10000 = 2^16 bytes, much larger than any RLP blob the EVM could possibly create.
(
"MAX_RLP_BLOB_SIZE",
hex!("0000000000000000000000000000000000000000000000000000000000010000"),
),
// Address where the txn RLP encoding starts.
// It is the offset 2 within SEGMENT_RLP_RAW.
// It is the offset 1 within SEGMENT_RLP_RAW.
// *Note*: Changing this will break some tests.
(
"INITIAL_TXN_RLP_ADDR",
hex!("0000000000000000000000000000000000000000000000000000000c00000002"),
hex!("0000000000000000000000000000000000000000000000000000000c00000001"),
),
];

Expand Down
2 changes: 1 addition & 1 deletion evm_arithmetization/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ impl<'a, F: Field> Interpreter<'a, F> {
pub(crate) fn initialize_rlp_segment(&mut self) {
self.generation_state
.memory
.set(MemoryAddress::new(0, Segment::RlpRaw, 1), 0x80.into())
.set(MemoryAddress::new(0, Segment::RlpRaw, 0), 0x80.into())
}
}

Expand Down
7 changes: 0 additions & 7 deletions evm_arithmetization/src/cpu/kernel/tests/account_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ fn prepare_interpreter<F: Field>(
trie_data.push(account.code_hash.into_uint());
let trie_data_len = trie_data.len().into();
interpreter.set_global_metadata_field(GlobalMetadata::TrieDataSize, trie_data_len);
// Initialize the trie data length in the RlpRaw segment.
interpreter.generation_state.memory.contexts[0].segments[Segment::RlpRaw.unscale()].content
[0] = trie_data_len;
interpreter
.push(0xDEADBEEFu32.into())
.expect("The stack should not overflow");
Expand Down Expand Up @@ -440,10 +437,6 @@ fn sload() -> Result<()> {

// Prepare the interpreter by inserting the account in the state trie.
prepare_interpreter_all_accounts(&mut interpreter, trie_inputs, addr, &code)?;
// Initialize the trie data length in the RlpRaw segment.
let trie_data_len = interpreter.get_trie_data().len().into();
interpreter.generation_state.memory.contexts[0].segments[Segment::RlpRaw.unscale()].content
[0] = trie_data_len;
interpreter.run()?;

// The first two elements in the stack are `success` and `leftover_gas`,
Expand Down
9 changes: 0 additions & 9 deletions evm_arithmetization/src/cpu/kernel/tests/add11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::cpu::kernel::constants::context_metadata::ContextMetadata;
use crate::cpu::kernel::interpreter::Interpreter;
use crate::generation::mpt::{AccountRlp, LegacyReceiptRlp};
use crate::generation::TrieInputs;
use crate::memory::segments::Segment;
use crate::proof::{BlockHashes, BlockMetadata, TrieRoots};
use crate::GenerationInputs;

Expand Down Expand Up @@ -164,10 +163,6 @@ fn test_add11_yml() {
// Switch context and initialize memory with the data we need for the tests.
interpreter.generation_state.registers.program_counter = route_txn_label;
interpreter.set_context_metadata_field(0, ContextMetadata::GasLimit, 1_000_000.into());
let trie_data_len = interpreter.get_trie_data().len().into();
// Initialize the trie data length in the RlpRaw segment.
interpreter.generation_state.memory.contexts[0].segments[Segment::RlpRaw.unscale()].content
[0] = trie_data_len;
interpreter.set_is_kernel(true);
interpreter.run().expect("Proving add11 failed.");
}
Expand Down Expand Up @@ -312,10 +307,6 @@ fn test_add11_yml_with_exception() {
// Switch context and initialize memory with the data we need for the tests.
interpreter.generation_state.registers.program_counter = route_txn_label;
interpreter.set_context_metadata_field(0, ContextMetadata::GasLimit, 1_000_000.into());
let trie_data_len = interpreter.get_trie_data().len().into();
// Initialize the trie data length in the RlpRaw segment.
interpreter.generation_state.memory.contexts[0].segments[Segment::RlpRaw.unscale()].content
[0] = trie_data_len;
interpreter.set_is_kernel(true);
interpreter
.run()
Expand Down
4 changes: 0 additions & 4 deletions evm_arithmetization/src/cpu/kernel/tests/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::cpu::kernel::interpreter::Interpreter;
use crate::cpu::kernel::tests::account_code::initialize_mpts;
use crate::cpu::kernel::tests::mpt::nibbles_64;
use crate::generation::mpt::AccountRlp;
use crate::memory::segments::Segment;
use crate::Node;

// Test account with a given code hash.
Expand Down Expand Up @@ -61,9 +60,6 @@ fn prepare_interpreter<F: Field>(
trie_data.push(account.code_hash.into_uint());
let trie_data_len = trie_data.len().into();
interpreter.set_global_metadata_field(GlobalMetadata::TrieDataSize, trie_data_len);
// Initialize the trie data length in the RlpRaw segment.
interpreter.generation_state.memory.contexts[0].segments[Segment::RlpRaw.unscale()].content
[0] = trie_data_len;
interpreter
.push(0xDEADBEEFu32.into())
.expect("The stack should not overflow");
Expand Down
4 changes: 0 additions & 4 deletions evm_arithmetization/src/cpu/kernel/tests/mpt/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::cpu::kernel::tests::account_code::initialize_mpts;
use crate::cpu::kernel::tests::mpt::{nibbles_64, test_account_1_rlp, test_account_2};
use crate::generation::mpt::AccountRlp;
use crate::generation::TrieInputs;
use crate::memory::segments::Segment;
use crate::Node;

#[test]
Expand Down Expand Up @@ -124,9 +123,6 @@ fn test_state_trie(
trie_data.push(account.code_hash.into_uint());
let trie_data_len = trie_data.len().into();
interpreter.set_global_metadata_field(GlobalMetadata::TrieDataSize, trie_data_len);
// Initialize the trie data length in the RlpRaw segment.
interpreter.generation_state.memory.contexts[0].segments[Segment::RlpRaw.unscale()].content
[0] = trie_data_len;
interpreter
.push(0xDEADBEEFu32.into())
.expect("The stack should not overflow");
Expand Down
5 changes: 0 additions & 5 deletions evm_arithmetization/src/cpu/kernel/tests/mpt/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::cpu::kernel::interpreter::Interpreter;
use crate::cpu::kernel::tests::account_code::initialize_mpts;
use crate::cpu::kernel::tests::mpt::{extension_to_leaf, test_account_1_rlp, test_account_2_rlp};
use crate::generation::TrieInputs;
use crate::memory::segments::Segment;
use crate::Node;

// TODO: Test with short leaf. Might need to be a storage trie.
Expand Down Expand Up @@ -126,10 +125,6 @@ fn test_state_trie(trie_inputs: TrieInputs) -> Result<()> {
interpreter
.push(1.into()) // Initial length of the trie data segment, unused.
.expect("The stack should not overflow");
let trie_data_len = interpreter.get_trie_data().len().into();
// Initialize the trie data length in the RlpRaw segment.
interpreter.generation_state.memory.contexts[0].segments[Segment::RlpRaw.unscale()].content
[0] = trie_data_len;
interpreter.run()?;

assert_eq!(
Expand Down
6 changes: 3 additions & 3 deletions evm_arithmetization/src/cpu/kernel/tests/mpt/hex_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ fn hex_prefix_odd_terminated_tiny() -> Result<()> {
assert_eq!(
interpreter.get_rlp_memory(),
vec![
// The two first values of the RLP segment are trie_data_len (here 0), and the
// hardcoded 0x80 for an empty node.
0,
// The two first values of the RLP segment are the hardcoded 0x80 for an empty
// node, and 0 (i.e. unset).
0x80,
0,
// No length prefix; this tiny string is its own RLP encoding.
(2 + 1) * 16 + 0xA,
]
Expand Down
4 changes: 0 additions & 4 deletions evm_arithmetization/src/cpu/kernel/tests/mpt/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::cpu::kernel::tests::mpt::{
};
use crate::generation::mpt::AccountRlp;
use crate::generation::TrieInputs;
use crate::memory::segments::Segment;
use crate::Node;

#[test]
Expand Down Expand Up @@ -199,9 +198,6 @@ fn test_state_trie(
trie_data.push(account.code_hash.into_uint());
let trie_data_len = trie_data.len().into();
interpreter.set_global_metadata_field(GlobalMetadata::TrieDataSize, trie_data_len);
// Initialize the trie data length in the RlpRaw segment.
interpreter.generation_state.memory.contexts[0].segments[Segment::RlpRaw.unscale()].content
[0] = trie_data_len;
interpreter
.push(0xDEADBEEFu32.into())
.expect("The stack should not overflow");
Expand Down
5 changes: 1 addition & 4 deletions evm_arithmetization/src/cpu/kernel/tests/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn test_receipt_encoding() -> Result<()> {
interpreter.run()?;
let rlp_pos = interpreter.pop().expect("The stack should not be empty");

let rlp_read: &[u8] = &interpreter.get_rlp_memory()[2..]; // skip trie_data_len and empty_node
let rlp_read: &[u8] = &interpreter.get_rlp_memory()[1..]; // skip empty_node

assert_eq!(rlp_pos.as_usize(), expected_rlp.len());
for i in 0..rlp_read.len() {
Expand Down Expand Up @@ -515,9 +515,6 @@ fn test_mpt_insert_receipt() -> Result<()> {
interpreter.set_memory_segment(Segment::TrieData, cur_trie_data.clone());
let trie_data_len = cur_trie_data.len().into();
interpreter.set_global_metadata_field(GlobalMetadata::TrieDataSize, trie_data_len);
// Initialize the trie data length in the RlpRaw segment.
interpreter.generation_state.memory.contexts[0].segments[Segment::RlpRaw.unscale()].content
[0] = trie_data_len;
interpreter.run()?;

// Finally, check that the hashes correspond.
Expand Down
12 changes: 6 additions & 6 deletions evm_arithmetization/src/cpu/kernel/tests/rlp/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ fn test_encode_rlp_scalar_small() -> Result<()> {
interpreter.run()?;
let expected_stack = vec![pos + U256::from(1)]; // pos' = pos + rlp_len = 2 + 1

// The two first values of the RLP segment are trie_data_len (here 0), and the
// hardcoded 0x80 for an empty node.
let expected_rlp = vec![0, 0x80, 42];
// The two first values of the RLP segment are the hardcoded 0x80 for an empty
// node, and 0 (i.e. unset).
let expected_rlp = vec![0x80, 0, 42];

assert_eq!(interpreter.stack(), expected_stack);
assert_eq!(interpreter.get_rlp_memory(), expected_rlp);
Expand All @@ -44,9 +44,9 @@ fn test_encode_rlp_scalar_medium() -> Result<()> {
interpreter.run()?;
let expected_stack = vec![pos + U256::from(4)]; // pos' = pos + rlp_len = 2 + 4

// The two first values of the RLP segment are trie_data_len (here 0), and the
// hardcoded 0x80 for an empty node.
let expected_rlp = vec![0, 0x80, 0x80 + 3, 0x01, 0x23, 0x45];
// The two first values of the RLP segment are the hardcoded 0x80 for an empty
// node, and 0 (i.e. unset).
let expected_rlp = vec![0x80, 0, 0x80 + 3, 0x01, 0x23, 0x45];

assert_eq!(interpreter.stack(), expected_stack);
assert_eq!(interpreter.get_rlp_memory(), expected_rlp);
Expand Down

0 comments on commit 08b32da

Please sign in to comment.