Assessment reports>Scroll zkEVM>Discussion>Asserting assumptions about equal gas costs

Asserting assumptions about equal gas costs

The ErrorOOGMemoryCopyGadget handles out-of-gas errors for different instructions that copy to memory, namely CALLDATACOPY, CODECOPY, EXTCODECOPY, RETURNDATACOPY, and MCOPY. For the case of instructions that are not EXTCODECOPY, the constant gas cost is obtained as in the following snippet from ErrorOOGMemoryCopyGadget::configure in zkevm-circuits/src/evm_circuit/execution/error_oog_memory_copy.rs:

// Constant gas cost is same for CALLDATACOPY, CODECOPY and RETURNDATACOPY.
OpcodeId::CALLDATACOPY.constant_gas_cost().expr(),

As the comment points out, a single constant can be used here as the relevant instructions (MCOPY should be added to the list in the comment) all have constant gas costs of 3. To ensure that the code here is changed should this change in the future, we recommend adding asserts to check this (e.g., assert_eq!(OpcodeId::CALLDATACOPY.constant_gas_cost(), OpcodeId::MCOPY.constant_gas_cost())). Then, should there at some point be an update where the constant gas costs become different, the assert will fail on tests, ensuring that it will not be forgotten to add the necessary logic distinguishing the opcodes here, thereby preventing an issue similar to Finding ref.

Zellic © 2024Back to top ↑