Repository navigation
feat(zkevm): fill public_keys execution witness field - #2825
Conversation
| original_stateless_input_bytes: Bytes, | ||
| execution_witness: ExecutionWitness, | ||
| execution_witness: ExecutionWitness | None = None, | ||
| public_keys: Tuple[Bytes, ...] | None = None, |
There was a problem hiding this comment.
Mainly adding public_keys as a new field in the execution witness that can be modified by tests to check if the guest program is correctly validating its correctness. (The execution_witness change is a simplification to make both potentially modified execution_witness and public_keys feel symmetrical at this method API design).
| return public_keys, payload_transactions | ||
|
|
||
|
|
||
| def verify_stateless_input_public_keys( |
There was a problem hiding this comment.
Whenever a test is filled, we always check the public_keys generated by t8n matches an explicit list of public keys recovered with ecrecover.
This happens automatically for all tests and do not need an explicit signal from the test creator. This might be a bit harshish on the filler to check this always, but it feels way safer since it is an invariant that must always hold.
I filled locally and didn't notice any observable slowdown because of ecrecovers, so looks should be okay. Eventually if we conclude otherwise, we can avoid doing it always and expect the test writer to provide explicit signal to do this check for partiular cases. NBD.
| new_payload_request: NewPayloadRequest, | ||
| pre_state: PreState, | ||
| chain_context: ChainContext, | ||
| transaction_public_keys: Optional[Tuple[Bytes, ...]] = None, |
There was a problem hiding this comment.
execute_new_paylaod_request can receive an optional list of transaction_public_keys.
The call of this method from the guest program perspective always provide the values. But remember this method is also called from the "Engine API" context which is stateful, in that case it isn't passed.
| if sender_public_key is None: | ||
| sender_address = recover_sender(block_env.chain_id, tx) | ||
| else: | ||
| sender_address = recover_sender_from_public_key( | ||
| block_env.chain_id, tx, sender_public_key | ||
| ) |
There was a problem hiding this comment.
Key point: if the public key ways provided, we use a recover_sender_from_public_key which will check if the sender_public_key provided is correct or not (and theoretically leverage it to make the signature check faster -- "theoretically" because of a comment I'll do later).
| if len(rlp.encode(block)) > MAX_RLP_BLOCK_SIZE: | ||
| raise InvalidBlock("Block rlp size exceeds MAX_RLP_BLOCK_SIZE") | ||
|
|
||
| if transaction_public_keys is not None and len( |
There was a problem hiding this comment.
Here in execute_block we do an early check of length matching between public_keys and txs count. Later when processing tx, each concrete pubkey will be verified against the tx signature.
| def recover_sender_from_public_key( | ||
| chain_id: U64, tx: Transaction, public_key: Bytes | ||
| ) -> Address: | ||
| """ | ||
| Verify that ``public_key`` is the transaction sender's canonical | ||
| uncompressed SEC1 public key. | ||
|
|
||
| This reference implementation verifies the supplied key by recovering the | ||
| canonical public key from the transaction signature and comparing the two. | ||
| Optimized implementations may avoid full public-key recovery, but must | ||
| still verify that the supplied key validates the signature and is | ||
| consistent with the transaction's recovery id / y-parity bit. Otherwise, | ||
| another valid recovery candidate could derive a different sender address. | ||
|
|
||
| Returns the sender address derived from the verified public key. | ||
| """ | ||
| if public_key != recover_transaction_public_key(chain_id, tx): | ||
| raise InvalidSignatureError | ||
| return _sender_address_from_public_key(public_key) |
There was a problem hiding this comment.
This is the "theoretically" comment I made before.
This is the method now called when processing transactions to get the address from the tx plus provided public key. Note the method comment.
For the spec, I want things to be simple, so I simply do ecrecover on the tx signature and check it against the provided public key in the witness. This is the simplest way to comply with the spec of verifying pubkey correctness.
As mentioned in this method comment, a real EL should leverage the public key to verify the ECDSA signature against it since that's faster than a full ecrecover. But I also add an important reminder point that that's not enough and it should also check the provided public key matches the parity bit from the tx signature. This is relevant since our case here is not really a normal ECDSA-verification that we trust the public key being correct, but should match what the yParity says in the signature to not have the symmetric EC point of r-X-coordinate.
TL;DR: in the spec, I'm doing the easiest non-optimized thing. But leaving the comment for EL authors reading this since might not be obvious.
I'm also planning to add a test to trigger this border case since it is far from obvious, and could lead to an incorrect address derivation of the sender.
cc @kevaundray
There was a problem hiding this comment.
The two simple tests for valid and invalid cases to do minimal coverage that things are working as expected.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## projects/zkevm #2825 +/- ##
==================================================
+ Coverage 85.72% 86.36% +0.63%
==================================================
Files 612 613 +1
Lines 37853 38157 +304
Branches 3932 3980 +48
==================================================
+ Hits 32451 32954 +503
+ Misses 4652 4556 -96
+ Partials 750 647 -103
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
🗒️ Description
Adds transaction public keys to Amsterdam stateless validation input and threads them through payload/block execution.
StatelessInput.public_keyswith one 65-byte uncompressed SEC1 public key per payload transaction.So, although I added some test cases to check tha valid construction works fine, and basic invalid public key are rejected by the guest program, I believe we need to add some extra tests here. Mainly regarding potential wrong public keys reg ECDSA parity checks, and probably other cases. We will do that in other upcoming PRs.
🔗 Related Issues or PRs
N/A.
✅ Checklist
just statictype(scope):.mkdocs servelocally and verified the auto-generated docs for new tests in the Test Case Reference are correctly formatted.@ported_frommarker.Cute Animal Picture