-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add compression circuit outline
- Loading branch information
Showing
6 changed files
with
227 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
yellow-paper/docs/rollup-circuits/message-compression.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
--- | ||
title: Message Compression | ||
--- | ||
|
||
To support easy consumption of l1 to l2 messages inside the proofs, we need to convert the tree of messages to a snark-friendly format. | ||
|
||
If you recall back in [L1 smart contracts](./../l1-smart-contracts/index.md#inbox) we were building a message tree on the L1 - which meant that we were using SHA256. | ||
As SHA256 is not snark-friendly, weak devices would not be able to prove inclusion of messages in the tree. | ||
|
||
This circuit is responsible for converting the tree such that users can easily build the proofs. | ||
We essentially use this circuit to front-load the work needed to prove the inclusion of messages in the tree. | ||
As earlier we are using a tree-like structure. | ||
|
||
```mermaid | ||
graph BT | ||
R((compress 4)) | ||
T0[compress_leaf] | ||
T1[compress_leaf] | ||
T2[compress_leaf] | ||
T3[compress_leaf] | ||
T0_P((compress 0)) | ||
T1_P((compress 1)) | ||
T2_P((compress 2)) | ||
T3_P((compress 3)) | ||
T4[compress] | ||
I0 --> T0 | ||
I1 --> T1 | ||
I2 --> T2 | ||
I3 --> T3 | ||
T0 --> T0_P | ||
T1 --> T1_P | ||
T2 --> T2_P | ||
T3 --> T3_P | ||
T0_P --> T4 | ||
T1_P --> T4 | ||
T2_P --> T4 | ||
T3_P --> T4 | ||
T4 --> R | ||
I0((MSG 0-3)) | ||
I1((MSG 4-7)) | ||
I2((MSG 8-11)) | ||
I3((MSG 12-15)) | ||
style R fill:#1976D2; | ||
style T0_P fill:#1976D2; | ||
style T1_P fill:#1976D2; | ||
style T2_P fill:#1976D2; | ||
style T3_P fill:#1976D2; | ||
style I0 fill:#1976D2; | ||
style I1 fill:#1976D2; | ||
style I2 fill:#1976D2; | ||
style I3 fill:#1976D2; | ||
``` | ||
|
||
Practically, this will be using two circuits, similar to how we have the `base` and `merge` circuits for transactions. | ||
The output of the "combined" circuit will be the `converted_root` which is the root of the snark-friendly message tree. | ||
And the `sha_root` which must match the root of the sha256 message tree from the L1 Inbox. | ||
The circuit must simply compute the two trees using the same inputs, and then we ensure that the elements of the trees match the inbox later at the [state transitioner](./../l1-smart-contracts/index.md#overview). | ||
|
||
|
||
```mermaid | ||
classDiagram | ||
direction LR | ||
class CompressPublicInput { | ||
aggregation_object: AggregationObject | ||
sha_root: Fr[2] | ||
converted_root: Fr | ||
} | ||
class CompressInput { | ||
msgs: List~TreeConversionData~ | ||
} | ||
CompressInput *-- TreeConversionData: msgs | ||
class TreeConversionData { | ||
proof: Proof | ||
public_inputs: CompressPublicInput | ||
} | ||
TreeConversionData *-- CompressPublicInput: public_inputs | ||
class CompressBaseInputs { | ||
msgs: List~Fr[2]~ | ||
} | ||
``` | ||
|
||
The computations performed by the circuits are quite simple, they simple need to progress building two trees, the sha tree and the snark-friendly tree. | ||
For optimization purposes, it can be useful to have the layers take more than 2 inputs to increase the task of every layer. | ||
If each just take 2 inputs, the overhead of recursing through the layers might be higher than the actual work done. | ||
|
||
```python | ||
def compress_leaf(msgs: List[Fr[2]]) -> CompressPublicInput: | ||
sha_root = MERKLE_TREE(msgs, SHA256); | ||
converted_root MERKLE_TREE(msgs, SNARK_FRIENDLY_HASH_FUNCTION); | ||
return CompressPublicInput(sha_root, converted_root) | ||
|
||
def compress(msgs: List[TreeConversionData]) -> CompressPublicInput: | ||
for msg in msgs: | ||
assert msg.proof.verify(msg.public_inputs); | ||
|
||
sha_root = MERKLE_TREE( | ||
[msg.public_inputs.sha_root for msg in msgs], | ||
SHA256 | ||
); | ||
converted_root = MERKLE_TREE( | ||
[msg.public_inputs.converted_root for msg in msgs], | ||
SNARK_FRIENDLY_HASH_FUNCTION | ||
); | ||
return CompressPublicInput(sha_root, converted_root) | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters