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

Feat/#318 precompile ecrecover #495

Merged
merged 15 commits into from
Oct 27, 2023
Merged

Conversation

KimiWu123
Copy link
Contributor

closed #318

@KimiWu123 KimiWu123 self-assigned this Oct 16, 2023
@KimiWu123 KimiWu123 force-pushed the feat/#318-precompile-ecrecover branch from 14624c1 to 52a8dd1 Compare October 16, 2023 05:44
@KimiWu123 KimiWu123 force-pushed the feat/#318-precompile-ecrecover branch from e0f42f3 to 9c6b839 Compare October 18, 2023 08:22
@KimiWu123 KimiWu123 force-pushed the feat/#318-precompile-ecrecover branch from 4420adf to b59667c Compare October 19, 2023 01:54
@KimiWu123 KimiWu123 force-pushed the feat/#318-precompile-ecrecover branch from b59667c to 9690c3d Compare October 19, 2023 03:24
@KimiWu123 KimiWu123 force-pushed the feat/#318-precompile-ecrecover branch from c8da278 to 1bf4565 Compare October 19, 2023 04:16
@han0110 han0110 self-requested a review October 20, 2023 03:40
@KimiWu123 KimiWu123 force-pushed the feat/#318-precompile-ecrecover branch from aebea3e to e53104a Compare October 23, 2023 02:54
@KimiWu123 KimiWu123 changed the title [WIP] Feat/#318 precompile ecrecover Feat/#318 precompile ecrecover Oct 23, 2023

# TODO: There is another one used in tx_circuit, try to merge into one.
# Reminder: endianness of public key is differ with the one in tx_circuit
class ECDSAVerifyChip:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was copied from tx_circuit and only change the endianness of public key (here and here)

from eth_utils import keccak


class Row:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This circuit comes from SignVerifyChip in tx_circuit and added some columns for sig_table

@KimiWu123 KimiWu123 marked this pull request as ready for review October 23, 2023 06:08
Copy link
Collaborator

@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.

Overall LGTM, the inputs (aux_data) are currently just pure witness, but in scroll it's constraint from the previous step (by the PrecompileGadget), so we'd also need to address this in the future.

src/zkevm_specs/sig_circuit.py Outdated Show resolved Hide resolved
src/zkevm_specs/util/ec.py Show resolved Hide resolved
src/zkevm_specs/util/tables.py Show resolved Hide resolved
src/zkevm_specs/util/tables.py Outdated Show resolved Hide resolved
src/zkevm_specs/sig_circuit.py Outdated Show resolved Hide resolved
is_recovered = FQ(instruction.is_zero(recovered_addr) != FQ(1))

# if the address is recovered, this call should be success either
instruction.constrain_equal(is_success, is_recovered)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems is_success will alwasy be true when gas is sufficient (scroll handles precompile insufficient gas in another execution state) even the input is malformed (ref: https://github.com/ethereum/execution-specs/blob/master/src/ethereum/shanghai/vm/precompiled_contracts/ecrecover.py)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in 3ae72d0

tests/evm/precompiles/test_ecRecover.py Outdated Show resolved Hide resolved
@KimiWu123
Copy link
Contributor Author

Overall LGTM, the inputs (aux_data) are currently just pure witness, but in scroll it's constraint from the previous step (by the PrecompileGadget), so we'd also need to address this in the future.

Have created #498 for it. My plan is to implement verification logic first and then do integration part (the PrecompileGadget you mentioned)

@miha-stopar miha-stopar self-requested a review October 26, 2023 10:07
Copy link
Contributor

@miha-stopar miha-stopar left a comment

Choose a reason for hiding this comment

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

LGTM! For me it's fine if the duplicated code gets removed in the follow up PRs.


### Inputs

The length of inputs is 128 bytes. The first 32 bytes is keccak hash of the message, and following 96 bytes are v, r, s values. v is either 27 or 28.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: "and following" -> "and the following", "v is either" -> "The value v is either"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in 86866f9


### Output

The recovered 20-byte address right aligned to 32 byte. If an address can't be recovered or not enough gas was given, then return 0.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: "then return 0" -> "then the output is 0".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in 86866f9


The Sig Circuit aims at proving the correctness of SigTable. This mainly includes the following type of constraints:
- checking that the signature is obtained correctly. This is done by the ECDSA chip, and the correctness of `v` is checked separately;
- checking that `msg_hash` is obtained correctly from Keccak hash function. This is done by lookup to Keccak table;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Perhaps make "checking" upper case as there are two sentences in the bullet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in 86866f9

is_recovered = FQ(instruction.is_zero(recovered_addr) != FQ(1))

# is_success is always true
# ref: ref: https://github.com/ethereum/execution-specs/blob/master/src/ethereum/shanghai/vm/precompiled_contracts/ecrecover.py
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: "ref:" two times

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in 86866f9

Copy link
Collaborator

@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! Nice work!

@KimiWu123 KimiWu123 merged commit 6a9b04c into master Oct 27, 2023
1 check passed
@KimiWu123 KimiWu123 deleted the feat/#318-precompile-ecrecover branch November 25, 2023 07:07
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.

Precompile: 0x01 ecRecover
3 participants