Source file src/math/big/nat.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This file implements unsigned multi-precision integers (natural
     6  // numbers). They are the building blocks for the implementation
     7  // of signed integers, rationals, and floating-point numbers.
     8  //
     9  // Caution: This implementation relies on the function "alias"
    10  //          which assumes that (nat) slice capacities are never
    11  //          changed (no 3-operand slice expressions). If that
    12  //          changes, alias needs to be updated for correctness.
    13  
    14  package big
    15  
    16  import (
    17  	"internal/byteorder"
    18  	"math/bits"
    19  	"math/rand"
    20  	"slices"
    21  	"sync"
    22  )
    23  
    24  // An unsigned integer x of the form
    25  //
    26  //	x = x[n-1]*_B^(n-1) + x[n-2]*_B^(n-2) + ... + x[1]*_B + x[0]
    27  //
    28  // with 0 <= x[i] < _B and 0 <= i < n is stored in a slice of length n,
    29  // with the digits x[i] as the slice elements.
    30  //
    31  // A number is normalized if the slice contains no leading 0 digits.
    32  // During arithmetic operations, denormalized values may occur but are
    33  // always normalized before returning the final result. The normalized
    34  // representation of 0 is the empty or nil slice (length = 0).
    35  type nat []Word
    36  
    37  var (
    38  	natOne  = nat{1}
    39  	natTwo  = nat{2}
    40  	natFive = nat{5}
    41  	natTen  = nat{10}
    42  )
    43  
    44  func (z nat) String() string {
    45  	return "0x" + string(z.itoa(false, 16))
    46  }
    47  
    48  func (z nat) norm() nat {
    49  	i := len(z)
    50  	for i > 0 && z[i-1] == 0 {
    51  		i--
    52  	}
    53  	return z[0:i]
    54  }
    55  
    56  func (z nat) make(n int) nat {
    57  	if n <= cap(z) {
    58  		return z[:n] // reuse z
    59  	}
    60  	if n == 1 {
    61  		// Most nats start small and stay that way; don't over-allocate.
    62  		return make(nat, 1)
    63  	}
    64  	// Choosing a good value for e has significant performance impact
    65  	// because it increases the chance that a value can be reused.
    66  	const e = 4 // extra capacity
    67  	return make(nat, n, n+e)
    68  }
    69  
    70  func (z nat) setWord(x Word) nat {
    71  	if x == 0 {
    72  		return z[:0]
    73  	}
    74  	z = z.make(1)
    75  	z[0] = x
    76  	return z
    77  }
    78  
    79  func (z nat) setUint64(x uint64) nat {
    80  	// single-word value
    81  	if w := Word(x); uint64(w) == x {
    82  		return z.setWord(w)
    83  	}
    84  	// 2-word value
    85  	z = z.make(2)
    86  	z[1] = Word(x >> 32)
    87  	z[0] = Word(x)
    88  	return z
    89  }
    90  
    91  func (z nat) set(x nat) nat {
    92  	z = z.make(len(x))
    93  	copy(z, x)
    94  	return z
    95  }
    96  
    97  func (z nat) add(x, y nat) nat {
    98  	m := len(x)
    99  	n := len(y)
   100  
   101  	switch {
   102  	case m < n:
   103  		return z.add(y, x)
   104  	case m == 0:
   105  		// n == 0 because m >= n; result is 0
   106  		return z[:0]
   107  	case n == 0:
   108  		// result is x
   109  		return z.set(x)
   110  	}
   111  	// m > 0
   112  
   113  	z = z.make(m + 1)
   114  	c := addVV(z[:n], x[:n], y[:n])
   115  	if m > n {
   116  		c = addVW(z[n:m], x[n:], c)
   117  	}
   118  	z[m] = c
   119  
   120  	return z.norm()
   121  }
   122  
   123  func (z nat) sub(x, y nat) nat {
   124  	m := len(x)
   125  	n := len(y)
   126  
   127  	switch {
   128  	case m < n:
   129  		panic("underflow")
   130  	case m == 0:
   131  		// n == 0 because m >= n; result is 0
   132  		return z[:0]
   133  	case n == 0:
   134  		// result is x
   135  		return z.set(x)
   136  	}
   137  	// m > 0
   138  
   139  	z = z.make(m)
   140  	c := subVV(z[:n], x[:n], y[:n])
   141  	if m > n {
   142  		c = subVW(z[n:], x[n:], c)
   143  	}
   144  	if c != 0 {
   145  		panic("underflow")
   146  	}
   147  
   148  	return z.norm()
   149  }
   150  
   151  func (x nat) cmp(y nat) (r int) {
   152  	m := len(x)
   153  	n := len(y)
   154  	if m != n || m == 0 {
   155  		switch {
   156  		case m < n:
   157  			r = -1
   158  		case m > n:
   159  			r = 1
   160  		}
   161  		return
   162  	}
   163  
   164  	i := m - 1
   165  	for i > 0 && x[i] == y[i] {
   166  		i--
   167  	}
   168  
   169  	switch {
   170  	case x[i] < y[i]:
   171  		r = -1
   172  	case x[i] > y[i]:
   173  		r = 1
   174  	}
   175  	return
   176  }
   177  
   178  // montgomery computes z mod m = x*y*2**(-n*_W) mod m,
   179  // assuming k = -1/m mod 2**_W.
   180  // z is used for storing the result which is returned;
   181  // z must not alias x, y or m.
   182  // See Gueron, "Efficient Software Implementations of Modular Exponentiation".
   183  // https://eprint.iacr.org/2011/239.pdf
   184  // In the terminology of that paper, this is an "Almost Montgomery Multiplication":
   185  // x and y are required to satisfy 0 <= z < 2**(n*_W) and then the result
   186  // z is guaranteed to satisfy 0 <= z < 2**(n*_W), but it may not be < m.
   187  func (z nat) montgomery(x, y, m nat, k Word, n int) nat {
   188  	// This code assumes x, y, m are all the same length, n.
   189  	// (required by addMulVVW and the for loop).
   190  	// It also assumes that x, y are already reduced mod m,
   191  	// or else the result will not be properly reduced.
   192  	if len(x) != n || len(y) != n || len(m) != n {
   193  		panic("math/big: mismatched montgomery number lengths")
   194  	}
   195  	z = z.make(n * 2)
   196  	clear(z)
   197  	var c Word
   198  	for i := 0; i < n; i++ {
   199  		d := y[i]
   200  		c2 := addMulVVWW(z[i:n+i], z[i:n+i], x, d, 0)
   201  		t := z[i] * k
   202  		c3 := addMulVVWW(z[i:n+i], z[i:n+i], m, t, 0)
   203  		cx := c + c2
   204  		cy := cx + c3
   205  		z[n+i] = cy
   206  		if cx < c2 || cy < c3 {
   207  			c = 1
   208  		} else {
   209  			c = 0
   210  		}
   211  	}
   212  	if c != 0 {
   213  		subVV(z[:n], z[n:], m)
   214  	} else {
   215  		copy(z[:n], z[n:])
   216  	}
   217  	return z[:n]
   218  }
   219  
   220  // alias reports whether x and y share the same base array.
   221  //
   222  // Note: alias assumes that the capacity of underlying arrays
   223  // is never changed for nat values; i.e. that there are
   224  // no 3-operand slice expressions in this code (or worse,
   225  // reflect-based operations to the same effect).
   226  func alias(x, y nat) bool {
   227  	return cap(x) > 0 && cap(y) > 0 && &x[0:cap(x)][cap(x)-1] == &y[0:cap(y)][cap(y)-1]
   228  }
   229  
   230  // addTo implements z += x; z must be long enough.
   231  // (we don't use nat.add because we need z to stay the same
   232  // slice, and we don't need to normalize z after each addition)
   233  func addTo(z, x nat) {
   234  	if n := len(x); n > 0 {
   235  		if c := addVV(z[:n], z[:n], x[:n]); c != 0 {
   236  			if n < len(z) {
   237  				addVW(z[n:], z[n:], c)
   238  			}
   239  		}
   240  	}
   241  }
   242  
   243  // mulRange computes the product of all the unsigned integers in the
   244  // range [a, b] inclusively. If a > b (empty range), the result is 1.
   245  // The caller may pass stk == nil to request that mulRange obtain and release one itself.
   246  func (z nat) mulRange(stk *stack, a, b uint64) nat {
   247  	switch {
   248  	case a == 0:
   249  		// cut long ranges short (optimization)
   250  		return z.setUint64(0)
   251  	case a > b:
   252  		return z.setUint64(1)
   253  	case a == b:
   254  		return z.setUint64(a)
   255  	case a+1 == b:
   256  		return z.mul(stk, nat(nil).setUint64(a), nat(nil).setUint64(b))
   257  	}
   258  
   259  	if stk == nil {
   260  		stk = getStack()
   261  		defer stk.free()
   262  	}
   263  
   264  	m := a + (b-a)/2 // avoid overflow
   265  	return z.mul(stk, nat(nil).mulRange(stk, a, m), nat(nil).mulRange(stk, m+1, b))
   266  }
   267  
   268  // A stackInner provides temporary storage for complex calculations
   269  // such as multiplication and division.
   270  // It should only be used by [stack], below.
   271  type stackInner struct {
   272  	w []Word
   273  }
   274  
   275  var stackPool sync.Pool // pool of *stackInner
   276  
   277  // getStack returns a temporary stack.
   278  // The caller must call [stack.free] to give up use of the stack when finished.
   279  func getStackInner() *stackInner {
   280  	s, _ := stackPool.Get().(*stackInner)
   281  	if s == nil {
   282  		s = new(stackInner)
   283  	}
   284  	return s
   285  }
   286  
   287  // free returns the stack for use by another calculation.
   288  func (s *stackInner) free() {
   289  	s.w = s.w[:0]
   290  	stackPool.Put(s)
   291  }
   292  
   293  // save returns the current stack pointer.
   294  // A future call to restore with the same value
   295  // frees any temporaries allocated on the stack after the call to save.
   296  func (s *stackInner) save() int {
   297  	return len(s.w)
   298  }
   299  
   300  // restore restores the stack pointer to n.
   301  // It is almost always invoked as
   302  //
   303  //	defer stk.restore(stk.save())
   304  //
   305  // which makes sure to pop any temporaries allocated in the current function
   306  // from the stack before returning.
   307  func (s *stackInner) restore(n int) {
   308  	s.w = s.w[:n]
   309  }
   310  
   311  // nat returns a nat of n words, allocated on the stack.
   312  func (s *stackInner) nat(n int) nat {
   313  	nr := (n + 3) &^ 3 // round up to multiple of 4
   314  	off := len(s.w)
   315  	s.w = slices.Grow(s.w, nr)
   316  	s.w = s.w[:off+nr]
   317  	x := s.w[off : off+n : off+n]
   318  	if n > 0 {
   319  		x[0] = 0xfedcb
   320  	}
   321  	return x
   322  }
   323  
   324  // A stack provides temporary storage for complex calculations
   325  // such as multiplication and division.
   326  // In general, if a function takes a *stack, it expects a non-nil *stack.
   327  // However, certain functions may allow passing a nil *stack instead,
   328  // so that they can handle trivial stack-free cases without forcing the
   329  // caller to obtain and free a stack that will be unused. These functions
   330  // document that they accept a nil *stack in their doc comments.
   331  type stack struct {
   332  	si *stackInner
   333  }
   334  
   335  func getStack() *stack {
   336  	return &stack{}
   337  }
   338  func (s *stack) free() {
   339  	si := s.si
   340  	if si != nil {
   341  		si.free()
   342  	}
   343  }
   344  func (s *stack) save() int {
   345  	si := s.si
   346  	if si == nil {
   347  		return 0
   348  	}
   349  	return si.save()
   350  }
   351  func (s *stack) restore(n int) {
   352  	si := s.si
   353  	if si == nil {
   354  		return
   355  	}
   356  	si.restore(n)
   357  }
   358  func (s *stack) nat(n int) nat {
   359  	si := s.si
   360  	if si == nil {
   361  		if n <= 4 {
   362  			// For small allocations, just ask the allocator.
   363  			// It isn't worth pooling these allocations.
   364  			// See issue 73999.
   365  			r := slices.Grow(nat(nil), n)
   366  			r = r[:n]
   367  			if n > 0 {
   368  				r[0] = 0xabcdef
   369  			}
   370  			return r
   371  		}
   372  		si, _ = stackPool.Get().(*stackInner)
   373  		if si == nil {
   374  			si = new(stackInner)
   375  		}
   376  		s.si = si
   377  	}
   378  	return si.nat(n)
   379  }
   380  
   381  // bitLen returns the length of x in bits.
   382  // Unlike most methods, it works even if x is not normalized.
   383  func (x nat) bitLen() int {
   384  	// This function is used in cryptographic operations. It must not leak
   385  	// anything but the Int's sign and bit size through side-channels. Any
   386  	// changes must be reviewed by a security expert.
   387  	if i := len(x) - 1; i >= 0 {
   388  		// bits.Len uses a lookup table for the low-order bits on some
   389  		// architectures. Neutralize any input-dependent behavior by setting all
   390  		// bits after the first one bit.
   391  		top := uint(x[i])
   392  		top |= top >> 1
   393  		top |= top >> 2
   394  		top |= top >> 4
   395  		top |= top >> 8
   396  		top |= top >> 16
   397  		top |= top >> 16 >> 16 // ">> 32" doesn't compile on 32-bit architectures
   398  		return i*_W + bits.Len(top)
   399  	}
   400  	return 0
   401  }
   402  
   403  // trailingZeroBits returns the number of consecutive least significant zero
   404  // bits of x.
   405  func (x nat) trailingZeroBits() uint {
   406  	if len(x) == 0 {
   407  		return 0
   408  	}
   409  	var i uint
   410  	for x[i] == 0 {
   411  		i++
   412  	}
   413  	// x[i] != 0
   414  	return i*_W + uint(bits.TrailingZeros(uint(x[i])))
   415  }
   416  
   417  // isPow2 returns i, true when x == 2**i and 0, false otherwise.
   418  func (x nat) isPow2() (uint, bool) {
   419  	var i uint
   420  	for x[i] == 0 {
   421  		i++
   422  	}
   423  	if i == uint(len(x))-1 && x[i]&(x[i]-1) == 0 {
   424  		return i*_W + uint(bits.TrailingZeros(uint(x[i]))), true
   425  	}
   426  	return 0, false
   427  }
   428  
   429  func same(x, y nat) bool {
   430  	return len(x) == len(y) && len(x) > 0 && &x[0] == &y[0]
   431  }
   432  
   433  // z = x << s
   434  func (z nat) lsh(x nat, s uint) nat {
   435  	if s == 0 {
   436  		if same(z, x) {
   437  			return z
   438  		}
   439  		if !alias(z, x) {
   440  			return z.set(x)
   441  		}
   442  	}
   443  
   444  	m := len(x)
   445  	if m == 0 {
   446  		return z[:0]
   447  	}
   448  	// m > 0
   449  
   450  	n := m + int(s/_W)
   451  	z = z.make(n + 1)
   452  	if s %= _W; s == 0 {
   453  		copy(z[n-m:n], x)
   454  		z[n] = 0
   455  	} else {
   456  		z[n] = lshVU(z[n-m:n], x, s)
   457  	}
   458  	clear(z[0 : n-m])
   459  
   460  	return z.norm()
   461  }
   462  
   463  // z = x >> s
   464  func (z nat) rsh(x nat, s uint) nat {
   465  	if s == 0 {
   466  		if same(z, x) {
   467  			return z
   468  		}
   469  		if !alias(z, x) {
   470  			return z.set(x)
   471  		}
   472  	}
   473  
   474  	m := len(x)
   475  	n := m - int(s/_W)
   476  	if n <= 0 {
   477  		return z[:0]
   478  	}
   479  	// n > 0
   480  
   481  	z = z.make(n)
   482  	if s %= _W; s == 0 {
   483  		copy(z, x[m-n:])
   484  	} else {
   485  		rshVU(z, x[m-n:], s)
   486  	}
   487  
   488  	return z.norm()
   489  }
   490  
   491  func (z nat) setBit(x nat, i uint, b uint) nat {
   492  	j := int(i / _W)
   493  	m := Word(1) << (i % _W)
   494  	n := len(x)
   495  	switch b {
   496  	case 0:
   497  		z = z.make(n)
   498  		copy(z, x)
   499  		if j >= n {
   500  			// no need to grow
   501  			return z
   502  		}
   503  		z[j] &^= m
   504  		return z.norm()
   505  	case 1:
   506  		if j >= n {
   507  			z = z.make(j + 1)
   508  			clear(z[n:])
   509  		} else {
   510  			z = z.make(n)
   511  		}
   512  		copy(z, x)
   513  		z[j] |= m
   514  		// no need to normalize
   515  		return z
   516  	}
   517  	panic("set bit is not 0 or 1")
   518  }
   519  
   520  // bit returns the value of the i'th bit, with lsb == bit 0.
   521  func (x nat) bit(i uint) uint {
   522  	j := i / _W
   523  	if j >= uint(len(x)) {
   524  		return 0
   525  	}
   526  	// 0 <= j < len(x)
   527  	return uint(x[j] >> (i % _W) & 1)
   528  }
   529  
   530  // sticky returns 1 if there's a 1 bit within the
   531  // i least significant bits, otherwise it returns 0.
   532  func (x nat) sticky(i uint) uint {
   533  	j := i / _W
   534  	if j >= uint(len(x)) {
   535  		if len(x) == 0 {
   536  			return 0
   537  		}
   538  		return 1
   539  	}
   540  	// 0 <= j < len(x)
   541  	for _, x := range x[:j] {
   542  		if x != 0 {
   543  			return 1
   544  		}
   545  	}
   546  	if x[j]<<(_W-i%_W) != 0 {
   547  		return 1
   548  	}
   549  	return 0
   550  }
   551  
   552  func (z nat) and(x, y nat) nat {
   553  	m := len(x)
   554  	n := len(y)
   555  	if m > n {
   556  		m = n
   557  	}
   558  	// m <= n
   559  
   560  	z = z.make(m)
   561  	for i := 0; i < m; i++ {
   562  		z[i] = x[i] & y[i]
   563  	}
   564  
   565  	return z.norm()
   566  }
   567  
   568  // trunc returns z = x mod 2ⁿ.
   569  func (z nat) trunc(x nat, n uint) nat {
   570  	w := (n + _W - 1) / _W
   571  	if uint(len(x)) < w {
   572  		return z.set(x)
   573  	}
   574  	z = z.make(int(w))
   575  	copy(z, x)
   576  	if n%_W != 0 {
   577  		z[len(z)-1] &= 1<<(n%_W) - 1
   578  	}
   579  	return z.norm()
   580  }
   581  
   582  func (z nat) andNot(x, y nat) nat {
   583  	m := len(x)
   584  	n := len(y)
   585  	if n > m {
   586  		n = m
   587  	}
   588  	// m >= n
   589  
   590  	z = z.make(m)
   591  	for i := 0; i < n; i++ {
   592  		z[i] = x[i] &^ y[i]
   593  	}
   594  	copy(z[n:m], x[n:m])
   595  
   596  	return z.norm()
   597  }
   598  
   599  func (z nat) or(x, y nat) nat {
   600  	m := len(x)
   601  	n := len(y)
   602  	s := x
   603  	if m < n {
   604  		n, m = m, n
   605  		s = y
   606  	}
   607  	// m >= n
   608  
   609  	z = z.make(m)
   610  	for i := 0; i < n; i++ {
   611  		z[i] = x[i] | y[i]
   612  	}
   613  	copy(z[n:m], s[n:m])
   614  
   615  	return z.norm()
   616  }
   617  
   618  func (z nat) xor(x, y nat) nat {
   619  	m := len(x)
   620  	n := len(y)
   621  	s := x
   622  	if m < n {
   623  		n, m = m, n
   624  		s = y
   625  	}
   626  	// m >= n
   627  
   628  	z = z.make(m)
   629  	for i := 0; i < n; i++ {
   630  		z[i] = x[i] ^ y[i]
   631  	}
   632  	copy(z[n:m], s[n:m])
   633  
   634  	return z.norm()
   635  }
   636  
   637  // random creates a random integer in [0..limit), using the space in z if
   638  // possible. n is the bit length of limit.
   639  func (z nat) random(rand *rand.Rand, limit nat, n int) nat {
   640  	if alias(z, limit) {
   641  		z = nil // z is an alias for limit - cannot reuse
   642  	}
   643  	z = z.make(len(limit))
   644  
   645  	bitLengthOfMSW := uint(n % _W)
   646  	if bitLengthOfMSW == 0 {
   647  		bitLengthOfMSW = _W
   648  	}
   649  	mask := Word((1 << bitLengthOfMSW) - 1)
   650  
   651  	for {
   652  		switch _W {
   653  		case 32:
   654  			for i := range z {
   655  				z[i] = Word(rand.Uint32())
   656  			}
   657  		case 64:
   658  			for i := range z {
   659  				z[i] = Word(rand.Uint32()) | Word(rand.Uint32())<<32
   660  			}
   661  		default:
   662  			panic("unknown word size")
   663  		}
   664  		z[len(limit)-1] &= mask
   665  		if z.cmp(limit) < 0 {
   666  			break
   667  		}
   668  	}
   669  
   670  	return z.norm()
   671  }
   672  
   673  // If m != 0 (i.e., len(m) != 0), expNN sets z to x**y mod m;
   674  // otherwise it sets z to x**y. The result is the value of z.
   675  // The caller may pass stk == nil to request that expNN obtain and release one itself.
   676  func (z nat) expNN(stk *stack, x, y, m nat, slow bool) nat {
   677  	if alias(z, x) || alias(z, y) {
   678  		// We cannot allow in-place modification of x or y.
   679  		z = nil
   680  	}
   681  
   682  	// x**y mod 1 == 0
   683  	if len(m) == 1 && m[0] == 1 {
   684  		return z.setWord(0)
   685  	}
   686  	// m == 0 || m > 1
   687  
   688  	// x**0 == 1
   689  	if len(y) == 0 {
   690  		return z.setWord(1)
   691  	}
   692  	// y > 0
   693  
   694  	// 0**y = 0
   695  	if len(x) == 0 {
   696  		return z.setWord(0)
   697  	}
   698  	// x > 0
   699  
   700  	// 1**y = 1
   701  	if len(x) == 1 && x[0] == 1 {
   702  		return z.setWord(1)
   703  	}
   704  	// x > 1
   705  
   706  	// x**1 == x
   707  	if len(y) == 1 && y[0] == 1 && len(m) == 0 {
   708  		return z.set(x)
   709  	}
   710  	if stk == nil {
   711  		stk = getStack()
   712  		defer stk.free()
   713  	}
   714  	if len(y) == 1 && y[0] == 1 { // len(m) > 0
   715  		return z.rem(stk, x, m)
   716  	}
   717  
   718  	// y > 1
   719  
   720  	if len(m) != 0 {
   721  		// We likely end up being as long as the modulus.
   722  		z = z.make(len(m))
   723  
   724  		// If the exponent is large, we use the Montgomery method for odd values,
   725  		// and a 4-bit, windowed exponentiation for powers of two,
   726  		// and a CRT-decomposed Montgomery method for the remaining values
   727  		// (even values times non-trivial odd values, which decompose into one
   728  		// instance of each of the first two cases).
   729  		if len(y) > 1 && !slow {
   730  			if m[0]&1 == 1 {
   731  				return z.expNNMontgomery(stk, x, y, m)
   732  			}
   733  			if logM, ok := m.isPow2(); ok {
   734  				return z.expNNWindowed(stk, x, y, logM)
   735  			}
   736  			return z.expNNMontgomeryEven(stk, x, y, m)
   737  		}
   738  	}
   739  
   740  	z = z.set(x)
   741  	v := y[len(y)-1] // v > 0 because y is normalized and y > 0
   742  	shift := nlz(v) + 1
   743  	v <<= shift
   744  	var q nat
   745  
   746  	const mask = 1 << (_W - 1)
   747  
   748  	// We walk through the bits of the exponent one by one. Each time we
   749  	// see a bit, we square, thus doubling the power. If the bit is a one,
   750  	// we also multiply by x, thus adding one to the power.
   751  
   752  	w := _W - int(shift)
   753  	// zz and r are used to avoid allocating in mul and div as
   754  	// otherwise the arguments would alias.
   755  	var zz, r nat
   756  	for j := 0; j < w; j++ {
   757  		zz = zz.sqr(stk, z)
   758  		zz, z = z, zz
   759  
   760  		if v&mask != 0 {
   761  			zz = zz.mul(stk, z, x)
   762  			zz, z = z, zz
   763  		}
   764  
   765  		if len(m) != 0 {
   766  			zz, r = zz.div(stk, r, z, m)
   767  			zz, r, q, z = q, z, zz, r
   768  		}
   769  
   770  		v <<= 1
   771  	}
   772  
   773  	for i := len(y) - 2; i >= 0; i-- {
   774  		v = y[i]
   775  
   776  		for j := 0; j < _W; j++ {
   777  			zz = zz.sqr(stk, z)
   778  			zz, z = z, zz
   779  
   780  			if v&mask != 0 {
   781  				zz = zz.mul(stk, z, x)
   782  				zz, z = z, zz
   783  			}
   784  
   785  			if len(m) != 0 {
   786  				zz, r = zz.div(stk, r, z, m)
   787  				zz, r, q, z = q, z, zz, r
   788  			}
   789  
   790  			v <<= 1
   791  		}
   792  	}
   793  
   794  	return z.norm()
   795  }
   796  
   797  // expNNMontgomeryEven calculates x**y mod m where m = m1 × m2 for m1 = 2ⁿ and m2 odd.
   798  // It uses two recursive calls to expNN for x**y mod m1 and x**y mod m2
   799  // and then uses the Chinese Remainder Theorem to combine the results.
   800  // The recursive call using m1 will use expNNWindowed,
   801  // while the recursive call using m2 will use expNNMontgomery.
   802  // For more details, see Ç. K. Koç, “Montgomery Reduction with Even Modulus”,
   803  // IEE Proceedings: Computers and Digital Techniques, 141(5) 314-316, September 1994.
   804  // http://www.people.vcu.edu/~jwang3/CMSC691/j34monex.pdf
   805  func (z nat) expNNMontgomeryEven(stk *stack, x, y, m nat) nat {
   806  	// Split m = m₁ × m₂ where m₁ = 2ⁿ
   807  	n := m.trailingZeroBits()
   808  	m1 := nat(nil).lsh(natOne, n)
   809  	m2 := nat(nil).rsh(m, n)
   810  
   811  	// We want z = x**y mod m.
   812  	// z₁ = x**y mod m1 = (x**y mod m) mod m1 = z mod m1
   813  	// z₂ = x**y mod m2 = (x**y mod m) mod m2 = z mod m2
   814  	// (We are using the math/big convention for names here,
   815  	// where the computation is z = x**y mod m, so its parts are z1 and z2.
   816  	// The paper is computing x = a**e mod n; it refers to these as x2 and z1.)
   817  	z1 := nat(nil).expNN(stk, x, y, m1, false)
   818  	z2 := nat(nil).expNN(stk, x, y, m2, false)
   819  
   820  	// Reconstruct z from z₁, z₂ using CRT, using algorithm from paper,
   821  	// which uses only a single modInverse (and an easy one at that).
   822  	//	p = (z₁ - z₂) × m₂⁻¹ (mod m₁)
   823  	//	z = z₂ + p × m₂
   824  	// The final addition is in range because:
   825  	//	z = z₂ + p × m₂
   826  	//	  ≤ z₂ + (m₁-1) × m₂
   827  	//	  < m₂ + (m₁-1) × m₂
   828  	//	  = m₁ × m₂
   829  	//	  = m.
   830  	z = z.set(z2)
   831  
   832  	// Compute (z₁ - z₂) mod m1 [m1 == 2**n] into z1.
   833  	z1 = z1.subMod2N(z1, z2, n)
   834  
   835  	// Reuse z2 for p = (z₁ - z₂) [in z1] * m2⁻¹ (mod m₁ [= 2ⁿ]).
   836  	m2inv := nat(nil).modInverse(m2, m1)
   837  	z2 = z2.mul(stk, z1, m2inv)
   838  	z2 = z2.trunc(z2, n)
   839  
   840  	// Reuse z1 for p * m2.
   841  	z = z.add(z, z1.mul(stk, z2, m2))
   842  
   843  	return z
   844  }
   845  
   846  // expNNWindowed calculates x**y mod m using a fixed, 4-bit window,
   847  // where m = 2**logM.
   848  func (z nat) expNNWindowed(stk *stack, x, y nat, logM uint) nat {
   849  	if len(y) <= 1 {
   850  		panic("big: misuse of expNNWindowed")
   851  	}
   852  	if x[0]&1 == 0 {
   853  		// len(y) > 1, so y  > logM.
   854  		// x is even, so x**y is a multiple of 2**y which is a multiple of 2**logM.
   855  		return z.setWord(0)
   856  	}
   857  	if logM == 1 {
   858  		return z.setWord(1)
   859  	}
   860  
   861  	// zz is used to avoid allocating in mul as otherwise
   862  	// the arguments would alias.
   863  	defer stk.restore(stk.save())
   864  	w := int((logM + _W - 1) / _W)
   865  	zz := stk.nat(w)
   866  
   867  	const n = 4
   868  	// powers[i] contains x^i.
   869  	var powers [1 << n]nat
   870  	for i := range powers {
   871  		powers[i] = stk.nat(w)
   872  	}
   873  	powers[0] = powers[0].set(natOne)
   874  	powers[1] = powers[1].trunc(x, logM)
   875  	for i := 2; i < 1<<n; i += 2 {
   876  		p2, p, p1 := &powers[i/2], &powers[i], &powers[i+1]
   877  		*p = p.sqr(stk, *p2)
   878  		*p = p.trunc(*p, logM)
   879  		*p1 = p1.mul(stk, *p, x)
   880  		*p1 = p1.trunc(*p1, logM)
   881  	}
   882  
   883  	// Because phi(2**logM) = 2**(logM-1), x**(2**(logM-1)) = 1,
   884  	// so we can compute x**(y mod 2**(logM-1)) instead of x**y.
   885  	// That is, we can throw away all but the bottom logM-1 bits of y.
   886  	// Instead of allocating a new y, we start reading y at the right word
   887  	// and truncate it appropriately at the start of the loop.
   888  	i := len(y) - 1
   889  	mtop := int((logM - 2) / _W) // -2 because the top word of N bits is the (N-1)/W'th word.
   890  	mmask := ^Word(0)
   891  	if mbits := (logM - 1) & (_W - 1); mbits != 0 {
   892  		mmask = (1 << mbits) - 1
   893  	}
   894  	if i > mtop {
   895  		i = mtop
   896  	}
   897  	advance := false
   898  	z = z.setWord(1)
   899  	for ; i >= 0; i-- {
   900  		yi := y[i]
   901  		if i == mtop {
   902  			yi &= mmask
   903  		}
   904  		for j := 0; j < _W; j += n {
   905  			if advance {
   906  				// Account for use of 4 bits in previous iteration.
   907  				// Unrolled loop for significant performance
   908  				// gain. Use go test -bench=".*" in crypto/rsa
   909  				// to check performance before making changes.
   910  				zz = zz.sqr(stk, z)
   911  				zz, z = z, zz
   912  				z = z.trunc(z, logM)
   913  
   914  				zz = zz.sqr(stk, z)
   915  				zz, z = z, zz
   916  				z = z.trunc(z, logM)
   917  
   918  				zz = zz.sqr(stk, z)
   919  				zz, z = z, zz
   920  				z = z.trunc(z, logM)
   921  
   922  				zz = zz.sqr(stk, z)
   923  				zz, z = z, zz
   924  				z = z.trunc(z, logM)
   925  			}
   926  
   927  			zz = zz.mul(stk, z, powers[yi>>(_W-n)])
   928  			zz, z = z, zz
   929  			z = z.trunc(z, logM)
   930  
   931  			yi <<= n
   932  			advance = true
   933  		}
   934  	}
   935  
   936  	return z.norm()
   937  }
   938  
   939  // expNNMontgomery calculates x**y mod m using a fixed, 4-bit window.
   940  // Uses Montgomery representation.
   941  func (z nat) expNNMontgomery(stk *stack, x, y, m nat) nat {
   942  	numWords := len(m)
   943  
   944  	// We want the lengths of x and m to be equal.
   945  	// It is OK if x >= m as long as len(x) == len(m).
   946  	if len(x) > numWords {
   947  		_, x = nat(nil).div(stk, nil, x, m)
   948  		// Note: now len(x) <= numWords, not guaranteed ==.
   949  	}
   950  	if len(x) < numWords {
   951  		rr := make(nat, numWords)
   952  		copy(rr, x)
   953  		x = rr
   954  	}
   955  
   956  	// Ideally the precomputations would be performed outside, and reused
   957  	// k0 = -m**-1 mod 2**_W. Algorithm from: Dumas, J.G. "On Newton–Raphson
   958  	// Iteration for Multiplicative Inverses Modulo Prime Powers".
   959  	k0 := 2 - m[0]
   960  	t := m[0] - 1
   961  	for i := 1; i < _W; i <<= 1 {
   962  		t *= t
   963  		k0 *= (t + 1)
   964  	}
   965  	k0 = -k0
   966  
   967  	// RR = 2**(2*_W*len(m)) mod m
   968  	RR := nat(nil).setWord(1)
   969  	zz := nat(nil).lsh(RR, uint(2*numWords*_W))
   970  	_, RR = nat(nil).div(stk, RR, zz, m)
   971  	if len(RR) < numWords {
   972  		zz = zz.make(numWords)
   973  		copy(zz, RR)
   974  		RR = zz
   975  	}
   976  	// one = 1, with equal length to that of m
   977  	one := make(nat, numWords)
   978  	one[0] = 1
   979  
   980  	const n = 4
   981  	// powers[i] contains x^i
   982  	var powers [1 << n]nat
   983  	powers[0] = powers[0].montgomery(one, RR, m, k0, numWords)
   984  	powers[1] = powers[1].montgomery(x, RR, m, k0, numWords)
   985  	for i := 2; i < 1<<n; i++ {
   986  		powers[i] = powers[i].montgomery(powers[i-1], powers[1], m, k0, numWords)
   987  	}
   988  
   989  	// initialize z = 1 (Montgomery 1)
   990  	z = z.make(numWords)
   991  	copy(z, powers[0])
   992  
   993  	zz = zz.make(numWords)
   994  
   995  	// same windowed exponent, but with Montgomery multiplications
   996  	for i := len(y) - 1; i >= 0; i-- {
   997  		yi := y[i]
   998  		for j := 0; j < _W; j += n {
   999  			if i != len(y)-1 || j != 0 {
  1000  				zz = zz.montgomery(z, z, m, k0, numWords)
  1001  				z = z.montgomery(zz, zz, m, k0, numWords)
  1002  				zz = zz.montgomery(z, z, m, k0, numWords)
  1003  				z = z.montgomery(zz, zz, m, k0, numWords)
  1004  			}
  1005  			zz = zz.montgomery(z, powers[yi>>(_W-n)], m, k0, numWords)
  1006  			z, zz = zz, z
  1007  			yi <<= n
  1008  		}
  1009  	}
  1010  	// convert to regular number
  1011  	zz = zz.montgomery(z, one, m, k0, numWords)
  1012  
  1013  	// One last reduction, just in case.
  1014  	// See golang.org/issue/13907.
  1015  	if zz.cmp(m) >= 0 {
  1016  		// Common case is m has high bit set; in that case,
  1017  		// since zz is the same length as m, there can be just
  1018  		// one multiple of m to remove. Just subtract.
  1019  		// We think that the subtract should be sufficient in general,
  1020  		// so do that unconditionally, but double-check,
  1021  		// in case our beliefs are wrong.
  1022  		// The div is not expected to be reached.
  1023  		zz = zz.sub(zz, m)
  1024  		if zz.cmp(m) >= 0 {
  1025  			_, zz = nat(nil).div(stk, nil, zz, m)
  1026  		}
  1027  	}
  1028  
  1029  	return zz.norm()
  1030  }
  1031  
  1032  // bytes writes the value of z into buf using big-endian encoding.
  1033  // The value of z is encoded in the slice buf[i:]. If the value of z
  1034  // cannot be represented in buf, bytes panics. The number i of unused
  1035  // bytes at the beginning of buf is returned as result.
  1036  func (z nat) bytes(buf []byte) (i int) {
  1037  	// This function is used in cryptographic operations. It must not leak
  1038  	// anything but the Int's sign and bit size through side-channels. Any
  1039  	// changes must be reviewed by a security expert.
  1040  	i = len(buf)
  1041  	for _, d := range z {
  1042  		for j := 0; j < _S; j++ {
  1043  			i--
  1044  			if i >= 0 {
  1045  				buf[i] = byte(d)
  1046  			} else if byte(d) != 0 {
  1047  				panic("math/big: buffer too small to fit value")
  1048  			}
  1049  			d >>= 8
  1050  		}
  1051  	}
  1052  
  1053  	if i < 0 {
  1054  		i = 0
  1055  	}
  1056  	for i < len(buf) && buf[i] == 0 {
  1057  		i++
  1058  	}
  1059  
  1060  	return
  1061  }
  1062  
  1063  // bigEndianWord returns the contents of buf interpreted as a big-endian encoded Word value.
  1064  func bigEndianWord(buf []byte) Word {
  1065  	if _W == 64 {
  1066  		return Word(byteorder.BEUint64(buf))
  1067  	}
  1068  	return Word(byteorder.BEUint32(buf))
  1069  }
  1070  
  1071  // setBytes interprets buf as the bytes of a big-endian unsigned
  1072  // integer, sets z to that value, and returns z.
  1073  func (z nat) setBytes(buf []byte) nat {
  1074  	z = z.make((len(buf) + _S - 1) / _S)
  1075  
  1076  	i := len(buf)
  1077  	for k := 0; i >= _S; k++ {
  1078  		z[k] = bigEndianWord(buf[i-_S : i])
  1079  		i -= _S
  1080  	}
  1081  	if i > 0 {
  1082  		var d Word
  1083  		for s := uint(0); i > 0; s += 8 {
  1084  			d |= Word(buf[i-1]) << s
  1085  			i--
  1086  		}
  1087  		z[len(z)-1] = d
  1088  	}
  1089  
  1090  	return z.norm()
  1091  }
  1092  
  1093  // sqrt sets z = ⌊√x⌋
  1094  // The caller may pass stk == nil to request that sqrt obtain and release one itself.
  1095  func (z nat) sqrt(stk *stack, x nat) nat {
  1096  	if x.cmp(natOne) <= 0 {
  1097  		return z.set(x)
  1098  	}
  1099  	if alias(z, x) {
  1100  		z = nil
  1101  	}
  1102  
  1103  	if stk == nil {
  1104  		stk = getStack()
  1105  		defer stk.free()
  1106  	}
  1107  
  1108  	// Start with value known to be too large and repeat "z = ⌊(z + ⌊x/z⌋)/2⌋" until it stops getting smaller.
  1109  	// See Brent and Zimmermann, Modern Computer Arithmetic, Algorithm 1.13 (SqrtInt).
  1110  	// https://members.loria.fr/PZimmermann/mca/pub226.html
  1111  	// If x is one less than a perfect square, the sequence oscillates between the correct z and z+1;
  1112  	// otherwise it converges to the correct z and stays there.
  1113  	var z1, z2 nat
  1114  	z1 = z
  1115  	z1 = z1.setUint64(1)
  1116  	z1 = z1.lsh(z1, uint(x.bitLen()+1)/2) // must be ≥ √x
  1117  	for n := 0; ; n++ {
  1118  		z2, _ = z2.div(stk, nil, x, z1)
  1119  		z2 = z2.add(z2, z1)
  1120  		z2 = z2.rsh(z2, 1)
  1121  		if z2.cmp(z1) >= 0 {
  1122  			// z1 is answer.
  1123  			// Figure out whether z1 or z2 is currently aliased to z by looking at loop count.
  1124  			if n&1 == 0 {
  1125  				return z1
  1126  			}
  1127  			return z.set(z1)
  1128  		}
  1129  		z1, z2 = z2, z1
  1130  	}
  1131  }
  1132  
  1133  // subMod2N returns z = (x - y) mod 2ⁿ.
  1134  func (z nat) subMod2N(x, y nat, n uint) nat {
  1135  	if uint(x.bitLen()) > n {
  1136  		if alias(z, x) {
  1137  			// ok to overwrite x in place
  1138  			x = x.trunc(x, n)
  1139  		} else {
  1140  			x = nat(nil).trunc(x, n)
  1141  		}
  1142  	}
  1143  	if uint(y.bitLen()) > n {
  1144  		if alias(z, y) {
  1145  			// ok to overwrite y in place
  1146  			y = y.trunc(y, n)
  1147  		} else {
  1148  			y = nat(nil).trunc(y, n)
  1149  		}
  1150  	}
  1151  	if x.cmp(y) >= 0 {
  1152  		return z.sub(x, y)
  1153  	}
  1154  	// x - y < 0; x - y mod 2ⁿ = x - y + 2ⁿ = 2ⁿ - (y - x) = 1 + 2ⁿ-1 - (y - x) = 1 + ^(y - x).
  1155  	z = z.sub(y, x)
  1156  	for uint(len(z))*_W < n {
  1157  		z = append(z, 0)
  1158  	}
  1159  	for i := range z {
  1160  		z[i] = ^z[i]
  1161  	}
  1162  	z = z.trunc(z, n)
  1163  	return z.add(z, natOne)
  1164  }
  1165  

View as plain text