github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/cmd/compile/internal/big/nat_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 "runtime" 9 "strings" 10 "testing" 11 ) 12 13 var cmpTests = []struct { 14 x, y nat 15 r int 16 }{ 17 {nil, nil, 0}, 18 {nil, nat(nil), 0}, 19 {nat(nil), nil, 0}, 20 {nat(nil), nat(nil), 0}, 21 {nat{0}, nat{0}, 0}, 22 {nat{0}, nat{1}, -1}, 23 {nat{1}, nat{0}, 1}, 24 {nat{1}, nat{1}, 0}, 25 {nat{0, _M}, nat{1}, 1}, 26 {nat{1}, nat{0, _M}, -1}, 27 {nat{1, _M}, nat{0, _M}, 1}, 28 {nat{0, _M}, nat{1, _M}, -1}, 29 {nat{16, 571956, 8794, 68}, nat{837, 9146, 1, 754489}, -1}, 30 {nat{34986, 41, 105, 1957}, nat{56, 7458, 104, 1957}, 1}, 31 } 32 33 func TestCmp(t *testing.T) { 34 for i, a := range cmpTests { 35 r := a.x.cmp(a.y) 36 if r != a.r { 37 t.Errorf("#%d got r = %v; want %v", i, r, a.r) 38 } 39 } 40 } 41 42 type funNN func(z, x, y nat) nat 43 type argNN struct { 44 z, x, y nat 45 } 46 47 var sumNN = []argNN{ 48 {}, 49 {nat{1}, nil, nat{1}}, 50 {nat{1111111110}, nat{123456789}, nat{987654321}}, 51 {nat{0, 0, 0, 1}, nil, nat{0, 0, 0, 1}}, 52 {nat{0, 0, 0, 1111111110}, nat{0, 0, 0, 123456789}, nat{0, 0, 0, 987654321}}, 53 {nat{0, 0, 0, 1}, nat{0, 0, _M}, nat{0, 0, 1}}, 54 } 55 56 var prodNN = []argNN{ 57 {}, 58 {nil, nil, nil}, 59 {nil, nat{991}, nil}, 60 {nat{991}, nat{991}, nat{1}}, 61 {nat{991 * 991}, nat{991}, nat{991}}, 62 {nat{0, 0, 991 * 991}, nat{0, 991}, nat{0, 991}}, 63 {nat{1 * 991, 2 * 991, 3 * 991, 4 * 991}, nat{1, 2, 3, 4}, nat{991}}, 64 {nat{4, 11, 20, 30, 20, 11, 4}, nat{1, 2, 3, 4}, nat{4, 3, 2, 1}}, 65 // 3^100 * 3^28 = 3^128 66 { 67 natFromString("11790184577738583171520872861412518665678211592275841109096961"), 68 natFromString("515377520732011331036461129765621272702107522001"), 69 natFromString("22876792454961"), 70 }, 71 // z = 111....1 (70000 digits) 72 // x = 10^(99*700) + ... + 10^1400 + 10^700 + 1 73 // y = 111....1 (700 digits, larger than Karatsuba threshold on 32-bit and 64-bit) 74 { 75 natFromString(strings.Repeat("1", 70000)), 76 natFromString("1" + strings.Repeat(strings.Repeat("0", 699)+"1", 99)), 77 natFromString(strings.Repeat("1", 700)), 78 }, 79 // z = 111....1 (20000 digits) 80 // x = 10^10000 + 1 81 // y = 111....1 (10000 digits) 82 { 83 natFromString(strings.Repeat("1", 20000)), 84 natFromString("1" + strings.Repeat("0", 9999) + "1"), 85 natFromString(strings.Repeat("1", 10000)), 86 }, 87 } 88 89 func natFromString(s string) nat { 90 x, _, _, err := nat(nil).scan(strings.NewReader(s), 0, false) 91 if err != nil { 92 panic(err) 93 } 94 return x 95 } 96 97 func TestSet(t *testing.T) { 98 for _, a := range sumNN { 99 z := nat(nil).set(a.z) 100 if z.cmp(a.z) != 0 { 101 t.Errorf("got z = %v; want %v", z, a.z) 102 } 103 } 104 } 105 106 func testFunNN(t *testing.T, msg string, f funNN, a argNN) { 107 z := f(nil, a.x, a.y) 108 if z.cmp(a.z) != 0 { 109 t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, z, a.z) 110 } 111 } 112 113 func TestFunNN(t *testing.T) { 114 for _, a := range sumNN { 115 arg := a 116 testFunNN(t, "add", nat.add, arg) 117 118 arg = argNN{a.z, a.y, a.x} 119 testFunNN(t, "add symmetric", nat.add, arg) 120 121 arg = argNN{a.x, a.z, a.y} 122 testFunNN(t, "sub", nat.sub, arg) 123 124 arg = argNN{a.y, a.z, a.x} 125 testFunNN(t, "sub symmetric", nat.sub, arg) 126 } 127 128 for _, a := range prodNN { 129 arg := a 130 testFunNN(t, "mul", nat.mul, arg) 131 132 arg = argNN{a.z, a.y, a.x} 133 testFunNN(t, "mul symmetric", nat.mul, arg) 134 } 135 } 136 137 var mulRangesN = []struct { 138 a, b uint64 139 prod string 140 }{ 141 {0, 0, "0"}, 142 {1, 1, "1"}, 143 {1, 2, "2"}, 144 {1, 3, "6"}, 145 {10, 10, "10"}, 146 {0, 100, "0"}, 147 {0, 1e9, "0"}, 148 {1, 0, "1"}, // empty range 149 {100, 1, "1"}, // empty range 150 {1, 10, "3628800"}, // 10! 151 {1, 20, "2432902008176640000"}, // 20! 152 {1, 100, 153 "933262154439441526816992388562667004907159682643816214685929" + 154 "638952175999932299156089414639761565182862536979208272237582" + 155 "51185210916864000000000000000000000000", // 100! 156 }, 157 } 158 159 func TestMulRangeN(t *testing.T) { 160 for i, r := range mulRangesN { 161 prod := string(nat(nil).mulRange(r.a, r.b).utoa(10)) 162 if prod != r.prod { 163 t.Errorf("#%d: got %s; want %s", i, prod, r.prod) 164 } 165 } 166 } 167 168 // allocBytes returns the number of bytes allocated by invoking f. 169 func allocBytes(f func()) uint64 { 170 var stats runtime.MemStats 171 runtime.ReadMemStats(&stats) 172 t := stats.TotalAlloc 173 f() 174 runtime.ReadMemStats(&stats) 175 return stats.TotalAlloc - t 176 } 177 178 // TestMulUnbalanced tests that multiplying numbers of different lengths 179 // does not cause deep recursion and in turn allocate too much memory. 180 // Test case for issue 3807. 181 func TestMulUnbalanced(t *testing.T) { 182 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1)) 183 x := rndNat(50000) 184 y := rndNat(40) 185 allocSize := allocBytes(func() { 186 nat(nil).mul(x, y) 187 }) 188 inputSize := uint64(len(x)+len(y)) * _S 189 if ratio := allocSize / uint64(inputSize); ratio > 10 { 190 t.Errorf("multiplication uses too much memory (%d > %d times the size of inputs)", allocSize, ratio) 191 } 192 } 193 194 func rndNat(n int) nat { 195 return nat(rndV(n)).norm() 196 } 197 198 func BenchmarkMul(b *testing.B) { 199 mulx := rndNat(1e4) 200 muly := rndNat(1e4) 201 b.ResetTimer() 202 for i := 0; i < b.N; i++ { 203 var z nat 204 z.mul(mulx, muly) 205 } 206 } 207 208 func TestNLZ(t *testing.T) { 209 var x Word = _B >> 1 210 for i := 0; i <= _W; i++ { 211 if int(nlz(x)) != i { 212 t.Errorf("failed at %x: got %d want %d", x, nlz(x), i) 213 } 214 x >>= 1 215 } 216 } 217 218 type shiftTest struct { 219 in nat 220 shift uint 221 out nat 222 } 223 224 var leftShiftTests = []shiftTest{ 225 {nil, 0, nil}, 226 {nil, 1, nil}, 227 {natOne, 0, natOne}, 228 {natOne, 1, natTwo}, 229 {nat{1 << (_W - 1)}, 1, nat{0}}, 230 {nat{1 << (_W - 1), 0}, 1, nat{0, 1}}, 231 } 232 233 func TestShiftLeft(t *testing.T) { 234 for i, test := range leftShiftTests { 235 var z nat 236 z = z.shl(test.in, test.shift) 237 for j, d := range test.out { 238 if j >= len(z) || z[j] != d { 239 t.Errorf("#%d: got: %v want: %v", i, z, test.out) 240 break 241 } 242 } 243 } 244 } 245 246 var rightShiftTests = []shiftTest{ 247 {nil, 0, nil}, 248 {nil, 1, nil}, 249 {natOne, 0, natOne}, 250 {natOne, 1, nil}, 251 {natTwo, 1, natOne}, 252 {nat{0, 1}, 1, nat{1 << (_W - 1)}}, 253 {nat{2, 1, 1}, 1, nat{1<<(_W-1) + 1, 1 << (_W - 1)}}, 254 } 255 256 func TestShiftRight(t *testing.T) { 257 for i, test := range rightShiftTests { 258 var z nat 259 z = z.shr(test.in, test.shift) 260 for j, d := range test.out { 261 if j >= len(z) || z[j] != d { 262 t.Errorf("#%d: got: %v want: %v", i, z, test.out) 263 break 264 } 265 } 266 } 267 } 268 269 type modWTest struct { 270 in string 271 dividend string 272 out string 273 } 274 275 var modWTests32 = []modWTest{ 276 {"23492635982634928349238759823742", "252341", "220170"}, 277 } 278 279 var modWTests64 = []modWTest{ 280 {"6527895462947293856291561095690465243862946", "524326975699234", "375066989628668"}, 281 } 282 283 func runModWTests(t *testing.T, tests []modWTest) { 284 for i, test := range tests { 285 in, _ := new(Int).SetString(test.in, 10) 286 d, _ := new(Int).SetString(test.dividend, 10) 287 out, _ := new(Int).SetString(test.out, 10) 288 289 r := in.abs.modW(d.abs[0]) 290 if r != out.abs[0] { 291 t.Errorf("#%d failed: got %d want %s", i, r, out) 292 } 293 } 294 } 295 296 func TestModW(t *testing.T) { 297 if _W >= 32 { 298 runModWTests(t, modWTests32) 299 } 300 if _W >= 64 { 301 runModWTests(t, modWTests64) 302 } 303 } 304 305 func TestTrailingZeroBits(t *testing.T) { 306 // test 0 case explicitly 307 if n := trailingZeroBits(0); n != 0 { 308 t.Errorf("got trailingZeroBits(0) = %d; want 0", n) 309 } 310 311 x := Word(1) 312 for i := uint(0); i < _W; i++ { 313 n := trailingZeroBits(x) 314 if n != i { 315 t.Errorf("got trailingZeroBits(%#x) = %d; want %d", x, n, i%_W) 316 } 317 x <<= 1 318 } 319 320 // test 0 case explicitly 321 if n := nat(nil).trailingZeroBits(); n != 0 { 322 t.Errorf("got nat(nil).trailingZeroBits() = %d; want 0", n) 323 } 324 325 y := nat(nil).set(natOne) 326 for i := uint(0); i <= 3*_W; i++ { 327 n := y.trailingZeroBits() 328 if n != i { 329 t.Errorf("got 0x%s.trailingZeroBits() = %d; want %d", y.utoa(16), n, i) 330 } 331 y = y.shl(y, 1) 332 } 333 } 334 335 var montgomeryTests = []struct { 336 x, y, m string 337 k0 uint64 338 out32, out64 string 339 }{ 340 { 341 "0xffffffffffffffffffffffffffffffffffffffffffffffffe", 342 "0xffffffffffffffffffffffffffffffffffffffffffffffffe", 343 "0xfffffffffffffffffffffffffffffffffffffffffffffffff", 344 1, 345 "0x1000000000000000000000000000000000000000000", 346 "0x10000000000000000000000000000000000", 347 }, 348 { 349 "0x000000000ffffff5", 350 "0x000000000ffffff0", 351 "0x0000000010000001", 352 0xff0000000fffffff, 353 "0x000000000bfffff4", 354 "0x0000000003400001", 355 }, 356 { 357 "0x0000000080000000", 358 "0x00000000ffffffff", 359 "0x1000000000000001", 360 0xfffffffffffffff, 361 "0x0800000008000001", 362 "0x0800000008000001", 363 }, 364 { 365 "0x0000000080000000", 366 "0x0000000080000000", 367 "0xffffffff00000001", 368 0xfffffffeffffffff, 369 "0xbfffffff40000001", 370 "0xbfffffff40000001", 371 }, 372 { 373 "0x0000000080000000", 374 "0x0000000080000000", 375 "0x00ffffff00000001", 376 0xfffffeffffffff, 377 "0xbfffff40000001", 378 "0xbfffff40000001", 379 }, 380 { 381 "0x0000000080000000", 382 "0x0000000080000000", 383 "0x0000ffff00000001", 384 0xfffeffffffff, 385 "0xbfff40000001", 386 "0xbfff40000001", 387 }, 388 { 389 "0x3321ffffffffffffffffffffffffffff00000000000022222623333333332bbbb888c0", 390 "0x3321ffffffffffffffffffffffffffff00000000000022222623333333332bbbb888c0", 391 "0x33377fffffffffffffffffffffffffffffffffffffffffffff0000000000022222eee1", 392 0xdecc8f1249812adf, 393 "0x04eb0e11d72329dc0915f86784820fc403275bf2f6620a20e0dd344c5cd0875e50deb5", 394 "0x0d7144739a7d8e11d72329dc0915f86784820fc403275bf2f61ed96f35dd34dbb3d6a0", 395 }, 396 { 397 "0x10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffff00000000000022222223333333333444444444", 398 "0x10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffff999999999999999aaabbbbbbbbcccccccccccc", 399 "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff33377fffffffffffffffffffffffffffffffffffffffffffff0000000000022222eee1", 400 0xdecc8f1249812adf, 401 "0x5c0d52f451aec609b15da8e5e5626c4eaa88723bdeac9d25ca9b961269400410ca208a16af9c2fb07d7a11c7772cba02c22f9711078d51a3797eb18e691295293284d988e349fa6deba46b25a4ecd9f715", 402 "0x92fcad4b5c0d52f451aec609b15da8e5e5626c4eaa88723bdeac9d25ca9b961269400410ca208a16af9c2fb07d799c32fe2f3cc5422f9711078d51a3797eb18e691295293284d8f5e69caf6decddfe1df6", 403 }, 404 } 405 406 func TestMontgomery(t *testing.T) { 407 one := NewInt(1) 408 _B := new(Int).Lsh(one, _W) 409 for i, test := range montgomeryTests { 410 x := natFromString(test.x) 411 y := natFromString(test.y) 412 m := natFromString(test.m) 413 for len(x) < len(m) { 414 x = append(x, 0) 415 } 416 for len(y) < len(m) { 417 y = append(y, 0) 418 } 419 420 if x.cmp(m) > 0 { 421 _, r := nat(nil).div(nil, x, m) 422 t.Errorf("#%d: x > m (0x%s > 0x%s; use 0x%s)", i, x.utoa(16), m.utoa(16), r.utoa(16)) 423 } 424 if y.cmp(m) > 0 { 425 _, r := nat(nil).div(nil, x, m) 426 t.Errorf("#%d: y > m (0x%s > 0x%s; use 0x%s)", i, y.utoa(16), m.utoa(16), r.utoa(16)) 427 } 428 429 var out nat 430 if _W == 32 { 431 out = natFromString(test.out32) 432 } else { 433 out = natFromString(test.out64) 434 } 435 436 // t.Logf("#%d: len=%d\n", i, len(m)) 437 438 // check output in table 439 xi := &Int{abs: x} 440 yi := &Int{abs: y} 441 mi := &Int{abs: m} 442 p := new(Int).Mod(new(Int).Mul(xi, new(Int).Mul(yi, new(Int).ModInverse(new(Int).Lsh(one, uint(len(m))*_W), mi))), mi) 443 if out.cmp(p.abs.norm()) != 0 { 444 t.Errorf("#%d: out in table=0x%s, computed=0x%s", i, out.utoa(16), p.abs.norm().utoa(16)) 445 } 446 447 // check k0 in table 448 k := new(Int).Mod(&Int{abs: m}, _B) 449 k = new(Int).Sub(_B, k) 450 k = new(Int).Mod(k, _B) 451 k0 := Word(new(Int).ModInverse(k, _B).Uint64()) 452 if k0 != Word(test.k0) { 453 t.Errorf("#%d: k0 in table=%#x, computed=%#x\n", i, test.k0, k0) 454 } 455 456 // check montgomery with correct k0 produces correct output 457 z := nat(nil).montgomery(x, y, m, k0, len(m)) 458 z = z.norm() 459 if z.cmp(out) != 0 { 460 t.Errorf("#%d: got 0x%s want 0x%s", i, z.utoa(16), out.utoa(16)) 461 } 462 } 463 } 464 465 var expNNTests = []struct { 466 x, y, m string 467 out string 468 }{ 469 {"0", "0", "0", "1"}, 470 {"0", "0", "1", "0"}, 471 {"1", "1", "1", "0"}, 472 {"2", "1", "1", "0"}, 473 {"2", "2", "1", "0"}, 474 {"10", "100000000000", "1", "0"}, 475 {"0x8000000000000000", "2", "", "0x40000000000000000000000000000000"}, 476 {"0x8000000000000000", "2", "6719", "4944"}, 477 {"0x8000000000000000", "3", "6719", "5447"}, 478 {"0x8000000000000000", "1000", "6719", "1603"}, 479 {"0x8000000000000000", "1000000", "6719", "3199"}, 480 { 481 "2938462938472983472983659726349017249287491026512746239764525612965293865296239471239874193284792387498274256129746192347", 482 "298472983472983471903246121093472394872319615612417471234712061", 483 "29834729834729834729347290846729561262544958723956495615629569234729836259263598127342374289365912465901365498236492183464", 484 "23537740700184054162508175125554701713153216681790245129157191391322321508055833908509185839069455749219131480588829346291", 485 }, 486 { 487 "11521922904531591643048817447554701904414021819823889996244743037378330903763518501116638828335352811871131385129455853417360623007349090150042001944696604737499160174391019030572483602867266711107136838523916077674888297896995042968746762200926853379", 488 "426343618817810911523", 489 "444747819283133684179", 490 "42", 491 }, 492 } 493 494 func TestExpNN(t *testing.T) { 495 for i, test := range expNNTests { 496 x := natFromString(test.x) 497 y := natFromString(test.y) 498 out := natFromString(test.out) 499 500 var m nat 501 if len(test.m) > 0 { 502 m = natFromString(test.m) 503 } 504 505 z := nat(nil).expNN(x, y, m) 506 if z.cmp(out) != 0 { 507 t.Errorf("#%d got %s want %s", i, z.utoa(10), out.utoa(10)) 508 } 509 } 510 } 511 512 func ExpHelper(b *testing.B, x, y Word) { 513 var z nat 514 for i := 0; i < b.N; i++ { 515 z.expWW(x, y) 516 } 517 } 518 519 func BenchmarkExp3Power0x10(b *testing.B) { ExpHelper(b, 3, 0x10) } 520 func BenchmarkExp3Power0x40(b *testing.B) { ExpHelper(b, 3, 0x40) } 521 func BenchmarkExp3Power0x100(b *testing.B) { ExpHelper(b, 3, 0x100) } 522 func BenchmarkExp3Power0x400(b *testing.B) { ExpHelper(b, 3, 0x400) } 523 func BenchmarkExp3Power0x1000(b *testing.B) { ExpHelper(b, 3, 0x1000) } 524 func BenchmarkExp3Power0x4000(b *testing.B) { ExpHelper(b, 3, 0x4000) } 525 func BenchmarkExp3Power0x10000(b *testing.B) { ExpHelper(b, 3, 0x10000) } 526 func BenchmarkExp3Power0x40000(b *testing.B) { ExpHelper(b, 3, 0x40000) } 527 func BenchmarkExp3Power0x100000(b *testing.B) { ExpHelper(b, 3, 0x100000) } 528 func BenchmarkExp3Power0x400000(b *testing.B) { ExpHelper(b, 3, 0x400000) } 529 530 func fibo(n int) nat { 531 switch n { 532 case 0: 533 return nil 534 case 1: 535 return nat{1} 536 } 537 f0 := fibo(0) 538 f1 := fibo(1) 539 var f2 nat 540 for i := 1; i < n; i++ { 541 f2 = f2.add(f0, f1) 542 f0, f1, f2 = f1, f2, f0 543 } 544 return f1 545 } 546 547 var fiboNums = []string{ 548 "0", 549 "55", 550 "6765", 551 "832040", 552 "102334155", 553 "12586269025", 554 "1548008755920", 555 "190392490709135", 556 "23416728348467685", 557 "2880067194370816120", 558 "354224848179261915075", 559 } 560 561 func TestFibo(t *testing.T) { 562 for i, want := range fiboNums { 563 n := i * 10 564 got := string(fibo(n).utoa(10)) 565 if got != want { 566 t.Errorf("fibo(%d) failed: got %s want %s", n, got, want) 567 } 568 } 569 } 570 571 func BenchmarkFibo(b *testing.B) { 572 for i := 0; i < b.N; i++ { 573 fibo(1e0) 574 fibo(1e1) 575 fibo(1e2) 576 fibo(1e3) 577 fibo(1e4) 578 fibo(1e5) 579 } 580 } 581 582 var bitTests = []struct { 583 x string 584 i uint 585 want uint 586 }{ 587 {"0", 0, 0}, 588 {"0", 1, 0}, 589 {"0", 1000, 0}, 590 591 {"0x1", 0, 1}, 592 {"0x10", 0, 0}, 593 {"0x10", 3, 0}, 594 {"0x10", 4, 1}, 595 {"0x10", 5, 0}, 596 597 {"0x8000000000000000", 62, 0}, 598 {"0x8000000000000000", 63, 1}, 599 {"0x8000000000000000", 64, 0}, 600 601 {"0x3" + strings.Repeat("0", 32), 127, 0}, 602 {"0x3" + strings.Repeat("0", 32), 128, 1}, 603 {"0x3" + strings.Repeat("0", 32), 129, 1}, 604 {"0x3" + strings.Repeat("0", 32), 130, 0}, 605 } 606 607 func TestBit(t *testing.T) { 608 for i, test := range bitTests { 609 x := natFromString(test.x) 610 if got := x.bit(test.i); got != test.want { 611 t.Errorf("#%d: %s.bit(%d) = %v; want %v", i, test.x, test.i, got, test.want) 612 } 613 } 614 } 615 616 var stickyTests = []struct { 617 x string 618 i uint 619 want uint 620 }{ 621 {"0", 0, 0}, 622 {"0", 1, 0}, 623 {"0", 1000, 0}, 624 625 {"0x1", 0, 0}, 626 {"0x1", 1, 1}, 627 628 {"0x1350", 0, 0}, 629 {"0x1350", 4, 0}, 630 {"0x1350", 5, 1}, 631 632 {"0x8000000000000000", 63, 0}, 633 {"0x8000000000000000", 64, 1}, 634 635 {"0x1" + strings.Repeat("0", 100), 400, 0}, 636 {"0x1" + strings.Repeat("0", 100), 401, 1}, 637 } 638 639 func TestSticky(t *testing.T) { 640 for i, test := range stickyTests { 641 x := natFromString(test.x) 642 if got := x.sticky(test.i); got != test.want { 643 t.Errorf("#%d: %s.sticky(%d) = %v; want %v", i, test.x, test.i, got, test.want) 644 } 645 if test.want == 1 { 646 // all subsequent i's should also return 1 647 for d := uint(1); d <= 3; d++ { 648 if got := x.sticky(test.i + d); got != 1 { 649 t.Errorf("#%d: %s.sticky(%d) = %v; want %v", i, test.x, test.i+d, got, 1) 650 } 651 } 652 } 653 } 654 }