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

fix(hasher): Fix chunk count in merkleizeImpl #147

Merged
merged 3 commits into from
Mar 20, 2024
Merged
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
5 changes: 4 additions & 1 deletion hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,10 @@ func getDepth(d uint64) uint8 {
}

func (h *Hasher) merkleizeImpl(dst []byte, input []byte, limit uint64) []byte {
count := uint64(len(input) / 32)
// count is the number of 32 byte chunks from the input, after right-padding
// with zeroes to the next multiple of 32 bytes when the input is not aligned
// to a multiple of 32 bytes.
count := uint64((len(input) + 31) / 32)
if limit == 0 {
limit = count
} else if count > limit {
Expand Down
7 changes: 7 additions & 0 deletions sszgen/testcases/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package testcases

//go:generate go run ../main.go --path container.go

type Vec struct {
Values []uint64 `ssz-size:"6"`
}
83 changes: 83 additions & 0 deletions sszgen/testcases/container_encoding.go

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

25 changes: 25 additions & 0 deletions sszgen/testcases/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package testcases

import (
"bytes"
"testing"
)

func TestVectorContainer(t *testing.T) {
v := &Vec{
Values: []uint64{1, 2, 3, 4, 5, 6},
}
expectedHash := [32]byte{
0xac, 0x13, 0x6e, 0xdd, 0xa3, 0xbd, 0xd2, 0xe9,
0x49, 0xa1, 0x9a, 0x94, 0x5b, 0x1a, 0xc5, 0x54,
0xe4, 0xb6, 0x07, 0xd3, 0x39, 0xa4, 0x3c, 0x54,
0x0c, 0x33, 0x60, 0x98, 0xff, 0xf9, 0x7f, 0x2b,
}
h, err := v.HashTreeRoot()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(h[:], expectedHash[:]) {
t.Fatalf("hash mismatch")
}
}
Loading