Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new version of gohashtree #11

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
30 changes: 12 additions & 18 deletions hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,50 +485,44 @@ 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[:]
}

// 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.
Expand Down
28 changes: 3 additions & 25 deletions vendor/github.com/prysmaticlabs/gohashtree/hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down