Skip to content

Commit

Permalink
utility to verify verkle proofs in blocks (ethereum#114)
Browse files Browse the repository at this point in the history
* utility to verify verkle proofs in blocks

* fix: reading from a leaf node with a different stem

* add new branch for workflow
  • Loading branch information
gballet committed Jul 7, 2022
1 parent 1daa043 commit 12761e4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master, verkle-trie-proof-in-block-rebased ]
branches: [ master, verkle-trie-proof-in-block-rebased, verkle-trie-post-merge ]

jobs:

Expand Down
44 changes: 44 additions & 0 deletions cmd/verkle/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"encoding/hex"
"fmt"
"os"

"github.com/crate-crypto/go-ipa/banderwagon"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
)

func main() {
if len(os.Args) != 3 {
fmt.Printf("Usage: %s <path to block file> <pre-root>\n", os.Args[0])
os.Exit(-1)
}

serializedBlock, err := os.ReadFile(os.Args[1])
if err != nil {
panic(err)
}

rootHex, err := hex.DecodeString(os.Args[2])
if err != nil {
panic(err)
}

var root banderwagon.Element
root.SetBytes(rootHex)

var block types.Block
rlp.DecodeBytes(serializedBlock, &block)

if len(block.Header().VerkleProof) == 0 {
panic("missing proof")
}

err = trie.DeserializeAndVerifyVerkleProof(block.Header().VerkleProof, &root, block.Header().VerkleKeyVals)
if err != nil {
fmt.Printf("error verifying proof: %v\n", err)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/fjl/gencodec v0.0.0-20220412091415-8bb9e558978c
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff
github.com/gballet/go-verkle v0.0.0-20220701145414-21d10bcd703f
github.com/gballet/go-verkle v0.0.0-20220707152227-8213973f8be2
github.com/go-stack/stack v1.8.0
github.com/golang-jwt/jwt/v4 v4.3.0
github.com/golang/protobuf v1.5.2
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqG
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww=
github.com/gballet/go-verkle v0.0.0-20220701145414-21d10bcd703f h1:B3a6/grCckvyEiyHR/+4ydo9Pdl0208DcDnGsc7REGE=
github.com/gballet/go-verkle v0.0.0-20220701145414-21d10bcd703f/go.mod h1:o/XfIXWi4/GqbQirfRm5uTbXMG5NpqxkxblnbZ+QM9I=
github.com/gballet/go-verkle v0.0.0-20220707083959-13b4fabe1d1a h1:KHyXbnUsGyGHesd8HNXflfZUKKnfMd1wr17F0pTnCiQ=
github.com/gballet/go-verkle v0.0.0-20220707083959-13b4fabe1d1a/go.mod h1:o/XfIXWi4/GqbQirfRm5uTbXMG5NpqxkxblnbZ+QM9I=
github.com/gballet/go-verkle v0.0.0-20220707142122-da8ca1103776 h1:FvPHeW38wyJqa4v2nch9EtgAW79EBg5EQ27agMLJAP4=
github.com/gballet/go-verkle v0.0.0-20220707142122-da8ca1103776/go.mod h1:o/XfIXWi4/GqbQirfRm5uTbXMG5NpqxkxblnbZ+QM9I=
github.com/gballet/go-verkle v0.0.0-20220707152227-8213973f8be2 h1:HnGTPJxmR6dV2HNE8yGbVRMKYHt4GznQ6Ou54kxqJLk=
github.com/gballet/go-verkle v0.0.0-20220707152227-8213973f8be2/go.mod h1:o/XfIXWi4/GqbQirfRm5uTbXMG5NpqxkxblnbZ+QM9I=
github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down
27 changes: 18 additions & 9 deletions trie/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package trie

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
Expand Down Expand Up @@ -228,11 +229,7 @@ func DeserializeAndVerifyVerkleProof(serialized []byte, rootC *verkle.Point, key
}

func deserializeVerkleProof(serialized []byte, rootC *verkle.Point, keyvals []verkle.KeyValuePair) (*verkle.Proof, []*verkle.Point, []byte, []*verkle.Fr, error) {
var (
indices []byte // List of zis
yis []*verkle.Fr // List of yis
others set // Mark when an "other" stem has been seen
)
var others set // Mark when an "other" stem has been seen

proof, err := verkle.DeserializeProof(serialized, keyvals)
if err != nil {
Expand All @@ -246,13 +243,25 @@ func deserializeVerkleProof(serialized []byte, rootC *verkle.Point, keyvals []ve
if len(proof.Keys) != len(proof.Values) {
return nil, nil, nil, nil, fmt.Errorf("keys and values are of different length %d != %d", len(proof.Keys), len(proof.Values))
}
if len(proof.Keys) != len(proof.ExtStatus) {
return nil, nil, nil, nil, fmt.Errorf("keys and values are of different length %d != %d", len(proof.Keys), len(proof.Values))

tree, err := verkle.TreeFromProof(proof, rootC)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("error rebuilding the tree from proof: %w", err)
}
for _, kv := range keyvals {
val, err := tree.Get(kv.Key, nil)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("could not find key %x in tree rebuilt from proof: %w", kv.Key, err)
}

if !bytes.Equal(val, kv.Value) {
return nil, nil, nil, nil, fmt.Errorf("could not find correct value at %x in tree rebuilt from proof: %x != %x", kv.Key, val, kv.Value)
}
}

_, err = verkle.TreeFromProof(proof, rootC)
pe, _, _ := tree.GetProofItems(proof.Keys)

return proof, proof.Cs, indices, yis, err
return proof, pe.Cis, pe.Zis, pe.Yis, nil
}

// Copy the values here so as to avoid an import cycle
Expand Down

0 comments on commit 12761e4

Please sign in to comment.