-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpublic_key_test.go
198 lines (169 loc) · 5.87 KB
/
public_key_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package easyecc
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"fmt"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
)
var serializedKeys = map[EllipticCurve]string{
SECP256K1: "0357a4f368868a8a6d572991e484e664810ff14c05c0fa023275251151fe0e53d1",
P256: "035959f21263a385367a2737020e9c912f7ec94a1c7f535bb104d8be472728bb84",
P384: "0283a4f85dbb29ffd415547c8f88924d2dc7d5c2e7ac371fded2360e645e142534d924d5fc7182298ae78e43dc042e3185",
P521: "02014f3231b54b107be2ed7ec03dd85e2169111a087bb29454200a06f2e470ef5060f97c255ce621a771c1689a4defafa057ae100c1552428de87c757510a71271b5cb",
}
type keyComponents struct {
X string
Y string
}
var serializedKeyComponents = map[EllipticCurve]keyComponents{
SECP256K1: {
X: "57a4f368868a8a6d572991e484e664810ff14c05c0fa023275251151fe0e53d1",
Y: "0d6cc87c5bc29b83368e17869e964f2f53d52ea3aa3e5a9efa1fa578123a0c6d",
},
P256: {
X: "5959f21263a385367a2737020e9c912f7ec94a1c7f535bb104d8be472728bb84",
Y: "669f338928b52ac42850f2444d1c1bf4db10e21a21151b39c126ed88ca1b93f5",
},
P384: {
X: "83a4f85dbb29ffd415547c8f88924d2dc7d5c2e7ac371fded2360e645e142534d924d5fc7182298ae78e43dc042e3185",
Y: "28cac85e8ab852371b3481ef26c3a08c7ab6834fca48b3238c80d924a79d8c2f3b1c2b54c8e2d949a77f6ce7f579bb08",
},
P521: {
X: "14f3231b54b107be2ed7ec03dd85e2169111a087bb29454200a06f2e470ef5060f97c255ce621a771c1689a4defafa057ae100c1552428de87c757510a71271b5cb",
Y: "172092d6462143a95cdbbb08dc1b2c464ce7ff915036cb9c844323e8c88cee4f4bc16d8bfbfafe3f3c1871373351bc69b46f541745d8acb5c66d3c24e70fde428a4",
},
}
func Test_PublicKey_SerializeCompressed(t *testing.T) {
assert := assert.New(t)
for _, curve := range curves {
privateKey := CreatePrivateKey(curve, big.NewInt(5001))
publicKey := privateKey.PublicKey()
serialized := publicKey.SerializeCompressed()
assert.EqualValues(serializedKeys[curve], fmt.Sprintf("%x", serialized))
assert.True(true)
}
}
func Test_PublicKey_Curve(t *testing.T) {
assert := assert.New(t)
for _, curve := range curves {
privateKey := CreatePrivateKey(curve, big.NewInt(5001))
publicKey := privateKey.PublicKey()
assert.Equal(curve, publicKey.Curve())
}
}
func Test_PublicKey_FromSerializedCompressed(t *testing.T) {
assert := assert.New(t)
for _, curve := range curves {
serialized, _ := new(big.Int).SetString(serializedKeys[curve], 16)
publicKey, err := DeserializeCompressed(curve, serialized.Bytes())
assert.NoError(err)
assert.NotNil(publicKey)
assert.EqualValues(serializedKeyComponents[curve].X, fmt.Sprintf("%064x", publicKey.publicKey.X))
assert.EqualValues(serializedKeyComponents[curve].Y, fmt.Sprintf("%064x", publicKey.publicKey.Y))
}
}
func Test_PublicKey_Address(t *testing.T) {
assert := assert.New(t)
secret, _ := new(big.Int).SetString("12345deadbeef", 16)
privateKey := NewPrivateKey(secret)
address, err := privateKey.PublicKey().BitcoinAddress()
assert.NoError(err)
assert.Equal("1F1Pn2y6pDb68E5nYJJeba4TLg2U7B6KF1", address)
}
func Test_PublicKey_Equal(t *testing.T) {
assert := assert.New(t)
privateKey1, err := NewRandomPrivateKey()
assert.NoError(err)
publicKey1 := privateKey1.PublicKey()
publicKey2, err := NewPublicFromSerializedCompressed(publicKey1.SerializeCompressed())
assert.NoError(err)
assert.True(publicKey1.Equal(publicKey2))
assert.True(publicKey1.EqualSerializedCompressed(publicKey2.SerializeCompressed()))
privateKey3, err := NewRandomPrivateKey()
assert.NoError(err)
publicKey3 := privateKey3.PublicKey()
assert.False(publicKey1.Equal(publicKey3))
assert.False(publicKey1.EqualSerializedCompressed(publicKey3.SerializeCompressed()))
}
func Test_PublicKey_SerializeSECP256K1(t *testing.T) {
assert := assert.New(t)
key, err := GeneratePrivateKey(SECP256K1)
assert.NoError(err)
b := key.PublicKey().Serialize()
assert.True(len(b) > 0)
publicKey, err := Deserialize(SECP256K1, b)
assert.NoError(err)
assert.True(publicKey.Equal(key.PublicKey()))
}
func Test_PublicKey_ToECDSA(t *testing.T) {
assert := assert.New(t)
for _, curve := range curves {
privateKey, err := GeneratePrivateKey(curve)
assert.NoError(err)
assert.NotNil(privateKey.PublicKey().ToECDSA())
}
}
func Test_PublicKey_BitcoinEthereumAddress(t *testing.T) {
assert := assert.New(t)
privateKey := CreatePrivateKey(SECP256K1, big.NewInt(12345))
publicKey := privateKey.PublicKey()
bitcoinAddress, err := publicKey.BitcoinAddress()
assert.NoError(err)
assert.Equal("12vieiAHxBe4qCUrwvfb2kRkDuc8kQ2VZ2", bitcoinAddress)
ethereumAddress, err := publicKey.EthereumAddress()
assert.NoError(err)
assert.Equal("0xEB4665750b1382DF4AeBF49E04B429AAAc4d9929", ethereumAddress)
wrongCurveKey := CreatePrivateKey(P521, big.NewInt(12345))
_, err = wrongCurveKey.PublicKey().BitcoinAddress()
assert.Equal(ErrUnsupportedCurve, err)
_, err = wrongCurveKey.PublicKey().EthereumAddress()
assert.Equal(ErrUnsupportedCurve, err)
}
func Test_PublicKey_CreateFromPoint(t *testing.T) {
randomBigInt := func(bitSize int) *big.Int {
max := new(big.Int)
max.Exp(big.NewInt(2), big.NewInt(int64(bitSize)), nil).Sub(max, big.NewInt(1))
n, err := rand.Int(rand.Reader, max)
if err != nil {
return nil
}
return n
}
x := randomBigInt(256)
y := randomBigInt(256)
type args struct {
curve elliptic.Curve
x *big.Int
y *big.Int
}
tests := []struct {
name string
args args
want *PublicKey
}{
{
name: "valid input",
args: args{curve: elliptic.P256(), x: x, y: y},
want: &PublicKey{publicKey: &ecdsa.PublicKey{elliptic.P256(), x, y}},
},
{
name: "invalid input x",
args: args{curve: elliptic.P256(), x: nil, y: y},
want: nil,
},
{
name: "invalid input y",
args: args{curve: elliptic.P256(), x: x, y: nil},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, CreatePublicKeyFromPoint(tt.args.curve,
tt.args.x, tt.args.y), "NewKey(%v, %v, %v)", tt.args.curve, tt.args.x, tt.args.y)
})
}
}