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