github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/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 "internal/testenv" 12 "math" 13 "math/rand" 14 "strconv" 15 "strings" 16 "testing" 17 "testing/quick" 18 ) 19 20 func isNormalized(x *Int) bool { 21 if len(x.abs) == 0 { 22 return !x.neg 23 } 24 // len(x.abs) > 0 25 return x.abs[len(x.abs)-1] != 0 26 } 27 28 type funZZ func(z, x, y *Int) *Int 29 type argZZ struct { 30 z, x, y *Int 31 } 32 33 var sumZZ = []argZZ{ 34 {NewInt(0), NewInt(0), NewInt(0)}, 35 {NewInt(1), NewInt(1), NewInt(0)}, 36 {NewInt(1111111110), NewInt(123456789), NewInt(987654321)}, 37 {NewInt(-1), NewInt(-1), NewInt(0)}, 38 {NewInt(864197532), NewInt(-123456789), NewInt(987654321)}, 39 {NewInt(-1111111110), NewInt(-123456789), NewInt(-987654321)}, 40 } 41 42 var prodZZ = []argZZ{ 43 {NewInt(0), NewInt(0), NewInt(0)}, 44 {NewInt(0), NewInt(1), NewInt(0)}, 45 {NewInt(1), NewInt(1), NewInt(1)}, 46 {NewInt(-991 * 991), NewInt(991), NewInt(-991)}, 47 // TODO(gri) add larger products 48 } 49 50 func TestSignZ(t *testing.T) { 51 var zero Int 52 for _, a := range sumZZ { 53 s := a.z.Sign() 54 e := a.z.Cmp(&zero) 55 if s != e { 56 t.Errorf("got %d; want %d for z = %v", s, e, a.z) 57 } 58 } 59 } 60 61 func TestSetZ(t *testing.T) { 62 for _, a := range sumZZ { 63 var z Int 64 z.Set(a.z) 65 if !isNormalized(&z) { 66 t.Errorf("%v is not normalized", z) 67 } 68 if (&z).Cmp(a.z) != 0 { 69 t.Errorf("got z = %v; want %v", z, a.z) 70 } 71 } 72 } 73 74 func TestAbsZ(t *testing.T) { 75 var zero Int 76 for _, a := range sumZZ { 77 var z Int 78 z.Abs(a.z) 79 var e Int 80 e.Set(a.z) 81 if e.Cmp(&zero) < 0 { 82 e.Sub(&zero, &e) 83 } 84 if z.Cmp(&e) != 0 { 85 t.Errorf("got z = %v; want %v", z, e) 86 } 87 } 88 } 89 90 func testFunZZ(t *testing.T, msg string, f funZZ, a argZZ) { 91 var z Int 92 f(&z, a.x, a.y) 93 if !isNormalized(&z) { 94 t.Errorf("%s%v is not normalized", msg, z) 95 } 96 if (&z).Cmp(a.z) != 0 { 97 t.Errorf("%v %s %v\n\tgot z = %v; want %v", a.x, msg, a.y, &z, a.z) 98 } 99 } 100 101 func TestSumZZ(t *testing.T) { 102 AddZZ := func(z, x, y *Int) *Int { return z.Add(x, y) } 103 SubZZ := func(z, x, y *Int) *Int { return z.Sub(x, y) } 104 for _, a := range sumZZ { 105 arg := a 106 testFunZZ(t, "AddZZ", AddZZ, arg) 107 108 arg = argZZ{a.z, a.y, a.x} 109 testFunZZ(t, "AddZZ symmetric", AddZZ, arg) 110 111 arg = argZZ{a.x, a.z, a.y} 112 testFunZZ(t, "SubZZ", SubZZ, arg) 113 114 arg = argZZ{a.y, a.z, a.x} 115 testFunZZ(t, "SubZZ symmetric", SubZZ, arg) 116 } 117 } 118 119 func TestProdZZ(t *testing.T) { 120 MulZZ := func(z, x, y *Int) *Int { return z.Mul(x, y) } 121 for _, a := range prodZZ { 122 arg := a 123 testFunZZ(t, "MulZZ", MulZZ, arg) 124 125 arg = argZZ{a.z, a.y, a.x} 126 testFunZZ(t, "MulZZ symmetric", MulZZ, arg) 127 } 128 } 129 130 // mulBytes returns x*y via grade school multiplication. Both inputs 131 // and the result are assumed to be in big-endian representation (to 132 // match the semantics of Int.Bytes and Int.SetBytes). 133 func mulBytes(x, y []byte) []byte { 134 z := make([]byte, len(x)+len(y)) 135 136 // multiply 137 k0 := len(z) - 1 138 for j := len(y) - 1; j >= 0; j-- { 139 d := int(y[j]) 140 if d != 0 { 141 k := k0 142 carry := 0 143 for i := len(x) - 1; i >= 0; i-- { 144 t := int(z[k]) + int(x[i])*d + carry 145 z[k], carry = byte(t), t>>8 146 k-- 147 } 148 z[k] = byte(carry) 149 } 150 k0-- 151 } 152 153 // normalize (remove leading 0's) 154 i := 0 155 for i < len(z) && z[i] == 0 { 156 i++ 157 } 158 159 return z[i:] 160 } 161 162 func checkMul(a, b []byte) bool { 163 var x, y, z1 Int 164 x.SetBytes(a) 165 y.SetBytes(b) 166 z1.Mul(&x, &y) 167 168 var z2 Int 169 z2.SetBytes(mulBytes(a, b)) 170 171 return z1.Cmp(&z2) == 0 172 } 173 174 func TestMul(t *testing.T) { 175 if err := quick.Check(checkMul, nil); err != nil { 176 t.Error(err) 177 } 178 } 179 180 var mulRangesZ = []struct { 181 a, b int64 182 prod string 183 }{ 184 // entirely positive ranges are covered by mulRangesN 185 {-1, 1, "0"}, 186 {-2, -1, "2"}, 187 {-3, -2, "6"}, 188 {-3, -1, "-6"}, 189 {1, 3, "6"}, 190 {-10, -10, "-10"}, 191 {0, -1, "1"}, // empty range 192 {-1, -100, "1"}, // empty range 193 {-1, 1, "0"}, // range includes 0 194 {-1e9, 0, "0"}, // range includes 0 195 {-1e9, 1e9, "0"}, // range includes 0 196 {-10, -1, "3628800"}, // 10! 197 {-20, -2, "-2432902008176640000"}, // -20! 198 {-99, -1, 199 "-933262154439441526816992388562667004907159682643816214685929" + 200 "638952175999932299156089414639761565182862536979208272237582" + 201 "511852109168640000000000000000000000", // -99! 202 }, 203 } 204 205 func TestMulRangeZ(t *testing.T) { 206 var tmp Int 207 // test entirely positive ranges 208 for i, r := range mulRangesN { 209 prod := tmp.MulRange(int64(r.a), int64(r.b)).String() 210 if prod != r.prod { 211 t.Errorf("#%da: got %s; want %s", i, prod, r.prod) 212 } 213 } 214 // test other ranges 215 for i, r := range mulRangesZ { 216 prod := tmp.MulRange(r.a, r.b).String() 217 if prod != r.prod { 218 t.Errorf("#%db: got %s; want %s", i, prod, r.prod) 219 } 220 } 221 } 222 223 func TestBinomial(t *testing.T) { 224 var z Int 225 for _, test := range []struct { 226 n, k int64 227 want string 228 }{ 229 {0, 0, "1"}, 230 {0, 1, "0"}, 231 {1, 0, "1"}, 232 {1, 1, "1"}, 233 {1, 10, "0"}, 234 {4, 0, "1"}, 235 {4, 1, "4"}, 236 {4, 2, "6"}, 237 {4, 3, "4"}, 238 {4, 4, "1"}, 239 {10, 1, "10"}, 240 {10, 9, "10"}, 241 {10, 5, "252"}, 242 {11, 5, "462"}, 243 {11, 6, "462"}, 244 {100, 10, "17310309456440"}, 245 {100, 90, "17310309456440"}, 246 {1000, 10, "263409560461970212832400"}, 247 {1000, 990, "263409560461970212832400"}, 248 } { 249 if got := z.Binomial(test.n, test.k).String(); got != test.want { 250 t.Errorf("Binomial(%d, %d) = %s; want %s", test.n, test.k, got, test.want) 251 } 252 } 253 } 254 255 func BenchmarkBinomial(b *testing.B) { 256 var z Int 257 for i := b.N - 1; i >= 0; i-- { 258 z.Binomial(1000, 990) 259 } 260 } 261 262 // Examples from the Go Language Spec, section "Arithmetic operators" 263 var divisionSignsTests = []struct { 264 x, y int64 265 q, r int64 // T-division 266 d, m int64 // Euclidean division 267 }{ 268 {5, 3, 1, 2, 1, 2}, 269 {-5, 3, -1, -2, -2, 1}, 270 {5, -3, -1, 2, -1, 2}, 271 {-5, -3, 1, -2, 2, 1}, 272 {1, 2, 0, 1, 0, 1}, 273 {8, 4, 2, 0, 2, 0}, 274 } 275 276 func TestDivisionSigns(t *testing.T) { 277 for i, test := range divisionSignsTests { 278 x := NewInt(test.x) 279 y := NewInt(test.y) 280 q := NewInt(test.q) 281 r := NewInt(test.r) 282 d := NewInt(test.d) 283 m := NewInt(test.m) 284 285 q1 := new(Int).Quo(x, y) 286 r1 := new(Int).Rem(x, y) 287 if !isNormalized(q1) { 288 t.Errorf("#%d Quo: %v is not normalized", i, *q1) 289 } 290 if !isNormalized(r1) { 291 t.Errorf("#%d Rem: %v is not normalized", i, *r1) 292 } 293 if q1.Cmp(q) != 0 || r1.Cmp(r) != 0 { 294 t.Errorf("#%d QuoRem: got (%s, %s), want (%s, %s)", i, q1, r1, q, r) 295 } 296 297 q2, r2 := new(Int).QuoRem(x, y, new(Int)) 298 if !isNormalized(q2) { 299 t.Errorf("#%d Quo: %v is not normalized", i, *q2) 300 } 301 if !isNormalized(r2) { 302 t.Errorf("#%d Rem: %v is not normalized", i, *r2) 303 } 304 if q2.Cmp(q) != 0 || r2.Cmp(r) != 0 { 305 t.Errorf("#%d QuoRem: got (%s, %s), want (%s, %s)", i, q2, r2, q, r) 306 } 307 308 d1 := new(Int).Div(x, y) 309 m1 := new(Int).Mod(x, y) 310 if !isNormalized(d1) { 311 t.Errorf("#%d Div: %v is not normalized", i, *d1) 312 } 313 if !isNormalized(m1) { 314 t.Errorf("#%d Mod: %v is not normalized", i, *m1) 315 } 316 if d1.Cmp(d) != 0 || m1.Cmp(m) != 0 { 317 t.Errorf("#%d DivMod: got (%s, %s), want (%s, %s)", i, d1, m1, d, m) 318 } 319 320 d2, m2 := new(Int).DivMod(x, y, new(Int)) 321 if !isNormalized(d2) { 322 t.Errorf("#%d Div: %v is not normalized", i, *d2) 323 } 324 if !isNormalized(m2) { 325 t.Errorf("#%d Mod: %v is not normalized", i, *m2) 326 } 327 if d2.Cmp(d) != 0 || m2.Cmp(m) != 0 { 328 t.Errorf("#%d DivMod: got (%s, %s), want (%s, %s)", i, d2, m2, d, m) 329 } 330 } 331 } 332 333 func norm(x nat) nat { 334 i := len(x) 335 for i > 0 && x[i-1] == 0 { 336 i-- 337 } 338 return x[:i] 339 } 340 341 func TestBits(t *testing.T) { 342 for _, test := range []nat{ 343 nil, 344 {0}, 345 {1}, 346 {0, 1, 2, 3, 4}, 347 {4, 3, 2, 1, 0}, 348 {4, 3, 2, 1, 0, 0, 0, 0}, 349 } { 350 var z Int 351 z.neg = true 352 got := z.SetBits(test) 353 want := norm(test) 354 if got.abs.cmp(want) != 0 { 355 t.Errorf("SetBits(%v) = %v; want %v", test, got.abs, want) 356 } 357 358 if got.neg { 359 t.Errorf("SetBits(%v): got negative result", test) 360 } 361 362 bits := nat(z.Bits()) 363 if bits.cmp(want) != 0 { 364 t.Errorf("%v.Bits() = %v; want %v", z.abs, bits, want) 365 } 366 } 367 } 368 369 func checkSetBytes(b []byte) bool { 370 hex1 := hex.EncodeToString(new(Int).SetBytes(b).Bytes()) 371 hex2 := hex.EncodeToString(b) 372 373 for len(hex1) < len(hex2) { 374 hex1 = "0" + hex1 375 } 376 377 for len(hex1) > len(hex2) { 378 hex2 = "0" + hex2 379 } 380 381 return hex1 == hex2 382 } 383 384 func TestSetBytes(t *testing.T) { 385 if err := quick.Check(checkSetBytes, nil); err != nil { 386 t.Error(err) 387 } 388 } 389 390 func checkBytes(b []byte) bool { 391 // trim leading zero bytes since Bytes() won't return them 392 // (was issue 12231) 393 for len(b) > 0 && b[0] == 0 { 394 b = b[1:] 395 } 396 b2 := new(Int).SetBytes(b).Bytes() 397 return bytes.Equal(b, b2) 398 } 399 400 func TestBytes(t *testing.T) { 401 if err := quick.Check(checkBytes, nil); err != nil { 402 t.Error(err) 403 } 404 } 405 406 func checkQuo(x, y []byte) bool { 407 u := new(Int).SetBytes(x) 408 v := new(Int).SetBytes(y) 409 410 if len(v.abs) == 0 { 411 return true 412 } 413 414 r := new(Int) 415 q, r := new(Int).QuoRem(u, v, r) 416 417 if r.Cmp(v) >= 0 { 418 return false 419 } 420 421 uprime := new(Int).Set(q) 422 uprime.Mul(uprime, v) 423 uprime.Add(uprime, r) 424 425 return uprime.Cmp(u) == 0 426 } 427 428 var quoTests = []struct { 429 x, y string 430 q, r string 431 }{ 432 { 433 "476217953993950760840509444250624797097991362735329973741718102894495832294430498335824897858659711275234906400899559094370964723884706254265559534144986498357", 434 "9353930466774385905609975137998169297361893554149986716853295022578535724979483772383667534691121982974895531435241089241440253066816724367338287092081996", 435 "50911", 436 "1", 437 }, 438 { 439 "11510768301994997771168", 440 "1328165573307167369775", 441 "8", 442 "885443715537658812968", 443 }, 444 } 445 446 func TestQuo(t *testing.T) { 447 if err := quick.Check(checkQuo, nil); err != nil { 448 t.Error(err) 449 } 450 451 for i, test := range quoTests { 452 x, _ := new(Int).SetString(test.x, 10) 453 y, _ := new(Int).SetString(test.y, 10) 454 expectedQ, _ := new(Int).SetString(test.q, 10) 455 expectedR, _ := new(Int).SetString(test.r, 10) 456 457 r := new(Int) 458 q, r := new(Int).QuoRem(x, y, r) 459 460 if q.Cmp(expectedQ) != 0 || r.Cmp(expectedR) != 0 { 461 t.Errorf("#%d got (%s, %s) want (%s, %s)", i, q, r, expectedQ, expectedR) 462 } 463 } 464 } 465 466 func TestQuoStepD6(t *testing.T) { 467 // See Knuth, Volume 2, section 4.3.1, exercise 21. This code exercises 468 // a code path which only triggers 1 in 10^{-19} cases. 469 470 u := &Int{false, nat{0, 0, 1 + 1<<(_W-1), _M ^ (1 << (_W - 1))}} 471 v := &Int{false, nat{5, 2 + 1<<(_W-1), 1 << (_W - 1)}} 472 473 r := new(Int) 474 q, r := new(Int).QuoRem(u, v, r) 475 const expectedQ64 = "18446744073709551613" 476 const expectedR64 = "3138550867693340382088035895064302439801311770021610913807" 477 const expectedQ32 = "4294967293" 478 const expectedR32 = "39614081266355540837921718287" 479 if q.String() != expectedQ64 && q.String() != expectedQ32 || 480 r.String() != expectedR64 && r.String() != expectedR32 { 481 t.Errorf("got (%s, %s) want (%s, %s) or (%s, %s)", q, r, expectedQ64, expectedR64, expectedQ32, expectedR32) 482 } 483 } 484 485 func BenchmarkQuoRem(b *testing.B) { 486 x, _ := new(Int).SetString("153980389784927331788354528594524332344709972855165340650588877572729725338415474372475094155672066328274535240275856844648695200875763869073572078279316458648124537905600131008790701752441155668003033945258023841165089852359980273279085783159654751552359397986180318708491098942831252291841441726305535546071", 0) 487 y, _ := new(Int).SetString("7746362281539803897849273317883545285945243323447099728551653406505888775727297253384154743724750941556720663282745352402758568446486952008757638690735720782793164586481245379056001310087907017524411556680030339452580238411650898523599802732790857831596547515523593979861803187084910989428312522918414417263055355460715745539358014631136245887418412633787074173796862711588221766398229333338511838891484974940633857861775630560092874987828057333663969469797013996401149696897591265769095952887917296740109742927689053276850469671231961384715398038978492733178835452859452433234470997285516534065058887757272972533841547437247509415567206632827453524027585684464869520087576386907357207827931645864812453790560013100879070175244115566800303394525802384116508985235998027327908578315965475155235939798618031870849109894283125229184144172630553554607112725169432413343763989564437170644270643461665184965150423819594083121075825", 0) 488 q := new(Int) 489 r := new(Int) 490 491 b.ResetTimer() 492 for i := 0; i < b.N; i++ { 493 q.QuoRem(y, x, r) 494 } 495 } 496 497 var bitLenTests = []struct { 498 in string 499 out int 500 }{ 501 {"-1", 1}, 502 {"0", 0}, 503 {"1", 1}, 504 {"2", 2}, 505 {"4", 3}, 506 {"0xabc", 12}, 507 {"0x8000", 16}, 508 {"0x80000000", 32}, 509 {"0x800000000000", 48}, 510 {"0x8000000000000000", 64}, 511 {"0x80000000000000000000", 80}, 512 {"-0x4000000000000000000000", 87}, 513 } 514 515 func TestBitLen(t *testing.T) { 516 for i, test := range bitLenTests { 517 x, ok := new(Int).SetString(test.in, 0) 518 if !ok { 519 t.Errorf("#%d test input invalid: %s", i, test.in) 520 continue 521 } 522 523 if n := x.BitLen(); n != test.out { 524 t.Errorf("#%d got %d want %d", i, n, test.out) 525 } 526 } 527 } 528 529 var expTests = []struct { 530 x, y, m string 531 out string 532 }{ 533 // y <= 0 534 {"0", "0", "", "1"}, 535 {"1", "0", "", "1"}, 536 {"-10", "0", "", "1"}, 537 {"1234", "-1", "", "1"}, 538 {"1234", "-1", "0", "1"}, 539 {"17", "-100", "1234", "865"}, 540 {"2", "-100", "1234", ""}, 541 542 // m == 1 543 {"0", "0", "1", "0"}, 544 {"1", "0", "1", "0"}, 545 {"-10", "0", "1", "0"}, 546 {"1234", "-1", "1", "0"}, 547 548 // misc 549 {"5", "1", "3", "2"}, 550 {"5", "-7", "", "1"}, 551 {"-5", "-7", "", "1"}, 552 {"5", "0", "", "1"}, 553 {"-5", "0", "", "1"}, 554 {"5", "1", "", "5"}, 555 {"-5", "1", "", "-5"}, 556 {"-5", "1", "7", "2"}, 557 {"-2", "3", "2", "0"}, 558 {"5", "2", "", "25"}, 559 {"1", "65537", "2", "1"}, 560 {"0x8000000000000000", "2", "", "0x40000000000000000000000000000000"}, 561 {"0x8000000000000000", "2", "6719", "4944"}, 562 {"0x8000000000000000", "3", "6719", "5447"}, 563 {"0x8000000000000000", "1000", "6719", "1603"}, 564 {"0x8000000000000000", "1000000", "6719", "3199"}, 565 {"0x8000000000000000", "-1000000", "6719", "3663"}, // 3663 = ModInverse(3199, 6719) Issue #25865 566 567 {"0xffffffffffffffffffffffffffffffff", "0x12345678123456781234567812345678123456789", "0x01112222333344445555666677778889", "0x36168FA1DB3AAE6C8CE647E137F97A"}, 568 569 { 570 "2938462938472983472983659726349017249287491026512746239764525612965293865296239471239874193284792387498274256129746192347", 571 "298472983472983471903246121093472394872319615612417471234712061", 572 "29834729834729834729347290846729561262544958723956495615629569234729836259263598127342374289365912465901365498236492183464", 573 "23537740700184054162508175125554701713153216681790245129157191391322321508055833908509185839069455749219131480588829346291", 574 }, 575 // test case for issue 8822 576 { 577 "11001289118363089646017359372117963499250546375269047542777928006103246876688756735760905680604646624353196869572752623285140408755420374049317646428185270079555372763503115646054602867593662923894140940837479507194934267532831694565516466765025434902348314525627418515646588160955862839022051353653052947073136084780742729727874803457643848197499548297570026926927502505634297079527299004267769780768565695459945235586892627059178884998772989397505061206395455591503771677500931269477503508150175717121828518985901959919560700853226255420793148986854391552859459511723547532575574664944815966793196961286234040892865", 578 "0xB08FFB20760FFED58FADA86DFEF71AD72AA0FA763219618FE022C197E54708BB1191C66470250FCE8879487507CEE41381CA4D932F81C2B3F1AB20B539D50DCD", 579 "0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73", 580 "21484252197776302499639938883777710321993113097987201050501182909581359357618579566746556372589385361683610524730509041328855066514963385522570894839035884713051640171474186548713546686476761306436434146475140156284389181808675016576845833340494848283681088886584219750554408060556769486628029028720727393293111678826356480455433909233520504112074401376133077150471237549474149190242010469539006449596611576612573955754349042329130631128234637924786466585703488460540228477440853493392086251021228087076124706778899179648655221663765993962724699135217212118535057766739392069738618682722216712319320435674779146070442", 581 }, 582 { 583 "-0x1BCE04427D8032319A89E5C4136456671AC620883F2C4139E57F91307C485AD2D6204F4F87A58262652DB5DBBAC72B0613E51B835E7153BEC6068F5C8D696B74DBD18FEC316AEF73985CF0475663208EB46B4F17DD9DA55367B03323E5491A70997B90C059FB34809E6EE55BCFBD5F2F52233BFE62E6AA9E4E26A1D4C2439883D14F2633D55D8AA66A1ACD5595E778AC3A280517F1157989E70C1A437B849F1877B779CC3CDDEDE2DAA6594A6C66D181A00A5F777EE60596D8773998F6E988DEAE4CCA60E4DDCF9590543C89F74F603259FCAD71660D30294FBBE6490300F78A9D63FA660DC9417B8B9DDA28BEB3977B621B988E23D4D954F322C3540541BC649ABD504C50FADFD9F0987D58A2BF689313A285E773FF02899A6EF887D1D4A0D2", 584 "0xB08FFB20760FFED58FADA86DFEF71AD72AA0FA763219618FE022C197E54708BB1191C66470250FCE8879487507CEE41381CA4D932F81C2B3F1AB20B539D50DCD", 585 "0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73", 586 "21484252197776302499639938883777710321993113097987201050501182909581359357618579566746556372589385361683610524730509041328855066514963385522570894839035884713051640171474186548713546686476761306436434146475140156284389181808675016576845833340494848283681088886584219750554408060556769486628029028720727393293111678826356480455433909233520504112074401376133077150471237549474149190242010469539006449596611576612573955754349042329130631128234637924786466585703488460540228477440853493392086251021228087076124706778899179648655221663765993962724699135217212118535057766739392069738618682722216712319320435674779146070442", 587 }, 588 589 // test cases for issue 13907 590 {"0xffffffff00000001", "0xffffffff00000001", "0xffffffff00000001", "0"}, 591 {"0xffffffffffffffff00000001", "0xffffffffffffffff00000001", "0xffffffffffffffff00000001", "0"}, 592 {"0xffffffffffffffffffffffff00000001", "0xffffffffffffffffffffffff00000001", "0xffffffffffffffffffffffff00000001", "0"}, 593 {"0xffffffffffffffffffffffffffffffff00000001", "0xffffffffffffffffffffffffffffffff00000001", "0xffffffffffffffffffffffffffffffff00000001", "0"}, 594 595 { 596 "2", 597 "0xB08FFB20760FFED58FADA86DFEF71AD72AA0FA763219618FE022C197E54708BB1191C66470250FCE8879487507CEE41381CA4D932F81C2B3F1AB20B539D50DCD", 598 "0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73", // odd 599 "0x6AADD3E3E424D5B713FCAA8D8945B1E055166132038C57BBD2D51C833F0C5EA2007A2324CE514F8E8C2F008A2F36F44005A4039CB55830986F734C93DAF0EB4BAB54A6A8C7081864F44346E9BC6F0A3EB9F2C0146A00C6A05187D0C101E1F2D038CDB70CB5E9E05A2D188AB6CBB46286624D4415E7D4DBFAD3BCC6009D915C406EED38F468B940F41E6BEDC0430DD78E6F19A7DA3A27498A4181E24D738B0072D8F6ADB8C9809A5B033A09785814FD9919F6EF9F83EEA519BEC593855C4C10CBEEC582D4AE0792158823B0275E6AEC35242740468FAF3D5C60FD1E376362B6322F78B7ED0CA1C5BBCD2B49734A56C0967A1D01A100932C837B91D592CE08ABFF", 600 }, 601 { 602 "2", 603 "0xB08FFB20760FFED58FADA86DFEF71AD72AA0FA763219618FE022C197E54708BB1191C66470250FCE8879487507CEE41381CA4D932F81C2B3F1AB20B539D50DCD", 604 "0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF72", // even 605 "0x7858794B5897C29F4ED0B40913416AB6C48588484E6A45F2ED3E26C941D878E923575AAC434EE2750E6439A6976F9BB4D64CEDB2A53CE8D04DD48CADCDF8E46F22747C6B81C6CEA86C0D873FBF7CEF262BAAC43A522BD7F32F3CDAC52B9337C77B3DCFB3DB3EDD80476331E82F4B1DF8EFDC1220C92656DFC9197BDC1877804E28D928A2A284B8DED506CBA304435C9D0133C246C98A7D890D1DE60CBC53A024361DA83A9B8775019083D22AC6820ED7C3C68F8E801DD4EC779EE0A05C6EB682EF9840D285B838369BA7E148FA27691D524FAEAF7C6ECE2A4B99A294B9F2C241857B5B90CC8BFFCFCF18DFA7D676131D5CD3855A5A3E8EBFA0CDFADB4D198B4A", 606 }, 607 } 608 609 func TestExp(t *testing.T) { 610 for i, test := range expTests { 611 x, ok1 := new(Int).SetString(test.x, 0) 612 y, ok2 := new(Int).SetString(test.y, 0) 613 614 var ok3, ok4 bool 615 var out, m *Int 616 617 if len(test.out) == 0 { 618 out, ok3 = nil, true 619 } else { 620 out, ok3 = new(Int).SetString(test.out, 0) 621 } 622 623 if len(test.m) == 0 { 624 m, ok4 = nil, true 625 } else { 626 m, ok4 = new(Int).SetString(test.m, 0) 627 } 628 629 if !ok1 || !ok2 || !ok3 || !ok4 { 630 t.Errorf("#%d: error in input", i) 631 continue 632 } 633 634 z1 := new(Int).Exp(x, y, m) 635 if z1 != nil && !isNormalized(z1) { 636 t.Errorf("#%d: %v is not normalized", i, *z1) 637 } 638 if !(z1 == nil && out == nil || z1.Cmp(out) == 0) { 639 t.Errorf("#%d: got %x want %x", i, z1, out) 640 } 641 642 if m == nil { 643 // The result should be the same as for m == 0; 644 // specifically, there should be no div-zero panic. 645 m = &Int{abs: nat{}} // m != nil && len(m.abs) == 0 646 z2 := new(Int).Exp(x, y, m) 647 if z2.Cmp(z1) != 0 { 648 t.Errorf("#%d: got %x want %x", i, z2, z1) 649 } 650 } 651 } 652 } 653 654 func BenchmarkExp(b *testing.B) { 655 x, _ := new(Int).SetString("11001289118363089646017359372117963499250546375269047542777928006103246876688756735760905680604646624353196869572752623285140408755420374049317646428185270079555372763503115646054602867593662923894140940837479507194934267532831694565516466765025434902348314525627418515646588160955862839022051353653052947073136084780742729727874803457643848197499548297570026926927502505634297079527299004267769780768565695459945235586892627059178884998772989397505061206395455591503771677500931269477503508150175717121828518985901959919560700853226255420793148986854391552859459511723547532575574664944815966793196961286234040892865", 0) 656 y, _ := new(Int).SetString("0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF72", 0) 657 n, _ := new(Int).SetString("0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73", 0) 658 out := new(Int) 659 for i := 0; i < b.N; i++ { 660 out.Exp(x, y, n) 661 } 662 } 663 664 func BenchmarkExp2(b *testing.B) { 665 x, _ := new(Int).SetString("2", 0) 666 y, _ := new(Int).SetString("0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF72", 0) 667 n, _ := new(Int).SetString("0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73", 0) 668 out := new(Int) 669 for i := 0; i < b.N; i++ { 670 out.Exp(x, y, n) 671 } 672 } 673 674 func checkGcd(aBytes, bBytes []byte) bool { 675 x := new(Int) 676 y := new(Int) 677 a := new(Int).SetBytes(aBytes) 678 b := new(Int).SetBytes(bBytes) 679 680 d := new(Int).GCD(x, y, a, b) 681 x.Mul(x, a) 682 y.Mul(y, b) 683 x.Add(x, y) 684 685 return x.Cmp(d) == 0 686 } 687 688 // euclidExtGCD is a reference implementation of Euclid's 689 // extended GCD algorithm for testing against optimized algorithms. 690 // Requirements: a, b > 0 691 func euclidExtGCD(a, b *Int) (g, x, y *Int) { 692 A := new(Int).Set(a) 693 B := new(Int).Set(b) 694 695 // A = Ua*a + Va*b 696 // B = Ub*a + Vb*b 697 Ua := new(Int).SetInt64(1) 698 Va := new(Int) 699 700 Ub := new(Int) 701 Vb := new(Int).SetInt64(1) 702 703 q := new(Int) 704 temp := new(Int) 705 706 r := new(Int) 707 for len(B.abs) > 0 { 708 q, r = q.QuoRem(A, B, r) 709 710 A, B, r = B, r, A 711 712 // Ua, Ub = Ub, Ua-q*Ub 713 temp.Set(Ub) 714 Ub.Mul(Ub, q) 715 Ub.Sub(Ua, Ub) 716 Ua.Set(temp) 717 718 // Va, Vb = Vb, Va-q*Vb 719 temp.Set(Vb) 720 Vb.Mul(Vb, q) 721 Vb.Sub(Va, Vb) 722 Va.Set(temp) 723 } 724 return A, Ua, Va 725 } 726 727 func checkLehmerGcd(aBytes, bBytes []byte) bool { 728 a := new(Int).SetBytes(aBytes) 729 b := new(Int).SetBytes(bBytes) 730 731 if a.Sign() <= 0 || b.Sign() <= 0 { 732 return true // can only test positive arguments 733 } 734 735 d := new(Int).lehmerGCD(nil, nil, a, b) 736 d0, _, _ := euclidExtGCD(a, b) 737 738 return d.Cmp(d0) == 0 739 } 740 741 func checkLehmerExtGcd(aBytes, bBytes []byte) bool { 742 a := new(Int).SetBytes(aBytes) 743 b := new(Int).SetBytes(bBytes) 744 x := new(Int) 745 y := new(Int) 746 747 if a.Sign() <= 0 || b.Sign() <= 0 { 748 return true // can only test positive arguments 749 } 750 751 d := new(Int).lehmerGCD(x, y, a, b) 752 d0, x0, y0 := euclidExtGCD(a, b) 753 754 return d.Cmp(d0) == 0 && x.Cmp(x0) == 0 && y.Cmp(y0) == 0 755 } 756 757 var gcdTests = []struct { 758 d, x, y, a, b string 759 }{ 760 // a <= 0 || b <= 0 761 {"0", "0", "0", "0", "0"}, 762 {"7", "0", "1", "0", "7"}, 763 {"7", "0", "-1", "0", "-7"}, 764 {"11", "1", "0", "11", "0"}, 765 {"7", "-1", "-2", "-77", "35"}, 766 {"935", "-3", "8", "64515", "24310"}, 767 {"935", "-3", "-8", "64515", "-24310"}, 768 {"935", "3", "-8", "-64515", "-24310"}, 769 770 {"1", "-9", "47", "120", "23"}, 771 {"7", "1", "-2", "77", "35"}, 772 {"935", "-3", "8", "64515", "24310"}, 773 {"935000000000000000", "-3", "8", "64515000000000000000", "24310000000000000000"}, 774 {"1", "-221", "22059940471369027483332068679400581064239780177629666810348940098015901108344", "98920366548084643601728869055592650835572950932266967461790948584315647051443", "991"}, 775 } 776 777 func testGcd(t *testing.T, d, x, y, a, b *Int) { 778 var X *Int 779 if x != nil { 780 X = new(Int) 781 } 782 var Y *Int 783 if y != nil { 784 Y = new(Int) 785 } 786 787 D := new(Int).GCD(X, Y, a, b) 788 if D.Cmp(d) != 0 { 789 t.Errorf("GCD(%s, %s, %s, %s): got d = %s, want %s", x, y, a, b, D, d) 790 } 791 if x != nil && X.Cmp(x) != 0 { 792 t.Errorf("GCD(%s, %s, %s, %s): got x = %s, want %s", x, y, a, b, X, x) 793 } 794 if y != nil && Y.Cmp(y) != 0 { 795 t.Errorf("GCD(%s, %s, %s, %s): got y = %s, want %s", x, y, a, b, Y, y) 796 } 797 798 // check results in presence of aliasing (issue #11284) 799 a2 := new(Int).Set(a) 800 b2 := new(Int).Set(b) 801 a2.GCD(X, Y, a2, b2) // result is same as 1st argument 802 if a2.Cmp(d) != 0 { 803 t.Errorf("aliased z = a GCD(%s, %s, %s, %s): got d = %s, want %s", x, y, a, b, a2, d) 804 } 805 if x != nil && X.Cmp(x) != 0 { 806 t.Errorf("aliased z = a GCD(%s, %s, %s, %s): got x = %s, want %s", x, y, a, b, X, x) 807 } 808 if y != nil && Y.Cmp(y) != 0 { 809 t.Errorf("aliased z = a GCD(%s, %s, %s, %s): got y = %s, want %s", x, y, a, b, Y, y) 810 } 811 812 a2 = new(Int).Set(a) 813 b2 = new(Int).Set(b) 814 b2.GCD(X, Y, a2, b2) // result is same as 2nd argument 815 if b2.Cmp(d) != 0 { 816 t.Errorf("aliased z = b GCD(%s, %s, %s, %s): got d = %s, want %s", x, y, a, b, b2, d) 817 } 818 if x != nil && X.Cmp(x) != 0 { 819 t.Errorf("aliased z = b GCD(%s, %s, %s, %s): got x = %s, want %s", x, y, a, b, X, x) 820 } 821 if y != nil && Y.Cmp(y) != 0 { 822 t.Errorf("aliased z = b GCD(%s, %s, %s, %s): got y = %s, want %s", x, y, a, b, Y, y) 823 } 824 825 a2 = new(Int).Set(a) 826 b2 = new(Int).Set(b) 827 D = new(Int).GCD(a2, b2, a2, b2) // x = a, y = b 828 if D.Cmp(d) != 0 { 829 t.Errorf("aliased x = a, y = b GCD(%s, %s, %s, %s): got d = %s, want %s", x, y, a, b, D, d) 830 } 831 if x != nil && a2.Cmp(x) != 0 { 832 t.Errorf("aliased x = a, y = b GCD(%s, %s, %s, %s): got x = %s, want %s", x, y, a, b, a2, x) 833 } 834 if y != nil && b2.Cmp(y) != 0 { 835 t.Errorf("aliased x = a, y = b GCD(%s, %s, %s, %s): got y = %s, want %s", x, y, a, b, b2, y) 836 } 837 838 a2 = new(Int).Set(a) 839 b2 = new(Int).Set(b) 840 D = new(Int).GCD(b2, a2, a2, b2) // x = b, y = a 841 if D.Cmp(d) != 0 { 842 t.Errorf("aliased x = b, y = a GCD(%s, %s, %s, %s): got d = %s, want %s", x, y, a, b, D, d) 843 } 844 if x != nil && b2.Cmp(x) != 0 { 845 t.Errorf("aliased x = b, y = a GCD(%s, %s, %s, %s): got x = %s, want %s", x, y, a, b, b2, x) 846 } 847 if y != nil && a2.Cmp(y) != 0 { 848 t.Errorf("aliased x = b, y = a GCD(%s, %s, %s, %s): got y = %s, want %s", x, y, a, b, a2, y) 849 } 850 } 851 852 func TestGcd(t *testing.T) { 853 for _, test := range gcdTests { 854 d, _ := new(Int).SetString(test.d, 0) 855 x, _ := new(Int).SetString(test.x, 0) 856 y, _ := new(Int).SetString(test.y, 0) 857 a, _ := new(Int).SetString(test.a, 0) 858 b, _ := new(Int).SetString(test.b, 0) 859 860 testGcd(t, d, nil, nil, a, b) 861 testGcd(t, d, x, nil, a, b) 862 testGcd(t, d, nil, y, a, b) 863 testGcd(t, d, x, y, a, b) 864 } 865 866 if err := quick.Check(checkGcd, nil); err != nil { 867 t.Error(err) 868 } 869 870 if err := quick.Check(checkLehmerGcd, nil); err != nil { 871 t.Error(err) 872 } 873 874 if err := quick.Check(checkLehmerExtGcd, nil); err != nil { 875 t.Error(err) 876 } 877 } 878 879 type intShiftTest struct { 880 in string 881 shift uint 882 out string 883 } 884 885 var rshTests = []intShiftTest{ 886 {"0", 0, "0"}, 887 {"-0", 0, "0"}, 888 {"0", 1, "0"}, 889 {"0", 2, "0"}, 890 {"1", 0, "1"}, 891 {"1", 1, "0"}, 892 {"1", 2, "0"}, 893 {"2", 0, "2"}, 894 {"2", 1, "1"}, 895 {"-1", 0, "-1"}, 896 {"-1", 1, "-1"}, 897 {"-1", 10, "-1"}, 898 {"-100", 2, "-25"}, 899 {"-100", 3, "-13"}, 900 {"-100", 100, "-1"}, 901 {"4294967296", 0, "4294967296"}, 902 {"4294967296", 1, "2147483648"}, 903 {"4294967296", 2, "1073741824"}, 904 {"18446744073709551616", 0, "18446744073709551616"}, 905 {"18446744073709551616", 1, "9223372036854775808"}, 906 {"18446744073709551616", 2, "4611686018427387904"}, 907 {"18446744073709551616", 64, "1"}, 908 {"340282366920938463463374607431768211456", 64, "18446744073709551616"}, 909 {"340282366920938463463374607431768211456", 128, "1"}, 910 } 911 912 func TestRsh(t *testing.T) { 913 for i, test := range rshTests { 914 in, _ := new(Int).SetString(test.in, 10) 915 expected, _ := new(Int).SetString(test.out, 10) 916 out := new(Int).Rsh(in, test.shift) 917 918 if !isNormalized(out) { 919 t.Errorf("#%d: %v is not normalized", i, *out) 920 } 921 if out.Cmp(expected) != 0 { 922 t.Errorf("#%d: got %s want %s", i, out, expected) 923 } 924 } 925 } 926 927 func TestRshSelf(t *testing.T) { 928 for i, test := range rshTests { 929 z, _ := new(Int).SetString(test.in, 10) 930 expected, _ := new(Int).SetString(test.out, 10) 931 z.Rsh(z, test.shift) 932 933 if !isNormalized(z) { 934 t.Errorf("#%d: %v is not normalized", i, *z) 935 } 936 if z.Cmp(expected) != 0 { 937 t.Errorf("#%d: got %s want %s", i, z, expected) 938 } 939 } 940 } 941 942 var lshTests = []intShiftTest{ 943 {"0", 0, "0"}, 944 {"0", 1, "0"}, 945 {"0", 2, "0"}, 946 {"1", 0, "1"}, 947 {"1", 1, "2"}, 948 {"1", 2, "4"}, 949 {"2", 0, "2"}, 950 {"2", 1, "4"}, 951 {"2", 2, "8"}, 952 {"-87", 1, "-174"}, 953 {"4294967296", 0, "4294967296"}, 954 {"4294967296", 1, "8589934592"}, 955 {"4294967296", 2, "17179869184"}, 956 {"18446744073709551616", 0, "18446744073709551616"}, 957 {"9223372036854775808", 1, "18446744073709551616"}, 958 {"4611686018427387904", 2, "18446744073709551616"}, 959 {"1", 64, "18446744073709551616"}, 960 {"18446744073709551616", 64, "340282366920938463463374607431768211456"}, 961 {"1", 128, "340282366920938463463374607431768211456"}, 962 } 963 964 func TestLsh(t *testing.T) { 965 for i, test := range lshTests { 966 in, _ := new(Int).SetString(test.in, 10) 967 expected, _ := new(Int).SetString(test.out, 10) 968 out := new(Int).Lsh(in, test.shift) 969 970 if !isNormalized(out) { 971 t.Errorf("#%d: %v is not normalized", i, *out) 972 } 973 if out.Cmp(expected) != 0 { 974 t.Errorf("#%d: got %s want %s", i, out, expected) 975 } 976 } 977 } 978 979 func TestLshSelf(t *testing.T) { 980 for i, test := range lshTests { 981 z, _ := new(Int).SetString(test.in, 10) 982 expected, _ := new(Int).SetString(test.out, 10) 983 z.Lsh(z, test.shift) 984 985 if !isNormalized(z) { 986 t.Errorf("#%d: %v is not normalized", i, *z) 987 } 988 if z.Cmp(expected) != 0 { 989 t.Errorf("#%d: got %s want %s", i, z, expected) 990 } 991 } 992 } 993 994 func TestLshRsh(t *testing.T) { 995 for i, test := range rshTests { 996 in, _ := new(Int).SetString(test.in, 10) 997 out := new(Int).Lsh(in, test.shift) 998 out = out.Rsh(out, test.shift) 999 1000 if !isNormalized(out) { 1001 t.Errorf("#%d: %v is not normalized", i, *out) 1002 } 1003 if in.Cmp(out) != 0 { 1004 t.Errorf("#%d: got %s want %s", i, out, in) 1005 } 1006 } 1007 for i, test := range lshTests { 1008 in, _ := new(Int).SetString(test.in, 10) 1009 out := new(Int).Lsh(in, test.shift) 1010 out.Rsh(out, test.shift) 1011 1012 if !isNormalized(out) { 1013 t.Errorf("#%d: %v is not normalized", i, *out) 1014 } 1015 if in.Cmp(out) != 0 { 1016 t.Errorf("#%d: got %s want %s", i, out, in) 1017 } 1018 } 1019 } 1020 1021 // Entries must be sorted by value in ascending order. 1022 var cmpAbsTests = []string{ 1023 "0", 1024 "1", 1025 "2", 1026 "10", 1027 "10000000", 1028 "2783678367462374683678456387645876387564783686583485", 1029 "2783678367462374683678456387645876387564783686583486", 1030 "32957394867987420967976567076075976570670947609750670956097509670576075067076027578341538", 1031 } 1032 1033 func TestCmpAbs(t *testing.T) { 1034 values := make([]*Int, len(cmpAbsTests)) 1035 var prev *Int 1036 for i, s := range cmpAbsTests { 1037 x, ok := new(Int).SetString(s, 0) 1038 if !ok { 1039 t.Fatalf("SetString(%s, 0) failed", s) 1040 } 1041 if prev != nil && prev.Cmp(x) >= 0 { 1042 t.Fatal("cmpAbsTests entries not sorted in ascending order") 1043 } 1044 values[i] = x 1045 prev = x 1046 } 1047 1048 for i, x := range values { 1049 for j, y := range values { 1050 // try all combinations of signs for x, y 1051 for k := 0; k < 4; k++ { 1052 var a, b Int 1053 a.Set(x) 1054 b.Set(y) 1055 if k&1 != 0 { 1056 a.Neg(&a) 1057 } 1058 if k&2 != 0 { 1059 b.Neg(&b) 1060 } 1061 1062 got := a.CmpAbs(&b) 1063 want := 0 1064 switch { 1065 case i > j: 1066 want = 1 1067 case i < j: 1068 want = -1 1069 } 1070 if got != want { 1071 t.Errorf("absCmp |%s|, |%s|: got %d; want %d", &a, &b, got, want) 1072 } 1073 } 1074 } 1075 } 1076 } 1077 1078 func TestIntCmpSelf(t *testing.T) { 1079 for _, s := range cmpAbsTests { 1080 x, ok := new(Int).SetString(s, 0) 1081 if !ok { 1082 t.Fatalf("SetString(%s, 0) failed", s) 1083 } 1084 got := x.Cmp(x) 1085 want := 0 1086 if got != want { 1087 t.Errorf("x = %s: x.Cmp(x): got %d; want %d", x, got, want) 1088 } 1089 } 1090 } 1091 1092 var int64Tests = []string{ 1093 // int64 1094 "0", 1095 "1", 1096 "-1", 1097 "4294967295", 1098 "-4294967295", 1099 "4294967296", 1100 "-4294967296", 1101 "9223372036854775807", 1102 "-9223372036854775807", 1103 "-9223372036854775808", 1104 1105 // not int64 1106 "0x8000000000000000", 1107 "-0x8000000000000001", 1108 "38579843757496759476987459679745", 1109 "-38579843757496759476987459679745", 1110 } 1111 1112 func TestInt64(t *testing.T) { 1113 for _, s := range int64Tests { 1114 var x Int 1115 _, ok := x.SetString(s, 0) 1116 if !ok { 1117 t.Errorf("SetString(%s, 0) failed", s) 1118 continue 1119 } 1120 1121 want, err := strconv.ParseInt(s, 0, 64) 1122 if err != nil { 1123 if err.(*strconv.NumError).Err == strconv.ErrRange { 1124 if x.IsInt64() { 1125 t.Errorf("IsInt64(%s) succeeded unexpectedly", s) 1126 } 1127 } else { 1128 t.Errorf("ParseInt(%s) failed", s) 1129 } 1130 continue 1131 } 1132 1133 if !x.IsInt64() { 1134 t.Errorf("IsInt64(%s) failed unexpectedly", s) 1135 } 1136 1137 got := x.Int64() 1138 if got != want { 1139 t.Errorf("Int64(%s) = %d; want %d", s, got, want) 1140 } 1141 } 1142 } 1143 1144 var uint64Tests = []string{ 1145 // uint64 1146 "0", 1147 "1", 1148 "4294967295", 1149 "4294967296", 1150 "8589934591", 1151 "8589934592", 1152 "9223372036854775807", 1153 "9223372036854775808", 1154 "0x08000000000000000", 1155 1156 // not uint64 1157 "0x10000000000000000", 1158 "-0x08000000000000000", 1159 "-1", 1160 } 1161 1162 func TestUint64(t *testing.T) { 1163 for _, s := range uint64Tests { 1164 var x Int 1165 _, ok := x.SetString(s, 0) 1166 if !ok { 1167 t.Errorf("SetString(%s, 0) failed", s) 1168 continue 1169 } 1170 1171 want, err := strconv.ParseUint(s, 0, 64) 1172 if err != nil { 1173 // check for sign explicitly (ErrRange doesn't cover signed input) 1174 if s[0] == '-' || err.(*strconv.NumError).Err == strconv.ErrRange { 1175 if x.IsUint64() { 1176 t.Errorf("IsUint64(%s) succeeded unexpectedly", s) 1177 } 1178 } else { 1179 t.Errorf("ParseUint(%s) failed", s) 1180 } 1181 continue 1182 } 1183 1184 if !x.IsUint64() { 1185 t.Errorf("IsUint64(%s) failed unexpectedly", s) 1186 } 1187 1188 got := x.Uint64() 1189 if got != want { 1190 t.Errorf("Uint64(%s) = %d; want %d", s, got, want) 1191 } 1192 } 1193 } 1194 1195 var bitwiseTests = []struct { 1196 x, y string 1197 and, or, xor, andNot string 1198 }{ 1199 {"0x00", "0x00", "0x00", "0x00", "0x00", "0x00"}, 1200 {"0x00", "0x01", "0x00", "0x01", "0x01", "0x00"}, 1201 {"0x01", "0x00", "0x00", "0x01", "0x01", "0x01"}, 1202 {"-0x01", "0x00", "0x00", "-0x01", "-0x01", "-0x01"}, 1203 {"-0xaf", "-0x50", "-0xf0", "-0x0f", "0xe1", "0x41"}, 1204 {"0x00", "-0x01", "0x00", "-0x01", "-0x01", "0x00"}, 1205 {"0x01", "0x01", "0x01", "0x01", "0x00", "0x00"}, 1206 {"-0x01", "-0x01", "-0x01", "-0x01", "0x00", "0x00"}, 1207 {"0x07", "0x08", "0x00", "0x0f", "0x0f", "0x07"}, 1208 {"0x05", "0x0f", "0x05", "0x0f", "0x0a", "0x00"}, 1209 {"0xff", "-0x0a", "0xf6", "-0x01", "-0xf7", "0x09"}, 1210 {"0x013ff6", "0x9a4e", "0x1a46", "0x01bffe", "0x01a5b8", "0x0125b0"}, 1211 {"-0x013ff6", "0x9a4e", "0x800a", "-0x0125b2", "-0x01a5bc", "-0x01c000"}, 1212 {"-0x013ff6", "-0x9a4e", "-0x01bffe", "-0x1a46", "0x01a5b8", "0x8008"}, 1213 { 1214 "0x1000009dc6e3d9822cba04129bcbe3401", 1215 "0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd", 1216 "0x1000001186210100001000009048c2001", 1217 "0xb9bd7d543685789d57cb918e8bfeff7fddb2ebe87dfbbdfe35fd", 1218 "0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fc", 1219 "0x8c40c2d8822caa04120b8321400", 1220 }, 1221 { 1222 "0x1000009dc6e3d9822cba04129bcbe3401", 1223 "-0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd", 1224 "0x8c40c2d8822caa04120b8321401", 1225 "-0xb9bd7d543685789d57ca918e82229142459020483cd2014001fd", 1226 "-0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fe", 1227 "0x1000001186210100001000009048c2000", 1228 }, 1229 { 1230 "-0x1000009dc6e3d9822cba04129bcbe3401", 1231 "-0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd", 1232 "-0xb9bd7d543685789d57cb918e8bfeff7fddb2ebe87dfbbdfe35fd", 1233 "-0x1000001186210100001000009048c2001", 1234 "0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fc", 1235 "0xb9bd7d543685789d57ca918e82229142459020483cd2014001fc", 1236 }, 1237 } 1238 1239 type bitFun func(z, x, y *Int) *Int 1240 1241 func testBitFun(t *testing.T, msg string, f bitFun, x, y *Int, exp string) { 1242 expected := new(Int) 1243 expected.SetString(exp, 0) 1244 1245 out := f(new(Int), x, y) 1246 if out.Cmp(expected) != 0 { 1247 t.Errorf("%s: got %s want %s", msg, out, expected) 1248 } 1249 } 1250 1251 func testBitFunSelf(t *testing.T, msg string, f bitFun, x, y *Int, exp string) { 1252 self := new(Int) 1253 self.Set(x) 1254 expected := new(Int) 1255 expected.SetString(exp, 0) 1256 1257 self = f(self, self, y) 1258 if self.Cmp(expected) != 0 { 1259 t.Errorf("%s: got %s want %s", msg, self, expected) 1260 } 1261 } 1262 1263 func altBit(x *Int, i int) uint { 1264 z := new(Int).Rsh(x, uint(i)) 1265 z = z.And(z, NewInt(1)) 1266 if z.Cmp(new(Int)) != 0 { 1267 return 1 1268 } 1269 return 0 1270 } 1271 1272 func altSetBit(z *Int, x *Int, i int, b uint) *Int { 1273 one := NewInt(1) 1274 m := one.Lsh(one, uint(i)) 1275 switch b { 1276 case 1: 1277 return z.Or(x, m) 1278 case 0: 1279 return z.AndNot(x, m) 1280 } 1281 panic("set bit is not 0 or 1") 1282 } 1283 1284 func testBitset(t *testing.T, x *Int) { 1285 n := x.BitLen() 1286 z := new(Int).Set(x) 1287 z1 := new(Int).Set(x) 1288 for i := 0; i < n+10; i++ { 1289 old := z.Bit(i) 1290 old1 := altBit(z1, i) 1291 if old != old1 { 1292 t.Errorf("bitset: inconsistent value for Bit(%s, %d), got %v want %v", z1, i, old, old1) 1293 } 1294 z := new(Int).SetBit(z, i, 1) 1295 z1 := altSetBit(new(Int), z1, i, 1) 1296 if z.Bit(i) == 0 { 1297 t.Errorf("bitset: bit %d of %s got 0 want 1", i, x) 1298 } 1299 if z.Cmp(z1) != 0 { 1300 t.Errorf("bitset: inconsistent value after SetBit 1, got %s want %s", z, z1) 1301 } 1302 z.SetBit(z, i, 0) 1303 altSetBit(z1, z1, i, 0) 1304 if z.Bit(i) != 0 { 1305 t.Errorf("bitset: bit %d of %s got 1 want 0", i, x) 1306 } 1307 if z.Cmp(z1) != 0 { 1308 t.Errorf("bitset: inconsistent value after SetBit 0, got %s want %s", z, z1) 1309 } 1310 altSetBit(z1, z1, i, old) 1311 z.SetBit(z, i, old) 1312 if z.Cmp(z1) != 0 { 1313 t.Errorf("bitset: inconsistent value after SetBit old, got %s want %s", z, z1) 1314 } 1315 } 1316 if z.Cmp(x) != 0 { 1317 t.Errorf("bitset: got %s want %s", z, x) 1318 } 1319 } 1320 1321 var bitsetTests = []struct { 1322 x string 1323 i int 1324 b uint 1325 }{ 1326 {"0", 0, 0}, 1327 {"0", 200, 0}, 1328 {"1", 0, 1}, 1329 {"1", 1, 0}, 1330 {"-1", 0, 1}, 1331 {"-1", 200, 1}, 1332 {"0x2000000000000000000000000000", 108, 0}, 1333 {"0x2000000000000000000000000000", 109, 1}, 1334 {"0x2000000000000000000000000000", 110, 0}, 1335 {"-0x2000000000000000000000000001", 108, 1}, 1336 {"-0x2000000000000000000000000001", 109, 0}, 1337 {"-0x2000000000000000000000000001", 110, 1}, 1338 } 1339 1340 func TestBitSet(t *testing.T) { 1341 for _, test := range bitwiseTests { 1342 x := new(Int) 1343 x.SetString(test.x, 0) 1344 testBitset(t, x) 1345 x = new(Int) 1346 x.SetString(test.y, 0) 1347 testBitset(t, x) 1348 } 1349 for i, test := range bitsetTests { 1350 x := new(Int) 1351 x.SetString(test.x, 0) 1352 b := x.Bit(test.i) 1353 if b != test.b { 1354 t.Errorf("#%d got %v want %v", i, b, test.b) 1355 } 1356 } 1357 z := NewInt(1) 1358 z.SetBit(NewInt(0), 2, 1) 1359 if z.Cmp(NewInt(4)) != 0 { 1360 t.Errorf("destination leaked into result; got %s want 4", z) 1361 } 1362 } 1363 1364 var tzbTests = []struct { 1365 in string 1366 out uint 1367 }{ 1368 {"0", 0}, 1369 {"1", 0}, 1370 {"-1", 0}, 1371 {"4", 2}, 1372 {"-8", 3}, 1373 {"0x4000000000000000000", 74}, 1374 {"-0x8000000000000000000", 75}, 1375 } 1376 1377 func TestTrailingZeroBits(t *testing.T) { 1378 for i, test := range tzbTests { 1379 in, _ := new(Int).SetString(test.in, 0) 1380 want := test.out 1381 got := in.TrailingZeroBits() 1382 1383 if got != want { 1384 t.Errorf("#%d: got %v want %v", i, got, want) 1385 } 1386 } 1387 } 1388 1389 func BenchmarkBitset(b *testing.B) { 1390 z := new(Int) 1391 z.SetBit(z, 512, 1) 1392 b.ResetTimer() 1393 b.StartTimer() 1394 for i := b.N - 1; i >= 0; i-- { 1395 z.SetBit(z, i&512, 1) 1396 } 1397 } 1398 1399 func BenchmarkBitsetNeg(b *testing.B) { 1400 z := NewInt(-1) 1401 z.SetBit(z, 512, 0) 1402 b.ResetTimer() 1403 b.StartTimer() 1404 for i := b.N - 1; i >= 0; i-- { 1405 z.SetBit(z, i&512, 0) 1406 } 1407 } 1408 1409 func BenchmarkBitsetOrig(b *testing.B) { 1410 z := new(Int) 1411 altSetBit(z, z, 512, 1) 1412 b.ResetTimer() 1413 b.StartTimer() 1414 for i := b.N - 1; i >= 0; i-- { 1415 altSetBit(z, z, i&512, 1) 1416 } 1417 } 1418 1419 func BenchmarkBitsetNegOrig(b *testing.B) { 1420 z := NewInt(-1) 1421 altSetBit(z, z, 512, 0) 1422 b.ResetTimer() 1423 b.StartTimer() 1424 for i := b.N - 1; i >= 0; i-- { 1425 altSetBit(z, z, i&512, 0) 1426 } 1427 } 1428 1429 // tri generates the trinomial 2**(n*2) - 2**n - 1, which is always 3 mod 4 and 1430 // 7 mod 8, so that 2 is always a quadratic residue. 1431 func tri(n uint) *Int { 1432 x := NewInt(1) 1433 x.Lsh(x, n) 1434 x2 := new(Int).Lsh(x, n) 1435 x2.Sub(x2, x) 1436 x2.Sub(x2, intOne) 1437 return x2 1438 } 1439 1440 func BenchmarkModSqrt225_Tonelli(b *testing.B) { 1441 p := tri(225) 1442 x := NewInt(2) 1443 for i := 0; i < b.N; i++ { 1444 x.SetUint64(2) 1445 x.modSqrtTonelliShanks(x, p) 1446 } 1447 } 1448 1449 func BenchmarkModSqrt225_3Mod4(b *testing.B) { 1450 p := tri(225) 1451 x := new(Int).SetUint64(2) 1452 for i := 0; i < b.N; i++ { 1453 x.SetUint64(2) 1454 x.modSqrt3Mod4Prime(x, p) 1455 } 1456 } 1457 1458 func BenchmarkModSqrt231_Tonelli(b *testing.B) { 1459 p := tri(231) 1460 p.Sub(p, intOne) 1461 p.Sub(p, intOne) // tri(231) - 2 is a prime == 5 mod 8 1462 x := new(Int).SetUint64(7) 1463 for i := 0; i < b.N; i++ { 1464 x.SetUint64(7) 1465 x.modSqrtTonelliShanks(x, p) 1466 } 1467 } 1468 1469 func BenchmarkModSqrt231_5Mod8(b *testing.B) { 1470 p := tri(231) 1471 p.Sub(p, intOne) 1472 p.Sub(p, intOne) // tri(231) - 2 is a prime == 5 mod 8 1473 x := new(Int).SetUint64(7) 1474 for i := 0; i < b.N; i++ { 1475 x.SetUint64(7) 1476 x.modSqrt5Mod8Prime(x, p) 1477 } 1478 } 1479 1480 func TestBitwise(t *testing.T) { 1481 x := new(Int) 1482 y := new(Int) 1483 for _, test := range bitwiseTests { 1484 x.SetString(test.x, 0) 1485 y.SetString(test.y, 0) 1486 1487 testBitFun(t, "and", (*Int).And, x, y, test.and) 1488 testBitFunSelf(t, "and", (*Int).And, x, y, test.and) 1489 testBitFun(t, "andNot", (*Int).AndNot, x, y, test.andNot) 1490 testBitFunSelf(t, "andNot", (*Int).AndNot, x, y, test.andNot) 1491 testBitFun(t, "or", (*Int).Or, x, y, test.or) 1492 testBitFunSelf(t, "or", (*Int).Or, x, y, test.or) 1493 testBitFun(t, "xor", (*Int).Xor, x, y, test.xor) 1494 testBitFunSelf(t, "xor", (*Int).Xor, x, y, test.xor) 1495 } 1496 } 1497 1498 var notTests = []struct { 1499 in string 1500 out string 1501 }{ 1502 {"0", "-1"}, 1503 {"1", "-2"}, 1504 {"7", "-8"}, 1505 {"0", "-1"}, 1506 {"-81910", "81909"}, 1507 { 1508 "298472983472983471903246121093472394872319615612417471234712061", 1509 "-298472983472983471903246121093472394872319615612417471234712062", 1510 }, 1511 } 1512 1513 func TestNot(t *testing.T) { 1514 in := new(Int) 1515 out := new(Int) 1516 expected := new(Int) 1517 for i, test := range notTests { 1518 in.SetString(test.in, 10) 1519 expected.SetString(test.out, 10) 1520 out = out.Not(in) 1521 if out.Cmp(expected) != 0 { 1522 t.Errorf("#%d: got %s want %s", i, out, expected) 1523 } 1524 out = out.Not(out) 1525 if out.Cmp(in) != 0 { 1526 t.Errorf("#%d: got %s want %s", i, out, in) 1527 } 1528 } 1529 } 1530 1531 var modInverseTests = []struct { 1532 element string 1533 modulus string 1534 }{ 1535 {"1234567", "458948883992"}, 1536 {"239487239847", "2410312426921032588552076022197566074856950548502459942654116941958108831682612228890093858261341614673227141477904012196503648957050582631942730706805009223062734745341073406696246014589361659774041027169249453200378729434170325843778659198143763193776859869524088940195577346119843545301547043747207749969763750084308926339295559968882457872412993810129130294592999947926365264059284647209730384947211681434464714438488520940127459844288859336526896320919633919"}, 1537 {"-10", "13"}, // issue #16984 1538 {"10", "-13"}, 1539 {"-17", "-13"}, 1540 } 1541 1542 func TestModInverse(t *testing.T) { 1543 var element, modulus, gcd, inverse Int 1544 one := NewInt(1) 1545 for _, test := range modInverseTests { 1546 (&element).SetString(test.element, 10) 1547 (&modulus).SetString(test.modulus, 10) 1548 (&inverse).ModInverse(&element, &modulus) 1549 (&inverse).Mul(&inverse, &element) 1550 (&inverse).Mod(&inverse, &modulus) 1551 if (&inverse).Cmp(one) != 0 { 1552 t.Errorf("ModInverse(%d,%d)*%d%%%d=%d, not 1", &element, &modulus, &element, &modulus, &inverse) 1553 } 1554 } 1555 // exhaustive test for small values 1556 for n := 2; n < 100; n++ { 1557 (&modulus).SetInt64(int64(n)) 1558 for x := 1; x < n; x++ { 1559 (&element).SetInt64(int64(x)) 1560 (&gcd).GCD(nil, nil, &element, &modulus) 1561 if (&gcd).Cmp(one) != 0 { 1562 continue 1563 } 1564 (&inverse).ModInverse(&element, &modulus) 1565 (&inverse).Mul(&inverse, &element) 1566 (&inverse).Mod(&inverse, &modulus) 1567 if (&inverse).Cmp(one) != 0 { 1568 t.Errorf("ModInverse(%d,%d)*%d%%%d=%d, not 1", &element, &modulus, &element, &modulus, &inverse) 1569 } 1570 } 1571 } 1572 } 1573 1574 func BenchmarkModInverse(b *testing.B) { 1575 p := new(Int).SetInt64(1) // Mersenne prime 2**1279 -1 1576 p.abs = p.abs.shl(p.abs, 1279) 1577 p.Sub(p, intOne) 1578 x := new(Int).Sub(p, intOne) 1579 z := new(Int) 1580 for i := 0; i < b.N; i++ { 1581 z.ModInverse(x, p) 1582 } 1583 } 1584 1585 // testModSqrt is a helper for TestModSqrt, 1586 // which checks that ModSqrt can compute a square-root of elt^2. 1587 func testModSqrt(t *testing.T, elt, mod, sq, sqrt *Int) bool { 1588 var sqChk, sqrtChk, sqrtsq Int 1589 sq.Mul(elt, elt) 1590 sq.Mod(sq, mod) 1591 z := sqrt.ModSqrt(sq, mod) 1592 if z != sqrt { 1593 t.Errorf("ModSqrt returned wrong value %s", z) 1594 } 1595 1596 // test ModSqrt arguments outside the range [0,mod) 1597 sqChk.Add(sq, mod) 1598 z = sqrtChk.ModSqrt(&sqChk, mod) 1599 if z != &sqrtChk || z.Cmp(sqrt) != 0 { 1600 t.Errorf("ModSqrt returned inconsistent value %s", z) 1601 } 1602 sqChk.Sub(sq, mod) 1603 z = sqrtChk.ModSqrt(&sqChk, mod) 1604 if z != &sqrtChk || z.Cmp(sqrt) != 0 { 1605 t.Errorf("ModSqrt returned inconsistent value %s", z) 1606 } 1607 1608 // test x aliasing z 1609 z = sqrtChk.ModSqrt(sqrtChk.Set(sq), mod) 1610 if z != &sqrtChk || z.Cmp(sqrt) != 0 { 1611 t.Errorf("ModSqrt returned inconsistent value %s", z) 1612 } 1613 1614 // make sure we actually got a square root 1615 if sqrt.Cmp(elt) == 0 { 1616 return true // we found the "desired" square root 1617 } 1618 sqrtsq.Mul(sqrt, sqrt) // make sure we found the "other" one 1619 sqrtsq.Mod(&sqrtsq, mod) 1620 return sq.Cmp(&sqrtsq) == 0 1621 } 1622 1623 func TestModSqrt(t *testing.T) { 1624 var elt, mod, modx4, sq, sqrt Int 1625 r := rand.New(rand.NewSource(9)) 1626 for i, s := range primes[1:] { // skip 2, use only odd primes 1627 mod.SetString(s, 10) 1628 modx4.Lsh(&mod, 2) 1629 1630 // test a few random elements per prime 1631 for x := 1; x < 5; x++ { 1632 elt.Rand(r, &modx4) 1633 elt.Sub(&elt, &mod) // test range [-mod, 3*mod) 1634 if !testModSqrt(t, &elt, &mod, &sq, &sqrt) { 1635 t.Errorf("#%d: failed (sqrt(e) = %s)", i, &sqrt) 1636 } 1637 } 1638 1639 if testing.Short() && i > 2 { 1640 break 1641 } 1642 } 1643 1644 if testing.Short() { 1645 return 1646 } 1647 1648 // exhaustive test for small values 1649 for n := 3; n < 100; n++ { 1650 mod.SetInt64(int64(n)) 1651 if !mod.ProbablyPrime(10) { 1652 continue 1653 } 1654 isSquare := make([]bool, n) 1655 1656 // test all the squares 1657 for x := 1; x < n; x++ { 1658 elt.SetInt64(int64(x)) 1659 if !testModSqrt(t, &elt, &mod, &sq, &sqrt) { 1660 t.Errorf("#%d: failed (sqrt(%d,%d) = %s)", x, &elt, &mod, &sqrt) 1661 } 1662 isSquare[sq.Uint64()] = true 1663 } 1664 1665 // test all non-squares 1666 for x := 1; x < n; x++ { 1667 sq.SetInt64(int64(x)) 1668 z := sqrt.ModSqrt(&sq, &mod) 1669 if !isSquare[x] && z != nil { 1670 t.Errorf("#%d: failed (sqrt(%d,%d) = nil)", x, &sqrt, &mod) 1671 } 1672 } 1673 } 1674 } 1675 1676 func TestJacobi(t *testing.T) { 1677 testCases := []struct { 1678 x, y int64 1679 result int 1680 }{ 1681 {0, 1, 1}, 1682 {0, -1, 1}, 1683 {1, 1, 1}, 1684 {1, -1, 1}, 1685 {0, 5, 0}, 1686 {1, 5, 1}, 1687 {2, 5, -1}, 1688 {-2, 5, -1}, 1689 {2, -5, -1}, 1690 {-2, -5, 1}, 1691 {3, 5, -1}, 1692 {5, 5, 0}, 1693 {-5, 5, 0}, 1694 {6, 5, 1}, 1695 {6, -5, 1}, 1696 {-6, 5, 1}, 1697 {-6, -5, -1}, 1698 } 1699 1700 var x, y Int 1701 1702 for i, test := range testCases { 1703 x.SetInt64(test.x) 1704 y.SetInt64(test.y) 1705 expected := test.result 1706 actual := Jacobi(&x, &y) 1707 if actual != expected { 1708 t.Errorf("#%d: Jacobi(%d, %d) = %d, but expected %d", i, test.x, test.y, actual, expected) 1709 } 1710 } 1711 } 1712 1713 func TestJacobiPanic(t *testing.T) { 1714 const failureMsg = "test failure" 1715 defer func() { 1716 msg := recover() 1717 if msg == nil || msg == failureMsg { 1718 panic(msg) 1719 } 1720 t.Log(msg) 1721 }() 1722 x := NewInt(1) 1723 y := NewInt(2) 1724 // Jacobi should panic when the second argument is even. 1725 Jacobi(x, y) 1726 panic(failureMsg) 1727 } 1728 1729 func TestIssue2607(t *testing.T) { 1730 // This code sequence used to hang. 1731 n := NewInt(10) 1732 n.Rand(rand.New(rand.NewSource(9)), n) 1733 } 1734 1735 func TestSqrt(t *testing.T) { 1736 root := 0 1737 r := new(Int) 1738 for i := 0; i < 10000; i++ { 1739 if (root+1)*(root+1) <= i { 1740 root++ 1741 } 1742 n := NewInt(int64(i)) 1743 r.SetInt64(-2) 1744 r.Sqrt(n) 1745 if r.Cmp(NewInt(int64(root))) != 0 { 1746 t.Errorf("Sqrt(%v) = %v, want %v", n, r, root) 1747 } 1748 } 1749 1750 for i := 0; i < 1000; i += 10 { 1751 n, _ := new(Int).SetString("1"+strings.Repeat("0", i), 10) 1752 r := new(Int).Sqrt(n) 1753 root, _ := new(Int).SetString("1"+strings.Repeat("0", i/2), 10) 1754 if r.Cmp(root) != 0 { 1755 t.Errorf("Sqrt(1e%d) = %v, want 1e%d", i, r, i/2) 1756 } 1757 } 1758 1759 // Test aliasing. 1760 r.SetInt64(100) 1761 r.Sqrt(r) 1762 if r.Int64() != 10 { 1763 t.Errorf("Sqrt(100) = %v, want 10 (aliased output)", r.Int64()) 1764 } 1765 } 1766 1767 // We can't test this together with the other Exp tests above because 1768 // it requires a different receiver setup. 1769 func TestIssue22830(t *testing.T) { 1770 one := new(Int).SetInt64(1) 1771 base, _ := new(Int).SetString("84555555300000000000", 10) 1772 mod, _ := new(Int).SetString("66666670001111111111", 10) 1773 want, _ := new(Int).SetString("17888885298888888889", 10) 1774 1775 var tests = []int64{ 1776 0, 1, -1, 1777 } 1778 1779 for _, n := range tests { 1780 m := NewInt(n) 1781 if got := m.Exp(base, one, mod); got.Cmp(want) != 0 { 1782 t.Errorf("(%v).Exp(%s, 1, %s) = %s, want %s", n, base, mod, got, want) 1783 } 1784 } 1785 } 1786 1787 func BenchmarkSqrt(b *testing.B) { 1788 n, _ := new(Int).SetString("1"+strings.Repeat("0", 1001), 10) 1789 b.ResetTimer() 1790 t := new(Int) 1791 for i := 0; i < b.N; i++ { 1792 t.Sqrt(n) 1793 } 1794 } 1795 1796 func benchmarkIntSqr(b *testing.B, nwords int) { 1797 x := new(Int) 1798 x.abs = rndNat(nwords) 1799 t := new(Int) 1800 b.ResetTimer() 1801 for i := 0; i < b.N; i++ { 1802 t.Mul(x, x) 1803 } 1804 } 1805 1806 func BenchmarkIntSqr(b *testing.B) { 1807 for _, n := range sqrBenchSizes { 1808 if isRaceBuilder && n > 1e3 { 1809 continue 1810 } 1811 b.Run(fmt.Sprintf("%d", n), func(b *testing.B) { 1812 benchmarkIntSqr(b, n) 1813 }) 1814 } 1815 } 1816 1817 func benchmarkDiv(b *testing.B, aSize, bSize int) { 1818 var r = rand.New(rand.NewSource(1234)) 1819 aa := randInt(r, uint(aSize)) 1820 bb := randInt(r, uint(bSize)) 1821 if aa.Cmp(bb) < 0 { 1822 aa, bb = bb, aa 1823 } 1824 x := new(Int) 1825 y := new(Int) 1826 1827 b.ResetTimer() 1828 for i := 0; i < b.N; i++ { 1829 x.DivMod(aa, bb, y) 1830 } 1831 } 1832 1833 func BenchmarkDiv(b *testing.B) { 1834 sizes := []int{ 1835 10, 20, 50, 100, 200, 500, 1000, 1836 1e4, 1e5, 1e6, 1e7, 1837 } 1838 for _, i := range sizes { 1839 j := 2 * i 1840 b.Run(fmt.Sprintf("%d/%d", j, i), func(b *testing.B) { 1841 benchmarkDiv(b, j, i) 1842 }) 1843 } 1844 } 1845 1846 func TestFillBytes(t *testing.T) { 1847 checkResult := func(t *testing.T, buf []byte, want *Int) { 1848 t.Helper() 1849 got := new(Int).SetBytes(buf) 1850 if got.CmpAbs(want) != 0 { 1851 t.Errorf("got 0x%x, want 0x%x: %x", got, want, buf) 1852 } 1853 } 1854 panics := func(f func()) (panic bool) { 1855 defer func() { panic = recover() != nil }() 1856 f() 1857 return 1858 } 1859 1860 for _, n := range []string{ 1861 "0", 1862 "1000", 1863 "0xffffffff", 1864 "-0xffffffff", 1865 "0xffffffffffffffff", 1866 "0x10000000000000000", 1867 "0xabababababababababababababababababababababababababa", 1868 "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 1869 } { 1870 t.Run(n, func(t *testing.T) { 1871 t.Logf(n) 1872 x, ok := new(Int).SetString(n, 0) 1873 if !ok { 1874 panic("invalid test entry") 1875 } 1876 1877 // Perfectly sized buffer. 1878 byteLen := (x.BitLen() + 7) / 8 1879 buf := make([]byte, byteLen) 1880 checkResult(t, x.FillBytes(buf), x) 1881 1882 // Way larger, checking all bytes get zeroed. 1883 buf = make([]byte, 100) 1884 for i := range buf { 1885 buf[i] = 0xff 1886 } 1887 checkResult(t, x.FillBytes(buf), x) 1888 1889 // Too small. 1890 if byteLen > 0 { 1891 buf = make([]byte, byteLen-1) 1892 if !panics(func() { x.FillBytes(buf) }) { 1893 t.Errorf("expected panic for small buffer and value %x", x) 1894 } 1895 } 1896 }) 1897 } 1898 } 1899 1900 func TestNewIntMinInt64(t *testing.T) { 1901 // Test for uint64 cast in NewInt. 1902 want := int64(math.MinInt64) 1903 if got := NewInt(want).Int64(); got != want { 1904 t.Fatalf("wanted %d, got %d", want, got) 1905 } 1906 } 1907 1908 func TestNewIntAllocs(t *testing.T) { 1909 testenv.SkipIfOptimizationOff(t) 1910 for _, n := range []int64{0, 7, -7, 1 << 30, -1 << 30, 1 << 50, -1 << 50} { 1911 x := NewInt(3) 1912 got := testing.AllocsPerRun(100, func() { 1913 // NewInt should inline, and all its allocations 1914 // can happen on the stack. Passing the result of NewInt 1915 // to Add should not cause any of those allocations to escape. 1916 x.Add(x, NewInt(n)) 1917 }) 1918 if got != 0 { 1919 t.Errorf("x.Add(x, NewInt(%d)), wanted 0 allocations, got %f", n, got) 1920 } 1921 } 1922 }