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