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

Implement BLOBBASEFEE #1313

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion evm/src/cpu/kernel/asm/core/exception.asm
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ min_stack_len_for_opcode:
BYTES 0 // 0x46, CHAINID
BYTES 0 // 0x47, SELFBALANCE
BYTES 0 // 0x48, BASEFEE
%rep 7 // 0x49-0x4f, invalid
BYTES 0 // 0x49, invalid
BYTES 0 // 0x4a, BLOBBASEFEE
%rep 5 // 0x4b-0x4f, invalid
BYTES 0
%endrep

Expand Down
6 changes: 4 additions & 2 deletions evm/src/cpu/kernel/asm/core/syscall.asm
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ global syscall_jumptable:
JUMPTABLE sys_chainid
JUMPTABLE sys_selfbalance
JUMPTABLE sys_basefee
%rep 7
JUMPTABLE panic // 0x49-0x4f are invalid opcodes
JUMPTABLE panic // 0x49 is invalid
JUMPTABLE sys_blobbasefee
%rep 5
JUMPTABLE panic // 0x4b-0x4f are invalid opcodes
%endrep

// 0x50-0x5f
Expand Down
13 changes: 13 additions & 0 deletions evm/src/cpu/kernel/asm/memory/metadata.asm
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ global sys_chainid:
%mload_global_metadata(@GLOBAL_METADATA_BLOCK_BASE_FEE)
%endmacro

%macro blobbasefee
%mload_global_metadata(@GLOBAL_METADATA_BLOCK_BLOB_BASE_FEE)
%endmacro

global sys_basefee:
// stack: kexit_info
%charge_gas_const(@GAS_BASE)
Expand All @@ -235,6 +239,15 @@ global sys_basefee:
SWAP1
EXIT_KERNEL

global sys_blobbasefee:
// stack: kexit_info
%charge_gas_const(@GAS_BASE)
// stack: kexit_info
%blobbasefee
// stack: blobbasefee, kexit_info
SWAP1
EXIT_KERNEL

global sys_blockhash:
// stack: kexit_info, block_number
%charge_gas_const(@GAS_BLOCKHASH)
Expand Down
1 change: 1 addition & 0 deletions evm/src/cpu/kernel/constants/exc_bitfields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub const STACK_LENGTH_INCREASING_OPCODES_USER: U256 = u256_from_set_index_range
0x3a..=0x3a, // GASPRICE
0x3d..=0x3d, // RETURNDATASIZE
0x41..=0x48, // COINBASE, TIMESTAMP, NUMBER, DIFFICULTY, GASLIMIT, CHAINID, SELFBALANCE, BASEFEE
0x4a..=0x4a, // BLOBBASEFEE
0x58..=0x5a, // PC, MSIZE, GAS
0x5f..=0x8f, // PUSH*, DUP*
]);
Expand Down
5 changes: 4 additions & 1 deletion evm/src/cpu/kernel/constants/global_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ pub(crate) enum GlobalMetadata {
LogsPayloadLen = 43,
TxnNumberBefore = 44,
TxnNumberAfter = 45,
BlockBlobBaseFee = 46,
}

