github.com/jonasi/go@v0.0.0-20150930005915-e78e654c1de0/src/math/big/int_test.go (about)

     1  // Copyright 2009 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 big
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/hex"
    10  	"fmt"
    11  	"math/rand"
    12  	"testing"
    13  	"testing/quick"
    14  )
    15  
    16  func isNormalized(x *Int) bool {
    17  	if len(x.abs) == 0 {
    18  		return !x.neg
    19  	}
    20  	// len(x.abs) > 0
    21  	return x.abs[len(x.abs)-1] != 0
    22  }
    23  
    24  type funZZ func(z, x, y *Int) *Int
    25  type argZZ struct {
    26  	z, x, y *Int
    27  }
    28  
    29  var sumZZ = []argZZ{
    30  	{NewInt(0), NewInt(0), NewInt(0)},
    31  	{NewInt(1), NewInt(1), NewInt(0)},
    32  	{NewInt(1111111110), NewInt(123456789), NewInt(987654321)},
    33  	{NewInt(-1), NewInt(-1), NewInt(0)},
    34  	{NewInt(864197532), NewInt(-123456789), NewInt(987654321)},
    35  	{NewInt(-1111111110), NewInt(-123456789), NewInt(-987654321)},
    36  }
    37  
    38  var prodZZ = []argZZ{
    39  	{NewInt(0), NewInt(0), NewInt(0)},
    40  	{NewInt(0), NewInt(1), NewInt(0)},
    41  	{NewInt(1), NewInt(1), NewInt(1)},
    42  	{NewInt(-991 * 991), NewInt(991), NewInt(-991)},
    43  	// TODO(gri) add larger products
    44  }
    45  
    46  func TestSignZ(t *testing.T) {
    47  	var zero Int
    48  	for _, a := range sumZZ {
    49  		s := a.z.Sign()
    50  		e := a.z.Cmp(&zero)
    51  		if s != e {
    52  			t.Errorf("got %d; want %d for z = %v", s, e, a.z)
    53  		}
    54  	}
    55  }
    56  
    57  func TestSetZ(t *testing.T) {
    58  	for _, a := range sumZZ {
    59  		var z Int
    60  		z.Set(a.z)
    61  		if !isNormalized(&z) {
    62  			t.Errorf("%v is not normalized", z)
    63  		}
    64  		if (&z).Cmp(a.z) != 0 {
    65  			t.Errorf("got z = %v; want %v", z, a.z)
    66  		}
    67  	}
    68  }
    69  
    70  func TestAbsZ(t *testing.T) {
    71  	var zero Int
    72  	for _, a := range sumZZ {
    73  		var z Int
    74  		z.Abs(a.z)
    75  		var e Int
    76  		e.Set(a.z)
    77  		if e.Cmp(&zero) < 0 {
    78  			e.Sub(&zero, &e)
    79  		}
    80  		if z.Cmp(&e) != 0 {
    81  			t.Errorf("got z = %v; want %v", z, e)
    82  		}
    83  	}
    84  }
    85  
    86  func testFunZZ(t *testing.T, msg string, f funZZ, a argZZ) {
    87  	var z Int
    88  	f(&z, a.x, a.y)
    89  	if !isNormalized(&z) {
    90  		t.Errorf("%s%v is not normalized", msg, z)
    91  	}
    92  	if (&z).Cmp(a.z) != 0 {
    93  		t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, &z, a.z)
    94  	}
    95  }
    96  
    97  func TestSumZZ(t *testing.T) {
    98  	AddZZ := func(z, x, y *Int) *Int { return z.Add(x, y) }
    99  	SubZZ := func(z, x, y *Int) *Int { return z.Sub(x, y) }
   100  	for _, a := range sumZZ {
   101  		arg := a
   102  		testFunZZ(t, "AddZZ", AddZZ, arg)
   103  
   104  		arg = argZZ{a.z, a.y, a.x}
   105  		testFunZZ(t, "AddZZ symmetric", AddZZ, arg)
   106  
   107  		arg = argZZ{a.x, a.z, a.y}
   108  		testFunZZ(t, "SubZZ", SubZZ, arg)
   109  
   110  		arg = argZZ{a.y, a.z, a.x}
   111  		testFunZZ(t, "SubZZ symmetric", SubZZ, arg)
   112  	}
   113  }
   114  
   115  func TestProdZZ(t *testing.T) {
   116  	MulZZ := func(z, x, y *Int) *Int { return z.Mul(x, y) }
   117  	for _, a := range prodZZ {
   118  		arg := a
   119  		testFunZZ(t, "MulZZ", MulZZ, arg)
   120  
   121  		arg = argZZ{a.z, a.y, a.x}
   122  		testFunZZ(t, "MulZZ symmetric", MulZZ, arg)
   123  	}
   124  }
   125  
   126  // mulBytes returns x*y via grade school multiplication. Both inputs
   127  // and the result are assumed to be in big-endian representation (to
   128  // match the semantics of Int.Bytes and Int.SetBytes).
   129  func mulBytes(x, y []byte) []byte {
   130  	z := make([]byte, len(x)+len(y))
   131  
   132  	// multiply
   133  	k0 := len(z) - 1
   134  	for j := len(y) - 1; j >= 0; j-- {
   135  		d := int(y[j])
   136  		if d != 0 {
   137  			k := k0
   138  			carry := 0
   139  			for i := len(x) - 1; i >= 0; i-- {
   140  				t := int(z[k]) + int(x[i])*d + carry
   141  				z[k], carry = byte(t), t>>8
   142  				k--
   143  			}
   144  			z[k] = byte(carry)
   145  		}
   146  		k0--
   147  	}
   148  
   149  	// normalize (remove leading 0's)
   150  	i := 0
   151  	for i < len(z) && z[i] == 0 {
   152  		i++
   153  	}
   154  
   155  	return z[i:]
   156  }
   157  
   158  func checkMul(a, b []byte) bool {
   159  	var x, y, z1 Int
   160  	x.SetBytes(a)
   161  	y.SetBytes(b)
   162  	z1.Mul(&x, &y)
   163  
   164  	var z2 Int
   165  	z2.SetBytes(mulBytes(a, b))
   166  
   167  	return z1.Cmp(&z2) == 0
   168  }
   169  
   170  func TestMul(t *testing.T) {
   171  	if err := quick.Check(checkMul, nil); err != nil {
   172  		t.Error(err)
   173  	}
   174  }
   175  
   176  var mulRangesZ = []struct {
   177  	a, b int64
   178  	prod string
   179  }{
   180  	// entirely positive ranges are covered by mulRangesN
   181  	{-1, 1, "0"},
   182  	{-2, -1, "2"},
   183  	{-3, -2, "6"},
   184  	{-3, -1, "-6"},
   185  	{1, 3, "6"},
   186  	{-10, -10, "-10"},
   187  	{0, -1, "1"},                      // empty range
   188  	{-1, -100, "1"},                   // empty range
   189  	{-1, 1, "0"},                      // range includes 0
   190  	{-1e9, 0, "0"},                    // range includes 0
   191  	{-1e9, 1e9, "0"},                  // range includes 0
   192  	{-10, -1, "3628800"},              // 10!
   193  	{-20, -2, "-2432902008176640000"}, // -20!
   194  	{-99, -1,
   195  		"-933262154439441526816992388562667004907159682643816214685929" +
   196  			"638952175999932299156089414639761565182862536979208272237582" +
   197  			"511852109168640000000000000000000000", // -99!
   198  	},
   199  }
   200  
   201  func TestMulRangeZ(t *testing.T) {
   202  	var tmp Int
   203  	// test entirely positive ranges
   204  	for i, r := range mulRangesN {
   205  		prod := tmp.MulRange(int64(r.a), int64(r.b)).String()
   206  		if prod != r.prod {
   207  			t.Errorf("#%da: got %s; want %s", i, prod, r.prod)
   208  		}
   209  	}
   210  	// test other ranges
   211  	for i, r := range mulRangesZ {
   212  		prod := tmp.MulRange(r.a, r.b).String()
   213  		if prod != r.prod {
   214  			t.Errorf("#%db: got %s; want %s", i, prod, r.prod)
   215  		}
   216  	}
   217  }
   218  
   219  func TestBinomial(t *testing.T) {
   220  	var z Int
   221  	for _, test := range []struct {
   222  		n, k int64
   223  		want string
   224  	}{
   225  		{0, 0, "1"},
   226  		{0, 1, "0"},
   227  		{1, 0, "1"},
   228  		{1, 1, "1"},
   229  		{1, 10, "0"},
   230  		{4, 0, "1"},
   231  		{4, 1, "4"},
   232  		{4, 2, "6"},
   233  		{4, 3, "4"},
   234  		{4, 4, "1"},
   235  		{10, 1, "10"},
   236  		{10, 9, "10"},
   237  		{10, 5, "252"},
   238  		{11, 5, "462"},
   239  		{11, 6, "462"},
   240  		{100, 10, "17310309456440"},
   241  		{100, 90, "17310309456440"},
   242  		{1000, 10, "263409560461970212832400"},
   243  		{1000, 990, "263409560461970212832400"},
   244  	} {
   245  		if got := z.Binomial(test.n, test.k).String(); got != test.want {
   246  			t.Errorf("Binomial(%d, %d) = %s; want %s", test.n, test.k, got, test.want)
   247  		}
   248  	}
   249  }
   250  
   251  func BenchmarkBinomial(b *testing.B) {
   252  	var z Int
   253  	for i := b.N - 1; i >= 0; i-- {
   254  		z.Binomial(1000, 990)
   255  	}
   256  }
   257  
   258  // Examples from the Go Language Spec, section "Arithmetic operators"
   259  var divisionSignsTests = []struct {
   260  	x, y int64
   261  	q, r int64 // T-division
   262  	d, m int64 // Euclidian division
   263  }{
   264  	{5, 3, 1, 2, 1, 2},
   265  	{-5, 3, -1, -2, -2, 1},
   266  	{5, -3, -1, 2, -1, 2},
   267  	{-5, -3, 1, -2, 2, 1},
   268  	{1, 2, 0, 1, 0, 1},
   269  	{8, 4, 2, 0, 2, 0},
   270  }
   271  
   272  func TestDivisionSigns(t *testing.T) {
   273  	for i, test := range divisionSignsTests {
   274  		x := NewInt(test.x)
   275  		y := NewInt(test.y)
   276  		q := NewInt(test.q)
   277  		r := NewInt(test.r)
   278  		d := NewInt(test.d)
   279  		m := NewInt(test.m)
   280  
   281  		q1 := new(Int).Quo(x, y)
   282  		r1 := new(Int).Rem(x, y)
   283  		if !isNormalized(q1) {
   284  			t.Errorf("#%d Quo: %v is not normalized", i, *q1)
   285  		}
   286  		if !isNormalized(r1) {
   287  			t.Errorf("#%d Rem: %v is not normalized", i, *r1)
   288  		}
   289  		if q1.Cmp(q) != 0 || r1.Cmp(r) != 0 {
   290  			t.Errorf("#%d QuoRem: got (%s, %s), want (%s, %s)", i, q1, r1, q, r)
   291  		}
   292  
   293  		q2, r2 := new(Int).QuoRem(x, y, new(Int))
   294  		if !isNormalized(q2) {
   295  			t.Errorf("#%d Quo: %v is not normalized", i, *q2)
   296  		}
   297  		if !isNormalized(r2) {
   298  			t.Errorf("#%d Rem: %v is not normalized", i, *r2)
   299  		}
   300  		if q2.Cmp(q) != 0 || r2.Cmp(r) != 0 {
   301  			t.Errorf("#%d QuoRem: got (%s, %s), want (%s, %s)", i, q2, r2, q, r)
   302  		}
   303  
   304  		d1 := new(Int).Div(x, y)
   305  		m1 := new(Int).Mod(x, y)
   306  		if !isNormalized(d1) {
   307  			t.Errorf("#%d Div: %v is not normalized", i, *d1)
   308  		}
   309  		if !isNormalized(m1) {
   310  			t.Errorf("#%d Mod: %v is not normalized", i, *m1)
   311  		}
   312  		if d1.Cmp(d) != 0 || m1.Cmp(m) != 0 {
   313  			t.Errorf("#%d DivMod: got (%s, %s), want (%s, %s)", i, d1, m1, d, m)
   314  		}
   315  
   316  		d2, m2 := new(Int).DivMod(x, y, new(Int))
   317  		if !isNormalized(d2) {
   318  			t.Errorf("#%d Div: %v is not normalized", i, *d2)
   319  		}
   320  		if !isNormalized(m2) {
   321  			t.Errorf("#%d Mod: %v is not normalized", i, *m2)
   322  		}
   323  		if d2.Cmp(d) != 0 || m2.Cmp(m) != 0 {
   324  			t.Errorf("#%d DivMod: got (%s, %s), want (%s, %s)", i, d2, m2, d, m)
   325  		}
   326  	}
   327  }
   328  
   329  func norm(x nat) nat {
   330  	i := len(x)
   331  	for i > 0 && x[i-1] == 0 {
   332  		i--
   333  	}
   334  	return x[:i]
   335  }
   336  
   337  func TestBits(t *testing.T) {
   338  	for _, test := range []nat{
   339  		nil,
   340  		{0},
   341  		{1},
   342  		{0, 1, 2, 3, 4},
   343  		{4, 3, 2, 1, 0},
   344  		{4, 3, 2, 1, 0, 0, 0, 0},
   345  	} {
   346  		var z Int
   347  		z.neg = true
   348  		got := z.SetBits(test)
   349  		want := norm(test)
   350  		if got.abs.cmp(want) != 0 {
   351  			t.Errorf("SetBits(%v) = %v; want %v", test, got.abs, want)
   352  		}
   353  
   354  		if got.neg {
   355  			t.Errorf("SetBits(%v): got negative result", test)
   356  		}
   357  
   358  		bits := nat(z.Bits())
   359  		if bits.cmp(want) != 0 {
   360  			t.Errorf("%v.Bits() = %v; want %v", z.abs, bits, want)
   361  		}
   362  	}
   363  }
   364  
   365  func checkSetBytes(b []byte) bool {
   366  	hex1 := hex.EncodeToString(new(Int).SetBytes(b).Bytes())
   367  	hex2 := hex.EncodeToString(b)
   368  
   369  	for len(hex1) < len(hex2) {
   370  		hex1 = "0" + hex1
   371  	}
   372  
   373  	for len(hex1) > len(hex2) {
   374  		hex2 = "0" + hex2
   375  	}
   376  
   377  	return hex1 == hex2
   378  }
   379  
   380  func TestSetBytes(t *testing.T) {
   381  	if err := quick.Check(checkSetBytes, nil); err != nil {
   382  		t.Error(err)
   383  	}
   384  }
   385  
   386  func checkBytes(b []byte) bool {
   387  	// trim leading zero bytes since Bytes() won't return them
   388  	// (was issue 12231)
   389  	for len(b) > 0 && b[0] == 0 {
   390  		b = b[1:]
   391  	}
   392  	b2 := new(Int).SetBytes(b).Bytes()
   393  	return bytes.Equal(b, b2)
   394  }
   395  
   396  func TestBytes(t *testing.T) {
   397  	if err := quick.Check(checkBytes, nil); err != nil {
   398  		t.Error(err)
   399  	}
   400  }
   401  
   402  func checkQuo(x, y []byte) bool {
   403  	u := new(Int).SetBytes(x)
   404  	v := new(Int).SetBytes(y)
   405  
   406  	if len(v.abs) == 0 {
   407  		return true
   408  	}
   409  
   410  	r := new(Int)
   411  	q, r := new(Int).QuoRem(u, v, r)
   412  
   413  	if r.Cmp(v) >= 0 {
   414  		return false
   415  	}
   416  
   417  	uprime := new(Int).Set(q)
   418  	uprime.Mul(uprime, v)
   419  	uprime.Add(uprime, r)
   420  
   421  	return uprime.Cmp(u) == 0
   422  }
   423  
   424  var quoTests = []struct {
   425  	x, y string
   426  	q, r string
   427  }{
   428  	{
   429  		"476217953993950760840509444250624797097991362735329973741718102894495832294430498335824897858659711275234906400899559094370964723884706254265559534144986498357",
   430  		"9353930466774385905609975137998169297361893554149986716853295022578535724979483772383667534691121982974895531435241089241440253066816724367338287092081996",
   431  		"50911",
   432  		"1",
   433  	},
   434  	{
   435  		"11510768301994997771168",
   436  		"1328165573307167369775",
   437  		"8",
   438  		"885443715537658812968",
   439  	},
   440  }
   441  
   442  func TestQuo(t *testing.T) {
   443  	if err := quick.Check(checkQuo, nil); err != nil {
   444  		t.Error(err)
   445  	}
   446  
   447  	for i, test := range quoTests {
   448  		x, _ := new(Int).SetString(test.x, 10)
   449  		y, _ := new(Int).SetString(test.y, 10)
   450  		expectedQ, _ := new(Int).SetString(test.q, 10)
   451  		expectedR, _ := new(Int).SetString(test.r, 10)
   452  
   453  		r := new(Int)
   454  		q, r := new(Int).QuoRem(x, y, r)
   455  
   456  		if q.Cmp(expectedQ) != 0 || r.Cmp(expectedR) != 0 {
   457  			t.Errorf("#%d got (%s, %s) want (%s, %s)", i, q, r, expectedQ, expectedR)
   458  		}
   459  	}
   460  }
   461  
   462  func TestQuoStepD6(t *testing.T) {
   463  	// See Knuth, Volume 2, section 4.3.1, exercise 21. This code exercises
   464  	// a code path which only triggers 1 in 10^{-19} cases.
   465  
   466  	u := &Int{false, nat{0, 0, 1 + 1<<(_W-1), _M ^ (1 << (_W - 1))}}
   467  	v := &Int{false, nat{5, 2 + 1<<(_W-1), 1 << (_W - 1)}}
   468  
   469  	r := new(Int)
   470  	q, r := new(Int).QuoRem(u, v, r)
   471  	const expectedQ64 = "18446744073709551613"
   472  	const expectedR64 = "3138550867693340382088035895064302439801311770021610913807"
   473  	const expectedQ32 = "4294967293"
   474  	const expectedR32 = "39614081266355540837921718287"
   475  	if q.String() != expectedQ64 && q.String() != expectedQ32 ||
   476  		r.String() != expectedR64 && r.String() != expectedR32 {
   477  		t.Errorf("got (%s, %s) want (%s, %s) or (%s, %s)", q, r, expectedQ64, expectedR64, expectedQ32, expectedR32)
   478  	}
   479  }
   480  
   481  var bitLenTests = []struct {
   482  	in  string
   483  	out int
   484  }{
   485  	{"-1", 1},
   486  	{"0", 0},
   487  	{"1", 1},
   488  	{"2", 2},
   489  	{"4", 3},
   490  	{"0xabc", 12},
   491  	{"0x8000", 16},
   492  	{"0x80000000", 32},
   493  	{"0x800000000000", 48},
   494  	{"0x8000000000000000", 64},
   495  	{"0x80000000000000000000", 80},
   496  	{"-0x4000000000000000000000", 87},
   497  }
   498  
   499  func TestBitLen(t *testing.T) {
   500  	for i, test := range bitLenTests {
   501  		x, ok := new(Int).SetString(test.in, 0)
   502  		if !ok {
   503  			t.Errorf("#%d test input invalid: %s", i, test.in)
   504  			continue
   505  		}
   506  
   507  		if n := x.BitLen(); n != test.out {
   508  			t.Errorf("#%d got %d want %d", i, n, test.out)
   509  		}
   510  	}
   511  }
   512  
   513  var expTests = []struct {
   514  	x, y, m string
   515  	out     string
   516  }{
   517  	// y <= 0
   518  	{"0", "0", "", "1"},
   519  	{"1", "0", "", "1"},
   520  	{"-10", "0", "", "1"},
   521  	{"1234", "-1", "", "1"},
   522  
   523  	// m == 1
   524  	{"0", "0", "1", "0"},
   525  	{"1", "0", "1", "0"},
   526  	{"-10", "0", "1", "0"},
   527  	{"1234", "-1", "1", "0"},
   528  
   529  	// misc
   530  	{"5", "1", "3", "2"},
   531  	{"5", "-7", "", "1"},
   532  	{"-5", "-7", "", "1"},
   533  	{"5", "0", "", "1"},
   534  	{"-5", "0", "", "1"},
   535  	{"5", "1", "", "5"},
   536  	{"-5", "1", "", "-5"},
   537  	{"-5", "1", "7", "2"},
   538  	{"-2", "3", "2", "0"},
   539  	{"5", "2", "", "25"},
   540  	{"1", "65537", "2", "1"},
   541  	{"0x8000000000000000", "2", "", "0x40000000000000000000000000000000"},
   542  	{"0x8000000000000000", "2", "6719", "4944"},
   543  	{"0x8000000000000000", "3", "6719", "5447"},
   544  	{"0x8000000000000000", "1000", "6719", "1603"},
   545  	{"0x8000000000000000", "1000000", "6719", "3199"},
   546  	{"0x8000000000000000", "-1000000", "6719", "1"},
   547  	{
   548  		"2938462938472983472983659726349017249287491026512746239764525612965293865296239471239874193284792387498274256129746192347",
   549  		"298472983472983471903246121093472394872319615612417471234712061",
   550  		"29834729834729834729347290846729561262544958723956495615629569234729836259263598127342374289365912465901365498236492183464",
   551  		"23537740700184054162508175125554701713153216681790245129157191391322321508055833908509185839069455749219131480588829346291",
   552  	},
   553  	// test case for issue 8822
   554  	{
   555  		"-0x1BCE04427D8032319A89E5C4136456671AC620883F2C4139E57F91307C485AD2D6204F4F87A58262652DB5DBBAC72B0613E51B835E7153BEC6068F5C8D696B74DBD18FEC316AEF73985CF0475663208EB46B4F17DD9DA55367B03323E5491A70997B90C059FB34809E6EE55BCFBD5F2F52233BFE62E6AA9E4E26A1D4C2439883D14F2633D55D8AA66A1ACD5595E778AC3A280517F1157989E70C1A437B849F1877B779CC3CDDEDE2DAA6594A6C66D181A00A5F777EE60596D8773998F6E988DEAE4CCA60E4DDCF9590543C89F74F603259FCAD71660D30294FBBE6490300F78A9D63FA660DC9417B8B9DDA28BEB3977B621B988E23D4D954F322C3540541BC649ABD504C50FADFD9F0987D58A2BF689313A285E773FF02899A6EF887D1D4A0D2",
   556  		"0xB08FFB20760FFED58FADA86DFEF71AD72AA0FA763219618FE022C197E54708BB1191C66470250FCE8879487507CEE41381CA4D932F81C2B3F1AB20B539D50DCD",
   557  		"0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73",
   558  		"21484252197776302499639938883777710321993113097987201050501182909581359357618579566746556372589385361683610524730509041328855066514963385522570894839035884713051640171474186548713546686476761306436434146475140156284389181808675016576845833340494848283681088886584219750554408060556769486628029028720727393293111678826356480455433909233520504112074401376133077150471237549474149190242010469539006449596611576612573955754349042329130631128234637924786466585703488460540228477440853493392086251021228087076124706778899179648655221663765993962724699135217212118535057766739392069738618682722216712319320435674779146070442",
   559  	},
   560  }
   561  
   562  func TestExp(t *testing.T) {
   563  	for i, test := range expTests {
   564  		x, ok1 := new(Int).SetString(test.x, 0)
   565  		y, ok2 := new(Int).SetString(test.y, 0)
   566  		out, ok3 := new(Int).SetString(test.out, 0)
   567  
   568  		var ok4 bool
   569  		var m *Int
   570  
   571  		if len(test.m) == 0 {
   572  			m, ok4 = nil, true
   573  		} else {
   574  			m, ok4 = new(Int).SetString(test.m, 0)
   575  		}
   576  
   577  		if !ok1 || !ok2 || !ok3 || !ok4 {
   578  			t.Errorf("#%d: error in input", i)
   579  			continue
   580  		}
   581  
   582  		z1 := new(Int).Exp(x, y, m)
   583  		if !isNormalized(z1) {
   584  			t.Errorf("#%d: %v is not normalized", i, *z1)
   585  		}
   586  		if z1.Cmp(out) != 0 {
   587  			t.Errorf("#%d: got %s want %s", i, z1, out)
   588  		}
   589  
   590  		if m == nil {
   591  			// The result should be the same as for m == 0;
   592  			// specifically, there should be no div-zero panic.
   593  			m = &Int{abs: nat{}} // m != nil && len(m.abs) == 0
   594  			z2 := new(Int).Exp(x, y, m)
   595  			if z2.Cmp(z1) != 0 {
   596  				t.Errorf("#%d: got %s want %s", i, z2, z1)
   597  			}
   598  		}
   599  	}
   600  }
   601  
   602  func checkGcd(aBytes, bBytes []byte) bool {
   603  	x := new(Int)
   604  	y := new(Int)
   605  	a := new(Int).SetBytes(aBytes)
   606  	b := new(Int).SetBytes(bBytes)
   607  
   608  	d := new(Int).GCD(x, y, a, b)
   609  	x.Mul(x, a)
   610  	y.Mul(y, b)
   611  	x.Add(x, y)
   612  
   613  	return x.Cmp(d) == 0
   614  }
   615  
   616  var gcdTests = []struct {
   617  	d, x, y, a, b string
   618  }{
   619  	// a <= 0 || b <= 0
   620  	{"0", "0", "0", "0", "0"},
   621  	{"0", "0", "0", "0", "7"},
   622  	{"0", "0", "0", "11", "0"},
   623  	{"0", "0", "0", "-77", "35"},
   624  	{"0", "0", "0", "64515", "-24310"},
   625  	{"0", "0", "0", "-64515", "-24310"},
   626  
   627  	{"1", "-9", "47", "120", "23"},
   628  	{"7", "1", "-2", "77", "35"},
   629  	{"935", "-3", "8", "64515", "24310"},
   630  	{"935000000000000000", "-3", "8", "64515000000000000000", "24310000000000000000"},
   631  	{"1", "-221", "22059940471369027483332068679400581064239780177629666810348940098015901108344", "98920366548084643601728869055592650835572950932266967461790948584315647051443", "991"},
   632  
   633  	// test early exit (after one Euclidean iteration) in binaryGCD
   634  	{"1", "", "", "1", "98920366548084643601728869055592650835572950932266967461790948584315647051443"},
   635  }
   636  
   637  func testGcd(t *testing.T, d, x, y, a, b *Int) {
   638  	var X *Int
   639  	if x != nil {
   640  		X = new(Int)
   641  	}
   642  	var Y *Int
   643  	if y != nil {
   644  		Y = new(Int)
   645  	}
   646  
   647  	D := new(Int).GCD(X, Y, a, b)
   648  	if D.Cmp(d) != 0 {
   649  		t.Errorf("GCD(%s, %s): got d = %s, want %s", a, b, D, d)
   650  	}
   651  	if x != nil && X.Cmp(x) != 0 {
   652  		t.Errorf("GCD(%s, %s): got x = %s, want %s", a, b, X, x)
   653  	}
   654  	if y != nil && Y.Cmp(y) != 0 {
   655  		t.Errorf("GCD(%s, %s): got y = %s, want %s", a, b, Y, y)
   656  	}
   657  
   658  	// binaryGCD requires a > 0 && b > 0
   659  	if a.Sign() <= 0 || b.Sign() <= 0 {
   660  		return
   661  	}
   662  
   663  	D.binaryGCD(a, b)
   664  	if D.Cmp(d) != 0 {
   665  		t.Errorf("binaryGcd(%s, %s): got d = %s, want %s", a, b, D, d)
   666  	}
   667  
   668  	// check results in presence of aliasing (issue #11284)
   669  	a2 := new(Int).Set(a)
   670  	b2 := new(Int).Set(b)
   671  	a2.binaryGCD(a2, b2) // result is same as 1st argument
   672  	if a2.Cmp(d) != 0 {
   673  		t.Errorf("binaryGcd(%s, %s): got d = %s, want %s", a, b, a2, d)
   674  	}
   675  
   676  	a2 = new(Int).Set(a)
   677  	b2 = new(Int).Set(b)
   678  	b2.binaryGCD(a2, b2) // result is same as 2nd argument
   679  	if b2.Cmp(d) != 0 {
   680  		t.Errorf("binaryGcd(%s, %s): got d = %s, want %s", a, b, b2, d)
   681  	}
   682  }
   683  
   684  func TestGcd(t *testing.T) {
   685  	for _, test := range gcdTests {
   686  		d, _ := new(Int).SetString(test.d, 0)
   687  		x, _ := new(Int).SetString(test.x, 0)
   688  		y, _ := new(Int).SetString(test.y, 0)
   689  		a, _ := new(Int).SetString(test.a, 0)
   690  		b, _ := new(Int).SetString(test.b, 0)
   691  
   692  		testGcd(t, d, nil, nil, a, b)
   693  		testGcd(t, d, x, nil, a, b)
   694  		testGcd(t, d, nil, y, a, b)
   695  		testGcd(t, d, x, y, a, b)
   696  	}
   697  
   698  	quick.Check(checkGcd, nil)
   699  }
   700  
   701  var primes = []string{
   702  	"2",
   703  	"3",
   704  	"5",
   705  	"7",
   706  	"11",
   707  
   708  	"13756265695458089029",
   709  	"13496181268022124907",
   710  	"10953742525620032441",
   711  	"17908251027575790097",
   712  
   713  	// https://golang.org/issue/638
   714  	"18699199384836356663",
   715  
   716  	"98920366548084643601728869055592650835572950932266967461790948584315647051443",
   717  	"94560208308847015747498523884063394671606671904944666360068158221458669711639",
   718  
   719  	// http://primes.utm.edu/lists/small/small3.html
   720  	"449417999055441493994709297093108513015373787049558499205492347871729927573118262811508386655998299074566974373711472560655026288668094291699357843464363003144674940345912431129144354948751003607115263071543163",
   721  	"230975859993204150666423538988557839555560243929065415434980904258310530753006723857139742334640122533598517597674807096648905501653461687601339782814316124971547968912893214002992086353183070342498989426570593",
   722  	"5521712099665906221540423207019333379125265462121169655563495403888449493493629943498064604536961775110765377745550377067893607246020694972959780839151452457728855382113555867743022746090187341871655890805971735385789993",
   723  	"203956878356401977405765866929034577280193993314348263094772646453283062722701277632936616063144088173312372882677123879538709400158306567338328279154499698366071906766440037074217117805690872792848149112022286332144876183376326512083574821647933992961249917319836219304274280243803104015000563790123",
   724  
   725  	// ECC primes: http://tools.ietf.org/html/draft-ladd-safecurves-02
   726  	"3618502788666131106986593281521497120414687020801267626233049500247285301239",                                                                                  // Curve1174: 2^251-9
   727  	"57896044618658097711785492504343953926634992332820282019728792003956564819949",                                                                                 // Curve25519: 2^255-19
   728  	"9850501549098619803069760025035903451269934817616361666987073351061430442874302652853566563721228910201656997576599",                                           // E-382: 2^382-105
   729  	"42307582002575910332922579714097346549017899709713998034217522897561970639123926132812109468141778230245837569601494931472367",                                 // Curve41417: 2^414-17
   730  	"6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151", // E-521: 2^521-1
   731  }
   732  
   733  var composites = []string{
   734  	"0",
   735  	"1",
   736  	"21284175091214687912771199898307297748211672914763848041968395774954376176754",
   737  	"6084766654921918907427900243509372380954290099172559290432744450051395395951",
   738  	"84594350493221918389213352992032324280367711247940675652888030554255915464401",
   739  	"82793403787388584738507275144194252681",
   740  }
   741  
   742  func TestProbablyPrime(t *testing.T) {
   743  	nreps := 20
   744  	if testing.Short() {
   745  		nreps = 1
   746  	}
   747  	for i, s := range primes {
   748  		p, _ := new(Int).SetString(s, 10)
   749  		if !p.ProbablyPrime(nreps) {
   750  			t.Errorf("#%d prime found to be non-prime (%s)", i, s)
   751  		}
   752  	}
   753  
   754  	for i, s := range composites {
   755  		c, _ := new(Int).SetString(s, 10)
   756  		if c.ProbablyPrime(nreps) {
   757  			t.Errorf("#%d composite found to be prime (%s)", i, s)
   758  		}
   759  		if testing.Short() {
   760  			break
   761  		}
   762  	}
   763  
   764  	// check that ProbablyPrime panics if n <= 0
   765  	c := NewInt(11) // a prime
   766  	for _, n := range []int{-1, 0, 1} {
   767  		func() {
   768  			defer func() {
   769  				if n <= 0 && recover() == nil {
   770  					t.Fatalf("expected panic from ProbablyPrime(%d)", n)
   771  				}
   772  			}()
   773  			if !c.ProbablyPrime(n) {
   774  				t.Fatalf("%v should be a prime", c)
   775  			}
   776  		}()
   777  	}
   778  }
   779  
   780  type intShiftTest struct {
   781  	in    string
   782  	shift uint
   783  	out   string
   784  }
   785  
   786  var rshTests = []intShiftTest{
   787  	{"0", 0, "0"},
   788  	{"-0", 0, "0"},
   789  	{"0", 1, "0"},
   790  	{"0", 2, "0"},
   791  	{"1", 0, "1"},
   792  	{"1", 1, "0"},
   793  	{"1", 2, "0"},
   794  	{"2", 0, "2"},
   795  	{"2", 1, "1"},
   796  	{"-1", 0, "-1"},
   797  	{"-1", 1, "-1"},
   798  	{"-1", 10, "-1"},
   799  	{"-100", 2, "-25"},
   800  	{"-100", 3, "-13"},
   801  	{"-100", 100, "-1"},
   802  	{"4294967296", 0, "4294967296"},
   803  	{"4294967296", 1, "2147483648"},
   804  	{"4294967296", 2, "1073741824"},
   805  	{"18446744073709551616", 0, "18446744073709551616"},
   806  	{"18446744073709551616", 1, "9223372036854775808"},
   807  	{"18446744073709551616", 2, "4611686018427387904"},
   808  	{"18446744073709551616", 64, "1"},
   809  	{"340282366920938463463374607431768211456", 64, "18446744073709551616"},
   810  	{"340282366920938463463374607431768211456", 128, "1"},
   811  }
   812  
   813  func TestRsh(t *testing.T) {
   814  	for i, test := range rshTests {
   815  		in, _ := new(Int).SetString(test.in, 10)
   816  		expected, _ := new(Int).SetString(test.out, 10)
   817  		out := new(Int).Rsh(in, test.shift)
   818  
   819  		if !isNormalized(out) {
   820  			t.Errorf("#%d: %v is not normalized", i, *out)
   821  		}
   822  		if out.Cmp(expected) != 0 {
   823  			t.Errorf("#%d: got %s want %s", i, out, expected)
   824  		}
   825  	}
   826  }
   827  
   828  func TestRshSelf(t *testing.T) {
   829  	for i, test := range rshTests {
   830  		z, _ := new(Int).SetString(test.in, 10)
   831  		expected, _ := new(Int).SetString(test.out, 10)
   832  		z.Rsh(z, test.shift)
   833  
   834  		if !isNormalized(z) {
   835  			t.Errorf("#%d: %v is not normalized", i, *z)
   836  		}
   837  		if z.Cmp(expected) != 0 {
   838  			t.Errorf("#%d: got %s want %s", i, z, expected)
   839  		}
   840  	}
   841  }
   842  
   843  var lshTests = []intShiftTest{
   844  	{"0", 0, "0"},
   845  	{"0", 1, "0"},
   846  	{"0", 2, "0"},
   847  	{"1", 0, "1"},
   848  	{"1", 1, "2"},
   849  	{"1", 2, "4"},
   850  	{"2", 0, "2"},
   851  	{"2", 1, "4"},
   852  	{"2", 2, "8"},
   853  	{"-87", 1, "-174"},
   854  	{"4294967296", 0, "4294967296"},
   855  	{"4294967296", 1, "8589934592"},
   856  	{"4294967296", 2, "17179869184"},
   857  	{"18446744073709551616", 0, "18446744073709551616"},
   858  	{"9223372036854775808", 1, "18446744073709551616"},
   859  	{"4611686018427387904", 2, "18446744073709551616"},
   860  	{"1", 64, "18446744073709551616"},
   861  	{"18446744073709551616", 64, "340282366920938463463374607431768211456"},
   862  	{"1", 128, "340282366920938463463374607431768211456"},
   863  }
   864  
   865  func TestLsh(t *testing.T) {
   866  	for i, test := range lshTests {
   867  		in, _ := new(Int).SetString(test.in, 10)
   868  		expected, _ := new(Int).SetString(test.out, 10)
   869  		out := new(Int).Lsh(in, test.shift)
   870  
   871  		if !isNormalized(out) {
   872  			t.Errorf("#%d: %v is not normalized", i, *out)
   873  		}
   874  		if out.Cmp(expected) != 0 {
   875  			t.Errorf("#%d: got %s want %s", i, out, expected)
   876  		}
   877  	}
   878  }
   879  
   880  func TestLshSelf(t *testing.T) {
   881  	for i, test := range lshTests {
   882  		z, _ := new(Int).SetString(test.in, 10)
   883  		expected, _ := new(Int).SetString(test.out, 10)
   884  		z.Lsh(z, test.shift)
   885  
   886  		if !isNormalized(z) {
   887  			t.Errorf("#%d: %v is not normalized", i, *z)
   888  		}
   889  		if z.Cmp(expected) != 0 {
   890  			t.Errorf("#%d: got %s want %s", i, z, expected)
   891  		}
   892  	}
   893  }
   894  
   895  func TestLshRsh(t *testing.T) {
   896  	for i, test := range rshTests {
   897  		in, _ := new(Int).SetString(test.in, 10)
   898  		out := new(Int).Lsh(in, test.shift)
   899  		out = out.Rsh(out, test.shift)
   900  
   901  		if !isNormalized(out) {
   902  			t.Errorf("#%d: %v is not normalized", i, *out)
   903  		}
   904  		if in.Cmp(out) != 0 {
   905  			t.Errorf("#%d: got %s want %s", i, out, in)
   906  		}
   907  	}
   908  	for i, test := range lshTests {
   909  		in, _ := new(Int).SetString(test.in, 10)
   910  		out := new(Int).Lsh(in, test.shift)
   911  		out.Rsh(out, test.shift)
   912  
   913  		if !isNormalized(out) {
   914  			t.Errorf("#%d: %v is not normalized", i, *out)
   915  		}
   916  		if in.Cmp(out) != 0 {
   917  			t.Errorf("#%d: got %s want %s", i, out, in)
   918  		}
   919  	}
   920  }
   921  
   922  var int64Tests = []int64{
   923  	0,
   924  	1,
   925  	-1,
   926  	4294967295,
   927  	-4294967295,
   928  	4294967296,
   929  	-4294967296,
   930  	9223372036854775807,
   931  	-9223372036854775807,
   932  	-9223372036854775808,
   933  }
   934  
   935  func TestInt64(t *testing.T) {
   936  	for i, testVal := range int64Tests {
   937  		in := NewInt(testVal)
   938  		out := in.Int64()
   939  
   940  		if out != testVal {
   941  			t.Errorf("#%d got %d want %d", i, out, testVal)
   942  		}
   943  	}
   944  }
   945  
   946  var uint64Tests = []uint64{
   947  	0,
   948  	1,
   949  	4294967295,
   950  	4294967296,
   951  	8589934591,
   952  	8589934592,
   953  	9223372036854775807,
   954  	9223372036854775808,
   955  	18446744073709551615, // 1<<64 - 1
   956  }
   957  
   958  func TestUint64(t *testing.T) {
   959  	in := new(Int)
   960  	for i, testVal := range uint64Tests {
   961  		in.SetUint64(testVal)
   962  		out := in.Uint64()
   963  
   964  		if out != testVal {
   965  			t.Errorf("#%d got %d want %d", i, out, testVal)
   966  		}
   967  
   968  		str := fmt.Sprint(testVal)
   969  		strOut := in.String()
   970  		if strOut != str {
   971  			t.Errorf("#%d.String got %s want %s", i, strOut, str)
   972  		}
   973  	}
   974  }
   975  
   976  var bitwiseTests = []struct {
   977  	x, y                 string
   978  	and, or, xor, andNot string
   979  }{
   980  	{"0x00", "0x00", "0x00", "0x00", "0x00", "0x00"},
   981  	{"0x00", "0x01", "0x00", "0x01", "0x01", "0x00"},
   982  	{"0x01", "0x00", "0x00", "0x01", "0x01", "0x01"},
   983  	{"-0x01", "0x00", "0x00", "-0x01", "-0x01", "-0x01"},
   984  	{"-0xaf", "-0x50", "-0xf0", "-0x0f", "0xe1", "0x41"},
   985  	{"0x00", "-0x01", "0x00", "-0x01", "-0x01", "0x00"},
   986  	{"0x01", "0x01", "0x01", "0x01", "0x00", "0x00"},
   987  	{"-0x01", "-0x01", "-0x01", "-0x01", "0x00", "0x00"},
   988  	{"0x07", "0x08", "0x00", "0x0f", "0x0f", "0x07"},
   989  	{"0x05", "0x0f", "0x05", "0x0f", "0x0a", "0x00"},
   990  	{"0xff", "-0x0a", "0xf6", "-0x01", "-0xf7", "0x09"},
   991  	{"0x013ff6", "0x9a4e", "0x1a46", "0x01bffe", "0x01a5b8", "0x0125b0"},
   992  	{"-0x013ff6", "0x9a4e", "0x800a", "-0x0125b2", "-0x01a5bc", "-0x01c000"},
   993  	{"-0x013ff6", "-0x9a4e", "-0x01bffe", "-0x1a46", "0x01a5b8", "0x8008"},
   994  	{
   995  		"0x1000009dc6e3d9822cba04129bcbe3401",
   996  		"0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
   997  		"0x1000001186210100001000009048c2001",
   998  		"0xb9bd7d543685789d57cb918e8bfeff7fddb2ebe87dfbbdfe35fd",
   999  		"0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fc",
  1000  		"0x8c40c2d8822caa04120b8321400",
  1001  	},
  1002  	{
  1003  		"0x1000009dc6e3d9822cba04129bcbe3401",
  1004  		"-0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
  1005  		"0x8c40c2d8822caa04120b8321401",
  1006  		"-0xb9bd7d543685789d57ca918e82229142459020483cd2014001fd",
  1007  		"-0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fe",
  1008  		"0x1000001186210100001000009048c2000",
  1009  	},
  1010  	{
  1011  		"-0x1000009dc6e3d9822cba04129bcbe3401",
  1012  		"-0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
  1013  		"-0xb9bd7d543685789d57cb918e8bfeff7fddb2ebe87dfbbdfe35fd",
  1014  		"-0x1000001186210100001000009048c2001",
  1015  		"0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fc",
  1016  		"0xb9bd7d543685789d57ca918e82229142459020483cd2014001fc",
  1017  	},
  1018  }
  1019  
  1020  type bitFun func(z, x, y *Int) *Int
  1021  
  1022  func testBitFun(t *testing.T, msg string, f bitFun, x, y *Int, exp string) {
  1023  	expected := new(Int)
  1024  	expected.SetString(exp, 0)
  1025  
  1026  	out := f(new(Int), x, y)
  1027  	if out.Cmp(expected) != 0 {
  1028  		t.Errorf("%s: got %s want %s", msg, out, expected)
  1029  	}
  1030  }
  1031  
  1032  func testBitFunSelf(t *testing.T, msg string, f bitFun, x, y *Int, exp string) {
  1033  	self := new(Int)
  1034  	self.Set(x)
  1035  	expected := new(Int)
  1036  	expected.SetString(exp, 0)
  1037  
  1038  	self = f(self, self, y)
  1039  	if self.Cmp(expected) != 0 {
  1040  		t.Errorf("%s: got %s want %s", msg, self, expected)
  1041  	}
  1042  }
  1043  
  1044  func altBit(x *Int, i int) uint {
  1045  	z := new(Int).Rsh(x, uint(i))
  1046  	z = z.And(z, NewInt(1))
  1047  	if z.Cmp(new(Int)) != 0 {
  1048  		return 1
  1049  	}
  1050  	return 0
  1051  }
  1052  
  1053  func altSetBit(z *Int, x *Int, i int, b uint) *Int {
  1054  	one := NewInt(1)
  1055  	m := one.Lsh(one, uint(i))
  1056  	switch b {
  1057  	case 1:
  1058  		return z.Or(x, m)
  1059  	case 0:
  1060  		return z.AndNot(x, m)
  1061  	}
  1062  	panic("set bit is not 0 or 1")
  1063  }
  1064  
  1065  func testBitset(t *testing.T, x *Int) {
  1066  	n := x.BitLen()
  1067  	z := new(Int).Set(x)
  1068  	z1 := new(Int).Set(x)
  1069  	for i := 0; i < n+10; i++ {
  1070  		old := z.Bit(i)
  1071  		old1 := altBit(z1, i)
  1072  		if old != old1 {
  1073  			t.Errorf("bitset: inconsistent value for Bit(%s, %d), got %v want %v", z1, i, old, old1)
  1074  		}
  1075  		z := new(Int).SetBit(z, i, 1)
  1076  		z1 := altSetBit(new(Int), z1, i, 1)
  1077  		if z.Bit(i) == 0 {
  1078  			t.Errorf("bitset: bit %d of %s got 0 want 1", i, x)
  1079  		}
  1080  		if z.Cmp(z1) != 0 {
  1081  			t.Errorf("bitset: inconsistent value after SetBit 1, got %s want %s", z, z1)
  1082  		}
  1083  		z.SetBit(z, i, 0)
  1084  		altSetBit(z1, z1, i, 0)
  1085  		if z.Bit(i) != 0 {
  1086  			t.Errorf("bitset: bit %d of %s got 1 want 0", i, x)
  1087  		}
  1088  		if z.Cmp(z1) != 0 {
  1089  			t.Errorf("bitset: inconsistent value after SetBit 0, got %s want %s", z, z1)
  1090  		}
  1091  		altSetBit(z1, z1, i, old)
  1092  		z.SetBit(z, i, old)
  1093  		if z.Cmp(z1) != 0 {
  1094  			t.Errorf("bitset: inconsistent value after SetBit old, got %s want %s", z, z1)
  1095  		}
  1096  	}
  1097  	if z.Cmp(x) != 0 {
  1098  		t.Errorf("bitset: got %s want %s", z, x)
  1099  	}
  1100  }
  1101  
  1102  var bitsetTests = []struct {
  1103  	x string
  1104  	i int
  1105  	b uint
  1106  }{
  1107  	{"0", 0, 0},
  1108  	{"0", 200, 0},
  1109  	{"1", 0, 1},
  1110  	{"1", 1, 0},
  1111  	{"-1", 0, 1},
  1112  	{"-1", 200, 1},
  1113  	{"0x2000000000000000000000000000", 108, 0},
  1114  	{"0x2000000000000000000000000000", 109, 1},
  1115  	{"0x2000000000000000000000000000", 110, 0},
  1116  	{"-0x2000000000000000000000000001", 108, 1},
  1117  	{"-0x2000000000000000000000000001", 109, 0},
  1118  	{"-0x2000000000000000000000000001", 110, 1},
  1119  }
  1120  
  1121  func TestBitSet(t *testing.T) {
  1122  	for _, test := range bitwiseTests {
  1123  		x := new(Int)
  1124  		x.SetString(test.x, 0)
  1125  		testBitset(t, x)
  1126  		x = new(Int)
  1127  		x.SetString(test.y, 0)
  1128  		testBitset(t, x)
  1129  	}
  1130  	for i, test := range bitsetTests {
  1131  		x := new(Int)
  1132  		x.SetString(test.x, 0)
  1133  		b := x.Bit(test.i)
  1134  		if b != test.b {
  1135  			t.Errorf("#%d got %v want %v", i, b, test.b)
  1136  		}
  1137  	}
  1138  	z := NewInt(1)
  1139  	z.SetBit(NewInt(0), 2, 1)
  1140  	if z.Cmp(NewInt(4)) != 0 {
  1141  		t.Errorf("destination leaked into result; got %s want 4", z)
  1142  	}
  1143  }
  1144  
  1145  func BenchmarkBitset(b *testing.B) {
  1146  	z := new(Int)
  1147  	z.SetBit(z, 512, 1)
  1148  	b.ResetTimer()
  1149  	b.StartTimer()
  1150  	for i := b.N - 1; i >= 0; i-- {
  1151  		z.SetBit(z, i&512, 1)
  1152  	}
  1153  }
  1154  
  1155  func BenchmarkBitsetNeg(b *testing.B) {
  1156  	z := NewInt(-1)
  1157  	z.SetBit(z, 512, 0)
  1158  	b.ResetTimer()
  1159  	b.StartTimer()
  1160  	for i := b.N - 1; i >= 0; i-- {
  1161  		z.SetBit(z, i&512, 0)
  1162  	}
  1163  }
  1164  
  1165  func BenchmarkBitsetOrig(b *testing.B) {
  1166  	z := new(Int)
  1167  	altSetBit(z, z, 512, 1)
  1168  	b.ResetTimer()
  1169  	b.StartTimer()
  1170  	for i := b.N - 1; i >= 0; i-- {
  1171  		altSetBit(z, z, i&512, 1)
  1172  	}
  1173  }
  1174  
  1175  func BenchmarkBitsetNegOrig(b *testing.B) {
  1176  	z := NewInt(-1)
  1177  	altSetBit(z, z, 512, 0)
  1178  	b.ResetTimer()
  1179  	b.StartTimer()
  1180  	for i := b.N - 1; i >= 0; i-- {
  1181  		altSetBit(z, z, i&512, 0)
  1182  	}
  1183  }
  1184  
  1185  // tri generates the trinomial 2**(n*2) - 2**n - 1, which is always 3 mod 4 and
  1186  // 7 mod 8, so that 2 is always a quadratic residue.
  1187  func tri(n uint) *Int {
  1188  	x := NewInt(1)
  1189  	x.Lsh(x, n)
  1190  	x2 := new(Int).Lsh(x, n)
  1191  	x2.Sub(x2, x)
  1192  	x2.Sub(x2, intOne)
  1193  	return x2
  1194  }
  1195  
  1196  func BenchmarkModSqrt225_Tonelli(b *testing.B) {
  1197  	p := tri(225)
  1198  	x := NewInt(2)
  1199  	for i := 0; i < b.N; i++ {
  1200  		x.SetUint64(2)
  1201  		x.modSqrtTonelliShanks(x, p)
  1202  	}
  1203  }
  1204  
  1205  func BenchmarkModSqrt224_3Mod4(b *testing.B) {
  1206  	p := tri(225)
  1207  	x := new(Int).SetUint64(2)
  1208  	for i := 0; i < b.N; i++ {
  1209  		x.SetUint64(2)
  1210  		x.modSqrt3Mod4Prime(x, p)
  1211  	}
  1212  }
  1213  
  1214  func BenchmarkModSqrt5430_Tonelli(b *testing.B) {
  1215  	p := tri(5430)
  1216  	x := new(Int).SetUint64(2)
  1217  	for i := 0; i < b.N; i++ {
  1218  		x.SetUint64(2)
  1219  		x.modSqrtTonelliShanks(x, p)
  1220  	}
  1221  }
  1222  
  1223  func BenchmarkModSqrt5430_3Mod4(b *testing.B) {
  1224  	p := tri(5430)
  1225  	x := new(Int).SetUint64(2)
  1226  	for i := 0; i < b.N; i++ {
  1227  		x.SetUint64(2)
  1228  		x.modSqrt3Mod4Prime(x, p)
  1229  	}
  1230  }
  1231  
  1232  func TestBitwise(t *testing.T) {
  1233  	x := new(Int)
  1234  	y := new(Int)
  1235  	for _, test := range bitwiseTests {
  1236  		x.SetString(test.x, 0)
  1237  		y.SetString(test.y, 0)
  1238  
  1239  		testBitFun(t, "and", (*Int).And, x, y, test.and)
  1240  		testBitFunSelf(t, "and", (*Int).And, x, y, test.and)
  1241  		testBitFun(t, "andNot", (*Int).AndNot, x, y, test.andNot)
  1242  		testBitFunSelf(t, "andNot", (*Int).AndNot, x, y, test.andNot)
  1243  		testBitFun(t, "or", (*Int).Or, x, y, test.or)
  1244  		testBitFunSelf(t, "or", (*Int).Or, x, y, test.or)
  1245  		testBitFun(t, "xor", (*Int).Xor, x, y, test.xor)
  1246  		testBitFunSelf(t, "xor", (*Int).Xor, x, y, test.xor)
  1247  	}
  1248  }
  1249  
  1250  var notTests = []struct {
  1251  	in  string
  1252  	out string
  1253  }{
  1254  	{"0", "-1"},
  1255  	{"1", "-2"},
  1256  	{"7", "-8"},
  1257  	{"0", "-1"},
  1258  	{"-81910", "81909"},
  1259  	{
  1260  		"298472983472983471903246121093472394872319615612417471234712061",
  1261  		"-298472983472983471903246121093472394872319615612417471234712062",
  1262  	},
  1263  }
  1264  
  1265  func TestNot(t *testing.T) {
  1266  	in := new(Int)
  1267  	out := new(Int)
  1268  	expected := new(Int)
  1269  	for i, test := range notTests {
  1270  		in.SetString(test.in, 10)
  1271  		expected.SetString(test.out, 10)
  1272  		out = out.Not(in)
  1273  		if out.Cmp(expected) != 0 {
  1274  			t.Errorf("#%d: got %s want %s", i, out, expected)
  1275  		}
  1276  		out = out.Not(out)
  1277  		if out.Cmp(in) != 0 {
  1278  			t.Errorf("#%d: got %s want %s", i, out, in)
  1279  		}
  1280  	}
  1281  }
  1282  
  1283  var modInverseTests = []struct {
  1284  	element string
  1285  	modulus string
  1286  }{
  1287  	{"1234567", "458948883992"},
  1288  	{"239487239847", "2410312426921032588552076022197566074856950548502459942654116941958108831682612228890093858261341614673227141477904012196503648957050582631942730706805009223062734745341073406696246014589361659774041027169249453200378729434170325843778659198143763193776859869524088940195577346119843545301547043747207749969763750084308926339295559968882457872412993810129130294592999947926365264059284647209730384947211681434464714438488520940127459844288859336526896320919633919"},
  1289  }
  1290  
  1291  func TestModInverse(t *testing.T) {
  1292  	var element, modulus, gcd, inverse Int
  1293  	one := NewInt(1)
  1294  	for i, test := range modInverseTests {
  1295  		(&element).SetString(test.element, 10)
  1296  		(&modulus).SetString(test.modulus, 10)
  1297  		(&inverse).ModInverse(&element, &modulus)
  1298  		(&inverse).Mul(&inverse, &element)
  1299  		(&inverse).Mod(&inverse, &modulus)
  1300  		if (&inverse).Cmp(one) != 0 {
  1301  			t.Errorf("#%d: failed (e·e^(-1)=%s)", i, &inverse)
  1302  		}
  1303  	}
  1304  	// exhaustive test for small values
  1305  	for n := 2; n < 100; n++ {
  1306  		(&modulus).SetInt64(int64(n))
  1307  		for x := 1; x < n; x++ {
  1308  			(&element).SetInt64(int64(x))
  1309  			(&gcd).GCD(nil, nil, &element, &modulus)
  1310  			if (&gcd).Cmp(one) != 0 {
  1311  				continue
  1312  			}
  1313  			(&inverse).ModInverse(&element, &modulus)
  1314  			(&inverse).Mul(&inverse, &element)
  1315  			(&inverse).Mod(&inverse, &modulus)
  1316  			if (&inverse).Cmp(one) != 0 {
  1317  				t.Errorf("ModInverse(%d,%d)*%d%%%d=%d, not 1", &element, &modulus, &element, &modulus, &inverse)
  1318  			}
  1319  		}
  1320  	}
  1321  }
  1322  
  1323  // testModSqrt is a helper for TestModSqrt,
  1324  // which checks that ModSqrt can compute a square-root of elt^2.
  1325  func testModSqrt(t *testing.T, elt, mod, sq, sqrt *Int) bool {
  1326  	var sqChk, sqrtChk, sqrtsq Int
  1327  	sq.Mul(elt, elt)
  1328  	sq.Mod(sq, mod)
  1329  	z := sqrt.ModSqrt(sq, mod)
  1330  	if z != sqrt {
  1331  		t.Errorf("ModSqrt returned wrong value %s", z)
  1332  	}
  1333  
  1334  	// test ModSqrt arguments outside the range [0,mod)
  1335  	sqChk.Add(sq, mod)
  1336  	z = sqrtChk.ModSqrt(&sqChk, mod)
  1337  	if z != &sqrtChk || z.Cmp(sqrt) != 0 {
  1338  		t.Errorf("ModSqrt returned inconsistent value %s", z)
  1339  	}
  1340  	sqChk.Sub(sq, mod)
  1341  	z = sqrtChk.ModSqrt(&sqChk, mod)
  1342  	if z != &sqrtChk || z.Cmp(sqrt) != 0 {
  1343  		t.Errorf("ModSqrt returned inconsistent value %s", z)
  1344  	}
  1345  
  1346  	// make sure we actually got a square root
  1347  	if sqrt.Cmp(elt) == 0 {
  1348  		return true // we found the "desired" square root
  1349  	}
  1350  	sqrtsq.Mul(sqrt, sqrt) // make sure we found the "other" one
  1351  	sqrtsq.Mod(&sqrtsq, mod)
  1352  	return sq.Cmp(&sqrtsq) == 0
  1353  }
  1354  
  1355  func TestModSqrt(t *testing.T) {
  1356  	var elt, mod, modx4, sq, sqrt Int
  1357  	r := rand.New(rand.NewSource(9))
  1358  	for i, s := range primes[1:] { // skip 2, use only odd primes
  1359  		mod.SetString(s, 10)
  1360  		modx4.Lsh(&mod, 2)
  1361  
  1362  		// test a few random elements per prime
  1363  		for x := 1; x < 5; x++ {
  1364  			elt.Rand(r, &modx4)
  1365  			elt.Sub(&elt, &mod) // test range [-mod, 3*mod)
  1366  			if !testModSqrt(t, &elt, &mod, &sq, &sqrt) {
  1367  				t.Errorf("#%d: failed (sqrt(e) = %s)", i, &sqrt)
  1368  			}
  1369  		}
  1370  	}
  1371  
  1372  	// exhaustive test for small values
  1373  	for n := 3; n < 100; n++ {
  1374  		mod.SetInt64(int64(n))
  1375  		if !mod.ProbablyPrime(10) {
  1376  			continue
  1377  		}
  1378  		isSquare := make([]bool, n)
  1379  
  1380  		// test all the squares
  1381  		for x := 1; x < n; x++ {
  1382  			elt.SetInt64(int64(x))
  1383  			if !testModSqrt(t, &elt, &mod, &sq, &sqrt) {
  1384  				t.Errorf("#%d: failed (sqrt(%d,%d) = %s)", x, &elt, &mod, &sqrt)
  1385  			}
  1386  			isSquare[sq.Uint64()] = true
  1387  		}
  1388  
  1389  		// test all non-squares
  1390  		for x := 1; x < n; x++ {
  1391  			sq.SetInt64(int64(x))
  1392  			z := sqrt.ModSqrt(&sq, &mod)
  1393  			if !isSquare[x] && z != nil {
  1394  				t.Errorf("#%d: failed (sqrt(%d,%d) = nil)", x, &sqrt, &mod)
  1395  			}
  1396  		}
  1397  	}
  1398  }
  1399  
  1400  func TestJacobi(t *testing.T) {
  1401  	testCases := []struct {
  1402  		x, y   int64
  1403  		result int
  1404  	}{
  1405  		{0, 1, 1},
  1406  		{0, -1, 1},
  1407  		{1, 1, 1},
  1408  		{1, -1, 1},
  1409  		{0, 5, 0},
  1410  		{1, 5, 1},
  1411  		{2, 5, -1},
  1412  		{-2, 5, -1},
  1413  		{2, -5, -1},
  1414  		{-2, -5, 1},
  1415  		{3, 5, -1},
  1416  		{5, 5, 0},
  1417  		{-5, 5, 0},
  1418  		{6, 5, 1},
  1419  		{6, -5, 1},
  1420  		{-6, 5, 1},
  1421  		{-6, -5, -1},
  1422  	}
  1423  
  1424  	var x, y Int
  1425  
  1426  	for i, test := range testCases {
  1427  		x.SetInt64(test.x)
  1428  		y.SetInt64(test.y)
  1429  		expected := test.result
  1430  		actual := Jacobi(&x, &y)
  1431  		if actual != expected {
  1432  			t.Errorf("#%d: Jacobi(%d, %d) = %d, but expected %d", i, test.x, test.y, actual, expected)
  1433  		}
  1434  	}
  1435  }
  1436  
  1437  func TestJacobiPanic(t *testing.T) {
  1438  	const failureMsg = "test failure"
  1439  	defer func() {
  1440  		msg := recover()
  1441  		if msg == nil || msg == failureMsg {
  1442  			panic(msg)
  1443  		}
  1444  		t.Log(msg)
  1445  	}()
  1446  	x := NewInt(1)
  1447  	y := NewInt(2)
  1448  	// Jacobi should panic when the second argument is even.
  1449  	Jacobi(x, y)
  1450  	panic(failureMsg)
  1451  }
  1452  
  1453  func TestIssue2607(t *testing.T) {
  1454  	// This code sequence used to hang.
  1455  	n := NewInt(10)
  1456  	n.Rand(rand.New(rand.NewSource(9)), n)
  1457  }