comparison libtommath/bn_s_mp_sqr.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents f52919ffd3b1
children
comparison
equal deleted inserted replaced
1691:2d3745d58843 1692:1051e4eea25a
1 #include "tommath_private.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 /* SPDX-License-Identifier: Unlicense */
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 * SPDX-License-Identifier: Unlicense
13 */
14 5
15 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */ 6 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
16 int s_mp_sqr(const mp_int *a, mp_int *b) 7 mp_err s_mp_sqr(const mp_int *a, mp_int *b)
17 { 8 {
18 mp_int t; 9 mp_int t;
19 int res, ix, iy, pa; 10 int ix, iy, pa;
20 mp_word r; 11 mp_err err;
12 mp_word r;
21 mp_digit u, tmpx, *tmpt; 13 mp_digit u, tmpx, *tmpt;
22 14
23 pa = a->used; 15 pa = a->used;
24 if ((res = mp_init_size(&t, (2 * pa) + 1)) != MP_OKAY) { 16 if ((err = mp_init_size(&t, (2 * pa) + 1)) != MP_OKAY) {
25 return res; 17 return err;
26 } 18 }
27 19
28 /* default used is maximum possible size */ 20 /* default used is maximum possible size */
29 t.used = (2 * pa) + 1; 21 t.used = (2 * pa) + 1;
30 22
36 28
37 /* store lower part in result */ 29 /* store lower part in result */
38 t.dp[ix+ix] = (mp_digit)(r & (mp_word)MP_MASK); 30 t.dp[ix+ix] = (mp_digit)(r & (mp_word)MP_MASK);
39 31
40 /* get the carry */ 32 /* get the carry */
41 u = (mp_digit)(r >> (mp_word)DIGIT_BIT); 33 u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
42 34
43 /* left hand side of A[ix] * A[iy] */ 35 /* left hand side of A[ix] * A[iy] */
44 tmpx = a->dp[ix]; 36 tmpx = a->dp[ix];
45 37
46 /* alias for where to store the results */ 38 /* alias for where to store the results */
57 49
58 /* store lower part */ 50 /* store lower part */
59 *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK); 51 *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
60 52
61 /* get carry */ 53 /* get carry */
62 u = (mp_digit)(r >> (mp_word)DIGIT_BIT); 54 u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
63 } 55 }
64 /* propagate upwards */ 56 /* propagate upwards */
65 while (u != 0uL) { 57 while (u != 0uL) {
66 r = (mp_word)*tmpt + (mp_word)u; 58 r = (mp_word)*tmpt + (mp_word)u;
67 *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK); 59 *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
68 u = (mp_digit)(r >> (mp_word)DIGIT_BIT); 60 u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
69 } 61 }
70 } 62 }
71 63
72 mp_clamp(&t); 64 mp_clamp(&t);
73 mp_exch(&t, b); 65 mp_exch(&t, b);
74 mp_clear(&t); 66 mp_clear(&t);
75 return MP_OKAY; 67 return MP_OKAY;
76 } 68 }
77 #endif 69 #endif
78
79 /* ref: HEAD -> master, tag: v1.1.0 */
80 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
81 /* commit time: 2019-01-28 20:32:32 +0100 */