Skip to content

Commit e935729

Browse files
authored
use standard library sha256 implementation for Go 1.21 (#2309)
See rational and benchmarks in multiformats/go-multihash#173. Fixes: #2308
1 parent d2398ee commit e935729

File tree

8 files changed

+53
-8
lines changed

8 files changed

+53
-8
lines changed

core/crypto/ecdsa.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import (
1212

1313
pb "github.com/libp2p/go-libp2p/core/crypto/pb"
1414
"github.com/libp2p/go-libp2p/core/internal/catch"
15-
16-
"github.com/minio/sha256-simd"
15+
"github.com/libp2p/go-libp2p/internal/sha256"
1716
)
1817

1918
// ECDSAPrivateKey is an implementation of an ECDSA private key

core/crypto/key_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import (
1616
. "github.com/libp2p/go-libp2p/core/crypto"
1717
pb "github.com/libp2p/go-libp2p/core/crypto/pb"
1818
"github.com/libp2p/go-libp2p/core/test"
19+
"github.com/libp2p/go-libp2p/internal/sha256"
1920

2021
"github.com/decred/dcrd/dcrec/secp256k1/v4"
2122
secp256k1ecdsa "github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
22-
"github.com/minio/sha256-simd"
2323
)
2424

2525
func TestKeys(t *testing.T) {

core/crypto/rsa_go.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import (
1010

1111
pb "github.com/libp2p/go-libp2p/core/crypto/pb"
1212
"github.com/libp2p/go-libp2p/core/internal/catch"
13-
14-
"github.com/minio/sha256-simd"
13+
"github.com/libp2p/go-libp2p/internal/sha256"
1514
)
1615

1716
// RsaPrivateKey is a rsa private key

core/crypto/secp256k1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/decred/dcrd/dcrec/secp256k1/v4"
1111
"github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
12-
"github.com/minio/sha256-simd"
12+
"github.com/libp2p/go-libp2p/internal/sha256"
1313
)
1414

1515
// Secp256k1PrivateKey is a Secp256k1 private key

internal/sha256/post_go1_21.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build go1.21
2+
3+
// This package use build tags to select between github.com/minio/sha256-simd
4+
// for go1.20 and bellow and crypto/sha256 for go1.21 and above.
5+
// This is used because a fast SHANI implementation of sha256 is only avaiable
6+
// in the std for go1.21 and above. See https://go.dev/issue/50543.
7+
// TODO: Once go1.22 releases remove this package and replace all uses
8+
// with crypto/sha256 because the two supported version of go will have the fast
9+
// implementation.
10+
package sha256
11+
12+
import (
13+
"crypto/sha256"
14+
"hash"
15+
)
16+
17+
func Sum256(b []byte) [sha256.Size]byte {
18+
return sha256.Sum256(b)
19+
}
20+
21+
func New() hash.Hash {
22+
return sha256.New()
23+
}

internal/sha256/pre_go1_21.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build !go1.21
2+
3+
// This package use build tags to select between github.com/minio/sha256-simd
4+
// for go1.20 and bellow and crypto/sha256 for go1.21 and above.
5+
// This is used because a fast SHANI implementation of sha256 is only avaiable
6+
// in the std for go1.21 and above. See https://go.dev/issue/50543.
7+
// TODO: Once go1.22 releases remove this package and replace all uses
8+
// with crypto/sha256 because the two supported version of go will have the fast
9+
// implementation.
10+
package sha256
11+
12+
import (
13+
"hash"
14+
15+
"github.com/minio/sha256-simd"
16+
)
17+
18+
func Sum256(b []byte) [sha256.Size]byte {
19+
return sha256.Sum256(b)
20+
}
21+
22+
func New() hash.Hash {
23+
return sha256.New()
24+
}

p2p/net/swarm/swarm_addr_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/libp2p/go-libp2p/core/peer"
1010
"github.com/libp2p/go-libp2p/core/peerstore"
1111
"github.com/libp2p/go-libp2p/core/test"
12+
"github.com/libp2p/go-libp2p/internal/sha256"
1213
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
1314
"github.com/libp2p/go-libp2p/p2p/net/swarm"
1415
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
@@ -18,7 +19,6 @@ import (
1819
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
1920
webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"
2021

21-
"github.com/minio/sha256-simd"
2222
ma "github.com/multiformats/go-multiaddr"
2323
"github.com/multiformats/go-multibase"
2424
"github.com/multiformats/go-multihash"

p2p/security/noise/handshake.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212

1313
"github.com/libp2p/go-libp2p/core/crypto"
1414
"github.com/libp2p/go-libp2p/core/peer"
15+
"github.com/libp2p/go-libp2p/internal/sha256"
1516
"github.com/libp2p/go-libp2p/p2p/security/noise/pb"
1617

1718
"github.com/flynn/noise"
1819
pool "github.com/libp2p/go-buffer-pool"
19-
"github.com/minio/sha256-simd"
2020
"google.golang.org/protobuf/proto"
2121
)
2222

0 commit comments

Comments
 (0)