Merge pull request #578 from PX4/fix_mp_prime_strong_lucas_lefridge_compilation
[libtommath.git] / mp_mod_2d.c
blob82c64f05fd76e01e7283f574977ee7c2a2178d97
1 #include "tommath_private.h"
2 #ifdef MP_MOD_2D_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* calc a value mod 2**b */
7 mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c)
9 int x;
10 mp_err err;
12 if (b < 0) {
13 return MP_VAL;
16 if (b == 0) {
17 mp_zero(c);
18 return MP_OKAY;
21 /* if the modulus is larger than the value than return */
22 if (b >= (a->used * MP_DIGIT_BIT)) {
23 return mp_copy(a, c);
26 if ((err = mp_copy(a, c)) != MP_OKAY) {
27 return err;
30 /* zero digits above the last digit of the modulus */
31 x = (b / MP_DIGIT_BIT) + (((b % MP_DIGIT_BIT) == 0) ? 0 : 1);
32 s_mp_zero_digs(c->dp + x, c->used - x);
34 /* clear the digit that is not completely outside/inside the modulus */
35 c->dp[b / MP_DIGIT_BIT] &=
36 ((mp_digit)1 << (mp_digit)(b % MP_DIGIT_BIT)) - (mp_digit)1;
37 mp_clamp(c);
38 return MP_OKAY;
40 #endif