From c34d871b2799b81aebe052a6a4272d5746135a53 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Thu, 31 Aug 2023 15:54:16 +0200 Subject: [PATCH 01/10] Add random to block metadata and fix `sys_prevrandao` --- evm/src/cpu/kernel/asm/core/syscall_stubs.asm | 6 +-- .../cpu/kernel/constants/global_metadata.rs | 47 ++++++++++--------- evm/src/generation/mod.rs | 1 + evm/src/proof.rs | 23 +++++++-- evm/src/recursive_verifier.rs | 13 ++++- evm/src/verifier.rs | 4 ++ evm/tests/add11_yml.rs | 1 + evm/tests/log_opcode.rs | 2 + evm/tests/simple_transfer.rs | 1 + 9 files changed, 65 insertions(+), 33 deletions(-) diff --git a/evm/src/cpu/kernel/asm/core/syscall_stubs.asm b/evm/src/cpu/kernel/asm/core/syscall_stubs.asm index 88124256f4..f84c4ae74d 100644 --- a/evm/src/cpu/kernel/asm/core/syscall_stubs.asm +++ b/evm/src/cpu/kernel/asm/core/syscall_stubs.asm @@ -10,11 +10,9 @@ global sys_blockhash: EXIT_KERNEL // This is a temporary version that returns the block difficulty (i.e. the old version of this opcode). -// TODO: Fix this. -// TODO: What semantics will this have for Edge? global sys_prevrandao: // stack: kexit_info %charge_gas_const(@GAS_BASE) - %mload_global_metadata(@GLOBAL_METADATA_BLOCK_DIFFICULTY) - %stack (difficulty, kexit_info) -> (kexit_info, difficulty) + %mload_global_metadata(@GLOBAL_METADATA_BLOCK_RANDOM) + %stack (random, kexit_info) -> (kexit_info, random) EXIT_KERNEL diff --git a/evm/src/cpu/kernel/constants/global_metadata.rs b/evm/src/cpu/kernel/constants/global_metadata.rs index f33fe7a17c..724c5bc8e4 100644 --- a/evm/src/cpu/kernel/constants/global_metadata.rs +++ b/evm/src/cpu/kernel/constants/global_metadata.rs @@ -39,46 +39,47 @@ pub(crate) enum GlobalMetadata { BlockTimestamp = 15, BlockNumber = 16, BlockDifficulty = 17, - BlockGasLimit = 18, - BlockChainId = 19, - BlockBaseFee = 20, + BlockRandom = 18, + BlockGasLimit = 19, + BlockChainId = 20, + BlockBaseFee = 21, /// Gas to refund at the end of the transaction. - RefundCounter = 21, + RefundCounter = 22, /// Length of the addresses access list. - AccessedAddressesLen = 22, + AccessedAddressesLen = 23, /// Length of the storage keys access list. - AccessedStorageKeysLen = 23, + AccessedStorageKeysLen = 24, /// Length of the self-destruct list. - SelfDestructListLen = 24, + SelfDestructListLen = 25, /// Length of the bloom entry buffer. - BloomEntryLen = 25, + BloomEntryLen = 26, /// Length of the journal. - JournalLen = 26, + JournalLen = 27, /// Length of the `JournalData` segment. - JournalDataLen = 27, + JournalDataLen = 28, /// Current checkpoint. - CurrentCheckpoint = 28, - TouchedAddressesLen = 29, + CurrentCheckpoint = 29, + TouchedAddressesLen = 30, // Gas cost for the access list in type-1 txns. See EIP-2930. - AccessListDataCost = 30, + AccessListDataCost = 31, // Start of the access list in the RLP for type-1 txns. - AccessListRlpStart = 31, + AccessListRlpStart = 32, // Length of the access list in the RLP for type-1 txns. - AccessListRlpLen = 32, + AccessListRlpLen = 33, // Boolean flag indicating if the txn is a contract creation txn. - ContractCreation = 33, - IsPrecompileFromEoa = 34, - CallStackDepth = 35, + ContractCreation = 34, + IsPrecompileFromEoa = 35, + CallStackDepth = 36, /// Transaction logs list length - LogsLen = 36, - LogsDataLen = 37, - LogsPayloadLen = 38, + LogsLen = 37, + LogsDataLen = 38, + LogsPayloadLen = 39, } impl GlobalMetadata { - pub(crate) const COUNT: usize = 39; + pub(crate) const COUNT: usize = 40; pub(crate) fn all() -> [Self; Self::COUNT] { [ @@ -100,6 +101,7 @@ impl GlobalMetadata { Self::BlockTimestamp, Self::BlockNumber, Self::BlockDifficulty, + Self::BlockRandom, Self::BlockGasLimit, Self::BlockChainId, Self::BlockBaseFee, @@ -145,6 +147,7 @@ impl GlobalMetadata { Self::BlockTimestamp => "GLOBAL_METADATA_BLOCK_TIMESTAMP", Self::BlockNumber => "GLOBAL_METADATA_BLOCK_NUMBER", Self::BlockDifficulty => "GLOBAL_METADATA_BLOCK_DIFFICULTY", + Self::BlockRandom => "GLOBAL_METADATA_BLOCK_RANDOM", Self::BlockGasLimit => "GLOBAL_METADATA_BLOCK_GAS_LIMIT", Self::BlockChainId => "GLOBAL_METADATA_BLOCK_CHAIN_ID", Self::BlockBaseFee => "GLOBAL_METADATA_BLOCK_BASE_FEE", diff --git a/evm/src/generation/mod.rs b/evm/src/generation/mod.rs index 5beded4e9b..2344df728b 100644 --- a/evm/src/generation/mod.rs +++ b/evm/src/generation/mod.rs @@ -91,6 +91,7 @@ fn apply_metadata_and_tries_memops, const D: usize> (GlobalMetadata::BlockTimestamp, metadata.block_timestamp), (GlobalMetadata::BlockNumber, metadata.block_number), (GlobalMetadata::BlockDifficulty, metadata.block_difficulty), + (GlobalMetadata::BlockRandom, metadata.block_random), (GlobalMetadata::BlockGasLimit, metadata.block_gaslimit), (GlobalMetadata::BlockChainId, metadata.block_chain_id), (GlobalMetadata::BlockBaseFee, metadata.block_base_fee), diff --git a/evm/src/proof.rs b/evm/src/proof.rs index 2cda5489d7..30c7fb6425 100644 --- a/evm/src/proof.rs +++ b/evm/src/proof.rs @@ -68,6 +68,7 @@ pub struct BlockMetadata { pub block_timestamp: U256, pub block_number: U256, pub block_difficulty: U256, + pub block_random: U256, pub block_gaslimit: U256, pub block_chain_id: U256, pub block_base_fee: U256, @@ -110,6 +111,7 @@ impl PublicValuesTarget { block_timestamp, block_number, block_difficulty, + block_random, block_gaslimit, block_chain_id, block_base_fee, @@ -120,6 +122,7 @@ impl PublicValuesTarget { buffer.write_target(block_timestamp)?; buffer.write_target(block_number)?; buffer.write_target(block_difficulty)?; + buffer.write_target_array(&block_random)?; buffer.write_target(block_gaslimit)?; buffer.write_target(block_chain_id)?; buffer.write_target_array(&block_base_fee)?; @@ -146,6 +149,7 @@ impl PublicValuesTarget { block_timestamp: buffer.read_target()?, block_number: buffer.read_target()?, block_difficulty: buffer.read_target()?, + block_random: buffer.read_target_array()?, block_gaslimit: buffer.read_target()?, block_chain_id: buffer.read_target()?, block_base_fee: buffer.read_target_array()?, @@ -266,6 +270,7 @@ pub struct BlockMetadataTarget { pub block_timestamp: Target, pub block_number: Target, pub block_difficulty: Target, + pub block_random: [Target; 8], pub block_gaslimit: Target, pub block_chain_id: Target, pub block_base_fee: [Target; 2], @@ -273,23 +278,25 @@ pub struct BlockMetadataTarget { } impl BlockMetadataTarget { - const SIZE: usize = 76; + const SIZE: usize = 84; pub fn from_public_inputs(pis: &[Target]) -> Self { let block_beneficiary = pis[0..5].try_into().unwrap(); let block_timestamp = pis[5]; let block_number = pis[6]; let block_difficulty = pis[7]; - let block_gaslimit = pis[8]; - let block_chain_id = pis[9]; - let block_base_fee = pis[10..12].try_into().unwrap(); - let block_bloom = pis[12..76].try_into().unwrap(); + let block_random = pis[8..16].try_into().unwrap(); + let block_gaslimit = pis[16]; + let block_chain_id = pis[17]; + let block_base_fee = pis[18..20].try_into().unwrap(); + let block_bloom = pis[20..84].try_into().unwrap(); Self { block_beneficiary, block_timestamp, block_number, block_difficulty, + block_random, block_gaslimit, block_chain_id, block_base_fee, @@ -314,6 +321,9 @@ impl BlockMetadataTarget { block_timestamp: builder.select(condition, bm0.block_timestamp, bm1.block_timestamp), block_number: builder.select(condition, bm0.block_number, bm1.block_number), block_difficulty: builder.select(condition, bm0.block_difficulty, bm1.block_difficulty), + block_random: core::array::from_fn(|i| { + builder.select(condition, bm0.block_random[i], bm1.block_random[i]) + }), block_gaslimit: builder.select(condition, bm0.block_gaslimit, bm1.block_gaslimit), block_chain_id: builder.select(condition, bm0.block_chain_id, bm1.block_chain_id), block_base_fee: core::array::from_fn(|i| { @@ -336,6 +346,9 @@ impl BlockMetadataTarget { builder.connect(bm0.block_timestamp, bm1.block_timestamp); builder.connect(bm0.block_number, bm1.block_number); builder.connect(bm0.block_difficulty, bm1.block_difficulty); + for i in 0..8 { + builder.connect(bm0.block_random[i], bm1.block_random[i]); + } builder.connect(bm0.block_gaslimit, bm1.block_gaslimit); builder.connect(bm0.block_chain_id, bm1.block_chain_id); for i in 0..2 { diff --git a/evm/src/recursive_verifier.rs b/evm/src/recursive_verifier.rs index 296cbfb2fb..5cf5ab42d7 100644 --- a/evm/src/recursive_verifier.rs +++ b/evm/src/recursive_verifier.rs @@ -509,7 +509,7 @@ pub(crate) fn get_memory_extra_looking_products_circuit< let mut product = builder.one(); // Add metadata writes. - let block_fields_without_beneficiary_and_basefee = [ + let block_fields_without_arrays = [ ( GlobalMetadata::BlockTimestamp as usize, public_values.block_metadata.block_timestamp, @@ -540,7 +540,7 @@ pub(crate) fn get_memory_extra_looking_products_circuit< &public_values.block_metadata.block_beneficiary, ); - block_fields_without_beneficiary_and_basefee.map(|(field, target)| { + block_fields_without_arrays.map(|(field, target)| { // Each of those fields fit in 32 bits, hence in a single Target. product = add_metadata_write(builder, challenge, product, field, &[target]); }); @@ -552,6 +552,13 @@ pub(crate) fn get_memory_extra_looking_products_circuit< GlobalMetadata::BlockBaseFee as usize, &public_values.block_metadata.block_base_fee, ); + product = add_metadata_write( + builder, + challenge, + product, + GlobalMetadata::BlockRandom as usize, + &public_values.block_metadata.block_random, + ); // Add trie roots writes. let trie_fields = [ @@ -680,6 +687,7 @@ pub(crate) fn add_virtual_block_metadata, const D: let block_timestamp = builder.add_virtual_public_input(); let block_number = builder.add_virtual_public_input(); let block_difficulty = builder.add_virtual_public_input(); + let block_random = builder.add_virtual_public_input_arr(); let block_gaslimit = builder.add_virtual_public_input(); let block_chain_id = builder.add_virtual_public_input(); let block_base_fee = builder.add_virtual_public_input_arr(); @@ -689,6 +697,7 @@ pub(crate) fn add_virtual_block_metadata, const D: block_timestamp, block_number, block_difficulty, + block_random, block_gaslimit, block_chain_id, block_base_fee, diff --git a/evm/src/verifier.rs b/evm/src/verifier.rs index 715b1738ae..042d796041 100644 --- a/evm/src/verifier.rs +++ b/evm/src/verifier.rs @@ -160,6 +160,10 @@ where GlobalMetadata::BlockNumber, public_values.block_metadata.block_number, ), + ( + GlobalMetadata::BlockRandom, + public_values.block_metadata.block_random, + ), ( GlobalMetadata::BlockDifficulty, public_values.block_metadata.block_difficulty, diff --git a/evm/tests/add11_yml.rs b/evm/tests/add11_yml.rs index e0b6f10446..07d9b8b101 100644 --- a/evm/tests/add11_yml.rs +++ b/evm/tests/add11_yml.rs @@ -83,6 +83,7 @@ fn add11_yml() -> anyhow::Result<()> { block_timestamp: 0x03e8.into(), block_number: 1.into(), block_difficulty: 0x020000.into(), + block_random: 0x020000.into(), block_gaslimit: 0xff112233u32.into(), block_chain_id: 1.into(), block_base_fee: 0xa.into(), diff --git a/evm/tests/log_opcode.rs b/evm/tests/log_opcode.rs index 8219e4edbf..053673482f 100644 --- a/evm/tests/log_opcode.rs +++ b/evm/tests/log_opcode.rs @@ -134,6 +134,7 @@ fn test_log_opcodes() -> anyhow::Result<()> { block_timestamp: 0x03e8.into(), block_number: 1.into(), block_difficulty: 0x020000.into(), + block_random: 0x020000.into(), block_gaslimit: 0xffffffffu32.into(), block_chain_id: 1.into(), block_base_fee: 0xa.into(), @@ -408,6 +409,7 @@ fn test_two_txn() -> anyhow::Result<()> { block_timestamp: 0x03e8.into(), block_number: 1.into(), block_difficulty: 0x020000.into(), + block_random: 0x020000.into(), block_gaslimit: 0xffffffffu32.into(), block_chain_id: 1.into(), block_base_fee: 0xa.into(), diff --git a/evm/tests/simple_transfer.rs b/evm/tests/simple_transfer.rs index 5bf95f58dc..1a5a2bad9a 100644 --- a/evm/tests/simple_transfer.rs +++ b/evm/tests/simple_transfer.rs @@ -71,6 +71,7 @@ fn test_simple_transfer() -> anyhow::Result<()> { block_timestamp: 0x03e8.into(), block_number: 1.into(), block_difficulty: 0x020000.into(), + block_random: 0x020000.into(), block_gaslimit: 0xff112233u32.into(), block_chain_id: 1.into(), block_base_fee: 0xa.into(), From 0027f069d4bb2827e76798e07883f5e9ff8ac921 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Thu, 31 Aug 2023 15:57:40 +0200 Subject: [PATCH 02/10] Minor --- evm/src/recursive_verifier.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/evm/src/recursive_verifier.rs b/evm/src/recursive_verifier.rs index 5cf5ab42d7..22763677d9 100644 --- a/evm/src/recursive_verifier.rs +++ b/evm/src/recursive_verifier.rs @@ -509,7 +509,7 @@ pub(crate) fn get_memory_extra_looking_products_circuit< let mut product = builder.one(); // Add metadata writes. - let block_fields_without_arrays = [ + let block_fields_scalars = [ ( GlobalMetadata::BlockTimestamp as usize, public_values.block_metadata.block_timestamp, @@ -540,7 +540,7 @@ pub(crate) fn get_memory_extra_looking_products_circuit< &public_values.block_metadata.block_beneficiary, ); - block_fields_without_arrays.map(|(field, target)| { + block_fields_scalars.map(|(field, target)| { // Each of those fields fit in 32 bits, hence in a single Target. product = add_metadata_write(builder, challenge, product, field, &[target]); }); @@ -549,15 +549,16 @@ pub(crate) fn get_memory_extra_looking_products_circuit< builder, challenge, product, - GlobalMetadata::BlockBaseFee as usize, - &public_values.block_metadata.block_base_fee, + GlobalMetadata::BlockRandom as usize, + &public_values.block_metadata.block_random, ); + product = add_metadata_write( builder, challenge, product, - GlobalMetadata::BlockRandom as usize, - &public_values.block_metadata.block_random, + GlobalMetadata::BlockBaseFee as usize, + &public_values.block_metadata.block_base_fee, ); // Add trie roots writes. From 3c2adf5847ee4b86c641e1d82d6c38e67e254863 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Fri, 1 Sep 2023 10:19:26 +0200 Subject: [PATCH 03/10] Observe block_random --- evm/src/get_challenges.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/evm/src/get_challenges.rs b/evm/src/get_challenges.rs index 9b03b80dd1..f5af8b7691 100644 --- a/evm/src/get_challenges.rs +++ b/evm/src/get_challenges.rs @@ -67,6 +67,7 @@ fn observe_block_metadata< challenger.observe_element(F::from_canonical_u32( block_metadata.block_difficulty.as_u32(), )); + challenger.observe_elements(&u256_limbs::(block_metadata.block_random)); challenger.observe_element(F::from_canonical_u32( block_metadata.block_gaslimit.as_u32(), )); @@ -95,6 +96,7 @@ fn observe_block_metadata_target< challenger.observe_element(block_metadata.block_timestamp); challenger.observe_element(block_metadata.block_number); challenger.observe_element(block_metadata.block_difficulty); + challenger.observe_elements(&block_metadata.block_random); challenger.observe_element(block_metadata.block_gaslimit); challenger.observe_element(block_metadata.block_chain_id); challenger.observe_elements(&block_metadata.block_base_fee); From 5048631c26cd1153a8ad69786ca04688f5a373c2 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Mon, 25 Sep 2023 15:33:19 +0200 Subject: [PATCH 04/10] Write block_random --- evm/src/recursive_verifier.rs | 8 ++++++-- evm/tests/basic_smart_contract.rs | 1 + evm/tests/log_opcode.rs | 1 + evm/tests/self_balance_gas_cost.rs | 1 + 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/evm/src/recursive_verifier.rs b/evm/src/recursive_verifier.rs index 650461e91f..b39a81c366 100644 --- a/evm/src/recursive_verifier.rs +++ b/evm/src/recursive_verifier.rs @@ -548,11 +548,15 @@ pub(crate) fn get_memory_extra_looking_products_circuit< ), ]; - let beneficiary_base_fee_cur_hash_fields: [(usize, &[Target]); 3] = [ + let beneficiary_random_base_fee_cur_hash_fields: [(usize, &[Target]); 4] = [ ( GlobalMetadata::BlockBeneficiary as usize, &public_values.block_metadata.block_beneficiary, ), + ( + GlobalMetadata::BlockRandom as usize, + &public_values.block_metadata.block_random, + ), ( GlobalMetadata::BlockBaseFee as usize, &public_values.block_metadata.block_base_fee, @@ -576,7 +580,7 @@ pub(crate) fn get_memory_extra_looking_products_circuit< ); }); - beneficiary_base_fee_cur_hash_fields.map(|(field, targets)| { + beneficiary_random_base_fee_cur_hash_fields.map(|(field, targets)| { product = add_data_write( builder, challenge, diff --git a/evm/tests/basic_smart_contract.rs b/evm/tests/basic_smart_contract.rs index 2cd549ff9e..ed5399a434 100644 --- a/evm/tests/basic_smart_contract.rs +++ b/evm/tests/basic_smart_contract.rs @@ -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_random: Default::default() }; let mut contract_code = HashMap::new(); diff --git a/evm/tests/log_opcode.rs b/evm/tests/log_opcode.rs index 9b89e15f1f..1309319e6f 100644 --- a/evm/tests/log_opcode.rs +++ b/evm/tests/log_opcode.rs @@ -366,6 +366,7 @@ fn test_log_with_aggreg() -> anyhow::Result<()> { .unwrap(), U256::from_dec_str("2722259584404615024560450425766186844160").unwrap(), ], + block_random: Default::default() }; let beneficiary_account_after = AccountRlp { diff --git a/evm/tests/self_balance_gas_cost.rs b/evm/tests/self_balance_gas_cost.rs index 9ba1ac5497..b106bf575b 100644 --- a/evm/tests/self_balance_gas_cost.rs +++ b/evm/tests/self_balance_gas_cost.rs @@ -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_random: Default::default() }; let mut contract_code = HashMap::new(); From e57873f58c79f84698bbf430ef49dfd9a7912d60 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Mon, 25 Sep 2023 15:37:36 +0200 Subject: [PATCH 05/10] cargo fmt --- evm/tests/basic_smart_contract.rs | 2 +- evm/tests/log_opcode.rs | 2 +- evm/tests/self_balance_gas_cost.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/evm/tests/basic_smart_contract.rs b/evm/tests/basic_smart_contract.rs index ed5399a434..0130c6feb2 100644 --- a/evm/tests/basic_smart_contract.rs +++ b/evm/tests/basic_smart_contract.rs @@ -115,7 +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_random: Default::default() + block_random: Default::default(), }; let mut contract_code = HashMap::new(); diff --git a/evm/tests/log_opcode.rs b/evm/tests/log_opcode.rs index 1309319e6f..1892d91e4d 100644 --- a/evm/tests/log_opcode.rs +++ b/evm/tests/log_opcode.rs @@ -366,7 +366,7 @@ fn test_log_with_aggreg() -> anyhow::Result<()> { .unwrap(), U256::from_dec_str("2722259584404615024560450425766186844160").unwrap(), ], - block_random: Default::default() + block_random: Default::default(), }; let beneficiary_account_after = AccountRlp { diff --git a/evm/tests/self_balance_gas_cost.rs b/evm/tests/self_balance_gas_cost.rs index b106bf575b..de16db944a 100644 --- a/evm/tests/self_balance_gas_cost.rs +++ b/evm/tests/self_balance_gas_cost.rs @@ -104,7 +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_random: Default::default() + block_random: Default::default(), }; let mut contract_code = HashMap::new(); From bcda8817d94ce17e7397d595b50d8f55bdd232fc Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Mon, 25 Sep 2023 16:55:11 +0200 Subject: [PATCH 06/10] block_random: H256 --- evm/src/generation/mod.rs | 5 ++++- evm/src/get_challenges.rs | 2 +- evm/src/proof.rs | 2 +- evm/src/verifier.rs | 4 ++-- evm/tests/add11_yml.rs | 4 ++-- evm/tests/log_opcode.rs | 6 +++--- evm/tests/simple_transfer.rs | 4 ++-- 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/evm/src/generation/mod.rs b/evm/src/generation/mod.rs index 3ab38cc5cf..3f5bafba1e 100644 --- a/evm/src/generation/mod.rs +++ b/evm/src/generation/mod.rs @@ -100,7 +100,10 @@ fn apply_metadata_and_tries_memops, const D: usize> (GlobalMetadata::BlockTimestamp, metadata.block_timestamp), (GlobalMetadata::BlockNumber, metadata.block_number), (GlobalMetadata::BlockDifficulty, metadata.block_difficulty), - (GlobalMetadata::BlockRandom, metadata.block_random), + ( + GlobalMetadata::BlockRandom, + metadata.block_random.into_uint(), + ), (GlobalMetadata::BlockGasLimit, metadata.block_gaslimit), (GlobalMetadata::BlockChainId, metadata.block_chain_id), (GlobalMetadata::BlockBaseFee, metadata.block_base_fee), diff --git a/evm/src/get_challenges.rs b/evm/src/get_challenges.rs index 8ccd868886..1d2ae6029d 100644 --- a/evm/src/get_challenges.rs +++ b/evm/src/get_challenges.rs @@ -65,7 +65,7 @@ fn observe_block_metadata< challenger.observe_element(u256_to_u32(block_metadata.block_number)?); challenger.observe_element(u256_to_u32(block_metadata.block_difficulty)?); challenger.observe_element(u256_to_u32(block_metadata.block_gaslimit)?); - challenger.observe_elements(&u256_limbs::(block_metadata.block_random)); + challenger.observe_elements(&h256_limbs::(block_metadata.block_random)); challenger.observe_element(u256_to_u32(block_metadata.block_chain_id)?); let basefee = u256_to_u64(block_metadata.block_base_fee)?; challenger.observe_element(basefee.0); diff --git a/evm/src/proof.rs b/evm/src/proof.rs index 8ac30c3906..03e520ca6a 100644 --- a/evm/src/proof.rs +++ b/evm/src/proof.rs @@ -101,7 +101,7 @@ pub struct BlockMetadata { pub block_number: U256, /// The difficulty (before PoS transition) of this block. pub block_difficulty: U256, - pub block_random: U256, + pub block_random: H256, /// The gas limit of this block. It must fit in a `u32`. pub block_gaslimit: U256, /// The chain id of this block. diff --git a/evm/src/verifier.rs b/evm/src/verifier.rs index 936de4c75b..96ef286061 100644 --- a/evm/src/verifier.rs +++ b/evm/src/verifier.rs @@ -1,7 +1,7 @@ use std::any::type_name; use anyhow::{ensure, Result}; -use ethereum_types::U256; +use ethereum_types::{BigEndianHash, U256}; use itertools::Itertools; use plonky2::field::extension::{Extendable, FieldExtension}; use plonky2::field::types::Field; @@ -159,7 +159,7 @@ where ), ( GlobalMetadata::BlockRandom, - public_values.block_metadata.block_random, + public_values.block_metadata.block_random.into_uint(), ), ( GlobalMetadata::BlockDifficulty, diff --git a/evm/tests/add11_yml.rs b/evm/tests/add11_yml.rs index c7180785df..a456f02d0c 100644 --- a/evm/tests/add11_yml.rs +++ b/evm/tests/add11_yml.rs @@ -5,7 +5,7 @@ use std::time::Duration; use env_logger::{try_init_from_env, Env, DEFAULT_FILTER_ENV}; use eth_trie_utils::nibbles::Nibbles; use eth_trie_utils::partial_trie::{HashedPartialTrie, PartialTrie}; -use ethereum_types::{Address, H256}; +use ethereum_types::{Address, BigEndianHash, H256}; use hex_literal::hex; use keccak_hash::keccak; use plonky2::field::goldilocks_field::GoldilocksField; @@ -83,7 +83,7 @@ fn add11_yml() -> anyhow::Result<()> { block_timestamp: 0x03e8.into(), block_number: 1.into(), block_difficulty: 0x020000.into(), - block_random: 0x020000.into(), + block_random: H256::from_uint(&0x020000.into()), block_gaslimit: 0xff112233u32.into(), block_chain_id: 1.into(), block_base_fee: 0xa.into(), diff --git a/evm/tests/log_opcode.rs b/evm/tests/log_opcode.rs index 1892d91e4d..e9aa8c38c9 100644 --- a/evm/tests/log_opcode.rs +++ b/evm/tests/log_opcode.rs @@ -8,7 +8,7 @@ use bytes::Bytes; use env_logger::{try_init_from_env, Env, DEFAULT_FILTER_ENV}; use eth_trie_utils::nibbles::Nibbles; use eth_trie_utils::partial_trie::{HashedPartialTrie, PartialTrie}; -use ethereum_types::{Address, H256, U256}; +use ethereum_types::{Address, BigEndianHash, H256, U256}; use hex_literal::hex; use keccak_hash::keccak; use plonky2::field::goldilocks_field::GoldilocksField; @@ -135,7 +135,7 @@ fn test_log_opcodes() -> anyhow::Result<()> { block_timestamp: 0x03e8.into(), block_number: 1.into(), block_difficulty: 0x020000.into(), - block_random: 0x020000.into(), + block_random: H256::from_uint(&0x020000.into()), block_gaslimit: 0xffffffffu32.into(), block_chain_id: 1.into(), block_base_fee: 0xa.into(), @@ -793,7 +793,7 @@ fn test_two_txn() -> anyhow::Result<()> { block_timestamp: 0x03e8.into(), block_number: 1.into(), block_difficulty: 0x020000.into(), - block_random: 0x020000.into(), + block_random: H256::from_low_u64_le(0x020000), block_gaslimit: 0xffffffffu32.into(), block_chain_id: 1.into(), block_base_fee: 0xa.into(), diff --git a/evm/tests/simple_transfer.rs b/evm/tests/simple_transfer.rs index 598b958bb2..268ad6615d 100644 --- a/evm/tests/simple_transfer.rs +++ b/evm/tests/simple_transfer.rs @@ -5,7 +5,7 @@ use std::time::Duration; use env_logger::{try_init_from_env, Env, DEFAULT_FILTER_ENV}; use eth_trie_utils::nibbles::Nibbles; use eth_trie_utils::partial_trie::{HashedPartialTrie, PartialTrie}; -use ethereum_types::{Address, H256, U256}; +use ethereum_types::{Address, BigEndianHash, H256, U256}; use hex_literal::hex; use keccak_hash::keccak; use plonky2::field::goldilocks_field::GoldilocksField; @@ -71,7 +71,7 @@ fn test_simple_transfer() -> anyhow::Result<()> { block_timestamp: 0x03e8.into(), block_number: 1.into(), block_difficulty: 0x020000.into(), - block_random: 0x020000.into(), + block_random: H256::from_uint(&0x020000.into()), block_gaslimit: 0xff112233u32.into(), block_chain_id: 1.into(), block_base_fee: 0xa.into(), From 0fa07e2aa851956799a26bf38e5735b514aabea7 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Mon, 25 Sep 2023 16:56:51 +0200 Subject: [PATCH 07/10] Move sys_prevrandao to metadata.asm and delete syscall_stubs.asm --- evm/src/cpu/kernel/aggregator.rs | 1 - evm/src/cpu/kernel/asm/core/syscall_stubs.asm | 10 ---------- evm/src/cpu/kernel/asm/memory/metadata.asm | 7 +++++++ 3 files changed, 7 insertions(+), 11 deletions(-) delete mode 100644 evm/src/cpu/kernel/asm/core/syscall_stubs.asm diff --git a/evm/src/cpu/kernel/aggregator.rs b/evm/src/cpu/kernel/aggregator.rs index 160702df74..0c7c657999 100644 --- a/evm/src/cpu/kernel/aggregator.rs +++ b/evm/src/cpu/kernel/aggregator.rs @@ -36,7 +36,6 @@ pub(crate) fn combined_kernel() -> Kernel { include_str!("asm/core/nonce.asm"), include_str!("asm/core/process_txn.asm"), include_str!("asm/core/syscall.asm"), - include_str!("asm/core/syscall_stubs.asm"), include_str!("asm/core/terminate.asm"), include_str!("asm/core/transfer.asm"), include_str!("asm/core/util.asm"), diff --git a/evm/src/cpu/kernel/asm/core/syscall_stubs.asm b/evm/src/cpu/kernel/asm/core/syscall_stubs.asm deleted file mode 100644 index c13858cd58..0000000000 --- a/evm/src/cpu/kernel/asm/core/syscall_stubs.asm +++ /dev/null @@ -1,10 +0,0 @@ -// Labels for unimplemented syscalls to make the kernel assemble. -// Each label should be removed from this file once it is implemented. - -// This is a temporary version that returns the block difficulty (i.e. the old version of this opcode). -global sys_prevrandao: - // stack: kexit_info - %charge_gas_const(@GAS_BASE) - %mload_global_metadata(@GLOBAL_METADATA_BLOCK_RANDOM) - %stack (random, kexit_info) -> (kexit_info, random) - EXIT_KERNEL diff --git a/evm/src/cpu/kernel/asm/memory/metadata.asm b/evm/src/cpu/kernel/asm/memory/metadata.asm index 5b1417da79..c26d3d5fb9 100644 --- a/evm/src/cpu/kernel/asm/memory/metadata.asm +++ b/evm/src/cpu/kernel/asm/memory/metadata.asm @@ -383,3 +383,10 @@ zero_hash: %decrement %mstore_global_metadata(@GLOBAL_METADATA_CALL_STACK_DEPTH) %endmacro + +global sys_prevrandao: + // stack: kexit_info + %charge_gas_const(@GAS_BASE) + %mload_global_metadata(@GLOBAL_METADATA_BLOCK_RANDOM) + %stack (random, kexit_info) -> (kexit_info, random) + EXIT_KERNEL From 63ff9a8c8d4a87f858bce4208004a10c2330d8d7 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Mon, 25 Sep 2023 17:02:25 +0200 Subject: [PATCH 08/10] Set block_random in set_block_metadata_target --- evm/src/recursive_verifier.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/evm/src/recursive_verifier.rs b/evm/src/recursive_verifier.rs index b39a81c366..1457344c52 100644 --- a/evm/src/recursive_verifier.rs +++ b/evm/src/recursive_verifier.rs @@ -1020,6 +1020,10 @@ where block_metadata_target.block_difficulty, u256_to_u32(block_metadata.block_difficulty)?, ); + witness.set_target_arr( + &block_metadata_target.block_random, + &h256_limbs(block_metadata.block_random), + ); witness.set_target( block_metadata_target.block_gaslimit, u256_to_u32(block_metadata.block_gaslimit)?, From 075603934cd7e3b67ee38f24a77bb8a3efaac337 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Mon, 25 Sep 2023 17:55:07 +0200 Subject: [PATCH 09/10] Minor --- evm/tests/log_opcode.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evm/tests/log_opcode.rs b/evm/tests/log_opcode.rs index e9aa8c38c9..395846064e 100644 --- a/evm/tests/log_opcode.rs +++ b/evm/tests/log_opcode.rs @@ -793,7 +793,7 @@ fn test_two_txn() -> anyhow::Result<()> { block_timestamp: 0x03e8.into(), block_number: 1.into(), block_difficulty: 0x020000.into(), - block_random: H256::from_low_u64_le(0x020000), + block_random: H256::from_uint(0x020000.into()), block_gaslimit: 0xffffffffu32.into(), block_chain_id: 1.into(), block_base_fee: 0xa.into(), From 0611740e7b12eb2448c3610c5e6f60966f5543a4 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Mon, 25 Sep 2023 18:04:07 +0200 Subject: [PATCH 10/10] Minor --- evm/tests/log_opcode.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evm/tests/log_opcode.rs b/evm/tests/log_opcode.rs index 395846064e..67407807ae 100644 --- a/evm/tests/log_opcode.rs +++ b/evm/tests/log_opcode.rs @@ -793,7 +793,7 @@ fn test_two_txn() -> anyhow::Result<()> { block_timestamp: 0x03e8.into(), block_number: 1.into(), block_difficulty: 0x020000.into(), - block_random: H256::from_uint(0x020000.into()), + block_random: H256::from_uint(&0x020000.into()), block_gaslimit: 0xffffffffu32.into(), block_chain_id: 1.into(), block_base_fee: 0xa.into(),