Skip to content
Open
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
19 changes: 19 additions & 0 deletions .github/workflows/bench.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: benchmark
on:
push:
branches:
- master
pull_request:
jobs:
bench:
runs-on: ubuntu-22.04
steps:
- uses: actions/setup-go@v5
with:
cache: false
go-version: '1.20'
- uses: actions/checkout@v4
- uses: n8maninger/action-golang-test@v2
with:
args: "-bench=.;-timeout=30m"

2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
cache: false
go-version: '1.20'
- name: golangci-lint
uses: golangci/golangci-lint-action@v5
uses: golangci/golangci-lint-action@v6
with:
version: 'latest'
args: '--timeout=60m'
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
*.so
*.dylib

# Jetbrains
.idea

# Test binary, build with `go test -c`
*.test

Expand Down
15 changes: 10 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: 2
# This file contains all available configuration options
# with their default values (in comments).
#
Expand Down Expand Up @@ -110,9 +111,14 @@ output:

# Sort results by: filepath, line and column.
# sort-results: true


# All available settings of specific linters.
formatters:
enable:
- gofmt
- gofumpt
- goimports
- golines

# All available settings of specific linters.
linters-settings:
gosec:
excludes:
Expand Down Expand Up @@ -185,6 +191,7 @@ linters-settings:
- name: var-naming
- name: waitgroup-by-value


linters:
# Enable all available linters.
# Default: false
Expand All @@ -197,10 +204,8 @@ linters:
- dupl
- exhaustive
- exhaustruct
- exportloopref
- forcetypeassert
- funlen
- gci
- gochecknoglobals
- gochecknoinits
- gocognit
Expand Down
61 changes: 61 additions & 0 deletions blake3/blake256.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright © 2019 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package blake2b provides hashing using the BLAKE 2b system.
package blake3

import (
"github.com/zeebo/blake3"
)

const _hashlength = 32

// BLAKE3 is the Blake3 hashing method.
type BLAKE3 struct{}

// New256 creates a new Blake3 hashing method.
func New256() *BLAKE3 {
return &BLAKE3{}
}

// HashLength returns the length of hashes generated by Hash() in bytes.
func (*BLAKE3) HashLength() int {
return _hashlength
}

// HashName returns the name of this hash.
func (*BLAKE3) HashName() string {
return "blake3-256"
}

// Hash generates a BLAKE2b hash from input byte arrays.
func (*BLAKE3) Hash(data ...[]byte) []byte {
var hash [_hashlength]byte
if len(data) == 1 {
hash = blake3.Sum256(data[0])
} else {
concatDataLen := 0
for _, d := range data {
concatDataLen += len(d)
}
concatData := make([]byte, concatDataLen)
curOffset := 0
for _, d := range data {
copy(concatData[curOffset:], d)
curOffset += len(d)
}
hash = blake3.Sum256(concatData)
}

return hash[:]
}
24 changes: 24 additions & 0 deletions blake3/blake256_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright © 2023 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package blake3

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestHashLength(t *testing.T) {
assert.Equal(t, _hashlength, New256().HashLength(), "incorrect hash length reported")
}
75 changes: 75 additions & 0 deletions blake3/blake256_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright © 2018, 2019 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package blake3_test

import (
"encoding/hex"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/wealdtech/go-merkletree/v2/blake3"
)

// _byteArray is a helper to turn a string in to a byte array
func _byteArray(input string) []byte {
x, err := hex.DecodeString(input)
if err != nil {
panic(err)
}
return x
}

func TestHash(t *testing.T) {
tests := []struct {
data []byte
output []byte
}{
{
data: _byteArray("e9e0083e456539e9f6336164cd98700e668178f98af147ef750eb90afcf2f637"),
output: _byteArray("92c7a270abba6545cff680c3452f1573b3b672d66f663b4c1d1d3ce7c35b5170"),
},
}

hash := blake3.New256()
assert.Equal(t, "blake3-256", hash.HashName())
for i, test := range tests {
output := hash.Hash(test.data)
assert.Equal(t, test.output, output, fmt.Sprintf("failed at test %d", i))
}
}

