Skip to content

Commit

Permalink
proofs: remove post-values in Multiproof (#297)
Browse files Browse the repository at this point in the history
* update proof generation

Signed-off-by: Ignacio Hagopian <[email protected]>

* simplification and comment

Signed-off-by: Ignacio Hagopian <[email protected]>

* update go-verkle

Signed-off-by: Ignacio Hagopian <[email protected]>

---------

Signed-off-by: Ignacio Hagopian <[email protected]>
  • Loading branch information
jsign authored and gballet committed May 8, 2024
1 parent 783e477 commit 26640f5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 34 deletions.
17 changes: 0 additions & 17 deletions consensus/beacon/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,23 +426,6 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
panic("invalid tree type")
}
if okpre && okpost {
// TODO: see if this can be captured at the witness
// level, like it used to.
for _, key := range keys {
// WORKAROUND: the post trie would normally not
// need to be searched for keys, as all of them
// were resolved during block execution.
// But since the prefetcher isn't currently used
// with verkle, the values that are read but not
// written to, are not resolved as they are read
// straight from the snapshot. They must be read
// in order to build the proof.
_, err = vtrpost.GetWithHashedKey(key)
if err != nil {
panic(err)
}
}

if len(keys) > 0 {
p, k, err = trie.ProveAndSerialize(vtrpre, vtrpost, keys, vtrpre.FlatdbNodeResolver)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func TestProcessVerkle(t *testing.T) {
//f.Write(buf.Bytes())
//fmt.Printf("root= %x\n", chain[0].Root())
// check the proof for the last block
err := trie.DeserializeAndVerifyVerkleProof(proofs[1], chain[0].Root().Bytes(), keyvals[1])
err := trie.DeserializeAndVerifyVerkleProof(proofs[1], chain[0].Root().Bytes(), chain[1].Root().Bytes(), keyvals[1])
if err != nil {
t.Fatal(err)
}
Expand Down
29 changes: 13 additions & 16 deletions trie/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,27 +336,16 @@ func ProveAndSerialize(pretrie, posttrie *VerkleTrie, keys [][]byte, resolver ve
return p, kvps, nil
}

type set = map[string]struct{}

func addKey(s set, key []byte) {
s[string(key)] = struct{}{}
}

func DeserializeAndVerifyVerkleProof(vp *verkle.VerkleProof, root []byte, statediff verkle.StateDiff) error {
rootC := new(verkle.Point)
rootC.SetBytes(root)

var others set = set{} // Mark when an "other" stem has been seen
func DeserializeAndVerifyVerkleProof(vp *verkle.VerkleProof, preStateRoot []byte, postStateRoot []byte, statediff verkle.StateDiff) error {
// TODO: check that `OtherStems` have expected length and values.

proof, err := verkle.DeserializeProof(vp, statediff)
if err != nil {
return fmt.Errorf("verkle proof deserialization error: %w", err)
}

for _, stem := range proof.PoaStems {
addKey(others, stem)
}

rootC := new(verkle.Point)
rootC.SetBytes(preStateRoot)
pretree, err := verkle.PreStateTreeFromProof(proof, rootC)
if err != nil {
return fmt.Errorf("error rebuilding the pre-tree from proof: %w", err)
Expand Down Expand Up @@ -385,12 +374,20 @@ func DeserializeAndVerifyVerkleProof(vp *verkle.VerkleProof, root []byte, stated
}
}

// TODO: this is necessary to verify that the post-values are the correct ones.
// But all this can be avoided with a even faster way. The EVM block execution can
// keep track of the written keys, and compare that list with this post-values list.
// This can avoid regenerating the post-tree which is somewhat expensive.
posttree, err := verkle.PostStateTreeFromStateDiff(pretree, statediff)
if err != nil {
return fmt.Errorf("error rebuilding the post-tree from proof: %w", err)
}
regeneratedPostTreeRoot := posttree.Commitment().Bytes()
if !bytes.Equal(regeneratedPostTreeRoot[:], postStateRoot) {
return fmt.Errorf("post tree root mismatch: %x != %x", regeneratedPostTreeRoot, postStateRoot)
}

return verkle.VerifyVerkleProofWithPreAndPostTrie(proof, pretree, posttree)
return verkle.VerifyVerkleProofWithPreState(proof, pretree)
}

// ChunkedCode represents a sequence of 32-bytes chunks of code (31 bytes of which
Expand Down

0 comments on commit 26640f5

Please sign in to comment.