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.
13 // TestSetInt ensures that setting a field value to various native integers
15 func TestSetInt(t *testing.T) {
20 {5, [10]uint32{5, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
22 {67108864, [10]uint32{67108864, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
24 {67108865, [10]uint32{67108865, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
26 {4294967295, [10]uint32{4294967295, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
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)
40 // TestZero ensures that zeroing a field value zero works as expected.
41 func TestZero(t *testing.T) {
42 f := new(fieldVal).SetInt(2)
44 for idx, rawInt := range f.n {
46 t.Errorf("internal field integer at index #%d is not "+
47 "zero - got %d", idx, rawInt)
52 // TestIsZero ensures that checking if a field IsZero works as expected.
53 func TestIsZero(t *testing.T) {
56 t.Errorf("new field value is not zero - got %v (rawints %x)", f,
62 t.Errorf("field claims it's zero when it's not - got %v "+
63 "(raw rawints %x)", f, f.n)
68 t.Errorf("field claims it's not zero when it is - got %v "+
69 "(raw rawints %x)", f, f.n)
73 // TestStringer ensures the stringer returns the appropriate hex string.
74 func TestStringer(t *testing.T) {
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"},
91 "0000000000000000000000000000000000000000000000000000000003ffffff",
96 "00000000000000000000000000000000000000000000000000000000ffffffff",
101 "000000000000000000000000000000000000000000000000ffffffffffffffff",
105 "ffffffffffffffffffffffff",
106 "0000000000000000000000000000000000000000ffffffffffffffffffffffff",
110 "ffffffffffffffffffffffffffffffff",
111 "00000000000000000000000000000000ffffffffffffffffffffffffffffffff",
115 "ffffffffffffffffffffffffffffffffffffffff",
116 "000000000000000000000000ffffffffffffffffffffffffffffffffffffffff",
120 "ffffffffffffffffffffffffffffffffffffffffffffffff",
121 "0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff",
125 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
126 "00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
128 // 2^256-4294968273 (the btcec prime, so should result in 0)
130 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
131 "0000000000000000000000000000000000000000000000000000000000000000",
133 // 2^256-4294968274 (the secp256k1 prime+1, so should result in 1)
135 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc30",
136 "0000000000000000000000000000000000000000000000000000000000000001",
140 {"g", "0000000000000000000000000000000000000000000000000000000000000000"},
141 {"1h", "0000000000000000000000000000000000000000000000000000000000000000"},
142 {"i1", "0000000000000000000000000000000000000000000000000000000000000000"},
145 t.Logf("Running %d tests", len(tests))
146 for i, test := range tests {
147 f := new(fieldVal).SetHex(test.in)
149 if result != test.expected {
150 t.Errorf("fieldVal.String #%d wrong result\ngot: %v\n"+
151 "want: %v", i, result, test.expected)
157 // TestNormalize ensures that normalizing the internal field words works as
159 func TestNormalize(t *testing.T) {
161 raw [10]uint32 // Intentionally denormalized value
162 normalized [10]uint32 // Normalized form of the raw value
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},
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},
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},
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},
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},
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},
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},
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},
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},
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},
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},
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},
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},
228 // 2^256 - 4294968273 (secp256k1 prime)
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},
233 // Prime larger than P where both first and second words are larger
234 // than P's first and second words
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},
239 // Prime larger than P where only the second word is larger
240 // than P's second words.
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},
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},
250 // Prime with field representation such that the initial
251 // reduction does not result in a carry to bit 256.
253 // 2^256 - 4294968273 (secp256k1 prime)
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},
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.
262 // 2^256 - 4294968272 (secp256k1 prime + 1)
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},
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.
271 // 2^256 - 4227859409 (secp256k1 prime + 0x4000000)
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},
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.
282 // 2^256 * 5 - ((4294968273 - (977+1)) * 4)
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},
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.
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},
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.
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},
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.
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},
312 t.Logf("Running %d tests", len(tests))
313 for i, test := range tests {
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)
325 // TestIsOdd ensures that checking if a field value IsOdd works as expected.
326 func TestIsOdd(t *testing.T) {
328 in string // hex encoded value
329 expected bool // expected oddness
337 {"fffffffffffffffe", false},
339 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", true},
342 t.Logf("Running %d tests", len(tests))
343 for i, test := range tests {
344 f := new(fieldVal).SetHex(test.in)
346 if result != test.expected {
347 t.Errorf("fieldVal.IsOdd #%d wrong result\n"+
348 "got: %v\nwant: %v", i, result, test.expected)
354 // TestEquals ensures that checking two field values for equality via Equals
355 // works as expected.
356 func TestEquals(t *testing.T) {
358 in1 string // hex encoded value
359 in2 string // hex encoded value
360 expected bool // expected equality
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},
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)
388 // TestNegate ensures that negating field values via Negate works as expected.
389 func TestNegate(t *testing.T) {
391 in string // hex encoded value
392 expected string // expected hex encoded value
394 // secp256k1 prime (aka 0)
396 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "0"},
397 {"0", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"},
399 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", "1"},
400 {"1", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e"},
402 {"2", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d"},
403 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d", "2"},
406 "b3d9aac9c5e43910b4385b53c7e78c21d4cd5f8e683c633aed04c233efc2e120",
407 "4c2655363a1bc6ef4bc7a4ac381873de2b32a07197c39cc512fb3dcb103d1b0f",
410 "f8a85984fee5a12a7c8dd08830d83423c937d77c379e4a958e447a25f407733f",
411 "757a67b011a5ed583722f77cf27cbdc36c82883c861b56a71bb85d90bf888f0",
414 "45ee6142a7fda884211e93352ed6cb2807800e419533be723a9548823ece8312",
415 "ba119ebd5802577bdee16ccad12934d7f87ff1be6acc418dc56ab77cc131791d",
418 "53c2a668f07e411a2e473e1c3b6dcb495dec1227af27673761d44afe5b43d22b",
419 "ac3d59970f81bee5d1b8c1e3c49234b6a213edd850d898c89e2bb500a4bc2a04",
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)
436 // TestAddInt ensures that adding an integer to field values via AddInt works as
438 func TestAddInt(t *testing.T) {
440 in1 string // hex encoded value
441 in2 uint // unsigned integer to add to the value above
442 expected string // expected hex encoded value
447 // secp256k1 prime-1 + 1
448 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", 1, "0"},
449 // secp256k1 prime + 1
450 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", 1, "1"},
453 "ff95ad9315aff04ab4af0ce673620c7145dc85d03bab5ba4b09ca2c4dec2d6c1",
455 "ff95ad9315aff04ab4af0ce673620c7145dc85d03bab5ba4b09ca2c4dec2d7d0",
458 "44bdae6b772e7987941f1ba314e6a5b7804a4c12c00961b57d20f41deea9cecf",
460 "44bdae6b772e7987941f1ba314e6a5b7804a4c12c00961b57d20f41e1b9aec10",
463 "88c3ecae67b591935fb1f6a9499c35315ffad766adca665c50b55f7105122c9c",
465 "88c3ecae67b591935fb1f6a9499c35315ffad766adca665c50b55f714d3bd6c9",
468 "8523e9edf360ca32a95aae4e57fcde5a542b471d08a974d94ea0ee09a015e2a6",
470 "8523e9edf360ca32a95aae4e57fcde5a542b471d08a974d94ea0ee0a4228484b",
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)
487 // TestAdd ensures that adding two field values together via Add works as
489 func TestAdd(t *testing.T) {
491 in1 string // first hex encoded value
492 in2 string // second hex encoded value to add
493 expected string // expected hex encoded value
498 // secp256k1 prime-1 + 1
499 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", "1", "0"},
500 // secp256k1 prime + 1
501 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "1", "1"},
504 "2b2012f975404e5065b4292fb8bed0a5d315eacf24c74d8b27e73bcc5430edcc",
505 "2c3cefa4e4753e8aeec6ac4c12d99da4d78accefda3b7885d4c6bab46c86db92",
506 "575d029e59b58cdb547ad57bcb986e4aaaa0b7beff02c610fcadf680c0b7c95e",
509 "8131e8722fe59bb189692b96c9f38de92885730f1dd39ab025daffb94c97f79c",
510 "ff5454b765f0aab5f0977dcc629becc84cabeb9def48e79c6aadb2622c490fa9",
511 "80863d2995d646677a00a9632c8f7ab175315ead0d1c824c9088b21c78e10b16",
514 "c7c95e93d0892b2b2cdd77e80eb646ea61be7a30ac7e097e9f843af73fad5c22",
515 "3afe6f91a74dfc1c7f15c34907ee981656c37236d946767dd53ccad9190e437c",
516 "02c7ce2577d72747abf33b3116a4df00b881ec6785c47ffc74c105d158bba36f",
519 "fd1c26f6a23381e5d785ba889494ec059369b888ad8431cd67d8c934b580dbe1",
520 "a475aa5a31dcca90ef5b53c097d9133d6b7117474b41e7877bb199590fc0489c",
521 "a191d150d4104c76c6e10e492c6dff42fedacfcff8c61954e38a628ec541284e",
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)
539 // TestAdd2 ensures that adding two field values together via Add2 works as
541 func TestAdd2(t *testing.T) {
543 in1 string // first hex encoded value
544 in2 string // second hex encoded value to add
545 expected string // expected hex encoded value
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"},
558 "ad82b8d1cc136e23e9fd77fe2c7db1fe5a2ecbfcbde59ab3529758334f862d28",
559 "4d6a4e95d6d61f4f46b528bebe152d408fd741157a28f415639347a84f6f574b",
560 "faed0767a2e98d7330b2a0bcea92df3eea060d12380e8ec8b62a9fdb9ef58473",
563 "f3f43a2540054a86e1df98547ec1c0e157b193e5350fb4a3c3ea214b228ac5e7",
564 "25706572592690ea3ddc951a1b48b504a4c83dc253756e1b96d56fdfb3199522",
565 "19649f97992bdb711fbc2d6e9a0a75e5fc79d1a7888522bf5abf912bd5a45eda",
568 "6915bb94eef13ff1bb9b2633d997e13b9b1157c713363cc0e891416d6734f5b8",
569 "11f90d6ac6fe1c4e8900b1c85fb575c251ec31b9bc34b35ada0aea1c21eded22",
570 "7b0ec8ffb5ef5c40449bd7fc394d56fdecfd8980cf6af01bc29c2b898922e2da",
573 "48b0c9eae622eed9335b747968544eb3e75cb2dc8128388f948aa30f88cabde4",
574 "0989882b52f85f9d524a3a3061a0e01f46d597839d2ba637320f4b9510c8d2d5",
575 "523a5216391b4e7685a5aea9c9f52ed32e324a601e53dec6c699eea4999390b9",
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)
593 // TestMulInt ensures that adding an integer to field values via MulInt works as
595 func TestMulInt(t *testing.T) {
597 in1 string // hex encoded value
598 in2 uint // unsigned integer to multiply with value above
599 expected string // expected hex encoded value
605 // secp256k1 prime-1 * 2
607 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
609 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d",
611 // secp256k1 prime * 3
612 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", 3, "0"},
613 // secp256k1 prime-1 * 8
615 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
617 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc27",
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
623 "b75674dc9180d306c692163ac5e089f7cef166af99645c0c23568ab6d967288a",
625 "4c06bd2b6904f228a76c8560a3433bced9a8681d985a2848d407404d186b0280",
628 "54873298ac2b5ba8591c125ae54931f5ea72040aee07b208d6135476fb5b9c0e",
630 "fd9597ca048212f90b543710afdb95e1bf560c20ca17161a8239fd64f212d42a",
633 "7c30fbd363a74c17e1198f56b090b59bbb6c8755a74927a6cba7a54843506401",
635 "6cf4eb20f2447c77657fccb172d38c0aa91ea4ac446dc641fa463a6b5091fba7",
638 "fb4529be3e027a3d1587d8a500b72f2d312e3577340ef5175f96d113be4c2ceb",
640 "da294df1f013d1e8ac3ec52805b979698971abb9a077a8bafcb688a4f261820f",
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)
657 // TestMul ensures that multiplying two field valuess via Mul works as expected.
658 func TestMul(t *testing.T) {
660 in1 string // first hex encoded value
661 in2 string // second hex encoded value to multiply with
662 expected string // expected hex encoded value
668 // slightly over prime
670 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffff1ffff",
674 // secp256k1 prime-1 * 2
676 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
678 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d",
680 // secp256k1 prime * 3
681 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "3", "0"},
682 // secp256k1 prime-1 * 8
684 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
686 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc27",
690 "cfb81753d5ef499a98ecc04c62cb7768c2e4f1740032946db1c12e405248137e",
691 "58f355ad27b4d75fb7db0442452e732c436c1f7c5a7c4e214fa9cc031426a7d3",
692 "1018cd2d7c2535235b71e18db9cd98027386328d2fa6a14b36ec663c4c87282b",
695 "26e9d61d1cdf3920e9928e85fa3df3e7556ef9ab1d14ec56d8b4fc8ed37235bf",
696 "2dfc4bbe537afee979c644f8c97b31e58be5296d6dbc460091eae630c98511cf",
697 "da85f48da2dc371e223a1ae63bd30b7e7ee45ae9b189ac43ff357e9ef8cf107a",
700 "5db64ed5afb71646c8b231585d5b2bf7e628590154e0854c4c29920b999ff351",
701 "279cfae5eea5d09ade8e6a7409182f9de40981bc31c84c3d3dfe1d933f152e9a",
702 "2c78fbae91792dd0b157abe3054920049b1879a7cc9d98cfda927d83be411b37",
705 "b66dfc1f96820b07d2bdbd559c19319a3a73c97ceb7b3d662f4fe75ecb6819e6",
706 "bf774aba43e3e49eb63a6e18037d1118152568f1a3ac4ec8b89aeb6ff8008ae1",
707 "c4f016558ca8e950c21c3f7fc15f640293a979c7b01754ee7f8b3340d4902ebb",
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)
725 // TestSquare ensures that squaring field values via Square works as expected.
726 func TestSquare(t *testing.T) {
728 in string // hex encoded value
729 expected string // expected hex encoded value
731 // secp256k1 prime (aka 0)
733 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "0"},
734 {"0", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"},
736 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e", "1"},
738 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d", "4"},
741 "b0ba920360ea8436a216128047aab9766d8faf468895eb5090fc8241ec758896",
742 "133896b0b69fda8ce9f648b9a3af38f345290c9eea3cbd35bafcadf7c34653d3",
745 "c55d0d730b1d0285a1599995938b042a756e6e8857d390165ffab480af61cbd5",
746 "cd81758b3f5877cbe7e5b0a10cebfa73bcbf0957ca6453e63ee8954ab7780bee",
749 "e89c1f9a70d93651a1ba4bca5b78658f00de65a66014a25544d3365b0ab82324",
750 "39ffc7a43e5dbef78fd5d0354fb82c6d34f5a08735e34df29da14665b43aa1f",
753 "7dc26186079d22bcbe1614aa20ae627e62d72f9be7ad1e99cac0feb438956f05",
754 "bf86bcfc4edb3d81f916853adfda80c07c57745b008b60f560b1912f95bce8ae",
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)
771 // TestInverse ensures that finding the multiplicative inverse via Inverse works
773 func TestInverse(t *testing.T) {
775 in string // hex encoded value
776 expected string // expected hex encoded value
778 // secp256k1 prime (aka 0)
780 {"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", "0"},
781 {"0", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"},
784 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
785 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2e",
789 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2d",
790 "7fffffffffffffffffffffffffffffffffffffffffffffffffffffff7ffffe17",
794 "16fb970147a9acc73654d4be233cc48b875ce20a2122d24f073d29bd28805aca",
795 "987aeb257b063df0c6d1334051c47092b6d8766c4bf10c463786d93f5bc54354",
798 "69d1323ce9f1f7b3bd3c7320b0d6311408e30281e273e39a0d8c7ee1c8257919",
799 "49340981fa9b8d3dad72de470b34f547ed9179c3953797d0943af67806f4bb6",
802 "e0debf988ae098ecda07d0b57713e97c6d213db19753e8c95aa12a2fc1cc5272",
803 "64f58077b68af5b656b413ea366863f7b2819f8d27375d9c4d9804135ca220c2",
806 "dcd394f91f74c2ba16aad74a22bb0ed47fe857774b8f2d6c09e28bfb14642878",
807 "fb848ec64d0be572a63c38fe83df5e7f3d032f60bf8c969ef67d36bf4ada22a9",
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)