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

use standard library sha256 implementation for Go 1.21 #2309

Merged
merged 1 commit into from
Aug 17, 2023
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
3 changes: 1 addition & 2 deletions core/crypto/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (

pb "github.com/libp2p/go-libp2p/core/crypto/pb"
"github.com/libp2p/go-libp2p/core/internal/catch"

"github.com/minio/sha256-simd"
"github.com/libp2p/go-libp2p/internal/sha256"
)

// ECDSAPrivateKey is an implementation of an ECDSA private key
Expand Down
2 changes: 1 addition & 1 deletion core/crypto/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
. "github.com/libp2p/go-libp2p/core/crypto"
pb "github.com/libp2p/go-libp2p/core/crypto/pb"
"github.com/libp2p/go-libp2p/core/test"
"github.com/libp2p/go-libp2p/internal/sha256"

"github.com/decred/dcrd/dcrec/secp256k1/v4"
secp256k1ecdsa "github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
"github.com/minio/sha256-simd"
)

func TestKeys(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions core/crypto/rsa_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (

pb "github.com/libp2p/go-libp2p/core/crypto/pb"
"github.com/libp2p/go-libp2p/core/internal/catch"

"github.com/minio/sha256-simd"
"github.com/libp2p/go-libp2p/internal/sha256"
)

// RsaPrivateKey is a rsa private key
Expand Down
2 changes: 1 addition & 1 deletion core/crypto/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
"github.com/minio/sha256-simd"
"github.com/libp2p/go-libp2p/internal/sha256"
)

// Secp256k1PrivateKey is a Secp256k1 private key
Expand Down
23 changes: 23 additions & 0 deletions internal/sha256/post_go1_21.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build go1.21

// This package use build tags to select between github.com/minio/sha256-simd
// for go1.20 and bellow and crypto/sha256 for go1.21 and above.
// This is used because a fast SHANI implementation of sha256 is only avaiable
// in the std for go1.21 and above. See https://go.dev/issue/50543.
// TODO: Once go1.22 releases remove this package and replace all uses
// with crypto/sha256 because the two supported version of go will have the fast
// implementation.
package sha256

import (
"crypto/sha256"
"hash"
)

func Sum256(b []byte) [sha256.Size]byte {
return sha256.Sum256(b)
}

func New() hash.Hash {
return sha256.New()
}
24 changes: 24 additions & 0 deletions internal/sha256/pre_go1_21.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build !go1.21

// This package use build tags to select between github.com/minio/sha256-simd
// for go1.20 and bellow and crypto/sha256 for go1.21 and above.
// This is used because a fast SHANI implementation of sha256 is only avaiable
// in the std for go1.21 and above. See https://go.dev/issue/50543.
// TODO: Once go1.22 releases remove this package and replace all uses
// with crypto/sha256 because the two supported version of go will have the fast
// implementation.
package sha256

import (
"hash"

"github.com/minio/sha256-simd"
)

func Sum256(b []byte) [sha256.Size]byte {
return sha256.Sum256(b)
}

func New() hash.Hash {
return sha256.New()
}
2 changes: 1 addition & 1 deletion p2p/net/swarm/swarm_addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/libp2p/go-libp2p/core/test"
"github.com/libp2p/go-libp2p/internal/sha256"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
"github.com/libp2p/go-libp2p/p2p/net/swarm"
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
Expand All @@ -18,7 +19,6 @@ import (
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"

"github.com/minio/sha256-simd"
ma "github.com/multiformats/go-multiaddr"
"github.com/multiformats/go-multibase"
"github.com/multiformats/go-multihash"
Expand Down
2 changes: 1 addition & 1 deletion p2p/security/noise/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (

"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/internal/sha256"
"github.com/libp2p/go-libp2p/p2p/security/noise/pb"

"github.com/flynn/noise"
pool "github.com/libp2p/go-buffer-pool"
"github.com/minio/sha256-simd"
"google.golang.org/protobuf/proto"
)

Expand Down