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

MPT description #136

Open
wants to merge 92 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
cf311ab
initial MPT description
miha-stopar Mar 1, 2022
a62a1ad
MPT docs
miha-stopar Mar 3, 2022
90e34ac
MPT: some info on extension nodes
miha-stopar Mar 4, 2022
936ee06
MPT: branch img & constraints
miha-stopar Mar 8, 2022
08a6928
MPT: branch hash in parent branch
miha-stopar Mar 9, 2022
c579d30
MPT: branch constraints
miha-stopar Mar 9, 2022
d62eb1d
MPT: extension nodes
miha-stopar Mar 14, 2022
4ef67c5
MPT diagram
miha-stopar Mar 15, 2022
36d2b0c
MPT diagram updated; proof diagram
miha-stopar Mar 16, 2022
7f36a02
fix
miha-stopar Mar 18, 2022
a0ebbf9
MPT diagrams
miha-stopar Mar 22, 2022
6e7ed44
MPT in parent diagrams
miha-stopar Mar 23, 2022
e2f4ec3
branch diagrams
miha-stopar Mar 25, 2022
1baa3f0
MPT branch / extension node
miha-stopar Apr 1, 2022
2ac8de8
MPT: storage leaf
miha-stopar Apr 6, 2022
ece4504
MPT: lookups
miha-stopar Apr 13, 2022
b5fc0f4
storage leaf constraints
miha-stopar Apr 13, 2022
4190c12
MPT: hash of a leaf
miha-stopar Apr 15, 2022
bd61499
MPT: key_rlc in extension nodes:
miha-stopar Apr 19, 2022
916b501
MPT: storage, account leaf
miha-stopar Apr 20, 2022
793bc02
minor update
miha-stopar Jun 1, 2022
49a36c6
key compact form description
miha-stopar Jul 25, 2022
7d0ab8c
started reworking MPT specs
miha-stopar Jul 29, 2022
9393700
fix
miha-stopar Jul 29, 2022
5962eb0
minor update in MPT
miha-stopar Aug 1, 2022
08de8e2
account leaf nonce balance docs
miha-stopar Aug 2, 2022
4a7d0e8
documenting account leaf nonce balance
miha-stopar Aug 3, 2022
710b884
MPT nonce balance specs
miha-stopar Aug 4, 2022
8d2b7df
MPT docs restructured; account_leaf_storage_codehash docs
miha-stopar Aug 8, 2022
27fed6a
minor update to account leaf key chip
miha-stopar Aug 10, 2022
8ae596c
account leaf key constraints update
miha-stopar Aug 11, 2022
1a83bc5
account leaf docs
miha-stopar Aug 12, 2022
3ce786c
account in added branch description
miha-stopar Aug 16, 2022
7a8bedc
documenting extension node & account leaf
miha-stopar Aug 17, 2022
2380bf3
account leaf minor update
miha-stopar Aug 18, 2022
07ade74
account non existing proof summary
miha-stopar Aug 19, 2022
62d2856
account leaf: account non existing constraints
miha-stopar Aug 22, 2022
53d648d
MPT account leaf updated
miha-stopar Aug 23, 2022
ce0e79e
fix
miha-stopar Aug 23, 2022
0b16f7d
documenting MPT branch
miha-stopar Aug 24, 2022
4bff73f
Documenting MPT branch and account leaf
miha-stopar Aug 26, 2022
6753764
MPT specs reformatted
miha-stopar Aug 29, 2022
2958329
MPT branch docs updated
miha-stopar Sep 1, 2022
be4bdec
branch key docs; proof type update
miha-stopar Sep 2, 2022
b0a9456
branch docs
miha-stopar Sep 5, 2022
fafced4
extension node key docs
miha-stopar Sep 6, 2022
67ed004
extension node key docs
miha-stopar Sep 7, 2022
e081a36
extension node key docs
miha-stopar Sep 8, 2022
6f7a2b2
extension node docs
miha-stopar Sep 9, 2022
94d0f24
documenting storage leaf
miha-stopar Sep 12, 2022
7bd7c83
MPT specs updated
miha-stopar Sep 14, 2022
f2d1c05
MPT specs updated
miha-stopar Sep 16, 2022
4f92031
documenting drifted leaf
miha-stopar Sep 19, 2022
960d98b
MPT specs updated
miha-stopar Sep 20, 2022
179daa3
proof chain constraints; storage leaf constraints updated
miha-stopar Sep 21, 2022
5590860
MPT specs: proof chain & selectors
miha-stopar Sep 22, 2022
6e01d00
mpt specs updated
miha-stopar Oct 13, 2022
34b25bf
mpt docs cleaned
miha-stopar Oct 17, 2022
c4239ba
MPT non-existing-storage docs
miha-stopar Oct 27, 2022
9d38948
MPT matching with scope document
miha-stopar Oct 28, 2022
5cda455
MPT docs fix
miha-stopar Oct 28, 2022
21640e4
Info about implicit leaf creations
miha-stopar Dec 1, 2022
0eccdc7
started rewriting MPT specs
miha-stopar Apr 18, 2023
9f05bcf
MPT memory
miha-stopar Apr 19, 2023
79b5a65
MainData, ParentData
miha-stopar Apr 20, 2023
24a7c02
MPT start constraints
miha-stopar Apr 24, 2023
acd3381
Started on account leaf rewriting
miha-stopar Jun 9, 2023
de58c6d
Working on MPT account leaf docs
miha-stopar Jun 12, 2023
80044e6
Account leaf docs
miha-stopar Jun 13, 2023
3645fb5
Started on MainRLPGadget docs
miha-stopar Jun 14, 2023
354cd28
Working on DriftedGadget specs
miha-stopar Jun 15, 2023
b7ba5c0
Drifted leaf gadget specs
miha-stopar Jun 16, 2023
899efbe
WrongGadget specs
miha-stopar Jun 19, 2023
6d60fb3
MPT state machine
miha-stopar Jun 26, 2023
0dc89f3
Account leaf specs
miha-stopar Jun 28, 2023
1572865
Placeholder cases briefly described.
miha-stopar Jul 25, 2023
6821454
MPT specs: some RLP related info.
miha-stopar Jul 26, 2023
91bf48d
Started rewriting MPT branch docs
miha-stopar Aug 28, 2023
c973200
Working on ListKeyGadget specs
miha-stopar Aug 29, 2023
3e11c90
MainRLPGadget specs
miha-stopar Aug 30, 2023
d226b07
Started rewriting branch docs
miha-stopar Nov 22, 2023
0e38823
Modified extension node docs
miha-stopar Nov 24, 2023
bad5bce
ExtensionBranch docs
miha-stopar Nov 28, 2023
6586415
RlpItemView docs
miha-stopar Nov 30, 2023
76062ed
Rewriting extension node docs
miha-stopar Dec 6, 2023
f1212eb
ext_key_rlc_expr helper function
miha-stopar Dec 11, 2023
31ceb03
formatting fix
miha-stopar Dec 11, 2023
1fc681c
Working on accout leaf docs
miha-stopar Dec 18, 2023
95b6e4d
IsPlaceholderLeafGadget docs; account leaf constraints
miha-stopar Jan 4, 2024
8fd8bc8
how to use MPT
miha-stopar Jan 30, 2024
14129c2
ListKeyGadget description
miha-stopar Feb 7, 2024
3deb4b5
MPT mod extension gadget
miha-stopar Apr 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,213 changes: 1,213 additions & 0 deletions specs/mpt/account-leaf.md

