comparison libtommath/bn_fast_s_mp_mul_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 c8c20fb57a9a
children
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_FAST_S_MP_MUL_DIGS_C 2 #ifdef BN_FAST_S_MP_MUL_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 /* Fast (comba) multiplier 15 /* Fast (comba) multiplier
19 * 16 *
20 * This is the fast column-array [comba] multiplier. It is 17 * This is the fast column-array [comba] multiplier. It is
21 * designed to compute the columns of the product first 18 * designed to compute the columns of the product first
22 * then handle the carries afterwards. This has the effect 19 * then handle the carries afterwards. This has the effect
23 * of making the nested loops that compute the columns very 20 * of making the nested loops that compute the columns very
24 * simple and schedulable on super-scalar processors. 21 * simple and schedulable on super-scalar processors.
25 * 22 *
26 * This has been modified to produce a variable number of 23 * This has been modified to produce a variable number of
27 * digits of output so if say only a half-product is required 24 * digits of output so if say only a half-product is required
28 * you don't have to compute the upper half (a feature 25 * you don't have to compute the upper half (a feature
29 * required for fast Barrett reduction). 26 * required for fast Barrett reduction).
30 * 27 *
31 * Based on Algorithm 14.12 on pp.595 of HAC. 28 * Based on Algorithm 14.12 on pp.595 of HAC.
32 * 29 *
33 */ 30 */
34 int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) 31 int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
35 { 32 {
36 int olduse, res, pa, ix, iz; 33 int olduse, res, pa, ix, iz;
37 mp_digit W[MP_WARRAY]; 34 mp_digit W[MP_WARRAY];
38 mp_word _W; 35 mp_word _W;
39 36
40 /* grow the destination as required */ 37 /* grow the destination as required */
41 if (c->alloc < digs) { 38 if (c->alloc < digs) {
42 if ((res = mp_grow (c, digs)) != MP_OKAY) { 39 if ((res = mp_grow(c, digs)) != MP_OKAY) {
43 return res; 40 return res;
44 } 41 }
45 } 42 }
46 43
47 /* number of output digits to produce */ 44 /* number of output digits to produce */
48 pa = MIN(digs, a->used + b->used); 45 pa = MIN(digs, a->used + b->used);
49 46
50 /* clear the carry */ 47 /* clear the carry */
51 _W = 0; 48 _W = 0;
52 for (ix = 0; ix < pa; ix++) { 49 for (ix = 0; ix < pa; ix++) {
53 int tx, ty; 50 int tx, ty;
54 int iy; 51 int iy;
55 mp_digit *tmpx, *tmpy; 52 mp_digit *tmpx, *tmpy;
56 53
57 /* get offsets into the two bignums */ 54 /* get offsets into the two bignums */
60 57
61 /* setup temp aliases */ 58 /* setup temp aliases */
62 tmpx = a->dp + tx; 59 tmpx = a->dp + tx;
63 tmpy = b->dp + ty; 60 tmpy = b->dp + ty;
64 61
65 /* this is the number of times the loop will iterrate, essentially 62 /* this is the number of times the loop will iterrate, essentially
66 while (tx++ < a->used && ty-- >= 0) { ... } 63 while (tx++ < a->used && ty-- >= 0) { ... }
67 */ 64 */
68 iy = MIN(a->used-tx, ty+1); 65 iy = MIN(a->used-tx, ty+1);
69 66
70 /* execute loop */ 67 /* execute loop */
71 for (iz = 0; iz < iy; ++iz) { 68 for (iz = 0; iz < iy; ++iz) {
72 _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); 69 _W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
73 70
74 } 71 }
75 72
76 /* store term */ 73 /* store term */
77 W[ix] = ((mp_digit)_W) & MP_MASK; 74 W[ix] = (mp_digit)_W & MP_MASK;
78 75
79 /* make next carry */ 76 /* make next carry */
80 _W = _W >> ((mp_word)DIGIT_BIT); 77 _W = _W >> (mp_word)DIGIT_BIT;
81 } 78 }
82 79
83 /* setup dest */ 80 /* setup dest */
84 olduse = c->used; 81 olduse = c->used;
85 c->used = pa; 82 c->used = pa;
86 83
87 { 84 {
88 mp_digit *tmpc; 85 mp_digit *tmpc;
89 tmpc = c->dp; 86 tmpc = c->dp;
90 for (ix = 0; ix < pa; ix++) { 87 for (ix = 0; ix < pa; ix++) {
91 /* now extract the previous digit [below the carry] */ 88 /* now extract the previous digit [below the carry] */
92 *tmpc++ = W[ix]; 89 *tmpc++ = W[ix];
93 } 90 }
94 91
95 /* clear unused digits [that existed in the old copy of c] */ 92 /* clear unused digits [that existed in the old copy of c] */
96 for (; ix < olduse; ix++) { 93 for (; ix < olduse; ix++) {
97 *tmpc++ = 0; 94 *tmpc++ = 0;
98 } 95 }
99 } 96 }
100 mp_clamp (c); 97 mp_clamp(c);
101 return MP_OKAY; 98 return MP_OKAY;
102 } 99 }
103 #endif 100 #endif
104 101
105 /* ref: $Format:%D$ */ 102 /* ref: HEAD -> master, tag: v1.1.0 */
106 /* git commit: $Format:%H$ */ 103 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
107 /* commit time: $Format:%ai$ */ 104 /* commit time: 2019-01-28 20:32:32 +0100 */