This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: callop and PrecompileGadget (draft) * feat: precompile oog * feat: remove rlc in callop for PrecompileGadget * test: refactor callop test for precompiles * test: complete testing for precompiles in callop * chore: add comments * feat: complete precompile checks in precompile_gadget * Update src/zkevm_specs/evm_circuit/execution/callop.py Co-authored-by: Chih Cheng Liang <[email protected]> * fix: incorrect caller/callee call context * fix: ecRecover allows input len is not 128 bytes * feat: add copy rlc for precompiles input/output * feat: add input/output rlc data check in ecRecover (PoC) * refactor ecrecover rlc input --------- Co-authored-by: Chih Cheng Liang <[email protected]>
- Loading branch information
1 parent
b871e6b
commit 163f3c0
Showing
10 changed files
with
715 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/zkevm_specs/evm_circuit/execution/precompiles/error_oog_precompile.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from zkevm_specs.evm_circuit.execution.precompiles.ecpairing import BYTES_PER_PAIRING | ||
from zkevm_specs.evm_circuit.instruction import Instruction | ||
from zkevm_specs.evm_circuit.precompile import Precompile | ||
from zkevm_specs.evm_circuit.table import CallContextFieldTag | ||
from zkevm_specs.util import FQ | ||
from zkevm_specs.util.param import N_BYTES_GAS, Bn254PairingPerPointGas, IdentityPerWordGas | ||
|
||
|
||
def error_oog_precompile(instruction: Instruction): | ||
address_word = instruction.call_context_lookup_word(CallContextFieldTag.CalleeAddress) | ||
address = instruction.word_to_address(address_word) | ||
calldata_len = instruction.call_context_lookup(CallContextFieldTag.CallDataLength) | ||
|
||
# the address must be one of precompiles | ||
instruction.constrain_equal(instruction.precompile(address), FQ.one()) | ||
|
||
# TODO: Handle OOG of SHA256, RIPEMD160, BIGMODEXP and BLAKE2F. | ||
### total gas cost | ||
# constant gas cost | ||
precompile = Precompile(address) | ||
gas_cost = precompile.base_gas_cost() | ||
# dynamic gas cost | ||
if precompile == Precompile.BN254PAIRING: | ||
pairs = calldata_len / BYTES_PER_PAIRING | ||
gas_cost += Bn254PairingPerPointGas * pairs | ||
elif precompile == Precompile.DATACOPY: | ||
gas_cost += instruction.memory_copier_gas_cost(calldata_len, FQ(0), IdentityPerWordGas) | ||
|
||
# check gas left is less than total gas required | ||
insufficient_gas, _ = instruction.compare(instruction.curr.gas_left, gas_cost, N_BYTES_GAS) | ||
instruction.constrain_equal(insufficient_gas, FQ(1)) | ||
|
||
instruction.constrain_error_state( | ||
instruction.rw_counter_offset + instruction.curr.reversible_write_counter | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from zkevm_specs.evm_circuit.precompile import Precompile | ||
from ...util import FQ | ||
from ..instruction import Instruction | ||
|
||
|
||
class PrecompileGadget: | ||
address: FQ | ||
|
||
def __init__( | ||
self, | ||
instruction: Instruction, | ||
callee_addr: FQ, | ||
precompile_return_len: FQ, | ||
calldata_len: FQ, | ||
): | ||
# next execution state must be one of precompiles | ||
instruction.constrain_equal(instruction.precompile(callee_addr), FQ.one()) | ||
|
||
### precompiles' specific constraints | ||
precompile = Precompile(callee_addr) | ||
if precompile == Precompile.DATACOPY: | ||
# input length is the same as return data length | ||
instruction.constrain_equal(precompile_return_len, calldata_len) | ||
elif precompile == Precompile.ECRECOVER: | ||
# The input different from 128 is allowed and is then right padded with zeros | ||
# We only ensure hat the return length is either 32 or 0. | ||
is_128 = instruction.is_equal(precompile_return_len, FQ(32)) | ||
is_zero = instruction.is_equal(precompile_return_len, FQ.zero()) | ||
instruction.constrain_equal(is_128 + is_zero, FQ.one()) | ||
elif precompile == Precompile.BN254ADD: | ||
# input length is 128 bytes | ||
instruction.constrain_equal(calldata_len, FQ(128)) | ||
elif precompile == Precompile.BN254SCALARMUL: | ||
# input length is 96 bytes | ||
instruction.constrain_equal(calldata_len, FQ(96)) | ||
elif precompile == Precompile.BN254PAIRING: | ||
# input length is 192 * n bytes | ||
instruction.constrain_equal(FQ(calldata_len.n % 192), FQ.zero()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.