crypto: add DecompressPubkey, VerifySignature (#15615)
We need those operations for p2p/enr. Also upgrade github.com/btcsuite/btcd/btcec to the latest version and improve BenchmarkSha3. The benchmark printed extra output that confused tools like benchstat and ignored N.
This commit is contained in:
20
vendor/github.com/btcsuite/btcd/btcec/README.md
generated
vendored
20
vendor/github.com/btcsuite/btcd/btcec/README.md
generated
vendored
@ -1,11 +1,9 @@
|
||||
btcec
|
||||
=====
|
||||
|
||||
[]
|
||||
(https://travis-ci.org/btcsuite/btcec) [![ISC License]
|
||||
(http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
|
||||
[]
|
||||
(http://godoc.org/github.com/btcsuite/btcd/btcec)
|
||||
[](https://travis-ci.org/btcsuite/btcec)
|
||||
[](http://copyfree.org)
|
||||
[](http://godoc.org/github.com/btcsuite/btcd/btcec)
|
||||
|
||||
Package btcec implements elliptic curve cryptography needed for working with
|
||||
Bitcoin (secp256k1 only for now). It is designed so that it may be used with the
|
||||
@ -27,23 +25,19 @@ $ go get -u github.com/btcsuite/btcd/btcec
|
||||
|
||||
## Examples
|
||||
|
||||
* [Sign Message]
|
||||
(http://godoc.org/github.com/btcsuite/btcd/btcec#example-package--SignMessage)
|
||||
* [Sign Message](http://godoc.org/github.com/btcsuite/btcd/btcec#example-package--SignMessage)
|
||||
Demonstrates signing a message with a secp256k1 private key that is first
|
||||
parsed form raw bytes and serializing the generated signature.
|
||||
|
||||
* [Verify Signature]
|
||||
(http://godoc.org/github.com/btcsuite/btcd/btcec#example-package--VerifySignature)
|
||||
* [Verify Signature](http://godoc.org/github.com/btcsuite/btcd/btcec#example-package--VerifySignature)
|
||||
Demonstrates verifying a secp256k1 signature against a public key that is
|
||||
first parsed from raw bytes. The signature is also parsed from raw bytes.
|
||||
|
||||
* [Encryption]
|
||||
(http://godoc.org/github.com/btcsuite/btcd/btcec#example-package--EncryptMessage)
|
||||
* [Encryption](http://godoc.org/github.com/btcsuite/btcd/btcec#example-package--EncryptMessage)
|
||||
Demonstrates encrypting a message for a public key that is first parsed from
|
||||
raw bytes, then decrypting it using the corresponding private key.
|
||||
|
||||
* [Decryption]
|
||||
(http://godoc.org/github.com/btcsuite/btcd/btcec#example-package--DecryptMessage)
|
||||
* [Decryption](http://godoc.org/github.com/btcsuite/btcd/btcec#example-package--DecryptMessage)
|
||||
Demonstrates decrypting a message using a private key that is first parsed
|
||||
from raw bytes.
|
||||
|
||||
|
12
vendor/github.com/btcsuite/btcd/btcec/btcec.go
generated
vendored
12
vendor/github.com/btcsuite/btcd/btcec/btcec.go
generated
vendored
@ -36,8 +36,9 @@ var (
|
||||
// interface from crypto/elliptic.
|
||||
type KoblitzCurve struct {
|
||||
*elliptic.CurveParams
|
||||
q *big.Int
|
||||
H int // cofactor of the curve.
|
||||
q *big.Int
|
||||
H int // cofactor of the curve.
|
||||
halfOrder *big.Int // half the order N
|
||||
|
||||
// byteSize is simply the bit size / 8 and is provided for convenience
|
||||
// since it is calculated repeatedly.
|
||||
@ -747,9 +748,9 @@ func NAF(k []byte) ([]byte, []byte) {
|
||||
}
|
||||
if carry {
|
||||
retPos[0] = 1
|
||||
return retPos, retNeg
|
||||
}
|
||||
|
||||
return retPos, retNeg
|
||||
return retPos[1:], retNeg[1:]
|
||||
}
|
||||
|
||||
// ScalarMult returns k*(Bx, By) where k is a big endian integer.
|
||||
@ -912,9 +913,10 @@ func initS256() {
|
||||
secp256k1.Gx = fromHex("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798")
|
||||
secp256k1.Gy = fromHex("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8")
|
||||
secp256k1.BitSize = 256
|
||||
secp256k1.H = 1
|
||||
secp256k1.q = new(big.Int).Div(new(big.Int).Add(secp256k1.P,
|
||||
big.NewInt(1)), big.NewInt(4))
|
||||
secp256k1.H = 1
|
||||
secp256k1.halfOrder = new(big.Int).Rsh(secp256k1.N, 1)
|
||||
|
||||
// Provided for convenience since this gets computed repeatedly.
|
||||
secp256k1.byteSize = secp256k1.BitSize / 8
|
||||
|
201
vendor/github.com/btcsuite/btcd/btcec/field.go
generated
vendored
201
vendor/github.com/btcsuite/btcd/btcec/field.go
generated
vendored
@ -100,10 +100,6 @@ const (
|
||||
// fieldPrimeWordOne is word one of the secp256k1 prime in the
|
||||
// internal field representation. It is used during negation.
|
||||
fieldPrimeWordOne = 0x3ffffbf
|
||||
|
||||
// primeLowBits is the lower 2*fieldBase bits of the secp256k1 prime in
|
||||
// its standard normalized form. It is used during modular reduction.
|
||||
primeLowBits = 0xffffefffffc2f
|
||||
)
|
||||
|
||||
// fieldVal implements optimized fixed-precision arithmetic over the
|
||||
@ -250,39 +246,15 @@ func (f *fieldVal) SetHex(hexString string) *fieldVal {
|
||||
// performs fast modular reduction over the secp256k1 prime by making use of the
|
||||
// special form of the prime.
|
||||
func (f *fieldVal) Normalize() *fieldVal {
|
||||
// The field representation leaves 6 bits of overflow in each
|
||||
// word so intermediate calculations can be performed without needing
|
||||
// to propagate the carry to each higher word during the calculations.
|
||||
// In order to normalize, first we need to "compact" the full 256-bit
|
||||
// value to the right and treat the additional 64 leftmost bits as
|
||||
// the magnitude.
|
||||
m := f.n[0]
|
||||
t0 := m & fieldBaseMask
|
||||
m = (m >> fieldBase) + f.n[1]
|
||||
t1 := m & fieldBaseMask
|
||||
m = (m >> fieldBase) + f.n[2]
|
||||
t2 := m & fieldBaseMask
|
||||
m = (m >> fieldBase) + f.n[3]
|
||||
t3 := m & fieldBaseMask
|
||||
m = (m >> fieldBase) + f.n[4]
|
||||
t4 := m & fieldBaseMask
|
||||
m = (m >> fieldBase) + f.n[5]
|
||||
t5 := m & fieldBaseMask
|
||||
m = (m >> fieldBase) + f.n[6]
|
||||
t6 := m & fieldBaseMask
|
||||
m = (m >> fieldBase) + f.n[7]
|
||||
t7 := m & fieldBaseMask
|
||||
m = (m >> fieldBase) + f.n[8]
|
||||
t8 := m & fieldBaseMask
|
||||
m = (m >> fieldBase) + f.n[9]
|
||||
t9 := m & fieldMSBMask
|
||||
m = m >> fieldMSBBits
|
||||
|
||||
// At this point, if the magnitude is greater than 0, the overall value
|
||||
// is greater than the max possible 256-bit value. In particular, it is
|
||||
// "how many times larger" than the max value it is. Since this field
|
||||
// is doing arithmetic modulo the secp256k1 prime, we need to perform
|
||||
// modular reduction over the prime.
|
||||
// The field representation leaves 6 bits of overflow in each word so
|
||||
// intermediate calculations can be performed without needing to
|
||||
// propagate the carry to each higher word during the calculations. In
|
||||
// order to normalize, we need to "compact" the full 256-bit value to
|
||||
// the right while propagating any carries through to the high order
|
||||
// word.
|
||||
//
|
||||
// Since this field is doing arithmetic modulo the secp256k1 prime, we
|
||||
// also need to perform modular reduction over the prime.
|
||||
//
|
||||
// Per [HAC] section 14.3.4: Reduction method of moduli of special form,
|
||||
// when the modulus is of the special form m = b^t - c, highly efficient
|
||||
@ -298,98 +270,87 @@ func (f *fieldVal) Normalize() *fieldVal {
|
||||
//
|
||||
// The algorithm presented in the referenced section typically repeats
|
||||
// until the quotient is zero. However, due to our field representation
|
||||
// we already know at least how many times we would need to repeat as
|
||||
// it's the value currently in m. Thus we can simply multiply the
|
||||
// magnitude by the field representation of the prime and do a single
|
||||
// iteration. Notice that nothing will be changed when the magnitude is
|
||||
// zero, so we could skip this in that case, however always running
|
||||
// regardless allows it to run in constant time.
|
||||
r := t0 + m*977
|
||||
t0 = r & fieldBaseMask
|
||||
r = (r >> fieldBase) + t1 + m*64
|
||||
t1 = r & fieldBaseMask
|
||||
r = (r >> fieldBase) + t2
|
||||
t2 = r & fieldBaseMask
|
||||
r = (r >> fieldBase) + t3
|
||||
t3 = r & fieldBaseMask
|
||||
r = (r >> fieldBase) + t4
|
||||
t4 = r & fieldBaseMask
|
||||
r = (r >> fieldBase) + t5
|
||||
t5 = r & fieldBaseMask
|
||||
r = (r >> fieldBase) + t6
|
||||
t6 = r & fieldBaseMask
|
||||
r = (r >> fieldBase) + t7
|
||||
t7 = r & fieldBaseMask
|
||||
r = (r >> fieldBase) + t8
|
||||
t8 = r & fieldBaseMask
|
||||
r = (r >> fieldBase) + t9
|
||||
t9 = r & fieldMSBMask
|
||||
// we already know to within one reduction how many times we would need
|
||||
// to repeat as it's the uppermost bits of the high order word. Thus we
|
||||
// can simply multiply the magnitude by the field representation of the
|
||||
// prime and do a single iteration. After this step there might be an
|
||||
// additional carry to bit 256 (bit 22 of the high order word).
|
||||
t9 := f.n[9]
|
||||
m := t9 >> fieldMSBBits
|
||||
t9 = t9 & fieldMSBMask
|
||||
t0 := f.n[0] + m*977
|
||||
t1 := (t0 >> fieldBase) + f.n[1] + (m << 6)
|
||||
t0 = t0 & fieldBaseMask
|
||||
t2 := (t1 >> fieldBase) + f.n[2]
|
||||
t1 = t1 & fieldBaseMask
|
||||
t3 := (t2 >> fieldBase) + f.n[3]
|
||||
t2 = t2 & fieldBaseMask
|
||||
t4 := (t3 >> fieldBase) + f.n[4]
|
||||
t3 = t3 & fieldBaseMask
|
||||
t5 := (t4 >> fieldBase) + f.n[5]
|
||||
t4 = t4 & fieldBaseMask
|
||||
t6 := (t5 >> fieldBase) + f.n[6]
|
||||
t5 = t5 & fieldBaseMask
|
||||
t7 := (t6 >> fieldBase) + f.n[7]
|
||||
t6 = t6 & fieldBaseMask
|
||||
t8 := (t7 >> fieldBase) + f.n[8]
|
||||
t7 = t7 & fieldBaseMask
|
||||
t9 = (t8 >> fieldBase) + t9
|
||||
t8 = t8 & fieldBaseMask
|
||||
|
||||
// At this point, the result will be in the range 0 <= result <=
|
||||
// prime + (2^64 - c). Therefore, one more subtraction of the prime
|
||||
// might be needed if the current result is greater than or equal to the
|
||||
// prime. The following does the final reduction in constant time.
|
||||
// Note that the if/else here intentionally does the bitwise OR with
|
||||
// zero even though it won't change the value to ensure constant time
|
||||
// between the branches.
|
||||
var mask int32
|
||||
lowBits := uint64(t1)<<fieldBase | uint64(t0)
|
||||
if lowBits < primeLowBits {
|
||||
mask |= -1
|
||||
// At this point, the magnitude is guaranteed to be one, however, the
|
||||
// value could still be greater than the prime if there was either a
|
||||
// carry through to bit 256 (bit 22 of the higher order word) or the
|
||||
// value is greater than or equal to the field characteristic. The
|
||||
// following determines if either or these conditions are true and does
|
||||
// the final reduction in constant time.
|
||||
//
|
||||
// Note that the if/else statements here intentionally do the bitwise
|
||||
// operators even when it won't change the value to ensure constant time
|
||||
// between the branches. Also note that 'm' will be zero when neither
|
||||
// of the aforementioned conditions are true and the value will not be
|
||||
// changed when 'm' is zero.
|
||||
m = 1
|
||||
if t9 == fieldMSBMask {
|
||||
m &= 1
|
||||
} else {
|
||||
mask |= 0
|
||||
m &= 0
|
||||
}
|
||||
if t2 < fieldBaseMask {
|
||||
mask |= -1
|
||||
if t2&t3&t4&t5&t6&t7&t8 == fieldBaseMask {
|
||||
m &= 1
|
||||
} else {
|
||||
mask |= 0
|
||||
m &= 0
|
||||
}
|
||||
if t3 < fieldBaseMask {
|
||||
mask |= -1
|
||||
if ((t0+977)>>fieldBase + t1 + 64) > fieldBaseMask {
|
||||
m &= 1
|
||||
} else {
|
||||
mask |= 0
|
||||
m &= 0
|
||||
}
|
||||
if t4 < fieldBaseMask {
|
||||
mask |= -1
|
||||
if t9>>fieldMSBBits != 0 {
|
||||
m |= 1
|
||||
} else {
|
||||
mask |= 0
|
||||
m |= 0
|
||||
}
|
||||
if t5 < fieldBaseMask {
|
||||
mask |= -1
|
||||
} else {
|
||||
mask |= 0
|
||||
}
|
||||
if t6 < fieldBaseMask {
|
||||
mask |= -1
|
||||
} else {
|
||||
mask |= 0
|
||||
}
|
||||
if t7 < fieldBaseMask {
|
||||
mask |= -1
|
||||
} else {
|
||||
mask |= 0
|
||||
}
|
||||
if t8 < fieldBaseMask {
|
||||
mask |= -1
|
||||
} else {
|
||||
mask |= 0
|
||||
}
|
||||
if t9 < fieldMSBMask {
|
||||
mask |= -1
|
||||
} else {
|
||||
mask |= 0
|
||||
}
|
||||
lowBits -= ^uint64(mask) & primeLowBits
|
||||
t0 = uint32(lowBits & fieldBaseMask)
|
||||
t1 = uint32((lowBits >> fieldBase) & fieldBaseMask)
|
||||
t2 = t2 & uint32(mask)
|
||||
t3 = t3 & uint32(mask)
|
||||
t4 = t4 & uint32(mask)
|
||||
t5 = t5 & uint32(mask)
|
||||
t6 = t6 & uint32(mask)
|
||||
t7 = t7 & uint32(mask)
|
||||
t8 = t8 & uint32(mask)
|
||||
t9 = t9 & uint32(mask)
|
||||
t0 = t0 + m*977
|
||||
t1 = (t0 >> fieldBase) + t1 + (m << 6)
|
||||
t0 = t0 & fieldBaseMask
|
||||
t2 = (t1 >> fieldBase) + t2
|
||||
t1 = t1 & fieldBaseMask
|
||||
t3 = (t2 >> fieldBase) + t3
|
||||
t2 = t2 & fieldBaseMask
|
||||
t4 = (t3 >> fieldBase) + t4
|
||||
t3 = t3 & fieldBaseMask
|
||||
t5 = (t4 >> fieldBase) + t5
|
||||
t4 = t4 & fieldBaseMask
|
||||
t6 = (t5 >> fieldBase) + t6
|
||||
t5 = t5 & fieldBaseMask
|
||||
t7 = (t6 >> fieldBase) + t7
|
||||
t6 = t6 & fieldBaseMask
|
||||
t8 = (t7 >> fieldBase) + t8
|
||||
t7 = t7 & fieldBaseMask
|
||||
t9 = (t8 >> fieldBase) + t9
|
||||
t8 = t8 & fieldBaseMask
|
||||
t9 = t9 & fieldMSBMask // Remove potential multiple of 2^256.
|
||||
|
||||
// Finally, set the normalized and reduced words.
|
||||
f.n[0] = t0
|
||||
|
63
vendor/github.com/btcsuite/btcd/btcec/genprecomps.go
generated
vendored
63
vendor/github.com/btcsuite/btcd/btcec/genprecomps.go
generated
vendored
@ -1,63 +0,0 @@
|
||||
// Copyright 2015 The btcsuite developers
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// This file is ignored during the regular build due to the following build tag.
|
||||
// It is called by go generate and used to automatically generate pre-computed
|
||||
// tables used to accelerate operations.
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fi, err := os.Create("secp256k1.go")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer fi.Close()
|
||||
|
||||
// Compress the serialized byte points.
|
||||
serialized := btcec.S256().SerializedBytePoints()
|
||||
var compressed bytes.Buffer
|
||||
w := zlib.NewWriter(&compressed)
|
||||
if _, err := w.Write(serialized); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
w.Close()
|
||||
|
||||
// Encode the compressed byte points with base64.
|
||||
encoded := make([]byte, base64.StdEncoding.EncodedLen(compressed.Len()))
|
||||
base64.StdEncoding.Encode(encoded, compressed.Bytes())
|
||||
|
||||
fmt.Fprintln(fi, "// Copyright (c) 2015 The btcsuite developers")
|
||||
fmt.Fprintln(fi, "// Use of this source code is governed by an ISC")
|
||||
fmt.Fprintln(fi, "// license that can be found in the LICENSE file.")
|
||||
fmt.Fprintln(fi)
|
||||
fmt.Fprintln(fi, "package btcec")
|
||||
fmt.Fprintln(fi)
|
||||
fmt.Fprintln(fi, "// Auto-generated file (see genprecomps.go)")
|
||||
fmt.Fprintln(fi, "// DO NOT EDIT")
|
||||
fmt.Fprintln(fi)
|
||||
fmt.Fprintf(fi, "var secp256k1BytePoints = %q\n", string(encoded))
|
||||
|
||||
a1, b1, a2, b2 := btcec.S256().EndomorphismVectors()
|
||||
fmt.Println("The following values are the computed linearly " +
|
||||
"independent vectors needed to make use of the secp256k1 " +
|
||||
"endomorphism:")
|
||||
fmt.Printf("a1: %x\n", a1)
|
||||
fmt.Printf("b1: %x\n", b1)
|
||||
fmt.Printf("a2: %x\n", a2)
|
||||
fmt.Printf("b2: %x\n", b2)
|
||||
}
|
9
vendor/github.com/btcsuite/btcd/btcec/pubkey.go
generated
vendored
9
vendor/github.com/btcsuite/btcd/btcec/pubkey.go
generated
vendored
@ -54,6 +54,15 @@ const (
|
||||
pubkeyHybrid byte = 0x6 // y_bit + x coord + y coord
|
||||
)
|
||||
|
||||
// IsCompressedPubKey returns true the the passed serialized public key has
|
||||
// been encoded in compressed format, and false otherwise.
|
||||
func IsCompressedPubKey(pubKey []byte) bool {
|
||||
// The public key is only compressed if it is the correct length and
|
||||
// the format (first byte) is one of the compressed pubkey values.
|
||||
return len(pubKey) == PubKeyBytesLenCompressed &&
|
||||
(pubKey[0]&^byte(0x1) == pubkeyCompressed)
|
||||
}
|
||||
|
||||
// ParsePubKey parses a public key for a koblitz curve from a bytestring into a
|
||||
// ecdsa.Publickey, verifying that it is valid. It supports compressed,
|
||||
// uncompressed and hybrid signature formats.
|
||||
|
2
vendor/github.com/btcsuite/btcd/btcec/secp256k1.go
generated
vendored
2
vendor/github.com/btcsuite/btcd/btcec/secp256k1.go
generated
vendored
File diff suppressed because one or more lines are too long
15
vendor/github.com/btcsuite/btcd/btcec/signature.go
generated
vendored
15
vendor/github.com/btcsuite/btcd/btcec/signature.go
generated
vendored
@ -29,10 +29,6 @@ type Signature struct {
|
||||
}
|
||||
|
||||
var (
|
||||
// Curve order and halforder, used to tame ECDSA malleability (see BIP-0062)
|
||||
order = new(big.Int).Set(S256().N)
|
||||
halforder = new(big.Int).Rsh(order, 1)
|
||||
|
||||
// Used in RFC6979 implementation when testing the nonce for correctness
|
||||
one = big.NewInt(1)
|
||||
|
||||
@ -51,8 +47,8 @@ var (
|
||||
func (sig *Signature) Serialize() []byte {
|
||||
// low 'S' malleability breaker
|
||||
sigS := sig.S
|
||||
if sigS.Cmp(halforder) == 1 {
|
||||
sigS = new(big.Int).Sub(order, sigS)
|
||||
if sigS.Cmp(S256().halfOrder) == 1 {
|
||||
sigS = new(big.Int).Sub(S256().N, sigS)
|
||||
}
|
||||
// Ensure the encoded bytes for the r and s values are canonical and
|
||||
// thus suitable for DER encoding.
|
||||
@ -62,7 +58,7 @@ func (sig *Signature) Serialize() []byte {
|
||||
// total length of returned signature is 1 byte for each magic and
|
||||
// length (6 total), plus lengths of r and s
|
||||
length := 6 + len(rb) + len(sb)
|
||||
b := make([]byte, length, length)
|
||||
b := make([]byte, length)
|
||||
|
||||
b[0] = 0x30
|
||||
b[1] = byte(length - 2)
|
||||
@ -420,7 +416,8 @@ func RecoverCompact(curve *KoblitzCurve, signature,
|
||||
func signRFC6979(privateKey *PrivateKey, hash []byte) (*Signature, error) {
|
||||
|
||||
privkey := privateKey.ToECDSA()
|
||||
N := order
|
||||
N := S256().N
|
||||
halfOrder := S256().halfOrder
|
||||
k := nonceRFC6979(privkey.D, hash)
|
||||
inv := new(big.Int).ModInverse(k, N)
|
||||
r, _ := privkey.Curve.ScalarBaseMult(k.Bytes())
|
||||
@ -438,7 +435,7 @@ func signRFC6979(privateKey *PrivateKey, hash []byte) (*Signature, error) {
|
||||
s.Mul(s, inv)
|
||||
s.Mod(s, N)
|
||||
|
||||
if s.Cmp(halforder) == 1 {
|
||||
if s.Cmp(halfOrder) == 1 {
|
||||
s.Sub(N, s)
|
||||
}
|
||||
if s.Sign() == 0 {
|
||||
|
Reference in New Issue
Block a user