From f88e6a042a3ff428fa97f627779cd09768b6b9cd Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Wed, 10 Dec 2014 18:32:44 +0100 Subject: [PATCH] replace mp_set_long() implementation by macro --- bn_mp_set_long.c | 27 +-------------------------- tommath.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/bn_mp_set_long.c b/bn_mp_set_long.c index 2050e9a..60e12b2 100644 --- a/bn_mp_set_long.c +++ b/bn_mp_set_long.c @@ -16,32 +16,7 @@ */ /* set a platform dependent unsigned long int */ -int mp_set_long (mp_int * a, unsigned long b) -{ - unsigned int x; - int res; - - mp_zero (a); - - /* set four bits at a time */ - for (x = 0; x < sizeof(unsigned long) * 2; x++) { - /* shift the number up four bits */ - if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { - return res; - } - - /* OR in the top four bits of the source */ - a->dp[0] |= (b >> ((sizeof(unsigned long)) * 8 - 4)) & 15; - - /* shift the source up to the next four bits */ - b <<= 4; - - /* ensure that digits are not clamped off */ - a->used += 1; - } - mp_clamp (a); - return MP_OKAY; -} +MP_SET_XLONG(mp_set_long, unsigned long) #endif /* $Source$ */ diff --git a/tommath.h b/tommath.h index a3faf35..051bdcb 100644 --- a/tommath.h +++ b/tommath.h @@ -596,6 +596,40 @@ void bn_reverse(unsigned char *s, int len); extern const char *mp_s_rmap; +/* Fancy macro to set an MPI from another type. + * There are several things assumed: + * x is the counter and unsigned + * a is the pointer to the MPI + * b is the original value that should be set in the MPI. + */ +#define MP_SET_XLONG(func_name, type) \ +int func_name (mp_int * a, type b) \ +{ \ + unsigned int x; \ + int res; \ + \ + mp_zero (a); \ + \ + /* set four bits at a time */ \ + for (x = 0; x < sizeof(type) * 2; x++) { \ + /* shift the number up four bits */ \ + if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { \ + return res; \ + } \ + \ + /* OR in the top four bits of the source */ \ + a->dp[0] |= (b >> ((sizeof(type)) * 8 - 4)) & 15; \ + \ + /* shift the source up to the next four bits */ \ + b <<= 4; \ + \ + /* ensure that digits are not clamped off */ \ + a->used += 1; \ + } \ + mp_clamp (a); \ + return MP_OKAY; \ +} + #ifdef __cplusplus } #endif