Merge btcec repo into btcec directory.
This commit is contained in:
commit
c360543fa9
19 changed files with 5794 additions and 0 deletions
74
btcec/README.md
Normal file
74
btcec/README.md
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
btcec
|
||||||
|
=====
|
||||||
|
|
||||||
|
[![Build Status](https://travis-ci.org/btcsuite/btcd.png?branch=master)]
|
||||||
|
(https://travis-ci.org/btcsuite/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
|
||||||
|
standard crypto/ecdsa packages provided with go. A comprehensive suite of test
|
||||||
|
is provided to ensure proper functionality. Package btcec was originally based
|
||||||
|
on work from ThePiachu which is licensed under the same terms as Go, but it has
|
||||||
|
signficantly diverged since then. The Conformal original is licensed under the
|
||||||
|
liberal ISC license.
|
||||||
|
|
||||||
|
Although this package was primarily written for btcd, it has intentionally been
|
||||||
|
designed so it can be used as a standalone package for any projects needing to
|
||||||
|
use secp256k1 elliptic curve cryptography.
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
[![GoDoc](https://godoc.org/github.com/btcsuite/btcd/btcec?status.png)]
|
||||||
|
(http://godoc.org/github.com/btcsuite/btcd/btcec)
|
||||||
|
|
||||||
|
Full `go doc` style documentation for the project can be viewed online without
|
||||||
|
installing this package by using the GoDoc site
|
||||||
|
[here](http://godoc.org/github.com/btcsuite/btcd/btcec).
|
||||||
|
|
||||||
|
You can also view the documentation locally once the package is installed with
|
||||||
|
the `godoc` tool by running `godoc -http=":6060"` and pointing your browser to
|
||||||
|
http://localhost:6060/pkg/github.com/btcsuite/btcd/btcec
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ go get github.com/btcsuite/btcec
|
||||||
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
* [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)
|
||||||
|
Demonstrates verifying a secp256k1 signature against a public key that is
|
||||||
|
first parsed from raw bytes. The signature is also parsed from raw bytes.
|
||||||
|
|
||||||
|
## GPG Verification Key
|
||||||
|
|
||||||
|
All official release tags are signed by Conformal so users can ensure the code
|
||||||
|
has not been tampered with and is coming from Conformal. To verify the
|
||||||
|
signature perform the following:
|
||||||
|
|
||||||
|
- Download the public key from the Conformal website at
|
||||||
|
https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt
|
||||||
|
|
||||||
|
- Import the public key into your GPG keyring:
|
||||||
|
```bash
|
||||||
|
gpg --import GIT-GPG-KEY-conformal.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
- Verify the release tag with the following command where `TAG_NAME` is a
|
||||||
|
placeholder for the specific tag:
|
||||||
|
```bash
|
||||||
|
git tag -v TAG_NAME
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Package btcec is licensed under the [copyfree](http://copyfree.org) ISC License
|
||||||
|
except for btcec.go and btcec_test.go which is under the same license as Go.
|
||||||
|
|
117
btcec/bench_test.go
Normal file
117
btcec/bench_test.go
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
// Copyright 2013-2014 Conformal Systems LLC. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/btcec"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BenchmarkAddJacobian benchmarks the secp256k1 curve addJacobian function with
|
||||||
|
// Z values of 1 so that the associated optimizations are used.
|
||||||
|
func BenchmarkAddJacobian(b *testing.B) {
|
||||||
|
b.StopTimer()
|
||||||
|
x1 := btcec.NewFieldVal().SetHex("34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6")
|
||||||
|
y1 := btcec.NewFieldVal().SetHex("0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232")
|
||||||
|
z1 := btcec.NewFieldVal().SetHex("1")
|
||||||
|
x2 := btcec.NewFieldVal().SetHex("34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6")
|
||||||
|
y2 := btcec.NewFieldVal().SetHex("0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232")
|
||||||
|
z2 := btcec.NewFieldVal().SetHex("1")
|
||||||
|
x3, y3, z3 := btcec.NewFieldVal(), btcec.NewFieldVal(), btcec.NewFieldVal()
|
||||||
|
curve := btcec.S256()
|
||||||
|
b.StartTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
curve.TstAddJacobian(x1, y1, z1, x2, y2, z2, x3, y3, z3)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkAddJacobianNotZOne benchmarks the secp256k1 curve addJacobian
|
||||||
|
// function with Z values other than one so the optimizations associated with
|
||||||
|
// Z=1 aren't used.
|
||||||
|
func BenchmarkAddJacobianNotZOne(b *testing.B) {
|
||||||
|
b.StopTimer()
|
||||||
|
x1 := btcec.NewFieldVal().SetHex("d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718")
|
||||||
|
y1 := btcec.NewFieldVal().SetHex("5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190")
|
||||||
|
z1 := btcec.NewFieldVal().SetHex("2")
|
||||||
|
x2 := btcec.NewFieldVal().SetHex("91abba6a34b7481d922a4bd6a04899d5a686f6cf6da4e66a0cb427fb25c04bd4")
|
||||||
|
y2 := btcec.NewFieldVal().SetHex("03fede65e30b4e7576a2abefc963ddbf9fdccbf791b77c29beadefe49951f7d1")
|
||||||
|
z2 := btcec.NewFieldVal().SetHex("3")
|
||||||
|
x3, y3, z3 := btcec.NewFieldVal(), btcec.NewFieldVal(), btcec.NewFieldVal()
|
||||||
|
curve := btcec.S256()
|
||||||
|
b.StartTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
curve.TstAddJacobian(x1, y1, z1, x2, y2, z2, x3, y3, z3)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkScalarBaseMult benchmarks the secp256k1 curve ScalarBaseMult
|
||||||
|
// function.
|
||||||
|
func BenchmarkScalarBaseMult(b *testing.B) {
|
||||||
|
k := fromHex("d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575")
|
||||||
|
curve := btcec.S256()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
curve.ScalarBaseMult(k.Bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkScalarBaseMultLarge benchmarks the secp256k1 curve ScalarBaseMult
|
||||||
|
// function with abnormally large k values.
|
||||||
|
func BenchmarkScalarBaseMultLarge(b *testing.B) {
|
||||||
|
k := fromHex("d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c005751111111011111110")
|
||||||
|
curve := btcec.S256()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
curve.ScalarBaseMult(k.Bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkScalarMult benchmarks the secp256k1 curve ScalarMult function.
|
||||||
|
func BenchmarkScalarMult(b *testing.B) {
|
||||||
|
x := fromHex("34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6")
|
||||||
|
y := fromHex("0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232")
|
||||||
|
k := fromHex("d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575")
|
||||||
|
curve := btcec.S256()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
curve.ScalarMult(x, y, k.Bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkNAF benchmarks the NAF function.
|
||||||
|
func BenchmarkNAF(b *testing.B) {
|
||||||
|
k := fromHex("d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575")
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
btcec.NAF(k.Bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkSigVerify benchmarks how long it takes the secp256k1 curve to
|
||||||
|
// verify signatures.
|
||||||
|
func BenchmarkSigVerify(b *testing.B) {
|
||||||
|
b.StopTimer()
|
||||||
|
// Randomly generated keypair.
|
||||||
|
// Private key: 9e0699c91ca1e3b7e3c9ba71eb71c89890872be97576010fe593fbf3fd57e66d
|
||||||
|
pubKey := btcec.PublicKey{
|
||||||
|
Curve: btcec.S256(),
|
||||||
|
X: fromHex("d2e670a19c6d753d1a6d8b20bd045df8a08fb162cf508956c31268c6d81ffdab"),
|
||||||
|
Y: fromHex("ab65528eefbb8057aa85d597258a3fbd481a24633bc9b47a9aa045c91371de52"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Double sha256 of []byte{0x01, 0x02, 0x03, 0x04}
|
||||||
|
msgHash := fromHex("8de472e2399610baaa7f84840547cd409434e31f5d3bd71e4d947f283874f9c0")
|
||||||
|
sig := btcec.Signature{
|
||||||
|
R: fromHex("fef45d2892953aa5bbcdb057b5e98b208f1617a7498af7eb765574e29b5d9c2c"),
|
||||||
|
S: fromHex("d47563f52aac6b04b55de236b7c515eb9311757db01e02cff079c3ca6efb063f"),
|
||||||
|
}
|
||||||
|
|
||||||
|
if !sig.Verify(msgHash.Bytes(), &pubKey) {
|
||||||
|
b.Errorf("Signature failed to verify")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.StartTimer()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
sig.Verify(msgHash.Bytes(), &pubKey)
|
||||||
|
}
|
||||||
|
}
|
924
btcec/btcec.go
Normal file
924
btcec/btcec.go
Normal file
|
@ -0,0 +1,924 @@
|
||||||
|
// Copyright 2010 The Go Authors. All rights reserved.
|
||||||
|
// Copyright 2011 ThePiachu. All rights reserved.
|
||||||
|
// Copyright 2013-2014 Conformal Systems LLC. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec
|
||||||
|
|
||||||
|
// References:
|
||||||
|
// [SECG]: Recommended Elliptic Curve Domain Parameters
|
||||||
|
// http://www.secg.org/sec2-v2.pdf
|
||||||
|
|
||||||
|
// This package operates, internally, on Jacobian coordinates. For a given
|
||||||
|
// (x, y) position on the curve, the Jacobian coordinates are (x1, y1, z1)
|
||||||
|
// where x = x1/z1² and y = y1/z1³. The greatest speedups come when the whole
|
||||||
|
// calculation can be performed within the transform (as in ScalarMult and
|
||||||
|
// ScalarBaseMult). But even for Add and Double, it's faster to apply and
|
||||||
|
// reverse the transform than to operate in affine coordinates.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/elliptic"
|
||||||
|
"math/big"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
//TODO: examine if we need to care about EC optimization as descibed here
|
||||||
|
// https://bitcointalk.org/index.php?topic=155054.0;all
|
||||||
|
|
||||||
|
var (
|
||||||
|
// fieldOne is simply the integer 1 in field representation. It is
|
||||||
|
// used to avoid needing to create it multiple times during the internal
|
||||||
|
// arithmetic.
|
||||||
|
fieldOne = new(fieldVal).SetInt(1)
|
||||||
|
)
|
||||||
|
|
||||||
|
// KoblitzCurve supports a koblitz curve implementation that fits the ECC Curve
|
||||||
|
// interface from crypto/elliptic.
|
||||||
|
type KoblitzCurve struct {
|
||||||
|
*elliptic.CurveParams
|
||||||
|
q *big.Int
|
||||||
|
H int // cofactor of the curve.
|
||||||
|
|
||||||
|
// The next 6 values are used specifically for endomorphism optimizations
|
||||||
|
// in ScalarMult.
|
||||||
|
|
||||||
|
// lambda should fulfill lambda^3 = 1 mod N where N is the order of G
|
||||||
|
lambda *big.Int
|
||||||
|
// beta should fulfill beta^3 = 1 mod P where P is the prime field of the curve
|
||||||
|
beta *fieldVal
|
||||||
|
// a1, b1, a2 and b2 are explained in detail in Guide To Elliptical Curve
|
||||||
|
// Cryptography (Hankerson, Menezes, Vanstone) in Algorithm 3.74
|
||||||
|
a1 *big.Int
|
||||||
|
b1 *big.Int
|
||||||
|
a2 *big.Int
|
||||||
|
b2 *big.Int
|
||||||
|
byteSize int
|
||||||
|
bytePoints *[32][256][3]fieldVal
|
||||||
|
}
|
||||||
|
|
||||||
|
// Params returns the parameters for the curve.
|
||||||
|
func (curve *KoblitzCurve) Params() *elliptic.CurveParams {
|
||||||
|
return curve.CurveParams
|
||||||
|
}
|
||||||
|
|
||||||
|
// bigAffineToField takes an affine point (x, y) as big integers and converts
|
||||||
|
// it to an affine point as field values.
|
||||||
|
func (curve *KoblitzCurve) bigAffineToField(x, y *big.Int) (*fieldVal, *fieldVal) {
|
||||||
|
x3, y3 := new(fieldVal), new(fieldVal)
|
||||||
|
x3.SetByteSlice(x.Bytes())
|
||||||
|
y3.SetByteSlice(y.Bytes())
|
||||||
|
|
||||||
|
return x3, y3
|
||||||
|
}
|
||||||
|
|
||||||
|
// fieldJacobianToBigAffine takes a Jacobian point (x, y, z) as field values and
|
||||||
|
// converts it to an affine point as big integers.
|
||||||
|
func (curve *KoblitzCurve) fieldJacobianToBigAffine(x, y, z *fieldVal) (*big.Int, *big.Int) {
|
||||||
|
// Inversions are expensive and both point addition and point doubling
|
||||||
|
// are faster when working with points that have a z value of one. So,
|
||||||
|
// if the point needs to be converted to affine, go ahead and normalize
|
||||||
|
// the point itself at the same time as the calculation is the same.
|
||||||
|
var zInv, tempZ fieldVal
|
||||||
|
zInv.Set(z).Inverse() // zInv = Z^-1
|
||||||
|
tempZ.SquareVal(&zInv) // tempZ = Z^-2
|
||||||
|
x.Mul(&tempZ) // X = X/Z^2 (mag: 1)
|
||||||
|
y.Mul(tempZ.Mul(&zInv)) // Y = Y/Z^3 (mag: 1)
|
||||||
|
z.SetInt(1) // Z = 1 (mag: 1)
|
||||||
|
|
||||||
|
// Normalize the x and y values.
|
||||||
|
x.Normalize()
|
||||||
|
y.Normalize()
|
||||||
|
|
||||||
|
// Convert the field values for the now affine point to big.Ints.
|
||||||
|
x3, y3 := new(big.Int), new(big.Int)
|
||||||
|
x3.SetBytes(x.Bytes()[:])
|
||||||
|
y3.SetBytes(y.Bytes()[:])
|
||||||
|
return x3, y3
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsOnCurve returns boolean if the point (x,y) is on the curve.
|
||||||
|
// Part of the elliptic.Curve interface. This function differs from the
|
||||||
|
// crypto/elliptic algorithm since a = 0 not -3.
|
||||||
|
func (curve *KoblitzCurve) IsOnCurve(x, y *big.Int) bool {
|
||||||
|
// Convert big ints to field values for faster arithmetic.
|
||||||
|
fx, fy := curve.bigAffineToField(x, y)
|
||||||
|
|
||||||
|
// Elliptic curve equation for secp256k1 is: y^2 = x^3 + 7
|
||||||
|
y2 := new(fieldVal).SquareVal(fy).Normalize()
|
||||||
|
result := new(fieldVal).SquareVal(fx).Mul(fx).AddInt(7).Normalize()
|
||||||
|
return y2.Equals(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// addZ1AndZ2EqualsOne adds two Jacobian points that are already known to have
|
||||||
|
// z values of 1 and stores the result in (x3, y3, z3). That is to say
|
||||||
|
// (x1, y1, 1) + (x2, y2, 1) = (x3, y3, z3). It performs faster addition than
|
||||||
|
// the generic add routine since less arithmetic is needed due to the ability to
|
||||||
|
// avoid the z value multiplications.
|
||||||
|
func (curve *KoblitzCurve) addZ1AndZ2EqualsOne(x1, y1, z1, x2, y2, x3, y3, z3 *fieldVal) {
|
||||||
|
// To compute the point addition efficiently, this implementation splits
|
||||||
|
// the equation into intermediate elements which are used to minimize
|
||||||
|
// the number of field multiplications using the method shown at:
|
||||||
|
// http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-mmadd-2007-bl
|
||||||
|
//
|
||||||
|
// In particular it performs the calculations using the following:
|
||||||
|
// H = X2-X1, HH = H^2, I = 4*HH, J = H*I, r = 2*(Y2-Y1), V = X1*I
|
||||||
|
// X3 = r^2-J-2*V, Y3 = r*(V-X3)-2*Y1*J, Z3 = 2*H
|
||||||
|
//
|
||||||
|
// This results in a cost of 4 field multiplications, 2 field squarings,
|
||||||
|
// 6 field additions, and 5 integer multiplications.
|
||||||
|
|
||||||
|
// When the x coordinates are the same for two points on the curve, the
|
||||||
|
// y coordinates either must be the same, in which case it is point
|
||||||
|
// doubling, or they are opposite and the result is the point at
|
||||||
|
// infinity per the group law for elliptic curve cryptography.
|
||||||
|
x1.Normalize()
|
||||||
|
y1.Normalize()
|
||||||
|
x2.Normalize()
|
||||||
|
y2.Normalize()
|
||||||
|
if x1.Equals(x2) {
|
||||||
|
if y1.Equals(y2) {
|
||||||
|
// Since x1 == x2 and y1 == y2, point doubling must be
|
||||||
|
// done, otherwise the addition would end up dividing
|
||||||
|
// by zero.
|
||||||
|
curve.doubleJacobian(x1, y1, z1, x3, y3, z3)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Since x1 == x2 and y1 == -y2, the sum is the point at
|
||||||
|
// infinity per the group law.
|
||||||
|
x3.SetInt(0)
|
||||||
|
y3.SetInt(0)
|
||||||
|
z3.SetInt(0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate X3, Y3, and Z3 according to the intermediate elements
|
||||||
|
// breakdown above.
|
||||||
|
var h, i, j, r, v fieldVal
|
||||||
|
var negJ, neg2V, negX3 fieldVal
|
||||||
|
h.Set(x1).Negate(1).Add(x2) // H = X2-X1 (mag: 3)
|
||||||
|
i.SquareVal(&h).MulInt(4) // I = 4*H^2 (mag: 4)
|
||||||
|
j.Mul2(&h, &i) // J = H*I (mag: 1)
|
||||||
|
r.Set(y1).Negate(1).Add(y2).MulInt(2) // r = 2*(Y2-Y1) (mag: 6)
|
||||||
|
v.Mul2(x1, &i) // V = X1*I (mag: 1)
|
||||||
|
negJ.Set(&j).Negate(1) // negJ = -J (mag: 2)
|
||||||
|
neg2V.Set(&v).MulInt(2).Negate(2) // neg2V = -(2*V) (mag: 3)
|
||||||
|
x3.Set(&r).Square().Add(&negJ).Add(&neg2V) // X3 = r^2-J-2*V (mag: 6)
|
||||||
|
negX3.Set(x3).Negate(6) // negX3 = -X3 (mag: 7)
|
||||||
|
j.Mul(y1).MulInt(2).Negate(2) // J = -(2*Y1*J) (mag: 3)
|
||||||
|
y3.Set(&v).Add(&negX3).Mul(&r).Add(&j) // Y3 = r*(V-X3)-2*Y1*J (mag: 4)
|
||||||
|
z3.Set(&h).MulInt(2) // Z3 = 2*H (mag: 6)
|
||||||
|
|
||||||
|
// Normalize the resulting field values to a magnitude of 1 as needed.
|
||||||
|
x3.Normalize()
|
||||||
|
y3.Normalize()
|
||||||
|
z3.Normalize()
|
||||||
|
}
|
||||||
|
|
||||||
|
// addZ1EqualsZ2 adds two Jacobian points that are already known to have the
|
||||||
|
// same z value and stores the result in (x3, y3, z3). That is to say
|
||||||
|
// (x1, y1, z1) + (x2, y2, z1) = (x3, y3, z3). It performs faster addition than
|
||||||
|
// the generic add routine since less arithmetic is needed due to the known
|
||||||
|
// equivalence.
|
||||||
|
func (curve *KoblitzCurve) addZ1EqualsZ2(x1, y1, z1, x2, y2, x3, y3, z3 *fieldVal) {
|
||||||
|
// To compute the point addition efficiently, this implementation splits
|
||||||
|
// the equation into intermediate elements which are used to minimize
|
||||||
|
// the number of field multiplications using a slightly modified version
|
||||||
|
// of the method shown at:
|
||||||
|
// http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-mmadd-2007-bl
|
||||||
|
//
|
||||||
|
// In particular it performs the calculations using the following:
|
||||||
|
// A = X2-X1, B = A^2, C=Y2-Y1, D = C^2, E = X1*B, F = X2*B
|
||||||
|
// X3 = D-E-F, Y3 = C*(E-X3)-Y1*(F-E), Z3 = Z1*A
|
||||||
|
//
|
||||||
|
// This results in a cost of 5 field multiplications, 2 field squarings,
|
||||||
|
// 9 field additions, and 0 integer multiplications.
|
||||||
|
|
||||||
|
// When the x coordinates are the same for two points on the curve, the
|
||||||
|
// y coordinates either must be the same, in which case it is point
|
||||||
|
// doubling, or they are opposite and the result is the point at
|
||||||
|
// infinity per the group law for elliptic curve cryptography.
|
||||||
|
x1.Normalize()
|
||||||
|
y1.Normalize()
|
||||||
|
x2.Normalize()
|
||||||
|
y2.Normalize()
|
||||||
|
if x1.Equals(x2) {
|
||||||
|
if y1.Equals(y2) {
|
||||||
|
// Since x1 == x2 and y1 == y2, point doubling must be
|
||||||
|
// done, otherwise the addition would end up dividing
|
||||||
|
// by zero.
|
||||||
|
curve.doubleJacobian(x1, y1, z1, x3, y3, z3)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Since x1 == x2 and y1 == -y2, the sum is the point at
|
||||||
|
// infinity per the group law.
|
||||||
|
x3.SetInt(0)
|
||||||
|
y3.SetInt(0)
|
||||||
|
z3.SetInt(0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate X3, Y3, and Z3 according to the intermediate elements
|
||||||
|
// breakdown above.
|
||||||
|
var a, b, c, d, e, f fieldVal
|
||||||
|
var negX1, negY1, negE, negX3 fieldVal
|
||||||
|
negX1.Set(x1).Negate(1) // negX1 = -X1 (mag: 2)
|
||||||
|
negY1.Set(y1).Negate(1) // negY1 = -Y1 (mag: 2)
|
||||||
|
a.Set(&negX1).Add(x2) // A = X2-X1 (mag: 3)
|
||||||
|
b.SquareVal(&a) // B = A^2 (mag: 1)
|
||||||
|
c.Set(&negY1).Add(y2) // C = Y2-Y1 (mag: 3)
|
||||||
|
d.SquareVal(&c) // D = C^2 (mag: 1)
|
||||||
|
e.Mul2(x1, &b) // E = X1*B (mag: 1)
|
||||||
|
negE.Set(&e).Negate(1) // negE = -E (mag: 2)
|
||||||
|
f.Mul2(x2, &b) // F = X2*B (mag: 1)
|
||||||
|
x3.Add2(&e, &f).Negate(3).Add(&d) // X3 = D-E-F (mag: 5)
|
||||||
|
negX3.Set(x3).Negate(5).Normalize() // negX3 = -X3 (mag: 1)
|
||||||
|
y3.Set(y1).Mul(f.Add(&negE)).Negate(3) // Y3 = -(Y1*(F-E)) (mag: 4)
|
||||||
|
y3.Add(e.Add(&negX3).Mul(&c)) // Y3 = C*(E-X3)+Y3 (mag: 5)
|
||||||
|
z3.Mul2(z1, &a) // Z3 = Z1*A (mag: 1)
|
||||||
|
|
||||||
|
// Normalize the resulting field values to a magnitude of 1 as needed.
|
||||||
|
x3.Normalize()
|
||||||
|
y3.Normalize()
|
||||||
|
}
|
||||||
|
|
||||||
|
// addZ2EqualsOne adds two Jacobian points when the second point is already
|
||||||
|
// known to have a z value of 1 (and the z value for the first point is not 1)
|
||||||
|
// and stores the result in (x3, y3, z3). That is to say (x1, y1, z1) +
|
||||||
|
// (x2, y2, 1) = (x3, y3, z3). It performs faster addition than the generic
|
||||||
|
// add routine since less arithmetic is needed due to the ability to avoid
|
||||||
|
// multiplications by the second point's z value.
|
||||||
|
func (curve *KoblitzCurve) addZ2EqualsOne(x1, y1, z1, x2, y2, x3, y3, z3 *fieldVal) {
|
||||||
|
// To compute the point addition efficiently, this implementation splits
|
||||||
|
// the equation into intermediate elements which are used to minimize
|
||||||
|
// the number of field multiplications using the method shown at:
|
||||||
|
// http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-madd-2007-bl
|
||||||
|
//
|
||||||
|
// In particular it performs the calculations using the following:
|
||||||
|
// Z1Z1 = Z1^2, U2 = X2*Z1Z1, S2 = Y2*Z1*Z1Z1, H = U2-X1, HH = H^2,
|
||||||
|
// I = 4*HH, J = H*I, r = 2*(S2-Y1), V = X1*I
|
||||||
|
// X3 = r^2-J-2*V, Y3 = r*(V-X3)-2*Y1*J, Z3 = (Z1+H)^2-Z1Z1-HH
|
||||||
|
//
|
||||||
|
// This results in a cost of 7 field multiplications, 4 field squarings,
|
||||||
|
// 9 field additions, and 4 integer multiplications.
|
||||||
|
|
||||||
|
// When the x coordinates are the same for two points on the curve, the
|
||||||
|
// y coordinates either must be the same, in which case it is point
|
||||||
|
// doubling, or they are opposite and the result is the point at
|
||||||
|
// infinity per the group law for elliptic curve cryptography. Since
|
||||||
|
// any number of Jacobian coordinates can represent the same affine
|
||||||
|
// point, the x and y values need to be converted to like terms. Due to
|
||||||
|
// the assumption made for this function that the second point has a z
|
||||||
|
// value of 1 (z2=1), the first point is already "converted".
|
||||||
|
var z1z1, u2, s2 fieldVal
|
||||||
|
x1.Normalize()
|
||||||
|
y1.Normalize()
|
||||||
|
z1z1.SquareVal(z1) // Z1Z1 = Z1^2 (mag: 1)
|
||||||
|
u2.Set(x2).Mul(&z1z1).Normalize() // U2 = X2*Z1Z1 (mag: 1)
|
||||||
|
s2.Set(y2).Mul(&z1z1).Mul(z1).Normalize() // S2 = Y2*Z1*Z1Z1 (mag: 1)
|
||||||
|
if x1.Equals(&u2) {
|
||||||
|
if y1.Equals(&s2) {
|
||||||
|
// Since x1 == x2 and y1 == y2, point doubling must be
|
||||||
|
// done, otherwise the addition would end up dividing
|
||||||
|
// by zero.
|
||||||
|
curve.doubleJacobian(x1, y1, z1, x3, y3, z3)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Since x1 == x2 and y1 == -y2, the sum is the point at
|
||||||
|
// infinity per the group law.
|
||||||
|
x3.SetInt(0)
|
||||||
|
y3.SetInt(0)
|
||||||
|
z3.SetInt(0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate X3, Y3, and Z3 according to the intermediate elements
|
||||||
|
// breakdown above.
|
||||||
|
var h, hh, i, j, r, rr, v fieldVal
|
||||||
|
var negX1, negY1, negX3 fieldVal
|
||||||
|
negX1.Set(x1).Negate(1) // negX1 = -X1 (mag: 2)
|
||||||
|
h.Add2(&u2, &negX1) // H = U2-X1 (mag: 3)
|
||||||
|
hh.SquareVal(&h) // HH = H^2 (mag: 1)
|
||||||
|
i.Set(&hh).MulInt(4) // I = 4 * HH (mag: 4)
|
||||||
|
j.Mul2(&h, &i) // J = H*I (mag: 1)
|
||||||
|
negY1.Set(y1).Negate(1) // negY1 = -Y1 (mag: 2)
|
||||||
|
r.Set(&s2).Add(&negY1).MulInt(2) // r = 2*(S2-Y1) (mag: 6)
|
||||||
|
rr.SquareVal(&r) // rr = r^2 (mag: 1)
|
||||||
|
v.Mul2(x1, &i) // V = X1*I (mag: 1)
|
||||||
|
x3.Set(&v).MulInt(2).Add(&j).Negate(3) // X3 = -(J+2*V) (mag: 4)
|
||||||
|
x3.Add(&rr) // X3 = r^2+X3 (mag: 5)
|
||||||
|
negX3.Set(x3).Negate(5) // negX3 = -X3 (mag: 6)
|
||||||
|
y3.Set(y1).Mul(&j).MulInt(2).Negate(2) // Y3 = -(2*Y1*J) (mag: 3)
|
||||||
|
y3.Add(v.Add(&negX3).Mul(&r)) // Y3 = r*(V-X3)+Y3 (mag: 4)
|
||||||
|
z3.Add2(z1, &h).Square() // Z3 = (Z1+H)^2 (mag: 1)
|
||||||
|
z3.Add(z1z1.Add(&hh).Negate(2)) // Z3 = Z3-(Z1Z1+HH) (mag: 4)
|
||||||
|
|
||||||
|
// Normalize the resulting field values to a magnitude of 1 as needed.
|
||||||
|
x3.Normalize()
|
||||||
|
y3.Normalize()
|
||||||
|
z3.Normalize()
|
||||||
|
}
|
||||||
|
|
||||||
|
// addGeneric adds two Jacobian points (x1, y1, z1) and (x2, y2, z2) without any
|
||||||
|
// assumptions about the z values of the two points and stores the result in
|
||||||
|
// (x3, y3, z3). That is to say (x1, y1, z1) + (x2, y2, z2) = (x3, y3, z3). It
|
||||||
|
// is the slowest of the add routines due to requiring the most arithmetic.
|
||||||
|
func (curve *KoblitzCurve) addGeneric(x1, y1, z1, x2, y2, z2, x3, y3, z3 *fieldVal) {
|
||||||
|
// To compute the point addition efficiently, this implementation splits
|
||||||
|
// the equation into intermediate elements which are used to minimize
|
||||||
|
// the number of field multiplications using the method shown at:
|
||||||
|
// http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
|
||||||
|
//
|
||||||
|
// In particular it performs the calculations using the following:
|
||||||
|
// Z1Z1 = Z1^2, Z2Z2 = Z2^2, U1 = X1*Z2Z2, U2 = X2*Z1Z1, S1 = Y1*Z2*Z2Z2
|
||||||
|
// S2 = Y2*Z1*Z1Z1, H = U2-U1, I = (2*H)^2, J = H*I, r = 2*(S2-S1)
|
||||||
|
// V = U1*I
|
||||||
|
// X3 = r^2-J-2*V, Y3 = r*(V-X3)-2*S1*J, Z3 = ((Z1+Z2)^2-Z1Z1-Z2Z2)*H
|
||||||
|
//
|
||||||
|
// This results in a cost of 11 field multiplications, 5 field squarings,
|
||||||
|
// 9 field additions, and 4 integer multiplications.
|
||||||
|
|
||||||
|
// When the x coordinates are the same for two points on the curve, the
|
||||||
|
// y coordinates either must be the same, in which case it is point
|
||||||
|
// doubling, or they are opposite and the result is the point at
|
||||||
|
// infinity. Since any number of Jacobian coordinates can represent the
|
||||||
|
// same affine point, the x and y values need to be converted to like
|
||||||
|
// terms.
|
||||||
|
var z1z1, z2z2, u1, u2, s1, s2 fieldVal
|
||||||
|
z1z1.SquareVal(z1) // Z1Z1 = Z1^2 (mag: 1)
|
||||||
|
z2z2.SquareVal(z2) // Z2Z2 = Z2^2 (mag: 1)
|
||||||
|
u1.Set(x1).Mul(&z2z2).Normalize() // U1 = X1*Z2Z2 (mag: 1)
|
||||||
|
u2.Set(x2).Mul(&z1z1).Normalize() // U2 = X2*Z1Z1 (mag: 1)
|
||||||
|
s1.Set(y1).Mul(&z2z2).Mul(z2).Normalize() // S1 = Y1*Z2*Z2Z2 (mag: 1)
|
||||||
|
s2.Set(y2).Mul(&z1z1).Mul(z1).Normalize() // S2 = Y2*Z1*Z1Z1 (mag: 1)
|
||||||
|
if u1.Equals(&u2) {
|
||||||
|
if s1.Equals(&s2) {
|
||||||
|
// Since x1 == x2 and y1 == y2, point doubling must be
|
||||||
|
// done, otherwise the addition would end up dividing
|
||||||
|
// by zero.
|
||||||
|
curve.doubleJacobian(x1, y1, z1, x3, y3, z3)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Since x1 == x2 and y1 == -y2, the sum is the point at
|
||||||
|
// infinity per the group law.
|
||||||
|
x3.SetInt(0)
|
||||||
|
y3.SetInt(0)
|
||||||
|
z3.SetInt(0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate X3, Y3, and Z3 according to the intermediate elements
|
||||||
|
// breakdown above.
|
||||||
|
var h, i, j, r, rr, v fieldVal
|
||||||
|
var negU1, negS1, negX3 fieldVal
|
||||||
|
negU1.Set(&u1).Negate(1) // negU1 = -U1 (mag: 2)
|
||||||
|
h.Add2(&u2, &negU1) // H = U2-U1 (mag: 3)
|
||||||
|
i.Set(&h).MulInt(2).Square() // I = (2*H)^2 (mag: 2)
|
||||||
|
j.Mul2(&h, &i) // J = H*I (mag: 1)
|
||||||
|
negS1.Set(&s1).Negate(1) // negS1 = -S1 (mag: 2)
|
||||||
|
r.Set(&s2).Add(&negS1).MulInt(2) // r = 2*(S2-S1) (mag: 6)
|
||||||
|
rr.SquareVal(&r) // rr = r^2 (mag: 1)
|
||||||
|
v.Mul2(&u1, &i) // V = U1*I (mag: 1)
|
||||||
|
x3.Set(&v).MulInt(2).Add(&j).Negate(3) // X3 = -(J+2*V) (mag: 4)
|
||||||
|
x3.Add(&rr) // X3 = r^2+X3 (mag: 5)
|
||||||
|
negX3.Set(x3).Negate(5) // negX3 = -X3 (mag: 6)
|
||||||
|
y3.Mul2(&s1, &j).MulInt(2).Negate(2) // Y3 = -(2*S1*J) (mag: 3)
|
||||||
|
y3.Add(v.Add(&negX3).Mul(&r)) // Y3 = r*(V-X3)+Y3 (mag: 4)
|
||||||
|
z3.Add2(z1, z2).Square() // Z3 = (Z1+Z2)^2 (mag: 1)
|
||||||
|
z3.Add(z1z1.Add(&z2z2).Negate(2)) // Z3 = Z3-(Z1Z1+Z2Z2) (mag: 4)
|
||||||
|
z3.Mul(&h) // Z3 = Z3*H (mag: 1)
|
||||||
|
|
||||||
|
// Normalize the resulting field values to a magnitude of 1 as needed.
|
||||||
|
x3.Normalize()
|
||||||
|
y3.Normalize()
|
||||||
|
}
|
||||||
|
|
||||||
|
// addJacobian adds the passed Jacobian points (x1, y1, z1) and (x2, y2, z2)
|
||||||
|
// together and stores the result in (x3, y3, z3).
|
||||||
|
func (curve *KoblitzCurve) addJacobian(x1, y1, z1, x2, y2, z2, x3, y3, z3 *fieldVal) {
|
||||||
|
// A point at infinity is the identity according to the group law for
|
||||||
|
// elliptic curve cryptography. Thus, ∞ + P = P and P + ∞ = P.
|
||||||
|
if (x1.IsZero() && y1.IsZero()) || z1.IsZero() {
|
||||||
|
x3.Set(x2)
|
||||||
|
y3.Set(y2)
|
||||||
|
z3.Set(z2)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (x2.IsZero() && y2.IsZero()) || z2.IsZero() {
|
||||||
|
x3.Set(x1)
|
||||||
|
y3.Set(y1)
|
||||||
|
z3.Set(z1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Faster point addition can be achieved when certain assumptions are
|
||||||
|
// met. For example, when both points have the same z value, arithmetic
|
||||||
|
// on the z values can be avoided. This section thus checks for these
|
||||||
|
// conditions and calls an appropriate add function which is accelerated
|
||||||
|
// by using those assumptions.
|
||||||
|
z1.Normalize()
|
||||||
|
z2.Normalize()
|
||||||
|
isZ1One := z1.Equals(fieldOne)
|
||||||
|
isZ2One := z2.Equals(fieldOne)
|
||||||
|
switch {
|
||||||
|
case isZ1One && isZ2One:
|
||||||
|
curve.addZ1AndZ2EqualsOne(x1, y1, z1, x2, y2, x3, y3, z3)
|
||||||
|
return
|
||||||
|
case z1.Equals(z2):
|
||||||
|
curve.addZ1EqualsZ2(x1, y1, z1, x2, y2, x3, y3, z3)
|
||||||
|
return
|
||||||
|
case isZ2One:
|
||||||
|
curve.addZ2EqualsOne(x1, y1, z1, x2, y2, x3, y3, z3)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// None of the above assumptions are true, so fall back to generic
|
||||||
|
// point addition.
|
||||||
|
curve.addGeneric(x1, y1, z1, x2, y2, z2, x3, y3, z3)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add returns the sum of (x1,y1) and (x2,y2). Part of the elliptic.Curve
|
||||||
|
// interface.
|
||||||
|
func (curve *KoblitzCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
|
||||||
|
// A point at infinity is the identity according to the group law for
|
||||||
|
// elliptic curve cryptography. Thus, ∞ + P = P and P + ∞ = P.
|
||||||
|
if x1.Sign() == 0 && y1.Sign() == 0 {
|
||||||
|
return x2, y2
|
||||||
|
}
|
||||||
|
if x2.Sign() == 0 && y2.Sign() == 0 {
|
||||||
|
return x1, y1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the affine coordinates from big integers to field values
|
||||||
|
// and do the point addition in Jacobian projective space.
|
||||||
|
fx1, fy1 := curve.bigAffineToField(x1, y1)
|
||||||
|
fx2, fy2 := curve.bigAffineToField(x2, y2)
|
||||||
|
fx3, fy3, fz3 := new(fieldVal), new(fieldVal), new(fieldVal)
|
||||||
|
fOne := new(fieldVal).SetInt(1)
|
||||||
|
curve.addJacobian(fx1, fy1, fOne, fx2, fy2, fOne, fx3, fy3, fz3)
|
||||||
|
|
||||||
|
// Convert the Jacobian coordinate field values back to affine big
|
||||||
|
// integers.
|
||||||
|
return curve.fieldJacobianToBigAffine(fx3, fy3, fz3)
|
||||||
|
}
|
||||||
|
|
||||||
|
// doubleZ1EqualsOne performs point doubling on the passed Jacobian point
|
||||||
|
// when the point is already known to have a z value of 1 and stores
|
||||||
|
// the result in (x3, y3, z3). That is to say (x3, y3, z3) = 2*(x1, y1, 1). It
|
||||||
|
// performs faster point doubling than the generic routine since less arithmetic
|
||||||
|
// is needed due to the ability to avoid multiplication by the z value.
|
||||||
|
func (curve *KoblitzCurve) doubleZ1EqualsOne(x1, y1, x3, y3, z3 *fieldVal) {
|
||||||
|
// This function uses the assumptions that z1 is 1, thus the point
|
||||||
|
// doubling formulas reduce to:
|
||||||
|
//
|
||||||
|
// X3 = (3*X1^2)^2 - 8*X1*Y1^2
|
||||||
|
// Y3 = (3*X1^2)*(4*X1*Y1^2 - X3) - 8*Y1^4
|
||||||
|
// Z3 = 2*Y1
|
||||||
|
//
|
||||||
|
// To compute the above efficiently, this implementation splits the
|
||||||
|
// equation into intermediate elements which are used to minimize the
|
||||||
|
// number of field multiplications in favor of field squarings which
|
||||||
|
// are roughly 35% faster than field multiplications with the current
|
||||||
|
// implementation at the time this was written.
|
||||||
|
//
|
||||||
|
// This uses a slightly modified version of the method shown at:
|
||||||
|
// http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-mdbl-2007-bl
|
||||||
|
//
|
||||||
|
// In particular it performs the calculations using the following:
|
||||||
|
// A = X1^2, B = Y1^2, C = B^2, D = 2*((X1+B)^2-A-C)
|
||||||
|
// E = 3*A, F = E^2, X3 = F-2*D, Y3 = E*(D-X3)-8*C
|
||||||
|
// Z3 = 2*Y1
|
||||||
|
//
|
||||||
|
// This results in a cost of 1 field multiplication, 5 field squarings,
|
||||||
|
// 6 field additions, and 5 integer multiplications.
|
||||||
|
var a, b, c, d, e, f fieldVal
|
||||||
|
z3.Set(y1).MulInt(2) // Z3 = 2*Y1 (mag: 2)
|
||||||
|
a.SquareVal(x1) // A = X1^2 (mag: 1)
|
||||||
|
b.SquareVal(y1) // B = Y1^2 (mag: 1)
|
||||||
|
c.SquareVal(&b) // C = B^2 (mag: 1)
|
||||||
|
b.Add(x1).Square() // B = (X1+B)^2 (mag: 1)
|
||||||
|
d.Set(&a).Add(&c).Negate(2) // D = -(A+C) (mag: 3)
|
||||||
|
d.Add(&b).MulInt(2) // D = 2*(B+D)(mag: 8)
|
||||||
|
e.Set(&a).MulInt(3) // E = 3*A (mag: 3)
|
||||||
|
f.SquareVal(&e) // F = E^2 (mag: 1)
|
||||||
|
x3.Set(&d).MulInt(2).Negate(16) // X3 = -(2*D) (mag: 17)
|
||||||
|
x3.Add(&f) // X3 = F+X3 (mag: 18)
|
||||||
|
f.Set(x3).Negate(18).Add(&d).Normalize() // F = D-X3 (mag: 1)
|
||||||
|
y3.Set(&c).MulInt(8).Negate(8) // Y3 = -(8*C) (mag: 9)
|
||||||
|
y3.Add(f.Mul(&e)) // Y3 = E*F+Y3 (mag: 10)
|
||||||
|
|
||||||
|
// Normalize the field values back to a magnitude of 1.
|
||||||
|
x3.Normalize()
|
||||||
|
y3.Normalize()
|
||||||
|
z3.Normalize()
|
||||||
|
}
|
||||||
|
|
||||||
|
// doubleGeneric performs point doubling on the passed Jacobian point without
|
||||||
|
// any assumptions about the z value and stores the result in (x3, y3, z3).
|
||||||
|
// That is to say (x3, y3, z3) = 2*(x1, y1, z1). It is the slowest of the point
|
||||||
|
// doubling routines due to requiring the most arithmetic.
|
||||||
|
func (curve *KoblitzCurve) doubleGeneric(x1, y1, z1, x3, y3, z3 *fieldVal) {
|
||||||
|
// Point doubling formula for Jacobian coordinates for the secp256k1
|
||||||
|
// curve:
|
||||||
|
// X3 = (3*X1^2)^2 - 8*X1*Y1^2
|
||||||
|
// Y3 = (3*X1^2)*(4*X1*Y1^2 - X3) - 8*Y1^4
|
||||||
|
// Z3 = 2*Y1*Z1
|
||||||
|
//
|
||||||
|
// To compute the above efficiently, this implementation splits the
|
||||||
|
// equation into intermediate elements which are used to minimize the
|
||||||
|
// number of field multiplications in favor of field squarings which
|
||||||
|
// are roughly 35% faster than field multiplications with the current
|
||||||
|
// implementation at the time this was written.
|
||||||
|
//
|
||||||
|
// This uses a slightly modified version of the method shown at:
|
||||||
|
// http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
|
||||||
|
//
|
||||||
|
// In particular it performs the calculations using the following:
|
||||||
|
// A = X1^2, B = Y1^2, C = B^2, D = 2*((X1+B)^2-A-C)
|
||||||
|
// E = 3*A, F = E^2, X3 = F-2*D, Y3 = E*(D-X3)-8*C
|
||||||
|
// Z3 = 2*Y1*Z1
|
||||||
|
//
|
||||||
|
// This results in a cost of 1 field multiplication, 5 field squarings,
|
||||||
|
// 6 field additions, and 5 integer multiplications.
|
||||||
|
var a, b, c, d, e, f fieldVal
|
||||||
|
z3.Mul2(y1, z1).MulInt(2) // Z3 = 2*Y1*Z1 (mag: 2)
|
||||||
|
a.SquareVal(x1) // A = X1^2 (mag: 1)
|
||||||
|
b.SquareVal(y1) // B = Y1^2 (mag: 1)
|
||||||
|
c.SquareVal(&b) // C = B^2 (mag: 1)
|
||||||
|
b.Add(x1).Square() // B = (X1+B)^2 (mag: 1)
|
||||||
|
d.Set(&a).Add(&c).Negate(2) // D = -(A+C) (mag: 3)
|
||||||
|
d.Add(&b).MulInt(2) // D = 2*(B+D)(mag: 8)
|
||||||
|
e.Set(&a).MulInt(3) // E = 3*A (mag: 3)
|
||||||
|
f.SquareVal(&e) // F = E^2 (mag: 1)
|
||||||
|
x3.Set(&d).MulInt(2).Negate(16) // X3 = -(2*D) (mag: 17)
|
||||||
|
x3.Add(&f) // X3 = F+X3 (mag: 18)
|
||||||
|
f.Set(x3).Negate(18).Add(&d).Normalize() // F = D-X3 (mag: 1)
|
||||||
|
y3.Set(&c).MulInt(8).Negate(8) // Y3 = -(8*C) (mag: 9)
|
||||||
|
y3.Add(f.Mul(&e)) // Y3 = E*F+Y3 (mag: 10)
|
||||||
|
|
||||||
|
// Normalize the field values back to a magnitude of 1.
|
||||||
|
x3.Normalize()
|
||||||
|
y3.Normalize()
|
||||||
|
z3.Normalize()
|
||||||
|
}
|
||||||
|
|
||||||
|
// doubleJacobian doubles the passed Jacobian point (x1, y1, z1) and stores the
|
||||||
|
// result in (x3, y3, z3).
|
||||||
|
func (curve *KoblitzCurve) doubleJacobian(x1, y1, z1, x3, y3, z3 *fieldVal) {
|
||||||
|
// Doubling a point at infinity is still infinity.
|
||||||
|
if y1.IsZero() || z1.IsZero() {
|
||||||
|
x3.SetInt(0)
|
||||||
|
y3.SetInt(0)
|
||||||
|
z3.SetInt(0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Slightly faster point doubling can be achieved when the z value is 1
|
||||||
|
// by avoiding the multiplication on the z value. This section calls
|
||||||
|
// a point doubling function which is accelerated by using that
|
||||||
|
// assumption when possible.
|
||||||
|
if z1.Normalize().Equals(fieldOne) {
|
||||||
|
curve.doubleZ1EqualsOne(x1, y1, x3, y3, z3)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall back to generic point doubling which works with arbitrary z
|
||||||
|
// values.
|
||||||
|
curve.doubleGeneric(x1, y1, z1, x3, y3, z3)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Double returns 2*(x1,y1). Part of the elliptic.Curve interface.
|
||||||
|
func (curve *KoblitzCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
|
||||||
|
if y1.Sign() == 0 {
|
||||||
|
return new(big.Int), new(big.Int)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the affine coordinates from big integers to field values
|
||||||
|
// and do the point doubling in Jacobian projective space.
|
||||||
|
fx1, fy1 := curve.bigAffineToField(x1, y1)
|
||||||
|
fx3, fy3, fz3 := new(fieldVal), new(fieldVal), new(fieldVal)
|
||||||
|
fOne := new(fieldVal).SetInt(1)
|
||||||
|
curve.doubleJacobian(fx1, fy1, fOne, fx3, fy3, fz3)
|
||||||
|
|
||||||
|
// Convert the Jacobian coordinate field values back to affine big
|
||||||
|
// integers.
|
||||||
|
return curve.fieldJacobianToBigAffine(fx3, fy3, fz3)
|
||||||
|
}
|
||||||
|
|
||||||
|
// splitK returns a balanced length-two representation of k and their
|
||||||
|
// signs.
|
||||||
|
// This is algorithm 3.74 from Guide to Elliptical Curve Cryptography (ref above)
|
||||||
|
// One thing of note about this algorithm is that no matter what c1 and c2 are,
|
||||||
|
// the final equation of k = k1 + k2 * lambda (mod n) will hold. This is provable
|
||||||
|
// mathematically due to how a1/b1/a2/b2 are computed.
|
||||||
|
// c1 and c2 are chosen to minimize the max(k1,k2).
|
||||||
|
func (curve *KoblitzCurve) splitK(k []byte) ([]byte, []byte, int, int) {
|
||||||
|
|
||||||
|
// All math here is done with big.Int, which is slow.
|
||||||
|
// At some point, it might be useful to write something similar to fieldVal
|
||||||
|
// but for N instead of P as the prime field if this ends up being a
|
||||||
|
// bottleneck.
|
||||||
|
bigIntK, c1, c2, tmp1, tmp2, k1, k2 := new(big.Int), new(big.Int), new(big.Int), new(big.Int), new(big.Int), new(big.Int), new(big.Int)
|
||||||
|
|
||||||
|
bigIntK.SetBytes(k)
|
||||||
|
// c1 = round(b2 * k / n) from step 4.
|
||||||
|
// Rounding isn't really necessary and costs too much, hence skipped
|
||||||
|
c1.Mul(curve.b2, bigIntK)
|
||||||
|
c1.Div(c1, curve.N)
|
||||||
|
// c2 = round(b1 * k / n) from step 4 (sign reversed to optimize one step)
|
||||||
|
// Rounding isn't really necessary and costs too much, hence skipped
|
||||||
|
c2.Mul(curve.b1, bigIntK)
|
||||||
|
c2.Div(c2, curve.N)
|
||||||
|
// k1 = k - c1 * a1 - c2 * a2 from step 5 (note c2's sign is reversed)
|
||||||
|
tmp1.Mul(c1, curve.a1)
|
||||||
|
tmp2.Mul(c2, curve.a2)
|
||||||
|
k1.Sub(bigIntK, tmp1)
|
||||||
|
k1.Add(k1, tmp2)
|
||||||
|
// k2 = - c1 * b1 - c2 * b2 from step 5 (note c2's sign is reversed)
|
||||||
|
tmp1.Mul(c1, curve.b1)
|
||||||
|
tmp2.Mul(c2, curve.b2)
|
||||||
|
k2.Sub(tmp2, tmp1)
|
||||||
|
|
||||||
|
// Note Bytes() throws out the sign of k1 and k2. This matters
|
||||||
|
// since k1 and/or k2 can be negative. Hence, we pass that
|
||||||
|
// back separately.
|
||||||
|
return k1.Bytes(), k2.Bytes(), k1.Sign(), k2.Sign()
|
||||||
|
}
|
||||||
|
|
||||||
|
// moduloReduce reduces k from more than 32 bytes to 32 bytes and under.
|
||||||
|
// This is done by doing a simple modulo curve.N. We can do this since
|
||||||
|
// G^N = 1 and thus any other valid point on the elliptical curve has the
|
||||||
|
// same order.
|
||||||
|
func (curve *KoblitzCurve) moduloReduce(k []byte) []byte {
|
||||||
|
// Since the order of G is curve.N, we can use a much smaller number
|
||||||
|
// by doing modulo curve.N
|
||||||
|
if len(k) > curve.byteSize {
|
||||||
|
// reduce k by performing modulo curve.N
|
||||||
|
tmpK := new(big.Int).SetBytes(k)
|
||||||
|
tmpK.Mod(tmpK, curve.N)
|
||||||
|
return tmpK.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
return k
|
||||||
|
}
|
||||||
|
|
||||||
|
// NAF takes a positive integer k and returns the Non-Adjacent Form (NAF)
|
||||||
|
// as two byte slices. The first is where 1's should be. The second is where
|
||||||
|
// -1's should be.
|
||||||
|
// NAF is also convenient in that on average, only 1/3rd of its values are
|
||||||
|
// non-zero.
|
||||||
|
// The algorithm here is from Guide to Elliptical Cryptography 3.30 (ref above)
|
||||||
|
// Essentially, this makes it possible to minimize the number of operations
|
||||||
|
// since the resulting ints returned will be at least 50% 0's.
|
||||||
|
func NAF(k []byte) ([]byte, []byte) {
|
||||||
|
|
||||||
|
// The essence of this algorithm is that whenever we have consecutive 1s
|
||||||
|
// in the binary, we want to put a -1 in the lowest bit and get a bunch of
|
||||||
|
// 0s up to the highest bit of consecutive 1s. This is due to this identity:
|
||||||
|
// 2^n + 2^(n-1) + 2^(n-2) + ... + 2^(n-k) = 2^(n+1) - 2^(n-k)
|
||||||
|
// The algorithm thus may need to go 1 more bit than the length of the bits
|
||||||
|
// we actually have, hence bits being 1 bit longer than was necessary.
|
||||||
|
// Since we need to know whether adding will cause a carry, we go from
|
||||||
|
// right-to-left in this addition.
|
||||||
|
var carry, curIsOne, nextIsOne bool
|
||||||
|
// these default to zero
|
||||||
|
retPos := make([]byte, len(k)+1)
|
||||||
|
retNeg := make([]byte, len(k)+1)
|
||||||
|
for i := len(k) - 1; i >= 0; i-- {
|
||||||
|
curByte := k[i]
|
||||||
|
for j := uint(0); j < 8; j++ {
|
||||||
|
curIsOne = curByte&1 == 1
|
||||||
|
if j == 7 {
|
||||||
|
if i == 0 {
|
||||||
|
nextIsOne = false
|
||||||
|
} else {
|
||||||
|
nextIsOne = k[i-1]&1 == 1
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
nextIsOne = curByte&2 == 2
|
||||||
|
}
|
||||||
|
if carry {
|
||||||
|
if curIsOne {
|
||||||
|
// This bit is 1, so we continue to carry and
|
||||||
|
// don't need to do anything
|
||||||
|
} else {
|
||||||
|
// We've hit a 0 after some number of 1s.
|
||||||
|
if nextIsOne {
|
||||||
|
// We start carrying again since we're starting
|
||||||
|
// a new sequence of 1s.
|
||||||
|
retNeg[i+1] += 1 << j
|
||||||
|
} else {
|
||||||
|
// We stop carrying since 1s have stopped.
|
||||||
|
carry = false
|
||||||
|
retPos[i+1] += 1 << j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if curIsOne {
|
||||||
|
if nextIsOne {
|
||||||
|
// if this is the start of at least 2 consecutive 1's
|
||||||
|
// we want to set the current one to -1 and start carrying
|
||||||
|
retNeg[i+1] += 1 << j
|
||||||
|
carry = true
|
||||||
|
} else {
|
||||||
|
// this is a singleton, not consecutive 1's.
|
||||||
|
retPos[i+1] += 1 << j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
curByte >>= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if carry {
|
||||||
|
retPos[0] = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return retPos, retNeg
|
||||||
|
}
|
||||||
|
|
||||||
|
// ScalarMult returns k*(Bx, By) where k is a big endian integer.
|
||||||
|
// Part of the elliptic.Curve interface.
|
||||||
|
func (curve *KoblitzCurve) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int) {
|
||||||
|
// Point Q = ∞ (point at infinity).
|
||||||
|
qx, qy, qz := new(fieldVal), new(fieldVal), new(fieldVal)
|
||||||
|
|
||||||
|
// decompose K into k1 and k2 in order to halve the number of EC ops
|
||||||
|
// see Algorithm 3.74 in Guide to Elliptical Curve Cryptography by
|
||||||
|
// Hankerson, et al.
|
||||||
|
k1, k2, signK1, signK2 := curve.splitK(curve.moduloReduce(k))
|
||||||
|
|
||||||
|
// The main equation here to remember is
|
||||||
|
// k * P = k1 * P + k2 * ϕ(P)
|
||||||
|
// P1 below is P in the equation, P2 below is ϕ(P) in the equation
|
||||||
|
p1x, p1y := curve.bigAffineToField(Bx, By)
|
||||||
|
// For NAF, we need the negative point
|
||||||
|
p1yNeg := new(fieldVal).NegateVal(p1y, 1)
|
||||||
|
p1z := new(fieldVal).SetInt(1)
|
||||||
|
// Note ϕ(x,y) = (βx,y), the Jacobian z coordinate is 1, so this math
|
||||||
|
// goes through.
|
||||||
|
p2x := new(fieldVal).Mul2(p1x, curve.beta)
|
||||||
|
p2y := new(fieldVal).Set(p1y)
|
||||||
|
// For NAF, we need the negative point
|
||||||
|
p2yNeg := new(fieldVal).NegateVal(p2y, 1)
|
||||||
|
p2z := new(fieldVal).SetInt(1)
|
||||||
|
|
||||||
|
// If k1 or k2 are negative, we flip the positive/negative values
|
||||||
|
if signK1 == -1 {
|
||||||
|
p1y, p1yNeg = p1yNeg, p1y
|
||||||
|
}
|
||||||
|
if signK2 == -1 {
|
||||||
|
p2y, p2yNeg = p2yNeg, p2y
|
||||||
|
}
|
||||||
|
|
||||||
|
// NAF versions of k1 and k2 should have a lot more zeros
|
||||||
|
// the Pos version of the bytes contain the +1's and the Neg versions
|
||||||
|
// contain the -1's
|
||||||
|
k1PosNAF, k1NegNAF := NAF(k1)
|
||||||
|
k2PosNAF, k2NegNAF := NAF(k2)
|
||||||
|
k1Len := len(k1PosNAF)
|
||||||
|
k2Len := len(k2PosNAF)
|
||||||
|
|
||||||
|
m := k1Len
|
||||||
|
if m < k2Len {
|
||||||
|
m = k2Len
|
||||||
|
}
|
||||||
|
|
||||||
|
// We add left-to-right using the NAF optimization. This is using
|
||||||
|
// algorithm 3.77 from Guide to Elliptical Curve Cryptography.
|
||||||
|
// This should be faster overall since there will be a lot more instances
|
||||||
|
// of 0, hence reducing the number of Jacobian additions at the cost
|
||||||
|
// of 1 possible extra doubling.
|
||||||
|
var k1BytePos, k1ByteNeg, k2BytePos, k2ByteNeg byte
|
||||||
|
for i := 0; i < m; i++ {
|
||||||
|
// Since we're going left-to-right, we need to pad the front with 0's
|
||||||
|
if i < m-k1Len {
|
||||||
|
k1BytePos = 0
|
||||||
|
k1ByteNeg = 0
|
||||||
|
} else {
|
||||||
|
k1BytePos = k1PosNAF[i-(m-k1Len)]
|
||||||
|
k1ByteNeg = k1NegNAF[i-(m-k1Len)]
|
||||||
|
}
|
||||||
|
if i < m-k2Len {
|
||||||
|
k2BytePos = 0
|
||||||
|
k2ByteNeg = 0
|
||||||
|
} else {
|
||||||
|
k2BytePos = k2PosNAF[i-(m-k2Len)]
|
||||||
|
k2ByteNeg = k2NegNAF[i-(m-k2Len)]
|
||||||
|
}
|
||||||
|
|
||||||
|
for j := 7; j >= 0; j-- {
|
||||||
|
// Q = 2 * Q
|
||||||
|
curve.doubleJacobian(qx, qy, qz, qx, qy, qz)
|
||||||
|
|
||||||
|
if k1BytePos&0x80 == 0x80 {
|
||||||
|
curve.addJacobian(qx, qy, qz, p1x, p1y, p1z, qx, qy, qz)
|
||||||
|
} else if k1ByteNeg&0x80 == 0x80 {
|
||||||
|
curve.addJacobian(qx, qy, qz, p1x, p1yNeg, p1z, qx, qy, qz)
|
||||||
|
}
|
||||||
|
|
||||||
|
if k2BytePos&0x80 == 0x80 {
|
||||||
|
curve.addJacobian(qx, qy, qz, p2x, p2y, p2z, qx, qy, qz)
|
||||||
|
} else if k2ByteNeg&0x80 == 0x80 {
|
||||||
|
curve.addJacobian(qx, qy, qz, p2x, p2yNeg, p2z, qx, qy, qz)
|
||||||
|
}
|
||||||
|
k1BytePos <<= 1
|
||||||
|
k1ByteNeg <<= 1
|
||||||
|
k2BytePos <<= 1
|
||||||
|
k2ByteNeg <<= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the Jacobian coordinate field values back to affine big.Ints.
|
||||||
|
return curve.fieldJacobianToBigAffine(qx, qy, qz)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ScalarBaseMult returns k*G where G is the base point of the group and k is a
|
||||||
|
// big endian integer.
|
||||||
|
// Part of the elliptic.Curve interface.
|
||||||
|
func (curve *KoblitzCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
|
||||||
|
newK := curve.moduloReduce(k)
|
||||||
|
diff := len(curve.bytePoints) - len(newK)
|
||||||
|
|
||||||
|
// Point Q = ∞ (point at infinity).
|
||||||
|
qx, qy, qz := new(fieldVal), new(fieldVal), new(fieldVal)
|
||||||
|
|
||||||
|
// curve.bytePoints has all 256 byte points for each 8-bit window. The
|
||||||
|
// strategy is to add up the byte points. This is best understood by
|
||||||
|
// expressing k in base-256 which it already sort of is.
|
||||||
|
// Each "digit" in the 8-bit window can be looked up using bytePoints
|
||||||
|
// and added together.
|
||||||
|
for i, byteVal := range newK {
|
||||||
|
point := curve.bytePoints[diff+i][byteVal]
|
||||||
|
curve.addJacobian(qx, qy, qz, &point[0], &point[1], &point[2], qx, qy, qz)
|
||||||
|
}
|
||||||
|
return curve.fieldJacobianToBigAffine(qx, qy, qz)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QPlus1Div4 returns the Q+1/4 constant for the curve for use in calculating
|
||||||
|
// square roots via exponention.
|
||||||
|
func (curve *KoblitzCurve) QPlus1Div4() *big.Int {
|
||||||
|
return curve.q
|
||||||
|
}
|
||||||
|
|
||||||
|
var initonce sync.Once
|
||||||
|
var secp256k1 KoblitzCurve
|
||||||
|
|
||||||
|
func initAll() {
|
||||||
|
initS256()
|
||||||
|
}
|
||||||
|
|
||||||
|
func initS256() {
|
||||||
|
// Curve parameters taken from [SECG] section 2.4.1.
|
||||||
|
secp256k1.CurveParams = new(elliptic.CurveParams)
|
||||||
|
secp256k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16)
|
||||||
|
secp256k1.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16)
|
||||||
|
secp256k1.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000000000000000000000000000007", 16)
|
||||||
|
secp256k1.Gx, _ = new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16)
|
||||||
|
secp256k1.Gy, _ = new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16)
|
||||||
|
secp256k1.BitSize = 256
|
||||||
|
secp256k1.H = 1
|
||||||
|
secp256k1.q = new(big.Int).Div(new(big.Int).Add(secp256k1.P,
|
||||||
|
big.NewInt(1)), big.NewInt(4))
|
||||||
|
|
||||||
|
// Deserialize and set the pre-computed table used to accelerate scalar
|
||||||
|
// base multiplication. This is hard-coded data, so any errors are
|
||||||
|
// panics because it means something is wrong in the source code.
|
||||||
|
if err := loadS256BytePoints(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next 6 constants are from Hal Finney's bitcointalk.org post:
|
||||||
|
// https://bitcointalk.org/index.php?topic=3238.msg45565#msg45565
|
||||||
|
// May he rest in peace.
|
||||||
|
// These have been independently verified by Dave Collins using
|
||||||
|
// an ecc math script.
|
||||||
|
secp256k1.lambda, _ = new(big.Int).SetString("5363AD4CC05C30E0A5261C028812645A122E22EA20816678DF02967C1B23BD72", 16)
|
||||||
|
secp256k1.beta = new(fieldVal).SetHex("7AE96A2B657C07106E64479EAC3434E99CF0497512F58995C1396C28719501EE")
|
||||||
|
secp256k1.a1, _ = new(big.Int).SetString("3086D221A7D46BCDE86C90E49284EB15", 16)
|
||||||
|
secp256k1.b1, _ = new(big.Int).SetString("-E4437ED6010E88286F547FA90ABFE4C3", 16)
|
||||||
|
secp256k1.a2, _ = new(big.Int).SetString("114CA50F7A8E2F3F657C1108D9D44CFD8", 16)
|
||||||
|
secp256k1.b2, _ = new(big.Int).SetString("3086D221A7D46BCDE86C90E49284EB15", 16)
|
||||||
|
|
||||||
|
// for convenience this gets computed repeatedly
|
||||||
|
secp256k1.byteSize = secp256k1.BitSize / 8
|
||||||
|
|
||||||
|
// Alternatively, we can use the parameters below, however, they seem
|
||||||
|
// to be about 8% slower.
|
||||||
|
// λ = AC9C52B33FA3CF1F5AD9E3FD77ED9BA4A880B9FC8EC739C2E0CFC810B51283CE
|
||||||
|
// β = 851695D49A83F8EF919BB86153CBCB16630FB68AED0A766A3EC693D68E6AFA40
|
||||||
|
// secp256k1.lambda, _ = new(big.Int).SetString("AC9C52B33FA3CF1F5AD9E3FD77ED9BA4A880B9FC8EC739C2E0CFC810B51283CE", 16)
|
||||||
|
// secp256k1.beta = new(fieldVal).SetHex("851695D49A83F8EF919BB86153CBCB16630FB68AED0A766A3EC693D68E6AFA40")
|
||||||
|
// secp256k1.a1, _ = new(big.Int).SetString("E4437ED6010E88286F547FA90ABFE4C3", 16)
|
||||||
|
// secp256k1.b1, _ = new(big.Int).SetString("-3086D221A7D46BCDE86C90E49284EB15", 16)
|
||||||
|
// secp256k1.a2, _ = new(big.Int).SetString("3086D221A7D46BCDE86C90E49284EB15", 16)
|
||||||
|
// secp256k1.b2, _ = new(big.Int).SetString("114CA50F7A8E2F3F657C1108D9D44CFD8", 16)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// S256 returns a Curve which implements secp256k1.
|
||||||
|
func S256() *KoblitzCurve {
|
||||||
|
initonce.Do(initAll)
|
||||||
|
return &secp256k1
|
||||||
|
}
|
861
btcec/btcec_test.go
Normal file
861
btcec/btcec_test.go
Normal file
|
@ -0,0 +1,861 @@
|
||||||
|
// Copyright 2011 The Go Authors. All rights reserved.
|
||||||
|
// Copyright 2011 ThePiachu. All rights reserved.
|
||||||
|
// Copyright 2013-2014 Conformal Systems LLC. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/btcec"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestAddJacobian tests addition of points projected in Jacobian coordinates.
|
||||||
|
func TestAddJacobian(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
x1, y1, z1 string // Coordinates (in hex) of first point to add
|
||||||
|
x2, y2, z2 string // Coordinates (in hex) of second point to add
|
||||||
|
x3, y3, z3 string // Coordinates (in hex) of expected point
|
||||||
|
}{
|
||||||
|
// Addition with a point at infinity (left hand side).
|
||||||
|
// ∞ + P = P
|
||||||
|
{
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
|
||||||
|
"131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
|
||||||
|
"1",
|
||||||
|
"d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
|
||||||
|
"131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
|
||||||
|
"1",
|
||||||
|
},
|
||||||
|
// Addition with a point at infinity (right hand side).
|
||||||
|
// P + ∞ = P
|
||||||
|
{
|
||||||
|
"d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
|
||||||
|
"131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
|
||||||
|
"1",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
|
||||||
|
"131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
|
||||||
|
"1",
|
||||||
|
},
|
||||||
|
|
||||||
|
// Addition with z1=z2=1 different x values.
|
||||||
|
{
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
|
||||||
|
"1",
|
||||||
|
"d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
|
||||||
|
"131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
|
||||||
|
"1",
|
||||||
|
"0cfbc7da1e569b334460788faae0286e68b3af7379d5504efc25e4dba16e46a6",
|
||||||
|
"e205f79361bbe0346b037b4010985dbf4f9e1e955e7d0d14aca876bfa79aad87",
|
||||||
|
"44a5646b446e3877a648d6d381370d9ef55a83b666ebce9df1b1d7d65b817b2f",
|
||||||
|
},
|
||||||
|
// Addition with z1=z2=1 same x opposite y.
|
||||||
|
// P(x, y, z) + P(x, -y, z) = infinity
|
||||||
|
{
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
|
||||||
|
"1",
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"f48e156428cf0276dc092da5856e182288d7569f97934a56fe44be60f0d359fd",
|
||||||
|
"1",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
},
|
||||||
|
// Addition with z1=z2=1 same point.
|
||||||
|
// P(x, y, z) + P(x, y, z) = 2P
|
||||||
|
{
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
|
||||||
|
"1",
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
|
||||||
|
"1",
|
||||||
|
"ec9f153b13ee7bd915882859635ea9730bf0dc7611b2c7b0e37ee64f87c50c27",
|
||||||
|
"b082b53702c466dcf6e984a35671756c506c67c2fcb8adb408c44dd0755c8f2a",
|
||||||
|
"16e3d537ae61fb1247eda4b4f523cfbaee5152c0d0d96b520376833c1e594464",
|
||||||
|
},
|
||||||
|
|
||||||
|
// Addition with z1=z2 (!=1) different x values.
|
||||||
|
{
|
||||||
|
"d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
|
||||||
|
"5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
|
||||||
|
"2",
|
||||||
|
"5d2fe112c21891d440f65a98473cb626111f8a234d2cd82f22172e369f002147",
|
||||||
|
"98e3386a0a622a35c4561ffb32308d8e1c6758e10ebb1b4ebd3d04b4eb0ecbe8",
|
||||||
|
"2",
|
||||||
|
"cfbc7da1e569b334460788faae0286e68b3af7379d5504efc25e4dba16e46a60",
|
||||||
|
"817de4d86ef80d1ac0ded00426176fd3e787a5579f43452b2a1db021e6ac3778",
|
||||||
|
"129591ad11b8e1de99235b4e04dc367bd56a0ed99baf3a77c6c75f5a6e05f08d",
|
||||||
|
},
|
||||||
|
// Addition with z1=z2 (!=1) same x opposite y.
|
||||||
|
// P(x, y, z) + P(x, -y, z) = infinity
|
||||||
|
{
|
||||||
|
"d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
|
||||||
|
"5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
|
||||||
|
"2",
|
||||||
|
"d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
|
||||||
|
"a470ab21467813b6e0496d2c2b70c11446bab4fcbc9a52b7f225f30e869aea9f",
|
||||||
|
"2",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
},
|
||||||
|
// Addition with z1=z2 (!=1) same point.
|
||||||
|
// P(x, y, z) + P(x, y, z) = 2P
|
||||||
|
{
|
||||||
|
"d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
|
||||||
|
"5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
|
||||||
|
"2",
|
||||||
|
"d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
|
||||||
|
"5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
|
||||||
|
"2",
|
||||||
|
"9f153b13ee7bd915882859635ea9730bf0dc7611b2c7b0e37ee65073c50fabac",
|
||||||
|
"2b53702c466dcf6e984a35671756c506c67c2fcb8adb408c44dd125dc91cb988",
|
||||||
|
"6e3d537ae61fb1247eda4b4f523cfbaee5152c0d0d96b520376833c2e5944a11",
|
||||||
|
},
|
||||||
|
|
||||||
|
// Addition with z1!=z2 and z2=1 different x values.
|
||||||
|
{
|
||||||
|
"d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
|
||||||
|
"5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
|
||||||
|
"2",
|
||||||
|
"d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
|
||||||
|
"131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
|
||||||
|
"1",
|
||||||
|
"3ef1f68795a6ccd1181e23eab80a1b9a2cebdcde755413bf097936eb5b91b4f3",
|
||||||
|
"0bef26c377c068d606f6802130bb7e9f3c3d2abcfa1a295950ed81133561cb04",
|
||||||
|
"252b235a2371c3bd3246b69c09b86cf7aad41db3375e74ef8d8ebeb4dc0be11a",
|
||||||
|
},
|
||||||
|
// Addition with z1!=z2 and z2=1 same x opposite y.
|
||||||
|
// P(x, y, z) + P(x, -y, z) = infinity
|
||||||
|
{
|
||||||
|
"d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
|
||||||
|
"5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
|
||||||
|
"2",
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"f48e156428cf0276dc092da5856e182288d7569f97934a56fe44be60f0d359fd",
|
||||||
|
"1",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
},
|
||||||
|
// Addition with z1!=z2 and z2=1 same point.
|
||||||
|
// P(x, y, z) + P(x, y, z) = 2P
|
||||||
|
{
|
||||||
|
"d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
|
||||||
|
"5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
|
||||||
|
"2",
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
|
||||||
|
"1",
|
||||||
|
"9f153b13ee7bd915882859635ea9730bf0dc7611b2c7b0e37ee65073c50fabac",
|
||||||
|
"2b53702c466dcf6e984a35671756c506c67c2fcb8adb408c44dd125dc91cb988",
|
||||||
|
"6e3d537ae61fb1247eda4b4f523cfbaee5152c0d0d96b520376833c2e5944a11",
|
||||||
|
},
|
||||||
|
|
||||||
|
// Addition with z1!=z2 and z2!=1 different x values.
|
||||||
|
// P(x, y, z) + P(x, y, z) = 2P
|
||||||
|
{
|
||||||
|
"d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
|
||||||
|
"5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
|
||||||
|
"2",
|
||||||
|
"91abba6a34b7481d922a4bd6a04899d5a686f6cf6da4e66a0cb427fb25c04bd4",
|
||||||
|
"03fede65e30b4e7576a2abefc963ddbf9fdccbf791b77c29beadefe49951f7d1",
|
||||||
|
"3",
|
||||||
|
"3f07081927fd3f6dadd4476614c89a09eba7f57c1c6c3b01fa2d64eac1eef31e",
|
||||||
|
"949166e04ebc7fd95a9d77e5dfd88d1492ecffd189792e3944eb2b765e09e031",
|
||||||
|
"eb8cba81bcffa4f44d75427506737e1f045f21e6d6f65543ee0e1d163540c931",
|
||||||
|
}, // Addition with z1!=z2 and z2!=1 same x opposite y.
|
||||||
|
// P(x, y, z) + P(x, -y, z) = infinity
|
||||||
|
{
|
||||||
|
"d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
|
||||||
|
"5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
|
||||||
|
"2",
|
||||||
|
"dcc3768780c74a0325e2851edad0dc8a566fa61a9e7fc4a34d13dcb509f99bc7",
|
||||||
|
"cafc41904dd5428934f7d075129c8ba46eb622d4fc88d72cd1401452664add18",
|
||||||
|
"3",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
},
|
||||||
|
// Addition with z1!=z2 and z2!=1 same point.
|
||||||
|
// P(x, y, z) + P(x, y, z) = 2P
|
||||||
|
{
|
||||||
|
"d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
|
||||||
|
"5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
|
||||||
|
"2",
|
||||||
|
"dcc3768780c74a0325e2851edad0dc8a566fa61a9e7fc4a34d13dcb509f99bc7",
|
||||||
|
"3503be6fb22abd76cb082f8aed63745b9149dd2b037728d32ebfebac99b51f17",
|
||||||
|
"3",
|
||||||
|
"9f153b13ee7bd915882859635ea9730bf0dc7611b2c7b0e37ee65073c50fabac",
|
||||||
|
"2b53702c466dcf6e984a35671756c506c67c2fcb8adb408c44dd125dc91cb988",
|
||||||
|
"6e3d537ae61fb1247eda4b4f523cfbaee5152c0d0d96b520376833c2e5944a11",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
// Convert hex to field values.
|
||||||
|
x1 := btcec.NewFieldVal().SetHex(test.x1)
|
||||||
|
y1 := btcec.NewFieldVal().SetHex(test.y1)
|
||||||
|
z1 := btcec.NewFieldVal().SetHex(test.z1)
|
||||||
|
x2 := btcec.NewFieldVal().SetHex(test.x2)
|
||||||
|
y2 := btcec.NewFieldVal().SetHex(test.y2)
|
||||||
|
z2 := btcec.NewFieldVal().SetHex(test.z2)
|
||||||
|
x3 := btcec.NewFieldVal().SetHex(test.x3)
|
||||||
|
y3 := btcec.NewFieldVal().SetHex(test.y3)
|
||||||
|
z3 := btcec.NewFieldVal().SetHex(test.z3)
|
||||||
|
|
||||||
|
// Ensure the test data is using points that are actually on
|
||||||
|
// the curve (or the point at infinity).
|
||||||
|
if !z1.IsZero() && !btcec.S256().TstIsJacobianOnCurve(x1, y1, z1) {
|
||||||
|
t.Errorf("#%d first point is not on the curve -- "+
|
||||||
|
"invalid test data", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !z2.IsZero() && !btcec.S256().TstIsJacobianOnCurve(x2, y2, z2) {
|
||||||
|
t.Errorf("#%d second point is not on the curve -- "+
|
||||||
|
"invalid test data", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !z3.IsZero() && !btcec.S256().TstIsJacobianOnCurve(x3, y3, z3) {
|
||||||
|
t.Errorf("#%d expected point is not on the curve -- "+
|
||||||
|
"invalid test data", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the two points.
|
||||||
|
rx, ry, rz := btcec.NewFieldVal(), btcec.NewFieldVal(), btcec.NewFieldVal()
|
||||||
|
btcec.S256().TstAddJacobian(x1, y1, z1, x2, y2, z2, rx, ry, rz)
|
||||||
|
|
||||||
|
// Ensure result matches expected.
|
||||||
|
if !rx.Equals(x3) || !ry.Equals(y3) || !rz.Equals(z3) {
|
||||||
|
t.Errorf("#%d wrong result\ngot: (%v, %v, %v)\n"+
|
||||||
|
"want: (%v, %v, %v)", i, rx, ry, rz, x3, y3, z3)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAddAffine tests addition of points in affine coordinates.
|
||||||
|
func TestAddAffine(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
x1, y1 string // Coordinates (in hex) of first point to add
|
||||||
|
x2, y2 string // Coordinates (in hex) of second point to add
|
||||||
|
x3, y3 string // Coordinates (in hex) of expected point
|
||||||
|
}{
|
||||||
|
// Addition with a point at infinity (left hand side).
|
||||||
|
// ∞ + P = P
|
||||||
|
{
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
|
||||||
|
"131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
|
||||||
|
"d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
|
||||||
|
"131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
|
||||||
|
},
|
||||||
|
// Addition with a point at infinity (right hand side).
|
||||||
|
// P + ∞ = P
|
||||||
|
{
|
||||||
|
"d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
|
||||||
|
"131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
|
||||||
|
"131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
|
||||||
|
},
|
||||||
|
|
||||||
|
// Addition with different x values.
|
||||||
|
{
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
|
||||||
|
"d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
|
||||||
|
"131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
|
||||||
|
"fd5b88c21d3143518d522cd2796f3d726793c88b3e05636bc829448e053fed69",
|
||||||
|
"21cf4f6a5be5ff6380234c50424a970b1f7e718f5eb58f68198c108d642a137f",
|
||||||
|
},
|
||||||
|
// Addition with same x opposite y.
|
||||||
|
// P(x, y) + P(x, -y) = infinity
|
||||||
|
{
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"f48e156428cf0276dc092da5856e182288d7569f97934a56fe44be60f0d359fd",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
},
|
||||||
|
// Addition with same point.
|
||||||
|
// P(x, y) + P(x, y) = 2P
|
||||||
|
{
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
|
||||||
|
"59477d88ae64a104dbb8d31ec4ce2d91b2fe50fa628fb6a064e22582196b365b",
|
||||||
|
"938dc8c0f13d1e75c987cb1a220501bd614b0d3dd9eb5c639847e1240216e3b6",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
// Convert hex to field values.
|
||||||
|
x1, y1 := fromHex(test.x1), fromHex(test.y1)
|
||||||
|
x2, y2 := fromHex(test.x2), fromHex(test.y2)
|
||||||
|
x3, y3 := fromHex(test.x3), fromHex(test.y3)
|
||||||
|
|
||||||
|
// Ensure the test data is using points that are actually on
|
||||||
|
// the curve (or the point at infinity).
|
||||||
|
if !(x1.Sign() == 0 && y1.Sign() == 0) && !btcec.S256().IsOnCurve(x1, y1) {
|
||||||
|
t.Errorf("#%d first point is not on the curve -- "+
|
||||||
|
"invalid test data", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !(x2.Sign() == 0 && y2.Sign() == 0) && !btcec.S256().IsOnCurve(x2, y2) {
|
||||||
|
t.Errorf("#%d second point is not on the curve -- "+
|
||||||
|
"invalid test data", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !(x3.Sign() == 0 && y3.Sign() == 0) && !btcec.S256().IsOnCurve(x3, y3) {
|
||||||
|
t.Errorf("#%d expected point is not on the curve -- "+
|
||||||
|
"invalid test data", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the two points.
|
||||||
|
rx, ry := btcec.S256().Add(x1, y1, x2, y2)
|
||||||
|
|
||||||
|
// Ensure result matches expected.
|
||||||
|
if rx.Cmp(x3) != 00 || ry.Cmp(y3) != 0 {
|
||||||
|
t.Errorf("#%d wrong result\ngot: (%x, %x)\n"+
|
||||||
|
"want: (%x, %x)", i, rx, ry, x3, y3)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDoubleJacobian tests doubling of points projected in Jacobian
|
||||||
|
// coordinates.
|
||||||
|
func TestDoubleJacobian(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
x1, y1, z1 string // Coordinates (in hex) of point to double
|
||||||
|
x3, y3, z3 string // Coordinates (in hex) of expected point
|
||||||
|
}{
|
||||||
|
// Doubling a point at infinity is still infinity.
|
||||||
|
{
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
},
|
||||||
|
// Doubling with z1=1.
|
||||||
|
{
|
||||||
|
"34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
|
||||||
|
"0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
|
||||||
|
"1",
|
||||||
|
"ec9f153b13ee7bd915882859635ea9730bf0dc7611b2c7b0e37ee64f87c50c27",
|
||||||
|
"b082b53702c466dcf6e984a35671756c506c67c2fcb8adb408c44dd0755c8f2a",
|
||||||
|
"16e3d537ae61fb1247eda4b4f523cfbaee5152c0d0d96b520376833c1e594464",
|
||||||
|
},
|
||||||
|
// Doubling with z1!=1.
|
||||||
|
{
|
||||||
|
"d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
|
||||||
|
"5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
|
||||||
|
"2",
|
||||||
|
"9f153b13ee7bd915882859635ea9730bf0dc7611b2c7b0e37ee65073c50fabac",
|
||||||
|
"2b53702c466dcf6e984a35671756c506c67c2fcb8adb408c44dd125dc91cb988",
|
||||||
|
"6e3d537ae61fb1247eda4b4f523cfbaee5152c0d0d96b520376833c2e5944a11",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
// Convert hex to field values.
|
||||||
|
x1 := btcec.NewFieldVal().SetHex(test.x1)
|
||||||
|
y1 := btcec.NewFieldVal().SetHex(test.y1)
|
||||||
|
z1 := btcec.NewFieldVal().SetHex(test.z1)
|
||||||
|
x3 := btcec.NewFieldVal().SetHex(test.x3)
|
||||||
|
y3 := btcec.NewFieldVal().SetHex(test.y3)
|
||||||
|
z3 := btcec.NewFieldVal().SetHex(test.z3)
|
||||||
|
|
||||||
|
// Ensure the test data is using points that are actually on
|
||||||
|
// the curve (or the point at infinity).
|
||||||
|
if !z1.IsZero() && !btcec.S256().TstIsJacobianOnCurve(x1, y1, z1) {
|
||||||
|
t.Errorf("#%d first point is not on the curve -- "+
|
||||||
|
"invalid test data", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !z3.IsZero() && !btcec.S256().TstIsJacobianOnCurve(x3, y3, z3) {
|
||||||
|
t.Errorf("#%d expected point is not on the curve -- "+
|
||||||
|
"invalid test data", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Double the point.
|
||||||
|
rx, ry, rz := btcec.NewFieldVal(), btcec.NewFieldVal(), btcec.NewFieldVal()
|
||||||
|
btcec.S256().TstDoubleJacobian(x1, y1, z1, rx, ry, rz)
|
||||||
|
|
||||||
|
// Ensure result matches expected.
|
||||||
|
if !rx.Equals(x3) || !ry.Equals(y3) || !rz.Equals(z3) {
|
||||||
|
t.Errorf("#%d wrong result\ngot: (%v, %v, %v)\n"+
|
||||||
|
"want: (%v, %v, %v)", i, rx, ry, rz, x3, y3, z3)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDoubleAffine tests doubling of points in affine coordinates.
|
||||||
|
func TestDoubleAffine(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
x1, y1 string // Coordinates (in hex) of point to double
|
||||||
|
x3, y3 string // Coordinates (in hex) of expected point
|
||||||
|
}{
|
||||||
|
// Doubling a point at infinity is still infinity.
|
||||||
|
// 2*∞ = ∞ (point at infinity)
|
||||||
|
|
||||||
|
{
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
"0",
|
||||||
|
},
|
||||||
|
|
||||||
|
// Random points.
|
||||||
|
{
|
||||||
|
"e41387ffd8baaeeb43c2faa44e141b19790e8ac1f7ff43d480dc132230536f86",
|
||||||
|
"1b88191d430f559896149c86cbcb703193105e3cf3213c0c3556399836a2b899",
|
||||||
|
"88da47a089d333371bd798c548ef7caae76e737c1980b452d367b3cfe3082c19",
|
||||||
|
"3b6f659b09a362821dfcfefdbfbc2e59b935ba081b6c249eb147b3c2100b1bc1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"b3589b5d984f03ef7c80aeae444f919374799edf18d375cab10489a3009cff0c",
|
||||||
|
"c26cf343875b3630e15bccc61202815b5d8f1fd11308934a584a5babe69db36a",
|
||||||
|
"e193860172998751e527bb12563855602a227fc1f612523394da53b746bb2fb1",
|
||||||
|
"2bfcf13d2f5ab8bb5c611fab5ebbed3dc2f057062b39a335224c22f090c04789",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"2b31a40fbebe3440d43ac28dba23eee71c62762c3fe3dbd88b4ab82dc6a82340",
|
||||||
|
"9ba7deb02f5c010e217607fd49d58db78ec273371ea828b49891ce2fd74959a1",
|
||||||
|
"2c8d5ef0d343b1a1a48aa336078eadda8481cb048d9305dc4fdf7ee5f65973a2",
|
||||||
|
"bb4914ac729e26d3cd8f8dc8f702f3f4bb7e0e9c5ae43335f6e94c2de6c3dc95",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"61c64b760b51981fab54716d5078ab7dffc93730b1d1823477e27c51f6904c7a",
|
||||||
|
"ef6eb16ea1a36af69d7f66524c75a3a5e84c13be8fbc2e811e0563c5405e49bd",
|
||||||
|
"5f0dcdd2595f5ad83318a0f9da481039e36f135005420393e72dfca985b482f4",
|
||||||
|
"a01c849b0837065c1cb481b0932c441f49d1cab1b4b9f355c35173d93f110ae0",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
// Convert hex to field values.
|
||||||
|
x1, y1 := fromHex(test.x1), fromHex(test.y1)
|
||||||
|
x3, y3 := fromHex(test.x3), fromHex(test.y3)
|
||||||
|
|
||||||
|
// Ensure the test data is using points that are actually on
|
||||||
|
// the curve (or the point at infinity).
|
||||||
|
if !(x1.Sign() == 0 && y1.Sign() == 0) && !btcec.S256().IsOnCurve(x1, y1) {
|
||||||
|
t.Errorf("#%d first point is not on the curve -- "+
|
||||||
|
"invalid test data", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !(x3.Sign() == 0 && y3.Sign() == 0) && !btcec.S256().IsOnCurve(x3, y3) {
|
||||||
|
t.Errorf("#%d expected point is not on the curve -- "+
|
||||||
|
"invalid test data", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Double the point.
|
||||||
|
rx, ry := btcec.S256().Double(x1, y1)
|
||||||
|
|
||||||
|
// Ensure result matches expected.
|
||||||
|
if rx.Cmp(x3) != 00 || ry.Cmp(y3) != 0 {
|
||||||
|
t.Errorf("#%d wrong result\ngot: (%x, %x)\n"+
|
||||||
|
"want: (%x, %x)", i, rx, ry, x3, y3)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOnCurve(t *testing.T) {
|
||||||
|
s256 := btcec.S256()
|
||||||
|
if !s256.IsOnCurve(s256.Params().Gx, s256.Params().Gy) {
|
||||||
|
t.Errorf("FAIL S256")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type baseMultTest struct {
|
||||||
|
k string
|
||||||
|
x, y string
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: add more test vectors
|
||||||
|
var s256BaseMultTests = []baseMultTest{
|
||||||
|
{
|
||||||
|
"AA5E28D6A97A2479A65527F7290311A3624D4CC0FA1578598EE3C2613BF99522",
|
||||||
|
"34F9460F0E4F08393D192B3C5133A6BA099AA0AD9FD54EBCCFACDFA239FF49C6",
|
||||||
|
"B71EA9BD730FD8923F6D25A7A91E7DD7728A960686CB5A901BB419E0F2CA232",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"7E2B897B8CEBC6361663AD410835639826D590F393D90A9538881735256DFAE3",
|
||||||
|
"D74BF844B0862475103D96A611CF2D898447E288D34B360BC885CB8CE7C00575",
|
||||||
|
"131C670D414C4546B88AC3FF664611B1C38CEB1C21D76369D7A7A0969D61D97D",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"6461E6DF0FE7DFD05329F41BF771B86578143D4DD1F7866FB4CA7E97C5FA945D",
|
||||||
|
"E8AECC370AEDD953483719A116711963CE201AC3EB21D3F3257BB48668C6A72F",
|
||||||
|
"C25CAF2F0EBA1DDB2F0F3F47866299EF907867B7D27E95B3873BF98397B24EE1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"376A3A2CDCD12581EFFF13EE4AD44C4044B8A0524C42422A7E1E181E4DEECCEC",
|
||||||
|
"14890E61FCD4B0BD92E5B36C81372CA6FED471EF3AA60A3E415EE4FE987DABA1",
|
||||||
|
"297B858D9F752AB42D3BCA67EE0EB6DCD1C2B7B0DBE23397E66ADC272263F982",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"1B22644A7BE026548810C378D0B2994EEFA6D2B9881803CB02CEFF865287D1B9",
|
||||||
|
"F73C65EAD01C5126F28F442D087689BFA08E12763E0CEC1D35B01751FD735ED3",
|
||||||
|
"F449A8376906482A84ED01479BD18882B919C140D638307F0C0934BA12590BDE",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: test different curves as well?
|
||||||
|
func TestBaseMult(t *testing.T) {
|
||||||
|
s256 := btcec.S256()
|
||||||
|
for i, e := range s256BaseMultTests {
|
||||||
|
k, ok := new(big.Int).SetString(e.k, 16)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("%d: bad value for k: %s", i, e.k)
|
||||||
|
}
|
||||||
|
x, y := s256.ScalarBaseMult(k.Bytes())
|
||||||
|
if fmt.Sprintf("%X", x) != e.x || fmt.Sprintf("%X", y) != e.y {
|
||||||
|
t.Errorf("%d: bad output for k=%s: got (%X, %X), want (%s, %s)", i, e.k, x, y, e.x, e.y)
|
||||||
|
}
|
||||||
|
if testing.Short() && i > 5 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBaseMultVerify(t *testing.T) {
|
||||||
|
s256 := btcec.S256()
|
||||||
|
for bytes := 1; bytes < 40; bytes++ {
|
||||||
|
for i := 0; i < 30; i++ {
|
||||||
|
data := make([]byte, bytes)
|
||||||
|
_, err := rand.Read(data)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to read random data for %s", i)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
x, y := s256.ScalarBaseMult(data)
|
||||||
|
xWant, yWant := s256.ScalarMult(s256.Gx, s256.Gy, data)
|
||||||
|
if x.Cmp(xWant) != 0 || y.Cmp(yWant) != 0 {
|
||||||
|
t.Errorf("%d: bad output for %X: got (%X, %X), want (%X, %X)", i, data, x, y, xWant, yWant)
|
||||||
|
}
|
||||||
|
if testing.Short() && i > 2 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScalarMult(t *testing.T) {
|
||||||
|
// Strategy for this test:
|
||||||
|
// Get a random exponent from the generator point at first
|
||||||
|
// This creates a new point which is used in the next iteration
|
||||||
|
// Use another random exponent on the new point.
|
||||||
|
// We use BaseMult to verify by multiplying the previous exponent
|
||||||
|
// and the new random exponent together (mod N)
|
||||||
|
s256 := btcec.S256()
|
||||||
|
x, y := s256.Gx, s256.Gy
|
||||||
|
exponent := big.NewInt(1)
|
||||||
|
for i := 0; i < 1024; i++ {
|
||||||
|
data := make([]byte, 32)
|
||||||
|
_, err := rand.Read(data)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to read random data at %d", i)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
x, y = s256.ScalarMult(x, y, data)
|
||||||
|
exponent.Mul(exponent, new(big.Int).SetBytes(data))
|
||||||
|
xWant, yWant := s256.ScalarBaseMult(exponent.Bytes())
|
||||||
|
if x.Cmp(xWant) != 0 || y.Cmp(yWant) != 0 {
|
||||||
|
t.Fatalf("%d: bad output for %X: got (%X, %X), want (%X, %X)", i, data, x, y, xWant, yWant)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: test more curves?
|
||||||
|
func BenchmarkBaseMult(b *testing.B) {
|
||||||
|
b.ResetTimer()
|
||||||
|
s256 := btcec.S256()
|
||||||
|
e := s256BaseMultTests[0] //TODO: check, used to be 25 instead of 0, but it's probably ok
|
||||||
|
k, _ := new(big.Int).SetString(e.k, 16)
|
||||||
|
b.StartTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
s256.ScalarBaseMult(k.Bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test this curve's usage with the ecdsa package.
|
||||||
|
|
||||||
|
func testKeyGeneration(t *testing.T, c *btcec.KoblitzCurve, tag string) {
|
||||||
|
priv, err := btcec.NewPrivateKey(c)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: error: %s", tag, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !c.IsOnCurve(priv.PublicKey.X, priv.PublicKey.Y) {
|
||||||
|
t.Errorf("%s: public key invalid: %s", tag, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKeyGeneration(t *testing.T) {
|
||||||
|
testKeyGeneration(t, btcec.S256(), "S256")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testSignAndVerify(t *testing.T, c *btcec.KoblitzCurve, tag string) {
|
||||||
|
priv, _ := btcec.NewPrivateKey(c)
|
||||||
|
pub := priv.PubKey()
|
||||||
|
|
||||||
|
hashed := []byte("testing")
|
||||||
|
sig, err := priv.Sign(hashed)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: error signing: %s", tag, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !sig.Verify(hashed, pub) {
|
||||||
|
t.Errorf("%s: Verify failed", tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
hashed[0] ^= 0xff
|
||||||
|
if sig.Verify(hashed, pub) {
|
||||||
|
t.Errorf("%s: Verify always works!", tag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSignAndVerify(t *testing.T) {
|
||||||
|
testSignAndVerify(t, btcec.S256(), "S256")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNAF(t *testing.T) {
|
||||||
|
negOne := big.NewInt(-1)
|
||||||
|
one := big.NewInt(1)
|
||||||
|
two := big.NewInt(2)
|
||||||
|
for i := 0; i < 1024; i++ {
|
||||||
|
data := make([]byte, 32)
|
||||||
|
_, err := rand.Read(data)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to read random data at %d", i)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
nafPos, nafNeg := btcec.NAF(data)
|
||||||
|
want := new(big.Int).SetBytes(data)
|
||||||
|
got := big.NewInt(0)
|
||||||
|
// Check that the NAF representation comes up with the right number
|
||||||
|
for i := 0; i < len(nafPos); i++ {
|
||||||
|
bytePos := nafPos[i]
|
||||||
|
byteNeg := nafNeg[i]
|
||||||
|
for j := 7; j >= 0; j-- {
|
||||||
|
got.Mul(got, two)
|
||||||
|
if bytePos&0x80 == 0x80 {
|
||||||
|
got.Add(got, one)
|
||||||
|
} else if byteNeg&0x80 == 0x80 {
|
||||||
|
got.Add(got, negOne)
|
||||||
|
}
|
||||||
|
bytePos <<= 1
|
||||||
|
byteNeg <<= 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if got.Cmp(want) != 0 {
|
||||||
|
t.Errorf("%d: Failed NAF got %X want %X", i, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromHex(s string) *big.Int {
|
||||||
|
r, ok := new(big.Int).SetString(s, 16)
|
||||||
|
if !ok {
|
||||||
|
panic("bad hex")
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// These test vectors were taken from
|
||||||
|
// http://csrc.nist.gov/groups/STM/cavp/documents/dss/ecdsatestvectors.zip
|
||||||
|
var testVectors = []struct {
|
||||||
|
msg string
|
||||||
|
Qx, Qy string
|
||||||
|
r, s string
|
||||||
|
ok bool
|
||||||
|
}{
|
||||||
|
/*
|
||||||
|
* All of these tests are disabled since they are for P224, not sec256k1.
|
||||||
|
* they are left here as an example of test vectors for when some *real*
|
||||||
|
* vectors may be found.
|
||||||
|
* - oga@conformal.com
|
||||||
|
{
|
||||||
|
"09626b45493672e48f3d1226a3aff3201960e577d33a7f72c7eb055302db8fe8ed61685dd036b554942a5737cd1512cdf811ee0c00e6dd2f08c69f08643be396e85dafda664801e772cdb7396868ac47b172245b41986aa2648cb77fbbfa562581be06651355a0c4b090f9d17d8f0ab6cced4e0c9d386cf465a516630f0231bd",
|
||||||
|
"9504b5b82d97a264d8b3735e0568decabc4b6ca275bc53cbadfc1c40",
|
||||||
|
"03426f80e477603b10dee670939623e3da91a94267fc4e51726009ed",
|
||||||
|
"81d3ac609f9575d742028dd496450a58a60eea2dcf8b9842994916e1",
|
||||||
|
"96a8c5f382c992e8f30ccce9af120b067ec1d74678fa8445232f75a5",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"96b2b6536f6df29be8567a72528aceeaccbaa66c66c534f3868ca9778b02faadb182e4ed34662e73b9d52ecbe9dc8e875fc05033c493108b380689ebf47e5b062e6a0cdb3dd34ce5fe347d92768d72f7b9b377c20aea927043b509c078ed2467d7113405d2ddd458811e6faf41c403a2a239240180f1430a6f4330df5d77de37",
|
||||||
|
"851e3100368a22478a0029353045ae40d1d8202ef4d6533cfdddafd8",
|
||||||
|
"205302ac69457dd345e86465afa72ee8c74ca97e2b0b999aec1f10c2",
|
||||||
|
"4450c2d38b697e990721aa2dbb56578d32b4f5aeb3b9072baa955ee0",
|
||||||
|
"e26d4b589166f7b4ba4b1c8fce823fa47aad22f8c9c396b8c6526e12",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"86778dbb4a068a01047a8d245d632f636c11d2ad350740b36fad90428b454ad0f120cb558d12ea5c8a23db595d87543d06d1ef489263d01ee529871eb68737efdb8ff85bc7787b61514bed85b7e01d6be209e0a4eb0db5c8df58a5c5bf706d76cb2bdf7800208639e05b89517155d11688236e6a47ed37d8e5a2b1e0adea338e",
|
||||||
|
"ad5bda09d319a717c1721acd6688d17020b31b47eef1edea57ceeffc",
|
||||||
|
"c8ce98e181770a7c9418c73c63d01494b8b80a41098c5ea50692c984",
|
||||||
|
"de5558c257ab4134e52c19d8db3b224a1899cbd08cc508ce8721d5e9",
|
||||||
|
"745db7af5a477e5046705c0a5eff1f52cb94a79d481f0c5a5e108ecd",
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"4bc6ef1958556686dab1e39c3700054a304cbd8f5928603dcd97fafd1f29e69394679b638f71c9344ce6a535d104803d22119f57b5f9477e253817a52afa9bfbc9811d6cc8c8be6b6566c6ef48b439bbb532abe30627548c598867f3861ba0b154dc1c3deca06eb28df8efd28258554b5179883a36fbb1eecf4f93ee19d41e3d",
|
||||||
|
"cc5eea2edf964018bdc0504a3793e4d2145142caa09a72ac5fb8d3e8",
|
||||||
|
"a48d78ae5d08aa725342773975a00d4219cf7a8029bb8cf3c17c374a",
|
||||||
|
"67b861344b4e416d4094472faf4272f6d54a497177fbc5f9ef292836",
|
||||||
|
"1d54f3fcdad795bf3b23408ecbac3e1321d1d66f2e4e3d05f41f7020",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bb658732acbf3147729959eb7318a2058308b2739ec58907dd5b11cfa3ecf69a1752b7b7d806fe00ec402d18f96039f0b78dbb90a59c4414fb33f1f4e02e4089de4122cd93df5263a95be4d7084e2126493892816e6a5b4ed123cb705bf930c8f67af0fb4514d5769232a9b008a803af225160ce63f675bd4872c4c97b146e5e",
|
||||||
|
"6234c936e27bf141fc7534bfc0a7eedc657f91308203f1dcbd642855",
|
||||||
|
"27983d87ca785ef4892c3591ef4a944b1deb125dd58bd351034a6f84",
|
||||||
|
"e94e05b42d01d0b965ffdd6c3a97a36a771e8ea71003de76c4ecb13f",
|
||||||
|
"1dc6464ffeefbd7872a081a5926e9fc3e66d123f1784340ba17737e9",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"7c00be9123bfa2c4290be1d8bc2942c7f897d9a5b7917e3aabd97ef1aab890f148400a89abd554d19bec9d8ed911ce57b22fbcf6d30ca2115f13ce0a3f569a23bad39ee645f624c49c60dcfc11e7d2be24de9c905596d8f23624d63dc46591d1f740e46f982bfae453f107e80db23545782be23ce43708245896fc54e1ee5c43",
|
||||||
|
"9f3f037282aaf14d4772edffff331bbdda845c3f65780498cde334f1",
|
||||||
|
"8308ee5a16e3bcb721b6bc30000a0419bc1aaedd761be7f658334066",
|
||||||
|
"6381d7804a8808e3c17901e4d283b89449096a8fba993388fa11dc54",
|
||||||
|
"8e858f6b5b253686a86b757bad23658cda53115ac565abca4e3d9f57",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cffc122a44840dc705bb37130069921be313d8bde0b66201aebc48add028ca131914ef2e705d6bedd19dc6cf9459bbb0f27cdfe3c50483808ffcdaffbeaa5f062e097180f07a40ef4ab6ed03fe07ed6bcfb8afeb42c97eafa2e8a8df469de07317c5e1494c41547478eff4d8c7d9f0f484ad90fedf6e1c35ee68fa73f1691601",
|
||||||
|
"a03b88a10d930002c7b17ca6af2fd3e88fa000edf787dc594f8d4fd4",
|
||||||
|
"e0cf7acd6ddc758e64847fe4df9915ebda2f67cdd5ec979aa57421f5",
|
||||||
|
"387b84dcf37dc343c7d2c5beb82f0bf8bd894b395a7b894565d296c1",
|
||||||
|
"4adc12ce7d20a89ce3925e10491c731b15ddb3f339610857a21b53b4",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"26e0e0cafd85b43d16255908ccfd1f061c680df75aba3081246b337495783052ba06c60f4a486c1591a4048bae11b4d7fec4f161d80bdc9a7b79d23e44433ed625eab280521a37f23dd3e1bdc5c6a6cfaa026f3c45cf703e76dab57add93fe844dd4cda67dc3bddd01f9152579e49df60969b10f09ce9372fdd806b0c7301866",
|
||||||
|
"9a8983c42f2b5a87c37a00458b5970320d247f0c8a88536440173f7d",
|
||||||
|
"15e489ec6355351361900299088cfe8359f04fe0cab78dde952be80c",
|
||||||
|
"929a21baa173d438ec9f28d6a585a2f9abcfc0a4300898668e476dc0",
|
||||||
|
"59a853f046da8318de77ff43f26fe95a92ee296fa3f7e56ce086c872",
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"1078eac124f48ae4f807e946971d0de3db3748dd349b14cca5c942560fb25401b2252744f18ad5e455d2d97ed5ae745f55ff509c6c8e64606afe17809affa855c4c4cdcaf6b69ab4846aa5624ed0687541aee6f2224d929685736c6a23906d974d3c257abce1a3fb8db5951b89ecb0cda92b5207d93f6618fd0f893c32cf6a6e",
|
||||||
|
"d6e55820bb62c2be97650302d59d667a411956138306bd566e5c3c2b",
|
||||||
|
"631ab0d64eaf28a71b9cbd27a7a88682a2167cee6251c44e3810894f",
|
||||||
|
"65af72bc7721eb71c2298a0eb4eed3cec96a737cc49125706308b129",
|
||||||
|
"bd5a987c78e2d51598dbd9c34a9035b0069c580edefdacee17ad892a",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"919deb1fdd831c23481dfdb2475dcbe325b04c34f82561ced3d2df0b3d749b36e255c4928973769d46de8b95f162b53cd666cad9ae145e7fcfba97919f703d864efc11eac5f260a5d920d780c52899e5d76f8fe66936ff82130761231f536e6a3d59792f784902c469aa897aabf9a0678f93446610d56d5e0981e4c8a563556b",
|
||||||
|
"269b455b1024eb92d860a420f143ac1286b8cce43031562ae7664574",
|
||||||
|
"baeb6ca274a77c44a0247e5eb12ca72bdd9a698b3f3ae69c9f1aaa57",
|
||||||
|
"cb4ec2160f04613eb0dfe4608486091a25eb12aa4dec1afe91cfb008",
|
||||||
|
"40b01d8cd06589481574f958b98ca08ade9d2a8fe31024375c01bb40",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"6e012361250dacf6166d2dd1aa7be544c3206a9d43464b3fcd90f3f8cf48d08ec099b59ba6fe7d9bdcfaf244120aed1695d8be32d1b1cd6f143982ab945d635fb48a7c76831c0460851a3d62b7209c30cd9c2abdbe3d2a5282a9fcde1a6f418dd23c409bc351896b9b34d7d3a1a63bbaf3d677e612d4a80fa14829386a64b33f",
|
||||||
|
"6d2d695efc6b43b13c14111f2109608f1020e3e03b5e21cfdbc82fcd",
|
||||||
|
"26a4859296b7e360b69cf40be7bd97ceaffa3d07743c8489fc47ca1b",
|
||||||
|
"9a8cb5f2fdc288b7183c5b32d8e546fc2ed1ca4285eeae00c8b572ad",
|
||||||
|
"8c623f357b5d0057b10cdb1a1593dab57cda7bdec9cf868157a79b97",
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bf6bd7356a52b234fe24d25557200971fc803836f6fec3cade9642b13a8e7af10ab48b749de76aada9d8927f9b12f75a2c383ca7358e2566c4bb4f156fce1fd4e87ef8c8d2b6b1bdd351460feb22cdca0437ac10ca5e0abbbce9834483af20e4835386f8b1c96daaa41554ceee56730aac04f23a5c765812efa746051f396566",
|
||||||
|
"14250131b2599939cf2d6bc491be80ddfe7ad9de644387ee67de2d40",
|
||||||
|
"b5dc473b5d014cd504022043c475d3f93c319a8bdcb7262d9e741803",
|
||||||
|
"4f21642f2201278a95339a80f75cc91f8321fcb3c9462562f6cbf145",
|
||||||
|
"452a5f816ea1f75dee4fd514fa91a0d6a43622981966c59a1b371ff8",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"0eb7f4032f90f0bd3cf9473d6d9525d264d14c031a10acd31a053443ed5fe919d5ac35e0be77813071b4062f0b5fdf58ad5f637b76b0b305aec18f82441b6e607b44cdf6e0e3c7c57f24e6fd565e39430af4a6b1d979821ed0175fa03e3125506847654d7e1ae904ce1190ae38dc5919e257bdac2db142a6e7cd4da6c2e83770",
|
||||||
|
"d1f342b7790a1667370a1840255ac5bbbdc66f0bc00ae977d99260ac",
|
||||||
|
"76416cabae2de9a1000b4646338b774baabfa3db4673790771220cdb",
|
||||||
|
"bc85e3fc143d19a7271b2f9e1c04b86146073f3fab4dda1c3b1f35ca",
|
||||||
|
"9a5c70ede3c48d5f43307a0c2a4871934424a3303b815df4bb0f128e",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"5cc25348a05d85e56d4b03cec450128727bc537c66ec3a9fb613c151033b5e86878632249cba83adcefc6c1e35dcd31702929c3b57871cda5c18d1cf8f9650a25b917efaed56032e43b6fc398509f0d2997306d8f26675f3a8683b79ce17128e006aa0903b39eeb2f1001be65de0520115e6f919de902b32c38d691a69c58c92",
|
||||||
|
"7e49a7abf16a792e4c7bbc4d251820a2abd22d9f2fc252a7bf59c9a6",
|
||||||
|
"44236a8fb4791c228c26637c28ae59503a2f450d4cfb0dc42aa843b9",
|
||||||
|
"084461b4050285a1a85b2113be76a17878d849e6bc489f4d84f15cd8",
|
||||||
|
"079b5bddcc4d45de8dbdfd39f69817c7e5afa454a894d03ee1eaaac3",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"1951533ce33afb58935e39e363d8497a8dd0442018fd96dff167b3b23d7206a3ee182a3194765df4768a3284e23b8696c199b4686e670d60c9d782f08794a4bccc05cffffbd1a12acd9eb1cfa01f7ebe124da66ecff4599ea7720c3be4bb7285daa1a86ebf53b042bd23208d468c1b3aa87381f8e1ad63e2b4c2ba5efcf05845",
|
||||||
|
"31945d12ebaf4d81f02be2b1768ed80784bf35cf5e2ff53438c11493",
|
||||||
|
"a62bebffac987e3b9d3ec451eb64c462cdf7b4aa0b1bbb131ceaa0a4",
|
||||||
|
"bc3c32b19e42b710bca5c6aaa128564da3ddb2726b25f33603d2af3c",
|
||||||
|
"ed1a719cc0c507edc5239d76fe50e2306c145ad252bd481da04180c0",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestVectors(t *testing.T) {
|
||||||
|
sha := sha1.New()
|
||||||
|
|
||||||
|
for i, test := range testVectors {
|
||||||
|
pub := btcec.PublicKey{
|
||||||
|
Curve: btcec.S256(),
|
||||||
|
X: fromHex(test.Qx),
|
||||||
|
Y: fromHex(test.Qy),
|
||||||
|
}
|
||||||
|
msg, _ := hex.DecodeString(test.msg)
|
||||||
|
sha.Reset()
|
||||||
|
sha.Write(msg)
|
||||||
|
hashed := sha.Sum(nil)
|
||||||
|
sig := btcec.Signature{R: fromHex(test.r), S: fromHex(test.s)}
|
||||||
|
if fuck := sig.Verify(hashed, &pub); fuck != test.ok {
|
||||||
|
//t.Errorf("%d: bad result %v %v", i, pub, hashed)
|
||||||
|
t.Errorf("%d: bad result %v instead of %v", i, fuck,
|
||||||
|
test.ok)
|
||||||
|
}
|
||||||
|
if testing.Short() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
21
btcec/doc.go
Normal file
21
btcec/doc.go
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package btcec implements support for the elliptic curves needed for bitcoin.
|
||||||
|
|
||||||
|
Bitcoin uses elliptic curve cryptography using koblitz curves
|
||||||
|
(specifically secp256k1) for cryptographic functions. See
|
||||||
|
http://www.secg.org/collateral/sec2_final.pdf for details on the
|
||||||
|
standard.
|
||||||
|
|
||||||
|
This package provides the data structures and functions implementing the
|
||||||
|
crypto/elliptic Curve interface in order to permit using these curves
|
||||||
|
with the standard crypto/ecdsa package provided with go. Helper
|
||||||
|
functionality is provided to parse signatures and public keys from
|
||||||
|
standard formats. It was designed for use with btcd, but should be
|
||||||
|
general enough for other uses of elliptic curve crypto. It was originally based
|
||||||
|
on some initial work by ThePiachu, but has significantly diverged since then.
|
||||||
|
*/
|
||||||
|
package btcec
|
89
btcec/example_test.go
Normal file
89
btcec/example_test.go
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
// Copyright (c) 2014 Conformal Systems LLC.
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/btcec"
|
||||||
|
"github.com/btcsuite/btcd/wire"
|
||||||
|
)
|
||||||
|
|
||||||
|
// This example demonstrates signing a message with a secp256k1 private key that
|
||||||
|
// is first parsed form raw bytes and serializing the generated signature.
|
||||||
|
func Example_signMessage() {
|
||||||
|
// Decode a hex-encoded private key.
|
||||||
|
pkBytes, err := hex.DecodeString("22a47fa09a223f2aa079edf85a7c2d4f87" +
|
||||||
|
"20ee63e502ee2869afab7de234b80c")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
privKey, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), pkBytes)
|
||||||
|
|
||||||
|
// Sign a message using the private key.
|
||||||
|
message := "test message"
|
||||||
|
messageHash := wire.DoubleSha256([]byte(message))
|
||||||
|
signature, err := privKey.Sign(messageHash)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize and display the signature.
|
||||||
|
//
|
||||||
|
// NOTE: This is commented out for the example since the signature
|
||||||
|
// produced uses random numbers and therefore will always be different.
|
||||||
|
//fmt.Printf("Serialized Signature: %x\n", signature.Serialize())
|
||||||
|
|
||||||
|
// Verify the signature for the message using the public key.
|
||||||
|
verified := signature.Verify(messageHash, pubKey)
|
||||||
|
fmt.Printf("Signature Verified? %v\n", verified)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Signature Verified? true
|
||||||
|
}
|
||||||
|
|
||||||
|
// This example demonstrates verifying a secp256k1 signature against a public
|
||||||
|
// key that is first parsed from raw bytes. The signature is also parsed from
|
||||||
|
// raw bytes.
|
||||||
|
func Example_verifySignature() {
|
||||||
|
// Decode hex-encoded serialized public key.
|
||||||
|
pubKeyBytes, err := hex.DecodeString("02a673638cb9587cb68ea08dbef685c" +
|
||||||
|
"6f2d2a751a8b3c6f2a7e9a4999e6e4bfaf5")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pubKey, err := btcec.ParsePubKey(pubKeyBytes, btcec.S256())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode hex-encoded serialized signature.
|
||||||
|
sigBytes, err := hex.DecodeString("30450220090ebfb3690a0ff115bb1b38b" +
|
||||||
|
"8b323a667b7653454f1bccb06d4bbdca42c2079022100ec95778b51e707" +
|
||||||
|
"1cb1205f8bde9af6592fc978b0452dafe599481c46d6b2e479")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
signature, err := btcec.ParseSignature(sigBytes, btcec.S256())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the signature for the message using the public key.
|
||||||
|
message := "test message"
|
||||||
|
messageHash := wire.DoubleSha256([]byte(message))
|
||||||
|
verified := signature.Verify(messageHash, pubKey)
|
||||||
|
fmt.Println("Signature Verified?", verified)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// Signature Verified? true
|
||||||
|
}
|
1263
btcec/field.go
Normal file
1263
btcec/field.go
Normal file
File diff suppressed because it is too large
Load diff
744
btcec/field_test.go
Normal file
744
btcec/field_test.go
Normal file
|
@ -0,0 +1,744 @@
|
||||||
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||||
|
// Copyright (c) 2013-2014 Dave Collins
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/btcec"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestSetInt ensures that setting a field value to various native integers
|
||||||
|
// works as expected.
|
||||||
|
func TestSetInt(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in uint
|
||||||
|
raw [10]uint32
|
||||||
|
}{
|
||||||
|
{5, [10]uint32{5, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
|
||||||
|
// 2^26
|
||||||
|
{67108864, [10]uint32{67108864, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
|
||||||
|
// 2^26 + 1
|
||||||
|
{67108865, [10]uint32{67108865, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
|
||||||
|
// 2^32 - 1
|
||||||
|
{4294967295, [10]uint32{4294967295, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().SetInt(test.in)
|
||||||
|
result := f.TstRawInts()
|
||||||
|
if !reflect.DeepEqual(result, test.raw) {
|
||||||
|
t.Errorf("fieldVal.Set #%d wrong result\ngot: %v\n"+
|
||||||
|
"want: %v", i, result, test.raw)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestZero ensures that zeroing a field value zero works as expected.
|
||||||
|
func TestZero(t *testing.T) {
|
||||||
|
f := btcec.NewFieldVal().SetInt(2)
|
||||||
|
f.Zero()
|
||||||
|
for idx, rawInt := range f.TstRawInts() {
|
||||||
|
if rawInt != 0 {
|
||||||
|
t.Errorf("internal field integer at index #%d is not "+
|
||||||
|
"zero - got %d", idx, rawInt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestIsZero ensures that checking if a field IsZero works as expected.
|
||||||
|
func TestIsZero(t *testing.T) {
|
||||||
|
f := btcec.NewFieldVal()
|
||||||
|
if !f.IsZero() {
|
||||||
|
t.Errorf("new field value is not zero - got %v (rawints %x)", f,
|
||||||
|
f.TstRawInts())
|
||||||
|
}
|
||||||
|
|
||||||
|
f.SetInt(1)
|
||||||
|
if f.IsZero() {
|
||||||
|
t.Errorf("field claims it's zero when it's not - got %v "+
|
||||||
|
"(raw rawints %x)", f, f.TstRawInts())
|
||||||
|
}
|
||||||
|
|
||||||
|
f.Zero()
|
||||||
|
if !f.IsZero() {
|
||||||
|
t.Errorf("field claims it's not zero when it is - got %v "+
|
||||||
|
"(raw rawints %x)", f, f.TstRawInts())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestStringer ensures the stringer returns the appropriate hex string.
|
||||||
|
func TestStringer(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"0", "0000000000000000000000000000000000000000000000000000000000000000"},
|
||||||
|
{"1", "0000000000000000000000000000000000000000000000000000000000000001"},
|
||||||
|
{"a", "000000000000000000000000000000000000000000000000000000000000000a"},
|
||||||
|
{"b", "000000000000000000000000000000000000000000000000000000000000000b"},
|
||||||
|
{"c", "000000000000000000000000000000000000000000000000000000000000000c"},
|
||||||
|
{"d", "000000000000000000000000000000000000000000000000000000000000000d"},
|
||||||
|
{"e", "000000000000000000000000000000000000000000000000000000000000000e"},
|
||||||
|
{"f", "000000000000000000000000000000000000000000000000000000000000000f"},
|
||||||
|
{"f0", "00000000000000000000000000000000000000000000000000000000000000f0"},
|
||||||
|
// 2^26-1
|
||||||
|
{
|
||||||
|
"3ffffff",
|
||||||
|
"0000000000000000000000000000000000000000000000000000000003ffffff",
|
||||||
|
},
|
||||||
|
// 2^32-1
|
||||||
|
{
|
||||||
|
"ffffffff",
|
||||||
|
"00000000000000000000000000000000000000000000000000000000ffffffff",
|
||||||
|
},
|
||||||
|
// 2^64-1
|
||||||
|
{
|
||||||
|
"ffffffffffffffff",
|
||||||
|
"000000000000000000000000000000000000000000000000ffffffffffffffff",
|
||||||
|
},
|
||||||
|
// 2^96-1
|
||||||
|
{
|
||||||
|
"ffffffffffffffffffffffff",
|
||||||
|
"0000000000000000000000000000000000000000ffffffffffffffffffffffff",
|
||||||
|
},
|
||||||
|
// 2^128-1
|
||||||
|
{
|
||||||
|
"ffffffffffffffffffffffffffffffff",
|
||||||
|
"00000000000000000000000000000000ffffffffffffffffffffffffffffffff",
|
||||||
|
},
|
||||||
|
// 2^160-1
|
||||||
|
{
|
||||||
|
"ffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"000000000000000000000000ffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
},
|
||||||
|
// 2^192-1
|
||||||
|
{
|
||||||
|
"ffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
},
|
||||||
|
// 2^224-1
|
||||||
|
{
|
||||||
|
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
"00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
},
|
||||||
|
// 2^256-4294968273 (the btcec prime, so should result in 0)
|
||||||
|
{
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||||
|
},
|
||||||
|
// 2^256-4294968274 (the secp256k1 prime+1, so should result in 1)
|
||||||
|
{
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc30",
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000001",
|
||||||
|
},
|
||||||
|
|
||||||
|
// Invalid hex
|
||||||
|
{"g", "0000000000000000000000000000000000000000000000000000000000000000"},
|
||||||
|
{"1h", "0000000000000000000000000000000000000000000000000000000000000000"},
|
||||||
|
{"i1", "0000000000000000000000000000000000000000000000000000000000000000"},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().SetHex(test.in)
|
||||||
|
result := f.String()
|
||||||
|
if result != test.expected {
|
||||||
|
t.Errorf("fieldVal.String #%d wrong result\ngot: %v\n"+
|
||||||
|
"want: %v", i, result, test.expected)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestNormalize ensures that normalizing the internal field words works as
|
||||||
|
// expected.
|
||||||
|
func TestNormalize(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
raw [10]uint32 // Intentionally denormalized value
|
||||||
|
normalized [10]uint32 // Normalized form of the raw value
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
[10]uint32{0x00000005, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x00000005, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^26
|
||||||
|
{
|
||||||
|
[10]uint32{0x04000000, 0x0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x00000000, 0x1, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^26 + 1
|
||||||
|
{
|
||||||
|
[10]uint32{0x04000001, 0x0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x00000001, 0x1, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^32 - 1
|
||||||
|
{
|
||||||
|
[10]uint32{0xffffffff, 0x00, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x03ffffff, 0x3f, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^32
|
||||||
|
{
|
||||||
|
[10]uint32{0x04000000, 0x3f, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x00000000, 0x40, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^32 + 1
|
||||||
|
{
|
||||||
|
[10]uint32{0x04000001, 0x3f, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x00000001, 0x40, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^64 - 1
|
||||||
|
{
|
||||||
|
[10]uint32{0xffffffff, 0xffffffc0, 0xfc0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x03ffffff, 0x03ffffff, 0xfff, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^64
|
||||||
|
{
|
||||||
|
[10]uint32{0x04000000, 0x03ffffff, 0x0fff, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x00000000, 0x00000000, 0x1000, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^64 + 1
|
||||||
|
{
|
||||||
|
[10]uint32{0x04000001, 0x03ffffff, 0x0fff, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x00000001, 0x00000000, 0x1000, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^96 - 1
|
||||||
|
{
|
||||||
|
[10]uint32{0xffffffff, 0xffffffc0, 0xffffffc0, 0x3ffc0, 0, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x03ffffff, 0x03ffffff, 0x03ffffff, 0x3ffff, 0, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^96
|
||||||
|
{
|
||||||
|
[10]uint32{0x04000000, 0x03ffffff, 0x03ffffff, 0x3ffff, 0, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x00000000, 0x00000000, 0x00000000, 0x40000, 0, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^128 - 1
|
||||||
|
{
|
||||||
|
[10]uint32{0xffffffff, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffc0, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0xffffff, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^128
|
||||||
|
{
|
||||||
|
[10]uint32{0x04000000, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x0ffffff, 0, 0, 0, 0, 0},
|
||||||
|
[10]uint32{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x1000000, 0, 0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
// 2^256 - 4294968273 (secp256k1 prime)
|
||||||
|
{
|
||||||
|
[10]uint32{0xfffffc2f, 0xffffff80, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0x3fffc0},
|
||||||
|
[10]uint32{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000000},
|
||||||
|
},
|
||||||
|
// 2^256 - 1
|
||||||
|
{
|
||||||
|
[10]uint32{0xffffffff, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0x3fffc0},
|
||||||
|
[10]uint32{0x000003d0, 0x00000040, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000000},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().TstSetRawInts(test.raw).Normalize()
|
||||||
|
result := f.TstRawInts()
|
||||||
|
if !reflect.DeepEqual(result, test.normalized) {
|
||||||
|
t.Errorf("fieldVal.Set #%d wrong normalized result\n"+
|
||||||
|
"got: %x\nwant: %x", i, result, test.normalized)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestIsOdd ensures that checking if a field value IsOdd works as expected.
|
||||||
|
func TestIsOdd(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in string // hex encoded value
|
||||||
|
expected bool // expected oddness
|
||||||
|
}{
|
||||||
|
{"0", false},
|
||||||
|
{"1", true},
|
||||||
|
{"2", false},
|
||||||
|
// 2^32 - 1
|
||||||
|
{"ffffffff", true},
|
||||||
|
// 2^64 - 2
|
||||||
|
{"fffffffffffffffe", false},
|
||||||
|
// secp256k1 prime
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().SetHex(test.in)
|
||||||
|
result := f.IsOdd()
|
||||||
|
if result != test.expected {
|
||||||
|
t.Errorf("fieldVal.IsOdd #%d wrong result\n"+
|
||||||
|
"got: %v\nwant: %v", i, result, test.expected)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestEquals ensures that checking two field values for equality via Equals
|
||||||
|
// works as expected.
|
||||||
|
func TestEquals(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in1 string // hex encoded value
|
||||||
|
in2 string // hex encoded value
|
||||||
|
expected bool // expected equality
|
||||||
|
}{
|
||||||
|
{"0", "0", true},
|
||||||
|
{"0", "1", false},
|
||||||
|
{"1", "0", false},
|
||||||
|
// 2^32 - 1 == 2^32 - 1?
|
||||||
|
{"ffffffff", "ffffffff", true},
|
||||||
|
// 2^64 - 1 == 2^64 - 2?
|
||||||
|
{"ffffffffffffffff", "fffffffffffffffe", false},
|
||||||
|
// 0 == prime (mod prime)?
|
||||||
|
{"0", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", true},
|
||||||
|
// 1 == prime+1 (mod prime)?
|
||||||
|
{"1", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc30", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().SetHex(test.in1).Normalize()
|
||||||
|
f2 := btcec.NewFieldVal().SetHex(test.in2).Normalize()
|
||||||
|
result := f.Equals(f2)
|
||||||
|
if result != test.expected {
|
||||||
|
t.Errorf("fieldVal.Equals #%d wrong result\n"+
|
||||||
|
"got: %v\nwant: %v", i, result, test.expected)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestNegate ensures that negating field values via Negate works as expected.
|
||||||
|
func TestNegate(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in string // hex encoded value
|
||||||
|
expected string // expected hex encoded value
|
||||||
|
}{
|
||||||
|
// secp256k1 prime (aka 0)
|
||||||
|
{"0", "0"},
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "0"},
|
||||||
|
{"0", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"},
|
||||||
|
// secp256k1 prime-1
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", "1"},
|
||||||
|
{"1", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e"},
|
||||||
|
// secp256k1 prime-2
|
||||||
|
{"2", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d"},
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d", "2"},
|
||||||
|
// Random sampling
|
||||||
|
{
|
||||||
|
"b3d9aac9c5e43910b4385b53c7e78c21d4cd5f8e683c633aed04c233efc2e120",
|
||||||
|
"4c2655363a1bc6ef4bc7a4ac381873de2b32a07197c39cc512fb3dcb103d1b0f",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"f8a85984fee5a12a7c8dd08830d83423c937d77c379e4a958e447a25f407733f",
|
||||||
|
"757a67b011a5ed583722f77cf27cbdc36c82883c861b56a71bb85d90bf888f0",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"45ee6142a7fda884211e93352ed6cb2807800e419533be723a9548823ece8312",
|
||||||
|
"ba119ebd5802577bdee16ccad12934d7f87ff1be6acc418dc56ab77cc131791d",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"53c2a668f07e411a2e473e1c3b6dcb495dec1227af27673761d44afe5b43d22b",
|
||||||
|
"ac3d59970f81bee5d1b8c1e3c49234b6a213edd850d898c89e2bb500a4bc2a04",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().SetHex(test.in).Normalize()
|
||||||
|
expected := btcec.NewFieldVal().SetHex(test.expected).Normalize()
|
||||||
|
result := f.Negate(1).Normalize()
|
||||||
|
if !result.Equals(expected) {
|
||||||
|
t.Errorf("fieldVal.Negate #%d wrong result\n"+
|
||||||
|
"got: %v\nwant: %v", i, result, expected)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAddInt ensures that adding an integer to field values via AddInt works as
|
||||||
|
// expected.
|
||||||
|
func TestAddInt(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in1 string // hex encoded value
|
||||||
|
in2 uint // unsigned integer to add to the value above
|
||||||
|
expected string // expected hex encoded value
|
||||||
|
}{
|
||||||
|
{"0", 1, "1"},
|
||||||
|
{"1", 0, "1"},
|
||||||
|
{"1", 1, "2"},
|
||||||
|
// secp256k1 prime-1 + 1
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", 1, "0"},
|
||||||
|
// secp256k1 prime + 1
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", 1, "1"},
|
||||||
|
// Random samples.
|
||||||
|
{
|
||||||
|
"ff95ad9315aff04ab4af0ce673620c7145dc85d03bab5ba4b09ca2c4dec2d6c1",
|
||||||
|
0x10f,
|
||||||
|
"ff95ad9315aff04ab4af0ce673620c7145dc85d03bab5ba4b09ca2c4dec2d7d0",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"44bdae6b772e7987941f1ba314e6a5b7804a4c12c00961b57d20f41deea9cecf",
|
||||||
|
0x2cf11d41,
|
||||||
|
"44bdae6b772e7987941f1ba314e6a5b7804a4c12c00961b57d20f41e1b9aec10",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"88c3ecae67b591935fb1f6a9499c35315ffad766adca665c50b55f7105122c9c",
|
||||||
|
0x4829aa2d,
|
||||||
|
"88c3ecae67b591935fb1f6a9499c35315ffad766adca665c50b55f714d3bd6c9",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"8523e9edf360ca32a95aae4e57fcde5a542b471d08a974d94ea0ee09a015e2a6",
|
||||||
|
0xa21265a5,
|
||||||
|
"8523e9edf360ca32a95aae4e57fcde5a542b471d08a974d94ea0ee0a4228484b",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().SetHex(test.in1).Normalize()
|
||||||
|
expected := btcec.NewFieldVal().SetHex(test.expected).Normalize()
|
||||||
|
result := f.AddInt(test.in2).Normalize()
|
||||||
|
if !result.Equals(expected) {
|
||||||
|
t.Errorf("fieldVal.AddInt #%d wrong result\n"+
|
||||||
|
"got: %v\nwant: %v", i, result, expected)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAdd ensures that adding two field values together via Add works as
|
||||||
|
// expected.
|
||||||
|
func TestAdd(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in1 string // first hex encoded value
|
||||||
|
in2 string // second hex encoded value to add
|
||||||
|
expected string // expected hex encoded value
|
||||||
|
}{
|
||||||
|
{"0", "1", "1"},
|
||||||
|
{"1", "0", "1"},
|
||||||
|
{"1", "1", "2"},
|
||||||
|
// secp256k1 prime-1 + 1
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", "1", "0"},
|
||||||
|
// secp256k1 prime + 1
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "1", "1"},
|
||||||
|
// Random samples.
|
||||||
|
{
|
||||||
|
"2b2012f975404e5065b4292fb8bed0a5d315eacf24c74d8b27e73bcc5430edcc",
|
||||||
|
"2c3cefa4e4753e8aeec6ac4c12d99da4d78accefda3b7885d4c6bab46c86db92",
|
||||||
|
"575d029e59b58cdb547ad57bcb986e4aaaa0b7beff02c610fcadf680c0b7c95e",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"8131e8722fe59bb189692b96c9f38de92885730f1dd39ab025daffb94c97f79c",
|
||||||
|
"ff5454b765f0aab5f0977dcc629becc84cabeb9def48e79c6aadb2622c490fa9",
|
||||||
|
"80863d2995d646677a00a9632c8f7ab175315ead0d1c824c9088b21c78e10b16",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"c7c95e93d0892b2b2cdd77e80eb646ea61be7a30ac7e097e9f843af73fad5c22",
|
||||||
|
"3afe6f91a74dfc1c7f15c34907ee981656c37236d946767dd53ccad9190e437c",
|
||||||
|
"02c7ce2577d72747abf33b3116a4df00b881ec6785c47ffc74c105d158bba36f",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fd1c26f6a23381e5d785ba889494ec059369b888ad8431cd67d8c934b580dbe1",
|
||||||
|
"a475aa5a31dcca90ef5b53c097d9133d6b7117474b41e7877bb199590fc0489c",
|
||||||
|
"a191d150d4104c76c6e10e492c6dff42fedacfcff8c61954e38a628ec541284e",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().SetHex(test.in1).Normalize()
|
||||||
|
f2 := btcec.NewFieldVal().SetHex(test.in2).Normalize()
|
||||||
|
expected := btcec.NewFieldVal().SetHex(test.expected).Normalize()
|
||||||
|
result := f.Add(f2).Normalize()
|
||||||
|
if !result.Equals(expected) {
|
||||||
|
t.Errorf("fieldVal.Add #%d wrong result\n"+
|
||||||
|
"got: %v\nwant: %v", i, result, expected)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestAdd2 ensures that adding two field values together via Add2 works as
|
||||||
|
// expected.
|
||||||
|
func TestAdd2(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in1 string // first hex encoded value
|
||||||
|
in2 string // second hex encoded value to add
|
||||||
|
expected string // expected hex encoded value
|
||||||
|
}{
|
||||||
|
{"0", "1", "1"},
|
||||||
|
{"1", "0", "1"},
|
||||||
|
{"1", "1", "2"},
|
||||||
|
// secp256k1 prime-1 + 1
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", "1", "0"},
|
||||||
|
// secp256k1 prime + 1
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "1", "1"},
|
||||||
|
// Random samples.
|
||||||
|
{
|
||||||
|
"ad82b8d1cc136e23e9fd77fe2c7db1fe5a2ecbfcbde59ab3529758334f862d28",
|
||||||
|
"4d6a4e95d6d61f4f46b528bebe152d408fd741157a28f415639347a84f6f574b",
|
||||||
|
"faed0767a2e98d7330b2a0bcea92df3eea060d12380e8ec8b62a9fdb9ef58473",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"f3f43a2540054a86e1df98547ec1c0e157b193e5350fb4a3c3ea214b228ac5e7",
|
||||||
|
"25706572592690ea3ddc951a1b48b504a4c83dc253756e1b96d56fdfb3199522",
|
||||||
|
"19649f97992bdb711fbc2d6e9a0a75e5fc79d1a7888522bf5abf912bd5a45eda",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"6915bb94eef13ff1bb9b2633d997e13b9b1157c713363cc0e891416d6734f5b8",
|
||||||
|
"11f90d6ac6fe1c4e8900b1c85fb575c251ec31b9bc34b35ada0aea1c21eded22",
|
||||||
|
"7b0ec8ffb5ef5c40449bd7fc394d56fdecfd8980cf6af01bc29c2b898922e2da",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"48b0c9eae622eed9335b747968544eb3e75cb2dc8128388f948aa30f88cabde4",
|
||||||
|
"0989882b52f85f9d524a3a3061a0e01f46d597839d2ba637320f4b9510c8d2d5",
|
||||||
|
"523a5216391b4e7685a5aea9c9f52ed32e324a601e53dec6c699eea4999390b9",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().SetHex(test.in1).Normalize()
|
||||||
|
f2 := btcec.NewFieldVal().SetHex(test.in2).Normalize()
|
||||||
|
expected := btcec.NewFieldVal().SetHex(test.expected).Normalize()
|
||||||
|
result := f.Add2(f, f2).Normalize()
|
||||||
|
if !result.Equals(expected) {
|
||||||
|
t.Errorf("fieldVal.Add2 #%d wrong result\n"+
|
||||||
|
"got: %v\nwant: %v", i, result, expected)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMulInt ensures that adding an integer to field values via MulInt works as
|
||||||
|
// expected.
|
||||||
|
func TestMulInt(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in1 string // hex encoded value
|
||||||
|
in2 uint // unsigned integer to multiply with value above
|
||||||
|
expected string // expected hex encoded value
|
||||||
|
}{
|
||||||
|
{"0", 0, "0"},
|
||||||
|
{"1", 0, "0"},
|
||||||
|
{"0", 1, "0"},
|
||||||
|
{"1", 1, "1"},
|
||||||
|
// secp256k1 prime-1 * 2
|
||||||
|
{
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
|
||||||
|
2,
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d",
|
||||||
|
},
|
||||||
|
// secp256k1 prime * 3
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", 3, "0"},
|
||||||
|
// secp256k1 prime-1 * 8
|
||||||
|
{
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
|
||||||
|
8,
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc27",
|
||||||
|
},
|
||||||
|
// Random samples for first value. The second value is limited
|
||||||
|
// to 8 since that is the maximum int used in the elliptic curve
|
||||||
|
// calculations.
|
||||||
|
{
|
||||||
|
"b75674dc9180d306c692163ac5e089f7cef166af99645c0c23568ab6d967288a",
|
||||||
|
6,
|
||||||
|
"4c06bd2b6904f228a76c8560a3433bced9a8681d985a2848d407404d186b0280",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"54873298ac2b5ba8591c125ae54931f5ea72040aee07b208d6135476fb5b9c0e",
|
||||||
|
3,
|
||||||
|
"fd9597ca048212f90b543710afdb95e1bf560c20ca17161a8239fd64f212d42a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"7c30fbd363a74c17e1198f56b090b59bbb6c8755a74927a6cba7a54843506401",
|
||||||
|
5,
|
||||||
|
"6cf4eb20f2447c77657fccb172d38c0aa91ea4ac446dc641fa463a6b5091fba7",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fb4529be3e027a3d1587d8a500b72f2d312e3577340ef5175f96d113be4c2ceb",
|
||||||
|
8,
|
||||||
|
"da294df1f013d1e8ac3ec52805b979698971abb9a077a8bafcb688a4f261820f",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().SetHex(test.in1).Normalize()
|
||||||
|
expected := btcec.NewFieldVal().SetHex(test.expected).Normalize()
|
||||||
|
result := f.MulInt(test.in2).Normalize()
|
||||||
|
if !result.Equals(expected) {
|
||||||
|
t.Errorf("fieldVal.MulInt #%d wrong result\n"+
|
||||||
|
"got: %v\nwant: %v", i, result, expected)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMul ensures that multiplying two field valuess via Mul works as expected.
|
||||||
|
func TestMul(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in1 string // first hex encoded value
|
||||||
|
in2 string // second hex encoded value to multiply with
|
||||||
|
expected string // expected hex encoded value
|
||||||
|
}{
|
||||||
|
{"0", "0", "0"},
|
||||||
|
{"1", "0", "0"},
|
||||||
|
{"0", "1", "0"},
|
||||||
|
{"1", "1", "1"},
|
||||||
|
// secp256k1 prime-1 * 2
|
||||||
|
{
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
|
||||||
|
"2",
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d",
|
||||||
|
},
|
||||||
|
// secp256k1 prime * 3
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "3", "0"},
|
||||||
|
// secp256k1 prime-1 * 8
|
||||||
|
{
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
|
||||||
|
"8",
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc27",
|
||||||
|
},
|
||||||
|
// Random samples.
|
||||||
|
{
|
||||||
|
"cfb81753d5ef499a98ecc04c62cb7768c2e4f1740032946db1c12e405248137e",
|
||||||
|
"58f355ad27b4d75fb7db0442452e732c436c1f7c5a7c4e214fa9cc031426a7d3",
|
||||||
|
"1018cd2d7c2535235b71e18db9cd98027386328d2fa6a14b36ec663c4c87282b",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"26e9d61d1cdf3920e9928e85fa3df3e7556ef9ab1d14ec56d8b4fc8ed37235bf",
|
||||||
|
"2dfc4bbe537afee979c644f8c97b31e58be5296d6dbc460091eae630c98511cf",
|
||||||
|
"da85f48da2dc371e223a1ae63bd30b7e7ee45ae9b189ac43ff357e9ef8cf107a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"5db64ed5afb71646c8b231585d5b2bf7e628590154e0854c4c29920b999ff351",
|
||||||
|
"279cfae5eea5d09ade8e6a7409182f9de40981bc31c84c3d3dfe1d933f152e9a",
|
||||||
|
"2c78fbae91792dd0b157abe3054920049b1879a7cc9d98cfda927d83be411b37",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"b66dfc1f96820b07d2bdbd559c19319a3a73c97ceb7b3d662f4fe75ecb6819e6",
|
||||||
|
"bf774aba43e3e49eb63a6e18037d1118152568f1a3ac4ec8b89aeb6ff8008ae1",
|
||||||
|
"c4f016558ca8e950c21c3f7fc15f640293a979c7b01754ee7f8b3340d4902ebb",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().SetHex(test.in1).Normalize()
|
||||||
|
f2 := btcec.NewFieldVal().SetHex(test.in2).Normalize()
|
||||||
|
expected := btcec.NewFieldVal().SetHex(test.expected).Normalize()
|
||||||
|
result := f.Mul(f2).Normalize()
|
||||||
|
if !result.Equals(expected) {
|
||||||
|
t.Errorf("fieldVal.Mul #%d wrong result\n"+
|
||||||
|
"got: %v\nwant: %v", i, result, expected)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSquare ensures that squaring field values via Square works as expected.
|
||||||
|
func TestSquare(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in string // hex encoded value
|
||||||
|
expected string // expected hex encoded value
|
||||||
|
}{
|
||||||
|
// secp256k1 prime (aka 0)
|
||||||
|
{"0", "0"},
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "0"},
|
||||||
|
{"0", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"},
|
||||||
|
// secp256k1 prime-1
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", "1"},
|
||||||
|
// secp256k1 prime-2
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d", "4"},
|
||||||
|
// Random sampling
|
||||||
|
{
|
||||||
|
"b0ba920360ea8436a216128047aab9766d8faf468895eb5090fc8241ec758896",
|
||||||
|
"133896b0b69fda8ce9f648b9a3af38f345290c9eea3cbd35bafcadf7c34653d3",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"c55d0d730b1d0285a1599995938b042a756e6e8857d390165ffab480af61cbd5",
|
||||||
|
"cd81758b3f5877cbe7e5b0a10cebfa73bcbf0957ca6453e63ee8954ab7780bee",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"e89c1f9a70d93651a1ba4bca5b78658f00de65a66014a25544d3365b0ab82324",
|
||||||
|
"39ffc7a43e5dbef78fd5d0354fb82c6d34f5a08735e34df29da14665b43aa1f",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"7dc26186079d22bcbe1614aa20ae627e62d72f9be7ad1e99cac0feb438956f05",
|
||||||
|
"bf86bcfc4edb3d81f916853adfda80c07c57745b008b60f560b1912f95bce8ae",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().SetHex(test.in).Normalize()
|
||||||
|
expected := btcec.NewFieldVal().SetHex(test.expected).Normalize()
|
||||||
|
result := f.Square().Normalize()
|
||||||
|
if !result.Equals(expected) {
|
||||||
|
t.Errorf("fieldVal.Square #%d wrong result\n"+
|
||||||
|
"got: %v\nwant: %v", i, result, expected)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestInverse ensures that finding the multiplicative inverse via Inverse works
|
||||||
|
// as expected.
|
||||||
|
func TestInverse(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in string // hex encoded value
|
||||||
|
expected string // expected hex encoded value
|
||||||
|
}{
|
||||||
|
// secp256k1 prime (aka 0)
|
||||||
|
{"0", "0"},
|
||||||
|
{"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "0"},
|
||||||
|
{"0", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"},
|
||||||
|
// secp256k1 prime-1
|
||||||
|
{
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
|
||||||
|
},
|
||||||
|
// secp256k1 prime-2
|
||||||
|
{
|
||||||
|
"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d",
|
||||||
|
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffff7ffffe17",
|
||||||
|
},
|
||||||
|
// Random sampling
|
||||||
|
{
|
||||||
|
"16fb970147a9acc73654d4be233cc48b875ce20a2122d24f073d29bd28805aca",
|
||||||
|
"987aeb257b063df0c6d1334051c47092b6d8766c4bf10c463786d93f5bc54354",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"69d1323ce9f1f7b3bd3c7320b0d6311408e30281e273e39a0d8c7ee1c8257919",
|
||||||
|
"49340981fa9b8d3dad72de470b34f547ed9179c3953797d0943af67806f4bb6",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"e0debf988ae098ecda07d0b57713e97c6d213db19753e8c95aa12a2fc1cc5272",
|
||||||
|
"64f58077b68af5b656b413ea366863f7b2819f8d27375d9c4d9804135ca220c2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"dcd394f91f74c2ba16aad74a22bb0ed47fe857774b8f2d6c09e28bfb14642878",
|
||||||
|
"fb848ec64d0be572a63c38fe83df5e7f3d032f60bf8c969ef67d36bf4ada22a9",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
f := btcec.NewFieldVal().SetHex(test.in).Normalize()
|
||||||
|
expected := btcec.NewFieldVal().SetHex(test.expected).Normalize()
|
||||||
|
result := f.Inverse().Normalize()
|
||||||
|
if !result.Equals(expected) {
|
||||||
|
t.Errorf("fieldVal.Inverse #%d wrong result\n"+
|
||||||
|
"got: %v\nwant: %v", i, result, expected)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
54
btcec/genprecomps.go
Normal file
54
btcec/genprecomps.go
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
// Copyright 2015 Conformal Systems LLC. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// 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 Conformal Systems LLC.")
|
||||||
|
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 = []byte(%q)\n", encoded)
|
||||||
|
}
|
73
btcec/gensecp256k1.go
Normal file
73
btcec/gensecp256k1.go
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
||||||
|
// 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.
|
||||||
|
// This build tag is set during go generate.
|
||||||
|
// +build gensecp256k1
|
||||||
|
|
||||||
|
package btcec
|
||||||
|
|
||||||
|
import "encoding/binary"
|
||||||
|
|
||||||
|
// secp256k1BytePoints are dummy points used so the code which generates the
|
||||||
|
// real values can compile.
|
||||||
|
var secp256k1BytePoints = []byte{}
|
||||||
|
|
||||||
|
// getDoublingPoints returns all the possible G^(2^i) for i in
|
||||||
|
// 0..n-1 where n is the curve's bit size (256 in the case of secp256k1)
|
||||||
|
// the coordinates are recorded as Jacobian coordinates.
|
||||||
|
func (curve *KoblitzCurve) getDoublingPoints() [][3]fieldVal {
|
||||||
|
doublingPoints := make([][3]fieldVal, curve.BitSize)
|
||||||
|
|
||||||
|
// initialize px, py, pz to the Jacobian coordinates for the base point
|
||||||
|
px, py := curve.bigAffineToField(curve.Gx, curve.Gy)
|
||||||
|
pz := new(fieldVal).SetInt(1)
|
||||||
|
for i := 0; i < bitSize; i++ {
|
||||||
|
doublingPoints[i] = [3]fieldVal{*px, *py, *pz}
|
||||||
|
// P = 2*P
|
||||||
|
curve.doubleJacobian(px, py, pz, px, py, pz)
|
||||||
|
}
|
||||||
|
return doublingPoints
|
||||||
|
}
|
||||||
|
|
||||||
|
// SerializedBytePoints returns a serialized byte slice which contains all of
|
||||||
|
// the possible points per 8-bit window. This is used to when generating
|
||||||
|
// secp256k1.go.
|
||||||
|
func (curve *KoblitzCurve) SerializedBytePoints() []byte {
|
||||||
|
doublingPoints := curve.getDoublingPoints()
|
||||||
|
|
||||||
|
// Segregate the bits into byte-sized windows
|
||||||
|
serialized := make([]byte, curve.byteSize*256*3*10*4)
|
||||||
|
offset := 0
|
||||||
|
for byteNum := 0; byteNum < curve.byteSize; byteNum++ {
|
||||||
|
// Grab the 8 bits that make up this byte from doublingPoints.
|
||||||
|
startingBit := 8 * (curve.byteSize - byteNum - 1)
|
||||||
|
computingPoints := doublingPoints[startingBit : startingBit+8]
|
||||||
|
|
||||||
|
// Compute all points in this window and serialize them.
|
||||||
|
for i := 0; i < 256; i++ {
|
||||||
|
px, py, pz := new(fieldVal), new(fieldVal), new(fieldVal)
|
||||||
|
for j := 0; j < 8; j++ {
|
||||||
|
if i>>uint(j)&1 == 1 {
|
||||||
|
curve.addJacobian(px, py, pz, &computingPoints[j][0],
|
||||||
|
&computingPoints[j][1], &computingPoints[j][2], px, py, pz)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
binary.LittleEndian.PutUint32(serialized[offset:], px.n[i])
|
||||||
|
offset += 4
|
||||||
|
}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
binary.LittleEndian.PutUint32(serialized[offset:], py.n[i])
|
||||||
|
offset += 4
|
||||||
|
}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
binary.LittleEndian.PutUint32(serialized[offset:], pz.n[i])
|
||||||
|
offset += 4
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return serialized
|
||||||
|
}
|
71
btcec/internal_test.go
Normal file
71
btcec/internal_test.go
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
TstPubkeyUncompressed = pubkeyUncompressed
|
||||||
|
TstPubkeyCompressed = pubkeyCompressed
|
||||||
|
TstPubkeyHybrid = pubkeyHybrid
|
||||||
|
)
|
||||||
|
|
||||||
|
// TstRawInts allows the test package to get the integers from the internal
|
||||||
|
// field representation for ensuring correctness. It is only available during
|
||||||
|
// the tests.
|
||||||
|
func (f *fieldVal) TstRawInts() [10]uint32 {
|
||||||
|
return f.n
|
||||||
|
}
|
||||||
|
|
||||||
|
// TstSetRawInts allows the test package to directly set the integers used by
|
||||||
|
// the internal field representation. It is only available during the tests.
|
||||||
|
func (f *fieldVal) TstSetRawInts(raw [10]uint32) *fieldVal {
|
||||||
|
for i := 0; i < len(raw); i++ {
|
||||||
|
f.n[i] = raw[i]
|
||||||
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
// TstFieldJacobianToBigAffine makes the internal fieldJacobianToBigAffine
|
||||||
|
// function available to the test package.
|
||||||
|
func (curve *KoblitzCurve) TstFieldJacobianToBigAffine(x, y, z *fieldVal) (*big.Int, *big.Int) {
|
||||||
|
return curve.fieldJacobianToBigAffine(x, y, z)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TstIsJacobianOnCurve returns boolean if the point (x,y,z) is on the curve.
|
||||||
|
func (curve *KoblitzCurve) TstIsJacobianOnCurve(x, y, z *fieldVal) bool {
|
||||||
|
// Elliptic curve equation for secp256k1 is: y^2 = x^3 + 7
|
||||||
|
// In Jacobian coordinates, Y = y/z^3 and X = x/z^2
|
||||||
|
// Thus:
|
||||||
|
// (y/z^3)^2 = (x/z^2)^3 + 7
|
||||||
|
// y^2/z^6 = x^3/z^6 + 7
|
||||||
|
// y^2 = x^3 + 7*z^6
|
||||||
|
var y2, z2, x3, result fieldVal
|
||||||
|
y2.SquareVal(y).Normalize()
|
||||||
|
z2.SquareVal(z)
|
||||||
|
x3.SquareVal(x).Mul(x)
|
||||||
|
result.SquareVal(&z2).Mul(&z2).MulInt(7).Add(&x3).Normalize()
|
||||||
|
return y2.Equals(&result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TstAddJacobian makes the internal addJacobian function available to the test
|
||||||
|
// package.
|
||||||
|
func (curve *KoblitzCurve) TstAddJacobian(x1, y1, z1, x2, y2, z2, x3, y3, z3 *fieldVal) {
|
||||||
|
curve.addJacobian(x1, y1, z1, x2, y2, z2, x3, y3, z3)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TstDoubleJacobian makes the internal doubleJacobian function available to the test
|
||||||
|
// package.
|
||||||
|
func (curve *KoblitzCurve) TstDoubleJacobian(x1, y1, z1, x3, y3, z3 *fieldVal) {
|
||||||
|
curve.doubleJacobian(x1, y1, z1, x3, y3, z3)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFieldVal returns a new field value set to 0. This is only available to
|
||||||
|
// the test package.
|
||||||
|
func NewFieldVal() *fieldVal {
|
||||||
|
return new(fieldVal)
|
||||||
|
}
|
71
btcec/precompute.go
Normal file
71
btcec/precompute.go
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
// Copyright 2015 Conformal Systems LLC. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"compress/zlib"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/binary"
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:generate go run -tags gensecp256k1 genprecomps.go
|
||||||
|
|
||||||
|
// loadS256BytePoints decompresses and deserializes the pre-computed byte points
|
||||||
|
// used to accelerate scalar base multiplication for the secp256k1 curve. This
|
||||||
|
// approach is used since it allows the compile to use significantly less ram
|
||||||
|
// and be performed much faster than it is with hard-coding the final in-memory
|
||||||
|
// data structure. At the same time, it is quite fast to generate the in-memory
|
||||||
|
// data structure at init time with this approach versus computing the table.
|
||||||
|
func loadS256BytePoints() error {
|
||||||
|
// There will be no byte points to load when generating them.
|
||||||
|
bp := secp256k1BytePoints
|
||||||
|
if len(secp256k1BytePoints) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decompress the pre-computed table used to accelerate scalar base
|
||||||
|
// multiplication.
|
||||||
|
decoded := make([]byte, base64.StdEncoding.DecodedLen(len(bp)))
|
||||||
|
if _, err := base64.StdEncoding.Decode(decoded, bp); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
r, err := zlib.NewReader(bytes.NewReader(decoded))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
serialized, err := ioutil.ReadAll(r)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deserialize the precomputed byte points and set the curve to them.
|
||||||
|
offset := 0
|
||||||
|
var bytePoints [32][256][3]fieldVal
|
||||||
|
for byteNum := 0; byteNum < 32; byteNum++ {
|
||||||
|
// All points in this window.
|
||||||
|
for i := 0; i < 256; i++ {
|
||||||
|
px, py, pz := new(fieldVal), new(fieldVal), new(fieldVal)
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
px.n[i] = binary.LittleEndian.Uint32(serialized[offset:])
|
||||||
|
offset += 4
|
||||||
|
}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
py.n[i] = binary.LittleEndian.Uint32(serialized[offset:])
|
||||||
|
offset += 4
|
||||||
|
}
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
pz.n[i] = binary.LittleEndian.Uint32(serialized[offset:])
|
||||||
|
offset += 4
|
||||||
|
}
|
||||||
|
bytePoints[byteNum][i][0] = *px
|
||||||
|
bytePoints[byteNum][i][1] = *py
|
||||||
|
bytePoints[byteNum][i][2] = *pz
|
||||||
|
}
|
||||||
|
}
|
||||||
|
secp256k1.bytePoints = &bytePoints
|
||||||
|
return nil
|
||||||
|
}
|
74
btcec/privkey.go
Normal file
74
btcec/privkey.go
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/rand"
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PrivateKey wraps an ecdsa.PrivateKey as a convenience mainly for signing
|
||||||
|
// things with the the private key without having to directly import the ecdsa
|
||||||
|
// package.
|
||||||
|
type PrivateKey ecdsa.PrivateKey
|
||||||
|
|
||||||
|
// PrivKeyFromBytes returns a private and public key for `curve' based on the
|
||||||
|
// private key passed as an argument as a byte slice.
|
||||||
|
func PrivKeyFromBytes(curve *KoblitzCurve, pk []byte) (*PrivateKey,
|
||||||
|
*PublicKey) {
|
||||||
|
x, y := curve.ScalarBaseMult(pk)
|
||||||
|
|
||||||
|
priv := &ecdsa.PrivateKey{
|
||||||
|
PublicKey: ecdsa.PublicKey{
|
||||||
|
Curve: curve,
|
||||||
|
X: x,
|
||||||
|
Y: y,
|
||||||
|
},
|
||||||
|
D: new(big.Int).SetBytes(pk),
|
||||||
|
}
|
||||||
|
|
||||||
|
return (*PrivateKey)(priv), (*PublicKey)(&priv.PublicKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPrivateKey is a wrapper for ecdsa.GenerateKey that returns a PrivateKey
|
||||||
|
// instead of the normal ecdsa.PrivateKey.
|
||||||
|
func NewPrivateKey(curve *KoblitzCurve) (*PrivateKey, error) {
|
||||||
|
key, err := ecdsa.GenerateKey(curve, rand.Reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return (*PrivateKey)(key), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PubKey returns the PublicKey corresponding to this private key.
|
||||||
|
func (p *PrivateKey) PubKey() *PublicKey {
|
||||||
|
return (*PublicKey)(&p.PublicKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToECDSA returns the private key as a *ecdsa.PrivateKey.
|
||||||
|
func (p *PrivateKey) ToECDSA() *ecdsa.PrivateKey {
|
||||||
|
return (*ecdsa.PrivateKey)(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sign wraps ecdsa.Sign to sign the provided hash (which should be the result
|
||||||
|
// of hashing a larger message) using the private key.
|
||||||
|
func (p *PrivateKey) Sign(hash []byte) (*Signature, error) {
|
||||||
|
r, s, err := ecdsa.Sign(rand.Reader, p.ToECDSA(), hash)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Signature{R: r, S: s}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrivKeyBytesLen defines the length in bytes of a serialized private key.
|
||||||
|
const PrivKeyBytesLen = 32
|
||||||
|
|
||||||
|
// Serialize returns the private key number d as a big-endian binary-encoded
|
||||||
|
// number, padded to a length of 32 bytes.
|
||||||
|
func (p *PrivateKey) Serialize() []byte {
|
||||||
|
b := make([]byte, 0, PrivKeyBytesLen)
|
||||||
|
return paddedAppend(PrivKeyBytesLen, b, p.ToECDSA().D.Bytes())
|
||||||
|
}
|
58
btcec/privkey_test.go
Normal file
58
btcec/privkey_test.go
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/btcec"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPrivKeys(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
key []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "check curve",
|
||||||
|
key: []byte{
|
||||||
|
0xea, 0xf0, 0x2c, 0xa3, 0x48, 0xc5, 0x24, 0xe6,
|
||||||
|
0x39, 0x26, 0x55, 0xba, 0x4d, 0x29, 0x60, 0x3c,
|
||||||
|
0xd1, 0xa7, 0x34, 0x7d, 0x9d, 0x65, 0xcf, 0xe9,
|
||||||
|
0x3c, 0xe1, 0xeb, 0xff, 0xdc, 0xa2, 0x26, 0x94,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
priv, pub := btcec.PrivKeyFromBytes(btcec.S256(), test.key)
|
||||||
|
|
||||||
|
_, err := btcec.ParsePubKey(
|
||||||
|
pub.SerializeUncompressed(), btcec.S256())
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s privkey: %v", test.name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
hash := []byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9}
|
||||||
|
sig, err := priv.Sign(hash)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s could not sign: %v", test.name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !sig.Verify(hash, pub) {
|
||||||
|
t.Errorf("%s could not verify: %v", test.name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
serializedKey := priv.Serialize()
|
||||||
|
if !bytes.Equal(serializedKey, test.key) {
|
||||||
|
t.Errorf("%s unexpected serialized bytes - got: %x, "+
|
||||||
|
"want: %x", test.name, serializedKey, test.key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
164
btcec/pubkey.go
Normal file
164
btcec/pubkey.go
Normal file
|
@ -0,0 +1,164 @@
|
||||||
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
// These constants define the lengths of serialized public keys.
|
||||||
|
const (
|
||||||
|
PubKeyBytesLenCompressed = 33
|
||||||
|
PubKeyBytesLenUncompressed = 65
|
||||||
|
PubKeyBytesLenHybrid = 65
|
||||||
|
)
|
||||||
|
|
||||||
|
func isOdd(a *big.Int) bool {
|
||||||
|
return a.Bit(0) == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// decompressPoint decompresses a point on the given curve given the X point and
|
||||||
|
// the solution to use.
|
||||||
|
func decompressPoint(curve *KoblitzCurve, x *big.Int, ybit bool) (*big.Int, error) {
|
||||||
|
// TODO(oga) This will probably only work for secp256k1 due to
|
||||||
|
// optimisations.
|
||||||
|
|
||||||
|
// Y = +-sqrt(x^3 + B)
|
||||||
|
x3 := new(big.Int).Mul(x, x)
|
||||||
|
x3.Mul(x3, x)
|
||||||
|
x3.Add(x3, curve.Params().B)
|
||||||
|
|
||||||
|
// now calculate sqrt mod p of x2 + B
|
||||||
|
// This code used to do a full sqrt based on tonelli/shanks,
|
||||||
|
// but this was replaced by the algorithms referenced in
|
||||||
|
// https://bitcointalk.org/index.php?topic=162805.msg1712294#msg1712294
|
||||||
|
y := new(big.Int).Exp(x3, curve.QPlus1Div4(), curve.Params().P)
|
||||||
|
|
||||||
|
if ybit != isOdd(y) {
|
||||||
|
y.Sub(curve.Params().P, y)
|
||||||
|
}
|
||||||
|
if ybit != isOdd(y) {
|
||||||
|
return nil, fmt.Errorf("ybit doesn't match oddness")
|
||||||
|
}
|
||||||
|
return y, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
pubkeyCompressed byte = 0x2 // y_bit + x coord
|
||||||
|
pubkeyUncompressed byte = 0x4 // x coord + y coord
|
||||||
|
pubkeyHybrid byte = 0x6 // y_bit + x coord + y coord
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
func ParsePubKey(pubKeyStr []byte, curve *KoblitzCurve) (key *PublicKey, err error) {
|
||||||
|
pubkey := PublicKey{}
|
||||||
|
pubkey.Curve = curve
|
||||||
|
|
||||||
|
if len(pubKeyStr) == 0 {
|
||||||
|
return nil, errors.New("pubkey string is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
format := pubKeyStr[0]
|
||||||
|
ybit := (format & 0x1) == 0x1
|
||||||
|
format &= ^byte(0x1)
|
||||||
|
|
||||||
|
switch len(pubKeyStr) {
|
||||||
|
case PubKeyBytesLenUncompressed:
|
||||||
|
if format != pubkeyUncompressed && format != pubkeyHybrid {
|
||||||
|
return nil, fmt.Errorf("invalid magic in pubkey str: "+
|
||||||
|
"%d", pubKeyStr[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
pubkey.X = new(big.Int).SetBytes(pubKeyStr[1:33])
|
||||||
|
pubkey.Y = new(big.Int).SetBytes(pubKeyStr[33:])
|
||||||
|
// hybrid keys have extra information, make use of it.
|
||||||
|
if format == pubkeyHybrid && ybit != isOdd(pubkey.Y) {
|
||||||
|
return nil, fmt.Errorf("ybit doesn't match oddness")
|
||||||
|
}
|
||||||
|
case PubKeyBytesLenCompressed:
|
||||||
|
// format is 0x2 | solution, <X coordinate>
|
||||||
|
// solution determines which solution of the curve we use.
|
||||||
|
/// y^2 = x^3 + Curve.B
|
||||||
|
if format != pubkeyCompressed {
|
||||||
|
return nil, fmt.Errorf("invalid magic in compressed "+
|
||||||
|
"pubkey string: %d", pubKeyStr[0])
|
||||||
|
}
|
||||||
|
pubkey.X = new(big.Int).SetBytes(pubKeyStr[1:33])
|
||||||
|
pubkey.Y, err = decompressPoint(curve, pubkey.X, ybit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
default: // wrong!
|
||||||
|
return nil, fmt.Errorf("invalid pub key length %d",
|
||||||
|
len(pubKeyStr))
|
||||||
|
}
|
||||||
|
|
||||||
|
if pubkey.X.Cmp(pubkey.Curve.Params().P) >= 0 {
|
||||||
|
return nil, fmt.Errorf("pubkey X parameter is >= to P")
|
||||||
|
}
|
||||||
|
if pubkey.Y.Cmp(pubkey.Curve.Params().P) >= 0 {
|
||||||
|
return nil, fmt.Errorf("pubkey Y parameter is >= to P")
|
||||||
|
}
|
||||||
|
if !pubkey.Curve.IsOnCurve(pubkey.X, pubkey.Y) {
|
||||||
|
return nil, fmt.Errorf("pubkey isn't on secp265k1 curve")
|
||||||
|
}
|
||||||
|
return &pubkey, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PublicKey is an ecdsa.PublicKey with additional functions to
|
||||||
|
// serialize in uncompressed, compressed, and hybrid formats.
|
||||||
|
type PublicKey ecdsa.PublicKey
|
||||||
|
|
||||||
|
// ToECDSA returns the public key as a *ecdsa.PublicKey.
|
||||||
|
func (p *PublicKey) ToECDSA() *ecdsa.PublicKey {
|
||||||
|
return (*ecdsa.PublicKey)(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SerializeUncompressed serializes a public key in a 65-byte uncompressed
|
||||||
|
// format.
|
||||||
|
func (p *PublicKey) SerializeUncompressed() []byte {
|
||||||
|
b := make([]byte, 0, PubKeyBytesLenUncompressed)
|
||||||
|
b = append(b, pubkeyUncompressed)
|
||||||
|
b = paddedAppend(32, b, p.X.Bytes())
|
||||||
|
return paddedAppend(32, b, p.Y.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
// SerializeCompressed serializes a public key in a 33-byte compressed format.
|
||||||
|
func (p *PublicKey) SerializeCompressed() []byte {
|
||||||
|
b := make([]byte, 0, PubKeyBytesLenCompressed)
|
||||||
|
format := pubkeyCompressed
|
||||||
|
if isOdd(p.Y) {
|
||||||
|
format |= 0x1
|
||||||
|
}
|
||||||
|
b = append(b, format)
|
||||||
|
return paddedAppend(32, b, p.X.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
// SerializeHybrid serializes a public key in a 65-byte hybrid format.
|
||||||
|
func (p *PublicKey) SerializeHybrid() []byte {
|
||||||
|
b := make([]byte, 0, PubKeyBytesLenHybrid)
|
||||||
|
format := pubkeyHybrid
|
||||||
|
if isOdd(p.Y) {
|
||||||
|
format |= 0x1
|
||||||
|
}
|
||||||
|
b = append(b, format)
|
||||||
|
b = paddedAppend(32, b, p.X.Bytes())
|
||||||
|
return paddedAppend(32, b, p.Y.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
// paddedAppend appends the src byte slice to dst, returning the new slice.
|
||||||
|
// If the length of the source is smaller than the passed size, leading zero
|
||||||
|
// bytes are appended to the dst slice before appending src.
|
||||||
|
func paddedAppend(size uint, dst, src []byte) []byte {
|
||||||
|
for i := 0; i < int(size)-len(src); i++ {
|
||||||
|
dst = append(dst, 0)
|
||||||
|
}
|
||||||
|
return append(dst, src...)
|
||||||
|
}
|
235
btcec/pubkey_test.go
Normal file
235
btcec/pubkey_test.go
Normal file
|
@ -0,0 +1,235 @@
|
||||||
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/btcec"
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
)
|
||||||
|
|
||||||
|
type pubKeyTest struct {
|
||||||
|
name string
|
||||||
|
key []byte
|
||||||
|
format byte
|
||||||
|
isValid bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var pubKeyTests = []pubKeyTest{
|
||||||
|
// pubkey from bitcoin blockchain tx
|
||||||
|
// 0437cd7f8525ceed2324359c2d0ba26006d92d85
|
||||||
|
{
|
||||||
|
name: "uncompressed ok",
|
||||||
|
key: []byte{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
|
||||||
|
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
|
||||||
|
0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
|
||||||
|
0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
|
||||||
|
0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
|
||||||
|
0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
|
||||||
|
0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
|
||||||
|
0xb4, 0x12, 0xa3,
|
||||||
|
},
|
||||||
|
isValid: true,
|
||||||
|
format: btcec.TstPubkeyUncompressed,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "uncompressed x changed",
|
||||||
|
key: []byte{0x04, 0x15, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
|
||||||
|
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
|
||||||
|
0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
|
||||||
|
0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
|
||||||
|
0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
|
||||||
|
0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
|
||||||
|
0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
|
||||||
|
0xb4, 0x12, 0xa3,
|
||||||
|
},
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "uncompressed y changed",
|
||||||
|
key: []byte{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
|
||||||
|
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
|
||||||
|
0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
|
||||||
|
0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
|
||||||
|
0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
|
||||||
|
0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
|
||||||
|
0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
|
||||||
|
0xb4, 0x12, 0xa4,
|
||||||
|
},
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "uncompressed claims compressed",
|
||||||
|
key: []byte{0x03, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
|
||||||
|
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
|
||||||
|
0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
|
||||||
|
0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
|
||||||
|
0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
|
||||||
|
0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
|
||||||
|
0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
|
||||||
|
0xb4, 0x12, 0xa3,
|
||||||
|
},
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "uncompressed as hybrid ok",
|
||||||
|
key: []byte{0x07, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
|
||||||
|
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
|
||||||
|
0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
|
||||||
|
0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
|
||||||
|
0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
|
||||||
|
0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
|
||||||
|
0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
|
||||||
|
0xb4, 0x12, 0xa3,
|
||||||
|
},
|
||||||
|
isValid: true,
|
||||||
|
format: btcec.TstPubkeyHybrid,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "uncompressed as hybrid wrong",
|
||||||
|
key: []byte{0x06, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
|
||||||
|
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
|
||||||
|
0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
|
||||||
|
0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xb2, 0xe0,
|
||||||
|
0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
|
||||||
|
0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
|
||||||
|
0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
|
||||||
|
0xb4, 0x12, 0xa3,
|
||||||
|
},
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
// from tx 0b09c51c51ff762f00fb26217269d2a18e77a4fa87d69b3c363ab4df16543f20
|
||||||
|
{
|
||||||
|
name: "compressed ok (ybit = 0)",
|
||||||
|
key: []byte{0x02, 0xce, 0x0b, 0x14, 0xfb, 0x84, 0x2b, 0x1b,
|
||||||
|
0xa5, 0x49, 0xfd, 0xd6, 0x75, 0xc9, 0x80, 0x75, 0xf1,
|
||||||
|
0x2e, 0x9c, 0x51, 0x0f, 0x8e, 0xf5, 0x2b, 0xd0, 0x21,
|
||||||
|
0xa9, 0xa1, 0xf4, 0x80, 0x9d, 0x3b, 0x4d,
|
||||||
|
},
|
||||||
|
isValid: true,
|
||||||
|
format: btcec.TstPubkeyCompressed,
|
||||||
|
},
|
||||||
|
// from tx fdeb8e72524e8dab0da507ddbaf5f88fe4a933eb10a66bc4745bb0aa11ea393c
|
||||||
|
{
|
||||||
|
name: "compressed ok (ybit = 1)",
|
||||||
|
key: []byte{0x03, 0x26, 0x89, 0xc7, 0xc2, 0xda, 0xb1, 0x33,
|
||||||
|
0x09, 0xfb, 0x14, 0x3e, 0x0e, 0x8f, 0xe3, 0x96, 0x34,
|
||||||
|
0x25, 0x21, 0x88, 0x7e, 0x97, 0x66, 0x90, 0xb6, 0xb4,
|
||||||
|
0x7f, 0x5b, 0x2a, 0x4b, 0x7d, 0x44, 0x8e,
|
||||||
|
},
|
||||||
|
isValid: true,
|
||||||
|
format: btcec.TstPubkeyCompressed,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "compressed claims uncompressed (ybit = 0)",
|
||||||
|
key: []byte{0x04, 0xce, 0x0b, 0x14, 0xfb, 0x84, 0x2b, 0x1b,
|
||||||
|
0xa5, 0x49, 0xfd, 0xd6, 0x75, 0xc9, 0x80, 0x75, 0xf1,
|
||||||
|
0x2e, 0x9c, 0x51, 0x0f, 0x8e, 0xf5, 0x2b, 0xd0, 0x21,
|
||||||
|
0xa9, 0xa1, 0xf4, 0x80, 0x9d, 0x3b, 0x4d,
|
||||||
|
},
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "compressed claims uncompressed (ybit = 1)",
|
||||||
|
key: []byte{0x05, 0x26, 0x89, 0xc7, 0xc2, 0xda, 0xb1, 0x33,
|
||||||
|
0x09, 0xfb, 0x14, 0x3e, 0x0e, 0x8f, 0xe3, 0x96, 0x34,
|
||||||
|
0x25, 0x21, 0x88, 0x7e, 0x97, 0x66, 0x90, 0xb6, 0xb4,
|
||||||
|
0x7f, 0x5b, 0x2a, 0x4b, 0x7d, 0x44, 0x8e,
|
||||||
|
},
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "wrong length)",
|
||||||
|
key: []byte{0x05},
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "X == P",
|
||||||
|
key: []byte{0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFC, 0x2F, 0xb2, 0xe0,
|
||||||
|
0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
|
||||||
|
0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
|
||||||
|
0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
|
||||||
|
0xb4, 0x12, 0xa3,
|
||||||
|
},
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "X > P",
|
||||||
|
key: []byte{0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFD, 0x2F, 0xb2, 0xe0,
|
||||||
|
0xea, 0xdd, 0xfb, 0x84, 0xcc, 0xf9, 0x74, 0x44, 0x64,
|
||||||
|
0xf8, 0x2e, 0x16, 0x0b, 0xfa, 0x9b, 0x8b, 0x64, 0xf9,
|
||||||
|
0xd4, 0xc0, 0x3f, 0x99, 0x9b, 0x86, 0x43, 0xf6, 0x56,
|
||||||
|
0xb4, 0x12, 0xa3,
|
||||||
|
},
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Y == P",
|
||||||
|
key: []byte{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
|
||||||
|
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
|
||||||
|
0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
|
||||||
|
0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF,
|
||||||
|
0xFF, 0xFC, 0x2F,
|
||||||
|
},
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Y > P",
|
||||||
|
key: []byte{0x04, 0x11, 0xdb, 0x93, 0xe1, 0xdc, 0xdb, 0x8a,
|
||||||
|
0x01, 0x6b, 0x49, 0x84, 0x0f, 0x8c, 0x53, 0xbc, 0x1e,
|
||||||
|
0xb6, 0x8a, 0x38, 0x2e, 0x97, 0xb1, 0x48, 0x2e, 0xca,
|
||||||
|
0xd7, 0xb1, 0x48, 0xa6, 0x90, 0x9a, 0x5c, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF,
|
||||||
|
0xFF, 0xFD, 0x2F,
|
||||||
|
},
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPubKeys(t *testing.T) {
|
||||||
|
for _, test := range pubKeyTests {
|
||||||
|
pk, err := btcec.ParsePubKey(test.key, btcec.S256())
|
||||||
|
if err != nil {
|
||||||
|
if test.isValid {
|
||||||
|
t.Errorf("%s pubkey failed when shouldn't %v",
|
||||||
|
test.name, err)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !test.isValid {
|
||||||
|
t.Errorf("%s counted as valid when it should fail",
|
||||||
|
test.name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var pkStr []byte
|
||||||
|
switch test.format {
|
||||||
|
case btcec.TstPubkeyUncompressed:
|
||||||
|
pkStr = (*btcec.PublicKey)(pk).SerializeUncompressed()
|
||||||
|
case btcec.TstPubkeyCompressed:
|
||||||
|
pkStr = (*btcec.PublicKey)(pk).SerializeCompressed()
|
||||||
|
case btcec.TstPubkeyHybrid:
|
||||||
|
pkStr = (*btcec.PublicKey)(pk).SerializeHybrid()
|
||||||
|
}
|
||||||
|
if !bytes.Equal(test.key, pkStr) {
|
||||||
|
t.Errorf("%s pubkey: serialized keys do not match.",
|
||||||
|
test.name)
|
||||||
|
spew.Dump(test.key)
|
||||||
|
spew.Dump(pkStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
btcec/secp256k1.go
Normal file
10
btcec/secp256k1.go
Normal file
File diff suppressed because one or more lines are too long
398
btcec/signature.go
Normal file
398
btcec/signature.go
Normal file
|
@ -0,0 +1,398 @@
|
||||||
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/elliptic"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Errors returned by canonicalPadding.
|
||||||
|
var (
|
||||||
|
errNegativeValue = errors.New("value may be interpreted as negative")
|
||||||
|
errExcessivelyPaddedValue = errors.New("value is excessively padded")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Signature is a type representing an ecdsa signature.
|
||||||
|
type Signature struct {
|
||||||
|
R *big.Int
|
||||||
|
S *big.Int
|
||||||
|
}
|
||||||
|
|
||||||
|
// curve order and halforder, used to tame ECDSA malleability (see BIP-0062)
|
||||||
|
var (
|
||||||
|
order = new(big.Int).Set(S256().N)
|
||||||
|
halforder = new(big.Int).Rsh(order, 1)
|
||||||
|
)
|
||||||
|
|
||||||
|
// Serialize returns the ECDSA signature in the more strict DER format. Note
|
||||||
|
// that the serialized bytes returned do not include the appended hash type
|
||||||
|
// used in Bitcoin signature scripts.
|
||||||
|
//
|
||||||
|
// encoding/asn1 is broken so we hand roll this output:
|
||||||
|
//
|
||||||
|
// 0x30 <length> 0x02 <length r> r 0x02 <length s> s
|
||||||
|
func (sig *Signature) Serialize() []byte {
|
||||||
|
// low 'S' malleability breaker
|
||||||
|
sigS := sig.S
|
||||||
|
if sigS.Cmp(halforder) == 1 {
|
||||||
|
sigS = new(big.Int).Sub(order, sigS)
|
||||||
|
}
|
||||||
|
// Ensure the encoded bytes for the r and s values are canonical and
|
||||||
|
// thus suitable for DER encoding.
|
||||||
|
rb := canonicalizeInt(sig.R)
|
||||||
|
sb := canonicalizeInt(sigS)
|
||||||
|
|
||||||
|
// 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[0] = 0x30
|
||||||
|
b[1] = byte(length - 2)
|
||||||
|
b[2] = 0x02
|
||||||
|
b[3] = byte(len(rb))
|
||||||
|
offset := copy(b[4:], rb) + 4
|
||||||
|
b[offset] = 0x02
|
||||||
|
b[offset+1] = byte(len(sb))
|
||||||
|
copy(b[offset+2:], sb)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify calls ecdsa.Verify to verify the signature of hash using the public
|
||||||
|
// key. It returns true if the signature is valid, false otherwise.
|
||||||
|
func (sig *Signature) Verify(hash []byte, pubKey *PublicKey) bool {
|
||||||
|
return ecdsa.Verify(pubKey.ToECDSA(), hash, sig.R, sig.S)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseSig(sigStr []byte, curve elliptic.Curve, der bool) (*Signature, error) {
|
||||||
|
// Originally this code used encoding/asn1 in order to parse the
|
||||||
|
// signature, but a number of problems were found with this approach.
|
||||||
|
// Despite the fact that signatures are stored as DER, the difference
|
||||||
|
// between go's idea of a bignum (and that they have sign) doesn't agree
|
||||||
|
// with the openssl one (where they do not). The above is true as of
|
||||||
|
// Go 1.1. In the end it was simpler to rewrite the code to explicitly
|
||||||
|
// understand the format which is this:
|
||||||
|
// 0x30 <length of whole message> <0x02> <length of R> <R> 0x2
|
||||||
|
// <length of S> <S>.
|
||||||
|
|
||||||
|
signature := &Signature{}
|
||||||
|
|
||||||
|
// minimal message is when both numbers are 1 bytes. adding up to:
|
||||||
|
// 0x30 + len + 0x02 + 0x01 + <byte> + 0x2 + 0x01 + <byte>
|
||||||
|
if len(sigStr) < 8 {
|
||||||
|
return nil, errors.New("malformed signature: too short")
|
||||||
|
}
|
||||||
|
// 0x30
|
||||||
|
index := 0
|
||||||
|
if sigStr[index] != 0x30 {
|
||||||
|
return nil, errors.New("malformed signature: no header magic")
|
||||||
|
}
|
||||||
|
index++
|
||||||
|
// length of remaining message
|
||||||
|
siglen := sigStr[index]
|
||||||
|
index++
|
||||||
|
if int(siglen+2) > len(sigStr) {
|
||||||
|
return nil, errors.New("malformed signature: bad length")
|
||||||
|
}
|
||||||
|
// trim the slice we're working on so we only look at what matters.
|
||||||
|
sigStr = sigStr[:siglen+2]
|
||||||
|
|
||||||
|
// 0x02
|
||||||
|
if sigStr[index] != 0x02 {
|
||||||
|
return nil,
|
||||||
|
errors.New("malformed signature: no 1st int marker")
|
||||||
|
}
|
||||||
|
index++
|
||||||
|
|
||||||
|
// Length of signature R.
|
||||||
|
rLen := int(sigStr[index])
|
||||||
|
// must be positive, must be able to fit in another 0x2, <len> <s>
|
||||||
|
// hence the -3. We assume that the length must be at least one byte.
|
||||||
|
index++
|
||||||
|
if rLen <= 0 || rLen > len(sigStr)-index-3 {
|
||||||
|
return nil, errors.New("malformed signature: bogus R length")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then R itself.
|
||||||
|
rBytes := sigStr[index : index+rLen]
|
||||||
|
if der {
|
||||||
|
switch err := canonicalPadding(rBytes); err {
|
||||||
|
case errNegativeValue:
|
||||||
|
return nil, errors.New("signature R is negative")
|
||||||
|
case errExcessivelyPaddedValue:
|
||||||
|
return nil, errors.New("signature R is excessively padded")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
signature.R = new(big.Int).SetBytes(rBytes)
|
||||||
|
index += rLen
|
||||||
|
// 0x02. length already checked in previous if.
|
||||||
|
if sigStr[index] != 0x02 {
|
||||||
|
return nil, errors.New("malformed signature: no 2nd int marker")
|
||||||
|
}
|
||||||
|
index++
|
||||||
|
|
||||||
|
// Length of signature S.
|
||||||
|
sLen := int(sigStr[index])
|
||||||
|
index++
|
||||||
|
// S should be the rest of the string.
|
||||||
|
if sLen <= 0 || sLen > len(sigStr)-index {
|
||||||
|
return nil, errors.New("malformed signature: bogus S length")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then S itself.
|
||||||
|
sBytes := sigStr[index : index+sLen]
|
||||||
|
if der {
|
||||||
|
switch err := canonicalPadding(sBytes); err {
|
||||||
|
case errNegativeValue:
|
||||||
|
return nil, errors.New("signature S is negative")
|
||||||
|
case errExcessivelyPaddedValue:
|
||||||
|
return nil, errors.New("signature S is excessively padded")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
signature.S = new(big.Int).SetBytes(sBytes)
|
||||||
|
index += sLen
|
||||||
|
|
||||||
|
// sanity check length parsing
|
||||||
|
if index != len(sigStr) {
|
||||||
|
return nil, fmt.Errorf("malformed signature: bad final length %v != %v",
|
||||||
|
index, len(sigStr))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify also checks this, but we can be more sure that we parsed
|
||||||
|
// correctly if we verify here too.
|
||||||
|
// FWIW the ecdsa spec states that R and S must be | 1, N - 1 |
|
||||||
|
// but crypto/ecdsa only checks for Sign != 0. Mirror that.
|
||||||
|
if signature.R.Sign() != 1 {
|
||||||
|
return nil, errors.New("signature R isn't 1 or more")
|
||||||
|
}
|
||||||
|
if signature.S.Sign() != 1 {
|
||||||
|
return nil, errors.New("signature S isn't 1 or more")
|
||||||
|
}
|
||||||
|
if signature.R.Cmp(curve.Params().N) >= 0 {
|
||||||
|
return nil, errors.New("signature R is >= curve.N")
|
||||||
|
}
|
||||||
|
if signature.S.Cmp(curve.Params().N) >= 0 {
|
||||||
|
return nil, errors.New("signature S is >= curve.N")
|
||||||
|
}
|
||||||
|
|
||||||
|
return signature, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseSignature parses a signature in BER format for the curve type `curve'
|
||||||
|
// into a Signature type, perfoming some basic sanity checks. If parsing
|
||||||
|
// according to the more strict DER format is needed, use ParseDERSignature.
|
||||||
|
func ParseSignature(sigStr []byte, curve elliptic.Curve) (*Signature, error) {
|
||||||
|
return parseSig(sigStr, curve, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseDERSignature parses a signature in DER format for the curve type
|
||||||
|
// `curve` into a Signature type. If parsing according to the less strict
|
||||||
|
// BER format is needed, use ParseSignature.
|
||||||
|
func ParseDERSignature(sigStr []byte, curve elliptic.Curve) (*Signature, error) {
|
||||||
|
return parseSig(sigStr, curve, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// canonicalizeInt returns the bytes for the passed big integer adjusted as
|
||||||
|
// necessary to ensure that a big-endian encoded integer can't possibly be
|
||||||
|
// misinterpreted as a negative number. This can happen when the most
|
||||||
|
// significant bit is set, so it is padded by a leading zero byte in this case.
|
||||||
|
// Also, the returned bytes will have at least a single byte when the passed
|
||||||
|
// value is 0. This is required for DER encoding.
|
||||||
|
func canonicalizeInt(val *big.Int) []byte {
|
||||||
|
b := val.Bytes()
|
||||||
|
if len(b) == 0 {
|
||||||
|
b = []byte{0x00}
|
||||||
|
}
|
||||||
|
if b[0]&0x80 != 0 {
|
||||||
|
paddedBytes := make([]byte, len(b)+1)
|
||||||
|
copy(paddedBytes[1:], b)
|
||||||
|
b = paddedBytes
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// canonicalPadding checks whether a big-endian encoded integer could
|
||||||
|
// possibly be misinterpreted as a negative number (even though OpenSSL
|
||||||
|
// treats all numbers as unsigned), or if there is any unnecessary
|
||||||
|
// leading zero padding.
|
||||||
|
func canonicalPadding(b []byte) error {
|
||||||
|
switch {
|
||||||
|
case b[0]&0x80 == 0x80:
|
||||||
|
return errNegativeValue
|
||||||
|
case len(b) > 1 && b[0] == 0x00 && b[1]&0x80 != 0x80:
|
||||||
|
return errExcessivelyPaddedValue
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// hashToInt converts a hash value to an integer. There is some disagreement
|
||||||
|
// about how this is done. [NSA] suggests that this is done in the obvious
|
||||||
|
// manner, but [SECG] truncates the hash to the bit-length of the curve order
|
||||||
|
// first. We follow [SECG] because that's what OpenSSL does. Additionally,
|
||||||
|
// OpenSSL right shifts excess bits from the number if the hash is too large
|
||||||
|
// and we mirror that too.
|
||||||
|
// This is borrowed from crypto/ecdsa.
|
||||||
|
func hashToInt(hash []byte, c elliptic.Curve) *big.Int {
|
||||||
|
orderBits := c.Params().N.BitLen()
|
||||||
|
orderBytes := (orderBits + 7) / 8
|
||||||
|
if len(hash) > orderBytes {
|
||||||
|
hash = hash[:orderBytes]
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := new(big.Int).SetBytes(hash)
|
||||||
|
excess := len(hash)*8 - orderBits
|
||||||
|
if excess > 0 {
|
||||||
|
ret.Rsh(ret, uint(excess))
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// recoverKeyFromSignature recoves a public key from the signature "sig" on the
|
||||||
|
// given message hash "msg". Based on the algorithm found in section 5.1.5 of
|
||||||
|
// SEC 1 Ver 2.0, page 47-48 (53 and 54 in the pdf). This performs the details
|
||||||
|
// in the inner loop in Step 1. The counter provided is actually the j parameter
|
||||||
|
// of the loop * 2 - on the first iteration of j we do the R case, else the -R
|
||||||
|
// case in step 1.6. This counter is used in the bitcoin compressed signature
|
||||||
|
// format and thus we match bitcoind's behaviour here.
|
||||||
|
func recoverKeyFromSignature(curve *KoblitzCurve, sig *Signature, msg []byte,
|
||||||
|
iter int, doChecks bool) (*PublicKey, error) {
|
||||||
|
// 1.1 x = (n * i) + r
|
||||||
|
Rx := new(big.Int).Mul(curve.Params().N,
|
||||||
|
new(big.Int).SetInt64(int64(iter/2)))
|
||||||
|
Rx.Add(Rx, sig.R)
|
||||||
|
if Rx.Cmp(curve.Params().P) != -1 {
|
||||||
|
return nil, errors.New("calculated Rx is larger than curve P")
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert 02<Rx> to point R. (step 1.2 and 1.3). If we are on an odd
|
||||||
|
// iteration then 1.6 will be done with -R, so we calculate the other
|
||||||
|
// term when uncompressing the point.
|
||||||
|
Ry, err := decompressPoint(curve, Rx, iter%2 == 1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1.4 Check n*R is point at infinity
|
||||||
|
if doChecks {
|
||||||
|
nRx, nRy := curve.ScalarMult(Rx, Ry, curve.Params().N.Bytes())
|
||||||
|
if nRx.Sign() != 0 || nRy.Sign() != 0 {
|
||||||
|
return nil, errors.New("n*R does not equal the point at infinity")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1.5 calculate e from message using the same algorithm as ecdsa
|
||||||
|
// signature calculation.
|
||||||
|
e := hashToInt(msg, curve)
|
||||||
|
|
||||||
|
// Step 1.6.1:
|
||||||
|
// We calculate the two terms sR and eG separately multiplied by the
|
||||||
|
// inverse of r (from the signature). We then add them to calculate
|
||||||
|
// Q = r^-1(sR-eG)
|
||||||
|
invr := new(big.Int).ModInverse(sig.R, curve.Params().N)
|
||||||
|
|
||||||
|
// first term.
|
||||||
|
invrS := new(big.Int).Mul(invr, sig.S)
|
||||||
|
invrS.Mod(invrS, curve.Params().N)
|
||||||
|
sRx, sRy := curve.ScalarMult(Rx, Ry, invrS.Bytes())
|
||||||
|
|
||||||
|
// second term.
|
||||||
|
e.Neg(e)
|
||||||
|
e.Mod(e, curve.Params().N)
|
||||||
|
e.Mul(e, invr)
|
||||||
|
e.Mod(e, curve.Params().N)
|
||||||
|
minuseGx, minuseGy := curve.ScalarBaseMult(e.Bytes())
|
||||||
|
|
||||||
|
// TODO(oga) this would be faster if we did a mult and add in one
|
||||||
|
// step to prevent the jacobian conversion back and forth.
|
||||||
|
Qx, Qy := curve.Add(sRx, sRy, minuseGx, minuseGy)
|
||||||
|
|
||||||
|
return &PublicKey{
|
||||||
|
Curve: curve,
|
||||||
|
X: Qx,
|
||||||
|
Y: Qy,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SignCompact produces a compact signature of the data in hash with the given
|
||||||
|
// private key on the given koblitz curve. The isCompressed parameter should
|
||||||
|
// be used to detail if the given signature should reference a compressed
|
||||||
|
// public key or not. If successful the bytes of the compact signature will be
|
||||||
|
// returned in the format:
|
||||||
|
// <(byte of 27+public key solution)+4 if compressed >< padded bytes for signature R><padded bytes for signature S>
|
||||||
|
// where the R and S parameters are padde up to the bitlengh of the curve.
|
||||||
|
func SignCompact(curve *KoblitzCurve, key *PrivateKey,
|
||||||
|
hash []byte, isCompressedKey bool) ([]byte, error) {
|
||||||
|
sig, err := key.Sign(hash)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// bitcoind checks the bit length of R and S here. The ecdsa signature
|
||||||
|
// algorithm returns R and S mod N therefore they will be the bitsize of
|
||||||
|
// the curve, and thus correctly sized.
|
||||||
|
for i := 0; i < (curve.H+1)*2; i++ {
|
||||||
|
pk, err := recoverKeyFromSignature(curve, sig, hash, i, true)
|
||||||
|
if err == nil && pk.X.Cmp(key.X) == 0 && pk.Y.Cmp(key.Y) == 0 {
|
||||||
|
result := make([]byte, 1, 2*curve.byteSize+1)
|
||||||
|
result[0] = 27 + byte(i)
|
||||||
|
if isCompressedKey {
|
||||||
|
result[0] += 4
|
||||||
|
}
|
||||||
|
// Not sure this needs rounding but safer to do so.
|
||||||
|
curvelen := (curve.BitSize + 7) / 8
|
||||||
|
|
||||||
|
// Pad R and S to curvelen if needed.
|
||||||
|
bytelen := (sig.R.BitLen() + 7) / 8
|
||||||
|
if bytelen < curvelen {
|
||||||
|
result = append(result,
|
||||||
|
make([]byte, curvelen-bytelen)...)
|
||||||
|
}
|
||||||
|
result = append(result, sig.R.Bytes()...)
|
||||||
|
|
||||||
|
bytelen = (sig.S.BitLen() + 7) / 8
|
||||||
|
if bytelen < curvelen {
|
||||||
|
result = append(result,
|
||||||
|
make([]byte, curvelen-bytelen)...)
|
||||||
|
}
|
||||||
|
result = append(result, sig.S.Bytes()...)
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("no valid solution for pubkey found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// RecoverCompact verifies the compact signature "signature" of "hash" for the
|
||||||
|
// Koblitz curve in "curve". If the signature matches then the recovered public
|
||||||
|
// key will be returned as well as a boolen if the original key was compressed
|
||||||
|
// or not, else an error will be returned.
|
||||||
|
func RecoverCompact(curve *KoblitzCurve, signature,
|
||||||
|
hash []byte) (*PublicKey, bool, error) {
|
||||||
|
bitlen := (curve.BitSize + 7) / 8
|
||||||
|
if len(signature) != 1+bitlen*2 {
|
||||||
|
return nil, false, errors.New("invalid compact signature size")
|
||||||
|
}
|
||||||
|
|
||||||
|
iteration := int((signature[0] - 27) & ^byte(4))
|
||||||
|
|
||||||
|
// format is <header byte><bitlen R><bitlen S>
|
||||||
|
sig := &Signature{
|
||||||
|
R: new(big.Int).SetBytes(signature[1 : bitlen+1]),
|
||||||
|
S: new(big.Int).SetBytes(signature[bitlen+1:]),
|
||||||
|
}
|
||||||
|
// The iteration used here was encoded
|
||||||
|
key, err := recoverKeyFromSignature(curve, sig, hash, iteration, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return key, ((signature[0] - 27) & 4) == 4, nil
|
||||||
|
}
|
493
btcec/signature_test.go
Normal file
493
btcec/signature_test.go
Normal file
|
@ -0,0 +1,493 @@
|
||||||
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package btcec_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/rand"
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/btcec"
|
||||||
|
)
|
||||||
|
|
||||||
|
type signatureTest struct {
|
||||||
|
name string
|
||||||
|
sig []byte
|
||||||
|
der bool
|
||||||
|
isValid bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var signatureTests = []signatureTest{
|
||||||
|
// signatures from bitcoin blockchain tx
|
||||||
|
// 0437cd7f8525ceed2324359c2d0ba26006d92d85
|
||||||
|
{
|
||||||
|
name: "valid signature.",
|
||||||
|
sig: []byte{0x30, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x20, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty.",
|
||||||
|
sig: []byte{},
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bad magic.",
|
||||||
|
sig: []byte{0x31, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x20, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bad 1st int marker magic.",
|
||||||
|
sig: []byte{0x30, 0x44, 0x03, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x20, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bad 2nd int marker.",
|
||||||
|
sig: []byte{0x30, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x03, 0x20, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "short len",
|
||||||
|
sig: []byte{0x30, 0x43, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x20, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "long len",
|
||||||
|
sig: []byte{0x30, 0x45, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x20, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "long X",
|
||||||
|
sig: []byte{0x30, 0x44, 0x02, 0x42, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x20, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "long Y",
|
||||||
|
sig: []byte{0x30, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x21, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "short Y",
|
||||||
|
sig: []byte{0x30, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x19, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "trailing crap.",
|
||||||
|
sig: []byte{0x30, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x20, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09, 0x01,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
|
||||||
|
// This test is now passing (used to be failing) because there
|
||||||
|
// are signatures in the blockchain that have trailing zero
|
||||||
|
// bytes before the hashtype. So ParseSignature was fixed to
|
||||||
|
// permit buffers with trailing nonsense after the actual
|
||||||
|
// signature.
|
||||||
|
isValid: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "X == N ",
|
||||||
|
sig: []byte{0x30, 0x44, 0x02, 0x20, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFE, 0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48,
|
||||||
|
0xA0, 0x3B, 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41,
|
||||||
|
0x41, 0x02, 0x20, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "X == N ",
|
||||||
|
sig: []byte{0x30, 0x44, 0x02, 0x20, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFE, 0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48,
|
||||||
|
0xA0, 0x3B, 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41,
|
||||||
|
0x42, 0x02, 0x20, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: false,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Y == N",
|
||||||
|
sig: []byte{0x30, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x20, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFE, 0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B,
|
||||||
|
0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x41,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Y > N",
|
||||||
|
sig: []byte{0x30, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x20, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||||
|
0xFE, 0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B,
|
||||||
|
0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x42,
|
||||||
|
},
|
||||||
|
der: false,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "0 len X.",
|
||||||
|
sig: []byte{0x30, 0x24, 0x02, 0x00, 0x02, 0x20, 0x18, 0x15,
|
||||||
|
0x22, 0xec, 0x8e, 0xca, 0x07, 0xde, 0x48, 0x60, 0xa4,
|
||||||
|
0xac, 0xdd, 0x12, 0x90, 0x9d, 0x83, 0x1c, 0xc5, 0x6c,
|
||||||
|
0xbb, 0xac, 0x46, 0x22, 0x08, 0x22, 0x21, 0xa8, 0x76,
|
||||||
|
0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "0 len Y.",
|
||||||
|
sig: []byte{0x30, 0x24, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x00,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "extra R padding.",
|
||||||
|
sig: []byte{0x30, 0x45, 0x02, 0x21, 0x00, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x20, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "extra S padding.",
|
||||||
|
sig: []byte{0x30, 0x45, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x21, 0x00, 0x18, 0x15, 0x22, 0xec, 0x8e, 0xca,
|
||||||
|
0x07, 0xde, 0x48, 0x60, 0xa4, 0xac, 0xdd, 0x12, 0x90,
|
||||||
|
0x9d, 0x83, 0x1c, 0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22,
|
||||||
|
0x08, 0x22, 0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: true,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
// Standard checks (in BER format, without checking for 'canonical' DER
|
||||||
|
// signatures) don't test for negative numbers here because there isn't
|
||||||
|
// a way that is the same between openssl and go that will mark a number
|
||||||
|
// as negative. The Go ASN.1 parser marks numbers as negative when
|
||||||
|
// openssl does not (it doesn't handle negative numbers that I can tell
|
||||||
|
// at all. When not parsing DER signatures, which is done by by bitcoind
|
||||||
|
// when accepting transactions into its mempool, we otherwise only check
|
||||||
|
// for the coordinates being zero.
|
||||||
|
{
|
||||||
|
name: "X == 0",
|
||||||
|
sig: []byte{0x30, 0x25, 0x02, 0x01, 0x00, 0x02, 0x20, 0x18,
|
||||||
|
0x15, 0x22, 0xec, 0x8e, 0xca, 0x07, 0xde, 0x48, 0x60,
|
||||||
|
0xa4, 0xac, 0xdd, 0x12, 0x90, 0x9d, 0x83, 0x1c, 0xc5,
|
||||||
|
0x6c, 0xbb, 0xac, 0x46, 0x22, 0x08, 0x22, 0x21, 0xa8,
|
||||||
|
0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
der: false,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Y == 0.",
|
||||||
|
sig: []byte{0x30, 0x25, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3, 0xa1,
|
||||||
|
0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32, 0xe9, 0xd6,
|
||||||
|
0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab, 0x5f, 0xb8, 0xcd,
|
||||||
|
0x41, 0x02, 0x01, 0x00,
|
||||||
|
},
|
||||||
|
der: false,
|
||||||
|
isValid: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSignatures(t *testing.T) {
|
||||||
|
for _, test := range signatureTests {
|
||||||
|
var err error
|
||||||
|
if test.der {
|
||||||
|
_, err = btcec.ParseDERSignature(test.sig, btcec.S256())
|
||||||
|
} else {
|
||||||
|
_, err = btcec.ParseSignature(test.sig, btcec.S256())
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
if test.isValid {
|
||||||
|
t.Errorf("%s signature failed when shouldn't %v",
|
||||||
|
test.name, err)
|
||||||
|
} /* else {
|
||||||
|
t.Errorf("%s got error %v", test.name, err)
|
||||||
|
} */
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !test.isValid {
|
||||||
|
t.Errorf("%s counted as valid when it should fail",
|
||||||
|
test.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSignatureSerialize ensures that serializing signatures works as expected.
|
||||||
|
func TestSignatureSerialize(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
ecsig *btcec.Signature
|
||||||
|
expected []byte
|
||||||
|
}{
|
||||||
|
// signature from bitcoin blockchain tx
|
||||||
|
// 0437cd7f8525ceed2324359c2d0ba26006d92d85
|
||||||
|
{
|
||||||
|
"valid 1 - r and s most significant bits are zero",
|
||||||
|
&btcec.Signature{
|
||||||
|
R: fromHex("4e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd41"),
|
||||||
|
S: fromHex("181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d09"),
|
||||||
|
},
|
||||||
|
[]byte{
|
||||||
|
0x30, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
|
||||||
|
0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3,
|
||||||
|
0xa1, 0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32,
|
||||||
|
0xe9, 0xd6, 0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab,
|
||||||
|
0x5f, 0xb8, 0xcd, 0x41, 0x02, 0x20, 0x18, 0x15,
|
||||||
|
0x22, 0xec, 0x8e, 0xca, 0x07, 0xde, 0x48, 0x60,
|
||||||
|
0xa4, 0xac, 0xdd, 0x12, 0x90, 0x9d, 0x83, 0x1c,
|
||||||
|
0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22, 0x08, 0x22,
|
||||||
|
0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// signature from bitcoin blockchain tx
|
||||||
|
// cb00f8a0573b18faa8c4f467b049f5d202bf1101d9ef2633bc611be70376a4b4
|
||||||
|
{
|
||||||
|
"valid 2 - r most significant bit is one",
|
||||||
|
&btcec.Signature{
|
||||||
|
R: fromHex("0082235e21a2300022738dabb8e1bbd9d19cfb1e7ab8c30a23b0afbb8d178abcf3"),
|
||||||
|
S: fromHex("24bf68e256c534ddfaf966bf908deb944305596f7bdcc38d69acad7f9c868724"),
|
||||||
|
},
|
||||||
|
[]byte{
|
||||||
|
0x30, 0x45, 0x02, 0x21, 0x00, 0x82, 0x23, 0x5e,
|
||||||
|
0x21, 0xa2, 0x30, 0x00, 0x22, 0x73, 0x8d, 0xab,
|
||||||
|
0xb8, 0xe1, 0xbb, 0xd9, 0xd1, 0x9c, 0xfb, 0x1e,
|
||||||
|
0x7a, 0xb8, 0xc3, 0x0a, 0x23, 0xb0, 0xaf, 0xbb,
|
||||||
|
0x8d, 0x17, 0x8a, 0xbc, 0xf3, 0x02, 0x20, 0x24,
|
||||||
|
0xbf, 0x68, 0xe2, 0x56, 0xc5, 0x34, 0xdd, 0xfa,
|
||||||
|
0xf9, 0x66, 0xbf, 0x90, 0x8d, 0xeb, 0x94, 0x43,
|
||||||
|
0x05, 0x59, 0x6f, 0x7b, 0xdc, 0xc3, 0x8d, 0x69,
|
||||||
|
0xac, 0xad, 0x7f, 0x9c, 0x86, 0x87, 0x24,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// signature from bitcoin blockchain tx
|
||||||
|
// fda204502a3345e08afd6af27377c052e77f1fefeaeb31bdd45f1e1237ca5470
|
||||||
|
{
|
||||||
|
"valid 3 - s most significant bit is one",
|
||||||
|
&btcec.Signature{
|
||||||
|
R: fromHex("1cadddc2838598fee7dc35a12b340c6bde8b389f7bfd19a1252a17c4b5ed2d71"),
|
||||||
|
S: new(big.Int).Add(fromHex("00c1a251bbecb14b058a8bd77f65de87e51c47e95904f4c0e9d52eddc21c1415ac"), btcec.S256().N),
|
||||||
|
},
|
||||||
|
[]byte{
|
||||||
|
0x30, 0x45, 0x02, 0x20, 0x1c, 0xad, 0xdd, 0xc2,
|
||||||
|
0x83, 0x85, 0x98, 0xfe, 0xe7, 0xdc, 0x35, 0xa1,
|
||||||
|
0x2b, 0x34, 0x0c, 0x6b, 0xde, 0x8b, 0x38, 0x9f,
|
||||||
|
0x7b, 0xfd, 0x19, 0xa1, 0x25, 0x2a, 0x17, 0xc4,
|
||||||
|
0xb5, 0xed, 0x2d, 0x71, 0x02, 0x21, 0x00, 0xc1,
|
||||||
|
0xa2, 0x51, 0xbb, 0xec, 0xb1, 0x4b, 0x05, 0x8a,
|
||||||
|
0x8b, 0xd7, 0x7f, 0x65, 0xde, 0x87, 0xe5, 0x1c,
|
||||||
|
0x47, 0xe9, 0x59, 0x04, 0xf4, 0xc0, 0xe9, 0xd5,
|
||||||
|
0x2e, 0xdd, 0xc2, 0x1c, 0x14, 0x15, 0xac,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"zero signature",
|
||||||
|
&btcec.Signature{
|
||||||
|
R: big.NewInt(0),
|
||||||
|
S: big.NewInt(0),
|
||||||
|
},
|
||||||
|
[]byte{0x30, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
result := test.ecsig.Serialize()
|
||||||
|
if !bytes.Equal(result, test.expected) {
|
||||||
|
t.Errorf("Serialize #%d (%s) unexpected result:\n"+
|
||||||
|
"got: %x\nwant: %x", i, test.name, result,
|
||||||
|
test.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testSignCompact(t *testing.T, tag string, curve *btcec.KoblitzCurve,
|
||||||
|
data []byte, isCompressed bool) {
|
||||||
|
tmp, _ := btcec.NewPrivateKey(curve)
|
||||||
|
priv := (*btcec.PrivateKey)(tmp)
|
||||||
|
|
||||||
|
hashed := []byte("testing")
|
||||||
|
sig, err := btcec.SignCompact(curve, priv, hashed, isCompressed)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: error signing: %s", tag, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pk, wasCompressed, err := btcec.RecoverCompact(curve, sig, hashed)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: error recovering: %s", tag, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if pk.X.Cmp(priv.X) != 0 || pk.Y.Cmp(priv.Y) != 0 {
|
||||||
|
t.Errorf("%s: recovered pubkey doesn't match original "+
|
||||||
|
"(%v,%v) vs (%v,%v) ", tag, pk.X, pk.Y, priv.X, priv.Y)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if wasCompressed != isCompressed {
|
||||||
|
t.Errorf("%s: recovered pubkey doesn't match compressed state "+
|
||||||
|
"(%v vs %v)", tag, isCompressed, wasCompressed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we change the compressed bit we should get the same key back,
|
||||||
|
// but the compressed flag should be reversed.
|
||||||
|
if isCompressed {
|
||||||
|
sig[0] -= 4
|
||||||
|
} else {
|
||||||
|
sig[0] += 4
|
||||||
|
}
|
||||||
|
|
||||||
|
pk, wasCompressed, err = btcec.RecoverCompact(curve, sig, hashed)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%s: error recovering (2): %s", tag, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if pk.X.Cmp(priv.X) != 0 || pk.Y.Cmp(priv.Y) != 0 {
|
||||||
|
t.Errorf("%s: recovered pubkey (2) doesn't match original "+
|
||||||
|
"(%v,%v) vs (%v,%v) ", tag, pk.X, pk.Y, priv.X, priv.Y)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if wasCompressed == isCompressed {
|
||||||
|
t.Errorf("%s: recovered pubkey doesn't match reversed "+
|
||||||
|
"compressed state (%v vs %v)", tag, isCompressed,
|
||||||
|
wasCompressed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSignCompact(t *testing.T) {
|
||||||
|
for i := 0; i < 256; i++ {
|
||||||
|
name := fmt.Sprintf("test %d", i)
|
||||||
|
data := make([]byte, 32)
|
||||||
|
_, err := rand.Read(data)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to read random data for %s", name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
compressed := i%2 != 0
|
||||||
|
testSignCompact(t, name, btcec.S256(), data, compressed)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue