comparison libtommath/bn_s_mp_mul_high_digs.c @ 1739:13d834efc376 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Thu, 15 Oct 2020 19:55:15 +0800
parents 1051e4eea25a
children
comparison
equal deleted inserted replaced
1562:768ebf737aa0 1739:13d834efc376
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 /* 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 * The library is free for all purposes without any express
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */
17 5
18 /* multiplies |a| * |b| and does not compute the lower digs digits 6 /* multiplies |a| * |b| and does not compute the lower digs digits
19 * [meant to get the higher part of the product] 7 * [meant to get the higher part of the product]
20 */ 8 */
21 int 9 mp_err 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 { 10 {
24 mp_int t; 11 mp_int t;
25 int res, pa, pb, ix, iy; 12 int pa, pb, ix, iy;
26 mp_digit u; 13 mp_err err;
27 mp_word r; 14 mp_digit u;
28 mp_digit tmpx, *tmpt, *tmpy; 15 mp_word r;
16 mp_digit tmpx, *tmpt, *tmpy;
29 17
30 /* can we use the fast multiplier? */ 18 /* can we use the fast multiplier? */
31 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C 19 if (MP_HAS(S_MP_MUL_HIGH_DIGS_FAST)
32 if (((a->used + b->used + 1) < MP_WARRAY) 20 && ((a->used + b->used + 1) < MP_WARRAY)
33 && (MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { 21 && (MP_MIN(a->used, b->used) < MP_MAXFAST)) {
34 return fast_s_mp_mul_high_digs (a, b, c, digs); 22 return s_mp_mul_high_digs_fast(a, b, c, digs);
35 } 23 }
36 #endif
37 24
38 if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) { 25 if ((err = mp_init_size(&t, a->used + b->used + 1)) != MP_OKAY) {
39 return res; 26 return err;
40 } 27 }
41 t.used = a->used + b->used + 1; 28 t.used = a->used + b->used + 1;
42 29
43 pa = a->used; 30 pa = a->used;
44 pb = b->used; 31 pb = b->used;
45 for (ix = 0; ix < pa; ix++) { 32 for (ix = 0; ix < pa; ix++) {
46 /* clear the carry */ 33 /* clear the carry */
47 u = 0; 34 u = 0;
48 35
49 /* left hand side of A[ix] * B[iy] */ 36 /* left hand side of A[ix] * B[iy] */
50 tmpx = a->dp[ix]; 37 tmpx = a->dp[ix];
51 38
52 /* alias to the address of where the digits will be stored */ 39 /* alias to the address of where the digits will be stored */
53 tmpt = &(t.dp[digs]); 40 tmpt = &(t.dp[digs]);
54 41
55 /* alias for where to read the right hand side from */ 42 /* alias for where to read the right hand side from */
56 tmpy = b->dp + (digs - ix); 43 tmpy = b->dp + (digs - ix);
57 44
58 for (iy = digs - ix; iy < pb; iy++) { 45 for (iy = digs - ix; iy < pb; iy++) {
59 /* calculate the double precision result */ 46 /* calculate the double precision result */
60 r = (mp_word)*tmpt + 47 r = (mp_word)*tmpt +
61 ((mp_word)tmpx * (mp_word)*tmpy++) + 48 ((mp_word)tmpx * (mp_word)*tmpy++) +
62 (mp_word)u; 49 (mp_word)u;
63 50
64 /* get the lower part */ 51 /* get the lower part */
65 *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); 52 *tmpt++ = (mp_digit)(r & (mp_word)MP_MASK);
66 53
67 /* carry the carry */ 54 /* carry the carry */
68 u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); 55 u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
69 } 56 }
70 *tmpt = u; 57 *tmpt = u;
71 } 58 }
72 mp_clamp (&t); 59 mp_clamp(&t);
73 mp_exch (&t, c); 60 mp_exch(&t, c);
74 mp_clear (&t); 61 mp_clear(&t);
75 return MP_OKAY; 62 return MP_OKAY;
76 } 63 }
77 #endif 64 #endif
78
79 /* ref: $Format:%D$ */
80 /* git commit: $Format:%H$ */
81 /* commit time: $Format:%ai$ */