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 922 : mp_err mp_2expt(mp_int *a, int b) 12 : { 13 32 : mp_err err; 14 : 15 : /* zero a as per default */ 16 922 : mp_zero(a); 17 : 18 : /* grow a to accomodate the single bit */ 19 922 : 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 922 : a->used = (b / MP_DIGIT_BIT) + 1; 25 : 26 : /* put the single bit in its place */ 27 922 : a->dp[b / MP_DIGIT_BIT] = (mp_digit)1 << (mp_digit)(b % MP_DIGIT_BIT); 28 : 29 922 : return MP_OKAY; 30 : } 31 : #endif