Skip to content
Newer
Older
100644 85 lines (70 sloc) 2.25 KB
e549ccf added libtommath-0.32
Tom St Denis authored Oct 29, 2004
1 #include <tommath.h>
2 #ifdef BN_S_MP_SQR_C
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 *
c1da6aa added libtommath-0.25
Tom St Denis authored Aug 5, 2003
5 * LibTomMath is a library that provides multiple-precision
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
6 * integer arithmetic as well as number theoretic functionality.
7 *
c1da6aa added libtommath-0.25
Tom St Denis authored Aug 5, 2003
8 * The library was designed directly after the MPI library by
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
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 *
333aebc added libtommath-0.41
Tom St Denis authored Apr 18, 2007
15 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
16 */
17
18 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
fdfa2f4 added libtommath-0.35
Tom St Denis authored Mar 12, 2005
19 int s_mp_sqr (mp_int * a, mp_int * b)
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
20 {
b66471f added libtommath-0.13
Tom St Denis authored Feb 28, 2003
21 mp_int t;
22 int res, ix, iy, pa;
0ef44ce added libtommath-0.18
Tom St Denis authored May 29, 2003
23 mp_word r;
24 mp_digit u, tmpx, *tmpt;
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
25
26 pa = a->used;
ef490f3 added libtommath-0.19
Tom St Denis authored Jun 6, 2003
27 if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) {
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
28 return res;
350578d added libtommath-0.30
Tom St Denis authored Apr 11, 2004
29 }
30
455bb4d added libtommath-0.28
Tom St Denis authored Dec 24, 2003
31 /* default used is maximum possible size */
ef490f3 added libtommath-0.19
Tom St Denis authored Jun 6, 2003
32 t.used = 2*pa + 1;
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
33
34 for (ix = 0; ix < pa; ix++) {
35 /* first calculate the digit at 2*ix */
36 /* calculate double precision result */
eed6765 added libtommath-0.23
Tom St Denis authored Jul 12, 2003
37 r = ((mp_word) t.dp[2*ix]) +
38 ((mp_word)a->dp[ix])*((mp_word)a->dp[ix]);
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
39
40 /* store lower part in result */
455bb4d added libtommath-0.28
Tom St Denis authored Dec 24, 2003
41 t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK));
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
42
43 /* get the carry */
455bb4d added libtommath-0.28
Tom St Denis authored Dec 24, 2003
44 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
45
46 /* left hand side of A[ix] * A[iy] */
455bb4d added libtommath-0.28
Tom St Denis authored Dec 24, 2003
47 tmpx = a->dp[ix];
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
48
49 /* alias for where to store the results */
455bb4d added libtommath-0.28
Tom St Denis authored Dec 24, 2003
50 tmpt = t.dp + (2*ix + 1);
0ef44ce added libtommath-0.18
Tom St Denis authored May 29, 2003
51
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
52 for (iy = ix + 1; iy < pa; iy++) {
53 /* first calculate the product */
455bb4d added libtommath-0.28
Tom St Denis authored Dec 24, 2003
54 r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]);
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
55
56 /* now calculate the double precision result, note we use
ef490f3 added libtommath-0.19
Tom St Denis authored Jun 6, 2003
57 * addition instead of *2 since it's easier to optimize
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
58 */
6e73234 added libtommath-0.26
Tom St Denis authored Aug 29, 2003
59 r = ((mp_word) *tmpt) + r + r + ((mp_word) u);
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
60
61 /* store lower part */
62 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
63
64 /* get carry */
6e73234 added libtommath-0.26
Tom St Denis authored Aug 29, 2003
65 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
66 }
67 /* propagate upwards */
0ef44ce added libtommath-0.18
Tom St Denis authored May 29, 2003
68 while (u != ((mp_digit) 0)) {
6e73234 added libtommath-0.26
Tom St Denis authored Aug 29, 2003
69 r = ((mp_word) *tmpt) + ((mp_word) u);
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
70 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
6e73234 added libtommath-0.26
Tom St Denis authored Aug 29, 2003
71 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
57354e1 added libtommath-0.12
Tom St Denis authored Feb 28, 2003
72 }
73 }
74
75 mp_clamp (&t);
76 mp_exch (&t, b);
77 mp_clear (&t);
78 return MP_OKAY;
79 }
e549ccf added libtommath-0.32
Tom St Denis authored Oct 29, 2004
80 #endif
9294e79 added libtommath-0.36
Tom St Denis authored Aug 1, 2005
81
82 /* $Source$ */
83 /* $Revision$ */
84 /* $Date$ */
Something went wrong with that request. Please try again.