Skip to content

Commit

Permalink
test: add additional test for crypto, merkle (berachain#1651)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoank101 authored Jun 29, 2024
1 parent 02ac4cb commit 12998fb
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 3 deletions.
110 changes: 110 additions & 0 deletions mod/primitives/pkg/crypto/hasher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2024, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

package crypto_test

import (
"crypto/md5"
"testing"

"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto"
"github.com/stretchr/testify/require"
)

func TestCombi(t *testing.T) {
// Initialize the hasher function
hashFunc := func(data []byte) [32]byte {
var result [32]byte
hash := md5.Sum(data)
copy(result[:], hash[:])
return result
}
hasher := crypto.NewHasher[[32]byte](hashFunc)

tests := []struct {
name string
a, b [32]byte
expected [32]byte
}{
{
name: "Simple combination",
a: [32]byte{1, 2, 3, 4},
b: [32]byte{5, 6, 7, 8},
expected: [32]uint8{0xa3, 0x56, 0x78, 0x7f, 0x44, 0x4f, 0x5c, 0xa3,
0x51, 0x4a, 0xfd, 0x73, 0x23, 0x18, 0xa7, 0x40},
},
{
name: "Another combination",
a: [32]byte{9, 10, 11, 12},
b: [32]byte{13, 14, 15, 16},
expected: [32]uint8{0xa, 0x63, 0x9a, 0x14, 0x35, 0xed, 0x3d, 0x9a,
0x39, 0xfa, 0x9a, 0xa4, 0x60, 0xdd, 0xda},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := hasher.Combi(tc.a, tc.b)
require.Equal(t, tc.expected, result,
"TestCase %s", tc.name)
})
}
}

func TestMixIn(t *testing.T) {
// Initialize the hasher function
hashFunc := func(data []byte) [32]byte {
var result [32]byte
hash := md5.Sum(data)
copy(result[:], hash[:])
return result
}
hasher := crypto.NewHasher[[32]byte](hashFunc)

tests := []struct {
name string
a [32]byte
i uint64
expected [32]byte
}{
{
name: "MixIn with integer 1",
a: [32]byte{1, 2, 3, 4},
i: 1,
expected: [32]uint8{0x90, 0xde, 0x5, 0xc9, 0x96, 0x7a, 0xc0, 0xdb,
0x74, 0xf2, 0x8e, 0xf1, 0xd4, 0x62, 0xaf, 0x4d},
},
{
name: "MixIn with integer 2",
a: [32]byte{5, 6, 7, 8},
i: 2,
expected: [32]uint8{0xaa, 0x93, 0x16, 0x9f, 0x1c, 0x2d, 0xc5, 0xb8,
0xaa, 0x40, 0x39, 0x5, 0xd7, 0x80, 0x6f, 0xa9},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := hasher.MixIn(tc.a, tc.i)
require.Equal(t, tc.expected, result,
"TestCase %s", tc.name)
})
}
}
80 changes: 77 additions & 3 deletions mod/primitives/pkg/merkle/hasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ func Test_GoHashTreeHashConformance(t *testing.T) {
merkle.MinParallelizationSize / 2,
false,
},
{"AtMinParallelizationSize", merkle.MinParallelizationSize, false},
{"AtMinParallelizationSize",
merkle.MinParallelizationSize, false},
{
"AboveMinParallelizationSize",
merkle.MinParallelizationSize * 2,
Expand All @@ -192,7 +193,8 @@ func Test_GoHashTreeHashConformance(t *testing.T) {
merkle.MinParallelizationSize - 2,
false,
},
{"TestOddLength", merkle.MinParallelizationSize + 1, true},
{"TestOddLength",
merkle.MinParallelizationSize + 1, true},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -229,6 +231,7 @@ func TestBuildParentTreeRootsWithNRoutines_DivisionByZero(t *testing.T) {
require.NoError(
t,
err,

"BuildParentTreeRootsWithNRoutines should handle n=0 without error",
)
}
Expand Down Expand Up @@ -264,7 +267,8 @@ func requireGoHashTreeEquivalence(

// Check for errors
if !expectError {
require.NoError(t, err1, "BuildParentTreeRootsWithNRoutines failed")
require.NoError(t, err1,
"BuildParentTreeRootsWithNRoutines failed")
require.NoError(t, err2, "gohashtree.Hash failed")
} else {
if err1 == nil && err2 == nil {
Expand Down Expand Up @@ -372,3 +376,73 @@ func TestNewRootWithDepth(t *testing.T) {
})
}
}

func TestNewRootWithMaxLeaves(t *testing.T) {
tests := []struct {
name string
leaves [][32]byte
limit uint64
wantErr bool
errMsg string
expected [32]byte
}{
{
name: "Empty leaves",
leaves: [][32]byte{},
limit: 0,
wantErr: false,
expected: zero.Hashes[0],
},
{
name: "One leaf",
leaves: [][32]byte{createDummyLeaf(1)},
limit: 1,
wantErr: false,
expected: createDummyLeaf(1),
},
{
name: "Exceeds limit",
leaves: make([][32]byte, 11),
limit: 10,
wantErr: true,
errMsg: "number of leaves exceeds limit",
},
{
name: "Power of two leaves",
leaves: [][32]byte{
createDummyLeaf(1),
createDummyLeaf(2),
createDummyLeaf(3),
createDummyLeaf(4),
},
limit: 4,
wantErr: false,
expected: [32]uint8{
0xbf, 0xe3, 0xc6, 0x65, 0xd2, 0xe5, 0x61, 0xf1, 0x3b, 0x30, 0x60,
0x6c, 0x58, 0xc, 0xb7, 0x3, 0xb2, 0x4, 0x12, 0x87, 0xe2, 0x12, 0xad,
0xe1, 0x10, 0xf0, 0xbf, 0xd8, 0x56, 0x3e, 0x21, 0xbb},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hasher := crypto.NewHasher[[32]byte](sha256.Hash)
rootHasher := merkle.NewRootHasher[[32]byte](
hasher, merkle.BuildParentTreeRoots,
)
root, err := rootHasher.NewRootWithMaxLeaves(tt.leaves,
math.U64(tt.limit))
if tt.wantErr {
require.Error(t, err,
"Expected error in test case %s", tt.name)
require.Equal(t, tt.errMsg, err.Error(),
"Error message mismatch in test case %s", tt.name)
} else {
require.NoError(t, err,
"Unexpected error in test case %s", tt.name)
require.Equal(t, tt.expected, root,
"Root mismatch in test case %s", tt.name)
}
})
}
}

0 comments on commit 12998fb

Please sign in to comment.