Skip to content

Commit bc367d5

Browse files
committed
chore: replace blake2b implementation by golang.org/x/crypto
In 2016-12-13 d8e61c69ab46ca38328da2f4995abaf93b252290 golang.org/x/crypto gained a blake2b AVX implementation. There is also an AVX2 one. It is faster than what we are using right now. This commit allows for faster code and remove a dependency. For future improvements (like NEON) /x/crypto seems more maintained (it isn't an archived repo). benchmark old ns/op new ns/op delta BenchmarkSum128-12 252 140 -44.35% BenchmarkSum1K-12 1221 986 -19.24% benchmark old MB/s new MB/s speedup BenchmarkSum128-12 507.37 911.98 1.80x BenchmarkSum1K-12 838.70 1038.43 1.24x
1 parent 584ca60 commit bc367d5

File tree

3 files changed

+28
-39
lines changed

3 files changed

+28
-39
lines changed

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
module github.com/multiformats/go-multihash
22

33
require (
4-
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
54
github.com/minio/sha256-simd v1.0.0
65
github.com/mr-tron/base58 v1.2.0
76
github.com/multiformats/go-varint v0.0.6
87
github.com/spaolacci/murmur3 v1.1.0
9-
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83
8+
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
109
lukechampine.com/blake3 v1.1.6
1110
)
1211

1312
require (
1413
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
15-
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect
14+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
1615
)
1716

1817
go 1.17

go.sum

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
22
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
33
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
4-
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
5-
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
64
github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=
75
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
86
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
@@ -11,15 +9,15 @@ github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2
119
github.com/multiformats/go-varint v0.0.6/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
1210
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
1311
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
14-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
15-
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g=
16-
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
17-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
18-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
19-
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
20-
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 h1:46ULzRKLh1CwgRq2dC5SlBzEqqNCi8rreOZnNrbqcIY=
21-
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
22-
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
23-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
12+
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
13+
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
14+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
15+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
16+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
17+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
18+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
19+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
20+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
21+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
2422
lukechampine.com/blake3 v1.1.6 h1:H3cROdztr7RCfoaTpGZFQsrqvweFLrqS73j7L7cmR5c=
2523
lukechampine.com/blake3 v1.1.6/go.mod h1:tkKEOtDkNtklkXtLNEOGNq5tcV90tJiA1vAA12R78LA=

register/blake2/multihash_blake2.go

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
/*
2-
This package has no purpose except to perform registration of multihashes.
2+
This package has no purpose except to perform registration of multihashes.
33
4-
It is meant to be used as a side-effecting import, e.g.
4+
It is meant to be used as a side-effecting import, e.g.
55
6-
import (
7-
_ "github.com/multiformats/go-multihash/register/blake2"
8-
)
6+
import (
7+
_ "github.com/multiformats/go-multihash/register/blake2"
8+
)
99
10-
This package registers several multihashes for the blake2 family
11-
(both the 's' and the 'b' variants, and in a variety of sizes).
10+
This package registers several multihashes for the blake2 family
11+
(both the 's' and the 'b' variants, and in a variety of sizes).
1212
*/
1313
package blake2
1414

1515
import (
1616
"hash"
1717

18-
"github.com/minio/blake2b-simd"
18+
"golang.org/x/crypto/blake2b"
1919
"golang.org/x/crypto/blake2s"
2020

2121
"github.com/multiformats/go-multihash/core"
@@ -31,29 +31,21 @@ const (
3131
func init() {
3232
// blake2s
3333
// This package only enables support for 32byte (256 bit) blake2s.
34-
multihash.Register(blake2s_min+31, func() hash.Hash { h, _ := blake2s.New256(nil); return h })
34+
multihash.Register(blake2s_min+31, func() hash.Hash {
35+
h, err := blake2s.New256(nil)
36+
if err != nil {
37+
panic(err)
38+
}
39+
return h
40+
})
3541

3642
// blake2b
3743
// There's a whole range of these.
3844
for c := uint64(blake2b_min); c <= blake2b_max; c++ {
3945
size := int(c - blake2b_min + 1)
4046

41-
// special case these lengths to avoid allocations.
42-
switch size {
43-
case 32:
44-
multihash.Register(c, blake2b.New256)
45-
continue
46-
case 64:
47-
multihash.Register(c, blake2b.New512)
48-
continue
49-
}
50-
51-
// Ok, allocate away.
52-
// (The config object here being a pointer is a tad unfortunate,
53-
// but we manage amortize it away by making them just once anyway.)
54-
cfg := &blake2b.Config{Size: uint8(size)}
5547
multihash.Register(c, func() hash.Hash {
56-
hasher, err := blake2b.New(cfg)
48+
hasher, err := blake2b.New(size, nil)
5749
if err != nil {
5850
panic(err)
5951
}

0 commit comments

Comments
 (0)