comparison libtommath/bn_s_mp_balance_mul.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents
children
comparison
equal deleted inserted replaced
1691:2d3745d58843 1692:1051e4eea25a
1 #include "tommath_private.h"
2 #ifdef BN_S_MP_BALANCE_MUL_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
5
6 /* single-digit multiplication with the smaller number as the single-digit */
7 mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
8 {
9 int count, len_a, len_b, nblocks, i, j, bsize;
10 mp_int a0, tmp, A, B, r;
11 mp_err err;
12
13 len_a = a->used;
14 len_b = b->used;
15
16 nblocks = MP_MAX(a->used, b->used) / MP_MIN(a->used, b->used);
17 bsize = MP_MIN(a->used, b->used) ;
18
19 if ((err = mp_init_size(&a0, bsize + 2)) != MP_OKAY) {
20 return err;
21 }
22 if ((err = mp_init_multi(&tmp, &r, NULL)) != MP_OKAY) {
23 mp_clear(&a0);
24 return err;
25 }
26
27 /* Make sure that A is the larger one*/
28 if (len_a < len_b) {
29 B = *a;
30 A = *b;
31 } else {
32 A = *a;
33 B = *b;
34 }
35
36 for (i = 0, j=0; i < nblocks; i++) {
37 /* Cut a slice off of a */
38 a0.used = 0;
39 for (count = 0; count < bsize; count++) {
40 a0.dp[count] = A.dp[ j++ ];
41 a0.used++;
42 }
43 mp_clamp(&a0);
44 /* Multiply with b */
45 if ((err = mp_mul(&a0, &B, &tmp)) != MP_OKAY) {
46 goto LBL_ERR;
47 }
48 /* Shift tmp to the correct position */
49 if ((err = mp_lshd(&tmp, bsize * i)) != MP_OKAY) {
50 goto LBL_ERR;
51 }
52 /* Add to output. No carry needed */
53 if ((err = mp_add(&r, &tmp, &r)) != MP_OKAY) {
54 goto LBL_ERR;
55 }
56 }
57 /* The left-overs; there are always left-overs */
58 if (j < A.used) {
59 a0.used = 0;
60 for (count = 0; j < A.used; count++) {
61 a0.dp[count] = A.dp[ j++ ];
62 a0.used++;
63 }
64 mp_clamp(&a0);
65 if ((err = mp_mul(&a0, &B, &tmp)) != MP_OKAY) {
66 goto LBL_ERR;
67 }
68 if ((err = mp_lshd(&tmp, bsize * i)) != MP_OKAY) {
69 goto LBL_ERR;
70 }
71 if ((err = mp_add(&r, &tmp, &r)) != MP_OKAY) {
72 goto LBL_ERR;
73 }
74 }
75
76 mp_exch(&r,c);
77 LBL_ERR:
78 mp_clear_multi(&a0, &tmp, &r,NULL);
79 return err;
80 }
81 #endif