Skip to content

Commit

Permalink
Merge 'main' into merge-not-pop
Browse files Browse the repository at this point in the history
  • Loading branch information
LindaGuiga committed Oct 27, 2023
2 parents 9d1543e + 0258ad4 commit fe97c7b
Show file tree
Hide file tree
Showing 40 changed files with 632 additions and 642 deletions.
8 changes: 8 additions & 0 deletions evm/src/cpu/bootstrap_kernel.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! The initial phase of execution, where the kernel code is hashed while being written to memory.
//! The hash is then checked against a precomputed kernel hash.
use ethereum_types::U256;
use itertools::Itertools;
use plonky2::field::extension::Extendable;
use plonky2::field::packed::PackedField;
Expand Down Expand Up @@ -52,6 +53,13 @@ pub(crate) fn generate_bootstrap_kernel<F: Field>(state: &mut GenerationState<F>
MemoryAddress::new(0, Segment::Code, 0),
KERNEL.code.clone(),
);
state.registers.stack_top = KERNEL
.code_hash
.iter()
.enumerate()
.fold(0.into(), |acc, (i, &elt)| {
acc + (U256::from(elt) << (224 - 32 * i))
});
state.traces.push_cpu(final_cpu_row);
log::info!("Bootstrapping took {} cycles", state.traces.clock());
}
Expand Down
11 changes: 3 additions & 8 deletions evm/src/cpu/cpu_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use super::halt;
use crate::all_stark::Table;
use crate::constraint_consumer::{ConstraintConsumer, RecursiveConstraintConsumer};
use crate::cpu::columns::{COL_MAP, NUM_CPU_COLUMNS};
use crate::cpu::membus::NUM_GP_CHANNELS;
use crate::cpu::{
bootstrap_kernel, contextops, control_flow, decode, dup_swap, gas, jumps, membus, memio,
modfp254, pc, push0, shift, simple_logic, stack, stack_bounds, syscalls_exceptions,
Expand All @@ -41,7 +40,7 @@ pub fn ctl_data_keccak_sponge<F: Field>() -> Vec<Column<F>> {
let timestamp = Column::linear_combination([(COL_MAP.clock, num_channels)]);

let mut cols = vec![context, segment, virt, len, timestamp];
cols.extend(COL_MAP.mem_channels[4].value.map(Column::single));
cols.extend(Column::singles_next_row(COL_MAP.mem_channels[0].value));
cols
}

Expand All @@ -54,9 +53,7 @@ pub fn ctl_filter_keccak_sponge<F: Field>() -> Column<F> {
fn ctl_data_binops<F: Field>() -> Vec<Column<F>> {
let mut res = Column::singles(COL_MAP.mem_channels[0].value).collect_vec();
res.extend(Column::singles(COL_MAP.mem_channels[1].value));
res.extend(Column::singles(
COL_MAP.mem_channels[NUM_GP_CHANNELS - 1].value,
));
res.extend(Column::singles_next_row(COL_MAP.mem_channels[0].value));
res
}

Expand All @@ -68,9 +65,7 @@ fn ctl_data_ternops<F: Field>() -> Vec<Column<F>> {
let mut res = Column::singles(COL_MAP.mem_channels[0].value).collect_vec();
res.extend(Column::singles(COL_MAP.mem_channels[1].value));
res.extend(Column::singles(COL_MAP.mem_channels[2].value));
res.extend(Column::singles(
COL_MAP.mem_channels[NUM_GP_CHANNELS - 1].value,
));
res.extend(Column::singles_next_row(COL_MAP.mem_channels[0].value));
res
}

Expand Down
51 changes: 43 additions & 8 deletions evm/src/cpu/dup_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 Expand Up @@ -129,7 +130,13 @@ fn eval_packed_dup<P: PackedField>(
// Constrain nv.stack_len.
yield_constr.constraint_transition(filter * (nv.stack_len - lv.stack_len - P::ONES));

// TODO: Constrain unused channels?
// Disable next top.
yield_constr.constraint(filter * nv.mem_channels[0].used);

// Constrain unused channels.
for i in 3..NUM_GP_CHANNELS {
yield_constr.constraint(filter * lv.mem_channels[i].used);
}
}

fn eval_ext_circuit_dup<F: RichField + Extendable<D>, const D: usize>(
Expand Down Expand Up @@ -175,11 +182,23 @@ fn eval_ext_circuit_dup<F: RichField + Extendable<D>, const D: usize>(
constrain_channel_ext_circuit(builder, true, filter, n, read_channel, lv, yield_constr);

// Constrain nv.stack_len.
let diff = builder.sub_extension(nv.stack_len, lv.stack_len);
let constr = builder.mul_sub_extension(filter, diff, filter);
yield_constr.constraint_transition(builder, constr);
{
let diff = builder.sub_extension(nv.stack_len, lv.stack_len);
let constr = builder.mul_sub_extension(filter, diff, filter);
yield_constr.constraint_transition(builder, constr);
}

// TODO: Constrain unused channels?
// Disable next top.
{
let constr = builder.mul_extension(filter, nv.mem_channels[0].used);
yield_constr.constraint(builder, constr);
}

// Constrain unused channels.
for i in 3..NUM_GP_CHANNELS {
let constr = builder.mul_extension(filter, lv.mem_channels[i].used);
yield_constr.constraint(builder, constr);
}
}

fn eval_packed_swap<P: PackedField>(
Expand All @@ -203,10 +222,16 @@ fn eval_packed_swap<P: PackedField>(
channels_equal_packed(filter, in2_channel, &nv.mem_channels[0], yield_constr);
constrain_channel_packed(true, filter, n_plus_one, in2_channel, lv, yield_constr);

// Constrain nv.stack_len;
// Constrain nv.stack_len.
yield_constr.constraint(filter * (nv.stack_len - lv.stack_len));

// TODO: Constrain unused channels?
// Disable next top.
yield_constr.constraint(filter * nv.mem_channels[0].used);

// Constrain unused channels.
for i in 3..NUM_GP_CHANNELS {
yield_constr.constraint(filter * lv.mem_channels[i].used);
}
}

fn eval_ext_circuit_swap<F: RichField + Extendable<D>, const D: usize>(
Expand Down Expand Up @@ -259,7 +284,17 @@ fn eval_ext_circuit_swap<F: RichField + Extendable<D>, const D: usize>(
let constr = builder.mul_extension(filter, diff);
yield_constr.constraint(builder, constr);

// TODO: Constrain unused channels?
// Disable next top.
{
let constr = builder.mul_extension(filter, nv.mem_channels[0].used);
yield_constr.constraint(builder, constr);
}

// Constrain unused channels.
for i in 3..NUM_GP_CHANNELS {
let constr = builder.mul_extension(filter, lv.mem_channels[i].used);
yield_constr.constraint(builder, constr);
}
}

pub fn eval_packed<P: PackedField>(
Expand Down
113 changes: 0 additions & 113 deletions evm/src/cpu/kernel/asm/account_code.asm
Original file line number Diff line number Diff line change
Expand Up @@ -80,119 +80,6 @@ global extcodesize:
// stack: extcodesize(address), retdest
SWAP1 JUMP

%macro extcodecopy
// stack: address, dest_offset, offset, size
%stack (address, dest_offset, offset, size) -> (address, dest_offset, offset, size, %%after)
%jump(extcodecopy)
%%after:
%endmacro

// Pre stack: kexit_info, address, dest_offset, offset, size
// Post stack: (empty)
global sys_extcodecopy:
%stack (kexit_info, address, dest_offset, offset, size)
-> (address, dest_offset, offset, size, kexit_info)
%u256_to_addr DUP1 %insert_accessed_addresses
// stack: cold_access, address, dest_offset, offset, size, kexit_info
PUSH @GAS_COLDACCOUNTACCESS_MINUS_WARMACCESS
MUL
PUSH @GAS_WARMACCESS
ADD
// stack: Gaccess, address, dest_offset, offset, size, kexit_info

DUP5
// stack: size, Gaccess, address, dest_offset, offset, size, kexit_info
ISZERO %jumpi(sys_extcodecopy_empty)

// stack: Gaccess, address, dest_offset, offset, size, kexit_info
DUP5 %num_bytes_to_num_words %mul_const(@GAS_COPY) ADD
%stack (gas, address, dest_offset, offset, size, kexit_info) -> (gas, kexit_info, address, dest_offset, offset, size)
%charge_gas

%stack (kexit_info, address, dest_offset, offset, size) -> (dest_offset, size, kexit_info, address, dest_offset, offset, size)
%add_or_fault
// stack: expanded_num_bytes, kexit_info, address, dest_offset, offset, size
DUP1 %ensure_reasonable_offset
%update_mem_bytes

%stack (kexit_info, address, dest_offset, offset, size) -> (address, dest_offset, offset, size, kexit_info)
%extcodecopy
// stack: kexit_info
EXIT_KERNEL

sys_extcodecopy_empty:
%stack (Gaccess, address, dest_offset, offset, size, kexit_info) -> (Gaccess, kexit_info)
%charge_gas
EXIT_KERNEL


// Pre stack: address, dest_offset, offset, size, retdest
// Post stack: (empty)
global extcodecopy:
// stack: address, dest_offset, offset, size, retdest
%stack (address, dest_offset, offset, size, retdest)
-> (address, 0, @SEGMENT_KERNEL_ACCOUNT_CODE, extcodecopy_contd, size, offset, dest_offset, retdest)
%jump(load_code)

extcodecopy_contd:
// stack: code_size, size, offset, dest_offset, retdest
DUP1 DUP4
// stack: offset, code_size, code_size, size, offset, dest_offset, retdest
GT %jumpi(extcodecopy_large_offset)
// stack: code_size, size, offset, dest_offset, retdest
SWAP1
// stack: size, code_size, offset, dest_offset, retdest
PUSH 0

// Loop copying the `code[offset]` to `memory[dest_offset]` until `i==size`.
// Each iteration increments `offset, dest_offset, i`.
// TODO: Consider implementing this with memcpy.
extcodecopy_loop:
// stack: i, size, code_size, offset, dest_offset, retdest
DUP2 DUP2 EQ
// stack: i == size, i, size, code_size, offset, dest_offset, retdest
%jumpi(extcodecopy_end)
%stack (i, size, code_size, offset, dest_offset, retdest)
-> (offset, code_size, offset, code_size, dest_offset, i, size, retdest)
LT
// stack: offset < code_size, offset, code_size, dest_offset, i, size, retdest
DUP2
// stack: offset, offset < code_size, offset, code_size, dest_offset, i, size, retdest
%mload_kernel(@SEGMENT_KERNEL_ACCOUNT_CODE)
// stack: opcode, offset < code_size, offset, code_size, dest_offset, i, size, retdest
%stack (opcode, offset_lt_code_size, offset, code_size, dest_offset, i, size, retdest)
-> (offset_lt_code_size, 0, opcode, offset, code_size, dest_offset, i, size, retdest)
// If `offset >= code_size`, use `opcode=0`. Necessary since `SEGMENT_KERNEL_ACCOUNT_CODE` might be clobbered from previous calls.
%select_bool
// stack: opcode, offset, code_size, dest_offset, i, size, retdest
DUP4
// stack: dest_offset, opcode, offset, code_size, dest_offset, i, size, retdest
%mstore_current(@SEGMENT_MAIN_MEMORY)
// stack: offset, code_size, dest_offset, i, size, retdest
%increment
// stack: offset+1, code_size, dest_offset, i, size, retdest
SWAP2
// stack: dest_offset, code_size, offset+1, i, size, retdest
%increment
// stack: dest_offset+1, code_size, offset+1, i, size, retdest
SWAP3
// stack: i, code_size, offset+1, dest_offset+1, size, retdest
%increment
// stack: i+1, code_size, offset+1, dest_offset+1, size, retdest
%stack (i, code_size, offset, dest_offset, size, retdest) -> (i, size, code_size, offset, dest_offset, retdest)
%jump(extcodecopy_loop)

extcodecopy_end:
%stack (i, size, code_size, offset, dest_offset, retdest) -> (retdest)
JUMP

extcodecopy_large_offset:
// offset is larger than the code size. So we just have to write zeros.
// stack: code_size, size, offset, dest_offset, retdest
GET_CONTEXT
%stack (context, code_size, size, offset, dest_offset, retdest) -> (context, @SEGMENT_MAIN_MEMORY, dest_offset, 0, size, retdest)
%jump(memset)

// Loads the code at `address` into memory, at the given context and segment, starting at offset 0.
// Checks that the hash of the loaded code corresponds to the `codehash` in the state trie.
// Pre stack: address, ctx, segment, retdest
Expand Down
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/asm/bignum/util.asm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
%macro clear_current_general
// stack: dst, len
GET_CONTEXT
%stack (context, dst, len) -> (context, @SEGMENT_KERNEL_GENERAL, dst, 0, len, %%after)
%stack (context, dst, len) -> (context, @SEGMENT_KERNEL_GENERAL, dst, len, %%after)
%jump(memset)
%%after:
%endmacro
24 changes: 15 additions & 9 deletions evm/src/cpu/kernel/asm/core/create_receipt.asm
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,23 @@ process_receipt_after_write:
DUP5
%mpt_insert_receipt_trie
// stack: new_cum_gas, txn_nb, num_nibbles, retdest
// Now, we set the Bloom filter back to 0.
// Now, we set the Bloom filter back to 0. We proceed by chunks of 32 bytes.
PUSH 32
PUSH 0
%rep 256
// stack: counter, new_cum_gas, txn_nb, num_nibbles, retdest
PUSH 0 DUP2
// stack: counter, 0, counter, new_cum_gas, txn_nb, num_nibbles, retdest
%mstore_kernel(@SEGMENT_TXN_BLOOM)
// stack: counter, new_cum_gas, txn_nb, num_nibbles, retdest
%increment
%rep 8
// stack: counter, 32, new_cum_gas, txn_nb, num_nibbles, retdest
DUP2
PUSH 0 // we will fill the memory segment with zeroes
DUP2
PUSH @SEGMENT_TXN_BLOOM
DUP3 // kernel context is 0
// stack: ctx, segment, counter, 0, 32, counter, 32, new_cum_gas, txn_nb, num_nibbles, retdest
MSTORE_32BYTES
// stack: counter, 32, new_cum_gas, txn_nb, num_nibbles, retdest
DUP2
ADD
%endrep
POP
%pop2
// stack: new_cum_gas, txn_nb, num_nibbles, retdest
%stack (new_cum_gas, txn_nb, num_nibbles, retdest) -> (retdest, new_cum_gas)
JUMP
Expand Down
Loading

0 comments on commit fe97c7b

Please sign in to comment.