github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/math/big/nat.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 // This file implements unsigned multi-precision integers (natural 6 // numbers). They are the building blocks for the implementation 7 // of signed integers, rationals, and floating-point numbers. 8 // 9 // Caution: This implementation relies on the function "alias" 10 // which assumes that (nat) slice capacities are never 11 // changed (no 3-operand slice expressions). If that 12 // changes, alias needs to be updated for correctness. 13 14 package big 15 16 import ( 17 "encoding/binary" 18 "math/bits" 19 "math/rand" 20 "sync" 21 ) 22 23 // An unsigned integer x of the form 24 // 25 // x = x[n-1]*_B^(n-1) + x[n-2]*_B^(n-2) + ... + x[1]*_B + x[0] 26 // 27 // with 0 <= x[i] < _B and 0 <= i < n is stored in a slice of length n, 28 // with the digits x[i] as the slice elements. 29 // 30 // A number is normalized if the slice contains no leading 0 digits. 31 // During arithmetic operations, denormalized values may occur but are 32 // always normalized before returning the final result. The normalized 33 // representation of 0 is the empty or nil slice (length = 0). 34 // 35 type nat []Word 36 37 var ( 38 natOne = nat{1} 39 natTwo = nat{2} 40 natTen = nat{10} 41 ) 42 43 func (z nat) clear() { 44 for i := range z { 45 z[i] = 0 46 } 47 } 48 49 func (z nat) norm() nat { 50 i := len(z) 51 for i > 0 && z[i-1] == 0 { 52 i-- 53 } 54 return z[0:i] 55 } 56 57 func (z nat) make(n int) nat { 58 if n <= cap(z) { 59 return z[:n] // reuse z 60 } 61 // Choosing a good value for e has significant performance impact 62 // because it increases the chance that a value can be reused. 63 const e = 4 // extra capacity 64 return make(nat, n, n+e) 65 } 66 67 func (z nat) setWord(x Word) nat { 68 if x == 0 { 69 return z[:0] 70 } 71 z = z.make(1) 72 z[0] = x 73 return z 74 } 75 76 func (z nat) setUint64(x uint64) nat { 77 // single-word value 78 if w := Word(x); uint64(w) == x { 79 return z.setWord(w) 80 } 81 // 2-word value 82 z = z.make(2) 83 z[1] = Word(x >> 32) 84 z[0] = Word(x) 85 return z 86 } 87 88 func (z nat) set(x nat) nat { 89 z = z.make(len(x)) 90 copy(z, x) 91 return z 92 } 93 94 func (z nat) add(x, y nat) nat { 95 m := len(x) 96 n := len(y) 97 98 switch { 99 case m < n: 100 return z.add(y, x) 101 case m == 0: 102 // n == 0 because m >= n; result is 0 103 return z[:0] 104 case n == 0: 105 // result is x 106 return z.set(x) 107 } 108 // m > 0 109 110 z = z.make(m + 1) 111 c := addVV(z[0:n], x, y) 112 if m > n { 113 c = addVW(z[n:m], x[n:], c) 114 } 115 z[m] = c 116 117 return z.norm() 118 } 119 120 func (z nat) sub(x, y nat) nat { 121 m := len(x) 122 n := len(y) 123 124 switch { 125 case m < n: 126 panic("underflow") 127 case m == 0: 128 // n == 0 because m >= n; result is 0 129 return z[:0] 130 case n == 0: 131 // result is x 132 return z.set(x) 133 } 134 // m > 0 135 136 z = z.make(m) 137 c := subVV(z[0:n], x, y) 138 if m > n { 139 c = subVW(z[n:], x[n:], c) 140 } 141 if c != 0 { 142 panic("underflow") 143 } 144 145 return z.norm() 146 } 147 148 func (x nat) cmp(y nat) (r int) { 149 m := len(x) 150 n := len(y) 151 if m != n || m == 0 { 152 switch { 153 case m < n: 154 r = -1 155 case m > n: 156 r = 1 157 } 158 return 159 } 160 161 i := m - 1 162 for i > 0 && x[i] == y[i] { 163 i-- 164 } 165 166 switch { 167 case x[i] < y[i]: 168 r = -1 169 case x[i] > y[i]: 170 r = 1 171 } 172 return 173 } 174 175 func (z nat) mulAddWW(x nat, y, r Word) nat { 176 m := len(x) 177 if m == 0 || y == 0 { 178 return z.setWord(r) // result is r 179 } 180 // m > 0 181 182 z = z.make(m + 1) 183 z[m] = mulAddVWW(z[0:m], x, y, r) 184 185 return z.norm() 186 } 187 188 // basicMul multiplies x and y and leaves the result in z. 189 // The (non-normalized) result is placed in z[0 : len(x) + len(y)]. 190 func basicMul(z, x, y nat) { 191 z[0 : len(x)+len(y)].clear() // initialize z 192 for i, d := range y { 193 if d != 0 { 194 z[len(x)+i] = addMulVVW(z[i:i+len(x)], x, d) 195 } 196 } 197 } 198 199 // montgomery computes z mod m = x*y*2**(-n*_W) mod m, 200 // assuming k = -1/m mod 2**_W. 201 // z is used for storing the result which is returned; 202 // z must not alias x, y or m. 203 // See Gueron, "Efficient Software Implementations of Modular Exponentiation". 204 // https://eprint.iacr.org/2011/239.pdf 205 // In the terminology of that paper, this is an "Almost Montgomery Multiplication": 206 // x and y are required to satisfy 0 <= z < 2**(n*_W) and then the result 207 // z is guaranteed to satisfy 0 <= z < 2**(n*_W), but it may not be < m. 208 func (z nat) montgomery(x, y, m nat, k Word, n int) nat { 209 // This code assumes x, y, m are all the same length, n. 210 // (required by addMulVVW and the for loop). 211 // It also assumes that x, y are already reduced mod m, 212 // or else the result will not be properly reduced. 213 if len(x) != n || len(y) != n || len(m) != n { 214 panic("math/big: mismatched montgomery number lengths") 215 } 216 z = z.make(n * 2) 217 z.clear() 218 var c Word 219 for i := 0; i < n; i++ { 220 d := y[i] 221 c2 := addMulVVW(z[i:n+i], x, d) 222 t := z[i] * k 223 c3 := addMulVVW(z[i:n+i], m, t) 224 cx := c + c2 225 cy := cx + c3 226 z[n+i] = cy 227 if cx < c2 || cy < c3 { 228 c = 1 229 } else { 230 c = 0 231 } 232 } 233 if c != 0 { 234 subVV(z[:n], z[n:], m) 235 } else { 236 copy(z[:n], z[n:]) 237 } 238 return z[:n] 239 } 240 241 // Fast version of z[0:n+n>>1].add(z[0:n+n>>1], x[0:n]) w/o bounds checks. 242 // Factored out for readability - do not use outside karatsuba. 243 func karatsubaAdd(z, x nat, n int) { 244 if c := addVV(z[0:n], z, x); c != 0 { 245 addVW(z[n:n+n>>1], z[n:], c) 246 } 247 } 248 249 // Like karatsubaAdd, but does subtract. 250 func karatsubaSub(z, x nat, n int) { 251 if c := subVV(z[0:n], z, x); c != 0 { 252 subVW(z[n:n+n>>1], z[n:], c) 253 } 254 } 255 256 // Operands that are shorter than karatsubaThreshold are multiplied using 257 // "grade school" multiplication; for longer operands the Karatsuba algorithm 258 // is used. 259 var karatsubaThreshold = 40 // computed by calibrate_test.go 260 261 // karatsuba multiplies x and y and leaves the result in z. 262 // Both x and y must have the same length n and n must be a 263 // power of 2. The result vector z must have len(z) >= 6*n. 264 // The (non-normalized) result is placed in z[0 : 2*n]. 265 func karatsuba(z, x, y nat) { 266 n := len(y) 267 268 // Switch to basic multiplication if numbers are odd or small. 269 // (n is always even if karatsubaThreshold is even, but be 270 // conservative) 271 if n&1 != 0 || n < karatsubaThreshold || n < 2 { 272 basicMul(z, x, y) 273 return 274 } 275 // n&1 == 0 && n >= karatsubaThreshold && n >= 2 276 277 // Karatsuba multiplication is based on the observation that 278 // for two numbers x and y with: 279 // 280 // x = x1*b + x0 281 // y = y1*b + y0 282 // 283 // the product x*y can be obtained with 3 products z2, z1, z0 284 // instead of 4: 285 // 286 // x*y = x1*y1*b*b + (x1*y0 + x0*y1)*b + x0*y0 287 // = z2*b*b + z1*b + z0 288 // 289 // with: 290 // 291 // xd = x1 - x0 292 // yd = y0 - y1 293 // 294 // z1 = xd*yd + z2 + z0 295 // = (x1-x0)*(y0 - y1) + z2 + z0 296 // = x1*y0 - x1*y1 - x0*y0 + x0*y1 + z2 + z0 297 // = x1*y0 - z2 - z0 + x0*y1 + z2 + z0 298 // = x1*y0 + x0*y1 299 300 // split x, y into "digits" 301 n2 := n >> 1 // n2 >= 1 302 x1, x0 := x[n2:], x[0:n2] // x = x1*b + y0 303 y1, y0 := y[n2:], y[0:n2] // y = y1*b + y0 304 305 // z is used for the result and temporary storage: 306 // 307 // 6*n 5*n 4*n 3*n 2*n 1*n 0*n 308 // z = [z2 copy|z0 copy| xd*yd | yd:xd | x1*y1 | x0*y0 ] 309 // 310 // For each recursive call of karatsuba, an unused slice of 311 // z is passed in that has (at least) half the length of the 312 // caller's z. 313 314 // compute z0 and z2 with the result "in place" in z 315 karatsuba(z, x0, y0) // z0 = x0*y0 316 karatsuba(z[n:], x1, y1) // z2 = x1*y1 317 318 // compute xd (or the negative value if underflow occurs) 319 s := 1 // sign of product xd*yd 320 xd := z[2*n : 2*n+n2] 321 if subVV(xd, x1, x0) != 0 { // x1-x0 322 s = -s 323 subVV(xd, x0, x1) // x0-x1 324 } 325 326 // compute yd (or the negative value if underflow occurs) 327 yd := z[2*n+n2 : 3*n] 328 if subVV(yd, y0, y1) != 0 { // y0-y1 329 s = -s 330 subVV(yd, y1, y0) // y1-y0 331 } 332 333 // p = (x1-x0)*(y0-y1) == x1*y0 - x1*y1 - x0*y0 + x0*y1 for s > 0 334 // p = (x0-x1)*(y0-y1) == x0*y0 - x0*y1 - x1*y0 + x1*y1 for s < 0 335 p := z[n*3:] 336 karatsuba(p, xd, yd) 337 338 // save original z2:z0 339 // (ok to use upper half of z since we're done recursing) 340 r := z[n*4:] 341 copy(r, z[:n*2]) 342 343 // add up all partial products 344 // 345 // 2*n n 0 346 // z = [ z2 | z0 ] 347 // + [ z0 ] 348 // + [ z2 ] 349 // + [ p ] 350 // 351 karatsubaAdd(z[n2:], r, n) 352 karatsubaAdd(z[n2:], r[n:], n) 353 if s > 0 { 354 karatsubaAdd(z[n2:], p, n) 355 } else { 356 karatsubaSub(z[n2:], p, n) 357 } 358 } 359 360 // alias reports whether x and y share the same base array. 361 // Note: alias assumes that the capacity of underlying arrays 362 // is never changed for nat values; i.e. that there are 363 // no 3-operand slice expressions in this code (or worse, 364 // reflect-based operations to the same effect). 365 func alias(x, y nat) bool { 366 return cap(x) > 0 && cap(y) > 0 && &x[0:cap(x)][cap(x)-1] == &y[0:cap(y)][cap(y)-1] 367 } 368 369 // addAt implements z += x<<(_W*i); z must be long enough. 370 // (we don't use nat.add because we need z to stay the same 371 // slice, and we don't need to normalize z after each addition) 372 func addAt(z, x nat, i int) { 373 if n := len(x); n > 0 { 374 if c := addVV(z[i:i+n], z[i:], x); c != 0 { 375 j := i + n 376 if j < len(z) { 377 addVW(z[j:], z[j:], c) 378 } 379 } 380 } 381 } 382 383 func max(x, y int) int { 384 if x > y { 385 return x 386 } 387 return y 388 } 389 390 // karatsubaLen computes an approximation to the maximum k <= n such that 391 // k = p<<i for a number p <= threshold and an i >= 0. Thus, the 392 // result is the largest number that can be divided repeatedly by 2 before 393 // becoming about the value of threshold. 394 func karatsubaLen(n, threshold int) int { 395 i := uint(0) 396 for n > threshold { 397 n >>= 1 398 i++ 399 } 400 return n << i 401 } 402 403 func (z nat) mul(x, y nat) nat { 404 m := len(x) 405 n := len(y) 406 407 switch { 408 case m < n: 409 return z.mul(y, x) 410 case m == 0 || n == 0: 411 return z[:0] 412 case n == 1: 413 return z.mulAddWW(x, y[0], 0) 414 } 415 // m >= n > 1 416 417 // determine if z can be reused 418 if alias(z, x) || alias(z, y) { 419 z = nil // z is an alias for x or y - cannot reuse 420 } 421 422 // use basic multiplication if the numbers are small 423 if n < karatsubaThreshold { 424 z = z.make(m + n) 425 basicMul(z, x, y) 426 return z.norm() 427 } 428 // m >= n && n >= karatsubaThreshold && n >= 2 429 430 // determine Karatsuba length k such that 431 // 432 // x = xh*b + x0 (0 <= x0 < b) 433 // y = yh*b + y0 (0 <= y0 < b) 434 // b = 1<<(_W*k) ("base" of digits xi, yi) 435 // 436 k := karatsubaLen(n, karatsubaThreshold) 437 // k <= n 438 439 // multiply x0 and y0 via Karatsuba 440 x0 := x[0:k] // x0 is not normalized 441 y0 := y[0:k] // y0 is not normalized 442 z = z.make(max(6*k, m+n)) // enough space for karatsuba of x0*y0 and full result of x*y 443 karatsuba(z, x0, y0) 444 z = z[0 : m+n] // z has final length but may be incomplete 445 z[2*k:].clear() // upper portion of z is garbage (and 2*k <= m+n since k <= n <= m) 446 447 // If xh != 0 or yh != 0, add the missing terms to z. For 448 // 449 // xh = xi*b^i + ... + x2*b^2 + x1*b (0 <= xi < b) 450 // yh = y1*b (0 <= y1 < b) 451 // 452 // the missing terms are 453 // 454 // x0*y1*b and xi*y0*b^i, xi*y1*b^(i+1) for i > 0 455 // 456 // since all the yi for i > 1 are 0 by choice of k: If any of them 457 // were > 0, then yh >= b^2 and thus y >= b^2. Then k' = k*2 would 458 // be a larger valid threshold contradicting the assumption about k. 459 // 460 if k < n || m != n { 461 var t nat 462 463 // add x0*y1*b 464 x0 := x0.norm() 465 y1 := y[k:] // y1 is normalized because y is 466 t = t.mul(x0, y1) // update t so we don't lose t's underlying array 467 addAt(z, t, k) 468 469 // add xi*y0<<i, xi*y1*b<<(i+k) 470 y0 := y0.norm() 471 for i := k; i < len(x); i += k { 472 xi := x[i:] 473 if len(xi) > k { 474 xi = xi[:k] 475 } 476 xi = xi.norm() 477 t = t.mul(xi, y0) 478 addAt(z, t, i) 479 t = t.mul(xi, y1) 480 addAt(z, t, i+k) 481 } 482 } 483 484 return z.norm() 485 } 486 487 // basicSqr sets z = x*x and is asymptotically faster than basicMul 488 // by about a factor of 2, but slower for small arguments due to overhead. 489 // Requirements: len(x) > 0, len(z) == 2*len(x) 490 // The (non-normalized) result is placed in z. 491 func basicSqr(z, x nat) { 492 n := len(x) 493 t := make(nat, 2*n) // temporary variable to hold the products 494 z[1], z[0] = mulWW(x[0], x[0]) // the initial square 495 for i := 1; i < n; i++ { 496 d := x[i] 497 // z collects the squares x[i] * x[i] 498 z[2*i+1], z[2*i] = mulWW(d, d) 499 // t collects the products x[i] * x[j] where j < i 500 t[2*i] = addMulVVW(t[i:2*i], x[0:i], d) 501 } 502 t[2*n-1] = shlVU(t[1:2*n-1], t[1:2*n-1], 1) // double the j < i products 503 addVV(z, z, t) // combine the result 504 } 505 506 // karatsubaSqr squares x and leaves the result in z. 507 // len(x) must be a power of 2 and len(z) >= 6*len(x). 508 // The (non-normalized) result is placed in z[0 : 2*len(x)]. 509 // 510 // The algorithm and the layout of z are the same as for karatsuba. 511 func karatsubaSqr(z, x nat) { 512 n := len(x) 513 514 if n&1 != 0 || n < karatsubaSqrThreshold || n < 2 { 515 basicSqr(z[:2*n], x) 516 return 517 } 518 519 n2 := n >> 1 520 x1, x0 := x[n2:], x[0:n2] 521 522 karatsubaSqr(z, x0) 523 karatsubaSqr(z[n:], x1) 524 525 // s = sign(xd*yd) == -1 for xd != 0; s == 1 for xd == 0 526 xd := z[2*n : 2*n+n2] 527 if subVV(xd, x1, x0) != 0 { 528 subVV(xd, x0, x1) 529 } 530 531 p := z[n*3:] 532 karatsubaSqr(p, xd) 533 534 r := z[n*4:] 535 copy(r, z[:n*2]) 536 537 karatsubaAdd(z[n2:], r, n) 538 karatsubaAdd(z[n2:], r[n:], n) 539 karatsubaSub(z[n2:], p, n) // s == -1 for p != 0; s == 1 for p == 0 540 } 541 542 // Operands that are shorter than basicSqrThreshold are squared using 543 // "grade school" multiplication; for operands longer than karatsubaSqrThreshold 544 // we use the Karatsuba algorithm optimized for x == y. 545 var basicSqrThreshold = 20 // computed by calibrate_test.go 546 var karatsubaSqrThreshold = 260 // computed by calibrate_test.go 547 548 // z = x*x 549 func (z nat) sqr(x nat) nat { 550 n := len(x) 551 switch { 552 case n == 0: 553 return z[:0] 554 case n == 1: 555 d := x[0] 556 z = z.make(2) 557 z[1], z[0] = mulWW(d, d) 558 return z.norm() 559 } 560 561 if alias(z, x) { 562 z = nil // z is an alias for x - cannot reuse 563 } 564 565 if n < basicSqrThreshold { 566 z = z.make(2 * n) 567 basicMul(z, x, x) 568 return z.norm() 569 } 570 if n < karatsubaSqrThreshold { 571 z = z.make(2 * n) 572 basicSqr(z, x) 573 return z.norm() 574 } 575 576 // Use Karatsuba multiplication optimized for x == y. 577 // The algorithm and layout of z are the same as for mul. 578 579 // z = (x1*b + x0)^2 = x1^2*b^2 + 2*x1*x0*b + x0^2 580 581 k := karatsubaLen(n, karatsubaSqrThreshold) 582 583 x0 := x[0:k] 584 z = z.make(max(6*k, 2*n)) 585 karatsubaSqr(z, x0) // z = x0^2 586 z = z[0 : 2*n] 587 z[2*k:].clear() 588 589 if k < n { 590 var t nat 591 x0 := x0.norm() 592 x1 := x[k:] 593 t = t.mul(x0, x1) 594 addAt(z, t, k) 595 addAt(z, t, k) // z = 2*x1*x0*b + x0^2 596 t = t.sqr(x1) 597 addAt(z, t, 2*k) // z = x1^2*b^2 + 2*x1*x0*b + x0^2 598 } 599 600 return z.norm() 601 } 602 603 // mulRange computes the product of all the unsigned integers in the 604 // range [a, b] inclusively. If a > b (empty range), the result is 1. 605 func (z nat) mulRange(a, b uint64) nat { 606 switch { 607 case a == 0: 608 // cut long ranges short (optimization) 609 return z.setUint64(0) 610 case a > b: 611 return z.setUint64(1) 612 case a == b: 613 return z.setUint64(a) 614 case a+1 == b: 615 return z.mul(nat(nil).setUint64(a), nat(nil).setUint64(b)) 616 } 617 m := (a + b) / 2 618 return z.mul(nat(nil).mulRange(a, m), nat(nil).mulRange(m+1, b)) 619 } 620 621 // q = (x-r)/y, with 0 <= r < y 622 func (z nat) divW(x nat, y Word) (q nat, r Word) { 623 m := len(x) 624 switch { 625 case y == 0: 626 panic("division by zero") 627 case y == 1: 628 q = z.set(x) // result is x 629 return 630 case m == 0: 631 q = z[:0] // result is 0 632 return 633 } 634 // m > 0 635 z = z.make(m) 636 r = divWVW(z, 0, x, y) 637 q = z.norm() 638 return 639 } 640 641 func (z nat) div(z2, u, v nat) (q, r nat) { 642 if len(v) == 0 { 643 panic("division by zero") 644 } 645 646 if u.cmp(v) < 0 { 647 q = z[:0] 648 r = z2.set(u) 649 return 650 } 651 652 if len(v) == 1 { 653 var r2 Word 654 q, r2 = z.divW(u, v[0]) 655 r = z2.setWord(r2) 656 return 657 } 658 659 q, r = z.divLarge(z2, u, v) 660 return 661 } 662 663 // getNat returns a *nat of len n. The contents may not be zero. 664 // The pool holds *nat to avoid allocation when converting to interface{}. 665 func getNat(n int) *nat { 666 var z *nat 667 if v := natPool.Get(); v != nil { 668 z = v.(*nat) 669 } 670 if z == nil { 671 z = new(nat) 672 } 673 *z = z.make(n) 674 return z 675 } 676 677 func putNat(x *nat) { 678 natPool.Put(x) 679 } 680 681 var natPool sync.Pool 682 683 // q = (uIn-r)/vIn, with 0 <= r < y 684 // Uses z as storage for q, and u as storage for r if possible. 685 // See Knuth, Volume 2, section 4.3.1, Algorithm D. 686 // Preconditions: 687 // len(vIn) >= 2 688 // len(uIn) >= len(vIn) 689 // u must not alias z 690 func (z nat) divLarge(u, uIn, vIn nat) (q, r nat) { 691 n := len(vIn) 692 m := len(uIn) - n 693 694 // D1. 695 shift := nlz(vIn[n-1]) 696 // do not modify vIn, it may be used by another goroutine simultaneously 697 vp := getNat(n) 698 v := *vp 699 shlVU(v, vIn, shift) 700 701 // u may safely alias uIn or vIn, the value of uIn is used to set u and vIn was already used 702 u = u.make(len(uIn) + 1) 703 u[len(uIn)] = shlVU(u[0:len(uIn)], uIn, shift) 704 705 // z may safely alias uIn or vIn, both values were used already 706 if alias(z, u) { 707 z = nil // z is an alias for u - cannot reuse 708 } 709 q = z.make(m + 1) 710 711 qhatvp := getNat(n + 1) 712 qhatv := *qhatvp 713 714 // D2. 715 vn1 := v[n-1] 716 for j := m; j >= 0; j-- { 717 // D3. 718 qhat := Word(_M) 719 if ujn := u[j+n]; ujn != vn1 { 720 var rhat Word 721 qhat, rhat = divWW(ujn, u[j+n-1], vn1) 722 723 // x1 | x2 = q̂v_{n-2} 724 vn2 := v[n-2] 725 x1, x2 := mulWW(qhat, vn2) 726 // test if q̂v_{n-2} > br̂ + u_{j+n-2} 727 ujn2 := u[j+n-2] 728 for greaterThan(x1, x2, rhat, ujn2) { 729 qhat-- 730 prevRhat := rhat 731 rhat += vn1 732 // v[n-1] >= 0, so this tests for overflow. 733 if rhat < prevRhat { 734 break 735 } 736 x1, x2 = mulWW(qhat, vn2) 737 } 738 } 739 740 // D4. 741 qhatv[n] = mulAddVWW(qhatv[0:n], v, qhat, 0) 742 743 c := subVV(u[j:j+len(qhatv)], u[j:], qhatv) 744 if c != 0 { 745 c := addVV(u[j:j+n], u[j:], v) 746 u[j+n] += c 747 qhat-- 748 } 749 750 q[j] = qhat 751 } 752 753 putNat(vp) 754 putNat(qhatvp) 755 756 q = q.norm() 757 shrVU(u, u, shift) 758 r = u.norm() 759 760 return q, r 761 } 762 763 // Length of x in bits. x must be normalized. 764 func (x nat) bitLen() int { 765 if i := len(x) - 1; i >= 0 { 766 return i*_W + bits.Len(uint(x[i])) 767 } 768 return 0 769 } 770 771 // trailingZeroBits returns the number of consecutive least significant zero 772 // bits of x. 773 func (x nat) trailingZeroBits() uint { 774 if len(x) == 0 { 775 return 0 776 } 777 var i uint 778 for x[i] == 0 { 779 i++ 780 } 781 // x[i] != 0 782 return i*_W + uint(bits.TrailingZeros(uint(x[i]))) 783 } 784 785 func same(x, y nat) bool { 786 return len(x) == len(y) && len(x) > 0 && &x[0] == &y[0] 787 } 788 789 // z = x << s 790 func (z nat) shl(x nat, s uint) nat { 791 if s == 0 { 792 if same(z, x) { 793 return z 794 } 795 if !alias(z, x) { 796 return z.set(x) 797 } 798 } 799 800 m := len(x) 801 if m == 0 { 802 return z[:0] 803 } 804 // m > 0 805 806 n := m + int(s/_W) 807 z = z.make(n + 1) 808 z[n] = shlVU(z[n-m:n], x, s%_W) 809 z[0 : n-m].clear() 810 811 return z.norm() 812 } 813 814 // z = x >> s 815 func (z nat) shr(x nat, s uint) nat { 816 if s == 0 { 817 if same(z, x) { 818 return z 819 } 820 if !alias(z, x) { 821 return z.set(x) 822 } 823 } 824 825 m := len(x) 826 n := m - int(s/_W) 827 if n <= 0 { 828 return z[:0] 829 } 830 // n > 0 831 832 z = z.make(n) 833 shrVU(z, x[m-n:], s%_W) 834 835 return z.norm() 836 } 837 838 func (z nat) setBit(x nat, i uint, b uint) nat { 839 j := int(i / _W) 840 m := Word(1) << (i % _W) 841 n := len(x) 842 switch b { 843 case 0: 844 z = z.make(n) 845 copy(z, x) 846 if j >= n { 847 // no need to grow 848 return z 849 } 850 z[j] &^= m 851 return z.norm() 852 case 1: 853 if j >= n { 854 z = z.make(j + 1) 855 z[n:].clear() 856 } else { 857 z = z.make(n) 858 } 859 copy(z, x) 860 z[j] |= m 861 // no need to normalize 862 return z 863 } 864 panic("set bit is not 0 or 1") 865 } 866 867 // bit returns the value of the i'th bit, with lsb == bit 0. 868 func (x nat) bit(i uint) uint { 869 j := i / _W 870 if j >= uint(len(x)) { 871 return 0 872 } 873 // 0 <= j < len(x) 874 return uint(x[j] >> (i % _W) & 1) 875 } 876 877 // sticky returns 1 if there's a 1 bit within the 878 // i least significant bits, otherwise it returns 0. 879 func (x nat) sticky(i uint) uint { 880 j := i / _W 881 if j >= uint(len(x)) { 882 if len(x) == 0 { 883 return 0 884 } 885 return 1 886 } 887 // 0 <= j < len(x) 888 for _, x := range x[:j] { 889 if x != 0 { 890 return 1 891 } 892 } 893 if x[j]<<(_W-i%_W) != 0 { 894 return 1 895 } 896 return 0 897 } 898 899 func (z nat) and(x, y nat) nat { 900 m := len(x) 901 n := len(y) 902 if m > n { 903 m = n 904 } 905 // m <= n 906 907 z = z.make(m) 908 for i := 0; i < m; i++ { 909 z[i] = x[i] & y[i] 910 } 911 912 return z.norm() 913 } 914 915 func (z nat) andNot(x, y nat) nat { 916 m := len(x) 917 n := len(y) 918 if n > m { 919 n = m 920 } 921 // m >= n 922 923 z = z.make(m) 924 for i := 0; i < n; i++ { 925 z[i] = x[i] &^ y[i] 926 } 927 copy(z[n:m], x[n:m]) 928 929 return z.norm() 930 } 931 932 func (z nat) or(x, y nat) nat { 933 m := len(x) 934 n := len(y) 935 s := x 936 if m < n { 937 n, m = m, n 938 s = y 939 } 940 // m >= n 941 942 z = z.make(m) 943 for i := 0; i < n; i++ { 944 z[i] = x[i] | y[i] 945 } 946 copy(z[n:m], s[n:m]) 947 948 return z.norm() 949 } 950 951 func (z nat) xor(x, y nat) nat { 952 m := len(x) 953 n := len(y) 954 s := x 955 if m < n { 956 n, m = m, n 957 s = y 958 } 959 // m >= n 960 961 z = z.make(m) 962 for i := 0; i < n; i++ { 963 z[i] = x[i] ^ y[i] 964 } 965 copy(z[n:m], s[n:m]) 966 967 return z.norm() 968 } 969 970 // greaterThan reports whether (x1<<_W + x2) > (y1<<_W + y2) 971 func greaterThan(x1, x2, y1, y2 Word) bool { 972 return x1 > y1 || x1 == y1 && x2 > y2 973 } 974 975 // modW returns x % d. 976 func (x nat) modW(d Word) (r Word) { 977 // TODO(agl): we don't actually need to store the q value. 978 var q nat 979 q = q.make(len(x)) 980 return divWVW(q, 0, x, d) 981 } 982 983 // random creates a random integer in [0..limit), using the space in z if 984 // possible. n is the bit length of limit. 985 func (z nat) random(rand *rand.Rand, limit nat, n int) nat { 986 if alias(z, limit) { 987 z = nil // z is an alias for limit - cannot reuse 988 } 989 z = z.make(len(limit)) 990 991 bitLengthOfMSW := uint(n % _W) 992 if bitLengthOfMSW == 0 { 993 bitLengthOfMSW = _W 994 } 995 mask := Word((1 << bitLengthOfMSW) - 1) 996 997 for { 998 switch _W { 999 case 32: 1000 for i := range z { 1001 z[i] = Word(rand.Uint32()) 1002 } 1003 case 64: 1004 for i := range z { 1005 z[i] = Word(rand.Uint32()) | Word(rand.Uint32())<<32 1006 } 1007 default: 1008 panic("unknown word size") 1009 } 1010 z[len(limit)-1] &= mask 1011 if z.cmp(limit) < 0 { 1012 break 1013 } 1014 } 1015 1016 return z.norm() 1017 } 1018 1019 // If m != 0 (i.e., len(m) != 0), expNN sets z to x**y mod m; 1020 // otherwise it sets z to x**y. The result is the value of z. 1021 func (z nat) expNN(x, y, m nat) nat { 1022 if alias(z, x) || alias(z, y) { 1023 // We cannot allow in-place modification of x or y. 1024 z = nil 1025 } 1026 1027 // x**y mod 1 == 0 1028 if len(m) == 1 && m[0] == 1 { 1029 return z.setWord(0) 1030 } 1031 // m == 0 || m > 1 1032 1033 // x**0 == 1 1034 if len(y) == 0 { 1035 return z.setWord(1) 1036 } 1037 // y > 0 1038 1039 // x**1 mod m == x mod m 1040 if len(y) == 1 && y[0] == 1 && len(m) != 0 { 1041 _, z = nat(nil).div(z, x, m) 1042 return z 1043 } 1044 // y > 1 1045 1046 if len(m) != 0 { 1047 // We likely end up being as long as the modulus. 1048 z = z.make(len(m)) 1049 } 1050 z = z.set(x) 1051 1052 // If the base is non-trivial and the exponent is large, we use 1053 // 4-bit, windowed exponentiation. This involves precomputing 14 values 1054 // (x^2...x^15) but then reduces the number of multiply-reduces by a 1055 // third. Even for a 32-bit exponent, this reduces the number of 1056 // operations. Uses Montgomery method for odd moduli. 1057 if x.cmp(natOne) > 0 && len(y) > 1 && len(m) > 0 { 1058 if m[0]&1 == 1 { 1059 return z.expNNMontgomery(x, y, m) 1060 } 1061 return z.expNNWindowed(x, y, m) 1062 } 1063 1064 v := y[len(y)-1] // v > 0 because y is normalized and y > 0 1065 shift := nlz(v) + 1 1066 v <<= shift 1067 var q nat 1068 1069 const mask = 1 << (_W - 1) 1070 1071 // We walk through the bits of the exponent one by one. Each time we 1072 // see a bit, we square, thus doubling the power. If the bit is a one, 1073 // we also multiply by x, thus adding one to the power. 1074 1075 w := _W - int(shift) 1076 // zz and r are used to avoid allocating in mul and div as 1077 // otherwise the arguments would alias. 1078 var zz, r nat 1079 for j := 0; j < w; j++ { 1080 zz = zz.sqr(z) 1081 zz, z = z, zz 1082 1083 if v&mask != 0 { 1084 zz = zz.mul(z, x) 1085 zz, z = z, zz 1086 } 1087 1088 if len(m) != 0 { 1089 zz, r = zz.div(r, z, m) 1090 zz, r, q, z = q, z, zz, r 1091 } 1092 1093 v <<= 1 1094 } 1095 1096 for i := len(y) - 2; i >= 0; i-- { 1097 v = y[i] 1098 1099 for j := 0; j < _W; j++ { 1100 zz = zz.sqr(z) 1101 zz, z = z, zz 1102 1103 if v&mask != 0 { 1104 zz = zz.mul(z, x) 1105 zz, z = z, zz 1106 } 1107 1108 if len(m) != 0 { 1109 zz, r = zz.div(r, z, m) 1110 zz, r, q, z = q, z, zz, r 1111 } 1112 1113 v <<= 1 1114 } 1115 } 1116 1117 return z.norm() 1118 } 1119 1120 // expNNWindowed calculates x**y mod m using a fixed, 4-bit window. 1121 func (z nat) expNNWindowed(x, y, m nat) nat { 1122 // zz and r are used to avoid allocating in mul and div as otherwise 1123 // the arguments would alias. 1124 var zz, r nat 1125 1126 const n = 4 1127 // powers[i] contains x^i. 1128 var powers [1 << n]nat 1129 powers[0] = natOne 1130 powers[1] = x 1131 for i := 2; i < 1<<n; i += 2 { 1132 p2, p, p1 := &powers[i/2], &powers[i], &powers[i+1] 1133 *p = p.sqr(*p2) 1134 zz, r = zz.div(r, *p, m) 1135 *p, r = r, *p 1136 *p1 = p1.mul(*p, x) 1137 zz, r = zz.div(r, *p1, m) 1138 *p1, r = r, *p1 1139 } 1140 1141 z = z.setWord(1) 1142 1143 for i := len(y) - 1; i >= 0; i-- { 1144 yi := y[i] 1145 for j := 0; j < _W; j += n { 1146 if i != len(y)-1 || j != 0 { 1147 // Unrolled loop for significant performance 1148 // gain. Use go test -bench=".*" in crypto/rsa 1149 // to check performance before making changes. 1150 zz = zz.sqr(z) 1151 zz, z = z, zz 1152 zz, r = zz.div(r, z, m) 1153 z, r = r, z 1154 1155 zz = zz.sqr(z) 1156 zz, z = z, zz 1157 zz, r = zz.div(r, z, m) 1158 z, r = r, z 1159 1160 zz = zz.sqr(z) 1161 zz, z = z, zz 1162 zz, r = zz.div(r, z, m) 1163 z, r = r, z 1164 1165 zz = zz.sqr(z) 1166 zz, z = z, zz 1167 zz, r = zz.div(r, z, m) 1168 z, r = r, z 1169 } 1170 1171 zz = zz.mul(z, powers[yi>>(_W-n)]) 1172 zz, z = z, zz 1173 zz, r = zz.div(r, z, m) 1174 z, r = r, z 1175 1176 yi <<= n 1177 } 1178 } 1179 1180 return z.norm() 1181 } 1182 1183 // expNNMontgomery calculates x**y mod m using a fixed, 4-bit window. 1184 // Uses Montgomery representation. 1185 func (z nat) expNNMontgomery(x, y, m nat) nat { 1186 numWords := len(m) 1187 1188 // We want the lengths of x and m to be equal. 1189 // It is OK if x >= m as long as len(x) == len(m). 1190 if len(x) > numWords { 1191 _, x = nat(nil).div(nil, x, m) 1192 // Note: now len(x) <= numWords, not guaranteed ==. 1193 } 1194 if len(x) < numWords { 1195 rr := make(nat, numWords) 1196 copy(rr, x) 1197 x = rr 1198 } 1199 1200 // Ideally the precomputations would be performed outside, and reused 1201 // k0 = -m**-1 mod 2**_W. Algorithm from: Dumas, J.G. "On Newton–Raphson 1202 // Iteration for Multiplicative Inverses Modulo Prime Powers". 1203 k0 := 2 - m[0] 1204 t := m[0] - 1 1205 for i := 1; i < _W; i <<= 1 { 1206 t *= t 1207 k0 *= (t + 1) 1208 } 1209 k0 = -k0 1210 1211 // RR = 2**(2*_W*len(m)) mod m 1212 RR := nat(nil).setWord(1) 1213 zz := nat(nil).shl(RR, uint(2*numWords*_W)) 1214 _, RR = nat(nil).div(RR, zz, m) 1215 if len(RR) < numWords { 1216 zz = zz.make(numWords) 1217 copy(zz, RR) 1218 RR = zz 1219 } 1220 // one = 1, with equal length to that of m 1221 one := make(nat, numWords) 1222 one[0] = 1 1223 1224 const n = 4 1225 // powers[i] contains x^i 1226 var powers [1 << n]nat 1227 powers[0] = powers[0].montgomery(one, RR, m, k0, numWords) 1228 powers[1] = powers[1].montgomery(x, RR, m, k0, numWords) 1229 for i := 2; i < 1<<n; i++ { 1230 powers[i] = powers[i].montgomery(powers[i-1], powers[1], m, k0, numWords) 1231 } 1232 1233 // initialize z = 1 (Montgomery 1) 1234 z = z.make(numWords) 1235 copy(z, powers[0]) 1236 1237 zz = zz.make(numWords) 1238 1239 // same windowed exponent, but with Montgomery multiplications 1240 for i := len(y) - 1; i >= 0; i-- { 1241 yi := y[i] 1242 for j := 0; j < _W; j += n { 1243 if i != len(y)-1 || j != 0 { 1244 zz = zz.montgomery(z, z, m, k0, numWords) 1245 z = z.montgomery(zz, zz, m, k0, numWords) 1246 zz = zz.montgomery(z, z, m, k0, numWords) 1247 z = z.montgomery(zz, zz, m, k0, numWords) 1248 } 1249 zz = zz.montgomery(z, powers[yi>>(_W-n)], m, k0, numWords) 1250 z, zz = zz, z 1251 yi <<= n 1252 } 1253 } 1254 // convert to regular number 1255 zz = zz.montgomery(z, one, m, k0, numWords) 1256 1257 // One last reduction, just in case. 1258 // See golang.org/issue/13907. 1259 if zz.cmp(m) >= 0 { 1260 // Common case is m has high bit set; in that case, 1261 // since zz is the same length as m, there can be just 1262 // one multiple of m to remove. Just subtract. 1263 // We think that the subtract should be sufficient in general, 1264 // so do that unconditionally, but double-check, 1265 // in case our beliefs are wrong. 1266 // The div is not expected to be reached. 1267 zz = zz.sub(zz, m) 1268 if zz.cmp(m) >= 0 { 1269 _, zz = nat(nil).div(nil, zz, m) 1270 } 1271 } 1272 1273 return zz.norm() 1274 } 1275 1276 // bytes writes the value of z into buf using big-endian encoding. 1277 // len(buf) must be >= len(z)*_S. The value of z is encoded in the 1278 // slice buf[i:]. The number i of unused bytes at the beginning of 1279 // buf is returned as result. 1280 func (z nat) bytes(buf []byte) (i int) { 1281 i = len(buf) 1282 for _, d := range z { 1283 for j := 0; j < _S; j++ { 1284 i-- 1285 buf[i] = byte(d) 1286 d >>= 8 1287 } 1288 } 1289 1290 for i < len(buf) && buf[i] == 0 { 1291 i++ 1292 } 1293 1294 return 1295 } 1296 1297 // bigEndianWord returns the contents of buf interpreted as a big-endian encoded Word value. 1298 func bigEndianWord(buf []byte) Word { 1299 if _W == 64 { 1300 return Word(binary.BigEndian.Uint64(buf)) 1301 } 1302 return Word(binary.BigEndian.Uint32(buf)) 1303 } 1304 1305 // setBytes interprets buf as the bytes of a big-endian unsigned 1306 // integer, sets z to that value, and returns z. 1307 func (z nat) setBytes(buf []byte) nat { 1308 z = z.make((len(buf) + _S - 1) / _S) 1309 1310 i := len(buf) 1311 for k := 0; i >= _S; k++ { 1312 z[k] = bigEndianWord(buf[i-_S : i]) 1313 i -= _S 1314 } 1315 if i > 0 { 1316 var d Word 1317 for s := uint(0); i > 0; s += 8 { 1318 d |= Word(buf[i-1]) << s 1319 i-- 1320 } 1321 z[len(z)-1] = d 1322 } 1323 1324 return z.norm() 1325 } 1326 1327 // sqrt sets z = ⌊√x⌋ 1328 func (z nat) sqrt(x nat) nat { 1329 if x.cmp(natOne) <= 0 { 1330 return z.set(x) 1331 } 1332 if alias(z, x) { 1333 z = nil 1334 } 1335 1336 // Start with value known to be too large and repeat "z = ⌊(z + ⌊x/z⌋)/2⌋" until it stops getting smaller. 1337 // See Brent and Zimmermann, Modern Computer Arithmetic, Algorithm 1.13 (SqrtInt). 1338 // https://members.loria.fr/PZimmermann/mca/pub226.html 1339 // If x is one less than a perfect square, the sequence oscillates between the correct z and z+1; 1340 // otherwise it converges to the correct z and stays there. 1341 var z1, z2 nat 1342 z1 = z 1343 z1 = z1.setUint64(1) 1344 z1 = z1.shl(z1, uint(x.bitLen()/2+1)) // must be ≥ √x 1345 for n := 0; ; n++ { 1346 z2, _ = z2.div(nil, x, z1) 1347 z2 = z2.add(z2, z1) 1348 z2 = z2.shr(z2, 1) 1349 if z2.cmp(z1) >= 0 { 1350 // z1 is answer. 1351 // Figure out whether z1 or z2 is currently aliased to z by looking at loop count. 1352 if n&1 == 0 { 1353 return z1 1354 } 1355 return z.set(z1) 1356 } 1357 z1, z2 = z2, z1 1358 } 1359 }