diff options
author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2019-07-26 13:12:31 (GMT) |
---|---|---|
committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2019-07-26 13:12:31 (GMT) |
commit | 1cb1fee6edc063cb49beb0188c2a3db4771846fa (patch) | |
tree | a36ad0bb70244ac8d7c74e3f72101e8299f68e64 /libtommath/bn_mp_expt_u32.c | |
parent | 508e022fa975ee61d6aef292a45eabccc2e3d662 (diff) | |
download | tcl-1cb1fee6edc063cb49beb0188c2a3db4771846fa.zip tcl-1cb1fee6edc063cb49beb0188c2a3db4771846fa.tar.gz tcl-1cb1fee6edc063cb49beb0188c2a3db4771846fa.tar.bz2 |
Update to latest "develop" branch of libtommath
Diffstat (limited to 'libtommath/bn_mp_expt_u32.c')
-rw-r--r-- | libtommath/bn_mp_expt_u32.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/libtommath/bn_mp_expt_u32.c b/libtommath/bn_mp_expt_u32.c new file mode 100644 index 0000000..4ec725e --- /dev/null +++ b/libtommath/bn_mp_expt_u32.c @@ -0,0 +1,45 @@ +#include "tommath_private.h" +#ifdef BN_MP_EXPT_U32_C +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +/* calculate c = a**b using a square-multiply algorithm */ +mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) +{ + mp_err err; + + mp_int g; + + if ((err = mp_init_copy(&g, a)) != MP_OKAY) { + return err; + } + + /* set initial result */ + mp_set(c, 1uL); + + while (b > 0u) { + /* if the bit is set multiply */ + if ((b & 1u) != 0u) { + if ((err = mp_mul(c, &g, c)) != MP_OKAY) { + mp_clear(&g); + return err; + } + } + + /* square */ + if (b > 1u) { + if ((err = mp_sqr(&g, &g)) != MP_OKAY) { + mp_clear(&g); + return err; + } + } + + /* shift to next bit */ + b >>= 1; + } + + mp_clear(&g); + return MP_OKAY; +} + +#endif |