OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / vendor / github.com / btcsuite / btcd / btcec / field_test.go
1 // Copyright (c) 2013-2016 The btcsuite developers
2 // Copyright (c) 2013-2016 Dave Collins
3 // Use of this source code is governed by an ISC
4 // license that can be found in the LICENSE file.
5
6 package btcec
7
8 import (
9         "reflect"
10         "testing"
11 )
12
13 // TestSetInt ensures that setting a field value to various native integers
14 // works as expected.
15 func TestSetInt(t *testing.T) {
16         tests := []struct {
17                 in  uint
18                 raw [10]uint32
19         }{
20                 {5, [10]uint32{5, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
21                 // 2^26
22                 {67108864, [10]uint32{67108864, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
23                 // 2^26 + 1
24                 {67108865, [10]uint32{67108865, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
25                 // 2^32 - 1
26                 {4294967295, [10]uint32{4294967295, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
27         }
28
29         t.Logf("Running %d tests", len(tests))
30         for i, test := range tests {
31                 f := new(fieldVal).SetInt(test.in)
32                 if !reflect.DeepEqual(f.n, test.raw) {
33                         t.Errorf("fieldVal.Set #%d wrong result\ngot: %v\n"+
34                                 "want: %v", i, f.n, test.raw)
35                         continue
36                 }
37         }
38 }
39
40 // TestZero ensures that zeroing a field value zero works as expected.
41 func TestZero(t *testing.T) {
42         f := new(fieldVal).SetInt(2)
43         f.Zero()
44         for idx, rawInt := range f.n {
45                 if rawInt != 0 {
46                         t.Errorf("internal field integer at index #%d is not "+
47                                 "zero - got %d", idx, rawInt)
48                 }
49         }
50 }
51
52 // TestIsZero ensures that checking if a field IsZero works as expected.
53 func TestIsZero(t *testing.T) {
54         f := new(fieldVal)
55         if !f.IsZero() {
56                 t.Errorf("new field value is not zero - got %v (rawints %x)", f,
57                         f.n)
58         }
59
60         f.SetInt(1)
61         if f.IsZero() {
62                 t.Errorf("field claims it's zero when it's not - got %v "+
63                         "(raw rawints %x)", f, f.n)
64         }
65
66         f.Zero()
67         if !f.IsZero() {
68                 t.Errorf("field claims it's not zero when it is - got %v "+
69                         "(raw rawints %x)", f, f.n)
70         }
71 }
72
73 // TestStringer ensures the stringer returns the appropriate hex string.
74 func TestStringer(t *testing.T) {
75         tests := []struct {
76                 in       string
77                 expected string
78         }{
79                 {"0", "0000000000000000000000000000000000000000000000000000000000000000"},
80                 {"1", "0000000000000000000000000000000000000000000000000000000000000001"},
81                 {"a", "000000000000000000000000000000000000000000000000000000000000000a"},
82                 {"b", "000000000000000000000000000000000000000000000000000000000000000b"},
83                 {"c", "000000000000000000000000000000000000000000000000000000000000000c"},
84                 {"d", "000000000000000000000000000000000000000000000000000000000000000d"},
85                 {"e", "000000000000000000000000000000000000000000000000000000000000000e"},
86                 {"f", "000000000000000000000000000000000000000000000000000000000000000f"},
87                 {"f0", "00000000000000000000000000000000000000000000000000000000000000f0"},
88                 // 2^26-1
89                 {
90                         "3ffffff",
91                         "0000000000000000000000000000000000000000000000000000000003ffffff",
92                 },
93                 // 2^32-1
94                 {
95                         "ffffffff",
96                         "00000000000000000000000000000000000000000000000000000000ffffffff",
97                 },
98                 // 2^64-1
99                 {
100                         "ffffffffffffffff",
101                         "000000000000000000000000000000000000000000000000ffffffffffffffff",
102                 },
103                 // 2^96-1
104                 {
105                         "ffffffffffffffffffffffff",
106                         "0000000000000000000000000000000000000000ffffffffffffffffffffffff",
107                 },
108                 // 2^128-1
109                 {
110                         "ffffffffffffffffffffffffffffffff",
111                         "00000000000000000000000000000000ffffffffffffffffffffffffffffffff",
112                 },
113                 // 2^160-1
114                 {
115                         "ffffffffffffffffffffffffffffffffffffffff",
116                         "000000000000000000000000ffffffffffffffffffffffffffffffffffffffff",
117                 },
118                 // 2^192-1
119                 {
120                         "ffffffffffffffffffffffffffffffffffffffffffffffff",
121                         "0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff",
122                 },
123                 // 2^224-1
124                 {
125                         "ffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
126                         "00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
127                 },
128                 // 2^256-4294968273 (the btcec prime, so should result in 0)
129                 {
130                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
131                         "0000000000000000000000000000000000000000000000000000000000000000",
132                 },
133                 // 2^256-4294968274 (the secp256k1 prime+1, so should result in 1)
134                 {
135                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc30",
136                         "0000000000000000000000000000000000000000000000000000000000000001",
137                 },
138
139                 // Invalid hex
140                 {"g", "0000000000000000000000000000000000000000000000000000000000000000"},
141                 {"1h", "0000000000000000000000000000000000000000000000000000000000000000"},
142                 {"i1", "0000000000000000000000000000000000000000000000000000000000000000"},
143         }
144
145         t.Logf("Running %d tests", len(tests))
146         for i, test := range tests {
147                 f := new(fieldVal).SetHex(test.in)
148                 result := f.String()
149                 if result != test.expected {
150                         t.Errorf("fieldVal.String #%d wrong result\ngot: %v\n"+
151                                 "want: %v", i, result, test.expected)
152                         continue
153                 }
154         }
155 }
156
157 // TestNormalize ensures that normalizing the internal field words works as
158 // expected.
159 func TestNormalize(t *testing.T) {
160         tests := []struct {
161                 raw        [10]uint32 // Intentionally denormalized value
162                 normalized [10]uint32 // Normalized form of the raw value
163         }{
164                 {
165                         [10]uint32{0x00000005, 0, 0, 0, 0, 0, 0, 0, 0, 0},
166                         [10]uint32{0x00000005, 0, 0, 0, 0, 0, 0, 0, 0, 0},
167                 },
168                 // 2^26
169                 {
170                         [10]uint32{0x04000000, 0x0, 0, 0, 0, 0, 0, 0, 0, 0},
171                         [10]uint32{0x00000000, 0x1, 0, 0, 0, 0, 0, 0, 0, 0},
172                 },
173                 // 2^26 + 1
174                 {
175                         [10]uint32{0x04000001, 0x0, 0, 0, 0, 0, 0, 0, 0, 0},
176                         [10]uint32{0x00000001, 0x1, 0, 0, 0, 0, 0, 0, 0, 0},
177                 },
178                 // 2^32 - 1
179                 {
180                         [10]uint32{0xffffffff, 0x00, 0, 0, 0, 0, 0, 0, 0, 0},
181                         [10]uint32{0x03ffffff, 0x3f, 0, 0, 0, 0, 0, 0, 0, 0},
182                 },
183                 // 2^32
184                 {
185                         [10]uint32{0x04000000, 0x3f, 0, 0, 0, 0, 0, 0, 0, 0},
186                         [10]uint32{0x00000000, 0x40, 0, 0, 0, 0, 0, 0, 0, 0},
187                 },
188                 // 2^32 + 1
189                 {
190                         [10]uint32{0x04000001, 0x3f, 0, 0, 0, 0, 0, 0, 0, 0},
191                         [10]uint32{0x00000001, 0x40, 0, 0, 0, 0, 0, 0, 0, 0},
192                 },
193                 // 2^64 - 1
194                 {
195                         [10]uint32{0xffffffff, 0xffffffc0, 0xfc0, 0, 0, 0, 0, 0, 0, 0},
196                         [10]uint32{0x03ffffff, 0x03ffffff, 0xfff, 0, 0, 0, 0, 0, 0, 0},
197                 },
198                 // 2^64
199                 {
200                         [10]uint32{0x04000000, 0x03ffffff, 0x0fff, 0, 0, 0, 0, 0, 0, 0},
201                         [10]uint32{0x00000000, 0x00000000, 0x1000, 0, 0, 0, 0, 0, 0, 0},
202                 },
203                 // 2^64 + 1
204                 {
205                         [10]uint32{0x04000001, 0x03ffffff, 0x0fff, 0, 0, 0, 0, 0, 0, 0},
206                         [10]uint32{0x00000001, 0x00000000, 0x1000, 0, 0, 0, 0, 0, 0, 0},
207                 },
208                 // 2^96 - 1
209                 {
210                         [10]uint32{0xffffffff, 0xffffffc0, 0xffffffc0, 0x3ffc0, 0, 0, 0, 0, 0, 0},
211                         [10]uint32{0x03ffffff, 0x03ffffff, 0x03ffffff, 0x3ffff, 0, 0, 0, 0, 0, 0},
212                 },
213                 // 2^96
214                 {
215                         [10]uint32{0x04000000, 0x03ffffff, 0x03ffffff, 0x3ffff, 0, 0, 0, 0, 0, 0},
216                         [10]uint32{0x00000000, 0x00000000, 0x00000000, 0x40000, 0, 0, 0, 0, 0, 0},
217                 },
218                 // 2^128 - 1
219                 {
220                         [10]uint32{0xffffffff, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffc0, 0, 0, 0, 0, 0},
221                         [10]uint32{0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0xffffff, 0, 0, 0, 0, 0},
222                 },
223                 // 2^128
224                 {
225                         [10]uint32{0x04000000, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x0ffffff, 0, 0, 0, 0, 0},
226                         [10]uint32{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x1000000, 0, 0, 0, 0, 0},
227                 },
228                 // 2^256 - 4294968273 (secp256k1 prime)
229                 {
230                         [10]uint32{0xfffffc2f, 0xffffff80, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0x3fffc0},
231                         [10]uint32{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000000},
232                 },
233                 // Prime larger than P where both first and second words are larger
234                 // than P's first and second words
235                 {
236                         [10]uint32{0xfffffc30, 0xffffff86, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0x3fffc0},
237                         [10]uint32{0x00000001, 0x00000006, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000000},
238                 },
239                 // Prime larger than P where only the second word is larger
240                 // than P's second words.
241                 {
242                         [10]uint32{0xfffffc2a, 0xffffff87, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0x3fffc0},
243                         [10]uint32{0x03fffffb, 0x00000006, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000000},
244                 },
245                 // 2^256 - 1
246                 {
247                         [10]uint32{0xffffffff, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0xffffffc0, 0x3fffc0},
248                         [10]uint32{0x000003d0, 0x00000040, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000000},
249                 },
250                 // Prime with field representation such that the initial
251                 // reduction does not result in a carry to bit 256.
252                 //
253                 // 2^256 - 4294968273 (secp256k1 prime)
254                 {
255                         [10]uint32{0x03fffc2f, 0x03ffffbf, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x003fffff},
256                         [10]uint32{0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
257                 },
258                 // Prime larger than P that reduces to a value which is still
259                 // larger than P when it has a magnitude of 1 due to its first
260                 // word and does not result in a carry to bit 256.
261                 //
262                 // 2^256 - 4294968272 (secp256k1 prime + 1)
263                 {
264                         [10]uint32{0x03fffc30, 0x03ffffbf, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x003fffff},
265                         [10]uint32{0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
266                 },
267                 // Prime larger than P that reduces to a value which is still
268                 // larger than P when it has a magnitude of 1 due to its second
269                 // word and does not result in a carry to bit 256.
270                 //
271                 // 2^256 - 4227859409 (secp256k1 prime + 0x4000000)
272                 {
273                         [10]uint32{0x03fffc2f, 0x03ffffc0, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x003fffff},
274                         [10]uint32{0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
275                 },
276                 // Prime larger than P that reduces to a value which is still
277                 // larger than P when it has a magnitude of 1 due to a carry to
278                 // bit 256, but would not be without the carry.  These values
279                 // come from the fact that P is 2^256 - 4294968273 and 977 is
280                 // the low order word in the internal field representation.
281                 //
282                 // 2^256 * 5 - ((4294968273 - (977+1)) * 4)
283                 {
284                         [10]uint32{0x03ffffff, 0x03fffeff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x0013fffff},
285                         [10]uint32{0x00001314, 0x00000040, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000000000},
286                 },
287                 // Prime larger than P that reduces to a value which is still
288                 // larger than P when it has a magnitude of 1 due to both a
289                 // carry to bit 256 and the first word.
290                 {
291                         [10]uint32{0x03fffc30, 0x03ffffbf, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x07ffffff, 0x003fffff},
292                         [10]uint32{0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001},
293                 },
294                 // Prime larger than P that reduces to a value which is still
295                 // larger than P when it has a magnitude of 1 due to both a
296                 // carry to bit 256 and the second word.
297                 //
298                 {
299                         [10]uint32{0x03fffc2f, 0x03ffffc0, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x3ffffff, 0x07ffffff, 0x003fffff},
300                         [10]uint32{0x00000000, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x0000000, 0x00000000, 0x00000001},
301                 },
302                 // Prime larger than P that reduces to a value which is still
303                 // larger than P when it has a magnitude of 1 due to a carry to
304                 // bit 256 and the first and second words.
305                 //
306                 {
307                         [10]uint32{0x03fffc30, 0x03ffffc0, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x07ffffff, 0x003fffff},
308                         [10]uint32{0x00000001, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001},
309                 },
310         }
311
312         t.Logf("Running %d tests", len(tests))
313         for i, test := range tests {
314                 f := new(fieldVal)
315                 f.n = test.raw
316                 f.Normalize()
317                 if !reflect.DeepEqual(f.n, test.normalized) {
318                         t.Errorf("fieldVal.Normalize #%d wrong result\n"+
319                                 "got: %x\nwant: %x", i, f.n, test.normalized)
320                         continue
321                 }
322         }
323 }
324
325 // TestIsOdd ensures that checking if a field value IsOdd works as expected.
326 func TestIsOdd(t *testing.T) {
327         tests := []struct {
328                 in       string // hex encoded value
329                 expected bool   // expected oddness
330         }{
331                 {"0", false},
332                 {"1", true},
333                 {"2", false},
334                 // 2^32 - 1
335                 {"ffffffff", true},
336                 // 2^64 - 2
337                 {"fffffffffffffffe", false},
338                 // secp256k1 prime
339                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", true},
340         }
341
342         t.Logf("Running %d tests", len(tests))
343         for i, test := range tests {
344                 f := new(fieldVal).SetHex(test.in)
345                 result := f.IsOdd()
346                 if result != test.expected {
347                         t.Errorf("fieldVal.IsOdd #%d wrong result\n"+
348                                 "got: %v\nwant: %v", i, result, test.expected)
349                         continue
350                 }
351         }
352 }
353
354 // TestEquals ensures that checking two field values for equality via Equals
355 // works as expected.
356 func TestEquals(t *testing.T) {
357         tests := []struct {
358                 in1      string // hex encoded value
359                 in2      string // hex encoded value
360                 expected bool   // expected equality
361         }{
362                 {"0", "0", true},
363                 {"0", "1", false},
364                 {"1", "0", false},
365                 // 2^32 - 1 == 2^32 - 1?
366                 {"ffffffff", "ffffffff", true},
367                 // 2^64 - 1 == 2^64 - 2?
368                 {"ffffffffffffffff", "fffffffffffffffe", false},
369                 // 0 == prime (mod prime)?
370                 {"0", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", true},
371                 // 1 == prime+1 (mod prime)?
372                 {"1", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc30", true},
373         }
374
375         t.Logf("Running %d tests", len(tests))
376         for i, test := range tests {
377                 f := new(fieldVal).SetHex(test.in1).Normalize()
378                 f2 := new(fieldVal).SetHex(test.in2).Normalize()
379                 result := f.Equals(f2)
380                 if result != test.expected {
381                         t.Errorf("fieldVal.Equals #%d wrong result\n"+
382                                 "got: %v\nwant: %v", i, result, test.expected)
383                         continue
384                 }
385         }
386 }
387
388 // TestNegate ensures that negating field values via Negate works as expected.
389 func TestNegate(t *testing.T) {
390         tests := []struct {
391                 in       string // hex encoded value
392                 expected string // expected hex encoded value
393         }{
394                 // secp256k1 prime (aka 0)
395                 {"0", "0"},
396                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "0"},
397                 {"0", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"},
398                 // secp256k1 prime-1
399                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", "1"},
400                 {"1", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e"},
401                 // secp256k1 prime-2
402                 {"2", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d"},
403                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d", "2"},
404                 // Random sampling
405                 {
406                         "b3d9aac9c5e43910b4385b53c7e78c21d4cd5f8e683c633aed04c233efc2e120",
407                         "4c2655363a1bc6ef4bc7a4ac381873de2b32a07197c39cc512fb3dcb103d1b0f",
408                 },
409                 {
410                         "f8a85984fee5a12a7c8dd08830d83423c937d77c379e4a958e447a25f407733f",
411                         "757a67b011a5ed583722f77cf27cbdc36c82883c861b56a71bb85d90bf888f0",
412                 },
413                 {
414                         "45ee6142a7fda884211e93352ed6cb2807800e419533be723a9548823ece8312",
415                         "ba119ebd5802577bdee16ccad12934d7f87ff1be6acc418dc56ab77cc131791d",
416                 },
417                 {
418                         "53c2a668f07e411a2e473e1c3b6dcb495dec1227af27673761d44afe5b43d22b",
419                         "ac3d59970f81bee5d1b8c1e3c49234b6a213edd850d898c89e2bb500a4bc2a04",
420                 },
421         }
422
423         t.Logf("Running %d tests", len(tests))
424         for i, test := range tests {
425                 f := new(fieldVal).SetHex(test.in).Normalize()
426                 expected := new(fieldVal).SetHex(test.expected).Normalize()
427                 result := f.Negate(1).Normalize()
428                 if !result.Equals(expected) {
429                         t.Errorf("fieldVal.Negate #%d wrong result\n"+
430                                 "got: %v\nwant: %v", i, result, expected)
431                         continue
432                 }
433         }
434 }
435
436 // TestAddInt ensures that adding an integer to field values via AddInt works as
437 // expected.
438 func TestAddInt(t *testing.T) {
439         tests := []struct {
440                 in1      string // hex encoded value
441                 in2      uint   // unsigned integer to add to the value above
442                 expected string // expected hex encoded value
443         }{
444                 {"0", 1, "1"},
445                 {"1", 0, "1"},
446                 {"1", 1, "2"},
447                 // secp256k1 prime-1 + 1
448                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", 1, "0"},
449                 // secp256k1 prime + 1
450                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", 1, "1"},
451                 // Random samples.
452                 {
453                         "ff95ad9315aff04ab4af0ce673620c7145dc85d03bab5ba4b09ca2c4dec2d6c1",
454                         0x10f,
455                         "ff95ad9315aff04ab4af0ce673620c7145dc85d03bab5ba4b09ca2c4dec2d7d0",
456                 },
457                 {
458                         "44bdae6b772e7987941f1ba314e6a5b7804a4c12c00961b57d20f41deea9cecf",
459                         0x2cf11d41,
460                         "44bdae6b772e7987941f1ba314e6a5b7804a4c12c00961b57d20f41e1b9aec10",
461                 },
462                 {
463                         "88c3ecae67b591935fb1f6a9499c35315ffad766adca665c50b55f7105122c9c",
464                         0x4829aa2d,
465                         "88c3ecae67b591935fb1f6a9499c35315ffad766adca665c50b55f714d3bd6c9",
466                 },
467                 {
468                         "8523e9edf360ca32a95aae4e57fcde5a542b471d08a974d94ea0ee09a015e2a6",
469                         0xa21265a5,
470                         "8523e9edf360ca32a95aae4e57fcde5a542b471d08a974d94ea0ee0a4228484b",
471                 },
472         }
473
474         t.Logf("Running %d tests", len(tests))
475         for i, test := range tests {
476                 f := new(fieldVal).SetHex(test.in1).Normalize()
477                 expected := new(fieldVal).SetHex(test.expected).Normalize()
478                 result := f.AddInt(test.in2).Normalize()
479                 if !result.Equals(expected) {
480                         t.Errorf("fieldVal.AddInt #%d wrong result\n"+
481                                 "got: %v\nwant: %v", i, result, expected)
482                         continue
483                 }
484         }
485 }
486
487 // TestAdd ensures that adding two field values together via Add works as
488 // expected.
489 func TestAdd(t *testing.T) {
490         tests := []struct {
491                 in1      string // first hex encoded value
492                 in2      string // second hex encoded value to add
493                 expected string // expected hex encoded value
494         }{
495                 {"0", "1", "1"},
496                 {"1", "0", "1"},
497                 {"1", "1", "2"},
498                 // secp256k1 prime-1 + 1
499                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", "1", "0"},
500                 // secp256k1 prime + 1
501                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "1", "1"},
502                 // Random samples.
503                 {
504                         "2b2012f975404e5065b4292fb8bed0a5d315eacf24c74d8b27e73bcc5430edcc",
505                         "2c3cefa4e4753e8aeec6ac4c12d99da4d78accefda3b7885d4c6bab46c86db92",
506                         "575d029e59b58cdb547ad57bcb986e4aaaa0b7beff02c610fcadf680c0b7c95e",
507                 },
508                 {
509                         "8131e8722fe59bb189692b96c9f38de92885730f1dd39ab025daffb94c97f79c",
510                         "ff5454b765f0aab5f0977dcc629becc84cabeb9def48e79c6aadb2622c490fa9",
511                         "80863d2995d646677a00a9632c8f7ab175315ead0d1c824c9088b21c78e10b16",
512                 },
513                 {
514                         "c7c95e93d0892b2b2cdd77e80eb646ea61be7a30ac7e097e9f843af73fad5c22",
515                         "3afe6f91a74dfc1c7f15c34907ee981656c37236d946767dd53ccad9190e437c",
516                         "02c7ce2577d72747abf33b3116a4df00b881ec6785c47ffc74c105d158bba36f",
517                 },
518                 {
519                         "fd1c26f6a23381e5d785ba889494ec059369b888ad8431cd67d8c934b580dbe1",
520                         "a475aa5a31dcca90ef5b53c097d9133d6b7117474b41e7877bb199590fc0489c",
521                         "a191d150d4104c76c6e10e492c6dff42fedacfcff8c61954e38a628ec541284e",
522                 },
523         }
524
525         t.Logf("Running %d tests", len(tests))
526         for i, test := range tests {
527                 f := new(fieldVal).SetHex(test.in1).Normalize()
528                 f2 := new(fieldVal).SetHex(test.in2).Normalize()
529                 expected := new(fieldVal).SetHex(test.expected).Normalize()
530                 result := f.Add(f2).Normalize()
531                 if !result.Equals(expected) {
532                         t.Errorf("fieldVal.Add #%d wrong result\n"+
533                                 "got: %v\nwant: %v", i, result, expected)
534                         continue
535                 }
536         }
537 }
538
539 // TestAdd2 ensures that adding two field values together via Add2 works as
540 // expected.
541 func TestAdd2(t *testing.T) {
542         tests := []struct {
543                 in1      string // first hex encoded value
544                 in2      string // second hex encoded value to add
545                 expected string // expected hex encoded value
546         }{
547                 {"0", "1", "1"},
548                 {"1", "0", "1"},
549                 {"1", "1", "2"},
550                 // secp256k1 prime-1 + 1
551                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", "1", "0"},
552                 // secp256k1 prime + 1
553                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "1", "1"},
554                 // close but over the secp256k1 prime
555                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000", "f1ffff000", "1ffff3d1"},
556                 // Random samples.
557                 {
558                         "ad82b8d1cc136e23e9fd77fe2c7db1fe5a2ecbfcbde59ab3529758334f862d28",
559                         "4d6a4e95d6d61f4f46b528bebe152d408fd741157a28f415639347a84f6f574b",
560                         "faed0767a2e98d7330b2a0bcea92df3eea060d12380e8ec8b62a9fdb9ef58473",
561                 },
562                 {
563                         "f3f43a2540054a86e1df98547ec1c0e157b193e5350fb4a3c3ea214b228ac5e7",
564                         "25706572592690ea3ddc951a1b48b504a4c83dc253756e1b96d56fdfb3199522",
565                         "19649f97992bdb711fbc2d6e9a0a75e5fc79d1a7888522bf5abf912bd5a45eda",
566                 },
567                 {
568                         "6915bb94eef13ff1bb9b2633d997e13b9b1157c713363cc0e891416d6734f5b8",
569                         "11f90d6ac6fe1c4e8900b1c85fb575c251ec31b9bc34b35ada0aea1c21eded22",
570                         "7b0ec8ffb5ef5c40449bd7fc394d56fdecfd8980cf6af01bc29c2b898922e2da",
571                 },
572                 {
573                         "48b0c9eae622eed9335b747968544eb3e75cb2dc8128388f948aa30f88cabde4",
574                         "0989882b52f85f9d524a3a3061a0e01f46d597839d2ba637320f4b9510c8d2d5",
575                         "523a5216391b4e7685a5aea9c9f52ed32e324a601e53dec6c699eea4999390b9",
576                 },
577         }
578
579         t.Logf("Running %d tests", len(tests))
580         for i, test := range tests {
581                 f := new(fieldVal).SetHex(test.in1).Normalize()
582                 f2 := new(fieldVal).SetHex(test.in2).Normalize()
583                 expected := new(fieldVal).SetHex(test.expected).Normalize()
584                 result := f.Add2(f, f2).Normalize()
585                 if !result.Equals(expected) {
586                         t.Errorf("fieldVal.Add2 #%d wrong result\n"+
587                                 "got: %v\nwant: %v", i, result, expected)
588                         continue
589                 }
590         }
591 }
592
593 // TestMulInt ensures that adding an integer to field values via MulInt works as
594 // expected.
595 func TestMulInt(t *testing.T) {
596         tests := []struct {
597                 in1      string // hex encoded value
598                 in2      uint   // unsigned integer to multiply with value above
599                 expected string // expected hex encoded value
600         }{
601                 {"0", 0, "0"},
602                 {"1", 0, "0"},
603                 {"0", 1, "0"},
604                 {"1", 1, "1"},
605                 // secp256k1 prime-1 * 2
606                 {
607                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
608                         2,
609                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d",
610                 },
611                 // secp256k1 prime * 3
612                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", 3, "0"},
613                 // secp256k1 prime-1 * 8
614                 {
615                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
616                         8,
617                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc27",
618                 },
619                 // Random samples for first value.  The second value is limited
620                 // to 8 since that is the maximum int used in the elliptic curve
621                 // calculations.
622                 {
623                         "b75674dc9180d306c692163ac5e089f7cef166af99645c0c23568ab6d967288a",
624                         6,
625                         "4c06bd2b6904f228a76c8560a3433bced9a8681d985a2848d407404d186b0280",
626                 },
627                 {
628                         "54873298ac2b5ba8591c125ae54931f5ea72040aee07b208d6135476fb5b9c0e",
629                         3,
630                         "fd9597ca048212f90b543710afdb95e1bf560c20ca17161a8239fd64f212d42a",
631                 },
632                 {
633                         "7c30fbd363a74c17e1198f56b090b59bbb6c8755a74927a6cba7a54843506401",
634                         5,
635                         "6cf4eb20f2447c77657fccb172d38c0aa91ea4ac446dc641fa463a6b5091fba7",
636                 },
637                 {
638                         "fb4529be3e027a3d1587d8a500b72f2d312e3577340ef5175f96d113be4c2ceb",
639                         8,
640                         "da294df1f013d1e8ac3ec52805b979698971abb9a077a8bafcb688a4f261820f",
641                 },
642         }
643
644         t.Logf("Running %d tests", len(tests))
645         for i, test := range tests {
646                 f := new(fieldVal).SetHex(test.in1).Normalize()
647                 expected := new(fieldVal).SetHex(test.expected).Normalize()
648                 result := f.MulInt(test.in2).Normalize()
649                 if !result.Equals(expected) {
650                         t.Errorf("fieldVal.MulInt #%d wrong result\n"+
651                                 "got: %v\nwant: %v", i, result, expected)
652                         continue
653                 }
654         }
655 }
656
657 // TestMul ensures that multiplying two field valuess via Mul works as expected.
658 func TestMul(t *testing.T) {
659         tests := []struct {
660                 in1      string // first hex encoded value
661                 in2      string // second hex encoded value to multiply with
662                 expected string // expected hex encoded value
663         }{
664                 {"0", "0", "0"},
665                 {"1", "0", "0"},
666                 {"0", "1", "0"},
667                 {"1", "1", "1"},
668                 // slightly over prime
669                 {
670                         "ffffffffffffffffffffffffffffffffffffffffffffffffffffffff1ffff",
671                         "1000",
672                         "1ffff3d1",
673                 },
674                 // secp256k1 prime-1 * 2
675                 {
676                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
677                         "2",
678                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d",
679                 },
680                 // secp256k1 prime * 3
681                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "3", "0"},
682                 // secp256k1 prime-1 * 8
683                 {
684                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
685                         "8",
686                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc27",
687                 },
688                 // Random samples.
689                 {
690                         "cfb81753d5ef499a98ecc04c62cb7768c2e4f1740032946db1c12e405248137e",
691                         "58f355ad27b4d75fb7db0442452e732c436c1f7c5a7c4e214fa9cc031426a7d3",
692                         "1018cd2d7c2535235b71e18db9cd98027386328d2fa6a14b36ec663c4c87282b",
693                 },
694                 {
695                         "26e9d61d1cdf3920e9928e85fa3df3e7556ef9ab1d14ec56d8b4fc8ed37235bf",
696                         "2dfc4bbe537afee979c644f8c97b31e58be5296d6dbc460091eae630c98511cf",
697                         "da85f48da2dc371e223a1ae63bd30b7e7ee45ae9b189ac43ff357e9ef8cf107a",
698                 },
699                 {
700                         "5db64ed5afb71646c8b231585d5b2bf7e628590154e0854c4c29920b999ff351",
701                         "279cfae5eea5d09ade8e6a7409182f9de40981bc31c84c3d3dfe1d933f152e9a",
702                         "2c78fbae91792dd0b157abe3054920049b1879a7cc9d98cfda927d83be411b37",
703                 },
704                 {
705                         "b66dfc1f96820b07d2bdbd559c19319a3a73c97ceb7b3d662f4fe75ecb6819e6",
706                         "bf774aba43e3e49eb63a6e18037d1118152568f1a3ac4ec8b89aeb6ff8008ae1",
707                         "c4f016558ca8e950c21c3f7fc15f640293a979c7b01754ee7f8b3340d4902ebb",
708                 },
709         }
710
711         t.Logf("Running %d tests", len(tests))
712         for i, test := range tests {
713                 f := new(fieldVal).SetHex(test.in1).Normalize()
714                 f2 := new(fieldVal).SetHex(test.in2).Normalize()
715                 expected := new(fieldVal).SetHex(test.expected).Normalize()
716                 result := f.Mul(f2).Normalize()
717                 if !result.Equals(expected) {
718                         t.Errorf("fieldVal.Mul #%d wrong result\n"+
719                                 "got: %v\nwant: %v", i, result, expected)
720                         continue
721                 }
722         }
723 }
724
725 // TestSquare ensures that squaring field values via Square works as expected.
726 func TestSquare(t *testing.T) {
727         tests := []struct {
728                 in       string // hex encoded value
729                 expected string // expected hex encoded value
730         }{
731                 // secp256k1 prime (aka 0)
732                 {"0", "0"},
733                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "0"},
734                 {"0", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"},
735                 // secp256k1 prime-1
736                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", "1"},
737                 // secp256k1 prime-2
738                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d", "4"},
739                 // Random sampling
740                 {
741                         "b0ba920360ea8436a216128047aab9766d8faf468895eb5090fc8241ec758896",
742                         "133896b0b69fda8ce9f648b9a3af38f345290c9eea3cbd35bafcadf7c34653d3",
743                 },
744                 {
745                         "c55d0d730b1d0285a1599995938b042a756e6e8857d390165ffab480af61cbd5",
746                         "cd81758b3f5877cbe7e5b0a10cebfa73bcbf0957ca6453e63ee8954ab7780bee",
747                 },
748                 {
749                         "e89c1f9a70d93651a1ba4bca5b78658f00de65a66014a25544d3365b0ab82324",
750                         "39ffc7a43e5dbef78fd5d0354fb82c6d34f5a08735e34df29da14665b43aa1f",
751                 },
752                 {
753                         "7dc26186079d22bcbe1614aa20ae627e62d72f9be7ad1e99cac0feb438956f05",
754                         "bf86bcfc4edb3d81f916853adfda80c07c57745b008b60f560b1912f95bce8ae",
755                 },
756         }
757
758         t.Logf("Running %d tests", len(tests))
759         for i, test := range tests {
760                 f := new(fieldVal).SetHex(test.in).Normalize()
761                 expected := new(fieldVal).SetHex(test.expected).Normalize()
762                 result := f.Square().Normalize()
763                 if !result.Equals(expected) {
764                         t.Errorf("fieldVal.Square #%d wrong result\n"+
765                                 "got: %v\nwant: %v", i, result, expected)
766                         continue
767                 }
768         }
769 }
770
771 // TestInverse ensures that finding the multiplicative inverse via Inverse works
772 // as expected.
773 func TestInverse(t *testing.T) {
774         tests := []struct {
775                 in       string // hex encoded value
776                 expected string // expected hex encoded value
777         }{
778                 // secp256k1 prime (aka 0)
779                 {"0", "0"},
780                 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "0"},
781                 {"0", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"},
782                 // secp256k1 prime-1
783                 {
784                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
785                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
786                 },
787                 // secp256k1 prime-2
788                 {
789                         "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d",
790                         "7fffffffffffffffffffffffffffffffffffffffffffffffffffffff7ffffe17",
791                 },
792                 // Random sampling
793                 {
794                         "16fb970147a9acc73654d4be233cc48b875ce20a2122d24f073d29bd28805aca",
795                         "987aeb257b063df0c6d1334051c47092b6d8766c4bf10c463786d93f5bc54354",
796                 },
797                 {
798                         "69d1323ce9f1f7b3bd3c7320b0d6311408e30281e273e39a0d8c7ee1c8257919",
799                         "49340981fa9b8d3dad72de470b34f547ed9179c3953797d0943af67806f4bb6",
800                 },
801                 {
802                         "e0debf988ae098ecda07d0b57713e97c6d213db19753e8c95aa12a2fc1cc5272",
803                         "64f58077b68af5b656b413ea366863f7b2819f8d27375d9c4d9804135ca220c2",
804                 },
805                 {
806                         "dcd394f91f74c2ba16aad74a22bb0ed47fe857774b8f2d6c09e28bfb14642878",
807                         "fb848ec64d0be572a63c38fe83df5e7f3d032f60bf8c969ef67d36bf4ada22a9",
808                 },
809         }
810
811         t.Logf("Running %d tests", len(tests))
812         for i, test := range tests {
813                 f := new(fieldVal).SetHex(test.in).Normalize()
814                 expected := new(fieldVal).SetHex(test.expected).Normalize()
815                 result := f.Inverse().Normalize()
816                 if !result.Equals(expected) {
817                         t.Errorf("fieldVal.Inverse #%d wrong result\n"+
818                                 "got: %v\nwant: %v", i, result, expected)
819                         continue
820                 }
821         }
822 }