| 1 |
#include "tommath_private.h" |
| 2 |
#ifdef BN_MP_MOD_C |
| 3 |
/* LibTomMath, multiple-precision integer library -- Tom St Denis */ |
| 4 |
/* SPDX-License-Identifier: Unlicense */ |
| 5 |
|
| 6 |
/* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */ |
| 7 |
mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c) |
| 8 |
{ |
| 9 |
mp_int t; |
| 10 |
mp_err err; |
| 11 |
|
| 12 |
if ((err = mp_init_size(&t, b->used)) != MP_OKAY) { |
| 13 |
return err; |
| 14 |
} |
| 15 |
|
| 16 |
if ((err = mp_div(a, b, NULL, &t)) != MP_OKAY) { |
| 17 |
goto LBL_ERR; |
| 18 |
} |
| 19 |
|
| 20 |
if (MP_IS_ZERO(&t) || (t.sign == b->sign)) { |
| 21 |
err = MP_OKAY; |
| 22 |
mp_exch(&t, c); |
| 23 |
} else { |
| 24 |
err = mp_add(b, &t, c); |
| 25 |
} |
| 26 |
|
| 27 |
LBL_ERR: |
| 28 |
mp_clear(&t); |
| 29 |
return err; |
| 30 |
} |
| 31 |
#endif |