blob: eee8ee3f66cc6dadf4f0c1998aa0cb54ed601d0c [file] [log] [blame]
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -07001// 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
Nigel Tao6a186d32011-04-20 09:57:05 +10005// Package big implements multi-precision arithmetic (big numbers).
Robert Griesemer08a209f2009-08-26 12:55:54 -07006// The following numeric types are supported:
7//
8// - Int signed integers
Evan Shaw5ac88f42010-05-21 16:14:55 -07009// - Rat rational numbers
Robert Griesemer08a209f2009-08-26 12:55:54 -070010//
11// All methods on Int take the result as the receiver; if it is one
12// of the operands it may be overwritten (and its memory reused).
13// To enable chaining of operations, the result is also returned.
14//
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070015package big
16
Nigel Tao6a186d32011-04-20 09:57:05 +100017// This file contains operations on unsigned multi-precision integers.
18// These are the building blocks for the operations on signed integers
19// and rationals.
20
Evan Shaw3b980572011-05-27 15:51:00 -070021import (
Russ Coxc2049d22011-11-01 22:04:37 -040022 "errors"
Evan Shaw3b980572011-05-27 15:51:00 -070023 "io"
Rob Pike45e3bcb2011-11-08 15:41:54 -080024 "math/rand"
Evan Shaw3b980572011-05-27 15:51:00 -070025)
Adam Langley19418552009-11-11 13:21:37 -080026
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070027// An unsigned integer x of the form
28//
29// x = x[n-1]*_B^(n-1) + x[n-2]*_B^(n-2) + ... + x[1]*_B + x[0]
30//
31// with 0 <= x[i] < _B and 0 <= i < n is stored in a slice of length n,
32// with the digits x[i] as the slice elements.
33//
34// A number is normalized if the slice contains no leading 0 digits.
35// During arithmetic operations, denormalized values may occur but are
36// always normalized before returning the final result. The normalized
37// representation of 0 is the empty or nil slice (length = 0).
Robert Griesemer696ced52011-10-21 14:11:36 -070038//
Evan Shaw841a32d2010-04-22 16:57:29 -070039type nat []Word
Robert Griesemere5874222009-08-15 11:43:54 -070040
Robert Griesemerb2183702010-04-27 19:16:08 -070041var (
42 natOne = nat{1}
43 natTwo = nat{2}
Evan Shaw5ac88f42010-05-21 16:14:55 -070044 natTen = nat{10}
Robert Griesemerb2183702010-04-27 19:16:08 -070045)
46
Robert Griesemer52cc0582010-05-08 13:52:36 -070047func (z nat) clear() {
Robert Griesemerb2183702010-04-27 19:16:08 -070048 for i := range z {
49 z[i] = 0
50 }
Robert Griesemerb2183702010-04-27 19:16:08 -070051}
52
Evan Shaw841a32d2010-04-22 16:57:29 -070053func (z nat) norm() nat {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080054 i := len(z)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070055 for i > 0 && z[i-1] == 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080056 i--
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070057 }
Robert Griesemer52cc0582010-05-08 13:52:36 -070058 return z[0:i]
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070059}
60
Robert Griesemer52cc0582010-05-08 13:52:36 -070061func (z nat) make(n int) nat {
62 if n <= cap(z) {
63 return z[0:n] // reuse z
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070064 }
Robert Griesemer52cc0582010-05-08 13:52:36 -070065 // Choosing a good value for e has significant performance impact
66 // because it increases the chance that a value can be reused.
67 const e = 4 // extra capacity
68 return make(nat, n, n+e)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070069}
70
Robert Griesemera688eb62010-05-19 09:36:50 -070071func (z nat) setWord(x Word) nat {
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070072 if x == 0 {
Robert Griesemerb2183702010-04-27 19:16:08 -070073 return z.make(0)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070074 }
Robert Griesemera688eb62010-05-19 09:36:50 -070075 z = z.make(1)
76 z[0] = x
77 return z
78}
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070079
Robert Griesemera688eb62010-05-19 09:36:50 -070080func (z nat) setUint64(x uint64) nat {
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070081 // single-digit values
Robert Griesemera688eb62010-05-19 09:36:50 -070082 if w := Word(x); uint64(w) == x {
83 return z.setWord(w)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070084 }
85
86 // compute number of words n required to represent x
Robert Griesemer5a1d3322009-12-15 15:33:31 -080087 n := 0
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070088 for t := x; t > 0; t >>= _W {
Robert Griesemer40621d52009-11-09 12:07:39 -080089 n++
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070090 }
91
92 // split x into n words
Robert Griesemerb2183702010-04-27 19:16:08 -070093 z = z.make(n)
Robert Griesemer52cc0582010-05-08 13:52:36 -070094 for i := range z {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080095 z[i] = Word(x & _M)
96 x >>= _W
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070097 }
98
Robert Griesemer5a1d3322009-12-15 15:33:31 -080099 return z
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700100}
101
Evan Shaw841a32d2010-04-22 16:57:29 -0700102func (z nat) set(x nat) nat {
Robert Griesemerb2183702010-04-27 19:16:08 -0700103 z = z.make(len(x))
Evan Shaw2e00bf92010-07-09 11:24:31 -0700104 copy(z, x)
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800105 return z
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700106}
107
Evan Shaw841a32d2010-04-22 16:57:29 -0700108func (z nat) add(x, y nat) nat {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800109 m := len(x)
110 n := len(y)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700111
112 switch {
113 case m < n:
Evan Shaw841a32d2010-04-22 16:57:29 -0700114 return z.add(y, x)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700115 case m == 0:
116 // n == 0 because m >= n; result is 0
Robert Griesemerb2183702010-04-27 19:16:08 -0700117 return z.make(0)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700118 case n == 0:
119 // result is x
Evan Shaw841a32d2010-04-22 16:57:29 -0700120 return z.set(x)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700121 }
Robert Griesemere5874222009-08-15 11:43:54 -0700122 // m > 0
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700123
Robert Griesemer52cc0582010-05-08 13:52:36 -0700124 z = z.make(m + 1)
125 c := addVV(z[0:n], x, y)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700126 if m > n {
Robert Griesemer52cc0582010-05-08 13:52:36 -0700127 c = addVW(z[n:m], x[n:], c)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700128 }
Robert Griesemer52cc0582010-05-08 13:52:36 -0700129 z[m] = c
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700130
Robert Griesemer52cc0582010-05-08 13:52:36 -0700131 return z.norm()
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700132}
133
Evan Shaw841a32d2010-04-22 16:57:29 -0700134func (z nat) sub(x, y nat) nat {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800135 m := len(x)
136 n := len(y)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700137
138 switch {
139 case m < n:
Robert Griesemer40621d52009-11-09 12:07:39 -0800140 panic("underflow")
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700141 case m == 0:
142 // n == 0 because m >= n; result is 0
Robert Griesemerb2183702010-04-27 19:16:08 -0700143 return z.make(0)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700144 case n == 0:
145 // result is x
Evan Shaw841a32d2010-04-22 16:57:29 -0700146 return z.set(x)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700147 }
Robert Griesemere5874222009-08-15 11:43:54 -0700148 // m > 0
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700149
Robert Griesemerb2183702010-04-27 19:16:08 -0700150 z = z.make(m)
Robert Griesemer52cc0582010-05-08 13:52:36 -0700151 c := subVV(z[0:n], x, y)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700152 if m > n {
Robert Griesemer52cc0582010-05-08 13:52:36 -0700153 c = subVW(z[n:], x[n:], c)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700154 }
155 if c != 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800156 panic("underflow")
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700157 }
Robert Griesemere5874222009-08-15 11:43:54 -0700158
Robert Griesemer26078c32010-05-01 15:11:27 -0700159 return z.norm()
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700160}
161
Evan Shaw841a32d2010-04-22 16:57:29 -0700162func (x nat) cmp(y nat) (r int) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800163 m := len(x)
164 n := len(y)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700165 if m != n || m == 0 {
Robert Griesemer88742ef2009-08-18 10:06:15 -0700166 switch {
Russ Coxc62b3262009-10-06 11:42:55 -0700167 case m < n:
Robert Griesemer40621d52009-11-09 12:07:39 -0800168 r = -1
Russ Coxc62b3262009-10-06 11:42:55 -0700169 case m > n:
Robert Griesemer40621d52009-11-09 12:07:39 -0800170 r = 1
Robert Griesemer88742ef2009-08-18 10:06:15 -0700171 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800172 return
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700173 }
174
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800175 i := m - 1
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700176 for i > 0 && x[i] == y[i] {
Robert Griesemer40621d52009-11-09 12:07:39 -0800177 i--
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700178 }
179
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700180 switch {
Russ Coxc62b3262009-10-06 11:42:55 -0700181 case x[i] < y[i]:
Robert Griesemer40621d52009-11-09 12:07:39 -0800182 r = -1
Russ Coxc62b3262009-10-06 11:42:55 -0700183 case x[i] > y[i]:
Robert Griesemer40621d52009-11-09 12:07:39 -0800184 r = 1
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700185 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800186 return
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700187}
188
Evan Shaw841a32d2010-04-22 16:57:29 -0700189func (z nat) mulAddWW(x nat, y, r Word) nat {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800190 m := len(x)
Robert Griesemere5874222009-08-15 11:43:54 -0700191 if m == 0 || y == 0 {
Robert Griesemera688eb62010-05-19 09:36:50 -0700192 return z.setWord(r) // result is r
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700193 }
194 // m > 0
Robert Griesemere5874222009-08-15 11:43:54 -0700195
Robert Griesemer52cc0582010-05-08 13:52:36 -0700196 z = z.make(m + 1)
197 z[m] = mulAddVWW(z[0:m], x, y, r)
Robert Griesemere5874222009-08-15 11:43:54 -0700198
Robert Griesemer52cc0582010-05-08 13:52:36 -0700199 return z.norm()
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700200}
201
Robert Griesemerb2183702010-04-27 19:16:08 -0700202// basicMul multiplies x and y and leaves the result in z.
203// The (non-normalized) result is placed in z[0 : len(x) + len(y)].
204func basicMul(z, x, y nat) {
Robert Griesemer3f287b52010-05-06 18:20:01 -0700205 z[0 : len(x)+len(y)].clear() // initialize z
Robert Griesemerb2183702010-04-27 19:16:08 -0700206 for i, d := range y {
207 if d != 0 {
Robert Griesemer52cc0582010-05-08 13:52:36 -0700208 z[len(x)+i] = addMulVVW(z[i:i+len(x)], x, d)
Robert Griesemerb2183702010-04-27 19:16:08 -0700209 }
210 }
211}
212
Robert Griesemerb2183702010-04-27 19:16:08 -0700213// Fast version of z[0:n+n>>1].add(z[0:n+n>>1], x[0:n]) w/o bounds checks.
214// Factored out for readability - do not use outside karatsuba.
215func karatsubaAdd(z, x nat, n int) {
Robert Griesemer52cc0582010-05-08 13:52:36 -0700216 if c := addVV(z[0:n], z, x); c != 0 {
217 addVW(z[n:n+n>>1], z[n:], c)
Robert Griesemerb2183702010-04-27 19:16:08 -0700218 }
219}
220
Robert Griesemerb2183702010-04-27 19:16:08 -0700221// Like karatsubaAdd, but does subtract.
222func karatsubaSub(z, x nat, n int) {
Robert Griesemer52cc0582010-05-08 13:52:36 -0700223 if c := subVV(z[0:n], z, x); c != 0 {
224 subVW(z[n:n+n>>1], z[n:], c)
Robert Griesemerb2183702010-04-27 19:16:08 -0700225 }
226}
227
Robert Griesemerb2183702010-04-27 19:16:08 -0700228// Operands that are shorter than karatsubaThreshold are multiplied using
229// "grade school" multiplication; for longer operands the Karatsuba algorithm
230// is used.
Robert Griesemer407dbb42010-04-30 11:54:27 -0700231var karatsubaThreshold int = 32 // computed by calibrate.go
Robert Griesemerb2183702010-04-27 19:16:08 -0700232
233// karatsuba multiplies x and y and leaves the result in z.
234// Both x and y must have the same length n and n must be a
235// power of 2. The result vector z must have len(z) >= 6*n.
236// The (non-normalized) result is placed in z[0 : 2*n].
237func karatsuba(z, x, y nat) {
238 n := len(y)
239
240 // Switch to basic multiplication if numbers are odd or small.
241 // (n is always even if karatsubaThreshold is even, but be
242 // conservative)
243 if n&1 != 0 || n < karatsubaThreshold || n < 2 {
244 basicMul(z, x, y)
245 return
246 }
247 // n&1 == 0 && n >= karatsubaThreshold && n >= 2
248
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 + z1 + z0
267 // = (x1-x0)*(y0 - y1) + z1 + z0
268 // = x1*y0 - x1*y1 - x0*y0 + x0*y1 + z1 + z0
269 // = x1*y0 - z1 - z0 + x0*y1 + z1 + z0
270 // = x1*y0 + x0*y1
271
272 // split x, y into "digits"
273 n2 := n >> 1 // n2 >= 1
274 x1, x0 := x[n2:], x[0:n2] // x = x1*b + y0
275 y1, y0 := y[n2:], y[0:n2] // y = y1*b + y0
276
277 // z is used for the result and temporary storage:
278 //
279 // 6*n 5*n 4*n 3*n 2*n 1*n 0*n
280 // z = [z2 copy|z0 copy| xd*yd | yd:xd | x1*y1 | x0*y0 ]
281 //
282 // For each recursive call of karatsuba, an unused slice of
283 // z is passed in that has (at least) half the length of the
284 // caller's z.
285
286 // compute z0 and z2 with the result "in place" in z
287 karatsuba(z, x0, y0) // z0 = x0*y0
288 karatsuba(z[n:], x1, y1) // z2 = x1*y1
289
290 // compute xd (or the negative value if underflow occurs)
291 s := 1 // sign of product xd*yd
292 xd := z[2*n : 2*n+n2]
Robert Griesemer52cc0582010-05-08 13:52:36 -0700293 if subVV(xd, x1, x0) != 0 { // x1-x0
Robert Griesemerb2183702010-04-27 19:16:08 -0700294 s = -s
Robert Griesemer52cc0582010-05-08 13:52:36 -0700295 subVV(xd, x0, x1) // x0-x1
Robert Griesemerb2183702010-04-27 19:16:08 -0700296 }
297
298 // compute yd (or the negative value if underflow occurs)
299 yd := z[2*n+n2 : 3*n]
Robert Griesemer52cc0582010-05-08 13:52:36 -0700300 if subVV(yd, y0, y1) != 0 { // y0-y1
Robert Griesemerb2183702010-04-27 19:16:08 -0700301 s = -s
Robert Griesemer52cc0582010-05-08 13:52:36 -0700302 subVV(yd, y1, y0) // y1-y0
Robert Griesemerb2183702010-04-27 19:16:08 -0700303 }
304
305 // p = (x1-x0)*(y0-y1) == x1*y0 - x1*y1 - x0*y0 + x0*y1 for s > 0
306 // p = (x0-x1)*(y0-y1) == x0*y0 - x0*y1 - x1*y0 + x1*y1 for s < 0
307 p := z[n*3:]
308 karatsuba(p, xd, yd)
309
310 // save original z2:z0
311 // (ok to use upper half of z since we're done recursing)
312 r := z[n*4:]
313 copy(r, z)
314
315 // add up all partial products
316 //
317 // 2*n n 0
318 // z = [ z2 | z0 ]
319 // + [ z0 ]
320 // + [ z2 ]
321 // + [ p ]
322 //
323 karatsubaAdd(z[n2:], r, n)
324 karatsubaAdd(z[n2:], r[n:], n)
325 if s > 0 {
326 karatsubaAdd(z[n2:], p, n)
327 } else {
328 karatsubaSub(z[n2:], p, n)
329 }
330}
331
Robert Griesemerb2183702010-04-27 19:16:08 -0700332// alias returns true if x and y share the same base array.
333func alias(x, y nat) bool {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700334 return cap(x) > 0 && cap(y) > 0 && &x[0:cap(x)][cap(x)-1] == &y[0:cap(y)][cap(y)-1]
Robert Griesemerb2183702010-04-27 19:16:08 -0700335}
336
Robert Griesemerb2183702010-04-27 19:16:08 -0700337// addAt implements z += x*(1<<(_W*i)); z must be long enough.
338// (we don't use nat.add because we need z to stay the same
339// slice, and we don't need to normalize z after each addition)
340func addAt(z, x nat, i int) {
341 if n := len(x); n > 0 {
Robert Griesemer52cc0582010-05-08 13:52:36 -0700342 if c := addVV(z[i:i+n], z[i:], x); c != 0 {
Robert Griesemerb2183702010-04-27 19:16:08 -0700343 j := i + n
344 if j < len(z) {
Robert Griesemer52cc0582010-05-08 13:52:36 -0700345 addVW(z[j:], z[j:], c)
Robert Griesemerb2183702010-04-27 19:16:08 -0700346 }
347 }
348 }
349}
350
Robert Griesemerb2183702010-04-27 19:16:08 -0700351func max(x, y int) int {
352 if x > y {
353 return x
354 }
355 return y
356}
357
Robert Griesemer407dbb42010-04-30 11:54:27 -0700358// karatsubaLen computes an approximation to the maximum k <= n such that
359// k = p<<i for a number p <= karatsubaThreshold and an i >= 0. Thus, the
360// result is the largest number that can be divided repeatedly by 2 before
361// becoming about the value of karatsubaThreshold.
362func karatsubaLen(n int) int {
363 i := uint(0)
364 for n > karatsubaThreshold {
365 n >>= 1
366 i++
367 }
368 return n << i
369}
370
Evan Shaw841a32d2010-04-22 16:57:29 -0700371func (z nat) mul(x, y nat) nat {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800372 m := len(x)
373 n := len(y)
Robert Griesemere5874222009-08-15 11:43:54 -0700374
375 switch {
376 case m < n:
Evan Shaw841a32d2010-04-22 16:57:29 -0700377 return z.mul(y, x)
Robert Griesemere5874222009-08-15 11:43:54 -0700378 case m == 0 || n == 0:
Robert Griesemerb2183702010-04-27 19:16:08 -0700379 return z.make(0)
Robert Griesemer88742ef2009-08-18 10:06:15 -0700380 case n == 1:
Evan Shaw841a32d2010-04-22 16:57:29 -0700381 return z.mulAddWW(x, y[0], 0)
Robert Griesemere5874222009-08-15 11:43:54 -0700382 }
Robert Griesemerb2183702010-04-27 19:16:08 -0700383 // m >= n > 1
Robert Griesemere5874222009-08-15 11:43:54 -0700384
Robert Griesemerb2183702010-04-27 19:16:08 -0700385 // determine if z can be reused
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700386 if alias(z, x) || alias(z, y) {
Robert Griesemerb2183702010-04-27 19:16:08 -0700387 z = nil // z is an alias for x or y - cannot reuse
Robert Griesemer88742ef2009-08-18 10:06:15 -0700388 }
Robert Griesemere5874222009-08-15 11:43:54 -0700389
Robert Griesemerb2183702010-04-27 19:16:08 -0700390 // use basic multiplication if the numbers are small
391 if n < karatsubaThreshold || n < 2 {
392 z = z.make(m + n)
393 basicMul(z, x, y)
394 return z.norm()
395 }
396 // m >= n && n >= karatsubaThreshold && n >= 2
397
Robert Griesemer407dbb42010-04-30 11:54:27 -0700398 // determine Karatsuba length k such that
Robert Griesemerb2183702010-04-27 19:16:08 -0700399 //
400 // x = x1*b + x0
401 // y = y1*b + y0 (and k <= len(y), which implies k <= len(x))
402 // b = 1<<(_W*k) ("base" of digits xi, yi)
403 //
Robert Griesemer407dbb42010-04-30 11:54:27 -0700404 k := karatsubaLen(n)
Robert Griesemerb2183702010-04-27 19:16:08 -0700405 // k <= n
406
407 // multiply x0 and y0 via Karatsuba
408 x0 := x[0:k] // x0 is not normalized
409 y0 := y[0:k] // y0 is not normalized
410 z = z.make(max(6*k, m+n)) // enough space for karatsuba of x0*y0 and full result of x*y
411 karatsuba(z, x0, y0)
412 z = z[0 : m+n] // z has final length but may be incomplete, upper portion is garbage
413
414 // If x1 and/or y1 are not 0, add missing terms to z explicitly:
415 //
416 // m+n 2*k 0
417 // z = [ ... | x0*y0 ]
418 // + [ x1*y1 ]
419 // + [ x1*y0 ]
420 // + [ x0*y1 ]
421 //
422 if k < n || m != n {
423 x1 := x[k:] // x1 is normalized because x is
424 y1 := y[k:] // y1 is normalized because y is
425 var t nat
426 t = t.mul(x1, y1)
427 copy(z[2*k:], t)
428 z[2*k+len(t):].clear() // upper portion of z is garbage
429 t = t.mul(x1, y0.norm())
430 addAt(z, t, k)
431 t = t.mul(x0.norm(), y1)
432 addAt(z, t, k)
433 }
434
435 return z.norm()
436}
437
Robert Griesemerb2183702010-04-27 19:16:08 -0700438// mulRange computes the product of all the unsigned integers in the
439// range [a, b] inclusively. If a > b (empty range), the result is 1.
440func (z nat) mulRange(a, b uint64) nat {
441 switch {
442 case a == 0:
443 // cut long ranges short (optimization)
Robert Griesemerdbb62322010-05-15 10:23:41 -0700444 return z.setUint64(0)
Robert Griesemerb2183702010-04-27 19:16:08 -0700445 case a > b:
Robert Griesemerdbb62322010-05-15 10:23:41 -0700446 return z.setUint64(1)
Robert Griesemerb2183702010-04-27 19:16:08 -0700447 case a == b:
Robert Griesemerdbb62322010-05-15 10:23:41 -0700448 return z.setUint64(a)
Robert Griesemerb2183702010-04-27 19:16:08 -0700449 case a+1 == b:
Robert Griesemerf5cf0a42011-11-14 13:35:22 -0800450 return z.mul(nat(nil).setUint64(a), nat(nil).setUint64(b))
Robert Griesemerb2183702010-04-27 19:16:08 -0700451 }
452 m := (a + b) / 2
Robert Griesemerf5cf0a42011-11-14 13:35:22 -0800453 return z.mul(nat(nil).mulRange(a, m), nat(nil).mulRange(m+1, b))
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700454}
455
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700456// q = (x-r)/y, with 0 <= r < y
Evan Shaw841a32d2010-04-22 16:57:29 -0700457func (z nat) divW(x nat, y Word) (q nat, r Word) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800458 m := len(x)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700459 switch {
460 case y == 0:
Robert Griesemer40621d52009-11-09 12:07:39 -0800461 panic("division by zero")
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700462 case y == 1:
Evan Shaw841a32d2010-04-22 16:57:29 -0700463 q = z.set(x) // result is x
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800464 return
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700465 case m == 0:
Robert Griesemer26078c32010-05-01 15:11:27 -0700466 q = z.make(0) // result is 0
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800467 return
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700468 }
469 // m > 0
Robert Griesemerb2183702010-04-27 19:16:08 -0700470 z = z.make(m)
Robert Griesemer52cc0582010-05-08 13:52:36 -0700471 r = divWVW(z, 0, x, y)
Evan Shaw841a32d2010-04-22 16:57:29 -0700472 q = z.norm()
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800473 return
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700474}
475
Evan Shaw841a32d2010-04-22 16:57:29 -0700476func (z nat) div(z2, u, v nat) (q, r nat) {
Adam Langley19418552009-11-11 13:21:37 -0800477 if len(v) == 0 {
Evan Shaw841a32d2010-04-22 16:57:29 -0700478 panic("division by zero")
Adam Langley19418552009-11-11 13:21:37 -0800479 }
480
Evan Shaw841a32d2010-04-22 16:57:29 -0700481 if u.cmp(v) < 0 {
Robert Griesemerb2183702010-04-27 19:16:08 -0700482 q = z.make(0)
Evan Shaw841a32d2010-04-22 16:57:29 -0700483 r = z2.set(u)
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800484 return
Adam Langley19418552009-11-11 13:21:37 -0800485 }
486
487 if len(v) == 1 {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800488 var rprime Word
Evan Shaw841a32d2010-04-22 16:57:29 -0700489 q, rprime = z.divW(u, v[0])
Adam Langley19418552009-11-11 13:21:37 -0800490 if rprime > 0 {
Robert Griesemerb2183702010-04-27 19:16:08 -0700491 r = z2.make(1)
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800492 r[0] = rprime
Adam Langley19418552009-11-11 13:21:37 -0800493 } else {
Robert Griesemerb2183702010-04-27 19:16:08 -0700494 r = z2.make(0)
Adam Langley19418552009-11-11 13:21:37 -0800495 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800496 return
Adam Langley19418552009-11-11 13:21:37 -0800497 }
498
Evan Shaw841a32d2010-04-22 16:57:29 -0700499 q, r = z.divLarge(z2, u, v)
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800500 return
Adam Langley19418552009-11-11 13:21:37 -0800501}
502
Adam Langley65063bc2009-11-05 15:55:41 -0800503// q = (uIn-r)/v, with 0 <= r < y
Robert Griesemer3f287b52010-05-06 18:20:01 -0700504// Uses z as storage for q, and u as storage for r if possible.
Adam Langley65063bc2009-11-05 15:55:41 -0800505// See Knuth, Volume 2, section 4.3.1, Algorithm D.
506// Preconditions:
507// len(v) >= 2
Adam Langley19418552009-11-11 13:21:37 -0800508// len(uIn) >= len(v)
Robert Griesemer3f287b52010-05-06 18:20:01 -0700509func (z nat) divLarge(u, uIn, v nat) (q, r nat) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800510 n := len(v)
Robert Griesemer3f287b52010-05-06 18:20:01 -0700511 m := len(uIn) - n
Adam Langley65063bc2009-11-05 15:55:41 -0800512
Robert Griesemer90d0c332010-05-18 16:31:49 -0700513 // determine if z can be reused
Robert Griesemera688eb62010-05-19 09:36:50 -0700514 // TODO(gri) should find a better solution - this if statement
515 // is very costly (see e.g. time pidigits -s -n 10000)
Robert Griesemer90d0c332010-05-18 16:31:49 -0700516 if alias(z, uIn) || alias(z, v) {
517 z = nil // z is an alias for uIn or v - cannot reuse
518 }
Robert Griesemerb2183702010-04-27 19:16:08 -0700519 q = z.make(m + 1)
Robert Griesemer90d0c332010-05-18 16:31:49 -0700520
Robert Griesemer3f287b52010-05-06 18:20:01 -0700521 qhatv := make(nat, n+1)
Robert Griesemer90d0c332010-05-18 16:31:49 -0700522 if alias(u, uIn) || alias(u, v) {
523 u = nil // u is an alias for uIn or v - cannot reuse
Robert Griesemer3f287b52010-05-06 18:20:01 -0700524 }
Robert Griesemer52cc0582010-05-08 13:52:36 -0700525 u = u.make(len(uIn) + 1)
526 u.clear()
Adam Langley65063bc2009-11-05 15:55:41 -0800527
528 // D1.
Robert Griesemer5bf57c12011-06-02 12:58:26 -0700529 shift := leadingZeros(v[n-1])
Robert Griesemer191a6bf2011-06-02 11:07:41 -0700530 if shift > 0 {
531 // do not modify v, it may be used by another goroutine simultaneously
532 v1 := make(nat, n)
Robert Griesemer5bf57c12011-06-02 12:58:26 -0700533 shlVU(v1, v, shift)
Robert Griesemer191a6bf2011-06-02 11:07:41 -0700534 v = v1
535 }
Robert Griesemer5bf57c12011-06-02 12:58:26 -0700536 u[len(uIn)] = shlVU(u[0:len(uIn)], uIn, shift)
Adam Langley65063bc2009-11-05 15:55:41 -0800537
538 // D2.
539 for j := m; j >= 0; j-- {
540 // D3.
Robert Griesemera688eb62010-05-19 09:36:50 -0700541 qhat := Word(_M)
542 if u[j+n] != v[n-1] {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800543 var rhat Word
Robert Griesemera688eb62010-05-19 09:36:50 -0700544 qhat, rhat = divWW(u[j+n], u[j+n-1], v[n-1])
Adam Langley65063bc2009-11-05 15:55:41 -0800545
Adam Langleybf1f63a2009-11-18 19:26:12 -0800546 // x1 | x2 = q̂v_{n-2}
Robert Griesemera688eb62010-05-19 09:36:50 -0700547 x1, x2 := mulWW(qhat, v[n-2])
Adam Langleybf1f63a2009-11-18 19:26:12 -0800548 // test if q̂v_{n-2} > br̂ + u_{j+n-2}
549 for greaterThan(x1, x2, rhat, u[j+n-2]) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800550 qhat--
551 prevRhat := rhat
552 rhat += v[n-1]
Adam Langleybf1f63a2009-11-18 19:26:12 -0800553 // v[n-1] >= 0, so this tests for overflow.
554 if rhat < prevRhat {
555 break
556 }
Robert Griesemera688eb62010-05-19 09:36:50 -0700557 x1, x2 = mulWW(qhat, v[n-2])
Adam Langley65063bc2009-11-05 15:55:41 -0800558 }
Adam Langley65063bc2009-11-05 15:55:41 -0800559 }
560
561 // D4.
Robert Griesemer52cc0582010-05-08 13:52:36 -0700562 qhatv[n] = mulAddVWW(qhatv[0:n], v, qhat, 0)
Adam Langley65063bc2009-11-05 15:55:41 -0800563
Robert Griesemer52cc0582010-05-08 13:52:36 -0700564 c := subVV(u[j:j+len(qhatv)], u[j:], qhatv)
Adam Langley65063bc2009-11-05 15:55:41 -0800565 if c != 0 {
Robert Griesemer52cc0582010-05-08 13:52:36 -0700566 c := addVV(u[j:j+n], u[j:], v)
Robert Griesemer3f287b52010-05-06 18:20:01 -0700567 u[j+n] += c
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800568 qhat--
Adam Langley65063bc2009-11-05 15:55:41 -0800569 }
570
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800571 q[j] = qhat
Adam Langley65063bc2009-11-05 15:55:41 -0800572 }
573
Evan Shaw841a32d2010-04-22 16:57:29 -0700574 q = q.norm()
Robert Griesemer5bf57c12011-06-02 12:58:26 -0700575 shrVU(u, u, shift)
Evan Shaw841a32d2010-04-22 16:57:29 -0700576 r = u.norm()
Adam Langley65063bc2009-11-05 15:55:41 -0800577
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800578 return q, r
Adam Langley65063bc2009-11-05 15:55:41 -0800579}
580
Robert Griesemer26078c32010-05-01 15:11:27 -0700581// Length of x in bits. x must be normalized.
582func (x nat) bitLen() int {
Robert Griesemerb2183702010-04-27 19:16:08 -0700583 if i := len(x) - 1; i >= 0 {
Robert Griesemer26078c32010-05-01 15:11:27 -0700584 return i*_W + bitLen(x[i])
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700585 }
Robert Griesemer26078c32010-05-01 15:11:27 -0700586 return 0
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700587}
588
Robert Griesemerce2701b2011-06-01 14:17:00 -0700589// MaxBase is the largest number base accepted for string conversions.
590const MaxBase = 'z' - 'a' + 10 + 1 // = hexValue('z') + 1
591
Russ Cox0e513312011-10-25 22:21:49 -0700592func hexValue(ch rune) Word {
Robert Griesemerce2701b2011-06-01 14:17:00 -0700593 d := MaxBase + 1 // illegal base
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700594 switch {
Russ Coxc62b3262009-10-06 11:42:55 -0700595 case '0' <= ch && ch <= '9':
Russ Cox0e513312011-10-25 22:21:49 -0700596 d = int(ch - '0')
Evan Shaw3b980572011-05-27 15:51:00 -0700597 case 'a' <= ch && ch <= 'z':
Russ Cox0e513312011-10-25 22:21:49 -0700598 d = int(ch - 'a' + 10)
Evan Shaw3b980572011-05-27 15:51:00 -0700599 case 'A' <= ch && ch <= 'Z':
Russ Cox0e513312011-10-25 22:21:49 -0700600 d = int(ch - 'A' + 10)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700601 }
Robert Griesemerce2701b2011-06-01 14:17:00 -0700602 return Word(d)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700603}
604
Robert Griesemerce2701b2011-06-01 14:17:00 -0700605// scan sets z to the natural number corresponding to the longest possible prefix
606// read from r representing an unsigned integer in a given conversion base.
607// It returns z, the actual conversion base used, and an error, if any. In the
608// error case, the value of z is undefined. The syntax follows the syntax of
Evan Shaw3b980572011-05-27 15:51:00 -0700609// unsigned integer literals in Go.
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700610//
Robert Griesemerce2701b2011-06-01 14:17:00 -0700611// The base argument must be 0 or a value from 2 through MaxBase. If the base
612// is 0, the string prefix determines the actual conversion base. A prefix of
613// ``0x'' or ``0X'' selects base 16; the ``0'' prefix selects base 8, and a
614// ``0b'' or ``0B'' prefix selects base 2. Otherwise the selected base is 10.
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700615//
Russ Coxc2049d22011-11-01 22:04:37 -0400616func (z nat) scan(r io.RuneScanner, base int) (nat, int, error) {
Robert Griesemerce2701b2011-06-01 14:17:00 -0700617 // reject illegal bases
618 if base < 0 || base == 1 || MaxBase < base {
Russ Coxc2049d22011-11-01 22:04:37 -0400619 return z, 0, errors.New("illegal number base")
Robert Griesemerce2701b2011-06-01 14:17:00 -0700620 }
621
622 // one char look-ahead
Evan Shaw3b980572011-05-27 15:51:00 -0700623 ch, _, err := r.ReadRune()
624 if err != nil {
625 return z, 0, err
626 }
Robert Griesemerce2701b2011-06-01 14:17:00 -0700627
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700628 // determine base if necessary
Robert Griesemerce2701b2011-06-01 14:17:00 -0700629 b := Word(base)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700630 if base == 0 {
Robert Griesemerce2701b2011-06-01 14:17:00 -0700631 b = 10
Evan Shaw3b980572011-05-27 15:51:00 -0700632 if ch == '0' {
Evan Shaw3b980572011-05-27 15:51:00 -0700633 switch ch, _, err = r.ReadRune(); err {
634 case nil:
Robert Griesemerce2701b2011-06-01 14:17:00 -0700635 b = 8
Evan Shaw3b980572011-05-27 15:51:00 -0700636 switch ch {
Robert Griesemerdbb62322010-05-15 10:23:41 -0700637 case 'x', 'X':
Robert Griesemerce2701b2011-06-01 14:17:00 -0700638 b = 16
Robert Griesemerdbb62322010-05-15 10:23:41 -0700639 case 'b', 'B':
Robert Griesemerce2701b2011-06-01 14:17:00 -0700640 b = 2
Adam Langley65063bc2009-11-05 15:55:41 -0800641 }
Robert Griesemerce2701b2011-06-01 14:17:00 -0700642 if b == 2 || b == 16 {
Evan Shaw3b980572011-05-27 15:51:00 -0700643 if ch, _, err = r.ReadRune(); err != nil {
Robert Griesemerce2701b2011-06-01 14:17:00 -0700644 return z, 0, err
Evan Shaw3b980572011-05-27 15:51:00 -0700645 }
646 }
Russ Coxc2049d22011-11-01 22:04:37 -0400647 case io.EOF:
Evan Shawde20cec2011-08-24 14:55:03 -0700648 return z.make(0), 10, nil
Evan Shaw3b980572011-05-27 15:51:00 -0700649 default:
Robert Griesemerce2701b2011-06-01 14:17:00 -0700650 return z, 10, err
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700651 }
652 }
653 }
Robert Griesemerdbb62322010-05-15 10:23:41 -0700654
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700655 // convert string
Robert Griesemerce2701b2011-06-01 14:17:00 -0700656 // - group as many digits d as possible together into a "super-digit" dd with "super-base" bb
657 // - only when bb does not fit into a word anymore, do a full number mulAddWW using bb and dd
Robert Griesemerdbb62322010-05-15 10:23:41 -0700658 z = z.make(0)
Robert Griesemerce2701b2011-06-01 14:17:00 -0700659 bb := Word(1)
660 dd := Word(0)
Robert Griesemer158b4272011-06-01 16:28:17 -0700661 for max := _M / b; ; {
Evan Shaw3b980572011-05-27 15:51:00 -0700662 d := hexValue(ch)
Robert Griesemerce2701b2011-06-01 14:17:00 -0700663 if d >= b {
664 r.UnreadRune() // ch does not belong to number anymore
665 break
Evan Shaw3b980572011-05-27 15:51:00 -0700666 }
Robert Griesemerce2701b2011-06-01 14:17:00 -0700667
Robert Griesemer158b4272011-06-01 16:28:17 -0700668 if bb <= max {
669 bb *= b
670 dd = dd*b + d
671 } else {
672 // bb * b would overflow
Robert Griesemerce2701b2011-06-01 14:17:00 -0700673 z = z.mulAddWW(z, bb, dd)
674 bb = b
675 dd = d
Robert Griesemerce2701b2011-06-01 14:17:00 -0700676 }
677
Evan Shaw3b980572011-05-27 15:51:00 -0700678 if ch, _, err = r.ReadRune(); err != nil {
Russ Coxc2049d22011-11-01 22:04:37 -0400679 if err != io.EOF {
Robert Griesemerce2701b2011-06-01 14:17:00 -0700680 return z, int(b), err
Evan Shaw3b980572011-05-27 15:51:00 -0700681 }
Robert Griesemerce2701b2011-06-01 14:17:00 -0700682 break
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700683 }
684 }
685
Robert Griesemerce2701b2011-06-01 14:17:00 -0700686 switch {
687 case bb > 1:
688 // there was at least one mantissa digit
689 z = z.mulAddWW(z, bb, dd)
690 case base == 0 && b == 8:
691 // there was only the octal prefix 0 (possibly followed by digits > 7);
692 // return base 10, not 8
693 return z, 10, nil
694 case base != 0 || b != 8:
695 // there was neither a mantissa digit nor the octal prefix 0
Russ Coxc2049d22011-11-01 22:04:37 -0400696 return z, int(b), errors.New("syntax error scanning number")
Robert Griesemerce2701b2011-06-01 14:17:00 -0700697 }
698
699 return z.norm(), int(b), nil
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700700}
701
Robert Griesemer9fa6cb22011-05-17 15:32:38 -0700702// Character sets for string conversion.
703const (
704 lowercaseDigits = "0123456789abcdefghijklmnopqrstuvwxyz"
705 uppercaseDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
706)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700707
Michael T. Jonesd5c45c52011-06-07 16:02:34 -0700708// decimalString returns a decimal representation of x.
709// It calls x.string with the charset "0123456789".
710func (x nat) decimalString() string {
711 return x.string(lowercaseDigits[0:10])
712}
713
Robert Griesemer9fa6cb22011-05-17 15:32:38 -0700714// string converts x to a string using digits from a charset; a digit with
715// value d is represented by charset[d]. The conversion base is determined
Robert Griesemer82ddf972011-05-18 11:02:08 -0700716// by len(charset), which must be >= 2.
Robert Griesemer9fa6cb22011-05-17 15:32:38 -0700717func (x nat) string(charset string) string {
Michael T. Jonesd5c45c52011-06-07 16:02:34 -0700718 b := Word(len(charset))
Robert Griesemer9fa6cb22011-05-17 15:32:38 -0700719
Robert Griesemer82ddf972011-05-18 11:02:08 -0700720 // special cases
Robert Griesemer9fa6cb22011-05-17 15:32:38 -0700721 switch {
Michael T. Jonesd5c45c52011-06-07 16:02:34 -0700722 case b < 2 || b > 256:
Robert Griesemer82ddf972011-05-18 11:02:08 -0700723 panic("illegal base")
Robert Griesemer9fa6cb22011-05-17 15:32:38 -0700724 case len(x) == 0:
725 return string(charset[0])
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700726 }
727
728 // allocate buffer for conversion
Michael T. Jonesd5c45c52011-06-07 16:02:34 -0700729 i := x.bitLen()/log2(b) + 1 // +1: round up
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800730 s := make([]byte, i)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700731
Michael T. Jonesd5c45c52011-06-07 16:02:34 -0700732 // special case: power of two bases can avoid divisions completely
733 if b == b&-b {
734 // shift is base-b digit size in bits
735 shift := uint(trailingZeroBits(b)) // shift > 0 because b >= 2
Michael T. Jonesd5c45c52011-06-07 16:02:34 -0700736 mask := Word(1)<<shift - 1
737 w := x[0]
738 nbits := uint(_W) // number of unprocessed bits in w
739
740 // convert less-significant words
Robert Griesemer2de06652011-06-08 11:24:24 -0700741 for k := 1; k < len(x); k++ {
Michael T. Jonesd5c45c52011-06-07 16:02:34 -0700742 // convert full digits
743 for nbits >= shift {
744 i--
745 s[i] = charset[w&mask]
746 w >>= shift
747 nbits -= shift
748 }
749
750 // convert any partial leading digit and advance to next word
751 if nbits == 0 {
752 // no partial digit remaining, just advance
Robert Griesemer2de06652011-06-08 11:24:24 -0700753 w = x[k]
Michael T. Jonesd5c45c52011-06-07 16:02:34 -0700754 nbits = _W
755 } else {
Robert Griesemer2de06652011-06-08 11:24:24 -0700756 // partial digit in current (k-1) and next (k) word
757 w |= x[k] << nbits
Michael T. Jonesd5c45c52011-06-07 16:02:34 -0700758 i--
759 s[i] = charset[w&mask]
760
761 // advance
Robert Griesemer2de06652011-06-08 11:24:24 -0700762 w = x[k] >> (shift - nbits)
Michael T. Jonesd5c45c52011-06-07 16:02:34 -0700763 nbits = _W - (shift - nbits)
764 }
765 }
766
767 // convert digits of most-significant word (omit leading zeros)
768 for nbits >= 0 && w != 0 {
769 i--
770 s[i] = charset[w&mask]
771 w >>= shift
772 nbits -= shift
773 }
774
775 return string(s[i:])
776 }
777
778 // general case: extract groups of digits by multiprecision division
779
780 // maximize ndigits where b**ndigits < 2^_W; bb (big base) is b**ndigits
781 bb := Word(1)
782 ndigits := 0
783 for max := Word(_M / b); bb <= max; bb *= b {
784 ndigits++
785 }
786
787 // preserve x, create local copy for use in repeated divisions
Robert Griesemerf5cf0a42011-11-14 13:35:22 -0800788 q := nat(nil).set(x)
Michael T. Jonesd5c45c52011-06-07 16:02:34 -0700789 var r Word
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700790
791 // convert
Michael T. Jonesd5c45c52011-06-07 16:02:34 -0700792 if b == 10 { // hard-coding for 10 here speeds this up by 1.25x
793 for len(q) > 0 {
794 // extract least significant, base bb "digit"
795 q, r = q.divW(q, bb) // N.B. >82% of time is here. Optimize divW
796 if len(q) == 0 {
797 // skip leading zeros in most-significant group of digits
798 for j := 0; j < ndigits && r != 0; j++ {
799 i--
800 s[i] = charset[r%10]
801 r /= 10
802 }
803 } else {
804 for j := 0; j < ndigits; j++ {
805 i--
806 s[i] = charset[r%10]
807 r /= 10
808 }
809 }
810 }
811 } else {
812 for len(q) > 0 {
813 // extract least significant group of digits
814 q, r = q.divW(q, bb) // N.B. >82% of time is here. Optimize divW
815 if len(q) == 0 {
816 // skip leading zeros in most-significant group of digits
817 for j := 0; j < ndigits && r != 0; j++ {
818 i--
819 s[i] = charset[r%b]
820 r /= b
821 }
822 } else {
823 for j := 0; j < ndigits; j++ {
824 i--
825 s[i] = charset[r%b]
826 r /= b
827 }
828 }
829 }
Russ Coxc62b3262009-10-06 11:42:55 -0700830 }
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700831
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800832 return string(s[i:])
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700833}
Adam Langley65063bc2009-11-05 15:55:41 -0800834
Adam Langley19418552009-11-11 13:21:37 -0800835const deBruijn32 = 0x077CB531
836
837var deBruijn32Lookup = []byte{
838 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
839 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9,
840}
841
842const deBruijn64 = 0x03f79d71b4ca8b09
843
844var deBruijn64Lookup = []byte{
845 0, 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4,
846 62, 47, 59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5,
847 63, 55, 48, 27, 60, 41, 37, 16, 46, 35, 44, 21, 52, 32, 23, 11,
848 54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6,
849}
850
851// trailingZeroBits returns the number of consecutive zero bits on the right
852// side of the given Word.
853// See Knuth, volume 4, section 7.3.1
854func trailingZeroBits(x Word) int {
855 // x & -x leaves only the right-most bit set in the word. Let k be the
856 // index of that bit. Since only a single bit is set, the value is two
Robert Henckec8727c82011-05-18 13:14:56 -0400857 // to the power of k. Multiplying by a power of two is equivalent to
Adam Langley19418552009-11-11 13:21:37 -0800858 // left shifting, in this case by k bits. The de Bruijn constant is
859 // such that all six bit, consecutive substrings are distinct.
860 // Therefore, if we have a left shifted version of this constant we can
861 // find by how many bits it was shifted by looking at which six bit
862 // substring ended up at the top of the word.
863 switch _W {
864 case 32:
865 return int(deBruijn32Lookup[((x&-x)*deBruijn32)>>27])
866 case 64:
867 return int(deBruijn64Lookup[((x&-x)*(deBruijn64&_M))>>58])
868 default:
869 panic("Unknown word size")
870 }
871
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800872 return 0
Adam Langley65063bc2009-11-05 15:55:41 -0800873}
874
Robert Griesemer58e77992010-04-30 21:25:48 -0700875// z = x << s
876func (z nat) shl(x nat, s uint) nat {
877 m := len(x)
878 if m == 0 {
879 return z.make(0)
880 }
881 // m > 0
Robert Griesemerb2183702010-04-27 19:16:08 -0700882
Robert Griesemer58e77992010-04-30 21:25:48 -0700883 n := m + int(s/_W)
884 z = z.make(n + 1)
Robert Griesemer5bf57c12011-06-02 12:58:26 -0700885 z[n] = shlVU(z[n-m:n], x, s%_W)
Robert Griesemer3f287b52010-05-06 18:20:01 -0700886 z[0 : n-m].clear()
Robert Griesemer58e77992010-04-30 21:25:48 -0700887
888 return z.norm()
889}
890
Robert Griesemer58e77992010-04-30 21:25:48 -0700891// z = x >> s
892func (z nat) shr(x nat, s uint) nat {
893 m := len(x)
894 n := m - int(s/_W)
895 if n <= 0 {
896 return z.make(0)
897 }
898 // n > 0
899
Robert Griesemer58e77992010-04-30 21:25:48 -0700900 z = z.make(n)
Robert Griesemer5bf57c12011-06-02 12:58:26 -0700901 shrVU(z, x[m-n:], s%_W)
Robert Griesemer58e77992010-04-30 21:25:48 -0700902
903 return z.norm()
904}
905
Roger Peppe83fd82b2011-05-17 13:38:21 -0700906func (z nat) setBit(x nat, i uint, b uint) nat {
907 j := int(i / _W)
908 m := Word(1) << (i % _W)
909 n := len(x)
910 switch b {
911 case 0:
912 z = z.make(n)
913 copy(z, x)
914 if j >= n {
915 // no need to grow
916 return z
917 }
918 z[j] &^= m
919 return z.norm()
920 case 1:
921 if j >= n {
922 n = j + 1
923 }
924 z = z.make(n)
925 copy(z, x)
926 z[j] |= m
927 // no need to normalize
928 return z
929 }
930 panic("set bit is not 0 or 1")
931}
932
Roger Peppe83fd82b2011-05-17 13:38:21 -0700933func (z nat) bit(i uint) uint {
934 j := int(i / _W)
935 if j >= len(z) {
936 return 0
937 }
938 return uint(z[j] >> (i % _W) & 1)
939}
940
Evan Shaw4d1b1572010-05-03 11:20:52 -0700941func (z nat) and(x, y nat) nat {
942 m := len(x)
943 n := len(y)
944 if m > n {
945 m = n
946 }
947 // m <= n
948
949 z = z.make(m)
950 for i := 0; i < m; i++ {
951 z[i] = x[i] & y[i]
952 }
953
954 return z.norm()
955}
956
Evan Shaw4d1b1572010-05-03 11:20:52 -0700957func (z nat) andNot(x, y nat) nat {
958 m := len(x)
959 n := len(y)
960 if n > m {
961 n = m
962 }
963 // m >= n
964
965 z = z.make(m)
966 for i := 0; i < n; i++ {
967 z[i] = x[i] &^ y[i]
968 }
969 copy(z[n:m], x[n:m])
970
971 return z.norm()
972}
973
Evan Shaw4d1b1572010-05-03 11:20:52 -0700974func (z nat) or(x, y nat) nat {
975 m := len(x)
976 n := len(y)
977 s := x
978 if m < n {
979 n, m = m, n
980 s = y
981 }
Evan Shaw28a09712010-08-09 10:21:54 -0700982 // m >= n
Evan Shaw4d1b1572010-05-03 11:20:52 -0700983
Evan Shaw28a09712010-08-09 10:21:54 -0700984 z = z.make(m)
985 for i := 0; i < n; i++ {
Evan Shaw4d1b1572010-05-03 11:20:52 -0700986 z[i] = x[i] | y[i]
987 }
Evan Shaw28a09712010-08-09 10:21:54 -0700988 copy(z[n:m], s[n:m])
Evan Shaw4d1b1572010-05-03 11:20:52 -0700989
990 return z.norm()
991}
992
Evan Shaw4d1b1572010-05-03 11:20:52 -0700993func (z nat) xor(x, y nat) nat {
994 m := len(x)
995 n := len(y)
996 s := x
Evan Shaw28a09712010-08-09 10:21:54 -0700997 if m < n {
Evan Shaw4d1b1572010-05-03 11:20:52 -0700998 n, m = m, n
999 s = y
1000 }
Evan Shaw28a09712010-08-09 10:21:54 -07001001 // m >= n
Evan Shaw4d1b1572010-05-03 11:20:52 -07001002
Evan Shaw28a09712010-08-09 10:21:54 -07001003 z = z.make(m)
1004 for i := 0; i < n; i++ {
Evan Shaw4d1b1572010-05-03 11:20:52 -07001005 z[i] = x[i] ^ y[i]
1006 }
Evan Shaw28a09712010-08-09 10:21:54 -07001007 copy(z[n:m], s[n:m])
Evan Shaw4d1b1572010-05-03 11:20:52 -07001008
1009 return z.norm()
1010}
1011
Adam Langley65063bc2009-11-05 15:55:41 -08001012// greaterThan returns true iff (x1<<_W + x2) > (y1<<_W + y2)
Michael T. Jonesd5c45c52011-06-07 16:02:34 -07001013func greaterThan(x1, x2, y1, y2 Word) bool {
1014 return x1 > y1 || x1 == y1 && x2 > y2
1015}
Adam Langley19418552009-11-11 13:21:37 -08001016
Evan Shaw841a32d2010-04-22 16:57:29 -07001017// modW returns x % d.
1018func (x nat) modW(d Word) (r Word) {
Adam Langley19418552009-11-11 13:21:37 -08001019 // TODO(agl): we don't actually need to store the q value.
Evan Shaw841a32d2010-04-22 16:57:29 -07001020 var q nat
Robert Griesemerb2183702010-04-27 19:16:08 -07001021 q = q.make(len(x))
Robert Griesemer52cc0582010-05-08 13:52:36 -07001022 return divWVW(q, 0, x, d)
Adam Langley19418552009-11-11 13:21:37 -08001023}
1024
Robert Griesemer5bf57c12011-06-02 12:58:26 -07001025// powersOfTwoDecompose finds q and k with x = q * 1<<k and q is odd, or q and k are 0.
1026func (x nat) powersOfTwoDecompose() (q nat, k int) {
1027 if len(x) == 0 {
1028 return x, 0
Adam Langley19418552009-11-11 13:21:37 -08001029 }
1030
Robert Griesemer5bf57c12011-06-02 12:58:26 -07001031 // One of the words must be non-zero by definition,
1032 // so this loop will terminate with i < len(x), and
1033 // i is the number of 0 words.
1034 i := 0
1035 for x[i] == 0 {
1036 i++
Adam Langley19418552009-11-11 13:21:37 -08001037 }
Robert Griesemer5bf57c12011-06-02 12:58:26 -07001038 n := trailingZeroBits(x[i]) // x[i] != 0
Adam Langley19418552009-11-11 13:21:37 -08001039
Robert Griesemer5bf57c12011-06-02 12:58:26 -07001040 q = make(nat, len(x)-i)
1041 shrVU(q, x[i:], uint(n))
1042
Evan Shaw841a32d2010-04-22 16:57:29 -07001043 q = q.norm()
Robert Griesemer5bf57c12011-06-02 12:58:26 -07001044 k = i*_W + n
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001045 return
Adam Langley19418552009-11-11 13:21:37 -08001046}
1047
Evan Shaw841a32d2010-04-22 16:57:29 -07001048// random creates a random integer in [0..limit), using the space in z if
Adam Langley19418552009-11-11 13:21:37 -08001049// possible. n is the bit length of limit.
Evan Shaw841a32d2010-04-22 16:57:29 -07001050func (z nat) random(rand *rand.Rand, limit nat, n int) nat {
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001051 bitLengthOfMSW := uint(n % _W)
Russ Coxcfbee342010-01-05 16:49:05 -08001052 if bitLengthOfMSW == 0 {
1053 bitLengthOfMSW = _W
1054 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001055 mask := Word((1 << bitLengthOfMSW) - 1)
Robert Griesemerb2183702010-04-27 19:16:08 -07001056 z = z.make(len(limit))
Adam Langley19418552009-11-11 13:21:37 -08001057
1058 for {
1059 for i := range z {
1060 switch _W {
1061 case 32:
1062 z[i] = Word(rand.Uint32())
1063 case 64:
1064 z[i] = Word(rand.Uint32()) | Word(rand.Uint32())<<32
1065 }
1066 }
1067
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001068 z[len(limit)-1] &= mask
Adam Langley19418552009-11-11 13:21:37 -08001069
Evan Shaw841a32d2010-04-22 16:57:29 -07001070 if z.cmp(limit) < 0 {
Adam Langley19418552009-11-11 13:21:37 -08001071 break
1072 }
1073 }
1074
Evan Shaw841a32d2010-04-22 16:57:29 -07001075 return z.norm()
Adam Langley19418552009-11-11 13:21:37 -08001076}
1077
Evan Shaw841a32d2010-04-22 16:57:29 -07001078// If m != nil, expNN calculates x**y mod m. Otherwise it calculates x**y. It
Adam Langley19418552009-11-11 13:21:37 -08001079// reuses the storage of z if possible.
Evan Shaw841a32d2010-04-22 16:57:29 -07001080func (z nat) expNN(x, y, m nat) nat {
Adam Langleyeadebba2010-05-24 14:32:55 -04001081 if alias(z, x) || alias(z, y) {
1082 // We cannot allow in place modification of x or y.
1083 z = nil
1084 }
1085
Adam Langley19418552009-11-11 13:21:37 -08001086 if len(y) == 0 {
Robert Griesemerb2183702010-04-27 19:16:08 -07001087 z = z.make(1)
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001088 z[0] = 1
1089 return z
Adam Langley19418552009-11-11 13:21:37 -08001090 }
1091
1092 if m != nil {
1093 // We likely end up being as long as the modulus.
Robert Griesemerb2183702010-04-27 19:16:08 -07001094 z = z.make(len(m))
Adam Langley19418552009-11-11 13:21:37 -08001095 }
Evan Shaw841a32d2010-04-22 16:57:29 -07001096 z = z.set(x)
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001097 v := y[len(y)-1]
Adam Langley19418552009-11-11 13:21:37 -08001098 // It's invalid for the most significant word to be zero, therefore we
1099 // will find a one bit.
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001100 shift := leadingZeros(v) + 1
1101 v <<= shift
Evan Shaw841a32d2010-04-22 16:57:29 -07001102 var q nat
Adam Langley19418552009-11-11 13:21:37 -08001103
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001104 const mask = 1 << (_W - 1)
Adam Langley19418552009-11-11 13:21:37 -08001105
1106 // We walk through the bits of the exponent one by one. Each time we
1107 // see a bit, we square, thus doubling the power. If the bit is a one,
1108 // we also multiply by x, thus adding one to the power.
1109
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001110 w := _W - int(shift)
Adam Langley19418552009-11-11 13:21:37 -08001111 for j := 0; j < w; j++ {
Evan Shaw841a32d2010-04-22 16:57:29 -07001112 z = z.mul(z, z)
Adam Langley19418552009-11-11 13:21:37 -08001113
1114 if v&mask != 0 {
Evan Shaw841a32d2010-04-22 16:57:29 -07001115 z = z.mul(z, x)
Adam Langley19418552009-11-11 13:21:37 -08001116 }
1117
1118 if m != nil {
Evan Shaw841a32d2010-04-22 16:57:29 -07001119 q, z = q.div(z, z, m)
Adam Langley19418552009-11-11 13:21:37 -08001120 }
1121
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001122 v <<= 1
Adam Langley19418552009-11-11 13:21:37 -08001123 }
1124
1125 for i := len(y) - 2; i >= 0; i-- {
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001126 v = y[i]
Adam Langley19418552009-11-11 13:21:37 -08001127
1128 for j := 0; j < _W; j++ {
Evan Shaw841a32d2010-04-22 16:57:29 -07001129 z = z.mul(z, z)
Adam Langley19418552009-11-11 13:21:37 -08001130
1131 if v&mask != 0 {
Evan Shaw841a32d2010-04-22 16:57:29 -07001132 z = z.mul(z, x)
Adam Langley19418552009-11-11 13:21:37 -08001133 }
1134
1135 if m != nil {
Evan Shaw841a32d2010-04-22 16:57:29 -07001136 q, z = q.div(z, z, m)
Adam Langley19418552009-11-11 13:21:37 -08001137 }
1138
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001139 v <<= 1
Adam Langley19418552009-11-11 13:21:37 -08001140 }
1141 }
1142
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001143 return z
Adam Langley19418552009-11-11 13:21:37 -08001144}
1145
Adam Langley308064f2010-03-05 15:55:26 -05001146// probablyPrime performs reps Miller-Rabin tests to check whether n is prime.
Russ Coxcfbee342010-01-05 16:49:05 -08001147// If it returns true, n is prime with probability 1 - 1/4^reps.
Adam Langley19418552009-11-11 13:21:37 -08001148// If it returns false, n is not prime.
Evan Shaw841a32d2010-04-22 16:57:29 -07001149func (n nat) probablyPrime(reps int) bool {
Adam Langley19418552009-11-11 13:21:37 -08001150 if len(n) == 0 {
1151 return false
1152 }
1153
1154 if len(n) == 1 {
Adam Langley308064f2010-03-05 15:55:26 -05001155 if n[0] < 2 {
1156 return false
1157 }
1158
Adam Langley19418552009-11-11 13:21:37 -08001159 if n[0]%2 == 0 {
1160 return n[0] == 2
1161 }
1162
1163 // We have to exclude these cases because we reject all
1164 // multiples of these numbers below.
Robert Griesemer407dbb42010-04-30 11:54:27 -07001165 switch n[0] {
1166 case 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53:
Adam Langley19418552009-11-11 13:21:37 -08001167 return true
1168 }
1169 }
1170
Robert Griesemerb2183702010-04-27 19:16:08 -07001171 const primesProduct32 = 0xC0CFD797 // Π {p ∈ primes, 2 < p <= 29}
1172 const primesProduct64 = 0xE221F97C30E94E1D // Π {p ∈ primes, 2 < p <= 53}
1173
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001174 var r Word
Adam Langley19418552009-11-11 13:21:37 -08001175 switch _W {
1176 case 32:
Evan Shaw841a32d2010-04-22 16:57:29 -07001177 r = n.modW(primesProduct32)
Adam Langley19418552009-11-11 13:21:37 -08001178 case 64:
Evan Shaw841a32d2010-04-22 16:57:29 -07001179 r = n.modW(primesProduct64 & _M)
Adam Langley19418552009-11-11 13:21:37 -08001180 default:
1181 panic("Unknown word size")
1182 }
1183
1184 if r%3 == 0 || r%5 == 0 || r%7 == 0 || r%11 == 0 ||
1185 r%13 == 0 || r%17 == 0 || r%19 == 0 || r%23 == 0 || r%29 == 0 {
1186 return false
1187 }
1188
1189 if _W == 64 && (r%31 == 0 || r%37 == 0 || r%41 == 0 ||
1190 r%43 == 0 || r%47 == 0 || r%53 == 0) {
1191 return false
1192 }
1193
Robert Griesemerf5cf0a42011-11-14 13:35:22 -08001194 nm1 := nat(nil).sub(n, natOne)
Adam Langley19418552009-11-11 13:21:37 -08001195 // 1<<k * q = nm1;
Evan Shaw841a32d2010-04-22 16:57:29 -07001196 q, k := nm1.powersOfTwoDecompose()
Adam Langley19418552009-11-11 13:21:37 -08001197
Robert Griesemerf5cf0a42011-11-14 13:35:22 -08001198 nm3 := nat(nil).sub(nm1, natTwo)
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001199 rand := rand.New(rand.NewSource(int64(n[0])))
Adam Langley19418552009-11-11 13:21:37 -08001200
Evan Shaw841a32d2010-04-22 16:57:29 -07001201 var x, y, quotient nat
Robert Griesemer26078c32010-05-01 15:11:27 -07001202 nm3Len := nm3.bitLen()
Adam Langley19418552009-11-11 13:21:37 -08001203
1204NextRandom:
1205 for i := 0; i < reps; i++ {
Evan Shaw841a32d2010-04-22 16:57:29 -07001206 x = x.random(rand, nm3, nm3Len)
Robert Griesemerb2183702010-04-27 19:16:08 -07001207 x = x.add(x, natTwo)
Evan Shaw841a32d2010-04-22 16:57:29 -07001208 y = y.expNN(x, q, n)
Robert Griesemerb2183702010-04-27 19:16:08 -07001209 if y.cmp(natOne) == 0 || y.cmp(nm1) == 0 {
Adam Langley19418552009-11-11 13:21:37 -08001210 continue
1211 }
Robert Griesemer5bf57c12011-06-02 12:58:26 -07001212 for j := 1; j < k; j++ {
Evan Shaw841a32d2010-04-22 16:57:29 -07001213 y = y.mul(y, y)
1214 quotient, y = quotient.div(y, y, n)
1215 if y.cmp(nm1) == 0 {
Adam Langley19418552009-11-11 13:21:37 -08001216 continue NextRandom
1217 }
Robert Griesemerb2183702010-04-27 19:16:08 -07001218 if y.cmp(natOne) == 0 {
Adam Langley19418552009-11-11 13:21:37 -08001219 return false
1220 }
1221 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001222 return false
Adam Langley19418552009-11-11 13:21:37 -08001223 }
1224
Robert Griesemer5a1d3322009-12-15 15:33:31 -08001225 return true
Adam Langley19418552009-11-11 13:21:37 -08001226}
Robert Griesemer758d0552011-03-08 17:27:44 -08001227
Robert Griesemer758d0552011-03-08 17:27:44 -08001228// bytes writes the value of z into buf using big-endian encoding.
1229// len(buf) must be >= len(z)*_S. The value of z is encoded in the
1230// slice buf[i:]. The number i of unused bytes at the beginning of
1231// buf is returned as result.
1232func (z nat) bytes(buf []byte) (i int) {
1233 i = len(buf)
1234 for _, d := range z {
1235 for j := 0; j < _S; j++ {
1236 i--
1237 buf[i] = byte(d)
1238 d >>= 8
1239 }
1240 }
1241
1242 for i < len(buf) && buf[i] == 0 {
1243 i++
1244 }
1245
1246 return
1247}
1248
Robert Griesemer758d0552011-03-08 17:27:44 -08001249// setBytes interprets buf as the bytes of a big-endian unsigned
1250// integer, sets z to that value, and returns z.
1251func (z nat) setBytes(buf []byte) nat {
1252 z = z.make((len(buf) + _S - 1) / _S)
1253
1254 k := 0
1255 s := uint(0)
1256 var d Word
1257 for i := len(buf); i > 0; i-- {
1258 d |= Word(buf[i-1]) << s
1259 if s += 8; s == _S*8 {
1260 z[k] = d
1261 k++
1262 s = 0
1263 d = 0
1264 }
1265 }
1266 if k < len(z) {
1267 z[k] = d
1268 }
1269
1270 return z.norm()
1271}