2019-04-06 16:55:43 +00:00
|
|
|
#include "tommath_private.h"
|
2019-10-30 16:26:27 +00:00
|
|
|
#ifdef S_MP_MUL_BALANCE_C
|
2019-04-06 16:55:43 +00:00
|
|
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
|
|
|
|
/* SPDX-License-Identifier: Unlicense */
|
|
|
|
|
|
|
|
/* single-digit multiplication with the smaller number as the single-digit */
|
2019-10-30 16:26:27 +00:00
|
|
|
mp_err s_mp_mul_balance(const mp_int *a, const mp_int *b, mp_int *c)
|
2019-04-06 16:55:43 +00:00
|
|
|
{
|
2019-10-29 19:05:30 +00:00
|
|
|
mp_int a0, tmp, r;
|
2019-05-19 15:16:13 +00:00
|
|
|
mp_err err;
|
2019-10-29 20:48:50 +00:00
|
|
|
int i, j,
|
2019-10-29 19:05:30 +00:00
|
|
|
nblocks = MP_MAX(a->used, b->used) / MP_MIN(a->used, b->used),
|
|
|
|
bsize = MP_MIN(a->used, b->used);
|
2019-04-06 16:55:43 +00:00
|
|
|
|
2019-05-19 15:16:13 +00:00
|
|
|
if ((err = mp_init_size(&a0, bsize + 2)) != MP_OKAY) {
|
|
|
|
return err;
|
2019-04-06 16:55:43 +00:00
|
|
|
}
|
2019-05-19 15:16:13 +00:00
|
|
|
if ((err = mp_init_multi(&tmp, &r, NULL)) != MP_OKAY) {
|
2019-04-06 16:55:43 +00:00
|
|
|
mp_clear(&a0);
|
2019-05-19 15:16:13 +00:00
|
|
|
return err;
|
2019-04-06 16:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that A is the larger one*/
|
2019-10-29 19:05:30 +00:00
|
|
|
if (a->used < b->used) {
|
|
|
|
MP_EXCH(const mp_int *, a, b);
|
2019-04-06 16:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, j=0; i < nblocks; i++) {
|
|
|
|
/* Cut a slice off of a */
|
2019-10-29 20:48:50 +00:00
|
|
|
a0.used = bsize;
|
|
|
|
s_mp_copy_digs(a0.dp, a->dp + j, a0.used);
|
|
|
|
j += a0.used;
|
2019-10-02 03:47:04 +00:00
|
|
|
mp_clamp(&a0);
|
2019-10-29 20:48:50 +00:00
|
|
|
|
2019-04-06 16:55:43 +00:00
|
|
|
/* Multiply with b */
|
2019-10-29 19:05:30 +00:00
|
|
|
if ((err = mp_mul(&a0, b, &tmp)) != MP_OKAY) {
|
2019-04-06 16:55:43 +00:00
|
|
|
goto LBL_ERR;
|
|
|
|
}
|
|
|
|
/* Shift tmp to the correct position */
|
2019-05-19 15:16:13 +00:00
|
|
|
if ((err = mp_lshd(&tmp, bsize * i)) != MP_OKAY) {
|
2019-04-06 16:55:43 +00:00
|
|
|
goto LBL_ERR;
|
|
|
|
}
|
|
|
|
/* Add to output. No carry needed */
|
2019-05-19 15:16:13 +00:00
|
|
|
if ((err = mp_add(&r, &tmp, &r)) != MP_OKAY) {
|
2019-04-06 16:55:43 +00:00
|
|
|
goto LBL_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* The left-overs; there are always left-overs */
|
2019-10-29 19:05:30 +00:00
|
|
|
if (j < a->used) {
|
2019-10-29 20:48:50 +00:00
|
|
|
a0.used = a->used - j;
|
|
|
|
s_mp_copy_digs(a0.dp, a->dp + j, a0.used);
|
|
|
|
j += a0.used;
|
2019-10-02 03:47:04 +00:00
|
|
|
mp_clamp(&a0);
|
2019-10-29 20:48:50 +00:00
|
|
|
|
2019-10-29 19:05:30 +00:00
|
|
|
if ((err = mp_mul(&a0, b, &tmp)) != MP_OKAY) {
|
2019-04-06 16:55:43 +00:00
|
|
|
goto LBL_ERR;
|
|
|
|
}
|
2019-05-19 15:16:13 +00:00
|
|
|
if ((err = mp_lshd(&tmp, bsize * i)) != MP_OKAY) {
|
2019-04-06 16:55:43 +00:00
|
|
|
goto LBL_ERR;
|
|
|
|
}
|
2019-05-19 15:16:13 +00:00
|
|
|
if ((err = mp_add(&r, &tmp, &r)) != MP_OKAY) {
|
2019-04-06 16:55:43 +00:00
|
|
|
goto LBL_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_exch(&r,c);
|
|
|
|
LBL_ERR:
|
|
|
|
mp_clear_multi(&a0, &tmp, &r,NULL);
|
2019-05-19 15:16:13 +00:00
|
|
|
return err;
|
2019-04-06 16:55:43 +00:00
|
|
|
}
|
|
|
|
#endif
|