github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/btcec/field_test.go (about)

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