impl GlobalMetadata {
pub(crate) const COUNT: usize = 46;
pub(crate) const COUNT: usize = 47;

pub(crate) fn all() -> [Self; Self::COUNT] {
[
Expand Down Expand Up @@ -138,6 +139,7 @@ impl GlobalMetadata {
Self::BlockCurrentHash,
Self::TxnNumberBefore,
Self::TxnNumberAfter,
Self::BlockBlobBaseFee,
]
}

Expand Down Expand Up @@ -190,6 +192,7 @@ impl GlobalMetadata {
Self::LogsPayloadLen => "GLOBAL_METADATA_LOGS_PAYLOAD_LEN",
Self::TxnNumberBefore => "GLOBAL_METADATA_TXN_NUMBER_BEFORE",
Self::TxnNumberAfter => "GLOBAL_METADATA_TXN_NUMBER_AFTER",
Self::BlockBlobBaseFee => "GLOBAL_METADATA_BLOCK_BLOB_BASE_FEE",
}
}
}
6 changes: 6 additions & 0 deletions evm/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ impl<'a> Interpreter<'a> {
0x46 => self.run_chainid(), // "CHAINID",
0x48 => self.run_basefee(), // "BASEFEE",
0x49 => self.run_prover_input()?, // "PROVER_INPUT",
0x4a => self.run_blobbasefee(), // "BLOBBASEFEE",
0x50 => self.run_pop(), // "POP",
0x51 => self.run_mload(), // "MLOAD",
0x52 => self.run_mstore(), // "MSTORE",
Expand Down Expand Up @@ -949,6 +950,10 @@ impl<'a> Interpreter<'a> {
Ok(())
}

fn run_blobbasefee(&mut self) {
self.push(self.get_global_metadata_field(GlobalMetadata::BlockBlobBaseFee))
}

fn run_pop(&mut self) {
self.pop();
}
Expand Down Expand Up @@ -1274,6 +1279,7 @@ fn get_mnemonic(opcode: u8) -> &'static str {
0x46 => "CHAINID",
0x48 => "BASEFEE",
0x49 => "PROVER_INPUT",
0x4a => "BLOBBASEFEE",
0x50 => "POP",
0x51 => "MLOAD",
0x52 => "MSTORE",
Expand Down
1 change: 1 addition & 0 deletions evm/src/cpu/kernel/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub fn get_opcode(mnemonic: &str) -> u8 {
"CHAINID" => 0x46,
"BASEFEE" => 0x48,
"PROVER_INPUT" => 0x49,
"BLOBBASEFEE" => 0x4a,
"POP" => 0x50,
"MLOAD" => 0x51,
"MSTORE" => 0x52,
Expand Down
4 changes: 4 additions & 0 deletions evm/src/generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ fn apply_metadata_and_tries_memops<F: RichField + Extendable<D>, const D: usize>
h2u(inputs.block_hashes.cur_hash),
),
(GlobalMetadata::BlockGasUsed, metadata.block_gas_used),
(
GlobalMetadata::BlockBlobBaseFee,
metadata.block_blob_base_fee,
),
(GlobalMetadata::BlockGasUsedBefore, inputs.gas_used_before),
(GlobalMetadata::BlockGasUsedAfter, inputs.gas_used_after),
(GlobalMetadata::TxnNumberBefore, inputs.txn_number_before),
Expand Down
22 changes: 20 additions & 2 deletions evm/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ pub struct BlockMetadata {
pub block_base_fee: U256,
/// The total gas used in this block. It must fit in a `u64`.
pub block_gas_used: U256,
/// The blob base fee. It must fit in a `u64`.
pub block_blob_base_fee: U256,
/// The block bloom of this block, represented as the consecutive
/// 32-byte chunks of a block's final bloom filter string.
pub block_bloom: [U256; 8],
Expand Down Expand Up @@ -180,6 +182,7 @@ impl PublicValuesTarget {
block_chain_id,
block_base_fee,
block_gas_used,
block_blob_base_fee,
block_bloom,
} = self.block_metadata;

Expand All @@ -192,6 +195,7 @@ impl PublicValuesTarget {
buffer.write_target(block_chain_id)?;
buffer.write_target_array(&block_base_fee)?;
buffer.write_target_array(&block_gas_used)?;
buffer.write_target_array(&block_blob_base_fee)?;
buffer.write_target_array(&block_bloom)?;

let BlockHashesTarget {
Expand Down Expand Up @@ -244,6 +248,7 @@ impl PublicValuesTarget {
block_chain_id: buffer.read_target()?,
block_base_fee: buffer.read_target_array()?,
block_gas_used: buffer.read_target_array()?,
block_blob_base_fee: buffer.read_target_array()?,
block_bloom: buffer.read_target_array()?,
};

Expand Down Expand Up @@ -418,11 +423,12 @@ pub struct BlockMetadataTarget {
pub block_chain_id: Target,
pub block_base_fee: [Target; 2],
pub block_gas_used: [Target; 2],
pub block_blob_base_fee: [Target; 2],
pub block_bloom: [Target; 64],
}

impl BlockMetadataTarget {
pub const SIZE: usize = 87;
pub const SIZE: usize = 89;

pub fn from_public_inputs(pis: &[Target]) -> Self {
let block_beneficiary = pis[0..5].try_into().unwrap();
Expand All @@ -434,7 +440,8 @@ impl BlockMetadataTarget {
let block_chain_id = pis[18];
let block_base_fee = pis[19..21].try_into().unwrap();
let block_gas_used = pis[21..23].try_into().unwrap();
let block_bloom = pis[23..87].try_into().unwrap();
let block_blob_base_fee = pis[23..25].try_into().unwrap();
let block_bloom = pis[25..89].try_into().unwrap();

Self {
block_beneficiary,
Expand All @@ -446,6 +453,7 @@ impl BlockMetadataTarget {
block_chain_id,
block_base_fee,
block_gas_used,
block_blob_base_fee,
block_bloom,
}
}
Expand Down Expand Up @@ -480,6 +488,13 @@ impl BlockMetadataTarget {
block_gas_used: core::array::from_fn(|i| {
builder.select(condition, bm0.block_gas_used[i], bm1.block_gas_used[i])
}),
block_blob_base_fee: core::array::from_fn(|i| {
builder.select(
condition,
bm0.block_blob_base_fee[i],
bm1.block_blob_base_fee[i],
)
}),
block_bloom: core::array::from_fn(|i| {
builder.select(condition, bm0.block_bloom[i], bm1.block_bloom[i])
}),
Expand Down Expand Up @@ -510,6 +525,9 @@ impl BlockMetadataTarget {
for i in 0..2 {
builder.connect(bm0.block_gas_used[i], bm1.block_gas_used[i])
}
for i in 0..2 {
builder.connect(bm0.block_blob_base_fee[i], bm1.block_blob_base_fee[i])
}
for i in 0..64 {
builder.connect(bm0.block_bloom[i], bm1.block_bloom[i])
}
Expand Down
16 changes: 13 additions & 3 deletions evm/src/recursive_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,9 @@ pub(crate) fn get_memory_extra_looking_products_circuit<
];

// This contains the `block_beneficiary`, `block_random`, `block_base_fee`,
// `block_gaslimit`, `block_gas_used` as well as `cur_hash`, `gas_used_before`
// and `gas_used_after`.
let block_fields_arrays: [(usize, &[Target]); 8] = [
// `block_gaslimit`, `block_gas_used`, `block_blob_base_fee` as well as `cur_hash`,
// `gas_used_before` and `gas_used_after`.
let block_fields_arrays: [(usize, &[Target]); 9] = [
(
GlobalMetadata::BlockBeneficiary as usize,
&public_values.block_metadata.block_beneficiary,
Expand All @@ -481,6 +481,10 @@ pub(crate) fn get_memory_extra_looking_products_circuit<
GlobalMetadata::BlockGasUsed as usize,
&public_values.block_metadata.block_gas_used,
),
(
GlobalMetadata::BlockBlobBaseFee as usize,
&public_values.block_metadata.block_blob_base_fee,
),
(
GlobalMetadata::BlockCurrentHash as usize,
&public_values.block_hashes.cur_hash,
Expand Down Expand Up @@ -708,6 +712,7 @@ pub(crate) fn add_virtual_block_metadata<F: RichField + Extendable<D>, const D:
let block_chain_id = builder.add_virtual_public_input();
let block_base_fee = builder.add_virtual_public_input_arr();
let block_gas_used = builder.add_virtual_public_input_arr();
let block_blob_base_fee = builder.add_virtual_public_input_arr();
let block_bloom = builder.add_virtual_public_input_arr();
BlockMetadataTarget {
block_beneficiary,
Expand All @@ -719,6 +724,7 @@ pub(crate) fn add_virtual_block_metadata<F: RichField + Extendable<D>, const D:
block_chain_id,
block_base_fee,
block_gas_used,
block_blob_base_fee,
block_bloom,
}
}
Expand Down Expand Up @@ -969,6 +975,10 @@ where
let gas_used = u256_to_u64(block_metadata.block_gas_used)?;
witness.set_target(block_metadata_target.block_gas_used[0], gas_used.0);
witness.set_target(block_metadata_target.block_gas_used[1], gas_used.1);
// Blobbasefee fits in 2 limbs
let blob_basefee = u256_to_u64(block_metadata.block_blob_base_fee)?;
witness.set_target(block_metadata_target.block_blob_base_fee[0], blob_basefee.0);
witness.set_target(block_metadata_target.block_blob_base_fee[1], blob_basefee.1);
let mut block_bloom_limbs = [F::ZERO; 64];
for (i, limbs) in block_bloom_limbs.chunks_exact_mut(8).enumerate() {
limbs.copy_from_slice(&u256_limbs(block_metadata.block_bloom[i]));
Expand Down
8 changes: 8 additions & 0 deletions evm/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ where
GlobalMetadata::BlockGasUsed,
public_values.block_metadata.block_gas_used,
),
(
GlobalMetadata::BlockBlobBaseFee,
public_values.block_metadata.block_blob_base_fee,
),
(
GlobalMetadata::TxnNumberBefore,
public_values.extra_block_data.txn_number_before,
Expand Down Expand Up @@ -509,6 +513,10 @@ pub(crate) mod testutils {
GlobalMetadata::BlockGasUsed,
public_values.block_metadata.block_gas_used,
),
(
GlobalMetadata::BlockBlobBaseFee,
public_values.block_metadata.block_blob_base_fee,
),
(
GlobalMetadata::TxnNumberBefore,
public_values.extra_block_data.txn_number_before,
Expand Down
1 change: 1 addition & 0 deletions evm/src/witness/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ fn decode(registers: RegistersState, opcode: u8) -> Result<Operation, ProgramErr
(0x47, _) => Ok(Operation::Syscall(opcode, 0, true)), // SELFBALANCE
(0x48, _) => Ok(Operation::Syscall(opcode, 0, true)), // BASEFEE
(0x49, true) => Ok(Operation::ProverInput),
(0x4a, _) => Ok(Operation::Syscall(opcode, 0, true)), // BLOBBASEFEE
(0x50, _) => Ok(Operation::Pop),
(0x51, _) => Ok(Operation::Syscall(opcode, 1, false)), // MLOAD
(0x52, _) => Ok(Operation::Syscall(opcode, 2, false)), // MSTORE
Expand Down
1 change: 1 addition & 0 deletions evm/tests/add11_yml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn add11_yml() -> anyhow::Result<()> {
block_chain_id: 1.into(),
block_base_fee: 0xa.into(),
block_gas_used: 0xa868u64.into(),
block_blob_base_fee: 0x2.into(),
block_bloom: [0.into(); 8],
};

Expand Down
1 change: 1 addition & 0 deletions evm/tests/basic_smart_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ fn test_basic_smart_contract() -> anyhow::Result<()> {
block_gas_used: gas_used.into(),
block_bloom: [0.into(); 8],
block_base_fee: 0xa.into(),
block_blob_base_fee: 0x2.into(),
block_random: Default::default(),
};

Expand Down
3 changes: 3 additions & 0 deletions evm/tests/log_opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ fn test_log_opcodes() -> anyhow::Result<()> {
block_chain_id: 1.into(),
block_base_fee: 0xa.into(),
block_gas_used: 0.into(),
block_blob_base_fee: 0x2.into(),
block_bloom: [0.into(); 8],
};

Expand Down Expand Up @@ -359,6 +360,7 @@ fn test_log_with_aggreg() -> anyhow::Result<()> {
block_chain_id: 1.into(),
block_base_fee: 0xa.into(),
block_gas_used: (22570 + 21000).into(),
block_blob_base_fee: 0x2.into(),
block_bloom: [
0.into(),
0.into(),
Expand Down Expand Up @@ -818,6 +820,7 @@ fn test_two_txn() -> anyhow::Result<()> {
block_chain_id: 1.into(),
block_base_fee: 0xa.into(),
block_gas_used: 0.into(),
block_blob_base_fee: 0x2.into(),
block_bloom: [0.into(); 8],
};

Expand Down
1 change: 1 addition & 0 deletions evm/tests/self_balance_gas_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ fn self_balance_gas_cost() -> anyhow::Result<()> {
block_gas_used: gas_used.into(),
block_bloom: [0.into(); 8],
block_base_fee: 0xa.into(),
block_blob_base_fee: 0x2.into(),
block_random: Default::default(),
};

Expand Down
1 change: 1 addition & 0 deletions evm/tests/simple_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn test_simple_transfer() -> anyhow::Result<()> {
block_chain_id: 1.into(),
block_base_fee: 0xa.into(),
block_gas_used: 21032.into(),
block_blob_base_fee: 0x2.into(),
block_bloom: [0.into(); 8],
};

Expand Down
Loading