crypto: fix golint warnings (#16710)
This commit is contained in:
		@@ -35,8 +35,8 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
	secp256k1_N, _  = new(big.Int).SetString("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16)
 | 
						secp256k1N, _  = new(big.Int).SetString("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16)
 | 
				
			||||||
	secp256k1_halfN = new(big.Int).Div(secp256k1_N, big.NewInt(2))
 | 
						secp256k1halfN = new(big.Int).Div(secp256k1N, big.NewInt(2))
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Keccak256 calculates and returns the Keccak256 hash of the input data.
 | 
					// Keccak256 calculates and returns the Keccak256 hash of the input data.
 | 
				
			||||||
@@ -68,7 +68,7 @@ func Keccak512(data ...[]byte) []byte {
 | 
				
			|||||||
	return d.Sum(nil)
 | 
						return d.Sum(nil)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Creates an ethereum address given the bytes and the nonce
 | 
					// CreateAddress creates an ethereum address given the bytes and the nonce
 | 
				
			||||||
func CreateAddress(b common.Address, nonce uint64) common.Address {
 | 
					func CreateAddress(b common.Address, nonce uint64) common.Address {
 | 
				
			||||||
	data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
 | 
						data, _ := rlp.EncodeToBytes([]interface{}{b, nonce})
 | 
				
			||||||
	return common.BytesToAddress(Keccak256(data)[12:])
 | 
						return common.BytesToAddress(Keccak256(data)[12:])
 | 
				
			||||||
@@ -99,7 +99,7 @@ func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) {
 | 
				
			|||||||
	priv.D = new(big.Int).SetBytes(d)
 | 
						priv.D = new(big.Int).SetBytes(d)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// The priv.D must < N
 | 
						// The priv.D must < N
 | 
				
			||||||
	if priv.D.Cmp(secp256k1_N) >= 0 {
 | 
						if priv.D.Cmp(secp256k1N) >= 0 {
 | 
				
			||||||
		return nil, fmt.Errorf("invalid private key, >=N")
 | 
							return nil, fmt.Errorf("invalid private key, >=N")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// The priv.D must not be zero or negative.
 | 
						// The priv.D must not be zero or negative.
 | 
				
			||||||
@@ -184,11 +184,11 @@ func ValidateSignatureValues(v byte, r, s *big.Int, homestead bool) bool {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	// reject upper range of s values (ECDSA malleability)
 | 
						// reject upper range of s values (ECDSA malleability)
 | 
				
			||||||
	// see discussion in secp256k1/libsecp256k1/include/secp256k1.h
 | 
						// see discussion in secp256k1/libsecp256k1/include/secp256k1.h
 | 
				
			||||||
	if homestead && s.Cmp(secp256k1_halfN) > 0 {
 | 
						if homestead && s.Cmp(secp256k1halfN) > 0 {
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// Frontier: allow s to be in full N range
 | 
						// Frontier: allow s to be in full N range
 | 
				
			||||||
	return r.Cmp(secp256k1_N) < 0 && s.Cmp(secp256k1_N) < 0 && (v == 0 || v == 1)
 | 
						return r.Cmp(secp256k1N) < 0 && s.Cmp(secp256k1N) < 0 && (v == 0 || v == 1)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func PubkeyToAddress(p ecdsa.PublicKey) common.Address {
 | 
					func PubkeyToAddress(p ecdsa.PublicKey) common.Address {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -154,7 +154,7 @@ func TestValidateSignatureValues(t *testing.T) {
 | 
				
			|||||||
	minusOne := big.NewInt(-1)
 | 
						minusOne := big.NewInt(-1)
 | 
				
			||||||
	one := common.Big1
 | 
						one := common.Big1
 | 
				
			||||||
	zero := common.Big0
 | 
						zero := common.Big0
 | 
				
			||||||
	secp256k1nMinus1 := new(big.Int).Sub(secp256k1_N, common.Big1)
 | 
						secp256k1nMinus1 := new(big.Int).Sub(secp256k1N, common.Big1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// correct v,r,s
 | 
						// correct v,r,s
 | 
				
			||||||
	check(true, 0, one, one)
 | 
						check(true, 0, one, one)
 | 
				
			||||||
@@ -181,9 +181,9 @@ func TestValidateSignatureValues(t *testing.T) {
 | 
				
			|||||||
	// correct sig with max r,s
 | 
						// correct sig with max r,s
 | 
				
			||||||
	check(true, 0, secp256k1nMinus1, secp256k1nMinus1)
 | 
						check(true, 0, secp256k1nMinus1, secp256k1nMinus1)
 | 
				
			||||||
	// correct v, combinations of incorrect r,s at upper limit
 | 
						// correct v, combinations of incorrect r,s at upper limit
 | 
				
			||||||
	check(false, 0, secp256k1_N, secp256k1nMinus1)
 | 
						check(false, 0, secp256k1N, secp256k1nMinus1)
 | 
				
			||||||
	check(false, 0, secp256k1nMinus1, secp256k1_N)
 | 
						check(false, 0, secp256k1nMinus1, secp256k1N)
 | 
				
			||||||
	check(false, 0, secp256k1_N, secp256k1_N)
 | 
						check(false, 0, secp256k1N, secp256k1N)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// current callers ensures r,s cannot be negative, but let's test for that too
 | 
						// current callers ensures r,s cannot be negative, but let's test for that too
 | 
				
			||||||
	// as crypto package could be used stand-alone
 | 
						// as crypto package could be used stand-alone
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -77,7 +77,7 @@ func (BitCurve *BitCurve) Params() *elliptic.CurveParams {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// IsOnBitCurve returns true if the given (x,y) lies on the BitCurve.
 | 
					// IsOnCurve returns true if the given (x,y) lies on the BitCurve.
 | 
				
			||||||
func (BitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool {
 | 
					func (BitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool {
 | 
				
			||||||
	// y² = x³ + b
 | 
						// y² = x³ + b
 | 
				
			||||||
	y2 := new(big.Int).Mul(y, y) //y²
 | 
						y2 := new(big.Int).Mul(y, y) //y²
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -49,7 +49,7 @@ func randSig() []byte {
 | 
				
			|||||||
// tests for malleability
 | 
					// tests for malleability
 | 
				
			||||||
// highest bit of signature ECDSA s value must be 0, in the 33th byte
 | 
					// highest bit of signature ECDSA s value must be 0, in the 33th byte
 | 
				
			||||||
func compactSigCheck(t *testing.T, sig []byte) {
 | 
					func compactSigCheck(t *testing.T, sig []byte) {
 | 
				
			||||||
	var b int = int(sig[32])
 | 
						var b = int(sig[32])
 | 
				
			||||||
	if b < 0 {
 | 
						if b < 0 {
 | 
				
			||||||
		t.Errorf("highest bit is negative: %d", b)
 | 
							t.Errorf("highest bit is negative: %d", b)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -88,7 +88,7 @@ func VerifySignature(pubkey, hash, signature []byte) bool {
 | 
				
			|||||||
		return false
 | 
							return false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// Reject malleable signatures. libsecp256k1 does this check but btcec doesn't.
 | 
						// Reject malleable signatures. libsecp256k1 does this check but btcec doesn't.
 | 
				
			||||||
	if sig.S.Cmp(secp256k1_halfN) > 0 {
 | 
						if sig.S.Cmp(secp256k1halfN) > 0 {
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return sig.Verify(hash, key)
 | 
						return sig.Verify(hash, key)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user