Skip to content

Commit

Permalink
Upgrade go-verkle to its IPA version (ethereum#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
gballet committed Nov 12, 2021
1 parent 6a1f344 commit 66956ea
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
2 changes: 1 addition & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
receipts, logs, _, usedGas, err = bc.processor.Process(block, statedb, bc.vmConfig)
} else {
var leaves map[common.Hash]common.Hash
_, _, _, leaves, err = trie.DeserializeAndVerifyVerkleProof(block.Header().VerkleProof)
leaves, err = trie.DeserializeAndVerifyVerkleProof(block.Header().VerkleProof)
if err != nil {
return it.index, err
}
Expand Down
39 changes: 17 additions & 22 deletions trie/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie/utils"
"github.com/gballet/go-verkle"
"github.com/protolambda/go-kzg/bls"
)

// VerkleTrie is a wrapper around VerkleNode that implements the trie.Trie
Expand Down Expand Up @@ -105,11 +104,11 @@ func (trie *VerkleTrie) TryDelete(key []byte) error {
func (trie *VerkleTrie) Hash() common.Hash {
// TODO cache this value
rootC := trie.root.ComputeCommitment()
return bls.FrTo32(rootC)
return rootC.Bytes()
}

func nodeToDBKey(n verkle.VerkleNode) []byte {
ret := bls.FrTo32(n.ComputeCommitment())
ret := n.ComputeCommitment().Bytes()
return ret[:]
}

Expand Down Expand Up @@ -172,23 +171,19 @@ type KeyValuePair struct {
}

type verkleproof struct {
D *bls.G1Point
Y *bls.Fr
Σ *bls.G1Point
Proof *verkle.Proof

Cis []*bls.G1Point
Indices []uint
Yis []*bls.Fr
Cis []*verkle.Point
Indices []byte
Yis []*verkle.Fr

Leaves []KeyValuePair
}

func (trie *VerkleTrie) ProveAndSerialize(keys [][]byte, kv map[common.Hash][]byte) ([]byte, error) {
d, y, σ, cis, indices, yis := verkle.MakeVerkleMultiProof(trie.root, keys)
proof, cis, indices, yis := verkle.MakeVerkleMultiProof(trie.root, keys)
vp := verkleproof{
D: d,
Y: y,
Σ: σ,
Proof: proof,
Cis: cis,
Indices: indices,
Yis: yis,
Expand All @@ -204,29 +199,29 @@ func (trie *VerkleTrie) ProveAndSerialize(keys [][]byte, kv map[common.Hash][]by
return rlp.EncodeToBytes(vp)
}

func DeserializeAndVerifyVerkleProof(proof []byte) (*bls.G1Point, *bls.Fr, *bls.G1Point, map[common.Hash]common.Hash, error) {
d, y, σ, cis, indices, yis, leaves, err := deserializeVerkleProof(proof)
func DeserializeAndVerifyVerkleProof(serialized []byte) (map[common.Hash]common.Hash, error) {
proof, cis, indices, yis, leaves, err := deserializeVerkleProof(serialized)
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("could not deserialize proof: %w", err)
return nil, fmt.Errorf("could not deserialize proof: %w", err)
}
if !verkle.VerifyVerkleProof(d, σ, y, cis, indices, yis, verkle.GetKZGConfig()) {
return nil, nil, nil, nil, errInvalidProof
if !verkle.VerifyVerkleProof(proof, cis, indices, yis, verkle.GetConfig()) {
return nil, errInvalidProof
}

return d, y, σ, leaves, nil
return leaves, nil
}

func deserializeVerkleProof(proof []byte) (*bls.G1Point, *bls.Fr, *bls.G1Point, []*bls.G1Point, []uint, []*bls.Fr, map[common.Hash]common.Hash, error) {
func deserializeVerkleProof(proof []byte) (*verkle.Proof, []*verkle.Point, []byte, []*verkle.Fr, map[common.Hash]common.Hash, error) {
var vp verkleproof
err := rlp.DecodeBytes(proof, &vp)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("verkle proof deserialization error: %w", err)
return nil, nil, nil, nil, nil, fmt.Errorf("verkle proof deserialization error: %w", err)
}
leaves := make(map[common.Hash]common.Hash, len(vp.Leaves))
for _, kvp := range vp.Leaves {
leaves[common.BytesToHash(kvp.Key)] = common.BytesToHash(kvp.Value)
}
return vp.D, vp.Y, vp.Σ, vp.Cis, vp.Indices, vp.Yis, leaves, nil
return vp.Proof, vp.Cis, vp.Indices, vp.Yis, leaves, nil
}

// Copy the values here so as to avoid an import cycle
Expand Down
6 changes: 2 additions & 4 deletions trie/verkle_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/protolambda/go-kzg/bls"

//"github.com/ethereum/go-ethereum/rlp"
"github.com/gballet/go-verkle"
)

Expand Down Expand Up @@ -133,13 +131,13 @@ func (it *verkleNodeIterator) Error() error {

// Hash returns the hash of the current node.
func (it *verkleNodeIterator) Hash() common.Hash {
return bls.FrTo32(it.current.ComputeCommitment())
return it.current.ComputeCommitment().Bytes()
}

// Parent returns the hash of the parent of the current node. The hash may be the one
// grandparent if the immediate parent is an internal node with no hash.
func (it *verkleNodeIterator) Parent() common.Hash {
return bls.FrTo32(it.stack[len(it.stack)-1].Node.ComputeCommitment())
return it.stack[len(it.stack)-1].Node.ComputeCommitment().Bytes()
}

// Path returns the hex-encoded path to the current node.
Expand Down

0 comments on commit 66956ea

Please sign in to comment.