comparison libtommath/bn_mp_mul_2.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_2_C 2 #ifdef BN_MP_MUL_2_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 /* b = a*2 */ 6 /* b = a*2 */
19 int mp_mul_2(mp_int * a, mp_int * b) 7 mp_err mp_mul_2(const mp_int *a, mp_int *b)
20 { 8 {
21 int x, res, oldused; 9 int x, oldused;
10 mp_err err;
22 11
23 /* grow to accomodate result */ 12 /* grow to accomodate result */
24 if (b->alloc < (a->used + 1)) { 13 if (b->alloc < (a->used + 1)) {
25 if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) { 14 if ((err = mp_grow(b, a->used + 1)) != MP_OKAY) {
26 return res; 15 return err;
27 } 16 }
28 } 17 }
29 18
30 oldused = b->used; 19 oldused = b->used;
31 b->used = a->used; 20 b->used = a->used;
32 21
33 { 22 {
34 mp_digit r, rr, *tmpa, *tmpb; 23 mp_digit r, rr, *tmpa, *tmpb;
35 24
36 /* alias for source */ 25 /* alias for source */
37 tmpa = a->dp; 26 tmpa = a->dp;
38
39 /* alias for dest */
40 tmpb = b->dp;
41 27
42 /* carry */ 28 /* alias for dest */
43 r = 0; 29 tmpb = b->dp;
44 for (x = 0; x < a->used; x++) { 30
45 31 /* carry */
46 /* get what will be the *next* carry bit from the 32 r = 0;
47 * MSB of the current digit 33 for (x = 0; x < a->used; x++) {
34
35 /* get what will be the *next* carry bit from the
36 * MSB of the current digit
37 */
38 rr = *tmpa >> (mp_digit)(MP_DIGIT_BIT - 1);
39
40 /* now shift up this digit, add in the carry [from the previous] */
41 *tmpb++ = ((*tmpa++ << 1uL) | r) & MP_MASK;
42
43 /* copy the carry that would be from the source
44 * digit into the next iteration
45 */
46 r = rr;
47 }
48
49 /* new leading digit? */
50 if (r != 0u) {
51 /* add a MSB which is always 1 at this point */
52 *tmpb = 1;
53 ++(b->used);
54 }
55
56 /* now zero any excess digits on the destination
57 * that we didn't write to
48 */ 58 */
49 rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1)); 59 MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used);
50 60 }
51 /* now shift up this digit, add in the carry [from the previous] */ 61 b->sign = a->sign;
52 *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK; 62 return MP_OKAY;
53
54 /* copy the carry that would be from the source
55 * digit into the next iteration
56 */
57 r = rr;
58 }
59
60 /* new leading digit? */
61 if (r != 0) {
62 /* add a MSB which is always 1 at this point */
63 *tmpb = 1;
64 ++(b->used);
65 }
66
67 /* now zero any excess digits on the destination
68 * that we didn't write to
69 */
70 tmpb = b->dp + b->used;
71 for (x = b->used; x < oldused; x++) {
72 *tmpb++ = 0;
73 }
74 }
75 b->sign = a->sign;
76 return MP_OKAY;
77 } 63 }
78 #endif 64 #endif
79
80 /* ref: $Format:%D$ */
81 /* git commit: $Format:%H$ */
82 /* commit time: $Format:%ai$ */