Large diffs are not rendered by default.

927 changes: 927 additions & 0 deletions specs/mpt/branch.md

Large diffs are not rendered by default.

201 changes: 201 additions & 0 deletions specs/mpt/extension-branch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# Branch

A branch consists of 21 rows. The enumerator with the branch row types is `ExtensionBranchRowType`:

```
pub(crate) enum ExtensionBranchRowType {
Mod,
Child0,
Child1,
Child2,
Child3,
Child4,
Child5,
Child6,
Child7,
Child8,
Child9,
Child10,
Child11,
Child12,
Child13,
Child14,
Child15,
Key, // Both (S and C) extension nodes have the same key - when it is different, we have a modified extension node case.
ValueS,
Nibbles, // The list of second nibbles (key byte = nibble1 * 16 + nibble2) that correspond to the key.
ValueC,
Count,
}
```

`S` and `C` branches have the same children except for the one at the index `modified_node`.
The witness stores the 16 `S` children in `Child1`, ..., `Child15` rows and stores at `Mod` row
the children of the `C` branch where the modification occurs (child of `C` proof at index `modified_node`).

The rows `Key`, `ValueS`, `Nibbles`, and `ValueC` are used only when the branch is an extension node.

The circuit handles extension node as a special case branch - branch
with a path elongated by one or more nibbles.
An example of the extension node returned by `get_Proof`:
```
[226 24 160 194 200 39 82 205 97 69 91 92 98 218 180 101 42 171 150 75 251 147 154 59 215 26 164 201 90 199 185 190 205 167 64]
```

The first byte (`226`) represents the length of the RLP stream (`226 - 192 = 34`). The second byte represents the nibble - (only one
nibble in that case: `24 - 16 = 8`).

The byte `160` represents the length (`32 = 160 - 128`) of the RLP substream that represents the hash of the branch.

To preserve the branch layout in the case of extension nodes, the branch
witness contains the extension node rows with information about
nibbles (`Key` row), `S` branch hash (`ValueS` row), `C` branch hash (`ValueC` row), and second nibbles (`Nibbles`).

The witness for the second nibbles is necessary for the cases as below:
```
[228, 130, 16 + 3, 9*16 + 5, 0, ...]
```
In this case, there are three nibbles: `3`, `9`, `5`. With
the knowledge of the second nibble `5`, the circuit can compute
the first nibble `9`.

Note that in some cases the number of nibbles is different in `S` and
`C` proofs. These cases are handles by
[`ModExtensionGadget`](mod_extension.md).


`ExtensionBranchConfig` is an abstraction of the extension nodes and branches. It contains information whether the branch:
* is a placeholder (`is_placeholder`): at most one of the two branches (`S` and `C`) is a placeholder,
* is an extension (`is_extension`): both branches are extension nodes or none.

`ExtensionBranchConfig` uses `ExtensionGadget` and `BranchGadget` to compute the intermediate values for the key RLC, the key multiplier,
the key parity, the number of nibbles after the branch (in both cases - in the case of the extension node, the intermediate values after the underlying branch
are computed).

Extension node and branch specific constraints are implemented in [`ExtensionGadget`](extension-node.md) and
[`BranchGadget`](branch.md) respectively.

## ExtensionBranchConfig

```
pub(crate) struct ExtensionBranchConfig<F> {
key_data: KeyData<F>,
parent_data: [ParentData<F>; 2],
is_placeholder: [Cell<F>; 2],
is_extension: Cell<F>,
extension: ExtensionGadget<F>,
branch: BranchGadget<F>,
}
```

It needs to be checked that the cells for `is_placeholder` and `is_extension` contain boolean values.

It needs to be checked that at most one branch (either in `S` or `C` proof or none) is a placeholder:

```
require!(config.is_placeholder[true.idx()].expr() + config.is_placeholder[false.idx()].expr() => bool);
```

The post state after the extension node (considering only the extension node nibbles, not its branch) is computed:

```
let (
num_nibbles,
is_key_odd,
key_rlc_post_ext,
key_mult_post_ext,
is_root_s,
is_root_c,
parent_word_s_lo,
parent_word_s_hi,
parent_word_c_lo,
parent_word_c_hi,
parent_rlc_s,
parent_rlc_c,
) = ifx! {config.is_extension => {
config.extension = ExtensionGadget::configure(
meta,
cb,
ctx.clone(),
&config.key_data,
&config.parent_data,
&config.is_placeholder,
);
let ext = config.extension.get_post_state();
(
ext.num_nibbles,
ext.is_key_odd,
ext.key_rlc,
ext.key_mult,
false.expr(),
false.expr(),
ext.branch_rlp_word[true.idx()].lo(),
ext.branch_rlp_word[true.idx()].hi(),
ext.branch_rlp_word[false.idx()].lo(),
ext.branch_rlp_word[false.idx()].hi(),
ext.branch_rlp_rlc[true.idx()].expr(),
ext.branch_rlp_rlc[false.idx()].expr(),
)
} elsex {
(
config.key_data.num_nibbles.expr(),
config.key_data.is_odd.expr(),
config.key_data.rlc.expr(),
config.key_data.mult.expr(),
config.parent_data[true.idx()].is_root.expr(),
config.parent_data[false.idx()].is_root.expr(),
config.parent_data[true.idx()].hash.lo().expr(),
config.parent_data[true.idx()].hash.hi().expr(),
config.parent_data[false.idx()].hash.lo().expr(),
config.parent_data[false.idx()].hash.hi().expr(),
config.parent_data[true.idx()].rlc.expr(),
config.parent_data[false.idx()].rlc.expr(),
)
}};
```

That means the number of nibbles is computed (the total number of nibbles used in the path so far). Note that when the leaf is reached, the number of nibbles combined
with the leaf nibbles needs to be `64`. The `ExtensionGadget` simply adds the number of the extension node nibbles to the current `num_nibbles` value.

The key parity `is_key_odd` is computed - if the number of nibbles in the extension node is even, the parity does not change.

The key RLC is computed - the extension node nibbles are added to the `key_rlc` value.

The parent word and RLC are computed - these values contain the information about the modified node (in the next level, the node will be checked against this value.

Note that if the node is a regular branch (and not extension node), this values are not computed in the `ExtensionGadget`, instead they are taken from
[`KeyData`](main.md) and [`ParentData`](main.md) (nothing has changed because there was no extension node and thus no extension node nibbles).

`BranchGadget` computes the post state after the branch (in both cases, in the extension node and regular branch). That means the `num_nibbles` value is increased
by `1`, the value `is_key_odd` is switched, the `key_rlc` value is updated with the branch `modified_index`. The `parent_word` and `parent_rlc` contain the information
about the modified child in the branch.

Finally, the values in the key and parent data store are updated:
```
KeyData::store(
cb,
&mut ctx.memory[key_memory(is_s)],
branch.key_rlc_post_branch.expr(),
branch.key_mult_post_branch.expr(),
branch.num_nibbles.expr(),
branch.is_key_odd.expr(),
branch.key_rlc_post_drifted.expr(),
0.expr(),
0.expr(),
false.expr(),
);
ParentData::store(
cb,
&mut ctx.memory[parent_memory(is_s)],
branch.mod_word[is_s.idx()].clone(),
branch.mod_rlc[is_s.idx()].expr(),
false.expr(),
false.expr(),
Word::<Expression<F>>::new([0.expr(), 0.expr()])
);
```





Loading
Loading