Line data Source code
1 : #include "tommath_private.h"
2 : #ifdef BN_MP_2EXPT_C
3 : /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 : /* SPDX-License-Identifier: Unlicense */
5 :
6 : /* computes a = 2**b
7 : *
8 : * Simple algorithm which zeroes the int, grows it then just sets one bit
9 : * as required.
10 : */
11 229 : mp_err mp_2expt(mp_int *a, int b)
12 : {
13 : mp_err err;
14 :
15 : /* zero a as per default */
16 229 : mp_zero(a);
17 :
18 : /* grow a to accomodate the single bit */
19 229 : if ((err = mp_grow(a, (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) {
20 0 : return err;
21 : }
22 :
23 : /* set the used count of where the bit will go */
24 229 : a->used = (b / MP_DIGIT_BIT) + 1;
25 :
26 : /* put the single bit in its place */
27 229 : a->dp[b / MP_DIGIT_BIT] = (mp_digit)1 << (mp_digit)(b % MP_DIGIT_BIT);
28 :
29 229 : return MP_OKAY;
30 : }
31 : #endif
|