split up again into separate files
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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
diff --git a/bn_conversion.c b/bn_conversion.c
deleted file mode 100644
index 19e5532..0000000
--- a/bn_conversion.c
+++ /dev/null
@@ -1,103 +0,0 @@
-#include "tommath_private.h"
-
-#ifdef BN_CONVERSION_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-#define MP_SET_UNSIGNED(name, w) \
- void name(mp_int * a, uint##w##_t b) \
- { \
- int i = 0; \
- while (b != 0u) { \
- a->dp[i++] = ((mp_digit)b & MP_MASK); \
- if (w <= MP_DIGIT_BIT) { break; } \
- b >>= ((w <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
- } \
- a->used = i; \
- a->sign = MP_ZPOS; \
- MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used); \
- }
-#define MP_SET_SIGNED(name, uname, w) \
- void name(mp_int * a, int##w##_t b) \
- { \
- uname(a, (b < 0) ? -(uint##w##_t)b : (uint##w##_t)b); \
- if (b < 0) { a->sign = MP_NEG; } \
- }
-#define MP_INIT_INT(name , set, type) \
- mp_err name(mp_int * a, type b) \
- { \
- mp_err err; \
- if ((err = mp_init(a)) != MP_OKAY) { \
- return err; \
- } \
- set(a, b); \
- return MP_OKAY; \
- }
-#define MP_GET_MAG(name, w) \
- uint##w##_t name(const mp_int* a) \
- { \
- unsigned i = MP_MIN((unsigned)a->used, (unsigned)((w + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
- uint##w##_t res = 0u; \
- while (i --> 0u) { \
- res <<= ((w <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
- res |= (uint##w##_t)a->dp[i]; \
- if (w <= MP_DIGIT_BIT) { break; } \
- } \
- return res; \
- }
-#define MP_GET_SIGNED(name, mag, w) \
- int##w##_t name(const mp_int* a) \
- { \
- uint64_t res = mag(a); \
- return (a->sign == MP_NEG) ? (int##w##_t)-res : (int##w##_t)res;\
- }
-
-#ifdef BN_MP_SET_U32_C
-MP_SET_UNSIGNED(mp_set_u32, 32)
-#endif
-
-#ifdef BN_MP_SET_U64_C
-MP_SET_UNSIGNED(mp_set_u64, 64)
-#endif
-
-#ifdef BN_MP_SET_I32_C
-MP_SET_SIGNED(mp_set_i32, mp_set_u32, 32)
-#endif
-
-#ifdef BN_MP_SET_I64_C
-MP_SET_SIGNED(mp_set_i64, mp_set_u64, 64)
-#endif
-
-#if defined(BN_MP_GET_I32_C) || defined(BN_MP_GET_U32_C)
-MP_GET_SIGNED(mp_get_i32, mp_get_mag32, 32)
-#endif
-
-#if defined(BN_MP_GET_I64_C) || defined(BN_MP_GET_U64_C)
-MP_GET_SIGNED(mp_get_i64, mp_get_mag64, 64)
-#endif
-
-#ifdef BN_MP_GET_MAG32_C
-MP_GET_MAG(mp_get_mag32, 32)
-#endif
-
-#ifdef BN_MP_GET_MAG64_C
-MP_GET_MAG(mp_get_mag64, 64)
-#endif
-
-#ifdef BN_MP_INIT_U32_C
-MP_INIT_INT(mp_init_u32, mp_set_u32, uint32_t)
-#endif
-
-#ifdef BN_MP_INIT_I32_C
-MP_INIT_INT(mp_init_i32, mp_set_i32, int32_t)
-#endif
-
-#ifdef BN_MP_INIT_U64_C
-MP_INIT_INT(mp_init_u64, mp_set_u64, uint64_t)
-#endif
-
-#ifdef BN_MP_INIT_I64_C
-MP_INIT_INT(mp_init_i64, mp_set_i64, int64_t)
-#endif
-
-#endif
diff --git a/bn_mp_get_i32.c b/bn_mp_get_i32.c
new file mode 100644
index 0000000..4dbfb32
--- /dev/null
+++ b/bn_mp_get_i32.c
@@ -0,0 +1,7 @@
+#include "tommath_private.h"
+#ifdef BN_MP_GET_I32_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+MP_GET_SIGNED(mp_get_i32, mp_get_mag32, 32)
+#endif
diff --git a/bn_mp_get_i64.c b/bn_mp_get_i64.c
new file mode 100644
index 0000000..c4c7362
--- /dev/null
+++ b/bn_mp_get_i64.c
@@ -0,0 +1,7 @@
+#include "tommath_private.h"
+#ifdef BN_MP_GET_I64_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+MP_GET_SIGNED(mp_get_i64, mp_get_mag64, 64)
+#endif
diff --git a/bn_mp_get_mag32.c b/bn_mp_get_mag32.c
new file mode 100644
index 0000000..6877d01
--- /dev/null
+++ b/bn_mp_get_mag32.c
@@ -0,0 +1,7 @@
+#include "tommath_private.h"
+#ifdef BN_MP_GET_MAG32_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+MP_GET_MAG(mp_get_mag32, 32)
+#endif
diff --git a/bn_mp_get_mag64.c b/bn_mp_get_mag64.c
new file mode 100644
index 0000000..0e05c7f
--- /dev/null
+++ b/bn_mp_get_mag64.c
@@ -0,0 +1,7 @@
+#include "tommath_private.h"
+#ifdef BN_MP_GET_MAG64_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+MP_GET_MAG(mp_get_mag64, 64)
+#endif
diff --git a/bn_mp_init_i32.c b/bn_mp_init_i32.c
new file mode 100644
index 0000000..bc4de8d
--- /dev/null
+++ b/bn_mp_init_i32.c
@@ -0,0 +1,7 @@
+#include "tommath_private.h"
+#ifdef BN_MP_INIT_I32_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+MP_INIT_INT(mp_init_i32, mp_set_i32, int32_t)
+#endif
diff --git a/bn_mp_init_i64.c b/bn_mp_init_i64.c
new file mode 100644
index 0000000..112a6d9
--- /dev/null
+++ b/bn_mp_init_i64.c
@@ -0,0 +1,7 @@
+#include "tommath_private.h"
+#ifdef BN_MP_INIT_I64_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+MP_INIT_INT(mp_init_u64, mp_set_u64, uint64_t)
+#endif
diff --git a/bn_mp_init_u32.c b/bn_mp_init_u32.c
new file mode 100644
index 0000000..015d89b
--- /dev/null
+++ b/bn_mp_init_u32.c
@@ -0,0 +1,7 @@
+#include "tommath_private.h"
+#ifdef BN_MP_INIT_U32_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+MP_INIT_INT(mp_init_u32, mp_set_u32, uint32_t)
+#endif
diff --git a/bn_mp_init_u64.c b/bn_mp_init_u64.c
new file mode 100644
index 0000000..2b35f7e
--- /dev/null
+++ b/bn_mp_init_u64.c
@@ -0,0 +1,7 @@
+#include "tommath_private.h"
+#ifdef BN_MP_INIT_U64_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+MP_INIT_INT(mp_init_u64, mp_set_u64, uint64_t)
+#endif
diff --git a/bn_mp_set_i32.c b/bn_mp_set_i32.c
new file mode 100644
index 0000000..a181156
--- /dev/null
+++ b/bn_mp_set_i32.c
@@ -0,0 +1,7 @@
+#include "tommath_private.h"
+#ifdef BN_MP_SET_I32_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+MP_SET_SIGNED(mp_set_i32, mp_set_u32, 32)
+#endif
diff --git a/bn_mp_set_i64.c b/bn_mp_set_i64.c
new file mode 100644
index 0000000..0b0fadc
--- /dev/null
+++ b/bn_mp_set_i64.c
@@ -0,0 +1,7 @@
+#include "tommath_private.h"
+#ifdef BN_MP_SET_I64_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+MP_SET_SIGNED(mp_set_i64, mp_set_u64, 64)
+#endif
diff --git a/bn_mp_set_u32.c b/bn_mp_set_u32.c
new file mode 100644
index 0000000..8edbd2e
--- /dev/null
+++ b/bn_mp_set_u32.c
@@ -0,0 +1,7 @@
+#include "tommath_private.h"
+#ifdef BN_MP_SET_U32_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+MP_SET_UNSIGNED(mp_set_u32, 32)
+#endif
diff --git a/bn_mp_set_u64.c b/bn_mp_set_u64.c
new file mode 100644
index 0000000..f5beb76
--- /dev/null
+++ b/bn_mp_set_u64.c
@@ -0,0 +1,7 @@
+#include "tommath_private.h"
+#ifdef BN_MP_SET_U64_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+MP_SET_UNSIGNED(mp_set_u64, 64)
+#endif
diff --git a/tommath_private.h b/tommath_private.h
index afe01de..6495ce1 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -231,4 +231,53 @@ MP_DEPRECATED(s_mp_toom_mul) mp_err mp_toom_mul(const mp_int *a, const mp_int *b
MP_DEPRECATED(s_mp_toom_sqr) mp_err mp_toom_sqr(const mp_int *a, mp_int *b);
MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len);
+/* code-generating macros */
+#define MP_SET_UNSIGNED(name, w) \
+ void name(mp_int * a, uint##w##_t b) \
+ { \
+ int i = 0; \
+ while (b != 0u) { \
+ a->dp[i++] = ((mp_digit)b & MP_MASK); \
+ if (w <= MP_DIGIT_BIT) { break; } \
+ b >>= ((w <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
+ } \
+ a->used = i; \
+ a->sign = MP_ZPOS; \
+ MP_ZERO_DIGITS(a->dp + a->used, a->alloc - a->used); \
+ }
+#define MP_SET_SIGNED(name, uname, w) \
+ void name(mp_int * a, int##w##_t b) \
+ { \
+ uname(a, (b < 0) ? -(uint##w##_t)b : (uint##w##_t)b); \
+ if (b < 0) { a->sign = MP_NEG; } \
+ }
+#define MP_INIT_INT(name , set, type) \
+ mp_err name(mp_int * a, type b) \
+ { \
+ mp_err err; \
+ if ((err = mp_init(a)) != MP_OKAY) { \
+ return err; \
+ } \
+ set(a, b); \
+ return MP_OKAY; \
+ }
+#define MP_GET_MAG(name, w) \
+ uint##w##_t name(const mp_int* a) \
+ { \
+ unsigned i = MP_MIN((unsigned)a->used, (unsigned)((w + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)); \
+ uint##w##_t res = 0u; \
+ while (i --> 0u) { \
+ res <<= ((w <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
+ res |= (uint##w##_t)a->dp[i]; \
+ if (w <= MP_DIGIT_BIT) { break; } \
+ } \
+ return res; \
+ }
+#define MP_GET_SIGNED(name, mag, w) \
+ int##w##_t name(const mp_int* a) \
+ { \
+ uint64_t res = mag(a); \
+ return (a->sign == MP_NEG) ? (int##w##_t)-res : (int##w##_t)res;\
+ }
+
#endif