From 3555824534c3a64b36f6931aed81340d9075b4a5 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 23 Jul 2024 20:06:48 +0200 Subject: [PATCH 1/3] add karalabe-SSZ serialization of VerkleProof --- gen_ipa_proof_ssz.go | 17 +++++++++++++++++ gen_verkle_proof_ssz.go | 37 +++++++++++++++++++++++++++++++++++++ go.mod | 15 ++++++++++++--- go.sum | 29 +++++++++++++++++++++++++---- proof_ipa.go | 12 ++++++++---- proof_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 139 insertions(+), 11 deletions(-) create mode 100644 gen_ipa_proof_ssz.go create mode 100644 gen_verkle_proof_ssz.go diff --git a/gen_ipa_proof_ssz.go b/gen_ipa_proof_ssz.go new file mode 100644 index 00000000..d66905e9 --- /dev/null +++ b/gen_ipa_proof_ssz.go @@ -0,0 +1,17 @@ +// Code generated by github.com/karalabe/ssz. DO NOT EDIT. + +package verkle + +import "github.com/karalabe/ssz" + +// SizeSSZ returns the total size of the static ssz object. +func (obj *IPAProof) SizeSSZ() uint32 { + return 8*32 + 8*32 + 32 +} + +// DefineSSZ defines how an object is encoded/decoded. +func (obj *IPAProof) DefineSSZ(codec *ssz.Codec) { + ssz.DefineUnsafeArrayOfStaticBytes(codec, obj.CL[:]) // Field (0) - CL - 256 bytes + ssz.DefineUnsafeArrayOfStaticBytes(codec, obj.CR[:]) // Field (1) - CR - 256 bytes + ssz.DefineStaticBytes(codec, &obj.FinalEvaluation) // Field (2) - FinalEvaluation - 32 bytes +} diff --git a/gen_verkle_proof_ssz.go b/gen_verkle_proof_ssz.go new file mode 100644 index 00000000..f6345927 --- /dev/null +++ b/gen_verkle_proof_ssz.go @@ -0,0 +1,37 @@ +// Code generated by github.com/karalabe/ssz. DO NOT EDIT. + +package verkle + +import "github.com/karalabe/ssz" + +// Cached static size computed on package init. +var staticSizeCacheVerkleProof = 4 + 4 + 4 + 32 + (*IPAProof)(nil).SizeSSZ() + +// SizeSSZ returns either the static size of the object if fixed == true, or +// the total size otherwise. +func (obj *VerkleProof) SizeSSZ(fixed bool) uint32 { + var size = uint32(staticSizeCacheVerkleProof) + if fixed { + return size + } + size += ssz.SizeSliceOfStaticBytes(obj.OtherStems) + size += ssz.SizeDynamicBytes(obj.DepthExtensionPresent) + size += ssz.SizeSliceOfStaticBytes(obj.CommitmentsByPath) + + return size +} + +// DefineSSZ defines how an object is encoded/decoded. +func (obj *VerkleProof) DefineSSZ(codec *ssz.Codec) { + // Define the static data (fields and dynamic offsets) + ssz.DefineSliceOfStaticBytesOffset(codec, &obj.OtherStems, 65536) // Offset (0) - OtherStems - 4 bytes + ssz.DefineDynamicBytesOffset(codec, &obj.DepthExtensionPresent, 65536) // Offset (1) - DepthExtensionPresent - 4 bytes + ssz.DefineSliceOfStaticBytesOffset(codec, &obj.CommitmentsByPath, 2162688) // Offset (2) - CommitmentsByPath - 4 bytes + ssz.DefineStaticBytes(codec, &obj.D) // Field (3) - D - 32 bytes + ssz.DefineStaticObject(codec, &obj.IPAProof) // Field (4) - IPAProof - ? bytes (IPAProof) + + // Define the dynamic data (fields) + ssz.DefineSliceOfStaticBytesContent(codec, &obj.OtherStems, 65536) // Field (0) - OtherStems - ? bytes + ssz.DefineDynamicBytesContent(codec, &obj.DepthExtensionPresent, 65536) // Field (1) - DepthExtensionPresent - ? bytes + ssz.DefineSliceOfStaticBytesContent(codec, &obj.CommitmentsByPath, 2162688) // Field (2) - CommitmentsByPath - ? bytes +} diff --git a/go.mod b/go.mod index 088ca77e..4985143c 100644 --- a/go.mod +++ b/go.mod @@ -1,18 +1,27 @@ module github.com/ethereum/go-verkle -go 1.19 +go 1.21 + +toolchain go1.22.1 require ( github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c github.com/davecgh/go-spew v1.1.1 - golang.org/x/sync v0.1.0 + github.com/karalabe/ssz v0.2.0 + golang.org/x/sync v0.7.0 ) require ( github.com/bits-and-blooms/bitset v1.7.0 // indirect github.com/consensys/bavard v0.1.13 // indirect github.com/consensys/gnark-crypto v0.12.1 // indirect + github.com/holiman/uint256 v1.3.0 // indirect + github.com/klauspost/cpuid/v2 v2.2.8 // indirect github.com/mmcloughlin/addchain v0.4.0 // indirect - golang.org/x/sys v0.9.0 // indirect + github.com/prysmaticlabs/go-bitfield v0.0.0-20240618144021-706c95b2dd15 // indirect + github.com/prysmaticlabs/gohashtree v0.0.4-beta // indirect + golang.org/x/mod v0.18.0 // indirect + golang.org/x/sys v0.22.0 // indirect + golang.org/x/tools v0.22.0 // indirect rsc.io/tmplfunc v0.0.3 // indirect ) diff --git a/go.sum b/go.sum index e16f4de4..921da732 100644 --- a/go.sum +++ b/go.sum @@ -9,16 +9,37 @@ github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c/go.mod h1:geZJ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= +github.com/holiman/uint256 v1.3.0 h1:4wdcm/tnd0xXdu7iS3ruNvxkWwrb4aeBQv19ayYn8F4= +github.com/holiman/uint256 v1.3.0/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E= +github.com/karalabe/ssz v0.2.0 h1:lwp2HDcj8I0YP+z456ZyyupcrWMWPSVAAoZRn/I3UEU= +github.com/karalabe/ssz v0.2.0/go.mod h1:SUFJO5R2VkUK3vT80pjfIB/g7eaQgSU2RhbuL8GOJq4= +github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM= +github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c= +github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8= +github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g= +github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM= github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY= github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU= github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prysmaticlabs/go-bitfield v0.0.0-20240618144021-706c95b2dd15 h1:lC8kiphgdOBTcbTvo8MwkvpKjO0SlAgjv4xIK5FGJ94= +github.com/prysmaticlabs/go-bitfield v0.0.0-20240618144021-706c95b2dd15/go.mod h1:8svFBIKKu31YriBG/pNizo9N0Jr9i5PQ+dFkxWg3x5k= +github.com/prysmaticlabs/gohashtree v0.0.4-beta h1:H/EbCuXPeTV3lpKeXGPpEV9gsUpkqOOVnWapUyeWro4= +github.com/prysmaticlabs/gohashtree v0.0.4-beta/go.mod h1:BFdtALS+Ffhg3lGQIHv9HDWuHS8cTvHZzrHWxwOtGOs= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= -golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= +golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA= +golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU= rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA= diff --git a/proof_ipa.go b/proof_ipa.go index 3f7af941..03093e20 100644 --- a/proof_ipa.go +++ b/proof_ipa.go @@ -36,7 +36,11 @@ import ( "github.com/crate-crypto/go-ipa/common" ) -const IPA_PROOF_DEPTH = 8 +const ( + MAX_STEMS = uint64(1 << 16) + MAX_COMMITMENTS_PER_STEM = uint64(1 << 33) + IPA_PROOF_DEPTH = 8 +) type IPAProof struct { CL [IPA_PROOF_DEPTH][32]byte `json:"cl"` @@ -45,9 +49,9 @@ type IPAProof struct { } type VerkleProof struct { - OtherStems [][StemSize]byte `json:"otherStems"` - DepthExtensionPresent []byte `json:"depthExtensionPresent"` - CommitmentsByPath [][32]byte `json:"commitmentsByPath"` + OtherStems [][StemSize]byte `json:"otherStems" ssz-max:"65536"` + DepthExtensionPresent []byte `json:"depthExtensionPresent" ssz-max:"65536"` + CommitmentsByPath [][32]byte `json:"commitmentsByPath" ssz-max:"2162688"` D [32]byte `json:"d"` IPAProof *IPAProof `json:"ipa_proof"` } diff --git a/proof_test.go b/proof_test.go index 8486551e..0715244c 100644 --- a/proof_test.go +++ b/proof_test.go @@ -34,6 +34,7 @@ import ( "testing" "github.com/crate-crypto/go-ipa/common" + "github.com/karalabe/ssz" ) func TestProofEmptyTree(t *testing.T) { @@ -1246,3 +1247,42 @@ func TestProveAbsenceInEmptyHalf(t *testing.T) { t.Fatalf("invalid number of extension status: %d", len(proof.ExtStatus)) } } + +func TestProofSSZEncoding(t *testing.T) { + t.Parallel() + + root := New() + + key1, _ := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000FF") + + if err := root.Insert(key1, fourtyKeyTest, nil); err != nil { + t.Fatalf("could not insert key: %v", err) + } + if err := root.Insert(key1, fourtyKeyTest, nil); err != nil { + t.Fatalf("could not insert key: %v", err) + } + + key2, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000100") + key3, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000000") + proof, _, _, _, err := MakeVerkleMultiProof(root, nil, keylist{key2, key3}, nil) + if err != nil { + t.Fatalf("error computing proof: %v", err) + } + vp, _, err := SerializeProof(proof) + if err != nil { + t.Fatalf("error serializing proof: %v", err) + } + bin := make([]byte, ssz.Size(vp)) + if err := ssz.EncodeToBytes(bin, vp); err != nil { + t.Fatalf("error encoding: %v", err) + } + + var vpdec VerkleProof + if err := ssz.DecodeFromBytes(bin, &vpdec); err != nil { + t.Fatalf("error decoding: %v", err) + } + + if vpdec.D != vp.D { + t.Fatalf("incorrect decoded evaluation point: %x != %x", vpdec.D, vp.D) + } +} From b72e8b69b0d901b1e8da22a531d54263ab450d06 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 23 Jul 2024 20:11:00 +0200 Subject: [PATCH 2/3] upgrade go version --- .github/workflows/go.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index ea959b8b..04e1538d 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -15,7 +15,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: 1.18 + go-version: 1.22 - name: Build run: go build -v ./... @@ -26,7 +26,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.18 + go-version: 1.22 - name: Download golangci-lint run: wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s latest - name: Lint @@ -41,6 +41,6 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.18 + go-version: 1.22 - name: Test run: go test -v -race ./... From 10337b89d4ddb22dcd32f0ffe9ea8f1768e472fd Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Fri, 26 Jul 2024 16:09:24 +0200 Subject: [PATCH 3/3] deprecation warning --- tree_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/tree_test.go b/tree_test.go index 594d6a75..9fc73b96 100644 --- a/tree_test.go +++ b/tree_test.go @@ -641,8 +641,6 @@ func benchmarkCommitNLeaves(b *testing.B, n int) { } func BenchmarkModifyLeaves(b *testing.B) { - mRand.Seed(time.Now().UnixNano()) // skipcq: GO-S1033 - n := 200000 toEdit := 10000 val := []byte{0}