efficiency improvement in get/set routines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
diff --git a/bn_mp_get_int.c b/bn_mp_get_int.c
index 0997495..d9c7a11 100644
--- a/bn_mp_get_int.c
+++ b/bn_mp_get_int.c
@@ -15,25 +15,8 @@
/* get the lower 32-bits of an mp_int */
unsigned long mp_get_int(const mp_int *a)
{
- int i;
- mp_min_u32 res;
-
- if (IS_ZERO(a)) {
- return 0;
- }
-
- /* get number of digits of the lsb we have to read */
- i = MIN(a->used, ((((int)sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
-
- /* get most significant digit of result */
- res = DIGIT(a, i);
-
- while (--i >= 0) {
- res = (res << DIGIT_BIT) | DIGIT(a, i);
- }
-
/* force result to 32-bits always so it is consistent on non 32-bit platforms */
- return res & 0xFFFFFFFFUL;
+ return mp_get_long(a) & 0xFFFFFFFFUL;
}
#endif
diff --git a/bn_mp_get_long.c b/bn_mp_get_long.c
index 134043b..b90cb3d 100644
--- a/bn_mp_get_long.c
+++ b/bn_mp_get_long.c
@@ -26,11 +26,11 @@ unsigned long mp_get_long(const mp_int *a)
i = MIN(a->used, ((((int)sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
/* get most significant digit of result */
- res = DIGIT(a, i);
+ res = (unsigned long)a->dp[i];
-#if (ULONG_MAX != 0xffffffffuL) || (DIGIT_BIT < 32)
+#if (ULONG_MAX != 0xFFFFFFFFUL) || (DIGIT_BIT < 32)
while (--i >= 0) {
- res = (res << DIGIT_BIT) | DIGIT(a, i);
+ res = (res << DIGIT_BIT) | (unsigned long)a->dp[i];
}
#endif
return res;
diff --git a/bn_mp_get_long_long.c b/bn_mp_get_long_long.c
index 75767df..50c34aa 100644
--- a/bn_mp_get_long_long.c
+++ b/bn_mp_get_long_long.c
@@ -26,11 +26,11 @@ unsigned long long mp_get_long_long(const mp_int *a)
i = MIN(a->used, ((((int)sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
/* get most significant digit of result */
- res = DIGIT(a, i);
+ res = (unsigned long long)a->dp[i];
#if DIGIT_BIT < 64
while (--i >= 0) {
- res = (res << DIGIT_BIT) | DIGIT(a, i);
+ res = (res << DIGIT_BIT) | (unsigned long long)a->dp[i];
}
#endif
return res;
diff --git a/bn_mp_set_int.c b/bn_mp_set_int.c
index 4f01e25..2cdc064 100644
--- a/bn_mp_set_int.c
+++ b/bn_mp_set_int.c
@@ -15,28 +15,7 @@
/* set a 32-bit const */
int mp_set_int(mp_int *a, unsigned long b)
{
- int x, res;
-
- mp_zero(a);
-
- /* set four bits at a time */
- for (x = 0; x < 8; 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 >> 28) & 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;
+ return mp_set_long(a, b & 0xFFFFFFFFUL);
}
#endif
diff --git a/bn_mp_set_long.c b/bn_mp_set_long.c
index 35be8e7..404ae97 100644
--- a/bn_mp_set_long.c
+++ b/bn_mp_set_long.c
@@ -13,7 +13,24 @@
*/
/* set a platform dependent unsigned long int */
+#if (ULONG_MAX != 0xFFFFFFFFUL) || (DIGIT_BIT < 32)
MP_SET_XLONG(mp_set_long, unsigned long)
+#else
+int mp_set_long(mp_int *a, unsigned long b)
+{
+ int x = 0;
+ int res = mp_grow(a, (CHAR_BIT * sizeof(unsigned long) + DIGIT_BIT - 1) / DIGIT_BIT);
+ if (res == MP_OKAY) {
+ mp_zero(a);
+ if (b) {
+ a->dp[x++] = (mp_digit)b;
+ }
+ a->used = x;
+ }
+ return res;
+}
+
+#endif
#endif
/* ref: $Format:%D$ */
diff --git a/tommath_private.h b/tommath_private.h
index 8feb1ad..ecd3447 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -83,36 +83,24 @@ 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 res = mp_grow(a, (CHAR_BIT * sizeof(type) + DIGIT_BIT - 1) / DIGIT_BIT); \
+ if (res == MP_OKAY) { \
+ mp_zero(a); \
+ while (b) { \
+ a->dp[x++] = ((mp_digit)b & MP_MASK); \
+ b >>= DIGIT_BIT; \
+ } \
+ a->used = x; \
+ } \
+ return res; \
}
#ifdef __cplusplus