func TestMultiHash(t *testing.T) {
tests := []struct {
data1 []byte
data2 []byte
data3 []byte
data4 []byte
output []byte
}{
{ // 0
data1: _byteArray("e9e0083e456539e9"),
data2: _byteArray("f6336164cd98700e"),
data3: _byteArray("668178f98af147ef"),
data4: _byteArray("750eb90afcf2f637"),
output: _byteArray("92c7a270abba6545cff680c3452f1573b3b672d66f663b4c1d1d3ce7c35b5170"),
},
}

hash := blake3.New256()
for i, test := range tests {
output := hash.Hash(test.data1, test.data2, test.data3, test.data4)
assert.Equal(t, test.output, output, fmt.Sprintf("failed at test %d", i))
}
}
61 changes: 61 additions & 0 deletions blake3/blake512.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright © 2019 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package blake3 provides hashing using the BLAKE3 system.
package blake3

import (
"github.com/zeebo/blake3"
)

const _512hashlength = 64

// BLAKE3_512 is the Blake3 hashing method with 512 bits.
type BLAKE3_512 struct{}

// New512 creates a new Blake3 hashing method.
func New512() *BLAKE3_512 {
return &BLAKE3_512{}
}

// HashLength returns the length of hashes generated by Hash() in bytes.
func (*BLAKE3_512) HashLength() int {
return _512hashlength
}

// HashName returns the name of this hash.
func (*BLAKE3_512) HashName() string {
return "blake3-512"
}

// Hash generates a BLAKE2b hash from input byte arrays.
func (*BLAKE3_512) Hash(data ...[]byte) []byte {
var hash [_512hashlength]byte
if len(data) == 1 {
hash = blake3.Sum512(data[0])
} else {
concatDataLen := 0
for _, d := range data {
concatDataLen += len(d)
}
concatData := make([]byte, concatDataLen)
curOffset := 0
for _, d := range data {
copy(concatData[curOffset:], d)
curOffset += len(d)
}
hash = blake3.Sum512(concatData)
}

return hash[:]
}
24 changes: 24 additions & 0 deletions blake3/blake512_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright © 2023 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package blake3

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test512HashLength(t *testing.T) {
assert.Equal(t, _512hashlength, New512().HashLength(), "incorrect hash length reported")
}
65 changes: 65 additions & 0 deletions blake3/blake512_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright © 2018, 2019 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package blake3_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/wealdtech/go-merkletree/v2/blake3"
)

func Test512Hash(t *testing.T) {
tests := []struct {
data []byte
output []byte
}{
{
data: _byteArray("e9e0083e456539e9f6336164cd98700e668178f98af147ef750eb90afcf2f637"),
output: _byteArray("92c7a270abba6545cff680c3452f1573b3b672d66f663b4c1d1d3ce7c35b5170"),
},
}

hash := blake3.New256()
assert.Equal(t, "blake3-512", hash.HashName())
for i, test := range tests {
output := hash.Hash(test.data)
assert.Equal(t, test.output, output, fmt.Sprintf("failed at test %d", i))
}
}

func Test512MultiHash(t *testing.T) {
tests := []struct {
data1 []byte
data2 []byte
data3 []byte
data4 []byte
output []byte
}{
{ // 0
data1: _byteArray("e9e0083e456539e9"),
data2: _byteArray("f6336164cd98700e"),
data3: _byteArray("668178f98af147ef"),
data4: _byteArray("750eb90afcf2f637"),
output: _byteArray("92c7a270abba6545cff680c3452f1573b3b672d66f663b4c1d1d3ce7c35b5170"),
},
}

hash := blake3.New512()
for i, test := range tests {
output := hash.Hash(test.data1, test.data2, test.data3, test.data4)
assert.Equal(t, test.output, output, fmt.Sprintf("failed at test %d", i))
}
}
Loading