bn_fast_s_mp_sqr.c 2.85 KB
Newer Older
1
#include "tommath_private.h"
2 3 4 5 6 7 8 9 10 11
#ifdef BN_FAST_S_MP_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
 *
 * LibTomMath is a library that provides multiple-precision
 * integer arithmetic as well as number theoretic functionality.
 *
 * The library was designed directly after the MPI library by
 * Michael Fromberger but has been written from scratch with
 * additional optimizations in place.
 *
12
 * SPDX-License-Identifier: Unlicense
13 14 15
 */

/* the jist of squaring...
16 17
 * you do like mult except the offset of the tmpx [one that
 * starts closer to zero] can't equal the offset of tmpy.
18
 * So basically you set up iy like before then you min it with
19
 * (ty-tx) so that it never happens.  You double all those
20 21 22 23 24
 * you add in the inner loop

After that loop you do the squares and add them in.
*/

25
int fast_s_mp_sqr(const mp_int *a, mp_int *b)
26
{
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
   int       olduse, res, pa, ix, iz;
   mp_digit   W[MP_WARRAY], *tmpx;
   mp_word   W1;

   /* grow the destination as required */
   pa = a->used + a->used;
   if (b->alloc < pa) {
      if ((res = mp_grow(b, pa)) != MP_OKAY) {
         return res;
      }
   }

   /* number of output digits to produce */
   W1 = 0;
   for (ix = 0; ix < pa; ix++) {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      int      tx, ty, iy;
      mp_word  _W;
      mp_digit *tmpy;

      /* clear counter */
      _W = 0;

      /* get offsets into the two bignums */
      ty = MIN(a->used-1, ix);
      tx = ix - ty;

      /* setup temp aliases */
      tmpx = a->dp + tx;
      tmpy = a->dp + ty;

      /* this is the number of times the loop will iterrate, essentially
         while (tx++ < a->used && ty-- >= 0) { ... }
       */
      iy = MIN(a->used-tx, ty+1);

62
      /* now for squaring tx can never equal ty
63 64 65
       * we halve the distance since they approach at a rate of 2x
       * and we have to round because odd cases need to be executed
       */
66
      iy = MIN(iy, ((ty-tx)+1)>>1);
67 68 69

      /* execute loop */
      for (iz = 0; iz < iy; iz++) {
70
         _W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
71 72 73 74 75 76
      }

      /* double the inner product and add carry */
      _W = _W + _W + W1;

      /* even columns have the square term in them */
77 78
      if (((unsigned)ix & 1u) == 0u) {
         _W += (mp_word)a->dp[ix>>1] * (mp_word)a->dp[ix>>1];
79 80 81
      }

      /* store it */
82
      W[ix] = _W & MP_MASK;
83 84

      /* make next carry */
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
      W1 = _W >> (mp_word)DIGIT_BIT;
   }

   /* setup dest */
   olduse  = b->used;
   b->used = a->used+a->used;

   {
      mp_digit *tmpb;
      tmpb = b->dp;
      for (ix = 0; ix < pa; ix++) {
         *tmpb++ = W[ix] & MP_MASK;
      }

      /* clear unused digits [that existed in the old copy of c] */
      for (; ix < olduse; ix++) {
         *tmpb++ = 0;
      }
   }
   mp_clamp(b);
   return MP_OKAY;
106 107 108
}
#endif

109 110 111
/* ref:         HEAD -> master, tag: v1.1.0 */
/* git commit:  08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
/* commit time: 2019-01-28 20:32:32 +0100 */