1 #include "tommath_private.h"
2 #ifdef BN_MP_MOD_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 *
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 * SPDX-License-Identifier: Unlicense
13 */
14
15 /* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */
mp_mod(const mp_int * a,const mp_int * b,mp_int * c)16 int mp_mod(const mp_int *a, const mp_int *b, mp_int *c)
17 {
18 mp_int t;
19 int res;
20
21 if ((res = mp_init_size(&t, b->used)) != MP_OKAY) {
22 return res;
23 }
24
25 if ((res = mp_div(a, b, NULL, &t)) != MP_OKAY) {
26 mp_clear(&t);
27 return res;
28 }
29
30 if ((mp_iszero(&t) != MP_NO) || (t.sign == b->sign)) {
31 res = MP_OKAY;
32 mp_exch(&t, c);
33 } else {
34 res = mp_add(b, &t, c);
35 }
36
37 mp_clear(&t);
38 return res;
39 }
40 #endif
41
42 /* ref: $Format:%D$ */
43 /* git commit: $Format:%H$ */
44 /* commit time: $Format:%ai$ */
45