From 057abd9e71dfb6e4fc24c6176a8b42f7003cbe97 Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 19 Jul 2022 17:13:35 +0200 Subject: [PATCH] Use new version of gohashtree --- go.mod | 2 +- go.sum | 4 +-- hasher.go | 30 ++++++++----------- .../prysmaticlabs/gohashtree/hash.go | 28 ++--------------- vendor/modules.txt | 14 +++++---- 5 files changed, 27 insertions(+), 51 deletions(-) diff --git a/go.mod b/go.mod index 36efa19..d7e588b 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/golang/snappy v0.0.3 github.com/minio/sha256-simd v1.0.0 github.com/mitchellh/mapstructure v1.3.2 - github.com/prysmaticlabs/gohashtree v0.0.0-20220517220438-192ee5ae6982 + github.com/prysmaticlabs/gohashtree v0.0.0-20220714111606-acbb2962fb48 golang.org/x/tools v0.1.8 gopkg.in/yaml.v2 v2.3.0 ) diff --git a/go.sum b/go.sum index 7e349b7..bfe3a19 100644 --- a/go.sum +++ b/go.sum @@ -7,8 +7,8 @@ github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM= github.com/mitchellh/mapstructure v1.3.2 h1:mRS76wmkOn3KkKAyXDu42V+6ebnXWIztFSYGN7GeoRg= github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= -github.com/prysmaticlabs/gohashtree v0.0.0-20220517220438-192ee5ae6982 h1:0izjBQiyBuyKJUe3QAnAmudVUX3AvylouGm5d+rz9bc= -github.com/prysmaticlabs/gohashtree v0.0.0-20220517220438-192ee5ae6982/go.mod h1:4pWaT30XoEx1j8KNJf3TV+E3mQkaufn7mf+jRNb/Fuk= +github.com/prysmaticlabs/gohashtree v0.0.0-20220714111606-acbb2962fb48 h1:VvdBN86teGCG5con7gm5ISmnVOAobIsFyG9LfcONH+I= +github.com/prysmaticlabs/gohashtree v0.0.0-20220714111606-acbb2962fb48/go.mod h1:4pWaT30XoEx1j8KNJf3TV+E3mQkaufn7mf+jRNb/Fuk= golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0= diff --git a/hasher.go b/hasher.go index 2324050..aab25a4 100755 --- a/hasher.go +++ b/hasher.go @@ -485,21 +485,11 @@ func (h *Hasher) merkleizeImpl(dst []byte, input []byte, limit uint64) []byte { } func merkleizeInput(input []byte, limit uint64) []byte { - chunkCount := (len(input) + 31) / 32 - chunks := make([][32]byte, chunkCount) - for i, j := 0, 0; j < chunkCount; i, j = i+32, j+1 { - if j == chunkCount-1 { - copy(chunks[j][:], input[i:]) - } else { - copy(chunks[j][:], input[i:i+32]) - } - } - var result [32]byte if limit == 0 { - result = merkleizeVector(chunks, uint64(chunkCount)) + result = merkleizeVector(input, uint64((len(input)+31)/32)) } else { - result = merkleizeVector(chunks, limit) + result = merkleizeVector(input, limit) } return result[:] @@ -507,28 +497,32 @@ func merkleizeInput(input []byte, limit uint64) []byte { // MerkleizeVector uses our optimized routine to hash a list of 32-byte // elements. -func merkleizeVector(elements [][32]byte, length uint64) [32]byte { +func merkleizeVector(elements []byte, length uint64) [32]byte { dep := depth(length) // Return zerohash at depth if len(elements) == 0 { return zeroHashesRaw[dep] } for i := uint8(0); i < dep; i++ { - layerLen := len(elements) + layerLen := (len(elements) + 31) / 32 oddNodeLength := layerLen%2 == 1 if oddNodeLength { zerohash := zeroHashesRaw[i] - elements = append(elements, zerohash) + elements = append(elements, zerohash[:]...) + layerLen += 1 } - outputLen := len(elements) / 2 + outputLen := layerLen / 2 // gohashtree concurrently overwrites elements err := gohashtree.Hash(elements, elements) if err != nil { panic(err) } - elements = elements[:outputLen] + elements = elements[:outputLen*32] } - return elements[0] + + var result [32]byte + copy(result[:], elements[:32]) + return result } // Depth retrieves the appropriate depth for the provided trie size. diff --git a/vendor/github.com/prysmaticlabs/gohashtree/hash.go b/vendor/github.com/prysmaticlabs/gohashtree/hash.go index 797bc10..5c54caa 100644 --- a/vendor/github.com/prysmaticlabs/gohashtree/hash.go +++ b/vendor/github.com/prysmaticlabs/gohashtree/hash.go @@ -23,31 +23,9 @@ SOFTWARE. */ package gohashtree -import ( - "fmt" -) +func _hash(digests *byte, p []byte, count uint32) -func _hash(digests *byte, p [][32]byte, count uint32) - -func Hash(digests [][32]byte, chunks [][32]byte) error { - if len(chunks) == 0 { - return nil - } - - if len(chunks)%2 == 1 { - return fmt.Errorf("odd number of chunks") - } - if len(digests) < len(chunks)/2 { - return fmt.Errorf("not enough digest length, need at least %v, got %v", len(chunks)/2, len(digests)) - } - if supportedCPU { - _hash(&digests[0][0], chunks, uint32(len(chunks)/2)) - } else { - sha256_1_generic(digests, chunks) - } +func Hash(digests []byte, chunks []byte) error { + _hash(&digests[0], chunks, uint32(len(chunks)/64)) return nil } - -func HashChunks(digests [][32]byte, chunks [][32]byte) { - _hash(&digests[0][0], chunks, uint32(len(chunks)/2)) -} diff --git a/vendor/modules.txt b/vendor/modules.txt index fe2493c..a2931fd 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -2,24 +2,27 @@ ## explicit github.com/golang/snappy # github.com/klauspost/cpuid/v2 v2.0.9 +## explicit; go 1.13 github.com/klauspost/cpuid/v2 # github.com/minio/sha256-simd v1.0.0 -## explicit +## explicit; go 1.13 github.com/minio/sha256-simd # github.com/mitchellh/mapstructure v1.3.2 -## explicit +## explicit; go 1.14 github.com/mitchellh/mapstructure -# github.com/prysmaticlabs/gohashtree v0.0.0-20220517220438-192ee5ae6982 -## explicit +# github.com/prysmaticlabs/gohashtree v0.0.0-20220714111606-acbb2962fb48 +## explicit; go 1.17 github.com/prysmaticlabs/gohashtree # golang.org/x/mod v0.5.1 +## explicit; go 1.17 golang.org/x/mod/internal/lazyregexp golang.org/x/mod/module golang.org/x/mod/semver # golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 +## explicit; go 1.17 golang.org/x/sys/execabs # golang.org/x/tools v0.1.8 -## explicit +## explicit; go 1.17 golang.org/x/tools/go/ast/astutil golang.org/x/tools/imports golang.org/x/tools/internal/event @@ -32,6 +35,7 @@ golang.org/x/tools/internal/gopathwalk golang.org/x/tools/internal/imports golang.org/x/tools/internal/typeparams # golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 +## explicit; go 1.11 golang.org/x/xerrors golang.org/x/xerrors/internal # gopkg.in/yaml.v2 v2.3.0