Skip to content

Commit

Permalink
use standard library sha256 implementation for Go 1.21 (#2309)
Browse files Browse the repository at this point in the history
See rational and benchmarks in multiformats/go-multihash#173.

Fixes: #2308
  • Loading branch information
Jorropo authored Aug 17, 2023
1 parent d2398ee commit e935729
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 8 deletions.
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

0 comments on commit e935729

Please sign in to comment.