Skip to content

Commit

Permalink
fix(hasher): Fix chunk count in merkleizeImpl (#147)
Browse files Browse the repository at this point in the history
* fixed chunk count

* space

* add test
  • Loading branch information
po-bera authored Mar 20, 2024
1 parent ea99d10 commit 879124e
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
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")
}
}

0 comments on commit 879124e

Please sign in to comment.