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(ssz): Minor merkle edge case handling #1636

Merged
merged 2 commits into from
Jun 27, 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
13 changes: 13 additions & 0 deletions mod/primitives/pkg/merkle/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ var (
// zero, which is invalid.
ErrZeroDepth = errors.New("depth must be greater than 0")

// ErrDepthExceedsLimitDepth indicates that the depth provided for the
// Merkle
// tree exceeds the specified limit depth.
ErrDepthExceedsLimitDepth = errors.New(
"depth exceeds the specified limit depth",
)

// ErrExceededDepth indicates that the provided depth exceeds the supported
// maximum depth for a Merkle tree.
ErrExceededDepth = errors.New("supported merkle tree depth exceeded")
Expand All @@ -53,4 +60,10 @@ var (
ErrMaxRootsExceeded = errors.New(
"number of roots exceeds the maximum allowed",
)

// ErrLeavesExceedsLimit is returned when the number of leaves exceeds the
// maximum allowed.
ErrLeavesExceedsLimit = errors.New(
"number of leaves exceeds the maximum allowed",
)
)
38 changes: 20 additions & 18 deletions mod/primitives/pkg/merkle/hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,15 @@ func (m *Hasher[RootT]) NewRootWithMaxLeaves(
limit math.U64,
) (RootT, error) {
count := math.U64(len(leaves))
if count > limit {
return zero.Hashes[0], errors.New("number of leaves exceeds limit")
}
if limit == 0 {
switch {
case count > limit:
return zero.Hashes[0], errors.Wrapf(
ErrLeavesExceedsLimit, "count: %d, limit: %d", count, limit,
)
case limit == 0:
return zero.Hashes[0], nil
}
if limit == 1 {
if count == 1 {
return leaves[0], nil
}
case limit == 1 && count == 1:
return leaves[0], nil
}

return m.NewRootWithDepth(
Expand All @@ -99,28 +98,31 @@ func (m *Hasher[RootT]) NewRootWithDepth(
depth uint8,
limitDepth uint8,
) (RootT, error) {
// Validate input.
if depth > limitDepth {
return zero.Hashes[0],
errors.Wrapf(
ErrDepthExceedsLimitDepth,
"depth %d exceeds limit depth %d", depth, limitDepth,
)
}

// Short-circuit to getting memory from the buffer.
if len(leaves) == 0 {
return zero.Hashes[limitDepth], nil
}

// Preallocate a single buffer large enough for the maximum layer size
// TODO: It seems that BuildParentTreeRoots has different behaviour
// when we pass leaves in directly.
buf := m.buffer.Get((len(leaves) + 1) / two)

var err error
for i := range depth {
layerLen := len(leaves)
if layerLen%two == 1 {
leaves = append(leaves, zero.Hashes[i])
}

newLayerSize := (layerLen + 1) / two
if err = m.hasher(buf[:newLayerSize], leaves); err != nil {
if err := m.hasher(leaves, leaves); err != nil {
return zero.Hashes[depth], err
}
leaves, buf = buf[:newLayerSize], leaves
newLayerSize := (layerLen + 1) / two
leaves = leaves[:newLayerSize]
}

// Handle the case where the tree is not full
Expand Down
15 changes: 13 additions & 2 deletions mod/primitives/pkg/merkle/hasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,26 @@ func TestNewRootWithDepth(t *testing.T) {
wantErr: false,
},
{
name: "odd number of leaves",
name: "not enough depth",
leaves: [][32]byte{
createDummyLeaf(1),
createDummyLeaf(2),
createDummyLeaf(3),
},
depth: 1,
expected: zero.Hashes[1],
wantErr: false,
wantErr: true,
},
{
name: "odd leaves",
leaves: [][32]byte{
createDummyLeaf(1),
createDummyLeaf(2),
createDummyLeaf(3),
},
depth: 2,
expected: zero.Hashes[1],
wantErr: true,
},
{
name: "hasher returns error",
Expand Down
Loading