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

Apply curie upgrade #13

Merged
merged 7 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
85 changes: 41 additions & 44 deletions bus-mapping/src/circuit_input_builder/curie.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// Adapted from https://github.com/scroll-tech/go-ethereum/blob/8dc419a70b94f5ca185dcf818a48a3bd2eefc392/consensus/misc/curie.go

use eth_types::{
utils::{hash_code, hash_code_keccak},
ToWord, Word,
};
use eth_types::Word;

use crate::{
l2_predeployed::l1_gas_price_oracle,
operation::{AccountField, AccountOp, StorageOp, RW},
operation::{StorageOp, RW},
Error,
};

Expand Down Expand Up @@ -38,48 +35,48 @@ pub fn is_curie_fork_block(chain_id: u64, blk: u64) -> bool {
}

/// Insert needed rws for the contract upgrade
/// Num of rws: 7
/// Num of rws: 3
pub fn apply_curie(state: &mut CircuitInputStateRef, step: &mut ExecStep) -> Result<(), Error> {
// The chunk should not includes other txs.
let v1_codesize = l1_gas_price_oracle::V1_BYTECODE.len();
let v1_codehash = hash_code(&l1_gas_price_oracle::V1_BYTECODE);
let v1_keccak_codehash = hash_code_keccak(&l1_gas_price_oracle::V1_BYTECODE);
log::debug!("l1_oracle poseidon codehash {:?}", v1_codehash);
log::debug!("l1_oracle keccak codehash {:?}", v1_keccak_codehash);
let v2_codesize = l1_gas_price_oracle::V2_BYTECODE.len();
let v2_codehash = hash_code(&l1_gas_price_oracle::V2_BYTECODE);
let v2_keccak_codehash = hash_code_keccak(&l1_gas_price_oracle::V2_BYTECODE);
// let v1_codesize = l1_gas_price_oracle::V1_BYTECODE.len();
// let v1_codehash = hash_code(&l1_gas_price_oracle::V1_BYTECODE);
// let v1_keccak_codehash = hash_code_keccak(&l1_gas_price_oracle::V1_BYTECODE);
// log::debug!("l1_oracle poseidon codehash {:?}", v1_codehash);
// log::debug!("l1_oracle keccak codehash {:?}", v1_keccak_codehash);
// let v2_codesize = l1_gas_price_oracle::V2_BYTECODE.len();
// let v2_codehash = hash_code(&l1_gas_price_oracle::V2_BYTECODE);
// let v2_keccak_codehash = hash_code_keccak(&l1_gas_price_oracle::V2_BYTECODE);

state.push_op(
step,
RW::WRITE,
AccountOp {
address: *l1_gas_price_oracle::ADDRESS,
field: AccountField::CodeHash,
value_prev: v1_codehash.to_word(),
value: v2_codehash.to_word(),
},
)?;
state.push_op(
step,
RW::WRITE,
AccountOp {
address: *l1_gas_price_oracle::ADDRESS,
field: AccountField::KeccakCodeHash,
value_prev: v1_keccak_codehash.to_word(),
value: v2_keccak_codehash.to_word(),
},
)?;
state.push_op(
step,
RW::WRITE,
AccountOp {
address: *l1_gas_price_oracle::ADDRESS,
field: AccountField::CodeSize,
value_prev: v1_codesize.to_word(),
value: v2_codesize.to_word(),
},
)?;
// state.push_op(
// step,
// RW::WRITE,
// AccountOp {
// address: *l1_gas_price_oracle::ADDRESS,
// field: AccountField::CodeHash,
// value_prev: v1_codehash.to_word(),
// value: v2_codehash.to_word(),
// },
// )?;
// state.push_op(
// step,
// RW::WRITE,
// AccountOp {
// address: *l1_gas_price_oracle::ADDRESS,
// field: AccountField::KeccakCodeHash,
// value_prev: v1_keccak_codehash.to_word(),
// value: v2_keccak_codehash.to_word(),
// },
// )?;
// state.push_op(
// step,
// RW::WRITE,
// AccountOp {
// address: *l1_gas_price_oracle::ADDRESS,
// field: AccountField::CodeSize,
// value_prev: v1_codesize.to_word(),
// value: v2_codesize.to_word(),
// },
// )?;

for (slot, value) in [
(*l1_gas_price_oracle::IS_CURIE_SLOT, Word::from(1)),
Expand Down
31 changes: 19 additions & 12 deletions eth-types/src/forks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Hardfork related codes for Scroll networks

use crate::constants::read_env_var;

/// Hardfork ID for scroll networks
Expand All @@ -9,23 +8,31 @@ pub enum HardforkId {
Curie = 3,
}

/// Scroll devnet chain id
pub const SCROLL_DEVNET_CHAIN_ID: u64 = 222222;
/// Scroll testnet chain id
pub const SCROLL_TESTNET_CHAIN_ID: u64 = 534351;
/// Scroll mainnet chain id
pub const SCROLL_MAINNET_CHAIN_ID: u64 = 534352;
/// Morph devnet and qanet chain id
pub const MORPH_DEVNET_CHAIN_ID: u64 = 53077;
/// Morph testnet chain id
pub const MORPH_TESTNET_CHAIN_ID: u64 = 2710;
/// Morph mainnet chain id
pub const MORPH_MAINNET_CHAIN_ID: u64 = 2818;

/// Get hardforks of Scroll networks.
/// Get hardforks of Morph networks.
/// Returns a list of triplets of (hardfork id, chain id, block number)
pub fn hardfork_heights() -> Vec<(HardforkId, u64, u64)> {
vec![
(HardforkId::Curie, SCROLL_DEVNET_CHAIN_ID, 5), // devnet
(HardforkId::Curie, SCROLL_TESTNET_CHAIN_ID, 4740239), // testnet
(
HardforkId::Curie,
SCROLL_MAINNET_CHAIN_ID,
read_env_var("SCROLL_MAINNET_CURIE_BLOCK", 6924036),
MORPH_DEVNET_CHAIN_ID,
read_env_var("MORPH_DEVNET_CURIE_BLOCK", u64::MAX),
), // devnet and qanet
(
HardforkId::Curie,
MORPH_TESTNET_CHAIN_ID,
read_env_var("MORPH_TESTNET_CURIE_BLOCK", u64::MAX),
), // testnet
(
HardforkId::Curie,
MORPH_MAINNET_CHAIN_ID,
read_env_var("MORPH_MAINNET_CURIE_BLOCK", u64::MAX),
), // mainnet
]
}
8 changes: 4 additions & 4 deletions eth-types/src/l2_predeployed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ pub mod l1_gas_price_oracle {

/// THe following 3 slots plus `BASE_FEE_SLOT` will be used for l1 fee after curie fork
/// L1 BlobBaseFee slot in L1GasPriceOracle after Curie fork
pub static L1_BLOB_BASEFEE_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(5));
pub static L1_BLOB_BASEFEE_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(6));
/// L1 commitScalar slot in L1GasPriceOracle after Curie fork
pub static COMMIT_SCALAR_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(6));
pub static COMMIT_SCALAR_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(7));
/// L1 blob_scalar slot in L1GasPriceOracle after Curie fork
pub static BLOB_SCALAR_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(7));
pub static IS_CURIE_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(8));
pub static BLOB_SCALAR_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(8));
pub static IS_CURIE_SLOT: LazyLock<U256> = LazyLock::new(|| U256::from(9));
pub static INITIAL_COMMIT_SCALAR: LazyLock<U256> =
LazyLock::new(|| U256::from(230759955285u64));
pub static INITIAL_BLOB_SCALAR: LazyLock<U256> = LazyLock::new(|| U256::from(417565260));
Expand Down
30 changes: 15 additions & 15 deletions zkevm-circuits/src/evm_circuit/util/common_gadget/curie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
};

