Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
367fd4c
mpt circuit - support for extension nodes and proofs of different len…
miha-stopar Dec 21, 2021
ba0a2fa
formatting
miha-stopar Dec 21, 2021
d7779cf
address_compr and key_compr removed
miha-stopar Dec 21, 2021
c64faca
value added to trie supported; sel1 and sel2 for key nibbles moved in…
miha-stopar Dec 22, 2021
f8030a9
branch placeholder initial constraints
miha-stopar Dec 24, 2021
0e34144
constraints for placeholder branch - key rlc
miha-stopar Dec 24, 2021
635afa9
assignments and selectors for cases when leaf turns into branch
miha-stopar Jan 5, 2022
12dd40e
extension nodes: two additional rows at the end of branch
miha-stopar Jan 5, 2022
d9039ff
leaf in added branch constraints
miha-stopar Jan 6, 2022
d9d3aaa
preparing for leaf in added branch hash checking
miha-stopar Jan 7, 2022
08afd5b
constraints for leaf that moved into new branch
miha-stopar Jan 7, 2022
aa8f218
first nibble constraints for added branch
miha-stopar Jan 11, 2022
1356f6b
all constraints related to moved leaf into new branch
miha-stopar Jan 11, 2022
3d105de
chip for selectors
miha-stopar Jan 11, 2022
91c6526
placeholder selector fix
miha-stopar Jan 11, 2022
caa61d4
placeholder branch only has two non nil nodes constraint
miha-stopar Jan 12, 2022
de65f78
range check for RLP data being bytes
miha-stopar Jan 12, 2022
541c2d6
storage delete test
miha-stopar Jan 12, 2022
543157e
branch init chip
miha-stopar Jan 13, 2022
84cd84f
branch key chip
miha-stopar Jan 13, 2022
7585b70
Tests for cases when branch is added/deleted; neighbouring leaf hash …
miha-stopar Jan 14, 2022
7a827ae
non-empty & empty branch node constraints
miha-stopar Jan 17, 2022
5c930fa
assignment and initial constraints for checking RLP length
miha-stopar Jan 18, 2022
4e34db7
removed key rlc constraints for placeholder leaf; added/deleted branc…
miha-stopar Jan 19, 2022
adfa461
branch RLP length constraints
miha-stopar Jan 19, 2022
5359e16
is_final_branch_child RLP length constraint
miha-stopar Jan 20, 2022
3296d09
branch hash checks moved into HashInParent chip
miha-stopar Jan 21, 2022
0d03246
HashInParent called two times (for S and C branch) instead of having …
miha-stopar Jan 21, 2022
953eb92
storage root in account leaf chip
miha-stopar Jan 21, 2022
2518073
branch rows chip
miha-stopar Jan 21, 2022
fd65fdb
extension node test; some docs
miha-stopar Jan 24, 2022
b971d04
branch hash in extension node check
miha-stopar Jan 25, 2022
80f1830
extension node hash constraints
miha-stopar Jan 25, 2022
641f9e5
extension node selectors constraints
miha-stopar Jan 26, 2022
43a1448
extension selectors handled a bit differently
miha-stopar Jan 26, 2022
f574b01
sel1 & sel2 constraints for branch and extension node key RLC
miha-stopar Jan 26, 2022
0eed931
extension key same for S and C constraints; compute_acc_and_mult moved
miha-stopar Jan 26, 2022
bac43cc
initial extension node key RLC constraints
miha-stopar Jan 27, 2022
8545c38
extracting account & storage key RLC from file name and checks (tempo…
miha-stopar Jan 28, 2022
f408e41
extension node key
miha-stopar Jan 28, 2022
0fe32a4
account proof (different addresses) added to all tests; address and k…
miha-stopar Jan 28, 2022
b514586
extension node key RLC short long fix
miha-stopar Jan 31, 2022
d157336
mult_diff for mult lookups
miha-stopar Jan 31, 2022
65f3696
missing mult diff constraint
miha-stopar Jan 31, 2022
99bd006
assignment is_short
miha-stopar Jan 31, 2022
9e22540
extension_node_mult_rlc assignment removed
miha-stopar Jan 31, 2022
0f01860
extension node with short key constraints
miha-stopar Feb 1, 2022
ead15a2
extension node short sel2 constraints
miha-stopar Feb 1, 2022
b178dd0
extension node two bytes sel2 test and initial constraints
miha-stopar Feb 1, 2022
f5a012b
extension long even: first_nibble, second_nibble constraints
miha-stopar Feb 2, 2022
92362dc
extension node key: all long even constraints implemented
miha-stopar Feb 2, 2022
4cf2f15
aggregated selectors to reduce number of lines
miha-stopar Feb 2, 2022
5782706
extension node: long odd sel1 constraints (no test yet)
miha-stopar Feb 2, 2022
597fe95
extension node: long odd sel2 constraints (no test yet)
miha-stopar Feb 2, 2022
5c2b884
extension node in storage first level
miha-stopar Feb 3, 2022
a29175c
extension node in first storage level long even
miha-stopar Feb 3, 2022
3e81469
account and storage first level extension node constraints merged
miha-stopar Feb 4, 2022
c945bd8
extension nodes long odd first level and after first level constraint…
miha-stopar Feb 4, 2022
4cbe3cc
extension nodes short first level and after first level constraints m…
miha-stopar Feb 4, 2022
6438f32
extension node three bytes long test and fix
miha-stopar Feb 4, 2022
051cbf3
missing selector added
miha-stopar Feb 4, 2022
fcf7d62
renamed: first_nibble -> drifted_pos
miha-stopar Feb 8, 2022
05786d0
check for extension hash in parent ignored when placeholder
miha-stopar Feb 8, 2022
22b7475
added extension test
miha-stopar Feb 8, 2022
1582d82
debugging leaf key RLC after placeholder
miha-stopar Feb 10, 2022
669e9fc
previous key RLC assigned to avoid ConstraintPoisened errors
miha-stopar Feb 11, 2022
bae0de9
constraints for drifted leaf key RLC
miha-stopar Feb 11, 2022
59f5d19
drifted leaf (short RLP) constraints
miha-stopar Feb 11, 2022
b6b6235
drifted leaf (long RLP) constraints
miha-stopar Feb 11, 2022
4b31006
fix in drifted leaf key RLC constraints
miha-stopar Feb 11, 2022
1829626
old drifted leaf constraints removed; now replaced by comparison of d…
miha-stopar Feb 11, 2022
de0b9d9
tests added
miha-stopar Feb 11, 2022
d52f737
drifted key sel1/sel2 fix
miha-stopar Feb 14, 2022
960e0b5
leaf key c_rlp1 added; constraints for parent hash when only leaf wit…
miha-stopar Feb 15, 2022
37ef6cb
storage root in account leaf constraint ignored if leaf without branch
miha-stopar Feb 15, 2022
3d418f0
test for first storage extension added
miha-stopar Feb 15, 2022
25cbfcd
extension tests added
miha-stopar Feb 15, 2022
e9ece6d
c_rlp for leaf key
miha-stopar Feb 15, 2022
6367b34
account leaf c_rlp; test added
miha-stopar Feb 15, 2022
715d945
decreasing degree in account_leaf_key
miha-stopar Feb 16, 2022
60da3bf
reduced degree in account_leaf_nonce_balance
miha-stopar Feb 17, 2022
494d090
leaf_key_in_added_branch refactored
miha-stopar Feb 17, 2022
0bd9de5
leaf_key missing constraints
miha-stopar Feb 17, 2022
508b4d4
byte range lookups
miha-stopar Feb 17, 2022
13e2672
constraints for key being 0 after key len uncommented
miha-stopar Feb 21, 2022
f9f443f
key_len_lookups commented out for now (tests last too long with the d…
miha-stopar Feb 21, 2022
afb00ee
key RLC when leaf without branch
miha-stopar Feb 21, 2022
948c401
Constraints for case when storage proof contains only a leaf
miha-stopar Feb 21, 2022
fc105b8
rotation fix in extension_node_key
miha-stopar Feb 22, 2022
06c243b
hash of account leaf lookup moved into account_leaf_storage_codehash
miha-stopar Feb 22, 2022
f960f09
ignore lookups into branch when account proof only have a leaf
miha-stopar Feb 22, 2022
f061a44
extension node uses now selectors with more packed info to reduce exp…
miha-stopar Mar 1, 2022
3470602
branch_key now uses sel1/sel2 (c16, c1) from two s_advices columns in…
miha-stopar Mar 1, 2022
1d218ef
reducing degree in extension_node_key
miha-stopar Mar 1, 2022
8cb8f47
removing old selectors in extension_node_key
miha-stopar Mar 1, 2022
a25b281
is_first_storage_level fix; moving from sel1/sel2 to selectors in s_a…
miha-stopar Mar 3, 2022
79e16b1
drifted key RLC in first storage level fix
miha-stopar Mar 3, 2022
c2ad33c
tests regenerated with new extension selectors
miha-stopar Mar 3, 2022
fa906ef
c16/c1 instead of sel1/sel2 in account_leaf_key
miha-stopar Mar 3, 2022
c3141ab
new extension selectors used for assignment
miha-stopar Mar 3, 2022
61fe3e5
new extension selectors in branch_key
miha-stopar Mar 3, 2022
211fcff
IS_EXTENSION_NODE_POS removed and replaced by new selectors
miha-stopar Mar 4, 2022
6d0cc5a
expression to prevent Poisened constraint removed from where not nece…
miha-stopar Mar 4, 2022
0c64ada
key_rlc/key_rlc_mult used for mult_diff/nonce/mult_diff_balance inste…
miha-stopar Mar 8, 2022
030059c
sel1/sel2 used for key_rlc_prev/key_rlc_mult_prev instead of s_keccak…
miha-stopar Mar 8, 2022
cabdb28
s(c)_modifed_node_rlc columns added that will replace s(c)_keccak; ne…
miha-stopar Mar 9, 2022
005c7b1
s_root and c_root added; witnesses regenerated
miha-stopar Mar 10, 2022
fb19197
key_rlc and address_rlc assert removed
miha-stopar Mar 10, 2022
ed82ad4
keccak output 4 columns with words -> 1 column with hash RLC
miha-stopar Mar 14, 2022
878619d
account leaf nonce balance C witness
miha-stopar Mar 16, 2022
d11d739
fixing is_leaf_without_branch selector
miha-stopar Mar 17, 2022
6c35ea0
rotation issues fixed
miha-stopar Mar 18, 2022
4c5d878
storage without leaf after branch placeholder root in account leaf
miha-stopar Mar 18, 2022
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/target
*.png
.DS_Store
.vscode
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ members = [
"keccak256",
"geth-utils",
"integration-tests",
"circuit-benchmarks"
"circuit-benchmarks",
"mpt"
]

[patch.crates-io]
Expand Down
17 changes: 17 additions & 0 deletions mpt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "mpt"
version = "0.1.0"
edition = "2018"

[dependencies]
halo2 = { git = "https://github.com/appliedzkp/halo2.git", rev = "b78c39cacc1c79d287032f1b5f94beb661b3fb42" }
itertools = "0.10.1"
num-bigint = "0.4.2"
num-traits = "0.2.14"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
keccak256 = { path = "../keccak256"}
pairing = { git = 'https://github.com/appliedzkp/pairing', package = "pairing_bn256" }
rand_xorshift = "0.3"
ark-std = { version = "0.3", features = ["print-trace"] }

234 changes: 234 additions & 0 deletions mpt/src/account_leaf_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
use halo2::{
circuit::Chip,
plonk::{
Advice, Column, ConstraintSystem, Expression, Fixed, VirtualCells,
},
poly::Rotation,
};
use pairing::arithmetic::FieldExt;
use std::marker::PhantomData;

use crate::{
helpers::{compute_rlc, key_len_lookup, mult_diff_lookup, range_lookups},
mpt::FixedTableTag,
param::{
HASH_WIDTH, IS_BRANCH_C16_POS, IS_BRANCH_C1_POS, LAYOUT_OFFSET,
R_TABLE_LEN,
},
};

#[derive(Clone, Debug)]
pub(crate) struct AccountLeafKeyConfig {}

// Verifies the hash of a leaf is in the parent branch.
pub(crate) struct AccountLeafKeyChip<F> {
config: AccountLeafKeyConfig,
_marker: PhantomData<F>,
}

impl<F: FieldExt> AccountLeafKeyChip<F> {
pub fn configure(
meta: &mut ConstraintSystem<F>,
q_enable: impl Fn(&mut VirtualCells<'_, F>) -> Expression<F> + Copy,
s_rlp1: Column<Advice>,
s_rlp2: Column<Advice>,
c_rlp1: Column<Advice>,
c_rlp2: Column<Advice>,
s_advices: [Column<Advice>; HASH_WIDTH],
acc: Column<Advice>,
acc_mult: Column<Advice>,
key_rlc: Column<Advice>,
key_rlc_mult: Column<Advice>,
r_table: Vec<Expression<F>>,
fixed_table: [Column<Fixed>; 3],
) -> AccountLeafKeyConfig {
let config = AccountLeafKeyConfig {};

meta.create_gate("account leaf key", |meta| {
let q_enable = q_enable(meta);
let mut constraints = vec![];

/*
[248,112,157,59,158,160,175,159,65,212,107,23,98,208,38,205,150,63,244,2,185,236,246,95,240,224,191,229,27,102,202,231,184,80
There are 112 bytes after the first two bytes.
157 means the key is 29 (157 - 128) bytes long.
*/

// TODO: RLP properties

let one = Expression::Constant(F::one());
let c248 = Expression::Constant(F::from(248));

let s_rlp1 = meta.query_advice(s_rlp1, Rotation::cur());
constraints.push((
"account leaf key s_rlp1 = 248",
q_enable.clone() * (s_rlp1.clone() - c248),
));

let mut ind = 0;
let mut expr = s_rlp1
+ meta.query_advice(s_rlp2, Rotation::cur())
* r_table[ind].clone();
ind += 1;

expr = expr
+ compute_rlc(
meta,
s_advices.to_vec(),
ind,
one.clone(),
0,
r_table.clone(),
);

let c_rlp1 = meta.query_advice(c_rlp1, Rotation::cur());
let c_rlp2 = meta.query_advice(c_rlp2, Rotation::cur());
expr = expr
+ c_rlp1.clone()
* r_table[R_TABLE_LEN - 1].clone()
* r_table[1].clone();
expr = expr
+ c_rlp2.clone()
* r_table[R_TABLE_LEN - 1].clone()
* r_table[2].clone();

let acc = meta.query_advice(acc, Rotation::cur());

constraints.push(("leaf key acc", q_enable.clone() * (expr - acc)));

constraints
});

// Note: key length is always in s_advices[0] here as opposed to storage
// key leaf where it can appear in s_rlp2 too. This is because account
// leaf contains nonce, balance, ... which makes it always longer than 55 bytes,
// which makes a RLP to start with 248 (s_rlp1) and having one byte (in s_rlp2)
// for the length of the remaining stream.
/*
for ind in 1..HASH_WIDTH {
key_len_lookup(
meta,
q_enable,
ind,
s_advices[0],
s_advices[ind],
fixed_table,
)
}
key_len_lookup(meta, q_enable, 32, s_advices[0], c_rlp1, fixed_table);
key_len_lookup(meta, q_enable, 33, s_advices[0], c_rlp2, fixed_table);
*/

// acc_mult corresponds to key length:
mult_diff_lookup(
meta,
q_enable,
3,
s_advices[0],
acc_mult,
fixed_table,
);

// No need to check key_rlc_mult as it's not used after this row.
meta.create_gate("Account leaf address RLC", |meta| {
let q_enable = q_enable(meta);
let mut constraints = vec![];

// key rlc is in the first branch node
let rot = -18;
let key_rlc_acc_start = meta.query_advice(key_rlc, Rotation(rot));
let key_mult_start = meta.query_advice(key_rlc_mult, Rotation(rot));
// sel1, sel2 is in init branch
let sel1 = meta.query_advice(
s_advices[IS_BRANCH_C16_POS - LAYOUT_OFFSET],
Rotation(rot - 1),
);
let sel2 = meta.query_advice(
s_advices[IS_BRANCH_C1_POS - LAYOUT_OFFSET],
Rotation(rot - 1),
);

let c32 = Expression::Constant(F::from(32));
let c48 = Expression::Constant(F::from(48));

// If sel1 = 1, we have nibble+48 in s_advices[0].
let s_advice1 = meta.query_advice(s_advices[1], Rotation::cur());
let mut key_rlc_acc = key_rlc_acc_start
+ (s_advice1.clone() - c48)
* key_mult_start.clone()
* sel1.clone();
let mut key_mult =
key_mult_start.clone() * r_table[0].clone() * sel1;
key_mult = key_mult + key_mult_start.clone() * sel2.clone(); // set to key_mult_start if sel2, stays key_mult if sel1

// If sel2 = 1, we have 32 in s_advices[0].
constraints.push((
"Account leaf key acc s_advice1",
q_enable.clone() * (s_advice1 - c32) * sel2,
));

let s_advices2 = meta.query_advice(s_advices[2], Rotation::cur());
key_rlc_acc = key_rlc_acc + s_advices2 * key_mult.clone();

for ind in 3..HASH_WIDTH {
let s = meta.query_advice(s_advices[ind], Rotation::cur());
key_rlc_acc = key_rlc_acc
+ s * key_mult.clone() * r_table[ind - 3].clone();
}

let c_rlp1 = meta.query_advice(c_rlp1, Rotation::cur());
let c_rlp2 = meta.query_advice(c_rlp2, Rotation::cur());
key_rlc_acc =
key_rlc_acc + c_rlp1 * key_mult.clone() * r_table[30].clone();
key_rlc_acc = key_rlc_acc + c_rlp2 * key_mult * r_table[31].clone();

let key_rlc = meta.query_advice(key_rlc, Rotation::cur());

// Key RLC is to be checked to verify that the proper key is used.
constraints.push((
"Account address RLC",
q_enable * (key_rlc_acc - key_rlc),
));

constraints
});

range_lookups(
meta,
q_enable,
s_advices.to_vec(),
FixedTableTag::Range256,
fixed_table,
);
// s_rlp1 is always 248 (checked above)
range_lookups(
meta,
q_enable,
[s_rlp2, c_rlp1, c_rlp2].to_vec(),
FixedTableTag::Range256,
fixed_table,
);

config
}

pub fn construct(config: AccountLeafKeyConfig) -> Self {
Self {
config,
_marker: PhantomData,
}
}
}

impl<F: FieldExt> Chip<F> for AccountLeafKeyChip<F> {
type Config = AccountLeafKeyConfig;
type Loaded = ();

fn config(&self) -> &Self::Config {
&self.config
}

fn loaded(&self) -> &Self::Loaded {
&()
}
}
Loading