comparison libtommath/bn_mp_mul.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_MP_MUL_C 2 #ifdef BN_MP_MUL_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 /* high level multiplication (handles sign) */ 15 /* high level multiplication (handles sign) */
19 int mp_mul (mp_int * a, mp_int * b, mp_int * c) 16 int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
20 { 17 {
21 int res, neg; 18 int res, neg;
22 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; 19 neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
23 20
24 /* use Toom-Cook? */ 21 /* use Toom-Cook? */
25 #ifdef BN_MP_TOOM_MUL_C 22 #ifdef BN_MP_TOOM_MUL_C
26 if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) { 23 if (MIN(a->used, b->used) >= TOOM_MUL_CUTOFF) {
27 res = mp_toom_mul(a, b, c); 24 res = mp_toom_mul(a, b, c);
28 } else 25 } else
29 #endif 26 #endif
30 #ifdef BN_MP_KARATSUBA_MUL_C 27 #ifdef BN_MP_KARATSUBA_MUL_C
31 /* use Karatsuba? */ 28 /* use Karatsuba? */
32 if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) { 29 if (MIN(a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
33 res = mp_karatsuba_mul (a, b, c); 30 res = mp_karatsuba_mul(a, b, c);
34 } else 31 } else
35 #endif 32 #endif
36 { 33 {
37 /* can we use the fast multiplier? 34 /* can we use the fast multiplier?
38 * 35 *
39 * The fast multiplier can be used if the output will 36 * The fast multiplier can be used if the output will
40 * have less than MP_WARRAY digits and the number of 37 * have less than MP_WARRAY digits and the number of
41 * digits won't affect carry propagation 38 * digits won't affect carry propagation
42 */ 39 */
43 int digs = a->used + b->used + 1; 40 int digs = a->used + b->used + 1;
44 41
45 #ifdef BN_FAST_S_MP_MUL_DIGS_C 42 #ifdef BN_FAST_S_MP_MUL_DIGS_C
46 if ((digs < MP_WARRAY) && 43 if ((digs < (int)MP_WARRAY) &&
47 (MIN(a->used, b->used) <= 44 (MIN(a->used, b->used) <=
48 (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { 45 (int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
49 res = fast_s_mp_mul_digs (a, b, c, digs); 46 res = fast_s_mp_mul_digs(a, b, c, digs);
50 } else 47 } else
51 #endif 48 #endif
52 { 49 {
53 #ifdef BN_S_MP_MUL_DIGS_C 50 #ifdef BN_S_MP_MUL_DIGS_C
54 res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */ 51 res = s_mp_mul(a, b, c); /* uses s_mp_mul_digs */
55 #else 52 #else
56 res = MP_VAL; 53 res = MP_VAL;
57 #endif 54 #endif
58 } 55 }
59 } 56 }
60 c->sign = (c->used > 0) ? neg : MP_ZPOS; 57 c->sign = (c->used > 0) ? neg : MP_ZPOS;
61 return res; 58 return res;
62 } 59 }
63 #endif 60 #endif
64 61
65 /* ref: $Format:%D$ */ 62 /* ref: HEAD -> master, tag: v1.1.0 */
66 /* git commit: $Format:%H$ */ 63 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
67 /* commit time: $Format:%ai$ */ 64 /* commit time: 2019-01-28 20:32:32 +0100 */