nat.go 27 KB

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