Skip to content

Commit

Permalink
crypto: replace noarg fmt.Errorf with errors.New (#27333)
Browse files Browse the repository at this point in the history
Signed-off-by: jsvisa <[email protected]>
  • Loading branch information
jsvisa authored May 24, 2023
1 parent b0095ee commit 21c87e0
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 18 deletions.
6 changes: 3 additions & 3 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) {

// The priv.D must < N
if priv.D.Cmp(secp256k1N) >= 0 {
return nil, fmt.Errorf("invalid private key, >=N")
return nil, errors.New("invalid private key, >=N")
}
// The priv.D must not be zero or negative.
if priv.D.Sign() <= 0 {
return nil, fmt.Errorf("invalid private key, zero or negative")
return nil, errors.New("invalid private key, zero or negative")
}

priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(d)
Expand Down Expand Up @@ -204,7 +204,7 @@ func LoadECDSA(file string) (*ecdsa.PrivateKey, error) {
if err != nil {
return nil, err
} else if n != len(buf) {
return nil, fmt.Errorf("key file too short, want 64 hex characters")
return nil, errors.New("key file too short, want 64 hex characters")
}
if err := checkKeyFileEnd(r); err != nil {
return nil, err
Expand Down
16 changes: 8 additions & 8 deletions crypto/ecies/ecies.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ import (
"crypto/hmac"
"crypto/subtle"
"encoding/binary"
"fmt"
"errors"
"hash"
"io"
"math/big"
)

var (
ErrImport = fmt.Errorf("ecies: failed to import key")
ErrInvalidCurve = fmt.Errorf("ecies: invalid elliptic curve")
ErrInvalidPublicKey = fmt.Errorf("ecies: invalid public key")
ErrSharedKeyIsPointAtInfinity = fmt.Errorf("ecies: shared key is point at infinity")
ErrSharedKeyTooBig = fmt.Errorf("ecies: shared key params are too big")
ErrImport = errors.New("ecies: failed to import key")
ErrInvalidCurve = errors.New("ecies: invalid elliptic curve")
ErrInvalidPublicKey = errors.New("ecies: invalid public key")
ErrSharedKeyIsPointAtInfinity = errors.New("ecies: shared key is point at infinity")
ErrSharedKeyTooBig = errors.New("ecies: shared key params are too big")
)

// PublicKey is a representation of an elliptic curve public key.
Expand Down Expand Up @@ -138,8 +138,8 @@ func (prv *PrivateKey) GenerateShared(pub *PublicKey, skLen, macLen int) (sk []b
}

var (
ErrSharedTooLong = fmt.Errorf("ecies: shared secret is too long")
ErrInvalidMessage = fmt.Errorf("ecies: invalid message")
ErrSharedTooLong = errors.New("ecies: shared secret is too long")
ErrInvalidMessage = errors.New("ecies: invalid message")
)

// NIST SP 800-56 Concatenation Key Derivation Function (see section 5.8.1).
Expand Down
4 changes: 2 additions & 2 deletions crypto/ecies/ecies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"errors"
"math/big"
"testing"

Expand All @@ -62,7 +62,7 @@ func TestKDF(t *testing.T) {
}
}

var ErrBadSharedKeys = fmt.Errorf("ecies: shared keys don't match")
var ErrBadSharedKeys = errors.New("ecies: shared keys don't match")

// cmpParams compares a set of ECIES parameters. We assume, as per the
// docs, that AES is the only supported symmetric encryption algorithm.
Expand Down
5 changes: 3 additions & 2 deletions crypto/ecies/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"crypto/elliptic"
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"
"hash"

Expand All @@ -47,8 +48,8 @@ import (

var (
DefaultCurve = ethcrypto.S256()
ErrUnsupportedECDHAlgorithm = fmt.Errorf("ecies: unsupported ECDH algorithm")
ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters")
ErrUnsupportedECDHAlgorithm = errors.New("ecies: unsupported ECDH algorithm")
ErrUnsupportedECIESParameters = errors.New("ecies: unsupported ECIES parameters")
ErrInvalidKeyLen = fmt.Errorf("ecies: invalid key size (> %d) in ECIESParams", maxKeyLen)
)

Expand Down
3 changes: 2 additions & 1 deletion crypto/signature_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package crypto
import (
"crypto/ecdsa"
"crypto/elliptic"
"errors"
"fmt"

"github.com/ethereum/go-ethereum/common/math"
Expand Down Expand Up @@ -72,7 +73,7 @@ func VerifySignature(pubkey, digestHash, signature []byte) bool {
func DecompressPubkey(pubkey []byte) (*ecdsa.PublicKey, error) {
x, y := secp256k1.DecompressPubkey(pubkey)
if x == nil {
return nil, fmt.Errorf("invalid public key")
return nil, errors.New("invalid public key")
}
return &ecdsa.PublicKey{X: x, Y: y, Curve: S256()}, nil
}
Expand Down
4 changes: 2 additions & 2 deletions crypto/signature_nocgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ func Sign(hash []byte, prv *ecdsa.PrivateKey) ([]byte, error) {
return nil, fmt.Errorf("hash is required to be exactly 32 bytes (%d)", len(hash))
}
if prv.Curve != btcec.S256() {
return nil, fmt.Errorf("private key curve is not secp256k1")
return nil, errors.New("private key curve is not secp256k1")
}
// ecdsa.PrivateKey -> btcec.PrivateKey
var priv btcec.PrivateKey
if overflow := priv.Key.SetByteSlice(prv.D.Bytes()); overflow || priv.Key.IsZero() {
return nil, fmt.Errorf("invalid private key")
return nil, errors.New("invalid private key")
}
defer priv.Zero()
sig, err := btc_ecdsa.SignCompact(&priv, hash, false) // ref uncompressed pubkey
Expand Down

0 comments on commit 21c87e0

Please sign in to comment.