use eth_types::forks::{
HardforkId, SCROLL_DEVNET_CHAIN_ID, SCROLL_MAINNET_CHAIN_ID, SCROLL_TESTNET_CHAIN_ID,
HardforkId, MORPH_DEVNET_CHAIN_ID, MORPH_MAINNET_CHAIN_ID, MORPH_TESTNET_CHAIN_ID,
};
use gadgets::util::not;
use halo2_proofs::{
Expand All @@ -23,8 +23,8 @@ use halo2_proofs::{
#[derive(Clone, Debug)]
pub(crate) struct CurieGadget<F> {
chain_id: Cell<F>,
/// Scroll chains have non-zero curie hard fork block number
is_scroll_chain: IsZeroGadget<F>,
/// Morph chains have non-zero curie hard fork block number
is_morph_chain: IsZeroGadget<F>,
/// The block height at which curie hard fork happens
curie_fork_block_num: Cell<F>,
pub(crate) is_before_curie: LtGadget<F, 8>, // block num is u64
Expand All @@ -41,18 +41,18 @@ impl<F: Field> CurieGadget<F> {
);

// TODO: refactor
// is_scoll_chain means (chain_id - 534352) * (chain_id - 222222) == 0
let is_scroll_chain = IsZeroGadget::construct(
// is_morph_chain means (chain_id - 53077) * (chain_id - 2710) == 0
chengwenxi marked this conversation as resolved.
Show resolved Hide resolved
let is_morph_chain = IsZeroGadget::construct(
chengwenxi marked this conversation as resolved.
Show resolved Hide resolved
cb,
(chain_id.expr() - SCROLL_MAINNET_CHAIN_ID.expr())
* (chain_id.expr() - SCROLL_DEVNET_CHAIN_ID.expr()),
(chain_id.expr() - MORPH_MAINNET_CHAIN_ID.expr())
* (chain_id.expr() - MORPH_DEVNET_CHAIN_ID.expr()),
);

// For Scroll Networks (mainnet, testnet, devnet),
// For Morph Networks (mainnet, testnet, devnet),
// curie_fork_block_num should be pre-defined.
// For other chain ids, it should be 0.
let curie_fork_block_num = cb.query_cell();
cb.condition(is_scroll_chain.expr(), |cb| {
cb.condition(is_morph_chain.expr(), |cb| {
cb.add_lookup(
"Hardfork lookup",
Lookup::Fixed {
Expand All @@ -65,7 +65,7 @@ impl<F: Field> CurieGadget<F> {
},
);
});
cb.condition(not::expr(is_scroll_chain.expr()), |cb| {
cb.condition(not::expr(is_morph_chain.expr()), |cb| {
cb.require_zero("enable curie since genesis", curie_fork_block_num.expr());
});

Expand All @@ -76,7 +76,7 @@ impl<F: Field> CurieGadget<F> {
);
Self {
chain_id,
is_scroll_chain,
is_morph_chain,
curie_fork_block_num,
is_before_curie,
}
Expand All @@ -91,13 +91,13 @@ impl<F: Field> CurieGadget<F> {
) -> Result<(), Error> {
self.chain_id
.assign(region, offset, Value::known(F::from(chain_id)))?;
self.is_scroll_chain.assign(
self.is_morph_chain.assign(
region,
offset,
(F::from(chain_id) - F::from(SCROLL_MAINNET_CHAIN_ID))
* (F::from(chain_id) - F::from(SCROLL_DEVNET_CHAIN_ID)),
(F::from(chain_id) - F::from(MORPH_MAINNET_CHAIN_ID))
* (F::from(chain_id) - F::from(MORPH_DEVNET_CHAIN_ID)),
)?;
let curie_fork_block_num = if chain_id == SCROLL_TESTNET_CHAIN_ID {
let curie_fork_block_num = if chain_id == MORPH_TESTNET_CHAIN_ID {
0
} else {
bus_mapping::circuit_input_builder::curie::get_curie_fork_block(chain_id)
Expand Down
Loading