comparison libtommath/bn_s_mp_mul_high_digs.c @ 1655:f52919ffd3b1

update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79) * make key-generation compliant to FIPS 186.4 * fix includes in tommath_class.h * update fuzzcorpus instead of error-out * fixup fuzzing make-targets * update Makefile.in * apply necessary patches to ltm sources * clean-up not required ltm files * update to vanilla ltm 1.1.0 this already only contains the required files * remove set/get double
author Steffen Jaeckel <s_jaeckel@gmx.de>
date Mon, 16 Sep 2019 15:50:38 +0200
parents 8bba51a55704
children 1051e4eea25a
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_S_MP_MUL_HIGH_DIGS_C 2 #ifdef BN_S_MP_MUL_HIGH_DIGS_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.
7 * 7 *
8 * The library was designed directly after the MPI library by 8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with 9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * SPDX-License-Identifier: Unlicense
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */ 13 */
17 14
18 /* multiplies |a| * |b| and does not compute the lower digs digits 15 /* multiplies |a| * |b| and does not compute the lower digs digits
19 * [meant to get the higher part of the product] 16 * [meant to get the higher part of the product]
20 */ 17 */
21 int 18 int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
22 s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
23 { 19 {
24 mp_int t; 20 mp_int t;
25 int res, pa, pb, ix, iy; 21 int res, pa, pb, ix, iy;
26 mp_digit u; 22 mp_digit u;
27 mp_word r; 23 mp_word r;
28 mp_digit tmpx, *tmpt, *tmpy; 24 mp_digit tmpx, *tmpt, *tmpy;
29 25
30 /* can we use the fast multiplier? */ 26 /* can we use the fast multiplier? */
31 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C 27 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
32 if (((a->used + b->used + 1) < MP_WARRAY) 28 if (((a->used + b->used + 1) < (int)MP_WARRAY)
33 && (MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { 29 && (MIN(a->used, b->used) < (int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
34 return fast_s_mp_mul_high_digs (a, b, c, digs); 30 return fast_s_mp_mul_high_digs(a, b, c, digs);
35 } 31 }
36 #endif 32 #endif
37 33
38 if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) { 34 if ((res = mp_init_size(&t, a->used + b->used + 1)) != MP_OKAY) {
39 return res; 35 return res;
40 } 36 }
41 t.used = a->used + b->used + 1; 37 t.used = a->used + b->used + 1;
42 38
43 pa = a->used; 39 pa = a->used;
44 pb = b->used; 40 pb = b->used;
45 for (ix = 0; ix < pa; ix++) { 41 for (ix = 0; ix < pa; ix++) {
46 /* clear the carry */ 42 /* clear the carry */
47 u = 0; 43 u = 0;
48 44
49 /* left hand side of A[ix] * B[iy] */ 45 /* left hand side of A[ix] * B[iy] */
50 tmpx = a->dp[ix]; 46 tmpx = a->dp[ix];
51 47
52 /* alias to the address of where the digits will be stored */ 48 /* alias to the address of where the digits will be stored */
53 tmpt = &(t.dp[digs]); 49 tmpt = &(t.dp[digs]);
54 50
55 /* alias for where to read the right hand side from */ 51 /* alias for where to read the right hand side from */
56 tmpy = b->dp + (digs - ix); 52 tmpy = b->dp + (digs - ix);
57 53
58 for (iy = digs - ix; iy < pb; iy++) { 54 for (iy = digs - ix; iy < pb; iy++) {
59 /* calculate the double precision result */ 55 /* calculate the double precision result */
60 r = (mp_word)*tmpt + 56 r = (mp_word)*tmpt +
61 ((mp_word)tmpx * (mp_word)*tmpy++) + 57 ((mp_word)tmpx * (mp_word)*tmpy++) +
62 (mp_word)u; 58 (mp_word)u;
63 59
64 /* get the lower part */ 60 /* get the lower part */
65 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); 61 *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
66 62
67 /* carry the carry */ 63 /* carry the carry */
68 u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); 64 u = (mp_digit)(r >> (mp_word)DIGIT_BIT);
69 } 65 }
70 *tmpt = u; 66 *tmpt = u;
71 } 67 }
72 mp_clamp (&t); 68 mp_clamp(&t);
73 mp_exch (&t, c); 69 mp_exch(&t, c);
74 mp_clear (&t); 70 mp_clear(&t);
75 return MP_OKAY; 71 return MP_OKAY;
76 } 72 }
77 #endif 73 #endif
78 74
79 /* ref: $Format:%D$ */ 75 /* ref: HEAD -> master, tag: v1.1.0 */
80 /* git commit: $Format:%H$ */ 76 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
81 /* commit time: $Format:%ai$ */ 77 /* commit time: 2019-01-28 20:32:32 +0100 */