Skip to content

Commit

Permalink
Some cleanup (0xPolygonZero#1498)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare authored Feb 3, 2024
1 parent 6357963 commit f3f7433
Show file tree
Hide file tree
Showing 38 changed files with 38 additions and 219 deletions.
2 changes: 0 additions & 2 deletions evm/src/all_stark.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use core::iter;
use core::ops::Deref;

use itertools::Itertools;
use plonky2::field::extension::Extendable;
use plonky2::field::types::Field;
use plonky2::hash::hash_types::RichField;
Expand Down
3 changes: 1 addition & 2 deletions evm/src/byte_packing/byte_packing_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use plonky2::timed;
use plonky2::util::timing::TimingTree;
use plonky2::util::transpose;

use super::columns::BYTE_VALUES_RANGE;
use super::NUM_BYTES;
use crate::byte_packing::columns::{
index_len, value_bytes, ADDR_CONTEXT, ADDR_SEGMENT, ADDR_VIRTUAL, IS_READ, LEN_INDICES_COLS,
Expand All @@ -62,7 +61,7 @@ pub(crate) fn ctl_looked_data<F: Field>() -> Vec<Column<F>> {
// obtain the corresponding limb.
let outputs: Vec<Column<F>> = (0..8)
.map(|i| {
let range = (value_bytes(i * 4)..value_bytes(i * 4) + 4);
let range = value_bytes(i * 4)..value_bytes(i * 4) + 4;
Column::linear_combination(
range
.enumerate()
Expand Down
4 changes: 0 additions & 4 deletions evm/src/byte_packing/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ pub(crate) const fn value_bytes(i: usize) -> usize {
BYTES_VALUES_START + i
}

/// Range of columns containing the bytes values.
pub(crate) const BYTE_VALUES_RANGE: Range<usize> =
BYTES_VALUES_START..BYTES_VALUES_START + NUM_BYTES;

/// The counter column (used for the range check) starts from 0 and increments.
pub(crate) const RANGE_COUNTER: usize = BYTES_VALUES_START + NUM_BYTES;
/// The frequencies column used in logUp.
Expand Down
11 changes: 4 additions & 7 deletions evm/src/cpu/contextops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ use plonky2::plonk::circuit_builder::CircuitBuilder;

use super::columns::ops::OpsColumnsView;
use super::cpu_stark::{disable_unused_channels, disable_unused_channels_circuit};
use super::membus::NUM_GP_CHANNELS;
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::cpu::columns::CpuColumnsView;
use crate::cpu::kernel::constants::context_metadata::ContextMetadata;
use crate::memory::segments::Segment;
use crate::memory::VALUE_LIMBS;

// If true, the instruction will keep the current context for the next row.
// If false, next row's context is handled manually.
Expand Down Expand Up @@ -88,7 +85,7 @@ fn eval_packed_get<P: PackedField>(
// Context is scaled by 2^64, hence stored in the 3rd limb.
yield_constr.constraint(filter * (new_stack_top[2] - lv.context));

for (i, &limb) in new_stack_top.iter().enumerate().filter(|(i, _)| *i != 2) {
for (_, &limb) in new_stack_top.iter().enumerate().filter(|(i, _)| *i != 2) {
yield_constr.constraint(filter * limb);
}

Expand Down Expand Up @@ -119,7 +116,7 @@ fn eval_ext_circuit_get<F: RichField + Extendable<D>, const D: usize>(
yield_constr.constraint(builder, constr);
}

for (i, &limb) in new_stack_top.iter().enumerate().filter(|(i, _)| *i != 2) {
for (_, &limb) in new_stack_top.iter().enumerate().filter(|(i, _)| *i != 2) {
let constr = builder.mul_extension(filter, limb);
yield_constr.constraint(builder, constr);
}
Expand Down Expand Up @@ -151,7 +148,7 @@ fn eval_packed_set<P: PackedField>(

// The next row's context is read from stack_top.
yield_constr.constraint(filter * (stack_top[2] - nv.context));
for (i, &limb) in stack_top.iter().enumerate().filter(|(i, _)| *i != 2) {
for (_, &limb) in stack_top.iter().enumerate().filter(|(i, _)| *i != 2) {
yield_constr.constraint(filter * limb);
}

Expand Down Expand Up @@ -199,7 +196,7 @@ fn eval_ext_circuit_set<F: RichField + Extendable<D>, const D: usize>(
let constr = builder.mul_extension(filter, diff);
yield_constr.constraint(builder, constr);
}
for (i, &limb) in stack_top.iter().enumerate().filter(|(i, _)| *i != 2) {
for (_, &limb) in stack_top.iter().enumerate().filter(|(i, _)| *i != 2) {
let constr = builder.mul_extension(filter, limb);
yield_constr.constraint(builder, constr);
}
Expand Down
2 changes: 1 addition & 1 deletion evm/src/cpu/cpu_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::cpu::{
};
use crate::cross_table_lookup::TableWithColumns;
use crate::evaluation_frame::{StarkEvaluationFrame, StarkFrame};
use crate::lookup::{Column, Filter, Lookup};
use crate::lookup::{Column, Filter};
use crate::memory::segments::Segment;
use crate::memory::{NUM_CHANNELS, VALUE_LIMBS};
use crate::stark::Stark;
Expand Down
1 change: 0 additions & 1 deletion evm/src/cpu/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use plonky2::field::packed::PackedField;
use plonky2::field::types::Field;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::ext_target::ExtensionTarget;
use plonky2::plonk::circuit_builder::CircuitBuilder;

use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::cpu::columns::{CpuColumnsView, COL_MAP};
Expand Down
1 change: 0 additions & 1 deletion evm/src/cpu/dup_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use plonky2::hash::hash_types::RichField;
use plonky2::iop::ext_target::ExtensionTarget;
use plonky2::plonk::circuit_builder::CircuitBuilder;

use super::membus::NUM_GP_CHANNELS;
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::cpu::columns::{CpuColumnsView, MemoryChannelView};
use crate::memory::segments::Segment;
Expand Down
1 change: 0 additions & 1 deletion evm/src/cpu/kernel/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ fn push_target_size(target: &PushTarget) -> u8 {
#[cfg(test)]
mod tests {
use super::*;
use crate::cpu::kernel::assembler::*;
use crate::cpu::kernel::parser::parse;

#[test]
Expand Down
1 change: 0 additions & 1 deletion evm/src/cpu/kernel/constants/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::collections::HashMap;

use ethereum_types::U256;
use hex_literal::hex;
use static_assertions::const_assert;

use crate::cpu::kernel::constants::context_metadata::ContextMetadata;
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
Expand Down
1 change: 0 additions & 1 deletion evm/src/cpu/kernel/cost_estimator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::opcodes::get_opcode;
use crate::cpu::kernel::assembler::BYTES_PER_OFFSET;
use crate::cpu::kernel::ast::Item;
use crate::cpu::kernel::ast::Item::*;
Expand Down
72 changes: 3 additions & 69 deletions evm/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use core::cmp::Ordering;
use core::ops::Range;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::collections::{BTreeSet, HashMap};

use anyhow::bail;
use eth_trie_utils::partial_trie::PartialTrie;
Expand All @@ -11,7 +11,6 @@ use keccak_hash::keccak;
use plonky2::field::goldilocks_field::GoldilocksField;

use super::assembler::BYTES_PER_OFFSET;
use super::utils::u256_from_bool;
use crate::cpu::kernel::aggregator::KERNEL;
use crate::cpu::kernel::constants::context_metadata::ContextMetadata;
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
Expand Down Expand Up @@ -1393,70 +1392,6 @@ impl<'a> Interpreter<'a> {
}
}

// Computes the two's complement of the given integer.
fn two_complement(x: U256) -> U256 {
let flipped_bits = x ^ MINUS_ONE;
flipped_bits.overflowing_add(U256::one()).0
}

fn signed_cmp(x: U256, y: U256) -> Ordering {
let x_is_zero = x.is_zero();
let y_is_zero = y.is_zero();

if x_is_zero && y_is_zero {
return Ordering::Equal;
}

let x_is_pos = x.eq(&(x & SIGN_MASK));
let y_is_pos = y.eq(&(y & SIGN_MASK));

if x_is_zero {
if y_is_pos {
return Ordering::Less;
} else {
return Ordering::Greater;
}
};

if y_is_zero {
if x_is_pos {
return Ordering::Greater;
} else {
return Ordering::Less;
}
};

match (x_is_pos, y_is_pos) {
(true, true) => x.cmp(&y),
(true, false) => Ordering::Greater,
(false, true) => Ordering::Less,
(false, false) => x.cmp(&y).reverse(),
}
}

/// -1 in two's complement representation consists in all bits set to 1.
const MINUS_ONE: U256 = U256([
0xffffffffffffffff,
0xffffffffffffffff,
0xffffffffffffffff,
0xffffffffffffffff,
]);

/// -2^255 in two's complement representation consists in the MSB set to 1.
const MIN_VALUE: U256 = U256([
0x0000000000000000,
0x0000000000000000,
0x0000000000000000,
0x8000000000000000,
]);

const SIGN_MASK: U256 = U256([
0xffffffffffffffff,
0xffffffffffffffff,
0xffffffffffffffff,
0x7fffffffffffffff,
]);

fn get_mnemonic(opcode: u8) -> &'static str {
match opcode {
0x00 => "STOP",
Expand Down Expand Up @@ -1651,7 +1586,6 @@ fn get_mnemonic(opcode: u8) -> &'static str {
}
}

#[macro_use]
macro_rules! unpack_address {
($addr:ident) => {{
let offset = $addr.low_u32() as usize;
Expand Down Expand Up @@ -1729,8 +1663,8 @@ mod tests {
interpreter.run()?;

// sys_stop returns `success` and `cum_gas_used`, that we need to pop.
interpreter.pop();
interpreter.pop();
interpreter.pop().expect("Stack should not be empty");
interpreter.pop().expect("Stack should not be empty");

assert_eq!(interpreter.stack(), &[0xff.into(), 0xff00.into()]);
assert_eq!(
Expand Down
6 changes: 3 additions & 3 deletions evm/src/cpu/kernel/tests/account_code.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use anyhow::{anyhow, Result};
use anyhow::Result;
use eth_trie_utils::nibbles::Nibbles;
use eth_trie_utils::partial_trie::{HashedPartialTrie, PartialTrie};
use ethereum_types::{Address, BigEndianHash, H256, U256};
Expand Down Expand Up @@ -327,8 +327,8 @@ fn sstore() -> Result<()> {

// The first two elements in the stack are `success` and `leftover_gas`,
// returned by the `sys_stop` opcode.
interpreter.pop();
interpreter.pop();
interpreter.pop().expect("Stack should not be empty");
interpreter.pop().expect("Stack should not be empty");

// The code should have added an element to the storage of `to_account`. We run
// `mpt_hash_state_trie` to check that.
Expand Down
8 changes: 1 addition & 7 deletions evm/src/cpu/kernel/tests/add11.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
use std::collections::HashMap;
use std::str::FromStr;

use anyhow::{anyhow, Result};
use eth_trie_utils::nibbles::Nibbles;
use eth_trie_utils::partial_trie::{HashedPartialTrie, Node, PartialTrie};
use ethereum_types::{Address, BigEndianHash, H256, U256};
use ethereum_types::{Address, BigEndianHash, H256};
use hex_literal::hex;
use keccak_hash::keccak;

use crate::cpu::kernel::aggregator::KERNEL;
use crate::cpu::kernel::constants::context_metadata::ContextMetadata;
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
use crate::cpu::kernel::interpreter::Interpreter;
use crate::cpu::kernel::tests::account_code::initialize_mpts;
use crate::generation::mpt::{AccountRlp, LegacyReceiptRlp};
use crate::generation::rlp::all_rlp_prover_inputs_reversed;
use crate::generation::TrieInputs;
use crate::memory::segments::Segment;
use crate::proof::{BlockHashes, BlockMetadata, TrieRoots};
use crate::util::h2u;
use crate::GenerationInputs;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/tests/balance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use anyhow::Result;
use eth_trie_utils::partial_trie::{HashedPartialTrie, PartialTrie};
use ethereum_types::{Address, BigEndianHash, H256, U256};
use keccak_hash::keccak;
Expand Down
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/tests/exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ethereum_types::U256;
use rand::{thread_rng, Rng};

use crate::cpu::kernel::aggregator::KERNEL;
use crate::cpu::kernel::interpreter::{run, run_interpreter, Interpreter};
use crate::cpu::kernel::interpreter::{run_interpreter, Interpreter};

#[test]
fn test_exp() -> Result<()> {
Expand Down
3 changes: 0 additions & 3 deletions evm/src/cpu/kernel/tests/kernel_consistency.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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<()> {
Expand Down
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/tests/mpt/delete.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use anyhow::Result;
use eth_trie_utils::nibbles::Nibbles;
use eth_trie_utils::partial_trie::{HashedPartialTrie, PartialTrie};
use ethereum_types::{BigEndianHash, H256, U512};
Expand Down
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/tests/mpt/hash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use anyhow::Result;
use eth_trie_utils::partial_trie::PartialTrie;
use ethereum_types::{BigEndianHash, H256};

Expand Down
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/tests/mpt/insert.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use anyhow::Result;
use eth_trie_utils::nibbles::Nibbles;
use eth_trie_utils::partial_trie::{HashedPartialTrie, PartialTrie};
use ethereum_types::{BigEndianHash, H256};
Expand Down
3 changes: 1 addition & 2 deletions evm/src/cpu/kernel/tests/mpt/load.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::str::FromStr;

use anyhow::{anyhow, Result};
use anyhow::Result;
use eth_trie_utils::nibbles::Nibbles;
use eth_trie_utils::partial_trie::HashedPartialTrie;
use ethereum_types::{BigEndianHash, H256, U256};
use hex_literal::hex;

use crate::cpu::kernel::aggregator::KERNEL;
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
use crate::cpu::kernel::constants::trie_type::PartialTrieType;
use crate::cpu::kernel::interpreter::Interpreter;
Expand Down
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/tests/mpt/read.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use anyhow::Result;
use ethereum_types::BigEndianHash;

use crate::cpu::kernel::aggregator::KERNEL;
Expand Down
5 changes: 1 addition & 4 deletions evm/src/cpu/kernel/tests/receipt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use anyhow::Result;
use ethereum_types::{Address, U256};
use hex_literal::hex;
use keccak_hash::keccak;
Expand Down Expand Up @@ -408,9 +408,6 @@ fn test_mpt_insert_receipt() -> Result<()> {
receipt.push(num_logs.into()); // num_logs
receipt.extend(logs_0.clone());

// First, we load all mpts.
let initial_stack: Vec<U256> = vec![retdest];

let mut interpreter = Interpreter::new_with_kernel(0, vec![]);
initialize_mpts(&mut interpreter, &trie_inputs);

Expand Down
3 changes: 1 addition & 2 deletions evm/src/cpu/syscalls_exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use plonky2::field::packed::PackedField;
use plonky2::field::types::Field;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::ext_target::ExtensionTarget;
use static_assertions::const_assert;

use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::cpu::columns::CpuColumnsView;
Expand Down Expand Up @@ -227,7 +226,7 @@ pub(crate) fn eval_ext_circuit<F: RichField + Extendable<D>, const D: usize>(
{
let diff_syscall =
builder.sub_extension(jumpdest_channel.addr_virtual, opcode_handler_addr_start);
let constr = builder.mul_extension((filter_syscall), diff_syscall);
let constr = builder.mul_extension(filter_syscall, diff_syscall);
yield_constr.constraint(builder, constr);
}
{
Expand Down
Loading

0 comments on commit f3f7433

Please sign in to comment.