Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Spec for ErrorInvalidOpcode state#362

Merged
lispc merged 9 commits into
privacy-ethereum:masterfrom
scroll-tech:execution/error-invalid-opcode
Feb 6, 2023
Merged

Spec for ErrorInvalidOpcode state#362
lispc merged 9 commits into
privacy-ethereum:masterfrom
scroll-tech:execution/error-invalid-opcode

Conversation

@silathdiir
Copy link
Copy Markdown
Contributor

@silathdiir silathdiir commented Jan 19, 2023

Close #350

Circuit PR privacy-ethereum/zkevm-circuits#1089

Reference https://www.evm.codes and verifies invalid opcode if any below condition is met:

  • opcode > 0x20 && opcode < 0x30
  • opcode > 0x48 && opcode < 0x50
  • opcode > 0xA4 && opcode < 0xF0
  • one of [0x0C, 0x0D, 0x0E, 0x0F, 0x1E, 0x1F, 0x5C, 0x5D, 0x5E, 0x5F, 0xF6, 0xF7, 0xF8, 0xF9, 0xFB, 0xFC, 0xFE]

@silathdiir silathdiir changed the title Implement ErrorInvalidOpcode state Spec for ErrorInvalidOpcode state Jan 19, 2023
opcode = instruction.opcode_lookup(True)
instruction.responsible_opcode_lookup(opcode)

op_gt_20, _ = instruction.compare(FQ(0x20), opcode, 1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if is not cheaper just to do a lookup that 5 compares + one constrain per SEPARATE_INVALID_OPCODES

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some investigation, I found that FixedTableTag.InvalidOpcode was deleted in PR #110 and @han0110 commented with #110 (comment).

I am sorry that I could not confirm if this 5 compares + one set constrain is correct. Or I should use fourth column auxiliary of ResponsibleOpcode to specify it's an invalid opcode. WDYT?

Also @han0110 may have some suggestions. Thanks.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if is not cheaper just to do a lookup that 5 compares + one constrain per SEPARATE_INVALID_OPCODES

if I remember correct, one lookup generate 4 gates 😄

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I have missed this, so I think the responsible_opcode_lookup already does the opcode check, since only the invalid opcode could pass the lookup, or perhaps I'm missing something?

The invalid opcode responsible opcodes are generated here:

https://github.com/privacy-scaling-explorations/zkevm-specs/blob/13a5cf983deea12dcf2972242d3d876a9a0337dd/src/zkevm_specs/evm/execution_state.py#L354-L355

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I see. I am sorry for my previous misunderstanding. It seems that responsible_opcode_lookup is enough, it has already lookup for the invalid opcode with ExecutionState.ErrorInvalidOpcode.

And it seems that I should also map the invalid_opcodes function to ExecutionState::ErrorInvalidOpcode in responsible_opcodes of zkevm-circuit. So I could do the same lookup in circuit. Thanks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah you are right! That's currently missed in circuit side.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Will update the code.

Copy link
Copy Markdown
Contributor Author

@silathdiir silathdiir Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed to only do fixed lookup for ResponsibleOpcode in 721a1ca.
And also updated the circuit PR privacy-ethereum/zkevm-circuits#1089

Comment on lines +52 to +74
is_success = instruction.call_context_lookup(CallContextFieldTag.IsSuccess)
instruction.constrain_equal(is_success, FQ(0))

# Go to EndTx only when is_root.
is_to_end_tx = instruction.is_equal(instruction.next.execution_state, ExecutionState.EndTx)
instruction.constrain_equal(FQ(instruction.curr.is_root), is_to_end_tx)

# When it's a root call.
if instruction.curr.is_root:
# Do step state transition.
instruction.constrain_step_state_transition(
rw_counter=Transition.delta(1 + instruction.curr.reversible_write_counter),
call_id=Transition.same(),
)
else:
# When it is internal call, need to restore caller's state as finishing this call.
# Restore caller state to next StepState.
instruction.step_state_transition_to_restored_context(
rw_counter_delta=1 + instruction.curr.reversible_write_counter.n,
return_data_offset=FQ(0),
return_data_length=FQ(0),
gas_left=instruction.curr.gas_left,
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I see. I found a similar circuit issue for common error gadget as privacy-ethereum/zkevm-circuits#1059

Will discuss with @DreamWuGit if we could update for both spec and circuit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed to add a constrain_error_state function to Instruction, and update code of related error states in 9b2a262.

@silathdiir
Copy link
Copy Markdown
Contributor Author

Hi @adria0, @DreamWuGit, @han0110 and @icemelon, I updated the code according to previous review, please help review again when you have time. Thanks.

@silathdiir silathdiir requested review from adria0 and han0110 and removed request for adria0, han0110 and icemelon February 3, 2023 09:25
Copy link
Copy Markdown
Contributor

@han0110 han0110 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Also nice refactor! Left some doc and refactor suggestions.

Comment thread specs/error_state/ErrorInvalidOpcode.md Outdated
Comment thread src/zkevm_specs/evm/instruction.py Outdated
Comment thread src/zkevm_specs/evm/execution/oog_call.py Outdated
…the hidden logic in `step_state_transition_to_restored_context`.
…ter.n` in OOG call. And updated test cases to set `reversible_write_counter = 0` for root call, and `reversible_write_counter = 2` (parent Call and Stop) for internal call.
@silathdiir silathdiir requested a review from han0110 February 4, 2023 10:26
Copy link
Copy Markdown
Contributor

@han0110 han0110 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All LGTM now!

… and `reversible_write_counter = caller_reversible_write_counter` in STOP step.
@lispc
Copy link
Copy Markdown
Collaborator

lispc commented Feb 6, 2023

file names not consistent?

error_invalid_opcode.py and error_Invalid_jump

Invalid / invalid upper case small case

@silathdiir
Copy link
Copy Markdown
Contributor Author

silathdiir commented Feb 6, 2023

file names not consistent?

error_invalid_opcode.py and error_Invalid_jump

Invalid / invalid upper case small case

Renamed error_Invalid_jump.py to error_invalid_jump.py (also reference in __init__.py) in 6f210bb.

@lispc lispc merged commit ee3885f into privacy-ethereum:master Feb 6, 2023
@lispc lispc deleted the execution/error-invalid-opcode branch February 6, 2023 10:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement ErrorInvalidOpcode state

5 participants