2015-11-11 19:49:07 -05:00
|
|
|
#include <tommath_private.h>
|
2004-10-29 18:07:18 -04:00
|
|
|
#ifdef BN_FAST_S_MP_SQR_C
|
2003-02-28 11:08:34 -05:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
|
|
|
*
|
2003-08-04 21:24:44 -04:00
|
|
|
* LibTomMath is a library that provides multiple-precision
|
2003-02-28 11:08:34 -05:00
|
|
|
* integer arithmetic as well as number theoretic functionality.
|
|
|
|
*
|
2003-08-04 21:24:44 -04:00
|
|
|
* The library was designed directly after the MPI library by
|
2003-02-28 11:08:34 -05:00
|
|
|
* Michael Fromberger but has been written from scratch with
|
|
|
|
* additional optimizations in place.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
|
|
|
* guarantee it works.
|
|
|
|
*
|
2015-10-30 17:55:29 -04:00
|
|
|
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
2003-02-28 11:08:34 -05:00
|
|
|
*/
|
|
|
|
|
2004-10-29 18:07:18 -04:00
|
|
|
/* the jist of squaring...
|
2017-08-29 23:51:11 -04:00
|
|
|
* you do like mult except the offset of the tmpx [one that
|
|
|
|
* starts closer to zero] can't equal the offset of tmpy.
|
2005-03-12 06:55:11 -05:00
|
|
|
* So basically you set up iy like before then you min it with
|
2017-08-29 23:51:11 -04:00
|
|
|
* (ty-tx) so that it never happens. You double all those
|
2005-03-12 06:55:11 -05:00
|
|
|
* you add in the inner loop
|
2004-10-29 18:07:18 -04:00
|
|
|
|
|
|
|
After that loop you do the squares and add them in.
|
|
|
|
*/
|
|
|
|
|
2004-04-11 16:46:22 -04:00
|
|
|
int fast_s_mp_sqr (mp_int * a, mp_int * b)
|
2003-02-28 11:08:34 -05:00
|
|
|
{
|
2004-10-29 18:07:18 -04:00
|
|
|
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) {
|
2017-08-30 14:08:58 -04:00
|
|
|
if ((res = mp_grow(b, pa)) != MP_OKAY) {
|
2003-02-28 11:08:34 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-29 18:07:18 -04:00
|
|
|
/* number of output digits to produce */
|
|
|
|
W1 = 0;
|
2017-08-29 23:51:11 -04:00
|
|
|
for (ix = 0; ix < pa; ix++) {
|
2004-10-29 18:07:18 -04:00
|
|
|
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;
|
|
|
|
|
2005-03-12 06:55:11 -05:00
|
|
|
/* this is the number of times the loop will iterrate, essentially
|
2004-10-29 18:07:18 -04:00
|
|
|
while (tx++ < a->used && ty-- >= 0) { ... }
|
|
|
|
*/
|
|
|
|
iy = MIN(a->used-tx, ty+1);
|
|
|
|
|
2017-08-29 23:51:11 -04:00
|
|
|
/* now for squaring tx can never equal ty
|
2004-10-29 18:07:18 -04:00
|
|
|
* we halve the distance since they approach at a rate of 2x
|
|
|
|
* and we have to round because odd cases need to be executed
|
|
|
|
*/
|
2015-10-17 12:28:05 -04:00
|
|
|
iy = MIN(iy, ((ty-tx)+1)>>1);
|
2004-10-29 18:07:18 -04:00
|
|
|
|
|
|
|
/* execute loop */
|
|
|
|
for (iz = 0; iz < iy; iz++) {
|
|
|
|
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
|
2003-02-28 11:08:34 -05:00
|
|
|
}
|
2004-10-29 18:07:18 -04:00
|
|
|
|
|
|
|
/* double the inner product and add carry */
|
|
|
|
_W = _W + _W + W1;
|
|
|
|
|
|
|
|
/* even columns have the square term in them */
|
|
|
|
if ((ix&1) == 0) {
|
|
|
|
_W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store it */
|
2005-03-12 06:55:11 -05:00
|
|
|
W[ix] = (mp_digit)(_W & MP_MASK);
|
2004-10-29 18:07:18 -04:00
|
|
|
|
|
|
|
/* make next carry */
|
|
|
|
W1 = _W >> ((mp_word)DIGIT_BIT);
|
2003-02-28 11:08:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* setup dest */
|
2003-05-29 09:35:26 -04:00
|
|
|
olduse = b->used;
|
2004-10-29 18:07:18 -04:00
|
|
|
b->used = a->used+a->used;
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2004-10-29 18:07:18 -04:00
|
|
|
{
|
|
|
|
mp_digit *tmpb;
|
2003-05-29 09:35:26 -04:00
|
|
|
tmpb = b->dp;
|
2004-10-29 18:07:18 -04:00
|
|
|
for (ix = 0; ix < pa; ix++) {
|
|
|
|
*tmpb++ = W[ix] & MP_MASK;
|
2003-02-28 11:09:08 -05:00
|
|
|
}
|
|
|
|
|
2004-10-29 18:07:18 -04:00
|
|
|
/* clear unused digits [that existed in the old copy of c] */
|
2003-02-28 11:09:08 -05:00
|
|
|
for (; ix < olduse; ix++) {
|
|
|
|
*tmpb++ = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-08-30 14:08:58 -04:00
|
|
|
mp_clamp(b);
|
2003-02-28 11:08:34 -05:00
|
|
|
return MP_OKAY;
|
|
|
|
}
|
2004-10-29 18:07:18 -04:00
|
|
|
#endif
|
2005-08-01 12:37:28 -04:00
|
|
|
|
2017-08-28 10:27:26 -04:00
|
|
|
/* ref: $Format:%D$ */
|
|
|
|
/* git commit: $Format:%H$ */
|
|
|
|
/* commit time: $Format:%ai$ */
|