2014-01-09 06:51:37 +01:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
2013-06-13 20:27:23 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package btcec_test
|
|
|
|
|
|
|
|
import (
|
2013-09-11 05:14:30 +02:00
|
|
|
"bytes"
|
2013-06-13 20:27:23 +02:00
|
|
|
"github.com/conformal/btcec"
|
2013-09-11 05:14:30 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2013-06-13 20:27:23 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-11-04 20:42:21 +01:00
|
|
|
type privKeyTest struct {
|
|
|
|
name string
|
|
|
|
key []byte
|
|
|
|
}
|
|
|
|
|
2013-06-13 20:27:23 +02:00
|
|
|
type pubKeyTest struct {
|
|
|
|
name string
|
|
|
|
key []byte
|
2013-09-11 05:14:30 +02:00
|
|
|
format byte
|
2013-06-13 20:27:23 +02:00
|
|
|
isValid bool
|
|
|
|
}
|
|
|
|
|
2013-11-04 20:42:21 +01:00
|
|
|
var privKeyTests = []privKeyTest{
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-11-04 20:42:21 +01:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-06-13 20:27:23 +02:00
|
|
|
var pubKeyTests = []pubKeyTest{
|
|
|
|
// pubkey from bitcoin blockchain tx
|
|
|
|
// 0437cd7f8525ceed2324359c2d0ba26006d92d85
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-13 20:27:23 +02:00
|
|
|
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,
|
2013-09-11 05:14:30 +02:00
|
|
|
format: btcec.TstPubkeyUncompressed,
|
2013-06-13 20:27:23 +02:00
|
|
|
},
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-13 20:27:23 +02:00
|
|
|
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,
|
|
|
|
},
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-13 20:27:23 +02:00
|
|
|
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,
|
|
|
|
},
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-14 00:55:52 +02:00
|
|
|
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,
|
2013-06-14 01:06:25 +02:00
|
|
|
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,
|
|
|
|
},
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-14 01:06:25 +02:00
|
|
|
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,
|
2013-09-11 05:14:30 +02:00
|
|
|
format: btcec.TstPubkeyHybrid,
|
2013-06-14 01:06:25 +02:00
|
|
|
},
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-14 01:06:25 +02:00
|
|
|
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,
|
2013-06-14 00:55:52 +02:00
|
|
|
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,
|
|
|
|
},
|
2013-06-13 20:27:23 +02:00
|
|
|
// from tx 0b09c51c51ff762f00fb26217269d2a18e77a4fa87d69b3c363ab4df16543f20
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-13 20:27:23 +02:00
|
|
|
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,
|
2013-09-11 05:14:30 +02:00
|
|
|
format: btcec.TstPubkeyCompressed,
|
2013-06-13 20:27:23 +02:00
|
|
|
},
|
|
|
|
// from tx fdeb8e72524e8dab0da507ddbaf5f88fe4a933eb10a66bc4745bb0aa11ea393c
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-13 20:27:23 +02:00
|
|
|
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,
|
2013-09-11 05:14:30 +02:00
|
|
|
format: btcec.TstPubkeyCompressed,
|
2013-06-13 20:27:23 +02:00
|
|
|
},
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-14 00:55:52 +02:00
|
|
|
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,
|
|
|
|
},
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-14 00:55:52 +02:00
|
|
|
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,
|
|
|
|
},
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-14 01:03:15 +02:00
|
|
|
name: "wrong length)",
|
|
|
|
key: []byte{0x05},
|
2013-06-14 00:55:52 +02:00
|
|
|
isValid: false,
|
|
|
|
},
|
2014-02-04 22:17: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xb4, 0x12, 0xa3,
|
|
|
|
},
|
|
|
|
isValid: false,
|
|
|
|
},
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-14 01:28:54 +02:00
|
|
|
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,
|
|
|
|
},
|
2014-02-04 22:17:45 +01:00
|
|
|
{
|
2013-06-14 01:28:54 +02:00
|
|
|
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,
|
|
|
|
},
|
2013-06-13 20:27:23 +02:00
|
|
|
}
|
|
|
|
|
2013-11-04 20:42:21 +01:00
|
|
|
func TestPrivKeys(t *testing.T) {
|
|
|
|
for _, test := range privKeyTests {
|
2014-05-15 13:16:24 +02:00
|
|
|
priv, pub := btcec.PrivKeyFromBytes(btcec.S256(), test.key)
|
2014-03-25 15:46:20 +01:00
|
|
|
|
|
|
|
_, err := btcec.ParsePubKey(
|
|
|
|
pub.SerializeUncompressed(), btcec.S256())
|
2013-11-04 20:42:21 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s privkey: %v", test.name, err)
|
|
|
|
continue
|
|
|
|
}
|
2014-05-15 13:16:24 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-20 06:21:39 +02:00
|
|
|
if !sig.Verify(hash, pub) {
|
2014-05-15 13:16:24 +02:00
|
|
|
t.Errorf("%s could not verify: %v", test.name, err)
|
|
|
|
continue
|
|
|
|
}
|
2013-11-04 20:42:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-13 20:27:23 +02:00
|
|
|
func TestPubKeys(t *testing.T) {
|
|
|
|
for _, test := range pubKeyTests {
|
2013-09-11 05:14:30 +02:00
|
|
|
pk, err := btcec.ParsePubKey(test.key, btcec.S256())
|
2013-06-13 20:27:23 +02:00
|
|
|
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)
|
2013-09-11 05:14:30 +02:00
|
|
|
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)
|
2013-06-13 20:27:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|