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

Copy Circuit#194

Merged
icemelon merged 16 commits into
privacy-ethereum:masterfrom
scroll-tech:feat/copy-circuit
Jul 7, 2022
Merged

Copy Circuit#194
icemelon merged 16 commits into
privacy-ethereum:masterfrom
scroll-tech:feat/copy-circuit

Conversation

@icemelon
Copy link
Copy Markdown
Collaborator

@icemelon icemelon commented May 7, 2022

This PR brings the copy circuit that eliminates the multi-slot opcodes such as CALLDATACOPY, CODECOPY, and LOG.

@icemelon icemelon changed the title Add Copy Circuit Copy Circuit May 7, 2022
@roynalnaruto roynalnaruto mentioned this pull request May 26, 2022
@roynalnaruto
Copy link
Copy Markdown
Collaborator

@icemelon You will need to edit this PR from "draft" to "open"

@icemelon icemelon marked this pull request as ready for review June 15, 2022 16:48
@ed255 ed255 self-requested a review June 16, 2022 09:21
Copy link
Copy Markdown
Contributor

@ed255 ed255 left a comment

Choose a reason for hiding this comment

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

Overall looks very good!
I've added a few notes here and there but they are mainly topics for discussion and some small suggestions.

Comment thread specs/tables.md Outdated
Comment thread specs/copy-proof.md Outdated
Comment thread specs/tables.md Outdated
Comment thread specs/tables.md
Comment thread specs/copy-proof.md
Comment thread src/zkevm_specs/evm/typing.py
Comment thread src/zkevm_specs/copy_circuit.py Outdated
Comment thread src/zkevm_specs/copy_circuit.py
Comment thread src/zkevm_specs/evm/typing.py Outdated

def __init__(self) -> None:
self.rows = []
self.pad_rows = [CopyCircuitRow(*[FQ(0)] * 18)] * 2
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.

This is an example of how padding rows work right? Not padding in the sense that a 0 is read after the src end; but in the sense that the circuit has a fixed number of rows and maybe not all of them are used.

If that's the case, could you add a line in the markdown doc saying that padding rows are set to all 0s, and that these values always pass the constraints?

Comment thread specs/copy-proof.md
@roynalnaruto
Copy link
Copy Markdown
Collaborator

Overall looks very good!

I've added a few notes here and there but they are mainly topics for discussion and some small suggestions.

Thanks for the suggestions. They all make sense and I'll update the PR soon :)

Comment thread specs/copy-proof.md Outdated
Comment thread src/zkevm_specs/copy_circuit.py
# bytes_left == 1 for last step
cs.constrain_zero(rows[1].is_last * (1 - rows[0].bytes_left))
# bytes_left == bytes_left_next + 1 for non-last step
cs.constrain_zero((1 - rows[1].is_last) * (rows[0].bytes_left - rows[2].bytes_left - 1))
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.

not sure why use rows[2].bytes_left , refer to last step second row?

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.

If rows[0] is the read step (given that condition rows[0].q_step) then we want to constrain:
rows[0].bytes_left == rows[2].bytes_left + 1 as rows[2] is the next read step if rows[1] was not the last step.

@DreamWuGit
Copy link
Copy Markdown
Collaborator

DreamWuGit commented Jun 23, 2022

just have a quick look ,one point to discuss: seems to me maybe there is possible duplicated constraint between copy circuit and state circuit. for example, read value constrain to be equal to writing value at the same address. in the state circuit should constrain this (even though not implemented yet sort of) by design, but it is not big problem from my side.

Comment thread src/zkevm_specs/copy_circuit.py Outdated
Copy link
Copy Markdown
Collaborator

@roynalnaruto roynalnaruto left a comment

Choose a reason for hiding this comment

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

Small question WRT some removed checks

@ed255
Copy link
Copy Markdown
Contributor

ed255 commented Jun 23, 2022

just have a quick look ,one point to discuss: seems to me maybe there is possible duplicated constraint between copy circuit and state circuit. for example, read value constrain to be equal to writing value at the same address. in the state circuit should constrain this (even though not implemented yet sort of) by design, but it is not big problem from my side.

Are you referring to the constraint that the value in the write row is the same as the value in the read row (of the same copy step)? If that's the case, I don't see how it's redundant as the CopyCircuit is copying between two different places. Could you elaborate on this duplicate constraint between Copy Circuit and State Circuit; and point to the exact constraint in the python spec?

Thanks!

Comment thread src/zkevm_specs/copy_circuit.py
Comment thread src/zkevm_specs/evm/table.py Outdated
Comment on lines +423 to +433
src_id: FQ
src_type: FQ
dst_id: FQ
dst_type: FQ
src_addr: FQ
src_addr_end: FQ
dst_addr: FQ
length: FQ
rw_counter: FQ
rwc_inc: FQ
log_id: FQ = field(default=FQ(0))
Copy link
Copy Markdown
Contributor

@ed255 ed255 Jun 27, 2022

Choose a reason for hiding this comment

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

Suggested change
src_id: FQ
src_type: FQ
dst_id: FQ
dst_type: FQ
src_addr: FQ
src_addr_end: FQ
dst_addr: FQ
length: FQ
rw_counter: FQ
rwc_inc: FQ
log_id: FQ = field(default=FQ(0))
src_id: FQ # Copy source type-specific id. The id for each type is: {Bytecode: code_hash, Memory: call_id, TxCallData: tx_id, TxLog: tx_id}
src_type: FQ # Copy source type from `CopyDataTypeTag`
dst_id: FQ # Copy destination type-specific id
dst_type: FQ # Copy destination type from `CopyDataTypeTag`
src_addr: FQ # Copy source start address / index
src_addr_end: FQ # Size of the Copy source. When source address equals this value or higher, the read value is `0`.
dst_addr: FQ # Copy destination start address / index
length: FQ # Copy length: number of bytes written into destination
rw_counter: FQ # Rw counter value at the beginning of the copy
rwc_inc: FQ # How much will the rw_counter increase after the copy is complete
log_id: FQ = field(default=FQ(0))

Field description suggestions.

NOTE: I later realized that this fields are properly described in tables.md, so maybe these comments are redundant.

Comment thread src/zkevm_specs/evm/typing.py Outdated
@ed255 ed255 mentioned this pull request Jun 28, 2022
2 tasks
@icemelon icemelon force-pushed the feat/copy-circuit branch from 3eb2dc3 to 50b77d2 Compare June 30, 2022 05:38
Copy link
Copy Markdown
Contributor

@ed255 ed255 left a comment

Choose a reason for hiding this comment

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

LGTM! Great work!

@DreamWuGit
Copy link
Copy Markdown
Collaborator

DreamWuGit commented Jun 30, 2022

Are you referring to the constraint that the value in the write row is the same as the value in the read row (of the same copy step)? If that's the case, I don't see how it's redundant as the CopyCircuit is copying between two different places. Could you elaborate on this duplicate constraint between Copy Circuit and State Circuit; and point to the exact constraint in the python spec?

I check current state circuit don't constrain value read/write consistency for now, so it not problem !

Comment thread specs/tables.md
- **is_last**: a boolean value to indicate the last row in a copy event.
- **ID**: could be `$txID`, `$callID`, `$codeHash` (RLC encoded).
- **Type**: indicates the type of data source, including `Memory`, `Bytecode`, `TxCalldata`, `TxLog`.
- **Address**: indicates the address in the source data, could be memory address, byte index in the bytecode, tx call data, and tx log data. When the data type is `TxLog`, the address is the combination of byte index, `TxLogFieldTag.Data` tag, and `LogID`.
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.

after #214 merged, Address for TxLog change to log_id + field_tag + index now. maybe need update accordingly . thanks !

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I already updated the txlog lookup in the latest commit. could you help take a look?

@ed255
Copy link
Copy Markdown
Contributor

ed255 commented Jun 30, 2022

I check current state circuit don't constrain value read/write consistency for now, so it not problem !

But that's just because it's a TODO rigth? The spec has de read consistency constraint: https://github.com/privacy-scaling-explorations/zkevm-specs/blob/34eafad5f5c1031834d48adc2499a0f94bc3ae3e/src/zkevm_specs/state.py#L490-L496
And the circuit should have it too.

Once the read consistency constraint is added to the state circuit, do you think there will be redundancy of constraints with the copy circuit?

@ed255
Copy link
Copy Markdown
Contributor

ed255 commented Jul 1, 2022

@lispc in case you haven't reviewed this yet; could you take a look please? So that we reach 2 approvals :)

@lispc
Copy link
Copy Markdown
Collaborator

lispc commented Jul 5, 2022

@ed255 @roynalnaruto OK! I missed it! i will have a review now

@lispc
Copy link
Copy Markdown
Collaborator

lispc commented Jul 7, 2022

great job! current codes seems much more elegant!

@ed255
Copy link
Copy Markdown
Contributor

ed255 commented Jul 7, 2022

@icemelon feel free to merge this PR whenever you like :) (as you submitted it originally)

@icemelon icemelon merged commit 1ad4004 into privacy-ethereum:master Jul 7, 2022
@icemelon icemelon deleted the feat/copy-circuit branch July 7, 2022 23:57
@DreamWuGit
Copy link
Copy Markdown
Collaborator

DreamWuGit commented Jul 8, 2022

I check current state circuit don't constrain value read/write consistency for now, so it not problem !

But that's just because it's a TODO rigth? The spec has de read consistency constraint:

https://github.com/privacy-scaling-explorations/zkevm-specs/blob/34eafad5f5c1031834d48adc2499a0f94bc3ae3e/src/zkevm_specs/state.py#L490-L496

And the circuit should have it too.
Once the read consistency constraint is added to the state circuit, do you think there will be redundancy of constraints with the copy circuit?

@ed255 sorry for the delay, yes, I think so , current state circuit don't implement yet. we can keep an eye when it starts implementation .
Filed issue #232 to track this in case we miss it in the future !

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.

5 participants