OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / btcec / btcec_test.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Copyright 2011 ThePiachu. All rights reserved.
3 // Copyright 2013-2016 The btcsuite developers
4 // Use of this source code is governed by an ISC
5 // license that can be found in the LICENSE file.
6
7 package btcec
8
9 import (
10         "crypto/rand"
11         "crypto/sha1"
12         "encoding/hex"
13         "fmt"
14         "math/big"
15         "testing"
16 )
17
18 // isJacobianOnS256Curve returns boolean if the point (x,y,z) is on the
19 // secp256k1 curve.
20 func isJacobianOnS256Curve(x, y, z *fieldVal) bool {
21         // Elliptic curve equation for secp256k1 is: y^2 = x^3 + 7
22         // In Jacobian coordinates, Y = y/z^3 and X = x/z^2
23         // Thus:
24         // (y/z^3)^2 = (x/z^2)^3 + 7
25         // y^2/z^6 = x^3/z^6 + 7
26         // y^2 = x^3 + 7*z^6
27         var y2, z2, x3, result fieldVal
28         y2.SquareVal(y).Normalize()
29         z2.SquareVal(z)
30         x3.SquareVal(x).Mul(x)
31         result.SquareVal(&z2).Mul(&z2).MulInt(7).Add(&x3).Normalize()
32         return y2.Equals(&result)
33 }
34
35 // TestAddJacobian tests addition of points projected in Jacobian coordinates.
36 func TestAddJacobian(t *testing.T) {
37         tests := []struct {
38                 x1, y1, z1 string // Coordinates (in hex) of first point to add
39                 x2, y2, z2 string // Coordinates (in hex) of second point to add
40                 x3, y3, z3 string // Coordinates (in hex) of expected point
41         }{
42                 // Addition with a point at infinity (left hand side).
43                 // ∞ + P = P
44                 {
45                         "0",
46                         "0",
47                         "0",
48                         "d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
49                         "131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
50                         "1",
51                         "d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
52                         "131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
53                         "1",
54                 },
55                 // Addition with a point at infinity (right hand side).
56                 // P + ∞ = P
57                 {
58                         "d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
59                         "131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
60                         "1",
61                         "0",
62                         "0",
63                         "0",
64                         "d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
65                         "131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
66                         "1",
67                 },
68                 // Addition with z1=z2=1 different x values.
69                 {
70                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
71                         "0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
72                         "1",
73                         "d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
74                         "131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
75                         "1",
76                         "0cfbc7da1e569b334460788faae0286e68b3af7379d5504efc25e4dba16e46a6",
77                         "e205f79361bbe0346b037b4010985dbf4f9e1e955e7d0d14aca876bfa79aad87",
78                         "44a5646b446e3877a648d6d381370d9ef55a83b666ebce9df1b1d7d65b817b2f",
79                 },
80                 // Addition with z1=z2=1 same x opposite y.
81                 // P(x, y, z) + P(x, -y, z) = infinity
82                 {
83                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
84                         "0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
85                         "1",
86                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
87                         "f48e156428cf0276dc092da5856e182288d7569f97934a56fe44be60f0d359fd",
88                         "1",
89                         "0",
90                         "0",
91                         "0",
92                 },
93                 // Addition with z1=z2=1 same point.
94                 // P(x, y, z) + P(x, y, z) = 2P
95                 {
96                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
97                         "0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
98                         "1",
99                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
100                         "0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
101                         "1",
102                         "ec9f153b13ee7bd915882859635ea9730bf0dc7611b2c7b0e37ee64f87c50c27",
103                         "b082b53702c466dcf6e984a35671756c506c67c2fcb8adb408c44dd0755c8f2a",
104                         "16e3d537ae61fb1247eda4b4f523cfbaee5152c0d0d96b520376833c1e594464",
105                 },
106
107                 // Addition with z1=z2 (!=1) different x values.
108                 {
109                         "d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
110                         "5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
111                         "2",
112                         "5d2fe112c21891d440f65a98473cb626111f8a234d2cd82f22172e369f002147",
113                         "98e3386a0a622a35c4561ffb32308d8e1c6758e10ebb1b4ebd3d04b4eb0ecbe8",
114                         "2",
115                         "cfbc7da1e569b334460788faae0286e68b3af7379d5504efc25e4dba16e46a60",
116                         "817de4d86ef80d1ac0ded00426176fd3e787a5579f43452b2a1db021e6ac3778",
117                         "129591ad11b8e1de99235b4e04dc367bd56a0ed99baf3a77c6c75f5a6e05f08d",
118                 },
119                 // Addition with z1=z2 (!=1) same x opposite y.
120                 // P(x, y, z) + P(x, -y, z) = infinity
121                 {
122                         "d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
123                         "5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
124                         "2",
125                         "d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
126                         "a470ab21467813b6e0496d2c2b70c11446bab4fcbc9a52b7f225f30e869aea9f",
127                         "2",
128                         "0",
129                         "0",
130                         "0",
131                 },
132                 // Addition with z1=z2 (!=1) same point.
133                 // P(x, y, z) + P(x, y, z) = 2P
134                 {
135                         "d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
136                         "5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
137                         "2",
138                         "d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
139                         "5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
140                         "2",
141                         "9f153b13ee7bd915882859635ea9730bf0dc7611b2c7b0e37ee65073c50fabac",
142                         "2b53702c466dcf6e984a35671756c506c67c2fcb8adb408c44dd125dc91cb988",
143                         "6e3d537ae61fb1247eda4b4f523cfbaee5152c0d0d96b520376833c2e5944a11",
144                 },
145
146                 // Addition with z1!=z2 and z2=1 different x values.
147                 {
148                         "d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
149                         "5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
150                         "2",
151                         "d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
152                         "131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
153                         "1",
154                         "3ef1f68795a6ccd1181e23eab80a1b9a2cebdcde755413bf097936eb5b91b4f3",
155                         "0bef26c377c068d606f6802130bb7e9f3c3d2abcfa1a295950ed81133561cb04",
156                         "252b235a2371c3bd3246b69c09b86cf7aad41db3375e74ef8d8ebeb4dc0be11a",
157                 },
158                 // Addition with z1!=z2 and z2=1 same x opposite y.
159                 // P(x, y, z) + P(x, -y, z) = infinity
160                 {
161                         "d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
162                         "5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
163                         "2",
164                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
165                         "f48e156428cf0276dc092da5856e182288d7569f97934a56fe44be60f0d359fd",
166                         "1",
167                         "0",
168                         "0",
169                         "0",
170                 },
171                 // Addition with z1!=z2 and z2=1 same point.
172                 // P(x, y, z) + P(x, y, z) = 2P
173                 {
174                         "d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
175                         "5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
176                         "2",
177                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
178                         "0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
179                         "1",
180                         "9f153b13ee7bd915882859635ea9730bf0dc7611b2c7b0e37ee65073c50fabac",
181                         "2b53702c466dcf6e984a35671756c506c67c2fcb8adb408c44dd125dc91cb988",
182                         "6e3d537ae61fb1247eda4b4f523cfbaee5152c0d0d96b520376833c2e5944a11",
183                 },
184
185                 // Addition with z1!=z2 and z2!=1 different x values.
186                 // P(x, y, z) + P(x, y, z) = 2P
187                 {
188                         "d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
189                         "5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
190                         "2",
191                         "91abba6a34b7481d922a4bd6a04899d5a686f6cf6da4e66a0cb427fb25c04bd4",
192                         "03fede65e30b4e7576a2abefc963ddbf9fdccbf791b77c29beadefe49951f7d1",
193                         "3",
194                         "3f07081927fd3f6dadd4476614c89a09eba7f57c1c6c3b01fa2d64eac1eef31e",
195                         "949166e04ebc7fd95a9d77e5dfd88d1492ecffd189792e3944eb2b765e09e031",
196                         "eb8cba81bcffa4f44d75427506737e1f045f21e6d6f65543ee0e1d163540c931",
197                 }, // Addition with z1!=z2 and z2!=1 same x opposite y.
198                 // P(x, y, z) + P(x, -y, z) = infinity
199                 {
200                         "d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
201                         "5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
202                         "2",
203                         "dcc3768780c74a0325e2851edad0dc8a566fa61a9e7fc4a34d13dcb509f99bc7",
204                         "cafc41904dd5428934f7d075129c8ba46eb622d4fc88d72cd1401452664add18",
205                         "3",
206                         "0",
207                         "0",
208                         "0",
209                 },
210                 // Addition with z1!=z2 and z2!=1 same point.
211                 // P(x, y, z) + P(x, y, z) = 2P
212                 {
213                         "d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
214                         "5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
215                         "2",
216                         "dcc3768780c74a0325e2851edad0dc8a566fa61a9e7fc4a34d13dcb509f99bc7",
217                         "3503be6fb22abd76cb082f8aed63745b9149dd2b037728d32ebfebac99b51f17",
218                         "3",
219                         "9f153b13ee7bd915882859635ea9730bf0dc7611b2c7b0e37ee65073c50fabac",
220                         "2b53702c466dcf6e984a35671756c506c67c2fcb8adb408c44dd125dc91cb988",
221                         "6e3d537ae61fb1247eda4b4f523cfbaee5152c0d0d96b520376833c2e5944a11",
222                 },
223         }
224
225         t.Logf("Running %d tests", len(tests))
226         for i, test := range tests {
227                 // Convert hex to field values.
228                 x1 := new(fieldVal).SetHex(test.x1)
229                 y1 := new(fieldVal).SetHex(test.y1)
230                 z1 := new(fieldVal).SetHex(test.z1)
231                 x2 := new(fieldVal).SetHex(test.x2)
232                 y2 := new(fieldVal).SetHex(test.y2)
233                 z2 := new(fieldVal).SetHex(test.z2)
234                 x3 := new(fieldVal).SetHex(test.x3)
235                 y3 := new(fieldVal).SetHex(test.y3)
236                 z3 := new(fieldVal).SetHex(test.z3)
237
238                 // Ensure the test data is using points that are actually on
239                 // the curve (or the point at infinity).
240                 if !z1.IsZero() && !isJacobianOnS256Curve(x1, y1, z1) {
241                         t.Errorf("#%d first point is not on the curve -- "+
242                                 "invalid test data", i)
243                         continue
244                 }
245                 if !z2.IsZero() && !isJacobianOnS256Curve(x2, y2, z2) {
246                         t.Errorf("#%d second point is not on the curve -- "+
247                                 "invalid test data", i)
248                         continue
249                 }
250                 if !z3.IsZero() && !isJacobianOnS256Curve(x3, y3, z3) {
251                         t.Errorf("#%d expected point is not on the curve -- "+
252                                 "invalid test data", i)
253                         continue
254                 }
255
256                 // Add the two points.
257                 rx, ry, rz := new(fieldVal), new(fieldVal), new(fieldVal)
258                 S256().addJacobian(x1, y1, z1, x2, y2, z2, rx, ry, rz)
259
260                 // Ensure result matches expected.
261                 if !rx.Equals(x3) || !ry.Equals(y3) || !rz.Equals(z3) {
262                         t.Errorf("#%d wrong result\ngot: (%v, %v, %v)\n"+
263                                 "want: (%v, %v, %v)", i, rx, ry, rz, x3, y3, z3)
264                         continue
265                 }
266         }
267 }
268
269 // TestAddAffine tests addition of points in affine coordinates.
270 func TestAddAffine(t *testing.T) {
271         tests := []struct {
272                 x1, y1 string // Coordinates (in hex) of first point to add
273                 x2, y2 string // Coordinates (in hex) of second point to add
274                 x3, y3 string // Coordinates (in hex) of expected point
275         }{
276                 // Addition with a point at infinity (left hand side).
277                 // ∞ + P = P
278                 {
279                         "0",
280                         "0",
281                         "d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
282                         "131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
283                         "d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
284                         "131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
285                 },
286                 // Addition with a point at infinity (right hand side).
287                 // P + ∞ = P
288                 {
289                         "d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
290                         "131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
291                         "0",
292                         "0",
293                         "d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
294                         "131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
295                 },
296
297                 // Addition with different x values.
298                 {
299                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
300                         "0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
301                         "d74bf844b0862475103d96a611cf2d898447e288d34b360bc885cb8ce7c00575",
302                         "131c670d414c4546b88ac3ff664611b1c38ceb1c21d76369d7a7a0969d61d97d",
303                         "fd5b88c21d3143518d522cd2796f3d726793c88b3e05636bc829448e053fed69",
304                         "21cf4f6a5be5ff6380234c50424a970b1f7e718f5eb58f68198c108d642a137f",
305                 },
306                 // Addition with same x opposite y.
307                 // P(x, y) + P(x, -y) = infinity
308                 {
309                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
310                         "0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
311                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
312                         "f48e156428cf0276dc092da5856e182288d7569f97934a56fe44be60f0d359fd",
313                         "0",
314                         "0",
315                 },
316                 // Addition with same point.
317                 // P(x, y) + P(x, y) = 2P
318                 {
319                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
320                         "0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
321                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
322                         "0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
323                         "59477d88ae64a104dbb8d31ec4ce2d91b2fe50fa628fb6a064e22582196b365b",
324                         "938dc8c0f13d1e75c987cb1a220501bd614b0d3dd9eb5c639847e1240216e3b6",
325                 },
326         }
327
328         t.Logf("Running %d tests", len(tests))
329         for i, test := range tests {
330                 // Convert hex to field values.
331                 x1, y1 := fromHex(test.x1), fromHex(test.y1)
332                 x2, y2 := fromHex(test.x2), fromHex(test.y2)
333                 x3, y3 := fromHex(test.x3), fromHex(test.y3)
334
335                 // Ensure the test data is using points that are actually on
336                 // the curve (or the point at infinity).
337                 if !(x1.Sign() == 0 && y1.Sign() == 0) && !S256().IsOnCurve(x1, y1) {
338                         t.Errorf("#%d first point is not on the curve -- "+
339                                 "invalid test data", i)
340                         continue
341                 }
342                 if !(x2.Sign() == 0 && y2.Sign() == 0) && !S256().IsOnCurve(x2, y2) {
343                         t.Errorf("#%d second point is not on the curve -- "+
344                                 "invalid test data", i)
345                         continue
346                 }
347                 if !(x3.Sign() == 0 && y3.Sign() == 0) && !S256().IsOnCurve(x3, y3) {
348                         t.Errorf("#%d expected point is not on the curve -- "+
349                                 "invalid test data", i)
350                         continue
351                 }
352
353                 // Add the two points.
354                 rx, ry := S256().Add(x1, y1, x2, y2)
355
356                 // Ensure result matches expected.
357                 if rx.Cmp(x3) != 00 || ry.Cmp(y3) != 0 {
358                         t.Errorf("#%d wrong result\ngot: (%x, %x)\n"+
359                                 "want: (%x, %x)", i, rx, ry, x3, y3)
360                         continue
361                 }
362         }
363 }
364
365 // TestDoubleJacobian tests doubling of points projected in Jacobian
366 // coordinates.
367 func TestDoubleJacobian(t *testing.T) {
368         tests := []struct {
369                 x1, y1, z1 string // Coordinates (in hex) of point to double
370                 x3, y3, z3 string // Coordinates (in hex) of expected point
371         }{
372                 // Doubling a point at infinity is still infinity.
373                 {
374                         "0",
375                         "0",
376                         "0",
377                         "0",
378                         "0",
379                         "0",
380                 },
381                 // Doubling with z1=1.
382                 {
383                         "34f9460f0e4f08393d192b3c5133a6ba099aa0ad9fd54ebccfacdfa239ff49c6",
384                         "0b71ea9bd730fd8923f6d25a7a91e7dd7728a960686cb5a901bb419e0f2ca232",
385                         "1",
386                         "ec9f153b13ee7bd915882859635ea9730bf0dc7611b2c7b0e37ee64f87c50c27",
387                         "b082b53702c466dcf6e984a35671756c506c67c2fcb8adb408c44dd0755c8f2a",
388                         "16e3d537ae61fb1247eda4b4f523cfbaee5152c0d0d96b520376833c1e594464",
389                 },
390                 // Doubling with z1!=1.
391                 {
392                         "d3e5183c393c20e4f464acf144ce9ae8266a82b67f553af33eb37e88e7fd2718",
393                         "5b8f54deb987ec491fb692d3d48f3eebb9454b034365ad480dda0cf079651190",
394                         "2",
395                         "9f153b13ee7bd915882859635ea9730bf0dc7611b2c7b0e37ee65073c50fabac",
396                         "2b53702c466dcf6e984a35671756c506c67c2fcb8adb408c44dd125dc91cb988",
397                         "6e3d537ae61fb1247eda4b4f523cfbaee5152c0d0d96b520376833c2e5944a11",
398                 },
399                 // From btcd issue #709.
400                 {
401                         "201e3f75715136d2f93c4f4598f91826f94ca01f4233a5bd35de9708859ca50d",
402                         "bdf18566445e7562c6ada68aef02d498d7301503de5b18c6aef6e2b1722412e1",
403                         "0000000000000000000000000000000000000000000000000000000000000001",
404                         "4a5e0559863ebb4e9ed85f5c4fa76003d05d9a7626616e614a1f738621e3c220",
405                         "00000000000000000000000000000000000000000000000000000001b1388778",
406                         "7be30acc88bceac58d5b4d15de05a931ae602a07bcb6318d5dedc563e4482993",
407                 },
408         }
409
410         t.Logf("Running %d tests", len(tests))
411         for i, test := range tests {
412                 // Convert hex to field values.
413                 x1 := new(fieldVal).SetHex(test.x1)
414                 y1 := new(fieldVal).SetHex(test.y1)
415                 z1 := new(fieldVal).SetHex(test.z1)
416                 x3 := new(fieldVal).SetHex(test.x3)
417                 y3 := new(fieldVal).SetHex(test.y3)
418                 z3 := new(fieldVal).SetHex(test.z3)
419
420                 // Ensure the test data is using points that are actually on
421                 // the curve (or the point at infinity).
422                 if !z1.IsZero() && !isJacobianOnS256Curve(x1, y1, z1) {
423                         t.Errorf("#%d first point is not on the curve -- "+
424                                 "invalid test data", i)
425                         continue
426                 }
427                 if !z3.IsZero() && !isJacobianOnS256Curve(x3, y3, z3) {
428                         t.Errorf("#%d expected point is not on the curve -- "+
429                                 "invalid test data", i)
430                         continue
431                 }
432
433                 // Double the point.
434                 rx, ry, rz := new(fieldVal), new(fieldVal), new(fieldVal)
435                 S256().doubleJacobian(x1, y1, z1, rx, ry, rz)
436
437                 // Ensure result matches expected.
438                 if !rx.Equals(x3) || !ry.Equals(y3) || !rz.Equals(z3) {
439                         t.Errorf("#%d wrong result\ngot: (%v, %v, %v)\n"+
440                                 "want: (%v, %v, %v)", i, rx, ry, rz, x3, y3, z3)
441                         continue
442                 }
443         }
444 }
445
446 // TestDoubleAffine tests doubling of points in affine coordinates.
447 func TestDoubleAffine(t *testing.T) {
448         tests := []struct {
449                 x1, y1 string // Coordinates (in hex) of point to double
450                 x3, y3 string // Coordinates (in hex) of expected point
451         }{
452                 // Doubling a point at infinity is still infinity.
453                 // 2*∞ = ∞ (point at infinity)
454
455                 {
456                         "0",
457                         "0",
458                         "0",
459                         "0",
460                 },
461
462                 // Random points.
463                 {
464                         "e41387ffd8baaeeb43c2faa44e141b19790e8ac1f7ff43d480dc132230536f86",
465                         "1b88191d430f559896149c86cbcb703193105e3cf3213c0c3556399836a2b899",
466                         "88da47a089d333371bd798c548ef7caae76e737c1980b452d367b3cfe3082c19",
467                         "3b6f659b09a362821dfcfefdbfbc2e59b935ba081b6c249eb147b3c2100b1bc1",
468                 },
469                 {
470                         "b3589b5d984f03ef7c80aeae444f919374799edf18d375cab10489a3009cff0c",
471                         "c26cf343875b3630e15bccc61202815b5d8f1fd11308934a584a5babe69db36a",
472                         "e193860172998751e527bb12563855602a227fc1f612523394da53b746bb2fb1",
473                         "2bfcf13d2f5ab8bb5c611fab5ebbed3dc2f057062b39a335224c22f090c04789",
474                 },
475                 {
476                         "2b31a40fbebe3440d43ac28dba23eee71c62762c3fe3dbd88b4ab82dc6a82340",
477                         "9ba7deb02f5c010e217607fd49d58db78ec273371ea828b49891ce2fd74959a1",
478                         "2c8d5ef0d343b1a1a48aa336078eadda8481cb048d9305dc4fdf7ee5f65973a2",
479                         "bb4914ac729e26d3cd8f8dc8f702f3f4bb7e0e9c5ae43335f6e94c2de6c3dc95",
480                 },
481                 {
482                         "61c64b760b51981fab54716d5078ab7dffc93730b1d1823477e27c51f6904c7a",
483                         "ef6eb16ea1a36af69d7f66524c75a3a5e84c13be8fbc2e811e0563c5405e49bd",
484                         "5f0dcdd2595f5ad83318a0f9da481039e36f135005420393e72dfca985b482f4",
485                         "a01c849b0837065c1cb481b0932c441f49d1cab1b4b9f355c35173d93f110ae0",
486                 },
487         }
488
489         t.Logf("Running %d tests", len(tests))
490         for i, test := range tests {
491                 // Convert hex to field values.
492                 x1, y1 := fromHex(test.x1), fromHex(test.y1)
493                 x3, y3 := fromHex(test.x3), fromHex(test.y3)
494
495                 // Ensure the test data is using points that are actually on
496                 // the curve (or the point at infinity).
497                 if !(x1.Sign() == 0 && y1.Sign() == 0) && !S256().IsOnCurve(x1, y1) {
498                         t.Errorf("#%d first point is not on the curve -- "+
499                                 "invalid test data", i)
500                         continue
501                 }
502                 if !(x3.Sign() == 0 && y3.Sign() == 0) && !S256().IsOnCurve(x3, y3) {
503                         t.Errorf("#%d expected point is not on the curve -- "+
504                                 "invalid test data", i)
505                         continue
506                 }
507
508                 // Double the point.
509                 rx, ry := S256().Double(x1, y1)
510
511                 // Ensure result matches expected.
512                 if rx.Cmp(x3) != 00 || ry.Cmp(y3) != 0 {
513                         t.Errorf("#%d wrong result\ngot: (%x, %x)\n"+
514                                 "want: (%x, %x)", i, rx, ry, x3, y3)
515                         continue
516                 }
517         }
518 }
519
520 func TestOnCurve(t *testing.T) {
521         s256 := S256()
522         if !s256.IsOnCurve(s256.Params().Gx, s256.Params().Gy) {
523                 t.Errorf("FAIL S256")
524         }
525 }
526
527 type baseMultTest struct {
528         k    string
529         x, y string
530 }
531
532 //TODO: add more test vectors
533 var s256BaseMultTests = []baseMultTest{
534         {
535                 "AA5E28D6A97A2479A65527F7290311A3624D4CC0FA1578598EE3C2613BF99522",
536                 "34F9460F0E4F08393D192B3C5133A6BA099AA0AD9FD54EBCCFACDFA239FF49C6",
537                 "B71EA9BD730FD8923F6D25A7A91E7DD7728A960686CB5A901BB419E0F2CA232",
538         },
539         {
540                 "7E2B897B8CEBC6361663AD410835639826D590F393D90A9538881735256DFAE3",
541                 "D74BF844B0862475103D96A611CF2D898447E288D34B360BC885CB8CE7C00575",
542                 "131C670D414C4546B88AC3FF664611B1C38CEB1C21D76369D7A7A0969D61D97D",
543         },
544         {
545                 "6461E6DF0FE7DFD05329F41BF771B86578143D4DD1F7866FB4CA7E97C5FA945D",
546                 "E8AECC370AEDD953483719A116711963CE201AC3EB21D3F3257BB48668C6A72F",
547                 "C25CAF2F0EBA1DDB2F0F3F47866299EF907867B7D27E95B3873BF98397B24EE1",
548         },
549         {
550                 "376A3A2CDCD12581EFFF13EE4AD44C4044B8A0524C42422A7E1E181E4DEECCEC",
551                 "14890E61FCD4B0BD92E5B36C81372CA6FED471EF3AA60A3E415EE4FE987DABA1",
552                 "297B858D9F752AB42D3BCA67EE0EB6DCD1C2B7B0DBE23397E66ADC272263F982",
553         },
554         {
555                 "1B22644A7BE026548810C378D0B2994EEFA6D2B9881803CB02CEFF865287D1B9",
556                 "F73C65EAD01C5126F28F442D087689BFA08E12763E0CEC1D35B01751FD735ED3",
557                 "F449A8376906482A84ED01479BD18882B919C140D638307F0C0934BA12590BDE",
558         },
559 }
560
561 //TODO: test different curves as well?
562 func TestBaseMult(t *testing.T) {
563         s256 := S256()
564         for i, e := range s256BaseMultTests {
565                 k, ok := new(big.Int).SetString(e.k, 16)
566                 if !ok {
567                         t.Errorf("%d: bad value for k: %s", i, e.k)
568                 }
569                 x, y := s256.ScalarBaseMult(k.Bytes())
570                 if fmt.Sprintf("%X", x) != e.x || fmt.Sprintf("%X", y) != e.y {
571                         t.Errorf("%d: bad output for k=%s: got (%X, %X), want (%s, %s)", i, e.k, x, y, e.x, e.y)
572                 }
573                 if testing.Short() && i > 5 {
574                         break
575                 }
576         }
577 }
578
579 func TestBaseMultVerify(t *testing.T) {
580         s256 := S256()
581         for bytes := 1; bytes < 40; bytes++ {
582                 for i := 0; i < 30; i++ {
583                         data := make([]byte, bytes)
584                         _, err := rand.Read(data)
585                         if err != nil {
586                                 t.Errorf("failed to read random data for %d", i)
587                                 continue
588                         }
589                         x, y := s256.ScalarBaseMult(data)
590                         xWant, yWant := s256.ScalarMult(s256.Gx, s256.Gy, data)
591                         if x.Cmp(xWant) != 0 || y.Cmp(yWant) != 0 {
592                                 t.Errorf("%d: bad output for %X: got (%X, %X), want (%X, %X)", i, data, x, y, xWant, yWant)
593                         }
594                         if testing.Short() && i > 2 {
595                                 break
596                         }
597                 }
598         }
599 }
600
601 func TestScalarMult(t *testing.T) {
602         tests := []struct {
603                 x  string
604                 y  string
605                 k  string
606                 rx string
607                 ry string
608         }{
609                 // base mult, essentially.
610                 {
611                         "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
612                         "483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8",
613                         "18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725",
614                         "50863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352",
615                         "2cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6",
616                 },
617                 // From btcd issue #709.
618                 {
619                         "000000000000000000000000000000000000000000000000000000000000002c",
620                         "420e7a99bba18a9d3952597510fd2b6728cfeafc21a4e73951091d4d8ddbe94e",
621                         "a2e8ba2e8ba2e8ba2e8ba2e8ba2e8ba219b51835b55cc30ebfe2f6599bc56f58",
622                         "a2112dcdfbcd10ae1133a358de7b82db68e0a3eb4b492cc8268d1e7118c98788",
623                         "27fc7463b7bb3c5f98ecf2c84a6272bb1681ed553d92c69f2dfe25a9f9fd3836",
624                 },
625         }
626
627         s256 := S256()
628         for i, test := range tests {
629                 x, _ := new(big.Int).SetString(test.x, 16)
630                 y, _ := new(big.Int).SetString(test.y, 16)
631                 k, _ := new(big.Int).SetString(test.k, 16)
632                 xWant, _ := new(big.Int).SetString(test.rx, 16)
633                 yWant, _ := new(big.Int).SetString(test.ry, 16)
634                 xGot, yGot := s256.ScalarMult(x, y, k.Bytes())
635                 if xGot.Cmp(xWant) != 0 || yGot.Cmp(yWant) != 0 {
636                         t.Fatalf("%d: bad output: got (%X, %X), want (%X, %X)", i, xGot, yGot, xWant, yWant)
637                 }
638         }
639 }
640
641 func TestScalarMultRand(t *testing.T) {
642         // Strategy for this test:
643         // Get a random exponent from the generator point at first
644         // This creates a new point which is used in the next iteration
645         // Use another random exponent on the new point.
646         // We use BaseMult to verify by multiplying the previous exponent
647         // and the new random exponent together (mod N)
648         s256 := S256()
649         x, y := s256.Gx, s256.Gy
650         exponent := big.NewInt(1)
651         for i := 0; i < 1024; i++ {
652                 data := make([]byte, 32)
653                 _, err := rand.Read(data)
654                 if err != nil {
655                         t.Fatalf("failed to read random data at %d", i)
656                         break
657                 }
658                 x, y = s256.ScalarMult(x, y, data)
659                 exponent.Mul(exponent, new(big.Int).SetBytes(data))
660                 xWant, yWant := s256.ScalarBaseMult(exponent.Bytes())
661                 if x.Cmp(xWant) != 0 || y.Cmp(yWant) != 0 {
662                         t.Fatalf("%d: bad output for %X: got (%X, %X), want (%X, %X)", i, data, x, y, xWant, yWant)
663                         break
664                 }
665         }
666 }
667
668 func TestSplitK(t *testing.T) {
669         tests := []struct {
670                 k      string
671                 k1, k2 string
672                 s1, s2 int
673         }{
674                 {
675                         "6df2b5d30854069ccdec40ae022f5c948936324a4e9ebed8eb82cfd5a6b6d766",
676                         "00000000000000000000000000000000b776e53fb55f6b006a270d42d64ec2b1",
677                         "00000000000000000000000000000000d6cc32c857f1174b604eefc544f0c7f7",
678                         -1, -1,
679                 },
680                 {
681                         "6ca00a8f10632170accc1b3baf2a118fa5725f41473f8959f34b8f860c47d88d",
682                         "0000000000000000000000000000000007b21976c1795723c1bfbfa511e95b84",
683                         "00000000000000000000000000000000d8d2d5f9d20fc64fd2cf9bda09a5bf90",
684                         1, -1,
685                 },
686                 {
687                         "b2eda8ab31b259032d39cbc2a234af17fcee89c863a8917b2740b67568166289",
688                         "00000000000000000000000000000000507d930fecda7414fc4a523b95ef3c8c",
689                         "00000000000000000000000000000000f65ffb179df189675338c6185cb839be",
690                         -1, -1,
691                 },
692                 {
693                         "f6f00e44f179936f2befc7442721b0633f6bafdf7161c167ffc6f7751980e3a0",
694                         "0000000000000000000000000000000008d0264f10bcdcd97da3faa38f85308d",
695                         "0000000000000000000000000000000065fed1506eb6605a899a54e155665f79",
696                         -1, -1,
697                 },
698                 {
699                         "8679085ab081dc92cdd23091ce3ee998f6b320e419c3475fae6b5b7d3081996e",
700                         "0000000000000000000000000000000089fbf24fbaa5c3c137b4f1cedc51d975",
701                         "00000000000000000000000000000000d38aa615bd6754d6f4d51ccdaf529fea",
702                         -1, -1,
703                 },
704                 {
705                         "6b1247bb7931dfcae5b5603c8b5ae22ce94d670138c51872225beae6bba8cdb3",
706                         "000000000000000000000000000000008acc2a521b21b17cfb002c83be62f55d",
707                         "0000000000000000000000000000000035f0eff4d7430950ecb2d94193dedc79",
708                         -1, -1,
709                 },
710                 {
711                         "a2e8ba2e8ba2e8ba2e8ba2e8ba2e8ba219b51835b55cc30ebfe2f6599bc56f58",
712                         "0000000000000000000000000000000045c53aa1bb56fcd68c011e2dad6758e4",
713                         "00000000000000000000000000000000a2e79d200f27f2360fba57619936159b",
714                         -1, -1,
715                 },
716         }
717
718         s256 := S256()
719         for i, test := range tests {
720                 k, ok := new(big.Int).SetString(test.k, 16)
721                 if !ok {
722                         t.Errorf("%d: bad value for k: %s", i, test.k)
723                 }
724                 k1, k2, k1Sign, k2Sign := s256.splitK(k.Bytes())
725                 k1str := fmt.Sprintf("%064x", k1)
726                 if test.k1 != k1str {
727                         t.Errorf("%d: bad k1: got %v, want %v", i, k1str, test.k1)
728                 }
729                 k2str := fmt.Sprintf("%064x", k2)
730                 if test.k2 != k2str {
731                         t.Errorf("%d: bad k2: got %v, want %v", i, k2str, test.k2)
732                 }
733                 if test.s1 != k1Sign {
734                         t.Errorf("%d: bad k1 sign: got %d, want %d", i, k1Sign, test.s1)
735                 }
736                 if test.s2 != k2Sign {
737                         t.Errorf("%d: bad k2 sign: got %d, want %d", i, k2Sign, test.s2)
738                 }
739                 k1Int := new(big.Int).SetBytes(k1)
740                 k1SignInt := new(big.Int).SetInt64(int64(k1Sign))
741                 k1Int.Mul(k1Int, k1SignInt)
742                 k2Int := new(big.Int).SetBytes(k2)
743                 k2SignInt := new(big.Int).SetInt64(int64(k2Sign))
744                 k2Int.Mul(k2Int, k2SignInt)
745                 gotK := new(big.Int).Mul(k2Int, s256.lambda)
746                 gotK.Add(k1Int, gotK)
747                 gotK.Mod(gotK, s256.N)
748                 if k.Cmp(gotK) != 0 {
749                         t.Errorf("%d: bad k: got %X, want %X", i, gotK.Bytes(), k.Bytes())
750                 }
751         }
752 }
753
754 func TestSplitKRand(t *testing.T) {
755         s256 := S256()
756         for i := 0; i < 1024; i++ {
757                 bytesK := make([]byte, 32)
758                 _, err := rand.Read(bytesK)
759                 if err != nil {
760                         t.Fatalf("failed to read random data at %d", i)
761                         break
762                 }
763                 k := new(big.Int).SetBytes(bytesK)
764                 k1, k2, k1Sign, k2Sign := s256.splitK(bytesK)
765                 k1Int := new(big.Int).SetBytes(k1)
766                 k1SignInt := new(big.Int).SetInt64(int64(k1Sign))
767                 k1Int.Mul(k1Int, k1SignInt)
768                 k2Int := new(big.Int).SetBytes(k2)
769                 k2SignInt := new(big.Int).SetInt64(int64(k2Sign))
770                 k2Int.Mul(k2Int, k2SignInt)
771                 gotK := new(big.Int).Mul(k2Int, s256.lambda)
772                 gotK.Add(k1Int, gotK)
773                 gotK.Mod(gotK, s256.N)
774                 if k.Cmp(gotK) != 0 {
775                         t.Errorf("%d: bad k: got %X, want %X", i, gotK.Bytes(), k.Bytes())
776                 }
777         }
778 }
779
780 // Test this curve's usage with the ecdsa package.
781
782 func testKeyGeneration(t *testing.T, c *KoblitzCurve, tag string) {
783         priv, err := NewPrivateKey(c)
784         if err != nil {
785                 t.Errorf("%s: error: %s", tag, err)
786                 return
787         }
788         if !c.IsOnCurve(priv.PublicKey.X, priv.PublicKey.Y) {
789                 t.Errorf("%s: public key invalid: %s", tag, err)
790         }
791 }
792
793 func TestKeyGeneration(t *testing.T) {
794         testKeyGeneration(t, S256(), "S256")
795 }
796
797 func testSignAndVerify(t *testing.T, c *KoblitzCurve, tag string) {
798         priv, _ := NewPrivateKey(c)
799         pub := priv.PubKey()
800
801         hashed := []byte("testing")
802         sig, err := priv.Sign(hashed)
803         if err != nil {
804                 t.Errorf("%s: error signing: %s", tag, err)
805                 return
806         }
807
808         if !sig.Verify(hashed, pub) {
809                 t.Errorf("%s: Verify failed", tag)
810         }
811
812         hashed[0] ^= 0xff
813         if sig.Verify(hashed, pub) {
814                 t.Errorf("%s: Verify always works!", tag)
815         }
816 }
817
818 func TestSignAndVerify(t *testing.T) {
819         testSignAndVerify(t, S256(), "S256")
820 }
821
822 func TestNAF(t *testing.T) {
823         tests := []string{
824                 "6df2b5d30854069ccdec40ae022f5c948936324a4e9ebed8eb82cfd5a6b6d766",
825                 "b776e53fb55f6b006a270d42d64ec2b1",
826                 "d6cc32c857f1174b604eefc544f0c7f7",
827                 "45c53aa1bb56fcd68c011e2dad6758e4",
828                 "a2e79d200f27f2360fba57619936159b",
829         }
830         negOne := big.NewInt(-1)
831         one := big.NewInt(1)
832         two := big.NewInt(2)
833         for i, test := range tests {
834                 want, _ := new(big.Int).SetString(test, 16)
835                 nafPos, nafNeg := NAF(want.Bytes())
836                 got := big.NewInt(0)
837                 // Check that the NAF representation comes up with the right number
838                 for i := 0; i < len(nafPos); i++ {
839                         bytePos := nafPos[i]
840                         byteNeg := nafNeg[i]
841                         for j := 7; j >= 0; j-- {
842                                 got.Mul(got, two)
843                                 if bytePos&0x80 == 0x80 {
844                                         got.Add(got, one)
845                                 } else if byteNeg&0x80 == 0x80 {
846                                         got.Add(got, negOne)
847                                 }
848                                 bytePos <<= 1
849                                 byteNeg <<= 1
850                         }
851                 }
852                 if got.Cmp(want) != 0 {
853                         t.Errorf("%d: Failed NAF got %X want %X", i, got, want)
854                 }
855         }
856 }
857
858 func TestNAFRand(t *testing.T) {
859         negOne := big.NewInt(-1)
860         one := big.NewInt(1)
861         two := big.NewInt(2)
862         for i := 0; i < 1024; i++ {
863                 data := make([]byte, 32)
864                 _, err := rand.Read(data)
865                 if err != nil {
866                         t.Fatalf("failed to read random data at %d", i)
867                         break
868                 }
869                 nafPos, nafNeg := NAF(data)
870                 want := new(big.Int).SetBytes(data)
871                 got := big.NewInt(0)
872                 // Check that the NAF representation comes up with the right number
873                 for i := 0; i < len(nafPos); i++ {
874                         bytePos := nafPos[i]
875                         byteNeg := nafNeg[i]
876                         for j := 7; j >= 0; j-- {
877                                 got.Mul(got, two)
878                                 if bytePos&0x80 == 0x80 {
879                                         got.Add(got, one)
880                                 } else if byteNeg&0x80 == 0x80 {
881                                         got.Add(got, negOne)
882                                 }
883                                 bytePos <<= 1
884                                 byteNeg <<= 1
885                         }
886                 }
887                 if got.Cmp(want) != 0 {
888                         t.Errorf("%d: Failed NAF got %X want %X", i, got, want)
889                 }
890         }
891 }
892
893 // These test vectors were taken from
894 //   http://csrc.nist.gov/groups/STM/cavp/documents/dss/ecdsatestvectors.zip
895 var testVectors = []struct {
896         msg    string
897         Qx, Qy string
898         r, s   string
899         ok     bool
900 }{
901 /*
902  * All of these tests are disabled since they are for P224, not sec256k1.
903  * they are left here as an example of test vectors for when some *real*
904  * vectors may be found.
905  * - oga@conformal.com
906         {
907                 "09626b45493672e48f3d1226a3aff3201960e577d33a7f72c7eb055302db8fe8ed61685dd036b554942a5737cd1512cdf811ee0c00e6dd2f08c69f08643be396e85dafda664801e772cdb7396868ac47b172245b41986aa2648cb77fbbfa562581be06651355a0c4b090f9d17d8f0ab6cced4e0c9d386cf465a516630f0231bd",
908                 "9504b5b82d97a264d8b3735e0568decabc4b6ca275bc53cbadfc1c40",
909                 "03426f80e477603b10dee670939623e3da91a94267fc4e51726009ed",
910                 "81d3ac609f9575d742028dd496450a58a60eea2dcf8b9842994916e1",
911                 "96a8c5f382c992e8f30ccce9af120b067ec1d74678fa8445232f75a5",
912                 false,
913         },
914         {
915                 "96b2b6536f6df29be8567a72528aceeaccbaa66c66c534f3868ca9778b02faadb182e4ed34662e73b9d52ecbe9dc8e875fc05033c493108b380689ebf47e5b062e6a0cdb3dd34ce5fe347d92768d72f7b9b377c20aea927043b509c078ed2467d7113405d2ddd458811e6faf41c403a2a239240180f1430a6f4330df5d77de37",
916                 "851e3100368a22478a0029353045ae40d1d8202ef4d6533cfdddafd8",
917                 "205302ac69457dd345e86465afa72ee8c74ca97e2b0b999aec1f10c2",
918                 "4450c2d38b697e990721aa2dbb56578d32b4f5aeb3b9072baa955ee0",
919                 "e26d4b589166f7b4ba4b1c8fce823fa47aad22f8c9c396b8c6526e12",
920                 false,
921         },
922         {
923                 "86778dbb4a068a01047a8d245d632f636c11d2ad350740b36fad90428b454ad0f120cb558d12ea5c8a23db595d87543d06d1ef489263d01ee529871eb68737efdb8ff85bc7787b61514bed85b7e01d6be209e0a4eb0db5c8df58a5c5bf706d76cb2bdf7800208639e05b89517155d11688236e6a47ed37d8e5a2b1e0adea338e",
924                 "ad5bda09d319a717c1721acd6688d17020b31b47eef1edea57ceeffc",
925                 "c8ce98e181770a7c9418c73c63d01494b8b80a41098c5ea50692c984",
926                 "de5558c257ab4134e52c19d8db3b224a1899cbd08cc508ce8721d5e9",
927                 "745db7af5a477e5046705c0a5eff1f52cb94a79d481f0c5a5e108ecd",
928                 true,
929         },
930         {
931                 "4bc6ef1958556686dab1e39c3700054a304cbd8f5928603dcd97fafd1f29e69394679b638f71c9344ce6a535d104803d22119f57b5f9477e253817a52afa9bfbc9811d6cc8c8be6b6566c6ef48b439bbb532abe30627548c598867f3861ba0b154dc1c3deca06eb28df8efd28258554b5179883a36fbb1eecf4f93ee19d41e3d",
932                 "cc5eea2edf964018bdc0504a3793e4d2145142caa09a72ac5fb8d3e8",
933                 "a48d78ae5d08aa725342773975a00d4219cf7a8029bb8cf3c17c374a",
934                 "67b861344b4e416d4094472faf4272f6d54a497177fbc5f9ef292836",
935                 "1d54f3fcdad795bf3b23408ecbac3e1321d1d66f2e4e3d05f41f7020",
936                 false,
937         },
938         {
939                 "bb658732acbf3147729959eb7318a2058308b2739ec58907dd5b11cfa3ecf69a1752b7b7d806fe00ec402d18f96039f0b78dbb90a59c4414fb33f1f4e02e4089de4122cd93df5263a95be4d7084e2126493892816e6a5b4ed123cb705bf930c8f67af0fb4514d5769232a9b008a803af225160ce63f675bd4872c4c97b146e5e",
940                 "6234c936e27bf141fc7534bfc0a7eedc657f91308203f1dcbd642855",
941                 "27983d87ca785ef4892c3591ef4a944b1deb125dd58bd351034a6f84",
942                 "e94e05b42d01d0b965ffdd6c3a97a36a771e8ea71003de76c4ecb13f",
943                 "1dc6464ffeefbd7872a081a5926e9fc3e66d123f1784340ba17737e9",
944                 false,
945         },
946         {
947                 "7c00be9123bfa2c4290be1d8bc2942c7f897d9a5b7917e3aabd97ef1aab890f148400a89abd554d19bec9d8ed911ce57b22fbcf6d30ca2115f13ce0a3f569a23bad39ee645f624c49c60dcfc11e7d2be24de9c905596d8f23624d63dc46591d1f740e46f982bfae453f107e80db23545782be23ce43708245896fc54e1ee5c43",
948                 "9f3f037282aaf14d4772edffff331bbdda845c3f65780498cde334f1",
949                 "8308ee5a16e3bcb721b6bc30000a0419bc1aaedd761be7f658334066",
950                 "6381d7804a8808e3c17901e4d283b89449096a8fba993388fa11dc54",
951                 "8e858f6b5b253686a86b757bad23658cda53115ac565abca4e3d9f57",
952                 false,
953         },
954         {
955                 "cffc122a44840dc705bb37130069921be313d8bde0b66201aebc48add028ca131914ef2e705d6bedd19dc6cf9459bbb0f27cdfe3c50483808ffcdaffbeaa5f062e097180f07a40ef4ab6ed03fe07ed6bcfb8afeb42c97eafa2e8a8df469de07317c5e1494c41547478eff4d8c7d9f0f484ad90fedf6e1c35ee68fa73f1691601",
956                 "a03b88a10d930002c7b17ca6af2fd3e88fa000edf787dc594f8d4fd4",
957                 "e0cf7acd6ddc758e64847fe4df9915ebda2f67cdd5ec979aa57421f5",
958                 "387b84dcf37dc343c7d2c5beb82f0bf8bd894b395a7b894565d296c1",
959                 "4adc12ce7d20a89ce3925e10491c731b15ddb3f339610857a21b53b4",
960                 false,
961         },
962         {
963                 "26e0e0cafd85b43d16255908ccfd1f061c680df75aba3081246b337495783052ba06c60f4a486c1591a4048bae11b4d7fec4f161d80bdc9a7b79d23e44433ed625eab280521a37f23dd3e1bdc5c6a6cfaa026f3c45cf703e76dab57add93fe844dd4cda67dc3bddd01f9152579e49df60969b10f09ce9372fdd806b0c7301866",
964                 "9a8983c42f2b5a87c37a00458b5970320d247f0c8a88536440173f7d",
965                 "15e489ec6355351361900299088cfe8359f04fe0cab78dde952be80c",
966                 "929a21baa173d438ec9f28d6a585a2f9abcfc0a4300898668e476dc0",
967                 "59a853f046da8318de77ff43f26fe95a92ee296fa3f7e56ce086c872",
968                 true,
969         },
970         {
971                 "1078eac124f48ae4f807e946971d0de3db3748dd349b14cca5c942560fb25401b2252744f18ad5e455d2d97ed5ae745f55ff509c6c8e64606afe17809affa855c4c4cdcaf6b69ab4846aa5624ed0687541aee6f2224d929685736c6a23906d974d3c257abce1a3fb8db5951b89ecb0cda92b5207d93f6618fd0f893c32cf6a6e",
972                 "d6e55820bb62c2be97650302d59d667a411956138306bd566e5c3c2b",
973                 "631ab0d64eaf28a71b9cbd27a7a88682a2167cee6251c44e3810894f",
974                 "65af72bc7721eb71c2298a0eb4eed3cec96a737cc49125706308b129",
975                 "bd5a987c78e2d51598dbd9c34a9035b0069c580edefdacee17ad892a",
976                 false,
977         },
978         {
979                 "919deb1fdd831c23481dfdb2475dcbe325b04c34f82561ced3d2df0b3d749b36e255c4928973769d46de8b95f162b53cd666cad9ae145e7fcfba97919f703d864efc11eac5f260a5d920d780c52899e5d76f8fe66936ff82130761231f536e6a3d59792f784902c469aa897aabf9a0678f93446610d56d5e0981e4c8a563556b",
980                 "269b455b1024eb92d860a420f143ac1286b8cce43031562ae7664574",
981                 "baeb6ca274a77c44a0247e5eb12ca72bdd9a698b3f3ae69c9f1aaa57",
982                 "cb4ec2160f04613eb0dfe4608486091a25eb12aa4dec1afe91cfb008",
983                 "40b01d8cd06589481574f958b98ca08ade9d2a8fe31024375c01bb40",
984                 false,
985         },
986         {
987                 "6e012361250dacf6166d2dd1aa7be544c3206a9d43464b3fcd90f3f8cf48d08ec099b59ba6fe7d9bdcfaf244120aed1695d8be32d1b1cd6f143982ab945d635fb48a7c76831c0460851a3d62b7209c30cd9c2abdbe3d2a5282a9fcde1a6f418dd23c409bc351896b9b34d7d3a1a63bbaf3d677e612d4a80fa14829386a64b33f",
988                 "6d2d695efc6b43b13c14111f2109608f1020e3e03b5e21cfdbc82fcd",
989                 "26a4859296b7e360b69cf40be7bd97ceaffa3d07743c8489fc47ca1b",
990                 "9a8cb5f2fdc288b7183c5b32d8e546fc2ed1ca4285eeae00c8b572ad",
991                 "8c623f357b5d0057b10cdb1a1593dab57cda7bdec9cf868157a79b97",
992                 true,
993         },
994         {
995                 "bf6bd7356a52b234fe24d25557200971fc803836f6fec3cade9642b13a8e7af10ab48b749de76aada9d8927f9b12f75a2c383ca7358e2566c4bb4f156fce1fd4e87ef8c8d2b6b1bdd351460feb22cdca0437ac10ca5e0abbbce9834483af20e4835386f8b1c96daaa41554ceee56730aac04f23a5c765812efa746051f396566",
996                 "14250131b2599939cf2d6bc491be80ddfe7ad9de644387ee67de2d40",
997                 "b5dc473b5d014cd504022043c475d3f93c319a8bdcb7262d9e741803",
998                 "4f21642f2201278a95339a80f75cc91f8321fcb3c9462562f6cbf145",
999                 "452a5f816ea1f75dee4fd514fa91a0d6a43622981966c59a1b371ff8",
1000                 false,
1001         },
1002         {
1003                 "0eb7f4032f90f0bd3cf9473d6d9525d264d14c031a10acd31a053443ed5fe919d5ac35e0be77813071b4062f0b5fdf58ad5f637b76b0b305aec18f82441b6e607b44cdf6e0e3c7c57f24e6fd565e39430af4a6b1d979821ed0175fa03e3125506847654d7e1ae904ce1190ae38dc5919e257bdac2db142a6e7cd4da6c2e83770",
1004                 "d1f342b7790a1667370a1840255ac5bbbdc66f0bc00ae977d99260ac",
1005                 "76416cabae2de9a1000b4646338b774baabfa3db4673790771220cdb",
1006                 "bc85e3fc143d19a7271b2f9e1c04b86146073f3fab4dda1c3b1f35ca",
1007                 "9a5c70ede3c48d5f43307a0c2a4871934424a3303b815df4bb0f128e",
1008                 false,
1009         },
1010         {
1011                 "5cc25348a05d85e56d4b03cec450128727bc537c66ec3a9fb613c151033b5e86878632249cba83adcefc6c1e35dcd31702929c3b57871cda5c18d1cf8f9650a25b917efaed56032e43b6fc398509f0d2997306d8f26675f3a8683b79ce17128e006aa0903b39eeb2f1001be65de0520115e6f919de902b32c38d691a69c58c92",
1012                 "7e49a7abf16a792e4c7bbc4d251820a2abd22d9f2fc252a7bf59c9a6",
1013                 "44236a8fb4791c228c26637c28ae59503a2f450d4cfb0dc42aa843b9",
1014                 "084461b4050285a1a85b2113be76a17878d849e6bc489f4d84f15cd8",
1015                 "079b5bddcc4d45de8dbdfd39f69817c7e5afa454a894d03ee1eaaac3",
1016                 false,
1017         },
1018         {
1019                 "1951533ce33afb58935e39e363d8497a8dd0442018fd96dff167b3b23d7206a3ee182a3194765df4768a3284e23b8696c199b4686e670d60c9d782f08794a4bccc05cffffbd1a12acd9eb1cfa01f7ebe124da66ecff4599ea7720c3be4bb7285daa1a86ebf53b042bd23208d468c1b3aa87381f8e1ad63e2b4c2ba5efcf05845",
1020                 "31945d12ebaf4d81f02be2b1768ed80784bf35cf5e2ff53438c11493",
1021                 "a62bebffac987e3b9d3ec451eb64c462cdf7b4aa0b1bbb131ceaa0a4",
1022                 "bc3c32b19e42b710bca5c6aaa128564da3ddb2726b25f33603d2af3c",
1023                 "ed1a719cc0c507edc5239d76fe50e2306c145ad252bd481da04180c0",
1024                 false,
1025         },
1026 */
1027 }
1028
1029 func TestVectors(t *testing.T) {
1030         sha := sha1.New()
1031
1032         for i, test := range testVectors {
1033                 pub := PublicKey{
1034                         Curve: S256(),
1035                         X:     fromHex(test.Qx),
1036                         Y:     fromHex(test.Qy),
1037                 }
1038                 msg, _ := hex.DecodeString(test.msg)
1039                 sha.Reset()
1040                 sha.Write(msg)
1041                 hashed := sha.Sum(nil)
1042                 sig := Signature{R: fromHex(test.r), S: fromHex(test.s)}
1043                 if verified := sig.Verify(hashed, &pub); verified != test.ok {
1044                         t.Errorf("%d: bad result %v instead of %v", i, verified,
1045                                 test.ok)
1046                 }
1047                 if testing.Short() {
1048                         break
1049                 }
1050         }
1051 }