Black Lives Matter. Support the Equal Justice Initiative.

Source file src/crypto/elliptic/p224_test.go

Documentation: crypto/elliptic

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package elliptic
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  	"math/big"
    11  	"math/bits"
    12  	"math/rand"
    13  	"reflect"
    14  	"testing"
    15  	"testing/quick"
    16  )
    17  
    18  var toFromBigTests = []string{
    19  	"0",
    20  	"1",
    21  	"23",
    22  	"b70e0cb46bb4bf7f321390b94a03c1d356c01122343280d6105c1d21",
    23  	"706a46d476dcb76798e6046d89474788d164c18032d268fd10704fa6",
    24  }
    25  
    26  func p224AlternativeToBig(in *p224FieldElement) *big.Int {
    27  	ret := new(big.Int)
    28  	tmp := new(big.Int)
    29  
    30  	for i := len(in) - 1; i >= 0; i-- {
    31  		ret.Lsh(ret, 28)
    32  		tmp.SetInt64(int64(in[i]))
    33  		ret.Add(ret, tmp)
    34  	}
    35  	ret.Mod(ret, P224().Params().P)
    36  	return ret
    37  }
    38  
    39  func TestP224ToFromBig(t *testing.T) {
    40  	for i, test := range toFromBigTests {
    41  		n, _ := new(big.Int).SetString(test, 16)
    42  		var x p224FieldElement
    43  		p224FromBig(&x, n)
    44  		m := p224ToBig(&x)
    45  		if n.Cmp(m) != 0 {
    46  			t.Errorf("#%d: %x != %x", i, n, m)
    47  		}
    48  		q := p224AlternativeToBig(&x)
    49  		if n.Cmp(q) != 0 {
    50  			t.Errorf("#%d: %x != %x (alternative)", i, n, q)
    51  		}
    52  	}
    53  }
    54  
    55  // quickCheckConfig32 will make each quickcheck test run (32 * -quickchecks)
    56  // times. The default value of -quickchecks is 100.
    57  var quickCheckConfig32 = &quick.Config{MaxCountScale: 32}
    58  
    59  // weirdLimbs can be combined to generate a range of edge-case field elements.
    60  var weirdLimbs = [...]uint32{
    61  	0, 1, (1 << 29) - 1,
    62  	(1 << 12), (1 << 12) - 1,
    63  	(1 << 28), (1 << 28) - 1,
    64  }
    65  
    66  func generateLimb(rand *rand.Rand) uint32 {
    67  	const bottom29Bits = 0x1fffffff
    68  	n := rand.Intn(len(weirdLimbs) + 3)
    69  	switch n {
    70  	case len(weirdLimbs):
    71  		// Random value.
    72  		return uint32(rand.Int31n(1 << 29))
    73  	case len(weirdLimbs) + 1:
    74  		// Sum of two values.
    75  		k := generateLimb(rand) + generateLimb(rand)
    76  		return k & bottom29Bits
    77  	case len(weirdLimbs) + 2:
    78  		// Difference of two values.
    79  		k := generateLimb(rand) - generateLimb(rand)
    80  		return k & bottom29Bits
    81  	default:
    82  		return weirdLimbs[n]
    83  	}
    84  }
    85  
    86  func (p224FieldElement) Generate(rand *rand.Rand, size int) reflect.Value {
    87  	return reflect.ValueOf(p224FieldElement{
    88  		generateLimb(rand),
    89  		generateLimb(rand),
    90  		generateLimb(rand),
    91  		generateLimb(rand),
    92  		generateLimb(rand),
    93  		generateLimb(rand),
    94  		generateLimb(rand),
    95  		generateLimb(rand),
    96  	})
    97  }
    98  
    99  func isInBounds(x *p224FieldElement) bool {
   100  	return bits.Len32(x[0]) <= 29 &&
   101  		bits.Len32(x[1]) <= 29 &&
   102  		bits.Len32(x[2]) <= 29 &&
   103  		bits.Len32(x[3]) <= 29 &&
   104  		bits.Len32(x[4]) <= 29 &&
   105  		bits.Len32(x[5]) <= 29 &&
   106  		bits.Len32(x[6]) <= 29 &&
   107  		bits.Len32(x[7]) <= 29
   108  }
   109  
   110  func TestP224Mul(t *testing.T) {
   111  	mulMatchesBigInt := func(a, b, out p224FieldElement) bool {
   112  		var tmp p224LargeFieldElement
   113  		p224Mul(&out, &a, &b, &tmp)
   114  
   115  		exp := new(big.Int).Mul(p224AlternativeToBig(&a), p224AlternativeToBig(&b))
   116  		exp.Mod(exp, P224().Params().P)
   117  		got := p224AlternativeToBig(&out)
   118  		if exp.Cmp(got) != 0 || !isInBounds(&out) {
   119  			t.Logf("a = %x", a)
   120  			t.Logf("b = %x", b)
   121  			t.Logf("p224Mul(a, b) = %x = %v", out, got)
   122  			t.Logf("a * b = %v", exp)
   123  			return false
   124  		}
   125  
   126  		return true
   127  	}
   128  
   129  	a := p224FieldElement{0xfffffff, 0xfffffff, 0xf00ffff, 0x20f, 0x0, 0x0, 0x0, 0x0}
   130  	b := p224FieldElement{1, 0, 0, 0, 0, 0, 0, 0}
   131  	if !mulMatchesBigInt(a, b, p224FieldElement{}) {
   132  		t.Fail()
   133  	}
   134  
   135  	if err := quick.Check(mulMatchesBigInt, quickCheckConfig32); err != nil {
   136  		t.Error(err)
   137  	}
   138  }
   139  
   140  func TestP224Square(t *testing.T) {
   141  	squareMatchesBigInt := func(a, out p224FieldElement) bool {
   142  		var tmp p224LargeFieldElement
   143  		p224Square(&out, &a, &tmp)
   144  
   145  		exp := p224AlternativeToBig(&a)
   146  		exp.Mul(exp, exp)
   147  		exp.Mod(exp, P224().Params().P)
   148  		got := p224AlternativeToBig(&out)
   149  		if exp.Cmp(got) != 0 || !isInBounds(&out) {
   150  			t.Logf("a = %x", a)
   151  			t.Logf("p224Square(a, b) = %x = %v", out, got)
   152  			t.Logf("a * a = %v", exp)
   153  			return false
   154  		}
   155  
   156  		return true
   157  	}
   158  
   159  	if err := quick.Check(squareMatchesBigInt, quickCheckConfig32); err != nil {
   160  		t.Error(err)
   161  	}
   162  }
   163  
   164  func TestP224Add(t *testing.T) {
   165  	addMatchesBigInt := func(a, b, out p224FieldElement) bool {
   166  		p224Add(&out, &a, &b)
   167  
   168  		exp := new(big.Int).Add(p224AlternativeToBig(&a), p224AlternativeToBig(&b))
   169  		exp.Mod(exp, P224().Params().P)
   170  		got := p224AlternativeToBig(&out)
   171  		if exp.Cmp(got) != 0 {
   172  			t.Logf("a = %x", a)
   173  			t.Logf("b = %x", b)
   174  			t.Logf("p224Add(a, b) = %x = %v", out, got)
   175  			t.Logf("a + b = %v", exp)
   176  			return false
   177  		}
   178  
   179  		return true
   180  	}
   181  
   182  	if err := quick.Check(addMatchesBigInt, quickCheckConfig32); err != nil {
   183  		t.Error(err)
   184  	}
   185  }
   186  
   187  func TestP224Reduce(t *testing.T) {
   188  	reduceMatchesBigInt := func(a p224FieldElement) bool {
   189  		out := a
   190  		// TODO: generate higher values for functions like p224Reduce that are
   191  		// expected to work with higher input bounds.
   192  		p224Reduce(&out)
   193  
   194  		exp := p224AlternativeToBig(&a)
   195  		got := p224AlternativeToBig(&out)
   196  		if exp.Cmp(got) != 0 || !isInBounds(&out) {
   197  			t.Logf("a = %x = %v", a, exp)
   198  			t.Logf("p224Reduce(a) = %x = %v", out, got)
   199  			return false
   200  		}
   201  
   202  		return true
   203  	}
   204  
   205  	if err := quick.Check(reduceMatchesBigInt, quickCheckConfig32); err != nil {
   206  		t.Error(err)
   207  	}
   208  }
   209  
   210  func TestP224Contract(t *testing.T) {
   211  	contractMatchesBigInt := func(a, out p224FieldElement) bool {
   212  		p224Contract(&out, &a)
   213  
   214  		exp := p224AlternativeToBig(&a)
   215  		got := p224AlternativeToBig(&out)
   216  		if exp.Cmp(got) != 0 {
   217  			t.Logf("a = %x = %v", a, exp)
   218  			t.Logf("p224Contract(a) = %x = %v", out, got)
   219  			return false
   220  		}
   221  
   222  		// Check that out < P.
   223  		for i := range p224P {
   224  			k := 8 - i - 1
   225  			if out[k] > p224P[k] {
   226  				t.Logf("p224Contract(a) = %x", out)
   227  				return false
   228  			}
   229  			if out[k] < p224P[k] {
   230  				return true
   231  			}
   232  		}
   233  		t.Logf("p224Contract(a) = %x", out)
   234  		return false
   235  	}
   236  
   237  	if !contractMatchesBigInt(p224P, p224FieldElement{}) {
   238  		t.Error("p224Contract(p) is broken")
   239  	}
   240  	pMinus1 := p224FieldElement{0, 0, 0, 0xffff000, 0xfffffff, 0xfffffff, 0xfffffff, 0xfffffff}
   241  	if !contractMatchesBigInt(pMinus1, p224FieldElement{}) {
   242  		t.Error("p224Contract(p - 1) is broken")
   243  	}
   244  	// Check that we can handle input above p, but lowest limb zero.
   245  	a := p224FieldElement{0, 1, 0, 0xffff000, 0xfffffff, 0xfffffff, 0xfffffff, 0xfffffff}
   246  	if !contractMatchesBigInt(a, p224FieldElement{}) {
   247  		t.Error("p224Contract(p + 2²⁸) is broken")
   248  	}
   249  	// Check that we can handle input above p, but lowest three limbs zero.
   250  	b := p224FieldElement{0, 0, 0, 0xffff001, 0xfffffff, 0xfffffff, 0xfffffff, 0xfffffff}
   251  	if !contractMatchesBigInt(b, p224FieldElement{}) {
   252  		t.Error("p224Contract(p + 2⁸⁴) is broken")
   253  	}
   254  
   255  	if err := quick.Check(contractMatchesBigInt, quickCheckConfig32); err != nil {
   256  		t.Error(err)
   257  	}
   258  }
   259  
   260  func TestP224IsZero(t *testing.T) {
   261  	if got := p224IsZero(&p224FieldElement{}); got != 1 {
   262  		t.Errorf("p224IsZero(0) = %d, expected 1", got)
   263  	}
   264  	if got := p224IsZero((*p224FieldElement)(&p224P)); got != 1 {
   265  		t.Errorf("p224IsZero(p) = %d, expected 1", got)
   266  	}
   267  	if got := p224IsZero(&p224FieldElement{1}); got != 0 {
   268  		t.Errorf("p224IsZero(1) = %d, expected 0", got)
   269  	}
   270  
   271  	isZeroMatchesBigInt := func(a p224FieldElement) bool {
   272  		isZero := p224IsZero(&a)
   273  
   274  		big := p224AlternativeToBig(&a)
   275  		if big.Sign() == 0 && isZero != 1 {
   276  			return false
   277  		}
   278  		if big.Sign() != 0 && isZero != 0 {
   279  			return false
   280  		}
   281  		return true
   282  	}
   283  
   284  	if err := quick.Check(isZeroMatchesBigInt, quickCheckConfig32); err != nil {
   285  		t.Error(err)
   286  	}
   287  }
   288  
   289  func TestP224Invert(t *testing.T) {
   290  	var out p224FieldElement
   291  
   292  	p224Invert(&out, &p224FieldElement{})
   293  	if got := p224IsZero(&out); got != 1 {
   294  		t.Errorf("p224Invert(0) = %x, expected 0", out)
   295  	}
   296  
   297  	p224Invert(&out, (*p224FieldElement)(&p224P))
   298  	if got := p224IsZero(&out); got != 1 {
   299  		t.Errorf("p224Invert(p) = %x, expected 0", out)
   300  	}
   301  
   302  	p224Invert(&out, &p224FieldElement{1})
   303  	p224Contract(&out, &out)
   304  	if out != (p224FieldElement{1}) {
   305  		t.Errorf("p224Invert(1) = %x, expected 1", out)
   306  	}
   307  
   308  	var tmp p224LargeFieldElement
   309  	a := p224FieldElement{1, 2, 3, 4, 5, 6, 7, 8}
   310  	p224Invert(&out, &a)
   311  	p224Mul(&out, &out, &a, &tmp)
   312  	p224Contract(&out, &out)
   313  	if out != (p224FieldElement{1}) {
   314  		t.Errorf("p224Invert(a) * a = %x, expected 1", out)
   315  	}
   316  }
   317  
   318  type baseMultTest struct {
   319  	k    string
   320  	x, y string
   321  }
   322  
   323  var p224BaseMultTests = []baseMultTest{
   324  	{
   325  		"1",
   326  		"b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21",
   327  		"bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34",
   328  	},
   329  	{
   330  		"2",
   331  		"706a46dc76dcb76798e60e6d89474788d16dc18032d268fd1a704fa6",
   332  		"1c2b76a7bc25e7702a704fa986892849fca629487acf3709d2e4e8bb",
   333  	},
   334  	{
   335  		"3",
   336  		"df1b1d66a551d0d31eff822558b9d2cc75c2180279fe0d08fd896d04",
   337  		"a3f7f03cadd0be444c0aa56830130ddf77d317344e1af3591981a925",
   338  	},
   339  	{
   340  		"4",
   341  		"ae99feebb5d26945b54892092a8aee02912930fa41cd114e40447301",
   342  		"482580a0ec5bc47e88bc8c378632cd196cb3fa058a7114eb03054c9",
   343  	},
   344  	{
   345  		"5",
   346  		"31c49ae75bce7807cdff22055d94ee9021fedbb5ab51c57526f011aa",
   347  		"27e8bff1745635ec5ba0c9f1c2ede15414c6507d29ffe37e790a079b",
   348  	},
   349  	{
   350  		"6",
   351  		"1f2483f82572251fca975fea40db821df8ad82a3c002ee6c57112408",
   352  		"89faf0ccb750d99b553c574fad7ecfb0438586eb3952af5b4b153c7e",
   353  	},
   354  	{
   355  		"7",
   356  		"db2f6be630e246a5cf7d99b85194b123d487e2d466b94b24a03c3e28",
   357  		"f3a30085497f2f611ee2517b163ef8c53b715d18bb4e4808d02b963",
   358  	},
   359  	{
   360  		"8",
   361  		"858e6f9cc6c12c31f5df124aa77767b05c8bc021bd683d2b55571550",
   362  		"46dcd3ea5c43898c5c5fc4fdac7db39c2f02ebee4e3541d1e78047a",
   363  	},
   364  	{
   365  		"9",
   366  		"2fdcccfee720a77ef6cb3bfbb447f9383117e3daa4a07e36ed15f78d",
   367  		"371732e4f41bf4f7883035e6a79fcedc0e196eb07b48171697517463",
   368  	},
   369  	{
   370  		"10",
   371  		"aea9e17a306517eb89152aa7096d2c381ec813c51aa880e7bee2c0fd",
   372  		"39bb30eab337e0a521b6cba1abe4b2b3a3e524c14a3fe3eb116b655f",
   373  	},
   374  	{
   375  		"11",
   376  		"ef53b6294aca431f0f3c22dc82eb9050324f1d88d377e716448e507c",
   377  		"20b510004092e96636cfb7e32efded8265c266dfb754fa6d6491a6da",
   378  	},
   379  	{
   380  		"12",
   381  		"6e31ee1dc137f81b056752e4deab1443a481033e9b4c93a3044f4f7a",
   382  		"207dddf0385bfdeab6e9acda8da06b3bbef224a93ab1e9e036109d13",
   383  	},
   384  	{
   385  		"13",
   386  		"34e8e17a430e43289793c383fac9774247b40e9ebd3366981fcfaeca",
   387  		"252819f71c7fb7fbcb159be337d37d3336d7feb963724fdfb0ecb767",
   388  	},
   389  	{
   390  		"14",
   391  		"a53640c83dc208603ded83e4ecf758f24c357d7cf48088b2ce01e9fa",
   392  		"d5814cd724199c4a5b974a43685fbf5b8bac69459c9469bc8f23ccaf",
   393  	},
   394  	{
   395  		"15",
   396  		"baa4d8635511a7d288aebeedd12ce529ff102c91f97f867e21916bf9",
   397  		"979a5f4759f80f4fb4ec2e34f5566d595680a11735e7b61046127989",
   398  	},
   399  	{
   400  		"16",
   401  		"b6ec4fe1777382404ef679997ba8d1cc5cd8e85349259f590c4c66d",
   402  		"3399d464345906b11b00e363ef429221f2ec720d2f665d7dead5b482",
   403  	},
   404  	{
   405  		"17",
   406  		"b8357c3a6ceef288310e17b8bfeff9200846ca8c1942497c484403bc",
   407  		"ff149efa6606a6bd20ef7d1b06bd92f6904639dce5174db6cc554a26",
   408  	},
   409  	{
   410  		"18",
   411  		"c9ff61b040874c0568479216824a15eab1a838a797d189746226e4cc",
   412  		"ea98d60e5ffc9b8fcf999fab1df7e7ef7084f20ddb61bb045a6ce002",
   413  	},
   414  	{
   415  		"19",
   416  		"a1e81c04f30ce201c7c9ace785ed44cc33b455a022f2acdbc6cae83c",
   417  		"dcf1f6c3db09c70acc25391d492fe25b4a180babd6cea356c04719cd",
   418  	},
   419  	{
   420  		"20",
   421  		"fcc7f2b45df1cd5a3c0c0731ca47a8af75cfb0347e8354eefe782455",
   422  		"d5d7110274cba7cdee90e1a8b0d394c376a5573db6be0bf2747f530",
   423  	},
   424  	{
   425  		"112233445566778899",
   426  		"61f077c6f62ed802dad7c2f38f5c67f2cc453601e61bd076bb46179e",
   427  		"2272f9e9f5933e70388ee652513443b5e289dd135dcc0d0299b225e4",
   428  	},
   429  	{
   430  		"112233445566778899112233445566778899",
   431  		"29895f0af496bfc62b6ef8d8a65c88c613949b03668aab4f0429e35",
   432  		"3ea6e53f9a841f2019ec24bde1a75677aa9b5902e61081c01064de93",
   433  	},
   434  	{
   435  		"6950511619965839450988900688150712778015737983940691968051900319680",
   436  		"ab689930bcae4a4aa5f5cb085e823e8ae30fd365eb1da4aba9cf0379",
   437  		"3345a121bbd233548af0d210654eb40bab788a03666419be6fbd34e7",
   438  	},
   439  	{
   440  		"13479972933410060327035789020509431695094902435494295338570602119423",
   441  		"bdb6a8817c1f89da1c2f3dd8e97feb4494f2ed302a4ce2bc7f5f4025",
   442  		"4c7020d57c00411889462d77a5438bb4e97d177700bf7243a07f1680",
   443  	},
   444  	{
   445  		"13479971751745682581351455311314208093898607229429740618390390702079",
   446  		"d58b61aa41c32dd5eba462647dba75c5d67c83606c0af2bd928446a9",
   447  		"d24ba6a837be0460dd107ae77725696d211446c5609b4595976b16bd",
   448  	},
   449  	{
   450  		"13479972931865328106486971546324465392952975980343228160962702868479",
   451  		"dc9fa77978a005510980e929a1485f63716df695d7a0c18bb518df03",
   452  		"ede2b016f2ddffc2a8c015b134928275ce09e5661b7ab14ce0d1d403",
   453  	},
   454  	{
   455  		"11795773708834916026404142434151065506931607341523388140225443265536",
   456  		"499d8b2829cfb879c901f7d85d357045edab55028824d0f05ba279ba",
   457  		"bf929537b06e4015919639d94f57838fa33fc3d952598dcdbb44d638",
   458  	},
   459  	{
   460  		"784254593043826236572847595991346435467177662189391577090",
   461  		"8246c999137186632c5f9eddf3b1b0e1764c5e8bd0e0d8a554b9cb77",
   462  		"e80ed8660bc1cb17ac7d845be40a7a022d3306f116ae9f81fea65947",
   463  	},
   464  	{
   465  		"13479767645505654746623887797783387853576174193480695826442858012671",
   466  		"6670c20afcceaea672c97f75e2e9dd5c8460e54bb38538ebb4bd30eb",
   467  		"f280d8008d07a4caf54271f993527d46ff3ff46fd1190a3f1faa4f74",
   468  	},
   469  	{
   470  		"205688069665150753842126177372015544874550518966168735589597183",
   471  		"eca934247425cfd949b795cb5ce1eff401550386e28d1a4c5a8eb",
   472  		"d4c01040dba19628931bc8855370317c722cbd9ca6156985f1c2e9ce",
   473  	},
   474  	{
   475  		"13479966930919337728895168462090683249159702977113823384618282123295",
   476  		"ef353bf5c73cd551b96d596fbc9a67f16d61dd9fe56af19de1fba9cd",
   477  		"21771b9cdce3e8430c09b3838be70b48c21e15bc09ee1f2d7945b91f",
   478  	},
   479  	{
   480  		"50210731791415612487756441341851895584393717453129007497216",
   481  		"4036052a3091eb481046ad3289c95d3ac905ca0023de2c03ecd451cf",
   482  		"d768165a38a2b96f812586a9d59d4136035d9c853a5bf2e1c86a4993",
   483  	},
   484  	{
   485  		"26959946667150639794667015087019625940457807714424391721682722368041",
   486  		"fcc7f2b45df1cd5a3c0c0731ca47a8af75cfb0347e8354eefe782455",
   487  		"f2a28eefd8b345832116f1e574f2c6b2c895aa8c24941f40d8b80ad1",
   488  	},
   489  	{
   490  		"26959946667150639794667015087019625940457807714424391721682722368042",
   491  		"a1e81c04f30ce201c7c9ace785ed44cc33b455a022f2acdbc6cae83c",
   492  		"230e093c24f638f533dac6e2b6d01da3b5e7f45429315ca93fb8e634",
   493  	},
   494  	{
   495  		"26959946667150639794667015087019625940457807714424391721682722368043",
   496  		"c9ff61b040874c0568479216824a15eab1a838a797d189746226e4cc",
   497  		"156729f1a003647030666054e208180f8f7b0df2249e44fba5931fff",
   498  	},
   499  	{
   500  		"26959946667150639794667015087019625940457807714424391721682722368044",
   501  		"b8357c3a6ceef288310e17b8bfeff9200846ca8c1942497c484403bc",
   502  		"eb610599f95942df1082e4f9426d086fb9c6231ae8b24933aab5db",
   503  	},
   504  	{
   505  		"26959946667150639794667015087019625940457807714424391721682722368045",
   506  		"b6ec4fe1777382404ef679997ba8d1cc5cd8e85349259f590c4c66d",
   507  		"cc662b9bcba6f94ee4ff1c9c10bd6ddd0d138df2d099a282152a4b7f",
   508  	},
   509  	{
   510  		"26959946667150639794667015087019625940457807714424391721682722368046",
   511  		"baa4d8635511a7d288aebeedd12ce529ff102c91f97f867e21916bf9",
   512  		"6865a0b8a607f0b04b13d1cb0aa992a5a97f5ee8ca1849efb9ed8678",
   513  	},
   514  	{
   515  		"26959946667150639794667015087019625940457807714424391721682722368047",
   516  		"a53640c83dc208603ded83e4ecf758f24c357d7cf48088b2ce01e9fa",
   517  		"2a7eb328dbe663b5a468b5bc97a040a3745396ba636b964370dc3352",
   518  	},
   519  	{
   520  		"26959946667150639794667015087019625940457807714424391721682722368048",
   521  		"34e8e17a430e43289793c383fac9774247b40e9ebd3366981fcfaeca",
   522  		"dad7e608e380480434ea641cc82c82cbc92801469c8db0204f13489a",
   523  	},
   524  	{
   525  		"26959946667150639794667015087019625940457807714424391721682722368049",
   526  		"6e31ee1dc137f81b056752e4deab1443a481033e9b4c93a3044f4f7a",
   527  		"df82220fc7a4021549165325725f94c3410ddb56c54e161fc9ef62ee",
   528  	},
   529  	{
   530  		"26959946667150639794667015087019625940457807714424391721682722368050",
   531  		"ef53b6294aca431f0f3c22dc82eb9050324f1d88d377e716448e507c",
   532  		"df4aefffbf6d1699c930481cd102127c9a3d992048ab05929b6e5927",
   533  	},
   534  	{
   535  		"26959946667150639794667015087019625940457807714424391721682722368051",
   536  		"aea9e17a306517eb89152aa7096d2c381ec813c51aa880e7bee2c0fd",
   537  		"c644cf154cc81f5ade49345e541b4d4b5c1adb3eb5c01c14ee949aa2",
   538  	},
   539  	{
   540  		"26959946667150639794667015087019625940457807714424391721682722368052",
   541  		"2fdcccfee720a77ef6cb3bfbb447f9383117e3daa4a07e36ed15f78d",
   542  		"c8e8cd1b0be40b0877cfca1958603122f1e6914f84b7e8e968ae8b9e",
   543  	},
   544  	{
   545  		"26959946667150639794667015087019625940457807714424391721682722368053",
   546  		"858e6f9cc6c12c31f5df124aa77767b05c8bc021bd683d2b55571550",
   547  		"fb9232c15a3bc7673a3a03b0253824c53d0fd1411b1cabe2e187fb87",
   548  	},
   549  	{
   550  		"26959946667150639794667015087019625940457807714424391721682722368054",
   551  		"db2f6be630e246a5cf7d99b85194b123d487e2d466b94b24a03c3e28",
   552  		"f0c5cff7ab680d09ee11dae84e9c1072ac48ea2e744b1b7f72fd469e",
   553  	},
   554  	{
   555  		"26959946667150639794667015087019625940457807714424391721682722368055",
   556  		"1f2483f82572251fca975fea40db821df8ad82a3c002ee6c57112408",
   557  		"76050f3348af2664aac3a8b05281304ebc7a7914c6ad50a4b4eac383",
   558  	},
   559  	{
   560  		"26959946667150639794667015087019625940457807714424391721682722368056",
   561  		"31c49ae75bce7807cdff22055d94ee9021fedbb5ab51c57526f011aa",
   562  		"d817400e8ba9ca13a45f360e3d121eaaeb39af82d6001c8186f5f866",
   563  	},
   564  	{
   565  		"26959946667150639794667015087019625940457807714424391721682722368057",
   566  		"ae99feebb5d26945b54892092a8aee02912930fa41cd114e40447301",
   567  		"fb7da7f5f13a43b81774373c879cd32d6934c05fa758eeb14fcfab38",
   568  	},
   569  	{
   570  		"26959946667150639794667015087019625940457807714424391721682722368058",
   571  		"df1b1d66a551d0d31eff822558b9d2cc75c2180279fe0d08fd896d04",
   572  		"5c080fc3522f41bbb3f55a97cfecf21f882ce8cbb1e50ca6e67e56dc",
   573  	},
   574  	{
   575  		"26959946667150639794667015087019625940457807714424391721682722368059",
   576  		"706a46dc76dcb76798e60e6d89474788d16dc18032d268fd1a704fa6",
   577  		"e3d4895843da188fd58fb0567976d7b50359d6b78530c8f62d1b1746",
   578  	},
   579  	{
   580  		"26959946667150639794667015087019625940457807714424391721682722368060",
   581  		"b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21",
   582  		"42c89c774a08dc04b3dd201932bc8a5ea5f8b89bbb2a7e667aff81cd",
   583  	},
   584  }
   585  
   586  func TestP224BaseMult(t *testing.T) {
   587  	p224 := P224()
   588  	for i, e := range p224BaseMultTests {
   589  		k, ok := new(big.Int).SetString(e.k, 10)
   590  		if !ok {
   591  			t.Errorf("%d: bad value for k: %s", i, e.k)
   592  		}
   593  		x, y := p224.ScalarBaseMult(k.Bytes())
   594  		if fmt.Sprintf("%x", x) != e.x || fmt.Sprintf("%x", y) != e.y {
   595  			t.Errorf("%d: bad output for k=%s: got (%x, %x), want (%s, %s)", i, e.k, x, y, e.x, e.y)
   596  		}
   597  		if testing.Short() && i > 5 {
   598  			break
   599  		}
   600  	}
   601  }
   602  
   603  func TestP224GenericBaseMult(t *testing.T) {
   604  	// We use the P224 CurveParams directly in order to test the generic implementation.
   605  	p224 := P224().Params()
   606  	for i, e := range p224BaseMultTests {
   607  		k, ok := new(big.Int).SetString(e.k, 10)
   608  		if !ok {
   609  			t.Errorf("%d: bad value for k: %s", i, e.k)
   610  		}
   611  		x, y := p224.ScalarBaseMult(k.Bytes())
   612  		if fmt.Sprintf("%x", x) != e.x || fmt.Sprintf("%x", y) != e.y {
   613  			t.Errorf("%d: bad output for k=%s: got (%x, %x), want (%s, %s)", i, e.k, x, y, e.x, e.y)
   614  		}
   615  		if testing.Short() && i > 5 {
   616  			break
   617  		}
   618  	}
   619  }
   620  
   621  func TestP224Overflow(t *testing.T) {
   622  	// This tests for a specific bug in the P224 implementation.
   623  	p224 := P224()
   624  	pointData, _ := hex.DecodeString("049B535B45FB0A2072398A6831834624C7E32CCFD5A4B933BCEAF77F1DD945E08BBE5178F5EDF5E733388F196D2A631D2E075BB16CBFEEA15B")
   625  	x, y := Unmarshal(p224, pointData)
   626  	if !p224.IsOnCurve(x, y) {
   627  		t.Error("P224 failed to validate a correct point")
   628  	}
   629  }
   630  

View as plain text