From 5645e4618941ecb8d7dad1c7fb5b59a1a1c7756f Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Mon, 27 May 2024 15:18:35 +0800 Subject: [PATCH 01/33] add new fields --- .../src/circuit_input_builder/transaction.rs | 22 +++++++++++++++++++ bus-mapping/src/l2_predeployed.rs | 9 ++++++++ zkevm-circuits/Cargo.toml | 1 + 3 files changed, 32 insertions(+) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index 0eecacf4b5..c059cfca13 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -476,6 +476,12 @@ pub struct TxL1Fee { pub fee_overhead: u64, /// L1 fee scalar pub fee_scalar: u64, + #[cfg(feature = "l1_fee_curie")] + pub l1BlobBaseFee: u64, + #[cfg(feature = "l1_fee_curie")] + pub commitScalar: u64, + #[cfg(feature = "l1_fee_curie")] + pub blobScalar: u64, } impl TxL1Fee { @@ -502,11 +508,27 @@ impl TxL1Fee { .1 .as_u64() }); + if cfg!(feature = "l1_fee_curie") { + let [base_fee, l1BlobBaseFee, commitScalar, blobScalar] = [ + &l1_gas_price_oracle::BASE_FEE_SLOT, + &l1_gas_price_oracle::l1BlobBaseFee, + &l1_gas_price_oracle::commitScalar, + &l1_gas_price_oracle::blobScalar, + ] + .map(|slot| { + sdb.get_storage(&l1_gas_price_oracle::ADDRESS, slot) + .1 + .as_u64() + }); + } Self { base_fee, fee_overhead, fee_scalar, + l1BlobBaseFee, + commitScalar, + blobScalar, } } diff --git a/bus-mapping/src/l2_predeployed.rs b/bus-mapping/src/l2_predeployed.rs index e7ff7e3535..32f6a1c885 100644 --- a/bus-mapping/src/l2_predeployed.rs +++ b/bus-mapping/src/l2_predeployed.rs @@ -29,4 +29,13 @@ pub mod l1_gas_price_oracle { pub static OVERHEAD_SLOT: LazyLock = LazyLock::new(|| U256::from(2)); /// L1 scalar slot in L1GasPriceOracle pub static SCALAR_SLOT: LazyLock = LazyLock::new(|| U256::from(3)); + /// L1 BlobBaseFee slot in L1GasPriceOracle after Curie fork + #[cfg(feature = "l1_fee_curie")] + pub static l1BlobBaseFee: LazyLock = LazyLock::new(|| U256::from(5)); + #[cfg(feature = "l1_fee_curie")] + /// L1 commitScalar slot in L1GasPriceOracle after Curie fork + pub static commitScalar: LazyLock = LazyLock::new(|| U256::from(6)); + #[cfg(feature = "l1_fee_curie")] + /// L1 blobScalar slot in L1GasPriceOracle after Curie fork + pub static blobScalar: LazyLock = LazyLock::new(|| U256::from(7)); } diff --git a/zkevm-circuits/Cargo.toml b/zkevm-circuits/Cargo.toml index 247af242aa..93b94c2aa8 100644 --- a/zkevm-circuits/Cargo.toml +++ b/zkevm-circuits/Cargo.toml @@ -71,3 +71,4 @@ debug-annotations = [] enable-stack = ["bus-mapping/enable-stack"] enable-memory = ["bus-mapping/enable-memory"] enable-storage = ["bus-mapping/enable-storage"] +l1_fee_curie = [] From aa0efcbc42596a22bedae60f23afa7be12b69386 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Mon, 27 May 2024 17:16:23 +0800 Subject: [PATCH 02/33] update get_committed_values_from_state_db --- .../src/circuit_input_builder/transaction.rs | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index c059cfca13..e13c495aa9 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -508,8 +508,9 @@ impl TxL1Fee { .1 .as_u64() }); - if cfg!(feature = "l1_fee_curie") { - let [base_fee, l1BlobBaseFee, commitScalar, blobScalar] = [ + //if cfg!(feature = "l1_fee_curie") { + #[cfg(feature = "l1_fee_curie")] + { let [base_fee, l1BlobBaseFee, commitScalar, blobScalar] = [ &l1_gas_price_oracle::BASE_FEE_SLOT, &l1_gas_price_oracle::l1BlobBaseFee, &l1_gas_price_oracle::commitScalar, @@ -526,8 +527,11 @@ impl TxL1Fee { base_fee, fee_overhead, fee_scalar, + #[cfg(feature = "l1_fee_curie")] l1BlobBaseFee, + #[cfg(feature = "l1_fee_curie")] commitScalar, + #[cfg(feature = "l1_fee_curie")] blobScalar, } } @@ -544,10 +548,32 @@ impl TxL1Fee { .as_u64() }); + #[cfg(feature = "l1_fee_curie")] + { let [base_fee, l1BlobBaseFee, commitScalar, blobScalar] = [ + &l1_gas_price_oracle::BASE_FEE_SLOT, + &l1_gas_price_oracle::l1BlobBaseFee, + &l1_gas_price_oracle::commitScalar, + &l1_gas_price_oracle::blobScalar, + ] + .map(|slot| { + sdb.get_committed_storage(&l1_gas_price_oracle::ADDRESS, slot) + .1 + .as_u64() + }); + } + + + Self { base_fee, fee_overhead, fee_scalar, + #[cfg(feature = "l1_fee_curie")] + l1BlobBaseFee, + #[cfg(feature = "l1_fee_curie")] + commitScalar, + #[cfg(feature = "l1_fee_curie")] + blobScalar, } } } From 6e52f5819ac846d229791f4431d871b02dae0bd2 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Tue, 28 May 2024 09:14:52 +0800 Subject: [PATCH 03/33] add curies field into TxL1FeeGadget --- .../src/circuit_input_builder/transaction.rs | 8 +++---- .../util/common_gadget/tx_l1_fee.rs | 21 ++++++++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index e13c495aa9..21b73a17d7 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -490,11 +490,11 @@ impl TxL1Fee { // let tx_l1_gas = tx_data_gas_cost + self.fee_overhead + TX_L1_COMMIT_EXTRA_COST; let tx_l1_fee = self.fee_scalar as u128 * self.base_fee as u128 * tx_l1_gas as u128; - + // TODO: check if the calculation changes for curie upgrade ( - (tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64, - (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, - ) + (tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64, + (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, + ) } fn get_current_values_from_state_db(sdb: &StateDB) -> Self { diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index 9e96751ca2..826a3be3b0 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -31,12 +31,30 @@ pub(crate) struct TxL1FeeGadget { fee_overhead_word: U64Word, /// Current value of L1 fee scalar fee_scalar_word: U64Word, - /// Committed value of L1 base fee + #[cfg(feature = "l1_fee_curie")] + /// Current value of L1 blob base fee + l1BlobBaseFee: Cell, + #[cfg(feature = "l1_fee_curie")] + /// Current value of L1 scalar fee + commitScalar: Cell, + #[cfg(feature = "l1_fee_curie")] + /// Current value of L1 blob scalar fee + blobScalar: Cell, + /// Current value of L1 base fee base_fee_committed: Cell, /// Committed value of L1 fee overhead fee_overhead_committed: Cell, /// Committed value of L1 fee scalar fee_scalar_committed: Cell, + #[cfg(feature = "l1_fee_curie")] + /// Committed value of L1 blob base fee + l1BlobBaseFee_committed: Cell, + #[cfg(feature = "l1_fee_curie")] + /// Committed value of L1 scalar fee + commitScalar_committed: Cell, + #[cfg(feature = "l1_fee_curie")] + /// Committed value of L1 blob scalar fee + blobScalar_committed: Cell, } impl TxL1FeeGadget { @@ -51,6 +69,7 @@ impl TxL1FeeGadget { "Unexpected address of l2 gasprice oracle contract -> Scalar conversion failure", )); + //TODO: add curie fork fields let [base_fee_slot, overhead_slot, scalar_slot] = [ &l1_gas_price_oracle::BASE_FEE_SLOT, &l1_gas_price_oracle::OVERHEAD_SLOT, From 0bf279f1292a3a9365f9ac3632a5260fd728654d Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Tue, 28 May 2024 09:49:36 +0800 Subject: [PATCH 04/33] add curie fields reads --- .../util/common_gadget/tx_l1_fee.rs | 57 ++++++++++++++++--- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index 826a3be3b0..52dac2ac8f 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -33,13 +33,13 @@ pub(crate) struct TxL1FeeGadget { fee_scalar_word: U64Word, #[cfg(feature = "l1_fee_curie")] /// Current value of L1 blob base fee - l1BlobBaseFee: Cell, + l1blob_basefee_word: U64Word, #[cfg(feature = "l1_fee_curie")] /// Current value of L1 scalar fee - commitScalar: Cell, + commit_scalar_word: U64Word, #[cfg(feature = "l1_fee_curie")] /// Current value of L1 blob scalar fee - blobScalar: Cell, + blob_scalar_word: U64Word, /// Current value of L1 base fee base_fee_committed: Cell, /// Committed value of L1 fee overhead @@ -48,13 +48,13 @@ pub(crate) struct TxL1FeeGadget { fee_scalar_committed: Cell, #[cfg(feature = "l1_fee_curie")] /// Committed value of L1 blob base fee - l1BlobBaseFee_committed: Cell, + l1blob_baseFee_committed: Cell, #[cfg(feature = "l1_fee_curie")] /// Committed value of L1 scalar fee - commitScalar_committed: Cell, + commit_scalar_committed: Cell, #[cfg(feature = "l1_fee_curie")] /// Committed value of L1 blob scalar fee - blobScalar_committed: Cell, + blob_scalar_committed: Cell, } impl TxL1FeeGadget { @@ -76,7 +76,15 @@ impl TxL1FeeGadget { &l1_gas_price_oracle::SCALAR_SLOT, ] .map(|slot| cb.word_rlc(slot.to_le_bytes().map(|b| b.expr()))); - + + #[cfg(feature = "l1_fee_curie")] + let [l1blob_baseFee, commit_scalar, blob_scalar] = [ + &l1_gas_price_oracle::l1BlobBaseFee, + &l1_gas_price_oracle::commitScalar, + &l1_gas_price_oracle::blobScalar, + ] + .map(|slot| cb.word_rlc(slot.to_le_bytes().map(|b| b.expr()))); + // Read L1 base fee cb.account_storage_read( l1_fee_address.expr(), @@ -104,6 +112,39 @@ impl TxL1FeeGadget { this.fee_scalar_committed.expr(), ); + // TODO: check if can really reuse base_fee_slot read rw above for curie + // now try to skip it. + // for curie hard fork + #[cfg(feature = "l1_fee_curie")] + // Read l1blob_baseFee_committed + cb.account_storage_read( + l1_fee_address.expr(), + l1blob_baseFee, + this.l1blob_basefee_word.expr(), + tx_id.expr(), + this.l1blob_baseFee_committed.expr(), + ); + + #[cfg(feature = "l1_fee_curie")] + // Read L1 commit_scalar_committed + cb.account_storage_read( + l1_fee_address.expr(), + commit_scalar, + this.commit_scalar_word.expr(), + tx_id.expr(), + this.commit_scalar_committed.expr(), + ); + + #[cfg(feature = "l1_fee_curie")] + // Read L1 blob_scalar_committed scalar + cb.account_storage_read( + l1_fee_address, + blob_scalar, + this.blob_scalar_word.expr(), + tx_id, + this.blob_scalar_committed.expr(), + ); + this } @@ -141,6 +182,8 @@ impl TxL1FeeGadget { offset, region.word_rlc(l1_fee_committed.fee_scalar.into()), )?; + + // TODO: add curie fields assignment Ok(()) } From b4b62be3e1f90b9960d789c6a1f0e6fefed5d4d9 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Tue, 28 May 2024 10:16:01 +0800 Subject: [PATCH 05/33] assign curie fields --- .../src/circuit_input_builder/transaction.rs | 16 +++---- .../util/common_gadget/tx_l1_fee.rs | 48 ++++++++++++++++--- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index 21b73a17d7..c3cc4d7047 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -477,7 +477,7 @@ pub struct TxL1Fee { /// L1 fee scalar pub fee_scalar: u64, #[cfg(feature = "l1_fee_curie")] - pub l1BlobBaseFee: u64, + pub l1BlobBaseFee: u64, // TODO: rename #[cfg(feature = "l1_fee_curie")] pub commitScalar: u64, #[cfg(feature = "l1_fee_curie")] @@ -492,9 +492,9 @@ impl TxL1Fee { let tx_l1_fee = self.fee_scalar as u128 * self.base_fee as u128 * tx_l1_gas as u128; // TODO: check if the calculation changes for curie upgrade ( - (tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64, - (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, - ) + (tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64, + (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, + ) } fn get_current_values_from_state_db(sdb: &StateDB) -> Self { @@ -510,7 +510,8 @@ impl TxL1Fee { }); //if cfg!(feature = "l1_fee_curie") { #[cfg(feature = "l1_fee_curie")] - { let [base_fee, l1BlobBaseFee, commitScalar, blobScalar] = [ + { + let [base_fee, l1BlobBaseFee, commitScalar, blobScalar] = [ &l1_gas_price_oracle::BASE_FEE_SLOT, &l1_gas_price_oracle::l1BlobBaseFee, &l1_gas_price_oracle::commitScalar, @@ -549,7 +550,8 @@ impl TxL1Fee { }); #[cfg(feature = "l1_fee_curie")] - { let [base_fee, l1BlobBaseFee, commitScalar, blobScalar] = [ + { + let [base_fee, l1BlobBaseFee, commitScalar, blobScalar] = [ &l1_gas_price_oracle::BASE_FEE_SLOT, &l1_gas_price_oracle::l1BlobBaseFee, &l1_gas_price_oracle::commitScalar, @@ -561,8 +563,6 @@ impl TxL1Fee { .as_u64() }); } - - Self { base_fee, diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index 52dac2ac8f..4e1f09fd0d 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -76,7 +76,7 @@ impl TxL1FeeGadget { &l1_gas_price_oracle::SCALAR_SLOT, ] .map(|slot| cb.word_rlc(slot.to_le_bytes().map(|b| b.expr()))); - + #[cfg(feature = "l1_fee_curie")] let [l1blob_baseFee, commit_scalar, blob_scalar] = [ &l1_gas_price_oracle::l1BlobBaseFee, @@ -84,7 +84,7 @@ impl TxL1FeeGadget { &l1_gas_price_oracle::blobScalar, ] .map(|slot| cb.word_rlc(slot.to_le_bytes().map(|b| b.expr()))); - + // Read L1 base fee cb.account_storage_read( l1_fee_address.expr(), @@ -117,7 +117,7 @@ impl TxL1FeeGadget { // for curie hard fork #[cfg(feature = "l1_fee_curie")] // Read l1blob_baseFee_committed - cb.account_storage_read( + cb.account_storage_read( l1_fee_address.expr(), l1blob_baseFee, this.l1blob_basefee_word.expr(), @@ -167,6 +167,20 @@ impl TxL1FeeGadget { .assign(region, offset, Some(l1_fee.fee_overhead.to_le_bytes()))?; self.fee_scalar_word .assign(region, offset, Some(l1_fee.fee_scalar.to_le_bytes()))?; + // curie fields + #[cfg(feature = "l1_fee_curie")] + self.l1blob_basefee_word.assign( + region, + offset, + Some(l1_fee.l1BlobBaseFee.to_le_bytes()), + )?; + #[cfg(feature = "l1_fee_curie")] + self.commit_scalar_word + .assign(region, offset, Some(l1_fee.commitScalar.to_le_bytes()))?; + #[cfg(feature = "l1_fee_curie")] + self.blob_scalar_word + .assign(region, offset, Some(l1_fee.blobScalar.to_le_bytes()))?; + self.base_fee_committed.assign( region, offset, @@ -182,8 +196,26 @@ impl TxL1FeeGadget { offset, region.word_rlc(l1_fee_committed.fee_scalar.into()), )?; - - // TODO: add curie fields assignment + + // curie fields + #[cfg(feature = "l1_fee_curie")] + self.l1blob_basefee_word.assign( + region, + offset, + Some(l1_fee_committed.l1BlobBaseFee.to_le_bytes()), + )?; + #[cfg(feature = "l1_fee_curie")] + self.commit_scalar_word.assign( + region, + offset, + Some(l1_fee_committed.commitScalar.to_le_bytes()), + )?; + #[cfg(feature = "l1_fee_curie")] + self.blob_scalar_word.assign( + region, + offset, + Some(l1_fee_committed.blobScalar.to_le_bytes()), + )?; Ok(()) } @@ -192,7 +224,11 @@ impl TxL1FeeGadget { // L1 base fee Read // L1 fee overhead Read // L1 fee scalar Read - 3.expr() + // + curie fields + // l1 blob baseFee + // commit scalar + // blob scalar + 6.expr() } pub(crate) fn tx_l1_fee(&self) -> Expression { From a9113a9e263791163dd589dbc2a89454a2fa97e1 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Tue, 28 May 2024 11:35:31 +0800 Subject: [PATCH 06/33] update raw_construct helper --- .../util/common_gadget/tx_l1_fee.rs | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index 4e1f09fd0d..e04d14bb27 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -246,6 +246,13 @@ impl TxL1FeeGadget { let base_fee_word = cb.query_word_rlc(); let fee_overhead_word = cb.query_word_rlc(); let fee_scalar_word = cb.query_word_rlc(); + // curie fields + #[cfg(feature = "l1_fee_curie")] + let l1blob_basefee_word = cb.query_word_rlc(); + #[cfg(feature = "l1_fee_curie")] + let commit_scalar_word = cb.query_word_rlc(); + #[cfg(feature = "l1_fee_curie")] + let blob_scalar_word = cb.query_word_rlc(); let tx_l1_fee = from_bytes::expr(&tx_l1_fee_word.cells[..N_BYTES_U64]); let [remainder, base_fee, fee_overhead, fee_scalar] = [ @@ -256,17 +263,40 @@ impl TxL1FeeGadget { ] .map(|word| from_bytes::expr(&word.cells[..N_BYTES_U64])); + #[cfg(feature = "l1_fee_curie")] + let [l1blob_basefee, commit_scalar, blob_scalar] = + [&l1blob_basefee_word, &commit_scalar_word, &blob_scalar_word] + .map(|word| from_bytes::expr(&word.cells[..N_BYTES_U64])); + // let tx_l1_gas = tx_data_gas_cost + TX_L1_COMMIT_EXTRA_COST.expr() + fee_overhead; - cb.require_equal( - "fee_scalar * base_fee * tx_l1_gas == tx_l1_fee * 10e9 + remainder", + if cfg!(feature = "l1_fee_curie") { + // TODO: new formula for curie + cb.require_equal( + "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee == tx_l1_fee * 10e9 + remainder", fee_scalar * base_fee * tx_l1_gas, + // * tx_l1_gas, tx_l1_fee * TX_L1_FEE_PRECISION.expr() + remainder, ); + } else { + // before curie formula + cb.require_equal( + "fee_scalar * base_fee * tx_l1_gas == tx_l1_fee * 10e9 + remainder", + fee_scalar * base_fee * tx_l1_gas, + tx_l1_fee * TX_L1_FEE_PRECISION.expr() + remainder, + ); + } let base_fee_committed = cb.query_cell_phase2(); let fee_overhead_committed = cb.query_cell_phase2(); let fee_scalar_committed = cb.query_cell_phase2(); + // curie fields + #[cfg(feature = "l1_fee_curie")] + let l1blob_basefee_committed = cb.query_cell_phase2(); + #[cfg(feature = "l1_fee_curie")] + let commit_scalar_committed = cb.query_cell_phase2(); + #[cfg(feature = "l1_fee_curie")] + let blob_scalar_committed = cb.query_cell_phase2(); Self { tx_l1_fee_word, @@ -277,6 +307,12 @@ impl TxL1FeeGadget { base_fee_committed, fee_overhead_committed, fee_scalar_committed, + #[cfg(feature = "l1_fee_curie")] + l1blob_basefee_committed, + #[cfg(feature = "l1_fee_curie")] + commit_scalar_committed, + #[cfg(feature = "l1_fee_curie")] + blob_scalar_committed, } } } From af080aedaf9f8245c790ffb4083a9e1bdab2d14f Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Tue, 28 May 2024 14:51:39 +0800 Subject: [PATCH 07/33] update l1fee formula --- .../src/evm_circuit/execution/begin_tx.rs | 6 ++-- .../util/common_gadget/tx_l1_fee.rs | 28 +++++++++++-------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs index a6583281dd..6cf312563a 100644 --- a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs @@ -117,7 +117,8 @@ impl ExecutionGadget for BeginTxGadget { let sender_nonce = cb.query_cell(); - let [tx_type, tx_nonce, tx_gas, tx_caller_address, tx_callee_address, tx_is_create, tx_call_data_length, tx_call_data_gas_cost, tx_data_gas_cost] = + let [tx_type, tx_nonce, tx_gas, tx_caller_address, tx_callee_address, tx_is_create, tx_call_data_length, tx_call_data_gas_cost, + tx_data_gas_cost, tx_signed_hash_length] = [ TxContextFieldTag::TxType, TxContextFieldTag::Nonce, @@ -128,6 +129,7 @@ impl ExecutionGadget for BeginTxGadget { TxContextFieldTag::CallDataLength, TxContextFieldTag::CallDataGasCost, TxContextFieldTag::TxDataGasCost, + TxContextFieldTag::TxHashLength, ] .map(|field_tag| cb.tx_context(tx_id.expr(), field_tag, None)); @@ -141,7 +143,7 @@ impl ExecutionGadget for BeginTxGadget { tx_nonce.expr(), sender_nonce.expr(), ); - TxL1FeeGadget::construct(cb, tx_id.expr(), tx_data_gas_cost.expr()) + TxL1FeeGadget::construct(cb, tx_id.expr(), tx_signed_hash_length.expr()) }); cb.condition(tx_l1_msg.is_l1_msg(), |cb| { cb.require_zero("l1fee is 0 for l1msg", tx_data_gas_cost.expr()); diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index e04d14bb27..df094b684f 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -62,8 +62,9 @@ impl TxL1FeeGadget { cb: &mut EVMConstraintBuilder, tx_id: Expression, tx_data_gas_cost: Expression, + tx_signed_hash_length: Expression, ) -> Self { - let this = Self::raw_construct(cb, tx_data_gas_cost); + let this = Self::raw_construct(cb, tx_data_gas_cost, tx_signed_hash_length); let l1_fee_address = Expression::Constant(l1_gas_price_oracle::ADDRESS.to_scalar().expect( "Unexpected address of l2 gasprice oracle contract -> Scalar conversion failure", @@ -239,7 +240,7 @@ impl TxL1FeeGadget { &self.tx_l1_fee_word } - fn raw_construct(cb: &mut EVMConstraintBuilder, tx_data_gas_cost: Expression) -> Self { + fn raw_construct(cb: &mut EVMConstraintBuilder, tx_data_gas_cost: Expression, tx_signed_hash_length: Expression) -> Self { let tx_l1_fee_word = cb.query_word_rlc(); let remainder_word = cb.query_word_rlc(); @@ -269,23 +270,28 @@ impl TxL1FeeGadget { .map(|word| from_bytes::expr(&word.cells[..N_BYTES_U64])); // - let tx_l1_gas = tx_data_gas_cost + TX_L1_COMMIT_EXTRA_COST.expr() + fee_overhead; - if cfg!(feature = "l1_fee_curie") { - // TODO: new formula for curie + let tx_l1_gas = if cfg!(feature = "l1_fee_curie"){ + 0.expr() + }else{ + tx_data_gas_cost + TX_L1_COMMIT_EXTRA_COST.expr() + fee_overhead + }; + + // TODO: new formula for curie + #[cfg(feature = "l1_fee_curie")] cb.require_equal( "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee == tx_l1_fee * 10e9 + remainder", - fee_scalar * base_fee * tx_l1_gas, + commit_scalar * base_fee + blob_scalar * tx_signed_hash_length * l1blob_basefee, // * tx_l1_gas, tx_l1_fee * TX_L1_FEE_PRECISION.expr() + remainder, ); - } else { - // before curie formula - cb.require_equal( + + // old formula before curie + #[cfg(not(feature = "l1_fee_curie"))] + cb.require_equal( "fee_scalar * base_fee * tx_l1_gas == tx_l1_fee * 10e9 + remainder", fee_scalar * base_fee * tx_l1_gas, tx_l1_fee * TX_L1_FEE_PRECISION.expr() + remainder, - ); - } + ); let base_fee_committed = cb.query_cell_phase2(); let fee_overhead_committed = cb.query_cell_phase2(); From 7047e472f76ef580a1ce022ba0766c5eca2739b4 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Tue, 28 May 2024 15:05:08 +0800 Subject: [PATCH 08/33] rename --- .../src/circuit_input_builder/transaction.rs | 26 +++++++++---------- .../src/evm_circuit/execution/begin_tx.rs | 2 +- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index c3cc4d7047..af40edccf0 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -432,6 +432,7 @@ impl Transaction { /// Calculate L1 fee of this transaction. pub fn l1_fee(&self) -> u64 { + //TODO: check if need to update for curie let tx_data_gas_cost = tx_data_gas_cost(&self.rlp_bytes); self.l1_fee.tx_l1_fee(tx_data_gas_cost).0 @@ -477,11 +478,11 @@ pub struct TxL1Fee { /// L1 fee scalar pub fee_scalar: u64, #[cfg(feature = "l1_fee_curie")] - pub l1BlobBaseFee: u64, // TODO: rename + pub l1blob_baseFee: u64, #[cfg(feature = "l1_fee_curie")] - pub commitScalar: u64, + pub commit_scalar: u64, #[cfg(feature = "l1_fee_curie")] - pub blobScalar: u64, + pub blob_scalar: u64, } impl TxL1Fee { @@ -508,11 +509,9 @@ impl TxL1Fee { .1 .as_u64() }); - //if cfg!(feature = "l1_fee_curie") { #[cfg(feature = "l1_fee_curie")] { - let [base_fee, l1BlobBaseFee, commitScalar, blobScalar] = [ - &l1_gas_price_oracle::BASE_FEE_SLOT, + let [l1blob_baseFee, commit_scalar, blob_scalar] = [ &l1_gas_price_oracle::l1BlobBaseFee, &l1_gas_price_oracle::commitScalar, &l1_gas_price_oracle::blobScalar, @@ -529,11 +528,11 @@ impl TxL1Fee { fee_overhead, fee_scalar, #[cfg(feature = "l1_fee_curie")] - l1BlobBaseFee, + l1blob_basefee, #[cfg(feature = "l1_fee_curie")] - commitScalar, + commit_scalar, #[cfg(feature = "l1_fee_curie")] - blobScalar, + blob_scalar, } } @@ -551,8 +550,7 @@ impl TxL1Fee { #[cfg(feature = "l1_fee_curie")] { - let [base_fee, l1BlobBaseFee, commitScalar, blobScalar] = [ - &l1_gas_price_oracle::BASE_FEE_SLOT, + let [l1blob_basefee, commit_scalar, blob_scalar] = [ &l1_gas_price_oracle::l1BlobBaseFee, &l1_gas_price_oracle::commitScalar, &l1_gas_price_oracle::blobScalar, @@ -569,11 +567,11 @@ impl TxL1Fee { fee_overhead, fee_scalar, #[cfg(feature = "l1_fee_curie")] - l1BlobBaseFee, + l1blob_basefee, #[cfg(feature = "l1_fee_curie")] - commitScalar, + commit_scalar, #[cfg(feature = "l1_fee_curie")] - blobScalar, + blob_scalar, } } } diff --git a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs index 6cf312563a..706d004bdb 100644 --- a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs @@ -143,7 +143,7 @@ impl ExecutionGadget for BeginTxGadget { tx_nonce.expr(), sender_nonce.expr(), ); - TxL1FeeGadget::construct(cb, tx_id.expr(), tx_signed_hash_length.expr()) + TxL1FeeGadget::construct(cb, tx_id.expr(), tx_data_gas_cost.expr(),tx_signed_hash_length.expr()) }); cb.condition(tx_l1_msg.is_l1_msg(), |cb| { cb.require_zero("l1fee is 0 for l1msg", tx_data_gas_cost.expr()); From 4295cff6230cbee8a3360a8cf596453c9e925583 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Tue, 28 May 2024 16:15:38 +0800 Subject: [PATCH 09/33] use upper case --- bus-mapping/Cargo.toml | 3 +- .../src/circuit_input_builder/transaction.rs | 49 +++++++++---------- bus-mapping/src/l2_predeployed.rs | 6 +-- zkevm-circuits/Cargo.toml | 2 +- .../util/common_gadget/tx_l1_fee.rs | 6 +-- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/bus-mapping/Cargo.toml b/bus-mapping/Cargo.toml index 0af3a7425d..dbc157e1d9 100644 --- a/bus-mapping/Cargo.toml +++ b/bus-mapping/Cargo.toml @@ -54,4 +54,5 @@ rpc-legacy-tracer = [] # For the trace obtained from erigon node, refund field is missed # and must be rebuild fix-refund = ["rpc-legacy-tracer"] -retrace-tx = [] \ No newline at end of file +retrace-tx = [] +l1_fee_curie = [] \ No newline at end of file diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index af40edccf0..fe7c85ed54 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -478,10 +478,13 @@ pub struct TxL1Fee { /// L1 fee scalar pub fee_scalar: u64, #[cfg(feature = "l1_fee_curie")] - pub l1blob_baseFee: u64, + /// L1 blob fee + pub l1blob_basefee: u64, #[cfg(feature = "l1_fee_curie")] + /// L1 commit scalar pub commit_scalar: u64, #[cfg(feature = "l1_fee_curie")] + /// l1 blob scalar pub blob_scalar: u64, } @@ -510,18 +513,16 @@ impl TxL1Fee { .as_u64() }); #[cfg(feature = "l1_fee_curie")] - { - let [l1blob_baseFee, commit_scalar, blob_scalar] = [ - &l1_gas_price_oracle::l1BlobBaseFee, - &l1_gas_price_oracle::commitScalar, - &l1_gas_price_oracle::blobScalar, - ] - .map(|slot| { - sdb.get_storage(&l1_gas_price_oracle::ADDRESS, slot) - .1 - .as_u64() - }); - } + let [l1blob_basefee, commit_scalar, blob_scalar] = [ + &l1_gas_price_oracle::BlOB_BASEFEE, + &l1_gas_price_oracle::COMMIT_SCALAR, + &l1_gas_price_oracle::BLOB_SCALAR, + ] + .map(|slot| { + sdb.get_storage(&l1_gas_price_oracle::ADDRESS, slot) + .1 + .as_u64() + }); Self { base_fee, @@ -549,18 +550,16 @@ impl TxL1Fee { }); #[cfg(feature = "l1_fee_curie")] - { - let [l1blob_basefee, commit_scalar, blob_scalar] = [ - &l1_gas_price_oracle::l1BlobBaseFee, - &l1_gas_price_oracle::commitScalar, - &l1_gas_price_oracle::blobScalar, - ] - .map(|slot| { - sdb.get_committed_storage(&l1_gas_price_oracle::ADDRESS, slot) - .1 - .as_u64() - }); - } + let [l1blob_basefee, commit_scalar, blob_scalar] = [ + &l1_gas_price_oracle::BlOB_BASEFEE, + &l1_gas_price_oracle::COMMIT_SCALAR, + &l1_gas_price_oracle::BLOB_SCALAR, + ] + .map(|slot| { + sdb.get_committed_storage(&l1_gas_price_oracle::ADDRESS, slot) + .1 + .as_u64() + }); Self { base_fee, diff --git a/bus-mapping/src/l2_predeployed.rs b/bus-mapping/src/l2_predeployed.rs index 32f6a1c885..1045db601e 100644 --- a/bus-mapping/src/l2_predeployed.rs +++ b/bus-mapping/src/l2_predeployed.rs @@ -31,11 +31,11 @@ pub mod l1_gas_price_oracle { pub static SCALAR_SLOT: LazyLock = LazyLock::new(|| U256::from(3)); /// L1 BlobBaseFee slot in L1GasPriceOracle after Curie fork #[cfg(feature = "l1_fee_curie")] - pub static l1BlobBaseFee: LazyLock = LazyLock::new(|| U256::from(5)); + pub static BlOB_BASEFEE: LazyLock = LazyLock::new(|| U256::from(5)); #[cfg(feature = "l1_fee_curie")] /// L1 commitScalar slot in L1GasPriceOracle after Curie fork - pub static commitScalar: LazyLock = LazyLock::new(|| U256::from(6)); + pub static COMMIT_SCALAR: LazyLock = LazyLock::new(|| U256::from(6)); #[cfg(feature = "l1_fee_curie")] /// L1 blobScalar slot in L1GasPriceOracle after Curie fork - pub static blobScalar: LazyLock = LazyLock::new(|| U256::from(7)); + pub static BLOB_SCALAR: LazyLock = LazyLock::new(|| U256::from(7)); } diff --git a/zkevm-circuits/Cargo.toml b/zkevm-circuits/Cargo.toml index 93b94c2aa8..6eca8f2d59 100644 --- a/zkevm-circuits/Cargo.toml +++ b/zkevm-circuits/Cargo.toml @@ -71,4 +71,4 @@ debug-annotations = [] enable-stack = ["bus-mapping/enable-stack"] enable-memory = ["bus-mapping/enable-memory"] enable-storage = ["bus-mapping/enable-storage"] -l1_fee_curie = [] +l1_fee_curie = ["bus-mapping/l1_fee_curie"] diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index df094b684f..0746704a23 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -80,9 +80,9 @@ impl TxL1FeeGadget { #[cfg(feature = "l1_fee_curie")] let [l1blob_baseFee, commit_scalar, blob_scalar] = [ - &l1_gas_price_oracle::l1BlobBaseFee, - &l1_gas_price_oracle::commitScalar, - &l1_gas_price_oracle::blobScalar, + &l1_gas_price_oracle::BlOB_BASEFEE, + &l1_gas_price_oracle::COMMIT_SCALAR, + &l1_gas_price_oracle::BLOB_SCALAR, ] .map(|slot| cb.word_rlc(slot.to_le_bytes().map(|b| b.expr()))); From 2e787c44e3a6315fa2f45c172f63927bff7ae1da Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Tue, 28 May 2024 16:53:49 +0800 Subject: [PATCH 10/33] fix build --- .../src/circuit_input_builder/transaction.rs | 4 +-- bus-mapping/src/l2_predeployed.rs | 2 +- .../util/common_gadget/tx_l1_fee.rs | 30 +++++++++++-------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index fe7c85ed54..9c7dcb908e 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -514,7 +514,7 @@ impl TxL1Fee { }); #[cfg(feature = "l1_fee_curie")] let [l1blob_basefee, commit_scalar, blob_scalar] = [ - &l1_gas_price_oracle::BlOB_BASEFEE, + &l1_gas_price_oracle::BLOB_BASEFEE, &l1_gas_price_oracle::COMMIT_SCALAR, &l1_gas_price_oracle::BLOB_SCALAR, ] @@ -551,7 +551,7 @@ impl TxL1Fee { #[cfg(feature = "l1_fee_curie")] let [l1blob_basefee, commit_scalar, blob_scalar] = [ - &l1_gas_price_oracle::BlOB_BASEFEE, + &l1_gas_price_oracle::BLOB_BASEFEE, &l1_gas_price_oracle::COMMIT_SCALAR, &l1_gas_price_oracle::BLOB_SCALAR, ] diff --git a/bus-mapping/src/l2_predeployed.rs b/bus-mapping/src/l2_predeployed.rs index 1045db601e..9ca66e39cd 100644 --- a/bus-mapping/src/l2_predeployed.rs +++ b/bus-mapping/src/l2_predeployed.rs @@ -31,7 +31,7 @@ pub mod l1_gas_price_oracle { pub static SCALAR_SLOT: LazyLock = LazyLock::new(|| U256::from(3)); /// L1 BlobBaseFee slot in L1GasPriceOracle after Curie fork #[cfg(feature = "l1_fee_curie")] - pub static BlOB_BASEFEE: LazyLock = LazyLock::new(|| U256::from(5)); + pub static BLOB_BASEFEE: LazyLock = LazyLock::new(|| U256::from(5)); #[cfg(feature = "l1_fee_curie")] /// L1 commitScalar slot in L1GasPriceOracle after Curie fork pub static COMMIT_SCALAR: LazyLock = LazyLock::new(|| U256::from(6)); diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index 0746704a23..f84f2691b6 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -48,7 +48,7 @@ pub(crate) struct TxL1FeeGadget { fee_scalar_committed: Cell, #[cfg(feature = "l1_fee_curie")] /// Committed value of L1 blob base fee - l1blob_baseFee_committed: Cell, + l1blob_basefee_committed: Cell, #[cfg(feature = "l1_fee_curie")] /// Committed value of L1 scalar fee commit_scalar_committed: Cell, @@ -80,7 +80,7 @@ impl TxL1FeeGadget { #[cfg(feature = "l1_fee_curie")] let [l1blob_baseFee, commit_scalar, blob_scalar] = [ - &l1_gas_price_oracle::BlOB_BASEFEE, + &l1_gas_price_oracle::BLOB_BASEFEE, &l1_gas_price_oracle::COMMIT_SCALAR, &l1_gas_price_oracle::BLOB_SCALAR, ] @@ -106,10 +106,10 @@ impl TxL1FeeGadget { // Read L1 fee scalar cb.account_storage_read( - l1_fee_address, + l1_fee_address.clone(), scalar_slot, this.fee_scalar_word.expr(), - tx_id, + tx_id.clone(), this.fee_scalar_committed.expr(), ); @@ -122,8 +122,8 @@ impl TxL1FeeGadget { l1_fee_address.expr(), l1blob_baseFee, this.l1blob_basefee_word.expr(), - tx_id.expr(), - this.l1blob_baseFee_committed.expr(), + tx_id.clone(), + this.l1blob_basefee_committed.expr(), ); #[cfg(feature = "l1_fee_curie")] @@ -173,14 +173,14 @@ impl TxL1FeeGadget { self.l1blob_basefee_word.assign( region, offset, - Some(l1_fee.l1BlobBaseFee.to_le_bytes()), + Some(l1_fee.l1blob_basefee.to_le_bytes()), )?; #[cfg(feature = "l1_fee_curie")] self.commit_scalar_word - .assign(region, offset, Some(l1_fee.commitScalar.to_le_bytes()))?; + .assign(region, offset, Some(l1_fee.commit_scalar.to_le_bytes()))?; #[cfg(feature = "l1_fee_curie")] self.blob_scalar_word - .assign(region, offset, Some(l1_fee.blobScalar.to_le_bytes()))?; + .assign(region, offset, Some(l1_fee.blob_scalar.to_le_bytes()))?; self.base_fee_committed.assign( region, @@ -203,19 +203,19 @@ impl TxL1FeeGadget { self.l1blob_basefee_word.assign( region, offset, - Some(l1_fee_committed.l1BlobBaseFee.to_le_bytes()), + Some(l1_fee_committed.l1blob_basefee.to_le_bytes()), )?; #[cfg(feature = "l1_fee_curie")] self.commit_scalar_word.assign( region, offset, - Some(l1_fee_committed.commitScalar.to_le_bytes()), + Some(l1_fee_committed.commit_scalar.to_le_bytes()), )?; #[cfg(feature = "l1_fee_curie")] self.blob_scalar_word.assign( region, offset, - Some(l1_fee_committed.blobScalar.to_le_bytes()), + Some(l1_fee_committed.blob_scalar.to_le_bytes()), )?; Ok(()) @@ -310,6 +310,12 @@ impl TxL1FeeGadget { base_fee_word, fee_overhead_word, fee_scalar_word, + #[cfg(feature = "l1_fee_curie")] + l1blob_basefee_word, + #[cfg(feature = "l1_fee_curie")] + commit_scalar_word, + #[cfg(feature = "l1_fee_curie")] + blob_scalar_word, base_fee_committed, fee_overhead_committed, fee_scalar_committed, From fa0f99b83aff882a59c7d30391dcd4704a356ae2 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Tue, 28 May 2024 17:23:44 +0800 Subject: [PATCH 11/33] fmt align --- .../src/circuit_input_builder/transaction.rs | 26 ++++---- bus-mapping/src/l2_predeployed.rs | 6 +- .../src/evm_circuit/execution/begin_tx.rs | 10 +++- .../util/common_gadget/tx_l1_fee.rs | 59 ++++++++++--------- 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index 9c7dcb908e..493217c493 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -478,10 +478,10 @@ pub struct TxL1Fee { /// L1 fee scalar pub fee_scalar: u64, #[cfg(feature = "l1_fee_curie")] - /// L1 blob fee - pub l1blob_basefee: u64, + /// L1 blob fee + pub l1_blob_basefee: u64, #[cfg(feature = "l1_fee_curie")] - /// L1 commit scalar + /// L1 commit scalar pub commit_scalar: u64, #[cfg(feature = "l1_fee_curie")] /// l1 blob scalar @@ -513,10 +513,10 @@ impl TxL1Fee { .as_u64() }); #[cfg(feature = "l1_fee_curie")] - let [l1blob_basefee, commit_scalar, blob_scalar] = [ - &l1_gas_price_oracle::BLOB_BASEFEE, - &l1_gas_price_oracle::COMMIT_SCALAR, - &l1_gas_price_oracle::BLOB_SCALAR, + let [l1_blob_basefee, commit_scalar, blob_scalar] = [ + &l1_gas_price_oracle::L1_BLOB_BASEFEE_SLOT, + &l1_gas_price_oracle::COMMIT_SCALAR_SLOT, + &l1_gas_price_oracle::BLOB_SCALAR_SLOT, ] .map(|slot| { sdb.get_storage(&l1_gas_price_oracle::ADDRESS, slot) @@ -529,7 +529,7 @@ impl TxL1Fee { fee_overhead, fee_scalar, #[cfg(feature = "l1_fee_curie")] - l1blob_basefee, + l1_blob_basefee, #[cfg(feature = "l1_fee_curie")] commit_scalar, #[cfg(feature = "l1_fee_curie")] @@ -550,10 +550,10 @@ impl TxL1Fee { }); #[cfg(feature = "l1_fee_curie")] - let [l1blob_basefee, commit_scalar, blob_scalar] = [ - &l1_gas_price_oracle::BLOB_BASEFEE, - &l1_gas_price_oracle::COMMIT_SCALAR, - &l1_gas_price_oracle::BLOB_SCALAR, + let [l1_blob_basefee, commit_scalar, blob_scalar] = [ + &l1_gas_price_oracle::L1_BLOB_BASEFEE_SLOT, + &l1_gas_price_oracle::COMMIT_SCALAR_SLOT, + &l1_gas_price_oracle::BLOB_SCALAR_SLOT, ] .map(|slot| { sdb.get_committed_storage(&l1_gas_price_oracle::ADDRESS, slot) @@ -566,7 +566,7 @@ impl TxL1Fee { fee_overhead, fee_scalar, #[cfg(feature = "l1_fee_curie")] - l1blob_basefee, + l1_blob_basefee, #[cfg(feature = "l1_fee_curie")] commit_scalar, #[cfg(feature = "l1_fee_curie")] diff --git a/bus-mapping/src/l2_predeployed.rs b/bus-mapping/src/l2_predeployed.rs index f08c76161e..448c0ce792 100644 --- a/bus-mapping/src/l2_predeployed.rs +++ b/bus-mapping/src/l2_predeployed.rs @@ -34,15 +34,13 @@ pub mod l1_gas_price_oracle { pub static OVERHEAD_SLOT: LazyLock = LazyLock::new(|| U256::from(2)); /// L1 scalar slot in L1GasPriceOracle pub static SCALAR_SLOT: LazyLock = LazyLock::new(|| U256::from(3)); - + /// 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 - #[cfg(feature = "l1_fee_curie")] pub static L1_BLOB_BASEFEE_SLOT: LazyLock = LazyLock::new(|| U256::from(5)); - #[cfg(feature = "l1_fee_curie")] /// L1 commitScalar slot in L1GasPriceOracle after Curie fork pub static COMMIT_SCALAR_SLOT: LazyLock = LazyLock::new(|| U256::from(6)); - #[cfg(feature = "l1_fee_curie")] + /// L1 commitScalar slot in L1GasPriceOracle after Curie forks pub static BLOB_SCALAR_SLOT: LazyLock = LazyLock::new(|| U256::from(7)); pub static IS_CURIE_SLOT: LazyLock = LazyLock::new(|| U256::from(8)); pub static INITIAL_COMMIT_SCALAR: LazyLock = diff --git a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs index 9f9cec0478..9388a707e8 100644 --- a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs @@ -117,8 +117,7 @@ impl ExecutionGadget for BeginTxGadget { let sender_nonce = cb.query_cell(); - let [tx_type, tx_nonce, tx_gas, tx_caller_address, tx_callee_address, tx_is_create, tx_call_data_length, tx_call_data_gas_cost, - tx_data_gas_cost, tx_signed_hash_length] = + let [tx_type, tx_nonce, tx_gas, tx_caller_address, tx_callee_address, tx_is_create, tx_call_data_length, tx_call_data_gas_cost, tx_data_gas_cost, tx_signed_hash_length] = [ TxContextFieldTag::TxType, TxContextFieldTag::Nonce, @@ -143,7 +142,12 @@ impl ExecutionGadget for BeginTxGadget { tx_nonce.expr(), sender_nonce.expr(), ); - TxL1FeeGadget::construct(cb, tx_id.expr(), tx_data_gas_cost.expr(),tx_signed_hash_length.expr()) + TxL1FeeGadget::construct( + cb, + tx_id.expr(), + tx_data_gas_cost.expr(), + tx_signed_hash_length.expr(), + ) }); cb.condition(tx_l1_msg.is_l1_msg(), |cb| { cb.require_zero("l1fee is 0 for l1msg", tx_data_gas_cost.expr()); diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index f84f2691b6..aa598b0a91 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -33,7 +33,7 @@ pub(crate) struct TxL1FeeGadget { fee_scalar_word: U64Word, #[cfg(feature = "l1_fee_curie")] /// Current value of L1 blob base fee - l1blob_basefee_word: U64Word, + l1_blob_basefee_word: U64Word, #[cfg(feature = "l1_fee_curie")] /// Current value of L1 scalar fee commit_scalar_word: U64Word, @@ -48,7 +48,7 @@ pub(crate) struct TxL1FeeGadget { fee_scalar_committed: Cell, #[cfg(feature = "l1_fee_curie")] /// Committed value of L1 blob base fee - l1blob_basefee_committed: Cell, + l1_blob_basefee_committed: Cell, #[cfg(feature = "l1_fee_curie")] /// Committed value of L1 scalar fee commit_scalar_committed: Cell, @@ -79,10 +79,10 @@ impl TxL1FeeGadget { .map(|slot| cb.word_rlc(slot.to_le_bytes().map(|b| b.expr()))); #[cfg(feature = "l1_fee_curie")] - let [l1blob_baseFee, commit_scalar, blob_scalar] = [ - &l1_gas_price_oracle::BLOB_BASEFEE, - &l1_gas_price_oracle::COMMIT_SCALAR, - &l1_gas_price_oracle::BLOB_SCALAR, + let [l1_blob_baseFee, commit_scalar, blob_scalar] = [ + &l1_gas_price_oracle::L1_BLOB_BASEFEE_SLOT, + &l1_gas_price_oracle::COMMIT_SCALAR_SLOT, + &l1_gas_price_oracle::BLOB_SCALAR_SLOT, ] .map(|slot| cb.word_rlc(slot.to_le_bytes().map(|b| b.expr()))); @@ -120,10 +120,10 @@ impl TxL1FeeGadget { // Read l1blob_baseFee_committed cb.account_storage_read( l1_fee_address.expr(), - l1blob_baseFee, - this.l1blob_basefee_word.expr(), + l1_blob_baseFee, + this.l1_blob_basefee_word.expr(), tx_id.clone(), - this.l1blob_basefee_committed.expr(), + this.l1_blob_basefee_committed.expr(), ); #[cfg(feature = "l1_fee_curie")] @@ -168,12 +168,13 @@ impl TxL1FeeGadget { .assign(region, offset, Some(l1_fee.fee_overhead.to_le_bytes()))?; self.fee_scalar_word .assign(region, offset, Some(l1_fee.fee_scalar.to_le_bytes()))?; + // curie fields #[cfg(feature = "l1_fee_curie")] - self.l1blob_basefee_word.assign( + self.l1_blob_basefee_word.assign( region, offset, - Some(l1_fee.l1blob_basefee.to_le_bytes()), + Some(l1_fee.l1_blob_basefee.to_le_bytes()), )?; #[cfg(feature = "l1_fee_curie")] self.commit_scalar_word @@ -200,10 +201,10 @@ impl TxL1FeeGadget { // curie fields #[cfg(feature = "l1_fee_curie")] - self.l1blob_basefee_word.assign( + self.l1_blob_basefee_word.assign( region, offset, - Some(l1_fee_committed.l1blob_basefee.to_le_bytes()), + Some(l1_fee_committed.l1_blob_basefee.to_le_bytes()), )?; #[cfg(feature = "l1_fee_curie")] self.commit_scalar_word.assign( @@ -240,7 +241,11 @@ impl TxL1FeeGadget { &self.tx_l1_fee_word } - fn raw_construct(cb: &mut EVMConstraintBuilder, tx_data_gas_cost: Expression, tx_signed_hash_length: Expression) -> Self { + fn raw_construct( + cb: &mut EVMConstraintBuilder, + tx_data_gas_cost: Expression, + tx_signed_hash_length: Expression, + ) -> Self { let tx_l1_fee_word = cb.query_word_rlc(); let remainder_word = cb.query_word_rlc(); @@ -249,7 +254,7 @@ impl TxL1FeeGadget { let fee_scalar_word = cb.query_word_rlc(); // curie fields #[cfg(feature = "l1_fee_curie")] - let l1blob_basefee_word = cb.query_word_rlc(); + let l1_blob_basefee_word = cb.query_word_rlc(); #[cfg(feature = "l1_fee_curie")] let commit_scalar_word = cb.query_word_rlc(); #[cfg(feature = "l1_fee_curie")] @@ -265,14 +270,14 @@ impl TxL1FeeGadget { .map(|word| from_bytes::expr(&word.cells[..N_BYTES_U64])); #[cfg(feature = "l1_fee_curie")] - let [l1blob_basefee, commit_scalar, blob_scalar] = - [&l1blob_basefee_word, &commit_scalar_word, &blob_scalar_word] + let [l1_blob_basefee, commit_scalar, blob_scalar] = + [&l1_blob_basefee_word, &commit_scalar_word, &blob_scalar_word] .map(|word| from_bytes::expr(&word.cells[..N_BYTES_U64])); // - let tx_l1_gas = if cfg!(feature = "l1_fee_curie"){ + let tx_l1_gas = if cfg!(feature = "l1_fee_curie") { 0.expr() - }else{ + } else { tx_data_gas_cost + TX_L1_COMMIT_EXTRA_COST.expr() + fee_overhead }; @@ -280,17 +285,17 @@ impl TxL1FeeGadget { #[cfg(feature = "l1_fee_curie")] cb.require_equal( "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee == tx_l1_fee * 10e9 + remainder", - commit_scalar * base_fee + blob_scalar * tx_signed_hash_length * l1blob_basefee, + commit_scalar * base_fee + blob_scalar * tx_signed_hash_length * l1_blob_basefee, // * tx_l1_gas, tx_l1_fee * TX_L1_FEE_PRECISION.expr() + remainder, ); - + // old formula before curie #[cfg(not(feature = "l1_fee_curie"))] cb.require_equal( - "fee_scalar * base_fee * tx_l1_gas == tx_l1_fee * 10e9 + remainder", - fee_scalar * base_fee * tx_l1_gas, - tx_l1_fee * TX_L1_FEE_PRECISION.expr() + remainder, + "fee_scalar * base_fee * tx_l1_gas == tx_l1_fee * 10e9 + remainder", + fee_scalar * base_fee * tx_l1_gas, + tx_l1_fee * TX_L1_FEE_PRECISION.expr() + remainder, ); let base_fee_committed = cb.query_cell_phase2(); @@ -298,7 +303,7 @@ impl TxL1FeeGadget { let fee_scalar_committed = cb.query_cell_phase2(); // curie fields #[cfg(feature = "l1_fee_curie")] - let l1blob_basefee_committed = cb.query_cell_phase2(); + let l1_blob_basefee_committed = cb.query_cell_phase2(); #[cfg(feature = "l1_fee_curie")] let commit_scalar_committed = cb.query_cell_phase2(); #[cfg(feature = "l1_fee_curie")] @@ -311,7 +316,7 @@ impl TxL1FeeGadget { fee_overhead_word, fee_scalar_word, #[cfg(feature = "l1_fee_curie")] - l1blob_basefee_word, + l1_blob_basefee_word, #[cfg(feature = "l1_fee_curie")] commit_scalar_word, #[cfg(feature = "l1_fee_curie")] @@ -320,7 +325,7 @@ impl TxL1FeeGadget { fee_overhead_committed, fee_scalar_committed, #[cfg(feature = "l1_fee_curie")] - l1blob_basefee_committed, + l1_blob_basefee_committed, #[cfg(feature = "l1_fee_curie")] commit_scalar_committed, #[cfg(feature = "l1_fee_curie")] From 830bb91ff3a63928017f92821d821882727e7f75 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Tue, 28 May 2024 22:27:17 +0800 Subject: [PATCH 12/33] fix test build --- .../src/evm_circuit/util/common_gadget/tx_l1_fee.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index aa598b0a91..6363b300dd 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -391,7 +391,9 @@ mod tests { let tx_data_gas_cost = cb.query_cell(); let expected_tx_l1_fee = cb.query_cell(); - let gadget = TxL1FeeGadget::::raw_construct(cb, tx_data_gas_cost.expr()); + // for non "l1_fee_curie" feature, tx_signed_hash_length is not used, can + // set to zero + let gadget = TxL1FeeGadget::::raw_construct(cb, tx_data_gas_cost.expr(), 0.expr()); cb.require_equal( "tx_l1_fee must be correct", From e9bf085007b25387aaf7c112b6dfa36ab5ec1ede Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 09:29:52 +0800 Subject: [PATCH 13/33] fix ci test --- .../src/evm_circuit/execution/begin_tx.rs | 7 +++-- .../util/common_gadget/tx_l1_fee.rs | 28 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs index 9388a707e8..3a82b0a579 100644 --- a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs @@ -117,7 +117,7 @@ impl ExecutionGadget for BeginTxGadget { let sender_nonce = cb.query_cell(); - let [tx_type, tx_nonce, tx_gas, tx_caller_address, tx_callee_address, tx_is_create, tx_call_data_length, tx_call_data_gas_cost, tx_data_gas_cost, tx_signed_hash_length] = + let [tx_type, tx_nonce, tx_gas, tx_caller_address, tx_callee_address, tx_is_create, tx_call_data_length, tx_call_data_gas_cost, tx_data_gas_cost] = [ TxContextFieldTag::TxType, TxContextFieldTag::Nonce, @@ -128,10 +128,12 @@ impl ExecutionGadget for BeginTxGadget { TxContextFieldTag::CallDataLength, TxContextFieldTag::CallDataGasCost, TxContextFieldTag::TxDataGasCost, - TxContextFieldTag::TxHashLength, ] .map(|field_tag| cb.tx_context(tx_id.expr(), field_tag, None)); + #[cfg(feature = "l1_fee_curie")] + let tx_signed_hash_length = + cb.tx_context(tx_id.expr(), TxContextFieldTag::TxHashLength, None); let tx_access_list = TxAccessListGadget::construct(cb, tx_id.expr(), tx_type.expr()); let is_call_data_empty = IsZeroGadget::construct(cb, tx_call_data_length.expr()); @@ -146,6 +148,7 @@ impl ExecutionGadget for BeginTxGadget { cb, tx_id.expr(), tx_data_gas_cost.expr(), + #[cfg(feature = "l1_fee_curie")] tx_signed_hash_length.expr(), ) }); diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index 6363b300dd..2c087d3987 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -62,9 +62,12 @@ impl TxL1FeeGadget { cb: &mut EVMConstraintBuilder, tx_id: Expression, tx_data_gas_cost: Expression, - tx_signed_hash_length: Expression, + #[cfg(feature = "l1_fee_curie")] tx_signed_hash_length: Expression, ) -> Self { + #[cfg(feature = "l1_fee_curie")] let this = Self::raw_construct(cb, tx_data_gas_cost, tx_signed_hash_length); + #[cfg(not(feature = "l1_fee_curie"))] + let this = Self::raw_construct(cb, tx_data_gas_cost); let l1_fee_address = Expression::Constant(l1_gas_price_oracle::ADDRESS.to_scalar().expect( "Unexpected address of l2 gasprice oracle contract -> Scalar conversion failure", @@ -168,7 +171,7 @@ impl TxL1FeeGadget { .assign(region, offset, Some(l1_fee.fee_overhead.to_le_bytes()))?; self.fee_scalar_word .assign(region, offset, Some(l1_fee.fee_scalar.to_le_bytes()))?; - + // curie fields #[cfg(feature = "l1_fee_curie")] self.l1_blob_basefee_word.assign( @@ -230,7 +233,11 @@ impl TxL1FeeGadget { // l1 blob baseFee // commit scalar // blob scalar - 6.expr() + if cfg!(feature = "l1_fee_curie") { + 6.expr() + } else { + 3.expr() + } } pub(crate) fn tx_l1_fee(&self) -> Expression { @@ -244,7 +251,7 @@ impl TxL1FeeGadget { fn raw_construct( cb: &mut EVMConstraintBuilder, tx_data_gas_cost: Expression, - tx_signed_hash_length: Expression, + #[cfg(feature = "l1_fee_curie")] tx_signed_hash_length: Expression, ) -> Self { let tx_l1_fee_word = cb.query_word_rlc(); let remainder_word = cb.query_word_rlc(); @@ -270,9 +277,12 @@ impl TxL1FeeGadget { .map(|word| from_bytes::expr(&word.cells[..N_BYTES_U64])); #[cfg(feature = "l1_fee_curie")] - let [l1_blob_basefee, commit_scalar, blob_scalar] = - [&l1_blob_basefee_word, &commit_scalar_word, &blob_scalar_word] - .map(|word| from_bytes::expr(&word.cells[..N_BYTES_U64])); + let [l1_blob_basefee, commit_scalar, blob_scalar] = [ + &l1_blob_basefee_word, + &commit_scalar_word, + &blob_scalar_word, + ] + .map(|word| from_bytes::expr(&word.cells[..N_BYTES_U64])); // let tx_l1_gas = if cfg!(feature = "l1_fee_curie") { @@ -391,9 +401,9 @@ mod tests { let tx_data_gas_cost = cb.query_cell(); let expected_tx_l1_fee = cb.query_cell(); - // for non "l1_fee_curie" feature, tx_signed_hash_length is not used, can + // for non "l1_fee_curie" feature, tx_signed_hash_length is not used, can // set to zero - let gadget = TxL1FeeGadget::::raw_construct(cb, tx_data_gas_cost.expr(), 0.expr()); + let gadget = TxL1FeeGadget::::raw_construct(cb, tx_data_gas_cost.expr()); cb.require_equal( "tx_l1_fee must be correct", From 0cd77fa222cb290e591ca84ed7535bf9495949e8 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 12:14:11 +0800 Subject: [PATCH 14/33] add get_rlp_signed --- .../src/circuit_input_builder/transaction.rs | 52 ++++++++++++++----- eth-types/src/geth_types.rs | 46 +++++++++++++++- 2 files changed, 84 insertions(+), 14 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index 493217c493..5b1c6eb3ba 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -5,7 +5,7 @@ use crate::{l2_predeployed::l1_gas_price_oracle, Error}; use eth_types::{ evm_types::{gas_utils::tx_data_gas_cost, OpcodeId}, geth_types, - geth_types::{get_rlp_unsigned, TxType}, + geth_types::{get_rlp_signed, get_rlp_unsigned, TxType}, state_db::{CodeDB, StateDB}, AccessList, Address, GethExecTrace, Signature, Word, H256, }; @@ -204,6 +204,8 @@ pub struct Transaction { pub rlp_bytes: Vec, /// RLP bytes for signing pub rlp_unsigned_bytes: Vec, + /// RLP bytes for signing + pub rlp_signed_bytes: Vec, /// Current values of L1 fee pub l1_fee: TxL1Fee, /// Committed values of L1 fee @@ -233,6 +235,7 @@ impl From<&Transaction> for geth_types::Transaction { gas_fee_cap: Some(tx.gas_fee_cap), gas_tip_cap: Some(tx.gas_tip_cap), rlp_unsigned_bytes: tx.rlp_unsigned_bytes.clone(), + //rlp_signed_bytes: tx.rlp_signed_bytes.clone(), rlp_bytes: tx.rlp_bytes.clone(), tx_type: tx.tx_type, ..Default::default() @@ -261,6 +264,7 @@ impl Transaction { }, rlp_bytes: vec![], rlp_unsigned_bytes: vec![], + rlp_signed_bytes: vec![], calls: Vec::new(), steps: Vec::new(), block_num: Default::default(), @@ -362,6 +366,7 @@ impl Transaction { tx_type, rlp_bytes: eth_tx.rlp().to_vec(), rlp_unsigned_bytes: get_rlp_unsigned(eth_tx), + rlp_signed_bytes: get_rlp_signed(eth_tx), nonce: eth_tx.nonce.as_u64(), gas: eth_tx.gas.as_u64(), gas_price: eth_tx.gas_price.unwrap_or_default(), @@ -433,9 +438,17 @@ impl Transaction { /// Calculate L1 fee of this transaction. pub fn l1_fee(&self) -> u64 { //TODO: check if need to update for curie - let tx_data_gas_cost = tx_data_gas_cost(&self.rlp_bytes); - - self.l1_fee.tx_l1_fee(tx_data_gas_cost).0 + #[cfg(not(feature = "l1_fee_curie"))] + { + let tx_data_gas_cost = tx_data_gas_cost(&self.rlp_bytes); + self.l1_fee.tx_l1_fee(tx_data_gas_cost, 0).0 + } + #[cfg(feature = "l1_fee_curie")] + { + // TODO: calculate tx rlp signed length + let tx_signed_length = self.rlp_signed_bytes.len(); + self.l1_fee.tx_l1_fee(0, tx_signed_length).0 + } } } @@ -490,15 +503,30 @@ pub struct TxL1Fee { impl TxL1Fee { /// Calculate L1 fee and remainder of transaction. - pub fn tx_l1_fee(&self, tx_data_gas_cost: u64) -> (u64, u64) { + /// for non curie upgrade case, tx_rlp_signed_len is not used, set to zero + pub fn tx_l1_fee(&self, tx_data_gas_cost: u64, tx_rlp_signed_len: u64) -> (u64, u64) { // - let tx_l1_gas = tx_data_gas_cost + self.fee_overhead + TX_L1_COMMIT_EXTRA_COST; - let tx_l1_fee = self.fee_scalar as u128 * self.base_fee as u128 * tx_l1_gas as u128; - // TODO: check if the calculation changes for curie upgrade - ( - (tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64, - (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, - ) + // check if the calculation changes for curie upgrade + #[cfg(not(feature = "l1_fee_curie"))] + { + let tx_l1_gas = tx_data_gas_cost + self.fee_overhead + TX_L1_COMMIT_EXTRA_COST; + let tx_l1_fee = self.fee_scalar as u128 * self.base_fee as u128 * tx_l1_gas as u128; + ( + (tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64, + (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, + ) + } + + #[cfg(feature = "l1_fee_curie")] + { + // "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee", + let tx_l1_fee = self.commit_scalar * self.l1_blob_basefee + + tx_rlp_signed_len * self.l1_blob_basefee; + ( + (tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64, + (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, + ) + } } fn get_current_values_from_state_db(sdb: &StateDB) -> Self { diff --git a/eth-types/src/geth_types.rs b/eth-types/src/geth_types.rs index e0b42f5d15..d785934c32 100644 --- a/eth-types/src/geth_types.rs +++ b/eth-types/src/geth_types.rs @@ -9,7 +9,7 @@ use crate::{ }; use ethers_core::types::{ transaction::eip2718::TypedTransaction, Eip1559TransactionRequest, Eip2930TransactionRequest, - NameOrAddress, TransactionRequest, H256, + NameOrAddress, Signature, TransactionRequest, H256, }; use halo2curves::{group::ff::PrimeField, secp256k1::Fq}; use num::Integer; @@ -158,6 +158,48 @@ pub fn get_rlp_unsigned(tx: &crate::Transaction) -> Vec { } } +/// Get the RLP signed bytes +/// NOTE: currently only used by curie upgrade calculating l1fee +pub fn get_rlp_signed(tx: &crate::Transaction) -> Vec { + let sig_v = tx.v; // U64 type + let signature = Signature { + r: tx.r, + s: tx.s, + v: tx.v.as_u64(), + }; + + match TxType::get_tx_type(tx) { + TxType::Eip155 => { + let mut tx: TransactionRequest = tx.into(); + tx.chain_id = Some(tx.chain_id.unwrap_or_else(|| { + let recv_v = TxType::Eip155.get_recovery_id(sig_v.as_u64()) as u64; + (sig_v - recv_v - 35) / 2 + })); + + tx.rlp_signed(&signature).to_vec() + } + TxType::PreEip155 => { + let tx: TransactionRequest = tx.into(); + tx.rlp_signed(&signature).to_vec() + } + TxType::Eip1559 => { + let tx: Eip1559TransactionRequest = tx.into(); + let typed_tx: TypedTransaction = tx.into(); + typed_tx.rlp_signed(&signature).to_vec() + } + TxType::Eip2930 => { + let tx: Eip2930TransactionRequest = tx.into(); + let typed_tx: TypedTransaction = tx.into(); + //typed_tx.rlp().to_vec() + typed_tx.rlp_signed(&signature).to_vec() + } + TxType::L1Msg => { + // L1 msg does not have signature + vec![] + } + } +} + /// Definition of all of the data related to an account. #[serde_as] #[derive(PartialEq, Eq, Debug, Default, Clone, Serialize)] @@ -289,7 +331,7 @@ pub struct Transaction { pub rlp_bytes: Vec, /// RLP unsigned bytes pub rlp_unsigned_bytes: Vec, - + // TODO: add rlp_signed_bytes as well ? /// Transaction hash pub hash: H256, } From e3e7af7010d9cb26f4047e7de97188d3f1fdd5fb Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 12:29:23 +0800 Subject: [PATCH 15/33] update end_tx etc --- .../src/evm_circuit/execution/begin_tx.rs | 20 +++++++++++++++---- .../src/evm_circuit/execution/end_tx.rs | 6 +++++- .../util/common_gadget/tx_l1_fee.rs | 8 +++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs index 3a82b0a579..cdcfa21a65 100644 --- a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs @@ -1200,10 +1200,21 @@ impl ExecutionGadget for BeginTxGadget { log::trace!("tx is l1msg and l1 fee is 0"); (U256::zero(), U256::zero()) } else { - ( - tx.l1_fee.tx_l1_fee(tx.tx_data_gas_cost).0.into(), - tx.gas_price * tx.gas, - ) + #[cfg(not(feature = "l1_fee_curie"))] + { + ( + tx.l1_fee.tx_l1_fee(tx.tx_data_gas_cost, 0).0.into(), + tx.gas_price * tx.gas, + ) + } + + #[cfg(feature = "l1_fee_curie")] + { + ( + tx.l1_fee.tx_l1_fee(0, tx.rlp_signed.len()).0.into(), + tx.gas_price * tx.gas, + ) + } }; if tx_fee != tx_l2_fee + tx_l1_fee { log::error!( @@ -1220,6 +1231,7 @@ impl ExecutionGadget for BeginTxGadget { tx.l1_fee, tx.l1_fee_committed, tx.tx_data_gas_cost, + tx.rlp_signed.len().try_into().unwrap(), )?; self.tx_access_list.assign(region, offset, tx)?; diff --git a/zkevm-circuits/src/evm_circuit/execution/end_tx.rs b/zkevm-circuits/src/evm_circuit/execution/end_tx.rs index 71af98bc10..1233e484f6 100644 --- a/zkevm-circuits/src/evm_circuit/execution/end_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/end_tx.rs @@ -415,7 +415,11 @@ impl ExecutionGadget for EndTxGadget { log::trace!("tx is l1msg and l1 fee is 0"); 0 } else { - tx.l1_fee.tx_l1_fee(tx.tx_data_gas_cost).0 + if cfg!(feature = "l1_fee_curie") { + tx.l1_fee.tx_l1_fee(0, tx.rlp_signed.len().try_into().unwrap()).0 + }else{ + tx.l1_fee.tx_l1_fee(tx.tx_data_gas_cost, 0).0 + } }; log::trace!( "tx_l1_fee: {}, coinbase_reward: {}", diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index 2c087d3987..4ad41c1deb 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -159,8 +159,14 @@ impl TxL1FeeGadget { l1_fee: TxL1Fee, l1_fee_committed: TxL1Fee, tx_data_gas_cost: u64, + tx_signed_length: u64, ) -> Result<(), Error> { - let (tx_l1_fee, remainder) = l1_fee.tx_l1_fee(tx_data_gas_cost); + let (tx_l1_fee, remainder) = if cfg!(feature = "l1_fee_curie") { + l1_fee.tx_l1_fee(tx_data_gas_cost, 0) + } else { + l1_fee.tx_l1_fee(0, tx_signed_length) + }; + self.tx_l1_fee_word .assign(region, offset, Some(U256::from(tx_l1_fee).to_le_bytes()))?; self.remainder_word From 76faebc8718dfd69fab5aa4bac47048be56c0c53 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 14:32:00 +0800 Subject: [PATCH 16/33] update TxL1FeeGadgetTestContainer --- zkevm-circuits/src/evm_circuit/execution/end_tx.rs | 6 ++++-- .../src/evm_circuit/util/common_gadget/tx_l1_fee.rs | 1 + zkevm-circuits/src/witness/l1_msg.rs | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/execution/end_tx.rs b/zkevm-circuits/src/evm_circuit/execution/end_tx.rs index 1233e484f6..bd85added8 100644 --- a/zkevm-circuits/src/evm_circuit/execution/end_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/end_tx.rs @@ -416,8 +416,10 @@ impl ExecutionGadget for EndTxGadget { 0 } else { if cfg!(feature = "l1_fee_curie") { - tx.l1_fee.tx_l1_fee(0, tx.rlp_signed.len().try_into().unwrap()).0 - }else{ + tx.l1_fee + .tx_l1_fee(0, tx.rlp_signed.len().try_into().unwrap()) + .0 + } else { tx.l1_fee.tx_l1_fee(tx.tx_data_gas_cost, 0).0 } }; diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index 4ad41c1deb..bc9c3ed4fa 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -442,6 +442,7 @@ mod tests { l1_fee, TxL1Fee::default(), tx_data_gas_cost.as_u64(), + 0, // TODO: check if need update here )?; self.tx_data_gas_cost.assign( region, diff --git a/zkevm-circuits/src/witness/l1_msg.rs b/zkevm-circuits/src/witness/l1_msg.rs index 70b5d2e37a..5759a2e112 100644 --- a/zkevm-circuits/src/witness/l1_msg.rs +++ b/zkevm-circuits/src/witness/l1_msg.rs @@ -93,7 +93,7 @@ mod tests { for (tx, (rlp_expected, l1fee_expected)) in txs.into_iter().zip(expected) { let rlp = tx.rlp().to_vec(); assert_eq!(rlp.len(), rlp_expected); - assert_eq!(l1fee.tx_l1_fee(tx_data_gas_cost(&rlp)).0, l1fee_expected) + assert_eq!(l1fee.tx_l1_fee(tx_data_gas_cost(&rlp), 0).0, l1fee_expected) } } } From 63cf7a0dd400bb1f5c6f49f0b009dfa4a46efd2a Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 14:41:14 +0800 Subject: [PATCH 17/33] fix l1fee_curie feature build err --- bus-mapping/src/circuit_input_builder/transaction.rs | 6 +++--- zkevm-circuits/src/evm_circuit/execution/begin_tx.rs | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index 5b1c6eb3ba..080e9c8b42 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -447,7 +447,7 @@ impl Transaction { { // TODO: calculate tx rlp signed length let tx_signed_length = self.rlp_signed_bytes.len(); - self.l1_fee.tx_l1_fee(0, tx_signed_length).0 + self.l1_fee.tx_l1_fee(0, tx_signed_length as u64).0 } } } @@ -520,8 +520,8 @@ impl TxL1Fee { #[cfg(feature = "l1_fee_curie")] { // "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee", - let tx_l1_fee = self.commit_scalar * self.l1_blob_basefee - + tx_rlp_signed_len * self.l1_blob_basefee; + let tx_l1_fee = self.commit_scalar as u128 * self.l1_blob_basefee as u128 + + tx_rlp_signed_len as u128 * self.l1_blob_basefee as u128; ( (tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64, (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, diff --git a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs index cdcfa21a65..1f1016b6fd 100644 --- a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs @@ -1211,7 +1211,10 @@ impl ExecutionGadget for BeginTxGadget { #[cfg(feature = "l1_fee_curie")] { ( - tx.l1_fee.tx_l1_fee(0, tx.rlp_signed.len()).0.into(), + tx.l1_fee + .tx_l1_fee(0, tx.rlp_signed.len().try_into().unwrap()) + .0 + .into(), tx.gas_price * tx.gas, ) } From fc5acf9d8e689cb6b6d7a7cf0801ef54c15e70c7 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 14:52:28 +0800 Subject: [PATCH 18/33] add debug log --- zkevm-circuits/src/evm_circuit/util/common_gadget.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget.rs index 0a85210336..9d849207c2 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget.rs @@ -841,6 +841,12 @@ impl TransferToGadget { } else { (0.into(), 0.into()) }; + + #[cfg(feature = "l1_fee_curie")] + println!("l1_fee_curie enabled"); + #[cfg(not(feature = "l1_fee_curie"))] + println!("l1_fee_curie not enabled"); + debug_assert_eq!(receiver_balance, prev_receiver_balance + value); self.receiver.assign( region, From b4dc8c9ab1fc13ce31432bf582fea081be808e1b Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 15:37:27 +0800 Subject: [PATCH 19/33] change gen_tx_l1_fee_ops in buss --- bus-mapping/src/evm/opcodes/begin_end_tx.rs | 59 ++++++++++++++++++- .../src/evm_circuit/util/common_gadget.rs | 2 +- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/bus-mapping/src/evm/opcodes/begin_end_tx.rs b/bus-mapping/src/evm/opcodes/begin_end_tx.rs index dca661fdcd..65c9b0d096 100644 --- a/bus-mapping/src/evm/opcodes/begin_end_tx.rs +++ b/bus-mapping/src/evm/opcodes/begin_end_tx.rs @@ -95,7 +95,7 @@ pub fn gen_begin_tx_steps(state: &mut CircuitInputStateRef) -> Result TransferToGadget { println!("l1_fee_curie enabled"); #[cfg(not(feature = "l1_fee_curie"))] println!("l1_fee_curie not enabled"); - + debug_assert_eq!(receiver_balance, prev_receiver_balance + value); self.receiver.assign( region, From 2dceeba4c2b5ad8397b0185bbe99a0170dc779a4 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 16:13:40 +0800 Subject: [PATCH 20/33] add blob_scalar --- bus-mapping/src/circuit_input_builder/transaction.rs | 2 +- bus-mapping/src/evm/opcodes/begin_end_tx.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index 080e9c8b42..453d8baa74 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -521,7 +521,7 @@ impl TxL1Fee { { // "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee", let tx_l1_fee = self.commit_scalar as u128 * self.l1_blob_basefee as u128 - + tx_rlp_signed_len as u128 * self.l1_blob_basefee as u128; + + self.blob_scalar as u128 * tx_rlp_signed_len as u128 * self.l1_blob_basefee as u128; ( (tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64, (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, diff --git a/bus-mapping/src/evm/opcodes/begin_end_tx.rs b/bus-mapping/src/evm/opcodes/begin_end_tx.rs index 65c9b0d096..27bf3e937a 100644 --- a/bus-mapping/src/evm/opcodes/begin_end_tx.rs +++ b/bus-mapping/src/evm/opcodes/begin_end_tx.rs @@ -749,7 +749,7 @@ fn gen_tx_l1_fee_ops( let base_fee_committed = Word::from(state.tx.l1_fee_committed.base_fee); let fee_overhead_committed = Word::from(state.tx.l1_fee_committed.fee_overhead); let fee_scalar_committed = Word::from(state.tx.l1_fee_committed.fee_scalar); - + #[cfg(feature = "l1_fee_curie")] let l1_blob_basefee_committed = Word::from(state.tx.l1_fee_committed.l1_blob_basefee); #[cfg(feature = "l1_fee_curie")] From 38c89788213d15654332b593f662d952e3e981ec Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 17:14:11 +0800 Subject: [PATCH 21/33] correct l1BaseFee --- bus-mapping/src/circuit_input_builder/transaction.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index 453d8baa74..c0a091b4db 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -516,11 +516,11 @@ impl TxL1Fee { (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, ) } - + #[cfg(feature = "l1_fee_curie")] { // "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee", - let tx_l1_fee = self.commit_scalar as u128 * self.l1_blob_basefee as u128 + let tx_l1_fee = self.commit_scalar as u128 * self.base_fee as u128 + self.blob_scalar as u128 * tx_rlp_signed_len as u128 * self.l1_blob_basefee as u128; ( (tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64, From 0941e1e0c7c6cda01b8a78787a103d4ac1e34332 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 17:46:18 +0800 Subject: [PATCH 22/33] begin_tx adjust rw offset when curie --- bus-mapping/src/circuit_input_builder/transaction.rs | 6 ++++-- zkevm-circuits/src/evm_circuit/execution/begin_tx.rs | 12 +++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index c0a091b4db..faf795af3c 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -516,12 +516,14 @@ impl TxL1Fee { (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, ) } - + #[cfg(feature = "l1_fee_curie")] { // "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee", let tx_l1_fee = self.commit_scalar as u128 * self.base_fee as u128 - + self.blob_scalar as u128 * tx_rlp_signed_len as u128 * self.l1_blob_basefee as u128; + + self.blob_scalar as u128 + * tx_rlp_signed_len as u128 + * self.l1_blob_basefee as u128; ( (tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64, (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, diff --git a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs index 1f1016b6fd..35e3626535 100644 --- a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs @@ -917,10 +917,16 @@ impl ExecutionGadget for BeginTxGadget { rws.offset_add(TxAccessListGadget::::rw_delta_value(tx) as usize); let rw = rws.next(); - debug_assert_eq!(rw.tag(), RwTableTag::CallContext); - debug_assert_eq!(rw.field_tag(), Some(CallContextFieldTag::L1Fee as u64)); - rws.offset_add(3); + #[cfg(not(feature = "l1_fee_curie"))] + { + debug_assert_eq!(rw.tag(), RwTableTag::CallContext); + debug_assert_eq!(rw.field_tag(), Some(CallContextFieldTag::L1Fee as u64)); + rws.offset_add(3); + } + + #[cfg(feature = "l1_fee_curie")] + rws.offset_add(6); let rw = rws.next(); debug_assert_eq!(rw.tag(), RwTableTag::Account); From da53bc21f5f9ad5ede7e23e4607138656157962f Mon Sep 17 00:00:00 2001 From: Zhang Zhuo Date: Wed, 29 May 2024 17:54:21 +0800 Subject: [PATCH 23/33] upgrade l2geth (with latest l1 fee) --- geth-utils/l2geth/go.mod | 2 +- geth-utils/l2geth/go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/geth-utils/l2geth/go.mod b/geth-utils/l2geth/go.mod index b479ff7e0c..f3c578d529 100644 --- a/geth-utils/l2geth/go.mod +++ b/geth-utils/l2geth/go.mod @@ -4,7 +4,7 @@ go 1.21 require ( github.com/imdario/mergo v0.3.16 - github.com/scroll-tech/go-ethereum v1.10.14-0.20240524071237-9b4a779104ac + github.com/scroll-tech/go-ethereum v1.10.14-0.20240528204611-34162effbcc1 ) require ( diff --git a/geth-utils/l2geth/go.sum b/geth-utils/l2geth/go.sum index 708f5d810b..d7128da6ed 100644 --- a/geth-utils/l2geth/go.sum +++ b/geth-utils/l2geth/go.sum @@ -170,6 +170,8 @@ github.com/scroll-tech/go-ethereum v1.10.14-0.20240428082752-c5836a42d3b9 h1:j+r github.com/scroll-tech/go-ethereum v1.10.14-0.20240428082752-c5836a42d3b9/go.mod h1:i4VBgWoaW/y0D8MmQb7hSOulyw1dKhuiSFAbznwivCA= github.com/scroll-tech/go-ethereum v1.10.14-0.20240524071237-9b4a779104ac h1:A7tBJeDFXVjL4URIR89Ir8amS345Kgnh//+FA2v3lEw= github.com/scroll-tech/go-ethereum v1.10.14-0.20240524071237-9b4a779104ac/go.mod h1:GLhY3RmqBezwgPCqrPM/dTjrweDzVSAQ+Ggldk4dCTM= +github.com/scroll-tech/go-ethereum v1.10.14-0.20240528204611-34162effbcc1 h1:U7c23zVBjt4H01scnaglcPth0eXL9nPJCMA7RfZPXAQ= +github.com/scroll-tech/go-ethereum v1.10.14-0.20240528204611-34162effbcc1/go.mod h1:GLhY3RmqBezwgPCqrPM/dTjrweDzVSAQ+Ggldk4dCTM= github.com/scroll-tech/zktrie v0.7.1 h1:NrmZNjuBzsbrKePqdHDG+t2cXnimbtezPAFS0+L9ElE= github.com/scroll-tech/zktrie v0.7.1/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk= github.com/scroll-tech/zktrie v0.8.2 h1:UMuIfA+jdgWMLmTgTL64Emo+zzMOdcnH0+eYdDcshxQ= From 5af852d9d2ddfa724063da89b2b3f7205110b8de Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 20:08:31 +0800 Subject: [PATCH 24/33] update tx_signed_length --- .../src/evm_circuit/execution/begin_tx.rs | 17 ++++++++++++++--- .../evm_circuit/util/common_gadget/tx_l1_fee.rs | 10 +++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs index 35e3626535..393b92b3df 100644 --- a/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs +++ b/zkevm-circuits/src/evm_circuit/execution/begin_tx.rs @@ -61,6 +61,9 @@ pub(crate) struct BeginTxGadget { tx_call_data_gas_cost: Cell, // The gas cost for rlp-encoded bytes of unsigned tx tx_data_gas_cost: Cell, + #[cfg(feature = "l1_fee_curie")] + // rlp signed tx bytes' length + tx_signed_length: Cell, reversion_info: ReversionInfo, intrinsic_gas_cost: Cell, sufficient_gas_left: RangeCheckGadget, @@ -132,8 +135,7 @@ impl ExecutionGadget for BeginTxGadget { .map(|field_tag| cb.tx_context(tx_id.expr(), field_tag, None)); #[cfg(feature = "l1_fee_curie")] - let tx_signed_hash_length = - cb.tx_context(tx_id.expr(), TxContextFieldTag::TxHashLength, None); + let tx_signed_length = cb.tx_context(tx_id.expr(), TxContextFieldTag::TxHashLength, None); let tx_access_list = TxAccessListGadget::construct(cb, tx_id.expr(), tx_type.expr()); let is_call_data_empty = IsZeroGadget::construct(cb, tx_call_data_length.expr()); @@ -149,7 +151,7 @@ impl ExecutionGadget for BeginTxGadget { tx_id.expr(), tx_data_gas_cost.expr(), #[cfg(feature = "l1_fee_curie")] - tx_signed_hash_length.expr(), + tx_signed_length.expr(), ) }); cb.condition(tx_l1_msg.is_l1_msg(), |cb| { @@ -810,6 +812,8 @@ impl ExecutionGadget for BeginTxGadget { is_call_data_empty, tx_call_data_word_length, tx_call_data_gas_cost, + #[cfg(feature = "l1_fee_curie")] + tx_signed_length, tx_data_gas_cost, reversion_info, intrinsic_gas_cost, @@ -1092,6 +1096,13 @@ impl ExecutionGadget for BeginTxGadget { )?; self.tx_data_gas_cost .assign(region, offset, Value::known(F::from(tx.tx_data_gas_cost)))?; + #[cfg(feature = "l1_fee_curie")] + self.tx_signed_length.assign( + region, + offset, + Value::known(F::from(tx.rlp_signed.len() as u64)), + )?; + self.reversion_info.assign( region, offset, diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index bc9c3ed4fa..cf4c5d11f0 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -62,10 +62,10 @@ impl TxL1FeeGadget { cb: &mut EVMConstraintBuilder, tx_id: Expression, tx_data_gas_cost: Expression, - #[cfg(feature = "l1_fee_curie")] tx_signed_hash_length: Expression, + #[cfg(feature = "l1_fee_curie")] tx_signed_length: Expression, ) -> Self { #[cfg(feature = "l1_fee_curie")] - let this = Self::raw_construct(cb, tx_data_gas_cost, tx_signed_hash_length); + let this = Self::raw_construct(cb, tx_data_gas_cost, tx_signed_length); #[cfg(not(feature = "l1_fee_curie"))] let this = Self::raw_construct(cb, tx_data_gas_cost); @@ -257,7 +257,7 @@ impl TxL1FeeGadget { fn raw_construct( cb: &mut EVMConstraintBuilder, tx_data_gas_cost: Expression, - #[cfg(feature = "l1_fee_curie")] tx_signed_hash_length: Expression, + #[cfg(feature = "l1_fee_curie")] tx_signed_length: Expression, ) -> Self { let tx_l1_fee_word = cb.query_word_rlc(); let remainder_word = cb.query_word_rlc(); @@ -301,7 +301,7 @@ impl TxL1FeeGadget { #[cfg(feature = "l1_fee_curie")] cb.require_equal( "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee == tx_l1_fee * 10e9 + remainder", - commit_scalar * base_fee + blob_scalar * tx_signed_hash_length * l1_blob_basefee, + commit_scalar * base_fee + blob_scalar * tx_signed_length * l1_blob_basefee, // * tx_l1_gas, tx_l1_fee * TX_L1_FEE_PRECISION.expr() + remainder, ); @@ -407,7 +407,7 @@ mod tests { let tx_data_gas_cost = cb.query_cell(); let expected_tx_l1_fee = cb.query_cell(); - // for non "l1_fee_curie" feature, tx_signed_hash_length is not used, can + // for non "l1_fee_curie" feature, tx_signed_length is not used, can // set to zero let gadget = TxL1FeeGadget::::raw_construct(cb, tx_data_gas_cost.expr()); From d6e9b5f200647c50535653594fbf9ecd9c7c8161 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 20:51:36 +0800 Subject: [PATCH 25/33] update committed assign --- .../src/evm_circuit/util/common_gadget/tx_l1_fee.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index cf4c5d11f0..4eb86b5a40 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -210,19 +210,19 @@ impl TxL1FeeGadget { // curie fields #[cfg(feature = "l1_fee_curie")] - self.l1_blob_basefee_word.assign( + self.l1_blob_basefee_committed.assign( region, offset, Some(l1_fee_committed.l1_blob_basefee.to_le_bytes()), )?; #[cfg(feature = "l1_fee_curie")] - self.commit_scalar_word.assign( + self.commit_scalar_committed.assign( region, offset, Some(l1_fee_committed.commit_scalar.to_le_bytes()), )?; #[cfg(feature = "l1_fee_curie")] - self.blob_scalar_word.assign( + self.blob_scalar_committed.assign( region, offset, Some(l1_fee_committed.blob_scalar.to_le_bytes()), From de28405d65a959e7f7f68fbbba4cc85459815192 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 21:03:59 +0800 Subject: [PATCH 26/33] fix build --- .../src/evm_circuit/util/common_gadget/tx_l1_fee.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index 4eb86b5a40..98c5de7f32 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -213,19 +213,19 @@ impl TxL1FeeGadget { self.l1_blob_basefee_committed.assign( region, offset, - Some(l1_fee_committed.l1_blob_basefee.to_le_bytes()), + region.word_rlc(l1_fee_committed.l1_blob_basefee.into()), )?; #[cfg(feature = "l1_fee_curie")] self.commit_scalar_committed.assign( region, offset, - Some(l1_fee_committed.commit_scalar.to_le_bytes()), + region.word_rlc(l1_fee_committed.commit_scalar.into()), )?; #[cfg(feature = "l1_fee_curie")] self.blob_scalar_committed.assign( region, offset, - Some(l1_fee_committed.blob_scalar.to_le_bytes()), + region.word_rlc(l1_fee_committed.blob_scalar.into()), )?; Ok(()) From 705622c812728e6d5c0784c0d1ec8a92a71c352d Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 21:40:13 +0800 Subject: [PATCH 27/33] debug log --- bus-mapping/src/circuit_input_builder/transaction.rs | 12 ++++++++++++ .../src/evm_circuit/util/common_gadget/tx_l1_fee.rs | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index faf795af3c..e853069b59 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -524,6 +524,18 @@ impl TxL1Fee { + self.blob_scalar as u128 * tx_rlp_signed_len as u128 * self.l1_blob_basefee as u128; + log::debug!( + "tx_l1_fee {} commit_scalar {} base_fee {} blob_scalar {} + tx_rlp_signed_len {} l1_blob_basefee {} tx_quient {},reminder {}", + tx_l1_fee, + self.commit_scalar, + self.base_fee, + self.blob_scalar, + tx_rlp_signed_len, + self.l1_blob_basefee, + tx_l1_fee / TX_L1_FEE_PRECISION as u128, + tx_l1_fee % TX_L1_FEE_PRECISION as u128 + ); ( (tx_l1_fee / TX_L1_FEE_PRECISION as u128) as u64, (tx_l1_fee % TX_L1_FEE_PRECISION as u128) as u64, diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index 98c5de7f32..f6ea1b23ed 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -161,6 +161,13 @@ impl TxL1FeeGadget { tx_data_gas_cost: u64, tx_signed_length: u64, ) -> Result<(), Error> { + #[cfg(feature = "l1_fee_curie")] + log::debug!( + "assign: tx_l1_fee {:?} l1_fee_committed {:?} tx_signed_length {}", + l1_fee, + l1_fee_committed, + tx_signed_length + ); let (tx_l1_fee, remainder) = if cfg!(feature = "l1_fee_curie") { l1_fee.tx_l1_fee(tx_data_gas_cost, 0) } else { From 7f115d68b2ae7df0eb2708ece0d6795ae7a70a30 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 21:58:34 +0800 Subject: [PATCH 28/33] minor fix --- .../src/evm_circuit/util/common_gadget/tx_l1_fee.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index f6ea1b23ed..c2aaeecbe5 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -169,9 +169,9 @@ impl TxL1FeeGadget { tx_signed_length ); let (tx_l1_fee, remainder) = if cfg!(feature = "l1_fee_curie") { - l1_fee.tx_l1_fee(tx_data_gas_cost, 0) - } else { l1_fee.tx_l1_fee(0, tx_signed_length) + } else { + l1_fee.tx_l1_fee(tx_data_gas_cost, 0) }; self.tx_l1_fee_word From e3cae39ebdeb01cd56c915194d64a674490dd2f3 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Wed, 29 May 2024 22:22:28 +0800 Subject: [PATCH 29/33] some cleanup --- bus-mapping/src/l2_predeployed.rs | 2 +- zkevm-circuits/src/evm_circuit/util/common_gadget.rs | 5 ----- .../src/evm_circuit/util/common_gadget/tx_l1_fee.rs | 4 ++-- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/bus-mapping/src/l2_predeployed.rs b/bus-mapping/src/l2_predeployed.rs index 448c0ce792..96a50f021d 100644 --- a/bus-mapping/src/l2_predeployed.rs +++ b/bus-mapping/src/l2_predeployed.rs @@ -40,7 +40,7 @@ pub mod l1_gas_price_oracle { pub static L1_BLOB_BASEFEE_SLOT: LazyLock = LazyLock::new(|| U256::from(5)); /// L1 commitScalar slot in L1GasPriceOracle after Curie fork pub static COMMIT_SCALAR_SLOT: LazyLock = LazyLock::new(|| U256::from(6)); - /// L1 commitScalar slot in L1GasPriceOracle after Curie forks + /// L1 blob_scalar slot in L1GasPriceOracle after Curie fork pub static BLOB_SCALAR_SLOT: LazyLock = LazyLock::new(|| U256::from(7)); pub static IS_CURIE_SLOT: LazyLock = LazyLock::new(|| U256::from(8)); pub static INITIAL_COMMIT_SCALAR: LazyLock = diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget.rs index 19ebf2fd7e..83645fd445 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget.rs @@ -842,11 +842,6 @@ impl TransferToGadget { (0.into(), 0.into()) }; - #[cfg(feature = "l1_fee_curie")] - println!("l1_fee_curie enabled"); - #[cfg(not(feature = "l1_fee_curie"))] - println!("l1_fee_curie not enabled"); - debug_assert_eq!(receiver_balance, prev_receiver_balance + value); self.receiver.assign( region, diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index c2aaeecbe5..584f78ac99 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -82,7 +82,7 @@ impl TxL1FeeGadget { .map(|slot| cb.word_rlc(slot.to_le_bytes().map(|b| b.expr()))); #[cfg(feature = "l1_fee_curie")] - let [l1_blob_baseFee, commit_scalar, blob_scalar] = [ + let [l1_blob_basefee, commit_scalar, blob_scalar] = [ &l1_gas_price_oracle::L1_BLOB_BASEFEE_SLOT, &l1_gas_price_oracle::COMMIT_SCALAR_SLOT, &l1_gas_price_oracle::BLOB_SCALAR_SLOT, @@ -123,7 +123,7 @@ impl TxL1FeeGadget { // Read l1blob_baseFee_committed cb.account_storage_read( l1_fee_address.expr(), - l1_blob_baseFee, + l1_blob_basefee, this.l1_blob_basefee_word.expr(), tx_id.clone(), this.l1_blob_basefee_committed.expr(), From 43e1200b332d255751caa0f8ec1cc55d29612277 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Thu, 30 May 2024 09:57:14 +0800 Subject: [PATCH 30/33] update link --- bus-mapping/src/circuit_input_builder/transaction.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index e853069b59..e2095c96f7 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -506,7 +506,6 @@ impl TxL1Fee { /// for non curie upgrade case, tx_rlp_signed_len is not used, set to zero pub fn tx_l1_fee(&self, tx_data_gas_cost: u64, tx_rlp_signed_len: u64) -> (u64, u64) { // - // check if the calculation changes for curie upgrade #[cfg(not(feature = "l1_fee_curie"))] { let tx_l1_gas = tx_data_gas_cost + self.fee_overhead + TX_L1_COMMIT_EXTRA_COST; @@ -519,6 +518,8 @@ impl TxL1Fee { #[cfg(feature = "l1_fee_curie")] { + // for curie upgrade: + // new formula: https://github.com/scroll-tech/go-ethereum/blob/develop/rollup/fees/rollup_fee.go#L165 // "commitScalar * l1BaseFee + blobScalar * _data.length * l1BlobBaseFee", let tx_l1_fee = self.commit_scalar as u128 * self.base_fee as u128 + self.blob_scalar as u128 From 5e0d566f04f3031371487d31f62b54c9a6da383f Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Thu, 30 May 2024 10:52:13 +0800 Subject: [PATCH 31/33] try eth_tx.rlp --- bus-mapping/src/circuit_input_builder/transaction.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index e2095c96f7..f58c820f98 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -204,7 +204,7 @@ pub struct Transaction { pub rlp_bytes: Vec, /// RLP bytes for signing pub rlp_unsigned_bytes: Vec, - /// RLP bytes for signing + /// RLP bytes for signed tx pub rlp_signed_bytes: Vec, /// Current values of L1 fee pub l1_fee: TxL1Fee, @@ -366,7 +366,8 @@ impl Transaction { tx_type, rlp_bytes: eth_tx.rlp().to_vec(), rlp_unsigned_bytes: get_rlp_unsigned(eth_tx), - rlp_signed_bytes: get_rlp_signed(eth_tx), + //rlp_signed_bytes: get_rlp_signed(eth_tx), + rlp_signed_bytes: eth_tx.rlp().to_vec(), nonce: eth_tx.nonce.as_u64(), gas: eth_tx.gas.as_u64(), gas_price: eth_tx.gas_price.unwrap_or_default(), From ec016bdd9e8d601c0f657994903f4bad18865026 Mon Sep 17 00:00:00 2001 From: Dream Wu Date: Thu, 30 May 2024 14:22:28 +0800 Subject: [PATCH 32/33] remove get_rlp_signed --- .../src/circuit_input_builder/transaction.rs | 2 +- eth-types/src/geth_types.rs | 42 ------------------- 2 files changed, 1 insertion(+), 43 deletions(-) diff --git a/bus-mapping/src/circuit_input_builder/transaction.rs b/bus-mapping/src/circuit_input_builder/transaction.rs index f58c820f98..cf049d5166 100644 --- a/bus-mapping/src/circuit_input_builder/transaction.rs +++ b/bus-mapping/src/circuit_input_builder/transaction.rs @@ -5,7 +5,7 @@ use crate::{l2_predeployed::l1_gas_price_oracle, Error}; use eth_types::{ evm_types::{gas_utils::tx_data_gas_cost, OpcodeId}, geth_types, - geth_types::{get_rlp_signed, get_rlp_unsigned, TxType}, + geth_types::{get_rlp_unsigned, TxType}, state_db::{CodeDB, StateDB}, AccessList, Address, GethExecTrace, Signature, Word, H256, }; diff --git a/eth-types/src/geth_types.rs b/eth-types/src/geth_types.rs index d785934c32..35a87b165f 100644 --- a/eth-types/src/geth_types.rs +++ b/eth-types/src/geth_types.rs @@ -158,48 +158,6 @@ pub fn get_rlp_unsigned(tx: &crate::Transaction) -> Vec { } } -/// Get the RLP signed bytes -/// NOTE: currently only used by curie upgrade calculating l1fee -pub fn get_rlp_signed(tx: &crate::Transaction) -> Vec { - let sig_v = tx.v; // U64 type - let signature = Signature { - r: tx.r, - s: tx.s, - v: tx.v.as_u64(), - }; - - match TxType::get_tx_type(tx) { - TxType::Eip155 => { - let mut tx: TransactionRequest = tx.into(); - tx.chain_id = Some(tx.chain_id.unwrap_or_else(|| { - let recv_v = TxType::Eip155.get_recovery_id(sig_v.as_u64()) as u64; - (sig_v - recv_v - 35) / 2 - })); - - tx.rlp_signed(&signature).to_vec() - } - TxType::PreEip155 => { - let tx: TransactionRequest = tx.into(); - tx.rlp_signed(&signature).to_vec() - } - TxType::Eip1559 => { - let tx: Eip1559TransactionRequest = tx.into(); - let typed_tx: TypedTransaction = tx.into(); - typed_tx.rlp_signed(&signature).to_vec() - } - TxType::Eip2930 => { - let tx: Eip2930TransactionRequest = tx.into(); - let typed_tx: TypedTransaction = tx.into(); - //typed_tx.rlp().to_vec() - typed_tx.rlp_signed(&signature).to_vec() - } - TxType::L1Msg => { - // L1 msg does not have signature - vec![] - } - } -} - /// Definition of all of the data related to an account. #[serde_as] #[derive(PartialEq, Eq, Debug, Default, Clone, Serialize)] From deb6d5c364263cd2ac45a3af8a931466a1bf7d19 Mon Sep 17 00:00:00 2001 From: Ho Date: Thu, 30 May 2024 15:26:47 +0900 Subject: [PATCH 33/33] [FIX #1307] (#1308) * fix constraint for remainder in l1fee Signed-off-by: noelwei * fmt Signed-off-by: noelwei --------- Signed-off-by: noelwei Co-authored-by: DreamWuGit --- .../util/common_gadget/tx_l1_fee.rs | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs index 584f78ac99..020b549471 100644 --- a/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs +++ b/zkevm-circuits/src/evm_circuit/util/common_gadget/tx_l1_fee.rs @@ -4,7 +4,9 @@ use crate::{ param::N_BYTES_U64, util::{ constraint_builder::{ConstrainBuilderCommon, EVMConstraintBuilder}, - from_bytes, U64Word, Word, + from_bytes, + math_gadget::LtGadget, + U64Word, Word, }, }, util::{Expr, Field}, @@ -25,6 +27,8 @@ pub(crate) struct TxL1FeeGadget { tx_l1_fee_word: Word, /// Remainder when calculating L1 fee remainder_word: U64Word, + /// Remainder must in [0, TX_L1_FEE_PRECISION) + remainder_range: LtGadget, /// Current value of L1 base fee base_fee_word: U64Word, /// Current value of L1 fee overhead @@ -184,6 +188,12 @@ impl TxL1FeeGadget { .assign(region, offset, Some(l1_fee.fee_overhead.to_le_bytes()))?; self.fee_scalar_word .assign(region, offset, Some(l1_fee.fee_scalar.to_le_bytes()))?; + self.remainder_range.assign( + region, + offset, + F::from(remainder), + F::from(TX_L1_FEE_PRECISION), + )?; // curie fields #[cfg(feature = "l1_fee_curie")] @@ -289,6 +299,13 @@ impl TxL1FeeGadget { ] .map(|word| from_bytes::expr(&word.cells[..N_BYTES_U64])); + let remainder_range = LtGadget::construct(cb, remainder.expr(), TX_L1_FEE_PRECISION.expr()); + cb.require_equal( + "remainder must less than l1 fee precision", + 1.expr(), + remainder_range.expr(), + ); + #[cfg(feature = "l1_fee_curie")] let [l1_blob_basefee, commit_scalar, blob_scalar] = [ &l1_blob_basefee_word, @@ -335,6 +352,7 @@ impl TxL1FeeGadget { Self { tx_l1_fee_word, remainder_word, + remainder_range, base_fee_word, fee_overhead_word, fee_scalar_word,