142
|
1 #include <tommath.h> |
|
2 #ifdef BN_S_MP_SQR_C |
2
|
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis |
|
4 * |
|
5 * LibTomMath is a library that provides multiple-precision |
|
6 * integer arithmetic as well as number theoretic functionality. |
|
7 * |
|
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. |
|
11 * |
|
12 * The library is free for all purposes without any express |
|
13 * guarantee it works. |
|
14 * |
|
15 * Tom St Denis, [email protected], http://math.libtomcrypt.org |
|
16 */ |
|
17 |
|
18 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */ |
|
19 int |
|
20 s_mp_sqr (mp_int * a, mp_int * b) |
|
21 { |
|
22 mp_int t; |
|
23 int res, ix, iy, pa; |
|
24 mp_word r; |
|
25 mp_digit u, tmpx, *tmpt; |
|
26 |
|
27 pa = a->used; |
|
28 if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) { |
|
29 return res; |
|
30 } |
|
31 |
|
32 /* default used is maximum possible size */ |
|
33 t.used = 2*pa + 1; |
|
34 |
|
35 for (ix = 0; ix < pa; ix++) { |
|
36 /* first calculate the digit at 2*ix */ |
|
37 /* calculate double precision result */ |
|
38 r = ((mp_word) t.dp[2*ix]) + |
|
39 ((mp_word)a->dp[ix])*((mp_word)a->dp[ix]); |
|
40 |
|
41 /* store lower part in result */ |
|
42 t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK)); |
|
43 |
|
44 /* get the carry */ |
|
45 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); |
|
46 |
|
47 /* left hand side of A[ix] * A[iy] */ |
|
48 tmpx = a->dp[ix]; |
|
49 |
|
50 /* alias for where to store the results */ |
|
51 tmpt = t.dp + (2*ix + 1); |
|
52 |
|
53 for (iy = ix + 1; iy < pa; iy++) { |
|
54 /* first calculate the product */ |
|
55 r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]); |
|
56 |
|
57 /* now calculate the double precision result, note we use |
|
58 * addition instead of *2 since it's easier to optimize |
|
59 */ |
|
60 r = ((mp_word) *tmpt) + r + r + ((mp_word) u); |
|
61 |
|
62 /* store lower part */ |
|
63 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
|
64 |
|
65 /* get carry */ |
|
66 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); |
|
67 } |
|
68 /* propagate upwards */ |
|
69 while (u != ((mp_digit) 0)) { |
|
70 r = ((mp_word) *tmpt) + ((mp_word) u); |
|
71 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); |
|
72 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); |
|
73 } |
|
74 } |
|
75 |
|
76 mp_clamp (&t); |
|
77 mp_exch (&t, b); |
|
78 mp_clear (&t); |
|
79 return MP_OKAY; |
|
80 } |
142
|
81 #endif |