comparison libtommath/bn_mp_mul_d.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_MP_MUL_D_C 2 #ifdef BN_MP_MUL_D_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 /* multiply by a digit */ 6 /* multiply by a digit */
19 int 7 mp_err mp_mul_d(const mp_int *a, mp_digit b, mp_int *c)
20 mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
21 { 8 {
22 mp_digit u, *tmpa, *tmpc; 9 mp_digit u, *tmpa, *tmpc;
23 mp_word r; 10 mp_word r;
24 int ix, res, olduse; 11 mp_err err;
12 int ix, olduse;
25 13
26 /* make sure c is big enough to hold a*b */ 14 /* make sure c is big enough to hold a*b */
27 if (c->alloc < (a->used + 1)) { 15 if (c->alloc < (a->used + 1)) {
28 if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) { 16 if ((err = mp_grow(c, a->used + 1)) != MP_OKAY) {
29 return res; 17 return err;
30 } 18 }
31 } 19 }
32 20
33 /* get the original destinations used count */ 21 /* get the original destinations used count */
34 olduse = c->used; 22 olduse = c->used;
35 23
36 /* set the sign */ 24 /* set the sign */
37 c->sign = a->sign; 25 c->sign = a->sign;
38 26
39 /* alias for a->dp [source] */ 27 /* alias for a->dp [source] */
40 tmpa = a->dp; 28 tmpa = a->dp;
41 29
42 /* alias for c->dp [dest] */ 30 /* alias for c->dp [dest] */
43 tmpc = c->dp; 31 tmpc = c->dp;
44 32
45 /* zero carry */ 33 /* zero carry */
46 u = 0; 34 u = 0;
47 35
48 /* compute columns */ 36 /* compute columns */
49 for (ix = 0; ix < a->used; ix++) { 37 for (ix = 0; ix < a->used; ix++) {
50 /* compute product and carry sum for this term */ 38 /* compute product and carry sum for this term */
51 r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b); 39 r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b);
52 40
53 /* mask off higher bits to get a single digit */ 41 /* mask off higher bits to get a single digit */
54 *tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK)); 42 *tmpc++ = (mp_digit)(r & (mp_word)MP_MASK);
55 43
56 /* send carry into next iteration */ 44 /* send carry into next iteration */
57 u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); 45 u = (mp_digit)(r >> (mp_word)MP_DIGIT_BIT);
58 } 46 }
59 47
60 /* store final carry [if any] and increment ix offset */ 48 /* store final carry [if any] and increment ix offset */
61 *tmpc++ = u; 49 *tmpc++ = u;
62 ++ix; 50 ++ix;
63 51
64 /* now zero digits above the top */ 52 /* now zero digits above the top */
65 while (ix++ < olduse) { 53 MP_ZERO_DIGITS(tmpc, olduse - ix);
66 *tmpc++ = 0;
67 }
68 54
69 /* set used count */ 55 /* set used count */
70 c->used = a->used + 1; 56 c->used = a->used + 1;
71 mp_clamp(c); 57 mp_clamp(c);
72 58
73 return MP_OKAY; 59 return MP_OKAY;
74 } 60 }
75 #endif 61 #endif
76
77 /* ref: $Format:%D$ */
78 /* git commit: $Format:%H$ */
79 /* commit time: $Format:%ai$ */