comparison libtommath/bn_s_mp_sqr.c @ 1436:60fc6476e044

Update to libtommath v1.0
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 22:37:14 +0800
parents 5ff8218bcee9
children 8bba51a55704
comparison
equal deleted inserted replaced
1435:f849a5ca2efc 1436:60fc6476e044
1 #include <tommath.h> 1 #include <tommath_private.h>
2 #ifdef BN_S_MP_SQR_C 2 #ifdef BN_S_MP_SQR_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 * 4 *
5 * LibTomMath is a library that provides multiple-precision 5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality. 6 * integer arithmetic as well as number theoretic functionality.
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * The library is free for all purposes without any express
13 * guarantee it works. 13 * guarantee it works.
14 * 14 *
15 * Tom St Denis, [email protected], http://math.libtomcrypt.com 15 * Tom St Denis, [email protected], http://libtom.org
16 */ 16 */
17 17
18 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */ 18 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
19 int s_mp_sqr (mp_int * a, mp_int * b) 19 int s_mp_sqr (mp_int * a, mp_int * b)
20 { 20 {
22 int res, ix, iy, pa; 22 int res, ix, iy, pa;
23 mp_word r; 23 mp_word r;
24 mp_digit u, tmpx, *tmpt; 24 mp_digit u, tmpx, *tmpt;
25 25
26 pa = a->used; 26 pa = a->used;
27 if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) { 27 if ((res = mp_init_size (&t, (2 * pa) + 1)) != MP_OKAY) {
28 return res; 28 return res;
29 } 29 }
30 30
31 /* default used is maximum possible size */ 31 /* default used is maximum possible size */
32 t.used = 2*pa + 1; 32 t.used = (2 * pa) + 1;
33 33
34 for (ix = 0; ix < pa; ix++) { 34 for (ix = 0; ix < pa; ix++) {
35 /* first calculate the digit at 2*ix */ 35 /* first calculate the digit at 2*ix */
36 /* calculate double precision result */ 36 /* calculate double precision result */
37 r = ((mp_word) t.dp[2*ix]) + 37 r = (mp_word)t.dp[2*ix] +
38 ((mp_word)a->dp[ix])*((mp_word)a->dp[ix]); 38 ((mp_word)a->dp[ix] * (mp_word)a->dp[ix]);
39 39
40 /* store lower part in result */ 40 /* store lower part in result */
41 t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK)); 41 t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK));
42 42
43 /* get the carry */ 43 /* get the carry */
45 45
46 /* left hand side of A[ix] * A[iy] */ 46 /* left hand side of A[ix] * A[iy] */
47 tmpx = a->dp[ix]; 47 tmpx = a->dp[ix];
48 48
49 /* alias for where to store the results */ 49 /* alias for where to store the results */
50 tmpt = t.dp + (2*ix + 1); 50 tmpt = t.dp + ((2 * ix) + 1);
51 51
52 for (iy = ix + 1; iy < pa; iy++) { 52 for (iy = ix + 1; iy < pa; iy++) {
53 /* first calculate the product */ 53 /* first calculate the product */
54 r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]); 54 r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]);
55 55
77 mp_clear (&t); 77 mp_clear (&t);
78 return MP_OKAY; 78 return MP_OKAY;
79 } 79 }
80 #endif 80 #endif
81 81
82 /* $Source: /cvs/libtom/libtommath/bn_s_mp_sqr.c,v $ */ 82 /* $Source$ */
83 /* $Revision: 1.3 $ */ 83 /* $Revision$ */
84 /* $Date: 2006/03/31 14:18:44 $ */ 84 /* $Date$ */