aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2019-08-22 21:14:06 +0800
committerGitHub <noreply@github.com>2019-08-22 21:14:06 +0800
commit54b271a86dd748f3b0bcebeaf678dc34e0d6177a (patch)
tree0a24f87f9dde9144b956a6adc54ba72c18f5ccd3 /crypto
parentb90cdbaa79cfe438aab0f1389d35980f3d38ec84 (diff)
downloadgo-tangerine-54b271a86dd748f3b0bcebeaf678dc34e0d6177a.tar
go-tangerine-54b271a86dd748f3b0bcebeaf678dc34e0d6177a.tar.gz
go-tangerine-54b271a86dd748f3b0bcebeaf678dc34e0d6177a.tar.bz2
go-tangerine-54b271a86dd748f3b0bcebeaf678dc34e0d6177a.tar.lz
go-tangerine-54b271a86dd748f3b0bcebeaf678dc34e0d6177a.tar.xz
go-tangerine-54b271a86dd748f3b0bcebeaf678dc34e0d6177a.tar.zst
go-tangerine-54b271a86dd748f3b0bcebeaf678dc34e0d6177a.zip
crypto: add SignatureLength constant and use it everywhere (#19996)
Original change by @jpeletier
Diffstat (limited to 'crypto')
-rw-r--r--crypto/crypto.go9
-rw-r--r--crypto/signature_cgo.go16
-rw-r--r--crypto/signature_nocgo.go2
3 files changed, 18 insertions, 9 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go
index 4567fafc7..2869b4c19 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -34,6 +34,15 @@ import (
"golang.org/x/crypto/sha3"
)
+//SignatureLength indicates the byte length required to carry a signature with recovery id.
+const SignatureLength = 64 + 1 // 64 bytes ECDSA signature + 1 byte recovery id
+
+// RecoveryIDOffset points to the byte offset within the signature that contains the recovery id.
+const RecoveryIDOffset = 64
+
+// DigestLength sets the signature digest exact length
+const DigestLength = 32
+
var (
secp256k1N, _ = new(big.Int).SetString("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16)
secp256k1halfN = new(big.Int).Div(secp256k1N, big.NewInt(2))
diff --git a/crypto/signature_cgo.go b/crypto/signature_cgo.go
index aadf028d2..1fe84509e 100644
--- a/crypto/signature_cgo.go
+++ b/crypto/signature_cgo.go
@@ -47,24 +47,24 @@ func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
//
// This function is susceptible to chosen plaintext attacks that can leak
// information about the private key that is used for signing. Callers must
-// be aware that the given hash cannot be chosen by an adversery. Common
+// be aware that the given digest cannot be chosen by an adversery. Common
// solution is to hash any input before calculating the signature.
//
// The produced signature is in the [R || S || V] format where V is 0 or 1.
-func Sign(hash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
- if len(hash) != 32 {
- return nil, fmt.Errorf("hash is required to be exactly 32 bytes (%d)", len(hash))
+func Sign(digestHash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {
+ if len(digestHash) != DigestLength {
+ return nil, fmt.Errorf("hash is required to be exactly %d bytes (%d)", DigestLength, len(digestHash))
}
seckey := math.PaddedBigBytes(prv.D, prv.Params().BitSize/8)
defer zeroBytes(seckey)
- return secp256k1.Sign(hash, seckey)
+ return secp256k1.Sign(digestHash, seckey)
}
-// VerifySignature checks that the given public key created signature over hash.
+// VerifySignature checks that the given public key created signature over digest.
// The public key should be in compressed (33 bytes) or uncompressed (65 bytes) format.
// The signature should have the 64 byte [R || S] format.
-func VerifySignature(pubkey, hash, signature []byte) bool {
- return secp256k1.VerifySignature(pubkey, hash, signature)
+func VerifySignature(pubkey, digestHash, signature []byte) bool {
+ return secp256k1.VerifySignature(pubkey, digestHash, signature)
}
// DecompressPubkey parses a public key in the 33-byte compressed format.
diff --git a/crypto/signature_nocgo.go b/crypto/signature_nocgo.go
index 90d072cda..067d32e13 100644
--- a/crypto/signature_nocgo.go
+++ b/crypto/signature_nocgo.go
@@ -41,7 +41,7 @@ func Ecrecover(hash, sig []byte) ([]byte, error) {
// SigToPub returns the public key that created the given signature.
func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
// Convert to btcec input format with 'recovery id' v at the beginning.
- btcsig := make([]byte, 65)
+ btcsig := make([]byte, SignatureLength)
btcsig[0] = sig[64] + 27
copy(btcsig[1:], sig)