From 66956ea1381b42b7e525e4b3e07d215e9f4968ab Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Thu, 4 Nov 2021 14:57:01 +0100 Subject: [PATCH] Upgrade go-verkle to its IPA version (#24) --- core/blockchain.go | 2 +- trie/verkle.go | 39 +++++++++++++++++---------------------- trie/verkle_iterator.go | 6 ++---- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index da67364eb6d8..6e7248dad8b5 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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 } diff --git a/trie/verkle.go b/trie/verkle.go index 2e5fdfbb95d4..d3499d9a8edc 100644 --- a/trie/verkle.go +++ b/trie/verkle.go @@ -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 @@ -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[:] } @@ -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, @@ -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 diff --git a/trie/verkle_iterator.go b/trie/verkle_iterator.go index 67cdf9c641ba..828116661c71 100644 --- a/trie/verkle_iterator.go +++ b/trie/verkle_iterator.go @@ -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" ) @@ -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.