Merge pull request #578 from PX4/fix_mp_prime_strong_lucas_lefridge_compilation
[libtommath.git] / mp_2expt.c
blobc11fe4cb30a13c90ab0804656881cce9e6e708cf
1 #include "tommath_private.h"
2 #ifdef MP_2EXPT_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* computes a = 2**b
8 * Simple algorithm which zeroes the int, grows it then just sets one bit
9 * as required.
11 mp_err mp_2expt(mp_int *a, int b)
13 mp_err err;
15 if (b < 0) {
16 return MP_VAL;
19 /* zero a as per default */
20 mp_zero(a);
22 /* grow a to accommodate the single bit */
23 if ((err = mp_grow(a, (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) {
24 return err;
27 /* set the used count of where the bit will go */
28 a->used = (b / MP_DIGIT_BIT) + 1;
30 /* put the single bit in its place */
31 a->dp[b / MP_DIGIT_BIT] = (mp_digit)1 << (mp_digit)(b % MP_DIGIT_BIT);
33 return MP_OKAY;
35 #endif