libtommath/bn_mp_mod_2d.c

39 lines
971 B
C
Raw Normal View History

#include "tommath_private.h"
2004-10-29 22:07:18 +00:00
#ifdef BN_MP_MOD_2D_C
2019-04-07 13:29:11 +00:00
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
2003-02-28 16:08:34 +00:00
2003-09-19 22:43:07 +00:00
/* calc a value mod 2**b */
mp_err mp_mod_2d(const mp_int *a, int b, mp_int *c)
2003-02-28 16:08:34 +00:00
{
int x;
2019-05-19 15:16:13 +00:00
mp_err err;
2003-02-28 16:08:34 +00:00
2017-08-30 17:11:35 +00:00
/* if b is <= 0 then zero the int */
if (b <= 0) {
mp_zero(c);
return MP_OKAY;
}
2003-02-28 16:08:34 +00:00
2017-08-30 17:11:35 +00:00
/* if the modulus is larger than the value than return */
2019-04-13 06:46:57 +00:00
if (b >= (a->used * MP_DIGIT_BIT)) {
return mp_copy(a, c);
2017-08-30 17:11:35 +00:00
}
2003-02-28 16:08:34 +00:00
2017-08-30 17:11:35 +00:00
/* copy */
2019-05-19 15:16:13 +00:00
if ((err = mp_copy(a, c)) != MP_OKAY) {
return err;
2017-08-30 17:11:35 +00:00
}
2003-02-28 16:08:34 +00:00
2017-08-30 17:11:35 +00:00
/* zero digits above the last digit of the modulus */
x = (b / MP_DIGIT_BIT) + (((b % MP_DIGIT_BIT) == 0) ? 0 : 1);
MP_ZERO_DIGITS(c->dp + x, c->used - x);
2017-08-30 17:11:35 +00:00
/* clear the digit that is not completely outside/inside the modulus */
2019-04-13 06:46:57 +00:00
c->dp[b / MP_DIGIT_BIT] &=
((mp_digit)1 << (mp_digit)(b % MP_DIGIT_BIT)) - (mp_digit)1;
2017-08-30 17:11:35 +00:00
mp_clamp(c);
return MP_OKAY;
2003-02-28 16:08:34 +00:00
}
2004-10-29 22:07:18 +00:00
#endif