summaryrefslogtreecommitdiffstats
path: root/libtommath/tommath_private.h
diff options
context:
space:
mode:
Diffstat (limited to 'libtommath/tommath_private.h')
-rw-r--r--libtommath/tommath_private.h67
1 files changed, 25 insertions, 42 deletions
diff --git a/libtommath/tommath_private.h b/libtommath/tommath_private.h
index 3546370..057f878 100644
--- a/libtommath/tommath_private.h
+++ b/libtommath/tommath_private.h
@@ -13,7 +13,6 @@
#define TOMMATH_PRIV_H_
#include "tommath.h"
-#include <ctype.h>
#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
@@ -25,32 +24,26 @@
#ifdef __cplusplus
extern "C" {
-
-/* C++ compilers don't like assigning void * to mp_digit * */
-#define OPT_CAST(x) (x *)
-
-#else
-
-/* C on the other hand doesn't care */
-#define OPT_CAST(x)
-
#endif
/* define heap macros */
#ifndef XMALLOC
/* default to libc stuff */
-# define XMALLOC malloc
-# define XFREE free
-# define XREALLOC realloc
-# define XCALLOC calloc
+# define XMALLOC(size) malloc(size)
+# define XFREE(mem, size) free(mem)
+# define XREALLOC(mem, oldsize, newsize) realloc(mem, newsize)
#else
/* prototypes for our heap functions */
-extern void *XMALLOC(size_t n);
-extern void *XREALLOC(void *p, size_t n);
-extern void *XCALLOC(size_t n, size_t s);
-extern void XFREE(void *p);
+extern void *XMALLOC(size_t size);
+extern void *XREALLOC(void *mem, size_t oldsize, size_t newsize);
+extern void XFREE(void *mem, size_t size);
#endif
+/* ---> Basic Manipulations <--- */
+#define IS_ZERO(a) ((a)->used == 0)
+#define IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
+#define IS_ODD(a) (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))
+
/* lowlevel functions, do not call! */
int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c);
int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c);
@@ -78,36 +71,26 @@ extern const size_t mp_s_rmap_reverse_sz;
/* Fancy macro to set an MPI from another type.
* There are several things assumed:
- * x is the counter and unsigned
+ * x is the counter
* 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) * 2u); 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] |= (mp_digit)(b >> ((sizeof(type) * 8u) - 4u)) & 15uL;\
- \
- /* 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; \
+ int x = 0; \
+ int new_size = (((CHAR_BIT * sizeof(type)) + DIGIT_BIT) - 1) / DIGIT_BIT; \
+ int res = mp_grow(a, new_size); \
+ if (res == MP_OKAY) { \
+ mp_zero(a); \
+ while (b != 0u) { \
+ a->dp[x++] = ((mp_digit)b & MP_MASK); \
+ if ((CHAR_BIT * sizeof (b)) <= DIGIT_BIT) { break; } \
+ b >>= ((CHAR_BIT * sizeof (b)) <= DIGIT_BIT ? 0 : DIGIT_BIT); \
+ } \
+ a->used = x; \
+ } \
+ return res; \
}
#ifdef __cplusplus