2
#ifdef BN_FAST_S_MP_SQR_C
3
/* LibTomMath, multiple-precision integer library -- Tom St Denis
5
* LibTomMath is a library that provides multiple-precision
6
* integer arithmetic as well as number theoretic functionality.
8
* The library was designed directly after the MPI library by
9
* Michael Fromberger but has been written from scratch with
10
* additional optimizations in place.
12
* The library is free for all purposes without any express
15
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
18
/* the jist of squaring...
19
* you do like mult except the offset of the tmpx [one that
20
* starts closer to zero] can't equal the offset of tmpy.
21
* So basically you set up iy like before then you min it with
22
* (ty-tx) so that it never happens. You double all those
23
* you add in the inner loop
25
After that loop you do the squares and add them in.
28
int fast_s_mp_sqr (mp_int * a, mp_int * b)
30
int olduse, res, pa, ix, iz;
31
mp_digit W[MP_WARRAY], *tmpx;
34
/* grow the destination as required */
35
pa = a->used + a->used;
37
if ((res = mp_grow (b, pa)) != MP_OKAY) {
42
/* number of output digits to produce */
44
for (ix = 0; ix < pa; ix++) {
52
/* get offsets into the two bignums */
53
ty = MIN(a->used-1, ix);
56
/* setup temp aliases */
60
/* this is the number of times the loop will iterrate, essentially
61
while (tx++ < a->used && ty-- >= 0) { ... }
63
iy = MIN(a->used-tx, ty+1);
65
/* now for squaring tx can never equal ty
66
* we halve the distance since they approach at a rate of 2x
67
* and we have to round because odd cases need to be executed
69
iy = MIN(iy, (ty-tx+1)>>1);
72
for (iz = 0; iz < iy; iz++) {
73
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
76
/* double the inner product and add carry */
79
/* even columns have the square term in them */
81
_W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]);
85
W[ix] = (mp_digit)(_W & MP_MASK);
88
W1 = _W >> ((mp_word)DIGIT_BIT);
93
b->used = a->used+a->used;
98
for (ix = 0; ix < pa; ix++) {
99
*tmpb++ = W[ix] & MP_MASK;
102
/* clear unused digits [that existed in the old copy of c] */
103
for (; ix < olduse; ix++) {
112
/* $Source: /cvs/libtom/libtommath/bn_fast_s_mp_sqr.c,v $ */
113
/* $Revision: 1.4 $ */
114
/* $Date: 2006/12/28 01:25:13 $ */