go-ethereum/trie/utils/verkle_test.go
Guillaume Ballet 9db5d5c357 port kaustinen code on top of shapella-rebased branch
activate proof generation on fork  + remove code dups

use go-verkle's post-state API to verify proofs (#262)

use prague as the verkle activation fork (#263)

upgrade to latest go-ipa

activate verkle transition in "miner" (#265)

fix: do not force cancunTime upon verkle activation

workaround: do not use root translation in replay

workaround: deactivate overlay transition for now

fixes from trying to get the devnet to work (#267)

this line was left out from the previous commit

upgrade to go-verkle with fixed newvalue serialization

fix: ensure point cache isn't nil in copy (#268)

fix: dependency cycle in tests (#269)

upgrade to latest go-verkle

fix: write trie preimage data to db (#274)

fix: zero-root in produced block + sync (#275)

upgrade go-ipa

fix build

fix typo

include review feedback

add switch to add proofs to blocks (#278)

add fee recipient to witness (#279)

touch all fields in withdrawal account header (#277)
2024-05-08 13:25:14 +02:00

95 lines
2.4 KiB
Go

// Copyright 2022 go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package utils
import (
"crypto/sha256"
"encoding/hex"
"math/big"
"math/rand"
"testing"
"github.com/gballet/go-verkle"
"github.com/holiman/uint256"
)
func TestGetTreeKey(t *testing.T) {
var addr [32]byte
for i := 0; i < 16; i++ {
addr[1+2*i] = 0xff
}
n := uint256.NewInt(1)
n = n.Lsh(n, 129)
n.Add(n, uint256.NewInt(3))
tk := GetTreeKey(addr[:], n, 1)
got := hex.EncodeToString(tk)
exp := "f42f932f43faf5d14b292b9009c45c28da61dbf66e20dbedc2e02dfd64ff5a01"
if got != exp {
t.Fatalf("Generated trie key is incorrect: %s != %s", got, exp)
}
}
func TestConstantPoint(t *testing.T) {
var expectedPoly [1]verkle.Fr
cfg := verkle.GetConfig()
verkle.FromLEBytes(&expectedPoly[0], []byte{2, 64})
expected := cfg.CommitToPoly(expectedPoly[:], 1)
if !expected.Equal(getTreePolyIndex0Point) {
t.Fatalf("Marshalled constant value is incorrect: %x != %x", expected.Bytes(), getTreePolyIndex0Point.Bytes())
}
}
func BenchmarkPedersenHash(b *testing.B) {
var addr, v [32]byte
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
rand.Read(v[:])
rand.Read(addr[:])
GetTreeKeyCodeSize(addr[:])
}
}
func sha256GetTreeKeyCodeSize(addr []byte) []byte {
digest := sha256.New()
digest.Write(addr)
treeIndexBytes := new(big.Int).Bytes()
var payload [32]byte
copy(payload[:len(treeIndexBytes)], treeIndexBytes)
digest.Write(payload[:])
h := digest.Sum(nil)
h[31] = CodeKeccakLeafKey
return h
}
func BenchmarkSha256Hash(b *testing.B) {
var addr, v [32]byte
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
rand.Read(v[:])
rand.Read(addr[:])
sha256GetTreeKeyCodeSize(addr[:])
}
}