Mercurial > dropbear
comparison bn_s_mp_sqr.c @ 2:86e0b50a9b58 libtommath-orig ltm-0.30-orig
ltm 0.30 orig import
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 31 May 2004 18:25:22 +0000 |
parents | |
children | d29b64170cf0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 2:86e0b50a9b58 |
---|---|
1 /* LibTomMath, multiple-precision integer library -- Tom St Denis | |
2 * | |
3 * LibTomMath is a library that provides multiple-precision | |
4 * integer arithmetic as well as number theoretic functionality. | |
5 * | |
6 * The library was designed directly after the MPI library by | |
7 * Michael Fromberger but has been written from scratch with | |
8 * additional optimizations in place. | |
9 * | |
10 * The library is free for all purposes without any express | |
11 * guarantee it works. | |
12 * | |
13 * Tom St Denis, [email protected], http://math.libtomcrypt.org | |
14 */ | |
15 #include <tommath.h> | |
16 | |
17 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */ | |
18 int | |
19 s_mp_sqr (mp_int * a, mp_int * b) | |
20 { | |
21 mp_int t; | |
22 int res, ix, iy, pa; | |
23 mp_word r; | |
24 mp_digit u, tmpx, *tmpt; | |
25 | |
26 pa = a->used; | |
27 if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) { | |
28 return res; | |
29 } | |
30 | |
31 /* default used is maximum possible size */ | |
32 t.used = 2*pa + 1; | |
33 | |
34 for (ix = 0; ix < pa; ix++) { | |
35 /* first calculate the digit at 2*ix */ | |
36 /* calculate double precision result */ | |
37 r = ((mp_word) t.dp[2*ix]) + | |
38 ((mp_word)a->dp[ix])*((mp_word)a->dp[ix]); | |
39 | |
40 /* store lower part in result */ | |
41 t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK)); | |
42 | |
43 /* get the carry */ | |
44 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); | |
45 | |
46 /* left hand side of A[ix] * A[iy] */ | |
47 tmpx = a->dp[ix]; | |
48 | |
49 /* alias for where to store the results */ | |
50 tmpt = t.dp + (2*ix + 1); | |
51 | |
52 for (iy = ix + 1; iy < pa; iy++) { | |
53 /* first calculate the product */ | |
54 r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]); | |
55 | |
56 /* now calculate the double precision result, note we use | |
57 * addition instead of *2 since it's easier to optimize | |
58 */ | |
59 r = ((mp_word) *tmpt) + r + r + ((mp_word) u); | |
60 | |
61 /* store lower part */ | |
62 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); | |
63 | |
64 /* get carry */ | |
65 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); | |
66 } | |
67 /* propagate upwards */ | |
68 while (u != ((mp_digit) 0)) { | |
69 r = ((mp_word) *tmpt) + ((mp_word) u); | |
70 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); | |
71 u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); | |
72 } | |
73 } | |
74 | |
75 mp_clamp (&t); | |
76 mp_exch (&t, b); | |
77 mp_clear (&t); | |
78 return MP_OKAY; | |
79 } |