tommath/bn_s_mp_sqr.c

78 lines
2.1 KiB
C
Raw Normal View History

2003-02-28 11:08:34 -05:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is library that provides for multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library is designed directly after the MPI library by
* 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.
*
2003-03-12 21:11:11 -05:00
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
2003-02-28 11:08:34 -05:00
*/
#include <tommath.h>
/* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
int
s_mp_sqr (mp_int * a, mp_int * b)
{
2003-02-28 11:09:08 -05:00
mp_int t;
int res, ix, iy, pa;
2003-05-29 09:35:26 -04:00
mp_word r;
mp_digit u, tmpx, *tmpt;
2003-02-28 11:08:34 -05:00
pa = a->used;
if ((res = mp_init_size (&t, pa + pa + 1)) != MP_OKAY) {
return res;
}
t.used = pa + pa + 1;
for (ix = 0; ix < pa; ix++) {
/* first calculate the digit at 2*ix */
/* calculate double precision result */
2003-05-29 09:35:26 -04:00
r = ((mp_word) t.dp[ix + ix]) +
((mp_word) a->dp[ix]) * ((mp_word) a->dp[ix]);
2003-02-28 11:08:34 -05:00
/* store lower part in result */
t.dp[ix + ix] = (mp_digit) (r & ((mp_word) MP_MASK));
/* get the carry */
u = (r >> ((mp_word) DIGIT_BIT));
/* left hand side of A[ix] * A[iy] */
tmpx = a->dp[ix];
/* alias for where to store the results */
2003-05-29 09:35:26 -04:00
tmpt = t.dp + (ix + ix + 1);
2003-02-28 11:08:34 -05:00
for (iy = ix + 1; iy < pa; iy++) {
/* first calculate the product */
r = ((mp_word) tmpx) * ((mp_word) a->dp[iy]);
/* now calculate the double precision result, note we use
* addition instead of *2 since its easier to optimize
*/
r = ((mp_word) * tmpt) + r + r + ((mp_word) u);
/* store lower part */
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
/* get carry */
u = (r >> ((mp_word) DIGIT_BIT));
}
/* propagate upwards */
2003-05-29 09:35:26 -04:00
while (u != ((mp_digit) 0)) {
r = ((mp_word) * tmpt) + ((mp_word) u);
2003-02-28 11:08:34 -05:00
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
u = (r >> ((mp_word) DIGIT_BIT));
}
}
mp_clamp (&t);
mp_exch (&t, b);
mp_clear (&t);
return MP_OKAY;
}