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

Feat/#498 integrate precompiles into callop #508

Merged
merged 13 commits into from
Feb 21, 2024

Conversation

KimiWu123
Copy link
Contributor

closed #498

@KimiWu123 KimiWu123 self-assigned this Dec 17, 2023
@KimiWu123 KimiWu123 marked this pull request as ready for review December 21, 2023 08:08
@KimiWu123 KimiWu123 changed the title [WIP] Feat/#498 integrate precompiles into callop Feat/#498 integrate precompiles into callop Dec 21, 2023
@KimiWu123
Copy link
Contributor Author

KimiWu123 commented Dec 21, 2023

Hi @roynalnaruto , I'm wondering if you have time to review this. I'm not confident that I understand your implementation fully. It would be nice if you could help reivew this or give a quick overview if you're busy.

There are two main difference in my PR,
1. removed RLC copy table lookup. I don't see why we need it bcs we're using word lo/hi as inputs/outputs for individual precompiles.
2. the constraints in PrecompileGadget. I followed your upstream PR #1628 but not sure how to add constraints for EC_ADD, EC_MUL and EC_PAIRING.

@KimiWu123
Copy link
Contributor Author

@roynalnaruto , do you have time to give me some feedback? Thanks. 😅

@KimiWu123 KimiWu123 marked this pull request as draft January 11, 2024 07:17
@KimiWu123 KimiWu123 marked this pull request as ready for review January 30, 2024 12:33
Copy link
Collaborator

@ChihChengLiang ChihChengLiang left a comment

Choose a reason for hiding this comment

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

Great work implementing the spec for the precompiles call. I left some comments.

src/zkevm_specs/evm_circuit/execution/callop.py Outdated Show resolved Hide resolved
src/zkevm_specs/evm_circuit/execution/callop.py Outdated Show resolved Hide resolved
src/zkevm_specs/evm_circuit/util/precompile_gadget.py Outdated Show resolved Hide resolved
Co-authored-by: Chih Cheng Liang <chihchengliang@gmail.com>
@miha-stopar miha-stopar self-requested a review February 7, 2024 10:10
@KimiWu123 KimiWu123 force-pushed the feat/#498-integrate-precompiles-into-callop branch from 14a38d4 to f3eee6f Compare February 14, 2024 08:16
Copy link
Collaborator

@ChihChengLiang ChihChengLiang left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the epic work!

input_bytes.extend(sig_r.int_value().to_bytes(32, "little"))
input_bytes.extend(sig_s.int_value().to_bytes(32, "little"))
input_rlc = RLC(bytes(reversed(input_bytes)), keccak_randomness, n_bytes=128).expr()
instruction.constrain_equal(rlc_data.input_rlc, input_rlc)
Copy link
Contributor

Choose a reason for hiding this comment

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

Aren't these two values actually the same (I mean not equal, but the same)? Because rlc_data.input_rlc is computed when instantiating the auxiliary data and input_rlc is computed accessing the same values in aux_data?

Copy link
Contributor Author

@KimiWu123 KimiWu123 Feb 20, 2024

Choose a reason for hiding this comment

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

For me, data assignment in testing is like what assign_exec_step does at proving time in our circuit. So, yes, it looks the same (but not always the same).
At proving time, a prover assigns

  • msg_hash
  • sig_v, sig_r, sig_s
  • RLC(msg_hash, sig_v, sig_r, sig_s) (a.k.a input_rlc here, this field is to verify data consistency between calls)

In verification logic here, we have to gaurantee msg_hash, sig_v, sig_r and sig_s are the same pairs (sig_v, sig_r and sig_s are coming from the signature of msg_hash). However, a malious signer could sign msg_hash2 and have sig_v2, sig_r2 and sig_s2. It still can pass all the constraints if we don't have calculate input_rlc here. I'm assuming rlc_data.input_rlc is coming from previous step (we can pass data around between previous step and next step in our circuit, but it seems not doable in our spec), so that's why rlc_data.input_rlc and input_rlc look "the same".

Does it make sense to you? Or do you think we need to copy rlc_data.input_rlc from copy_table?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I am missing something and would like to fix my understanding :). My understanding was that we need to ensure that instruction.curr.aux_data is the same as used in callop.py and that this should be done by checking the RLC, so to have a lookup call into copy_table (RLC field) here in ecrecover.py. Maybe this is not the case?

I agree on the assignment part, putting a link to the assignment was probably misleading from my side. What it seems to me here is that we have aux_data: PrecompileAuxData = instruction.curr.aux_data[0] and then both values, aux_data.input_rlc and input_rlc, are computed using aux_data. Or is it aux_data.input_rlc checked somewhere else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My understanding was that we need to ensure that instruction.curr.aux_data is the same as used in callop.py and that this should be done by checking the RLC, so to have a lookup call into copy_table (RLC field) here in ecrecover.py. Maybe this is not the case?

Yes, that's exactly what I want to do.

I agree on the assignment part, putting a link to the assignment was probably misleading from my side. What it seems to me here is that we have aux_data: PrecompileAuxData = instruction.curr.aux_data[0] and then both values, aux_data.input_rlc and input_rlc, are computed using aux_data. Or is it aux_data.input_rlc checked somewhere else?

No, aux_data.input_rlc was not check in other place.

In my implementation, I treated aux_data as witness inputs, a place I can assign my witnesses. Those data (e.g. sig_r, sig_v...etc) can't be retrieved from stack or memory like what other opcode gagdets implemented.

I might not explain it very well, but you can check Scroll's impl. The msg_hash_raw was converted into rlc formate and compared with msg_hash_keccak_rlc. In the assignment, you can see both of them come from the same source, here and here.

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.

Great work!

@KimiWu123 KimiWu123 merged commit 163f3c0 into master Feb 21, 2024
1 check passed
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.

Integrate precompiles into callop
3 participants