tommath/bn_s_mp_sqr.c

85 lines
2.2 KiB
C
Raw Normal View History

2004-10-29 18:07:18 -04:00
#include <tommath.h>
#ifdef BN_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.
*
2007-04-18 05:58:18 -04:00
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
2003-02-28 11:08:34 -05:00
*/
/* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
2005-03-12 06:55:11 -05:00
int s_mp_sqr (mp_int * a, mp_int * b)
2003-02-28 11:08:34 -05:00
{
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;
2003-06-06 15:35:48 -04:00
if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) {
2003-02-28 11:08:34 -05:00
return res;
2004-04-11 16:46:22 -04:00
}
2003-12-24 13:59:22 -05:00
/* default used is maximum possible size */
2003-06-06 15:35:48 -04:00
t.used = 2*pa + 1;
2003-02-28 11:08:34 -05:00
for (ix = 0; ix < pa; ix++) {
/* first calculate the digit at 2*ix */
/* calculate double precision result */
2003-07-12 10:31:43 -04:00
r = ((mp_word) t.dp[2*ix]) +
((mp_word)a->dp[ix])*((mp_word)a->dp[ix]);
2003-02-28 11:08:34 -05:00
/* store lower part in result */
2003-12-24 13:59:22 -05:00
t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK));
2003-02-28 11:08:34 -05:00
/* get the carry */
2003-12-24 13:59:22 -05:00
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
2003-02-28 11:08:34 -05:00
/* left hand side of A[ix] * A[iy] */
2003-12-24 13:59:22 -05:00
tmpx = a->dp[ix];
2003-02-28 11:08:34 -05:00
/* alias for where to store the results */
2003-12-24 13:59:22 -05:00
tmpt = t.dp + (2*ix + 1);
2003-05-29 09:35:26 -04:00
2003-02-28 11:08:34 -05:00
for (iy = ix + 1; iy < pa; iy++) {
/* first calculate the product */
2003-12-24 13:59:22 -05:00
r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]);
2003-02-28 11:08:34 -05:00
/* now calculate the double precision result, note we use
2003-06-06 15:35:48 -04:00
* addition instead of *2 since it's easier to optimize
2003-02-28 11:08:34 -05:00
*/
2003-08-29 10:06:56 -04:00
r = ((mp_word) *tmpt) + r + r + ((mp_word) u);
2003-02-28 11:08:34 -05:00
/* store lower part */
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
/* get carry */
2003-08-29 10:06:56 -04:00
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
2003-02-28 11:08:34 -05:00
}
/* propagate upwards */
2003-05-29 09:35:26 -04:00
while (u != ((mp_digit) 0)) {
2003-08-29 10:06:56 -04:00
r = ((mp_word) *tmpt) + ((mp_word) u);
2003-02-28 11:08:34 -05:00
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
2003-08-29 10:06:56 -04:00
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
2003-02-28 11:08:34 -05:00
}
}
mp_clamp (&t);
mp_exch (&t, b);
mp_clear (&t);
return MP_OKAY;
}
2004-10-29 18:07:18 -04:00
#endif
2005-08-01 12:37:28 -04:00
/* $Source$ */
/* $Revision$ */
/* $Date$ */