From 1e29c4f4ad0ec8903b99967d45b6ea0ea76c6111 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Mon, 7 Oct 2019 11:37:36 +0000 Subject: Update to latest "develop" branch --- libtommath/README.md | 13 +- libtommath/bn_deprecated.c | 84 +++++++++ libtommath/bn_mp_addmod.c | 5 +- libtommath/bn_mp_copy.c | 27 ++- libtommath/bn_mp_div.c | 87 +++------ libtommath/bn_mp_div_2.c | 37 ++-- libtommath/bn_mp_div_d.c | 6 +- libtommath/bn_mp_export.c | 10 +- libtommath/bn_mp_expt_u32.c | 11 +- libtommath/bn_mp_exptmod.c | 55 ++---- libtommath/bn_mp_exteuclid.c | 72 ++------ libtommath/bn_mp_from_sbin.c | 25 +++ libtommath/bn_mp_from_ubin.c | 39 ++++ libtommath/bn_mp_fwrite.c | 13 +- libtommath/bn_mp_ilogb.c | 6 +- libtommath/bn_mp_incr.c | 2 +- libtommath/bn_mp_invmod.c | 12 +- libtommath/bn_mp_mod.c | 4 +- libtommath/bn_mp_mul.c | 110 ++++-------- libtommath/bn_mp_mulmod.c | 5 +- libtommath/bn_mp_prime_frobenius_underwood.c | 85 +++------ libtommath/bn_mp_prime_next_prime.c | 38 ++-- libtommath/bn_mp_prime_rand.c | 3 +- libtommath/bn_mp_prime_strong_lucas_selfridge.c | 153 +++++----------- libtommath/bn_mp_radix_size.c | 10 +- libtommath/bn_mp_read_signed_bin.c | 25 --- libtommath/bn_mp_read_unsigned_bin.c | 39 ---- libtommath/bn_mp_reduce.c | 14 +- libtommath/bn_mp_reduce_is_2k.c | 2 +- libtommath/bn_mp_reduce_is_2k_l.c | 2 +- libtommath/bn_mp_root_u32.c | 50 ++---- libtommath/bn_mp_sbin_size.c | 11 ++ libtommath/bn_mp_signed_bin_size.c | 11 -- libtommath/bn_mp_sqr.c | 42 ++--- libtommath/bn_mp_sqrmod.c | 5 +- libtommath/bn_mp_sqrtmod_prime.c | 6 +- libtommath/bn_mp_submod.c | 5 +- libtommath/bn_mp_to_radix.c | 74 ++++++++ libtommath/bn_mp_to_sbin.c | 23 +++ libtommath/bn_mp_to_signed_bin.c | 16 -- libtommath/bn_mp_to_signed_bin_n.c | 15 -- libtommath/bn_mp_to_ubin.c | 50 ++++++ libtommath/bn_mp_to_unsigned_bin.c | 33 ---- libtommath/bn_mp_to_unsigned_bin_n.c | 15 -- libtommath/bn_mp_toradix.c | 60 ------- libtommath/bn_mp_toradix_n.c | 73 -------- libtommath/bn_mp_ubin_size.c | 12 ++ libtommath/bn_mp_unsigned_bin_size.c | 12 -- libtommath/bn_prime_tab.c | 2 +- libtommath/bn_s_mp_balance_mul.c | 2 + libtommath/bn_s_mp_exptmod.c | 88 +++------ libtommath/bn_s_mp_exptmod_fast.c | 146 +++++---------- libtommath/bn_s_mp_invmod_fast.c | 66 ++----- libtommath/bn_s_mp_invmod_slow.c | 90 +++------- libtommath/bn_s_mp_mul_high_digs.c | 5 +- libtommath/bn_s_mp_rand_platform.c | 63 +++---- libtommath/bn_s_mp_reverse.c | 8 +- libtommath/bn_s_mp_toom_mul.c | 228 +++++++++--------------- libtommath/bn_s_mp_toom_sqr.c | 116 ++++-------- libtommath/helper.pl | 2 +- libtommath/libtommath_VS2008.vcproj | 44 ++--- libtommath/makefile | 70 ++++---- libtommath/makefile.mingw | 55 +++--- libtommath/makefile.msvc | 57 +++--- libtommath/makefile.shared | 72 ++++---- libtommath/makefile.unix | 53 +++--- libtommath/makefile_include.mk | 45 ++--- libtommath/tommath.def | 17 +- libtommath/tommath.h | 57 ++++-- libtommath/tommath_class.h | 109 +++++------ libtommath/tommath_private.h | 9 +- libtommath/tommath_superclass.h | 81 ++++++--- 72 files changed, 1241 insertions(+), 1751 deletions(-) create mode 100644 libtommath/bn_mp_from_sbin.c create mode 100644 libtommath/bn_mp_from_ubin.c delete mode 100644 libtommath/bn_mp_read_signed_bin.c delete mode 100644 libtommath/bn_mp_read_unsigned_bin.c create mode 100644 libtommath/bn_mp_sbin_size.c delete mode 100644 libtommath/bn_mp_signed_bin_size.c create mode 100644 libtommath/bn_mp_to_radix.c create mode 100644 libtommath/bn_mp_to_sbin.c delete mode 100644 libtommath/bn_mp_to_signed_bin.c delete mode 100644 libtommath/bn_mp_to_signed_bin_n.c create mode 100644 libtommath/bn_mp_to_ubin.c delete mode 100644 libtommath/bn_mp_to_unsigned_bin.c delete mode 100644 libtommath/bn_mp_to_unsigned_bin_n.c delete mode 100644 libtommath/bn_mp_toradix.c delete mode 100644 libtommath/bn_mp_toradix_n.c create mode 100644 libtommath/bn_mp_ubin_size.c delete mode 100644 libtommath/bn_mp_unsigned_bin_size.c diff --git a/libtommath/README.md b/libtommath/README.md index 70e015d..be5b207 100644 --- a/libtommath/README.md +++ b/libtommath/README.md @@ -24,15 +24,20 @@ API/ABI changes: [check here](https://abi-laboratory.pro/tracker/timeline/libtom The `develop` branch contains the in-development version. Stable releases are tagged. -Documentation is built from the LaTeX file `bn.tex`. There is also limited documentation in `tommath.h`. There is also a document, `tommath.pdf`, which describes the goals of the project and many of the algorithms used. +Documentation is built from the LaTeX file `bn.tex`. There is also limited documentation in `tommath.h`. +There is also a document, `tommath.pdf`, which describes the goals of the project and many of the algorithms used. -The project can be build by using `make`. Along with the usual `make`, `make clean` and `make install`, there are several other build targets, see the makefile for details. There are also makefiles for certain specific platforms. +The project can be build by using `make`. Along with the usual `make`, `make clean` and `make install`, +there are several other build targets, see the makefile for details. +There are also makefiles for certain specific platforms. ## Testing Tests are located in `demo/` and can be built in two flavors. -* `make test` creates a test binary that is intended to be run against `mtest`. `mtest` can be built with `make mtest` and test execution is done like `./mtest/mtest | ./test`. `mtest` is creating test vectors using an alternative MPI library and `test` is consuming these vectors to verify correct behavior of ltm -* `make test_standalone` creates a stand-alone test binary that executes several test routines. +* `make test` creates a stand-alone test binary that executes several test routines. +* `make mtest_opponent` creates a test binary that is intended to be run against `mtest`. + `mtest` can be built with `make mtest` and test execution is done like `./mtest/mtest | ./mtest_opponent`. + `mtest` is creating test vectors using an alternative MPI library and `test` is consuming these vectors to verify correct behavior of ltm ## Building and Installing diff --git a/libtommath/bn_deprecated.c b/libtommath/bn_deprecated.c index 4beafe6..19b0e49 100644 --- a/libtommath/bn_deprecated.c +++ b/libtommath/bn_deprecated.c @@ -229,4 +229,88 @@ mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c) return mp_root_u32(a, (uint32_t)b, c); } #endif + +#ifdef BN_MP_UNSIGNED_BIN_SIZE_C +int mp_unsigned_bin_size(const mp_int *a) +{ + return (int)mp_ubin_size(a); +} +#endif + +#ifdef BN_MP_READ_UNSIGNED_BIN_C +mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) +{ + return mp_from_ubin(a, b, (size_t) c); +} +#endif + +#ifdef BN_MP_TO_UNSIGNED_BIN_C +mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b) +{ + return mp_to_ubin(a, b, SIZE_MAX, NULL); +} +#endif + +#ifdef BN_MP_TO_UNSIGNED_BIN_N_C +mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) +{ + if (*outlen < (unsigned long)mp_ubin_size(a)) { + return MP_VAL; + } + /* TODO: or use "outlen" instead of NULL? */ + *outlen = (unsigned long)mp_ubin_size(a); + return mp_to_ubin(a, b, (size_t)(*outlen), NULL); +} +#endif + +#ifdef BN_MP_SIGNED_BIN_SIZE_C +int mp_signed_bin_size(const mp_int *a) +{ + return (int)mp_sbin_size(a); +} +#endif + +#ifdef BN_MP_READ_SIGNED_BIN_C +mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) +{ + return mp_from_sbin(a, b, (size_t) c); +} +#endif + +#ifdef BN_MP_TO_SIGNED_BIN_C +mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b) +{ + return mp_to_sbin(a, b, SIZE_MAX, NULL); +} +#endif + +#ifdef BN_MP_TO_SIGNED_BIN_N_C +mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) +{ + if (*outlen < (unsigned long)mp_sbin_size(a)) { + return MP_VAL; + } + *outlen = (unsigned long)mp_sbin_size(a); + return mp_to_sbin(a, b, (size_t)(*outlen), NULL); +} +#endif + + + + +#ifdef BN_MP_TORADIX_N_C +mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) +{ + if (maxlen < 0) { + return MP_VAL; + } + return mp_to_radix(a, str, (size_t)maxlen, radix); +} +#endif +#ifdef BN_MP_TORADIX_C +mp_err mp_toradix(const mp_int *a, char *str, int radix) +{ + return mp_to_radix(a, str, SIZE_MAX, radix); +} +#endif #endif diff --git a/libtommath/bn_mp_addmod.c b/libtommath/bn_mp_addmod.c index 2636e2a..1dcfb67 100644 --- a/libtommath/bn_mp_addmod.c +++ b/libtommath/bn_mp_addmod.c @@ -14,10 +14,11 @@ mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) } if ((err = mp_add(a, b, &t)) != MP_OKAY) { - mp_clear(&t); - return err; + goto LBL_ERR; } err = mp_mod(&t, c, d); + +LBL_ERR: mp_clear(&t); return err; } diff --git a/libtommath/bn_mp_copy.c b/libtommath/bn_mp_copy.c index 141dd0e..e72fcf6 100644 --- a/libtommath/bn_mp_copy.c +++ b/libtommath/bn_mp_copy.c @@ -7,6 +7,7 @@ mp_err mp_copy(const mp_int *a, mp_int *b) { int n; + mp_digit *tmpa, *tmpb; mp_err err; /* if dst == src do nothing */ @@ -22,26 +23,22 @@ mp_err mp_copy(const mp_int *a, mp_int *b) } /* zero b and copy the parameters over */ - { - mp_digit *tmpa, *tmpb; + /* pointer aliases */ - /* pointer aliases */ + /* source */ + tmpa = a->dp; - /* source */ - tmpa = a->dp; + /* destination */ + tmpb = b->dp; - /* destination */ - tmpb = b->dp; - - /* copy all the digits */ - for (n = 0; n < a->used; n++) { - *tmpb++ = *tmpa++; - } - - /* clear high digits */ - MP_ZERO_DIGITS(tmpb, b->used - n); + /* copy all the digits */ + for (n = 0; n < a->used; n++) { + *tmpb++ = *tmpa++; } + /* clear high digits */ + MP_ZERO_DIGITS(tmpb, b->used - n); + /* copy used count and sign */ b->used = a->used; b->sign = a->sign; diff --git a/libtommath/bn_mp_div.c b/libtommath/bn_mp_div.c index fad3d2a..71de55b 100644 --- a/libtommath/bn_mp_div.c +++ b/libtommath/bn_mp_div.c @@ -38,24 +38,18 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) mp_set(&tq, 1uL); n = mp_count_bits(a) - mp_count_bits(b); - if (((err = mp_abs(a, &ta)) != MP_OKAY) || - ((err = mp_abs(b, &tb)) != MP_OKAY) || - ((err = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) || - ((err = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) { - goto LBL_ERR; - } + if ((err = mp_abs(a, &ta)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_abs(b, &tb)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_mul_2d(&tq, n, &tq)) != MP_OKAY) goto LBL_ERR; while (n-- >= 0) { if (mp_cmp(&tb, &ta) != MP_GT) { - if (((err = mp_sub(&ta, &tb, &ta)) != MP_OKAY) || - ((err = mp_add(&q, &tq, &q)) != MP_OKAY)) { - goto LBL_ERR; - } - } - if (((err = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) || - ((err = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) { - goto LBL_ERR; + if ((err = mp_sub(&ta, &tb, &ta)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_add(&q, &tq, &q)) != MP_OKAY) goto LBL_ERR; } + if ((err = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY) goto LBL_ERR; } /* now q == quotient and ta == remainder */ @@ -119,21 +113,13 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) } q.used = a->used + 2; - if ((err = mp_init(&t1)) != MP_OKAY) { - goto LBL_Q; - } + if ((err = mp_init(&t1)) != MP_OKAY) goto LBL_Q; - if ((err = mp_init(&t2)) != MP_OKAY) { - goto LBL_T1; - } + if ((err = mp_init(&t2)) != MP_OKAY) goto LBL_T1; - if ((err = mp_init_copy(&x, a)) != MP_OKAY) { - goto LBL_T2; - } + if ((err = mp_init_copy(&x, a)) != MP_OKAY) goto LBL_T2; - if ((err = mp_init_copy(&y, b)) != MP_OKAY) { - goto LBL_X; - } + if ((err = mp_init_copy(&y, b)) != MP_OKAY) goto LBL_X; /* fix the sign */ neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; @@ -143,12 +129,8 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) norm = mp_count_bits(&y) % MP_DIGIT_BIT; if (norm < (MP_DIGIT_BIT - 1)) { norm = (MP_DIGIT_BIT - 1) - norm; - if ((err = mp_mul_2d(&x, norm, &x)) != MP_OKAY) { - goto LBL_Y; - } - if ((err = mp_mul_2d(&y, norm, &y)) != MP_OKAY) { - goto LBL_Y; - } + if ((err = mp_mul_2d(&x, norm, &x)) != MP_OKAY) goto LBL_Y; + if ((err = mp_mul_2d(&y, norm, &y)) != MP_OKAY) goto LBL_Y; } else { norm = 0; } @@ -158,15 +140,12 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) t = y.used - 1; /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */ - if ((err = mp_lshd(&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */ - goto LBL_Y; - } + /* y = y*b**{n-t} */ + if ((err = mp_lshd(&y, n - t)) != MP_OKAY) goto LBL_Y; while (mp_cmp(&x, &y) != MP_LT) { ++(q.dp[n - t]); - if ((err = mp_sub(&x, &y, &x)) != MP_OKAY) { - goto LBL_Y; - } + if ((err = mp_sub(&x, &y, &x)) != MP_OKAY) goto LBL_Y; } /* reset y by shifting it back down */ @@ -207,9 +186,7 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) t1.dp[0] = ((t - 1) < 0) ? 0u : y.dp[t - 1]; t1.dp[1] = y.dp[t]; t1.used = 2; - if ((err = mp_mul_d(&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) { - goto LBL_Y; - } + if ((err = mp_mul_d(&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) goto LBL_Y; /* find right hand */ t2.dp[0] = ((i - 2) < 0) ? 0u : x.dp[i - 2]; @@ -219,29 +196,17 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) } while (mp_cmp_mag(&t1, &t2) == MP_GT); /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */ - if ((err = mp_mul_d(&y, q.dp[(i - t) - 1], &t1)) != MP_OKAY) { - goto LBL_Y; - } + if ((err = mp_mul_d(&y, q.dp[(i - t) - 1], &t1)) != MP_OKAY) goto LBL_Y; - if ((err = mp_lshd(&t1, (i - t) - 1)) != MP_OKAY) { - goto LBL_Y; - } + if ((err = mp_lshd(&t1, (i - t) - 1)) != MP_OKAY) goto LBL_Y; - if ((err = mp_sub(&x, &t1, &x)) != MP_OKAY) { - goto LBL_Y; - } + if ((err = mp_sub(&x, &t1, &x)) != MP_OKAY) goto LBL_Y; /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */ if (x.sign == MP_NEG) { - if ((err = mp_copy(&y, &t1)) != MP_OKAY) { - goto LBL_Y; - } - if ((err = mp_lshd(&t1, (i - t) - 1)) != MP_OKAY) { - goto LBL_Y; - } - if ((err = mp_add(&x, &t1, &x)) != MP_OKAY) { - goto LBL_Y; - } + if ((err = mp_copy(&y, &t1)) != MP_OKAY) goto LBL_Y; + if ((err = mp_lshd(&t1, (i - t) - 1)) != MP_OKAY) goto LBL_Y; + if ((err = mp_add(&x, &t1, &x)) != MP_OKAY) goto LBL_Y; q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1uL) & MP_MASK; } @@ -261,9 +226,7 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) } if (d != NULL) { - if ((err = mp_div_2d(&x, norm, &x, NULL)) != MP_OKAY) { - goto LBL_Y; - } + if ((err = mp_div_2d(&x, norm, &x, NULL)) != MP_OKAY) goto LBL_Y; mp_exch(&x, d); } diff --git a/libtommath/bn_mp_div_2.c b/libtommath/bn_mp_div_2.c index 2561e5a..f56ea81 100644 --- a/libtommath/bn_mp_div_2.c +++ b/libtommath/bn_mp_div_2.c @@ -7,6 +7,7 @@ mp_err mp_div_2(const mp_int *a, mp_int *b) { int x, oldused; + mp_digit r, rr, *tmpa, *tmpb; mp_err err; /* copy */ @@ -18,31 +19,29 @@ mp_err mp_div_2(const mp_int *a, mp_int *b) oldused = b->used; b->used = a->used; - { - mp_digit r, rr, *tmpa, *tmpb; - /* source alias */ - tmpa = a->dp + b->used - 1; + /* source alias */ + tmpa = a->dp + b->used - 1; - /* dest alias */ - tmpb = b->dp + b->used - 1; + /* dest alias */ + tmpb = b->dp + b->used - 1; - /* carry */ - r = 0; - for (x = b->used - 1; x >= 0; x--) { - /* get the carry for the next iteration */ - rr = *tmpa & 1u; + /* carry */ + r = 0; + for (x = b->used - 1; x >= 0; x--) { + /* get the carry for the next iteration */ + rr = *tmpa & 1u; - /* shift the current digit, add in carry and store */ - *tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1)); + /* shift the current digit, add in carry and store */ + *tmpb-- = (*tmpa-- >> 1) | (r << (MP_DIGIT_BIT - 1)); - /* forward carry to next iteration */ - r = rr; - } - - /* zero excess digits */ - MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used); + /* forward carry to next iteration */ + r = rr; } + + /* zero excess digits */ + MP_ZERO_DIGITS(b->dp + b->used, oldused - b->used); + b->sign = a->sign; mp_clamp(b); return MP_OKAY; diff --git a/libtommath/bn_mp_div_d.c b/libtommath/bn_mp_div_d.c index 569f594..b9d718b 100644 --- a/libtommath/bn_mp_div_d.c +++ b/libtommath/bn_mp_div_d.c @@ -29,7 +29,7 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) } /* power of two ? */ - if ((b & (b-1)) == 0u) { + if ((b & (b - 1u)) == 0u) { ix = 1; while ((ix < MP_DIGIT_BIT) && (b != (((mp_digit)1)<used)) != MP_OKAY) { diff --git a/libtommath/bn_mp_export.c b/libtommath/bn_mp_export.c index c9de48b..9dea54e 100644 --- a/libtommath/bn_mp_export.c +++ b/libtommath/bn_mp_export.c @@ -53,19 +53,19 @@ mp_err mp_export(void *rop, size_t *countp, int order, size_t size, *byte = (unsigned char)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL)); if ((err = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) { - mp_clear(&t); - return err; + goto LBL_ERR; } } } - mp_clear(&t); - if (countp != NULL) { *countp = count; } + err = MP_OKAY; - return MP_OKAY; +LBL_ERR: + mp_clear(&t); + return err; } #endif diff --git a/libtommath/bn_mp_expt_u32.c b/libtommath/bn_mp_expt_u32.c index 4ec725e..2ab67ba 100644 --- a/libtommath/bn_mp_expt_u32.c +++ b/libtommath/bn_mp_expt_u32.c @@ -21,16 +21,14 @@ mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) /* if the bit is set multiply */ if ((b & 1u) != 0u) { if ((err = mp_mul(c, &g, c)) != MP_OKAY) { - mp_clear(&g); - return err; + goto LBL_ERR; } } /* square */ if (b > 1u) { if ((err = mp_sqr(&g, &g)) != MP_OKAY) { - mp_clear(&g); - return err; + goto LBL_ERR; } } @@ -38,8 +36,11 @@ mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) b >>= 1; } + err = MP_OKAY; + +LBL_ERR: mp_clear(&g); - return MP_OKAY; + return err; } #endif diff --git a/libtommath/bn_mp_exptmod.c b/libtommath/bn_mp_exptmod.c index 95a1dcb..5f811eb 100644 --- a/libtommath/bn_mp_exptmod.c +++ b/libtommath/bn_mp_exptmod.c @@ -19,77 +19,58 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) /* if exponent X is negative we have to recurse */ if (X->sign == MP_NEG) { -#ifdef BN_MP_INVMOD_C mp_int tmpG, tmpX; mp_err err; - /* first compute 1/G mod P */ - if ((err = mp_init(&tmpG)) != MP_OKAY) { + if (!MP_HAS(MP_INVMOD)) { + return MP_VAL; + } + + if ((err = mp_init_multi(&tmpG, &tmpX, NULL)) != MP_OKAY) { return err; } + + /* first compute 1/G mod P */ if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) { - mp_clear(&tmpG); - return err; + goto LBL_ERR; } /* now get |X| */ - if ((err = mp_init(&tmpX)) != MP_OKAY) { - mp_clear(&tmpG); - return err; - } if ((err = mp_abs(X, &tmpX)) != MP_OKAY) { - mp_clear_multi(&tmpG, &tmpX, NULL); - return err; + goto LBL_ERR; } /* and now compute (1/G)**|X| instead of G**X [X < 0] */ err = mp_exptmod(&tmpG, &tmpX, P, Y); +LBL_ERR: mp_clear_multi(&tmpG, &tmpX, NULL); return err; -#else - /* no invmod */ - return MP_VAL; -#endif } /* modified diminished radix reduction */ -#if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && defined(BN_S_MP_EXPTMOD_C) - if (mp_reduce_is_2k_l(P) == MP_YES) { + if (MP_HAS(MP_REDUCE_IS_2K_L) && MP_HAS(MP_REDUCE_2K_L) && MP_HAS(S_MP_EXPTMOD) && + (mp_reduce_is_2k_l(P) == MP_YES)) { return s_mp_exptmod(G, X, P, Y, 1); } -#endif -#ifdef BN_MP_DR_IS_MODULUS_C - /* is it a DR modulus? */ - dr = (mp_dr_is_modulus(P) == MP_YES) ? 1 : 0; -#else - /* default to no */ - dr = 0; -#endif + /* is it a DR modulus? default to no */ + dr = (MP_HAS(MP_DR_IS_MODULUS) && (mp_dr_is_modulus(P) == MP_YES)) ? 1 : 0; -#ifdef BN_MP_REDUCE_IS_2K_C /* if not, is it a unrestricted DR modulus? */ - if (dr == 0) { + if (MP_HAS(MP_REDUCE_IS_2K) && (dr == 0)) { dr = (mp_reduce_is_2k(P) == MP_YES) ? 2 : 0; } -#endif /* if the modulus is odd or dr != 0 use the montgomery method */ -#ifdef BN_S_MP_EXPTMOD_FAST_C - if (MP_IS_ODD(P) || (dr != 0)) { + if (MP_HAS(S_MP_EXPTMOD_FAST) && (MP_IS_ODD(P) || (dr != 0))) { return s_mp_exptmod_fast(G, X, P, Y, dr); - } else { -#endif -#ifdef BN_S_MP_EXPTMOD_C + } else if (MP_HAS(S_MP_EXPTMOD)) { /* otherwise use the generic Barrett reduction technique */ return s_mp_exptmod(G, X, P, Y, 0); -#else + } else { /* no exptmod for evens */ return MP_VAL; -#endif -#ifdef BN_S_MP_EXPTMOD_FAST_C } -#endif } #endif diff --git a/libtommath/bn_mp_exteuclid.c b/libtommath/bn_mp_exteuclid.c index 3af7467..faf47ba 100644 --- a/libtommath/bn_mp_exteuclid.c +++ b/libtommath/bn_mp_exteuclid.c @@ -17,77 +17,41 @@ mp_err mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp /* initialize, (u1,u2,u3) = (1,0,a) */ mp_set(&u1, 1uL); - if ((err = mp_copy(a, &u3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_copy(a, &u3)) != MP_OKAY) goto LBL_ERR; /* initialize, (v1,v2,v3) = (0,1,b) */ mp_set(&v2, 1uL); - if ((err = mp_copy(b, &v3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_copy(b, &v3)) != MP_OKAY) goto LBL_ERR; /* loop while v3 != 0 */ while (!MP_IS_ZERO(&v3)) { /* q = u3/v3 */ - if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) goto LBL_ERR; /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */ - if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) goto LBL_ERR; /* (u1,u2,u3) = (v1,v2,v3) */ - if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_copy(&v1, &u1)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_copy(&v2, &u2)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_copy(&v3, &u3)) != MP_OKAY) goto LBL_ERR; /* (v1,v2,v3) = (t1,t2,t3) */ - if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_copy(&t1, &v1)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_copy(&t2, &v2)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_copy(&t3, &v3)) != MP_OKAY) goto LBL_ERR; } /* make sure U3 >= 0 */ if (u3.sign == MP_NEG) { - if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_neg(&u3, &u3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_neg(&u1, &u1)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_neg(&u2, &u2)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_neg(&u3, &u3)) != MP_OKAY) goto LBL_ERR; } /* copy result out */ diff --git a/libtommath/bn_mp_from_sbin.c b/libtommath/bn_mp_from_sbin.c new file mode 100644 index 0000000..20e4597 --- /dev/null +++ b/libtommath/bn_mp_from_sbin.c @@ -0,0 +1,25 @@ +#include "tommath_private.h" +#ifdef BN_MP_FROM_SBIN_C +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +/* read signed bin, big endian, first byte is 0==positive or 1==negative */ +mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) +{ + mp_err err; + + /* read magnitude */ + if ((err = mp_from_ubin(a, buf + 1, size - 1u)) != MP_OKAY) { + return err; + } + + /* first byte is 0 for positive, non-zero for negative */ + if (buf[0] == (unsigned char)0) { + a->sign = MP_ZPOS; + } else { + a->sign = MP_NEG; + } + + return MP_OKAY; +} +#endif diff --git a/libtommath/bn_mp_from_ubin.c b/libtommath/bn_mp_from_ubin.c new file mode 100644 index 0000000..7f73cbc --- /dev/null +++ b/libtommath/bn_mp_from_ubin.c @@ -0,0 +1,39 @@ +#include "tommath_private.h" +#ifdef BN_MP_FROM_UBIN_C +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +/* reads a unsigned char array, assumes the msb is stored first [big endian] */ +mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) +{ + mp_err err; + + /* make sure there are at least two digits */ + if (a->alloc < 2) { + if ((err = mp_grow(a, 2)) != MP_OKAY) { + return err; + } + } + + /* zero the int */ + mp_zero(a); + + /* read the bytes in */ + while (size-- > 0u) { + if ((err = mp_mul_2d(a, 8, a)) != MP_OKAY) { + return err; + } + +#ifndef MP_8BIT + a->dp[0] |= *buf++; + a->used += 1; +#else + a->dp[0] = (*buf & MP_MASK); + a->dp[1] |= ((*buf++ >> 7) & 1u); + a->used += 2; +#endif + } + mp_clamp(a); + return MP_OKAY; +} +#endif diff --git a/libtommath/bn_mp_fwrite.c b/libtommath/bn_mp_fwrite.c index 2a59755..eaa5d06 100644 --- a/libtommath/bn_mp_fwrite.c +++ b/libtommath/bn_mp_fwrite.c @@ -19,18 +19,19 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) return MP_MEM; } - if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) { - MP_FREE_BUFFER(buf, (size_t)len); - return err; + if ((err = mp_to_radix(a, buf, (size_t)len, radix)) != MP_OKAY) { + goto LBL_ERR; } if (fwrite(buf, (size_t)len, 1uL, stream) != 1uL) { - MP_FREE_BUFFER(buf, (size_t)len); - return MP_ERR; + err = MP_ERR; + goto LBL_ERR; } + err = MP_OKAY; +LBL_ERR: MP_FREE_BUFFER(buf, (size_t)len); - return MP_OKAY; + return err; } #endif diff --git a/libtommath/bn_mp_ilogb.c b/libtommath/bn_mp_ilogb.c index 3d23d38..3185ca6 100644 --- a/libtommath/bn_mp_ilogb.c +++ b/libtommath/bn_mp_ilogb.c @@ -140,11 +140,7 @@ mp_err mp_ilogb(const mp_int *a, uint32_t base, mp_int *c) while ((high - low) > 1u) { mid = (high + low) >> 1; - /* Difference can be larger then the type behind mp_digit can hold */ - if ((mid - low) > (unsigned int)(MP_MASK)) { - err = MP_VAL; - goto LBL_ERR; - } + if ((err = mp_expt_u32(&bi_base, (uint32_t)(mid - low), &t)) != MP_OKAY) { goto LBL_ERR; } diff --git a/libtommath/bn_mp_incr.c b/libtommath/bn_mp_incr.c index 5d0039e..7695ac7 100644 --- a/libtommath/bn_mp_incr.c +++ b/libtommath/bn_mp_incr.c @@ -20,7 +20,7 @@ mp_err mp_incr(mp_int *a) a->sign = MP_NEG; } return MP_OKAY; - } else if (a->dp[0] < MP_MASK) { + } else if (a->dp[0] < MP_DIGIT_MAX) { a->dp[0]++; return MP_OKAY; } else { diff --git a/libtommath/bn_mp_invmod.c b/libtommath/bn_mp_invmod.c index d5d6bbc..7b35a24 100644 --- a/libtommath/bn_mp_invmod.c +++ b/libtommath/bn_mp_invmod.c @@ -11,17 +11,13 @@ mp_err mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) return MP_VAL; } -#ifdef BN_S_MP_INVMOD_FAST_C /* if the modulus is odd we can use a faster routine instead */ - if (MP_IS_ODD(b)) { + if (MP_HAS(S_MP_INVMOD_FAST) && MP_IS_ODD(b)) { return s_mp_invmod_fast(a, b, c); } -#endif -#ifdef BN_S_MP_INVMOD_SLOW_C - return s_mp_invmod_slow(a, b, c); -#else - return MP_VAL; -#endif + return MP_HAS(S_MP_INVMOD_SLOW) + ? s_mp_invmod_slow(a, b, c) + : MP_VAL; } #endif diff --git a/libtommath/bn_mp_mod.c b/libtommath/bn_mp_mod.c index 18b4e6b..8fbfe08 100644 --- a/libtommath/bn_mp_mod.c +++ b/libtommath/bn_mp_mod.c @@ -14,8 +14,7 @@ mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c) } if ((err = mp_div(a, b, NULL, &t)) != MP_OKAY) { - mp_clear(&t); - return err; + goto LBL_ERR; } if (MP_IS_ZERO(&t) || (t.sign == b->sign)) { @@ -25,6 +24,7 @@ mp_err mp_mod(const mp_int *a, const mp_int *b, mp_int *c) err = mp_add(b, &t, c); } +LBL_ERR: mp_clear(&t); return err; } diff --git a/libtommath/bn_mp_mul.c b/libtommath/bn_mp_mul.c index f0ca04a..561913a 100644 --- a/libtommath/bn_mp_mul.c +++ b/libtommath/bn_mp_mul.c @@ -6,80 +6,46 @@ /* high level multiplication (handles sign) */ mp_err mp_mul(const mp_int *a, const mp_int *b, mp_int *c) { - mp_err err; - mp_sign neg; -#ifdef BN_S_MP_BALANCE_MUL_C - int len_b, len_a; -#endif - neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; -#ifdef BN_S_MP_BALANCE_MUL_C - len_a = a->used; - len_b = b->used; - - if (len_a == len_b) { - goto GO_ON; - } - /* - * Check sizes. The smaller one needs to be larger than the Karatsuba cut-off. - * The bigger one needs to be at least about one KARATSUBA_MUL_CUTOFF bigger - * to make some sense, but it depends on architecture, OS, position of the - * stars... so YMMV. - * Using it to cut the input into slices small enough for fast_s_mp_mul_digs - * was actually slower on the author's machine, but YMMV. - */ - if ((MP_MIN(len_a, len_b) < MP_KARATSUBA_MUL_CUTOFF) - || ((MP_MAX(len_a, len_b) / 2) < MP_KARATSUBA_MUL_CUTOFF)) { - goto GO_ON; - } - /* - * Not much effect was observed below a ratio of 1:2, but again: YMMV. - */ - if ((MP_MAX(len_a, len_b) / MP_MIN(len_a, len_b)) < 2) { - goto GO_ON; - } - - err = s_mp_balance_mul(a,b,c); - goto END; - -GO_ON: -#endif + mp_err err; + int min_len = MP_MIN(a->used, b->used), + max_len = MP_MAX(a->used, b->used), + digs = a->used + b->used + 1; + mp_sign neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG; - /* use Toom-Cook? */ -#ifdef BN_S_MP_TOOM_MUL_C - if (MP_MIN(a->used, b->used) >= MP_TOOM_MUL_CUTOFF) { + if (MP_HAS(S_MP_BALANCE_MUL) && + /* Check sizes. The smaller one needs to be larger than the Karatsuba cut-off. + * The bigger one needs to be at least about one MP_KARATSUBA_MUL_CUTOFF bigger + * to make some sense, but it depends on architecture, OS, position of the + * stars... so YMMV. + * Using it to cut the input into slices small enough for fast_s_mp_mul_digs + * was actually slower on the author's machine, but YMMV. + */ + (min_len >= MP_KARATSUBA_MUL_CUTOFF) && + ((max_len / 2) >= MP_KARATSUBA_MUL_CUTOFF) && + /* Not much effect was observed below a ratio of 1:2, but again: YMMV. */ + (max_len >= (2 * min_len))) { + err = s_mp_balance_mul(a,b,c); + } else if (MP_HAS(S_MP_TOOM_MUL) && + (min_len >= MP_TOOM_MUL_CUTOFF)) { err = s_mp_toom_mul(a, b, c); - } else -#endif -#ifdef BN_S_MP_KARATSUBA_MUL_C - /* use Karatsuba? */ - if (MP_MIN(a->used, b->used) >= MP_KARATSUBA_MUL_CUTOFF) { - err = s_mp_karatsuba_mul(a, b, c); - } else -#endif - { - /* can we use the fast multiplier? - * - * The fast multiplier can be used if the output will - * have less than MP_WARRAY digits and the number of - * digits won't affect carry propagation - */ - int digs = a->used + b->used + 1; - -#ifdef BN_S_MP_MUL_DIGS_FAST_C - if ((digs < MP_WARRAY) && - (MP_MIN(a->used, b->used) <= MP_MAXFAST)) { - err = s_mp_mul_digs_fast(a, b, c, digs); - } else -#endif - { -#ifdef BN_S_MP_MUL_DIGS_C - err = s_mp_mul_digs(a, b, c, a->used + b->used + 1); -#else - err = MP_VAL; -#endif - } - } -END: + } else if (MP_HAS(S_MP_KARATSUBA_MUL) && + (min_len >= MP_KARATSUBA_MUL_CUTOFF)) { + err = s_mp_karatsuba_mul(a, b, c); + } else if (MP_HAS(S_MP_MUL_DIGS_FAST) && + /* can we use the fast multiplier? + * + * The fast multiplier can be used if the output will + * have less than MP_WARRAY digits and the number of + * digits won't affect carry propagation + */ + (digs < MP_WARRAY) && + (min_len <= MP_MAXFAST)) { + err = s_mp_mul_digs_fast(a, b, c, digs); + } else if (MP_HAS(S_MP_MUL_DIGS)) { + err = s_mp_mul_digs(a, b, c, digs); + } else { + err = MP_VAL; + } c->sign = (c->used > 0) ? neg : MP_ZPOS; return err; } diff --git a/libtommath/bn_mp_mulmod.c b/libtommath/bn_mp_mulmod.c index 763f923..160d162 100644 --- a/libtommath/bn_mp_mulmod.c +++ b/libtommath/bn_mp_mulmod.c @@ -14,10 +14,11 @@ mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) } if ((err = mp_mul(a, b, &t)) != MP_OKAY) { - mp_clear(&t); - return err; + goto LBL_ERR; } err = mp_mod(&t, c, d); + +LBL_ERR: mp_clear(&t); return err; } diff --git a/libtommath/bn_mp_prime_frobenius_underwood.c b/libtommath/bn_mp_prime_frobenius_underwood.c index e2f9f1f..a7a943a 100644 --- a/libtommath/bn_mp_prime_frobenius_underwood.c +++ b/libtommath/bn_mp_prime_frobenius_underwood.c @@ -45,17 +45,11 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) /* (32764^2 - 4) < 2^31, no bigint for >MP_8BIT needed) */ mp_set_u32(&T1z, (uint32_t)a); - if ((err = mp_sqr(&T1z, &T1z)) != MP_OKAY) { - goto LBL_FU_ERR; - } + if ((err = mp_sqr(&T1z, &T1z)) != MP_OKAY) goto LBL_FU_ERR; - if ((err = mp_sub_d(&T1z, 4uL, &T1z)) != MP_OKAY) { - goto LBL_FU_ERR; - } + if ((err = mp_sub_d(&T1z, 4uL, &T1z)) != MP_OKAY) goto LBL_FU_ERR; - if ((err = mp_kronecker(&T1z, N, &j)) != MP_OKAY) { - goto LBL_FU_ERR; - } + if ((err = mp_kronecker(&T1z, N, &j)) != MP_OKAY) goto LBL_FU_ERR; if (j == -1) { break; @@ -74,18 +68,12 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) /* Composite if N and (a+4)*(2*a+5) are not coprime */ mp_set_u32(&T1z, (uint32_t)((a+4)*((2*a)+5))); - if ((err = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) { - goto LBL_FU_ERR; - } + if ((err = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) goto LBL_FU_ERR; - if (!((T1z.used == 1) && (T1z.dp[0] == 1u))) { - goto LBL_FU_ERR; - } + if (!((T1z.used == 1) && (T1z.dp[0] == 1u))) goto LBL_FU_ERR; ap2 = a + 2; - if ((err = mp_add_d(N, 1uL, &Np1z)) != MP_OKAY) { - goto LBL_FU_ERR; - } + if ((err = mp_add_d(N, 1uL, &Np1z)) != MP_OKAY) goto LBL_FU_ERR; mp_set(&sz, 1uL); mp_set(&tz, 2uL); @@ -97,38 +85,20 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) * tz = ((tz-sz)*(tz+sz))%N; * sz = temp; */ - if ((err = mp_mul_2(&tz, &T2z)) != MP_OKAY) { - goto LBL_FU_ERR; - } + if ((err = mp_mul_2(&tz, &T2z)) != MP_OKAY) goto LBL_FU_ERR; /* a = 0 at about 50% of the cases (non-square and odd input) */ if (a != 0) { - if ((err = mp_mul_d(&sz, (mp_digit)a, &T1z)) != MP_OKAY) { - goto LBL_FU_ERR; - } - if ((err = mp_add(&T1z, &T2z, &T2z)) != MP_OKAY) { - goto LBL_FU_ERR; - } + if ((err = mp_mul_d(&sz, (mp_digit)a, &T1z)) != MP_OKAY) goto LBL_FU_ERR; + if ((err = mp_add(&T1z, &T2z, &T2z)) != MP_OKAY) goto LBL_FU_ERR; } - if ((err = mp_mul(&T2z, &sz, &T1z)) != MP_OKAY) { - goto LBL_FU_ERR; - } - if ((err = mp_sub(&tz, &sz, &T2z)) != MP_OKAY) { - goto LBL_FU_ERR; - } - if ((err = mp_add(&sz, &tz, &sz)) != MP_OKAY) { - goto LBL_FU_ERR; - } - if ((err = mp_mul(&sz, &T2z, &tz)) != MP_OKAY) { - goto LBL_FU_ERR; - } - if ((err = mp_mod(&tz, N, &tz)) != MP_OKAY) { - goto LBL_FU_ERR; - } - if ((err = mp_mod(&T1z, N, &sz)) != MP_OKAY) { - goto LBL_FU_ERR; - } + if ((err = mp_mul(&T2z, &sz, &T1z)) != MP_OKAY) goto LBL_FU_ERR; + if ((err = mp_sub(&tz, &sz, &T2z)) != MP_OKAY) goto LBL_FU_ERR; + if ((err = mp_add(&sz, &tz, &sz)) != MP_OKAY) goto LBL_FU_ERR; + if ((err = mp_mul(&sz, &T2z, &tz)) != MP_OKAY) goto LBL_FU_ERR; + if ((err = mp_mod(&tz, N, &tz)) != MP_OKAY) goto LBL_FU_ERR; + if ((err = mp_mod(&T1z, N, &sz)) != MP_OKAY) goto LBL_FU_ERR; if (s_mp_get_bit(&Np1z, (unsigned int)i) == MP_YES) { /* * temp = (a+2) * sz + tz @@ -136,34 +106,21 @@ mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result) * sz = temp */ if (a == 0) { - if ((err = mp_mul_2(&sz, &T1z)) != MP_OKAY) { - goto LBL_FU_ERR; - } + if ((err = mp_mul_2(&sz, &T1z)) != MP_OKAY) goto LBL_FU_ERR; } else { - if ((err = mp_mul_d(&sz, (mp_digit)ap2, &T1z)) != MP_OKAY) { - goto LBL_FU_ERR; - } - } - if ((err = mp_add(&T1z, &tz, &T1z)) != MP_OKAY) { - goto LBL_FU_ERR; - } - if ((err = mp_mul_2(&tz, &T2z)) != MP_OKAY) { - goto LBL_FU_ERR; - } - if ((err = mp_sub(&T2z, &sz, &tz)) != MP_OKAY) { - goto LBL_FU_ERR; + if ((err = mp_mul_d(&sz, (mp_digit)ap2, &T1z)) != MP_OKAY) goto LBL_FU_ERR; } + if ((err = mp_add(&T1z, &tz, &T1z)) != MP_OKAY) goto LBL_FU_ERR; + if ((err = mp_mul_2(&tz, &T2z)) != MP_OKAY) goto LBL_FU_ERR; + if ((err = mp_sub(&T2z, &sz, &tz)) != MP_OKAY) goto LBL_FU_ERR; mp_exch(&sz, &T1z); } } mp_set_u32(&T1z, (uint32_t)((2 * a) + 5)); - if ((err = mp_mod(&T1z, N, &T1z)) != MP_OKAY) { - goto LBL_FU_ERR; - } + if ((err = mp_mod(&T1z, N, &T1z)) != MP_OKAY) goto LBL_FU_ERR; if (MP_IS_ZERO(&sz) && (mp_cmp(&tz, &T1z) == MP_EQ)) { *result = MP_YES; - goto LBL_FU_ERR; } LBL_FU_ERR: diff --git a/libtommath/bn_mp_prime_next_prime.c b/libtommath/bn_mp_prime_next_prime.c index aaa821b..1e971fa 100644 --- a/libtommath/bn_mp_prime_next_prime.c +++ b/libtommath/bn_mp_prime_next_prime.c @@ -10,7 +10,7 @@ */ mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style) { - int x, y; + int x, y, cmp; mp_err err; mp_bool res = MP_NO; mp_digit res_tab[PRIVATE_MP_PRIME_TAB_SIZE], step, kstep; @@ -21,36 +21,22 @@ mp_err mp_prime_next_prime(mp_int *a, int t, int bbs_style) /* simple algo if a is less than the largest prime in the table */ if (mp_cmp_d(a, s_mp_prime_tab[PRIVATE_MP_PRIME_TAB_SIZE-1]) == MP_LT) { - /* find which prime it is bigger than */ - for (x = PRIVATE_MP_PRIME_TAB_SIZE - 2; x >= 0; x--) { - if (mp_cmp_d(a, s_mp_prime_tab[x]) != MP_LT) { - if (bbs_style == 1) { - /* ok we found a prime smaller or - * equal [so the next is larger] - * - * however, the prime must be - * congruent to 3 mod 4 - */ - if ((s_mp_prime_tab[x + 1] & 3u) != 3u) { - /* scan upwards for a prime congruent to 3 mod 4 */ - for (y = x + 1; y < PRIVATE_MP_PRIME_TAB_SIZE; y++) { - if ((s_mp_prime_tab[y] & 3u) == 3u) { - mp_set(a, s_mp_prime_tab[y]); - return MP_OKAY; - } - } - } + /* find which prime it is bigger than "a" */ + for (x = 0; x < PRIVATE_MP_PRIME_TAB_SIZE; x++) { + cmp = mp_cmp_d(a, s_mp_prime_tab[x]); + if (cmp == MP_EQ) { + continue; + } + if (cmp != MP_GT) { + if ((bbs_style == 1) && ((s_mp_prime_tab[x] & 3u) != 3u)) { + /* try again until we get a prime congruent to 3 mod 4 */ + continue; } else { - mp_set(a, s_mp_prime_tab[x + 1]); + mp_set(a, s_mp_prime_tab[x]); return MP_OKAY; } } } - /* at this point a maybe 1 */ - if (mp_cmp_d(a, 1uL) == MP_EQ) { - mp_set(a, 2uL); - return MP_OKAY; - } /* fall through to the sieve */ } diff --git a/libtommath/bn_mp_prime_rand.c b/libtommath/bn_mp_prime_rand.c index 1cfe514..4530e9a 100644 --- a/libtommath/bn_mp_prime_rand.c +++ b/libtommath/bn_mp_prime_rand.c @@ -76,7 +76,8 @@ mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_pr tmp[bsize-1] |= maskOR_lsb; /* read it in */ - if ((err = mp_read_unsigned_bin(a, tmp, bsize)) != MP_OKAY) { + /* TODO: casting only for now until all lengths have been changed to the type "size_t"*/ + if ((err = mp_from_ubin(a, tmp, (size_t)bsize)) != MP_OKAY) { goto error; } diff --git a/libtommath/bn_mp_prime_strong_lucas_selfridge.c b/libtommath/bn_mp_prime_strong_lucas_selfridge.c index 58e59d9..330caaa 100644 --- a/libtommath/bn_mp_prime_strong_lucas_selfridge.c +++ b/libtommath/bn_mp_prime_strong_lucas_selfridge.c @@ -84,9 +84,8 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) Ds = sign * D; sign = -sign; mp_set_u32(&Dz, (uint32_t)D); - if ((err = mp_gcd(a, &Dz, &gcd)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_gcd(a, &Dz, &gcd)) != MP_OKAY) goto LBL_LS_ERR; + /* if 1 < GCD < N then N is composite with factor "D", and Jacobi(D,N) is technically undefined (but often returned as zero). */ @@ -96,9 +95,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) if (Ds < 0) { Dz.sign = MP_NEG; } - if ((err = mp_kronecker(&Dz, a, &J)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_kronecker(&Dz, a, &J)) != MP_OKAY) goto LBL_LS_ERR; if (J == -1) { break; @@ -146,9 +143,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) Baillie-PSW test based on the strong Lucas-Selfridge test should be more reliable. */ - if ((err = mp_add_d(a, 1uL, &Np1)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_add_d(a, 1uL, &Np1)) != MP_OKAY) goto LBL_LS_ERR; s = mp_cnt_lsb(&Np1); /* CZ @@ -158,9 +153,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) * dividing an even number by two does not produce * any leftovers. */ - if ((err = mp_div_2d(&Np1, s, &Dz, NULL)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_div_2d(&Np1, s, &Dz, NULL)) != MP_OKAY) goto LBL_LS_ERR; /* We must now compute U_d and V_d. Since d is odd, the accumulated values U and V are initialized to U_1 and V_1 (if the target index were even, U and V would be initialized instead to U_0=0 @@ -178,9 +171,7 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) mp_set(&V2mz, (mp_digit)P); /* V_1 */ mp_set_i32(&Qmz, Q); - if ((err = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) goto LBL_LS_ERR; /* Initializes calculation of Q^d */ mp_set_i32(&Qkdz, Q); @@ -195,32 +186,19 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) * V_2m = V_m*V_m - 2*Q^m */ - if ((err = mp_mul(&U2mz, &V2mz, &U2mz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_mod(&U2mz, a, &U2mz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_sqr(&V2mz, &V2mz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_sub(&V2mz, &Q2mz, &V2mz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_mod(&V2mz, a, &V2mz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_mul(&U2mz, &V2mz, &U2mz)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_mod(&U2mz, a, &U2mz)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_sqr(&V2mz, &V2mz)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_sub(&V2mz, &Q2mz, &V2mz)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_mod(&V2mz, a, &V2mz)) != MP_OKAY) goto LBL_LS_ERR; + /* Must calculate powers of Q for use in V_2m, also for Q^d later */ - if ((err = mp_sqr(&Qmz, &Qmz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_sqr(&Qmz, &Qmz)) != MP_OKAY) goto LBL_LS_ERR; + /* prevents overflow */ /* CZ still necessary without a fixed prealloc'd mem.? */ - if ((err = mp_mod(&Qmz, a, &Qmz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_mod(&Qmz, a, &Qmz)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) goto LBL_LS_ERR; + if (s_mp_get_bit(&Dz, (unsigned int)u) == MP_YES) { /* Formulas for addition of indices (carried out mod N); * @@ -229,28 +207,14 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) * * Be careful with division by 2 (mod N)! */ - if ((err = mp_mul(&U2mz, &Vz, &T1z)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_mul(&Uz, &V2mz, &T2z)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_mul(&V2mz, &Vz, &T3z)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_mul(&U2mz, &Uz, &T4z)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = s_mp_mul_si(&T4z, Ds, &T4z)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_add(&T1z, &T2z, &Uz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_mul(&U2mz, &Vz, &T1z)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_mul(&Uz, &V2mz, &T2z)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_mul(&V2mz, &Vz, &T3z)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_mul(&U2mz, &Uz, &T4z)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = s_mp_mul_si(&T4z, Ds, &T4z)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_add(&T1z, &T2z, &Uz)) != MP_OKAY) goto LBL_LS_ERR; if (MP_IS_ODD(&Uz)) { - if ((err = mp_add(&Uz, a, &Uz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_add(&Uz, a, &Uz)) != MP_OKAY) goto LBL_LS_ERR; } /* CZ * This should round towards negative infinity because @@ -258,44 +222,25 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) * But mp_div_2() does not do so, it is truncating instead. */ oddness = MP_IS_ODD(&Uz) ? MP_YES : MP_NO; - if ((err = mp_div_2(&Uz, &Uz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_div_2(&Uz, &Uz)) != MP_OKAY) goto LBL_LS_ERR; if ((Uz.sign == MP_NEG) && (oddness != MP_NO)) { - if ((err = mp_sub_d(&Uz, 1uL, &Uz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - } - if ((err = mp_add(&T3z, &T4z, &Vz)) != MP_OKAY) { - goto LBL_LS_ERR; + if ((err = mp_sub_d(&Uz, 1uL, &Uz)) != MP_OKAY) goto LBL_LS_ERR; } + if ((err = mp_add(&T3z, &T4z, &Vz)) != MP_OKAY) goto LBL_LS_ERR; if (MP_IS_ODD(&Vz)) { - if ((err = mp_add(&Vz, a, &Vz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_add(&Vz, a, &Vz)) != MP_OKAY) goto LBL_LS_ERR; } oddness = MP_IS_ODD(&Vz) ? MP_YES : MP_NO; - if ((err = mp_div_2(&Vz, &Vz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_div_2(&Vz, &Vz)) != MP_OKAY) goto LBL_LS_ERR; if ((Vz.sign == MP_NEG) && (oddness != MP_NO)) { - if ((err = mp_sub_d(&Vz, 1uL, &Vz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - } - if ((err = mp_mod(&Uz, a, &Uz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_mod(&Vz, a, &Vz)) != MP_OKAY) { - goto LBL_LS_ERR; + if ((err = mp_sub_d(&Vz, 1uL, &Vz)) != MP_OKAY) goto LBL_LS_ERR; } + if ((err = mp_mod(&Uz, a, &Uz)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_mod(&Vz, a, &Vz)) != MP_OKAY) goto LBL_LS_ERR; + /* Calculating Q^d for later use */ - if ((err = mp_mul(&Qkdz, &Qmz, &Qkdz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_mod(&Qkdz, a, &Qkdz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_mul(&Qkdz, &Qmz, &Qkdz)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_mod(&Qkdz, a, &Qkdz)) != MP_OKAY) goto LBL_LS_ERR; } } @@ -318,35 +263,21 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result) Lucas pseudoprime. */ /* Initialize 2*Q^(d*2^r) for V_2m */ - if ((err = mp_mul_2(&Qkdz, &Q2kdz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_mul_2(&Qkdz, &Q2kdz)) != MP_OKAY) goto LBL_LS_ERR; for (r = 1; r < s; r++) { - if ((err = mp_sqr(&Vz, &Vz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_sub(&Vz, &Q2kdz, &Vz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_mod(&Vz, a, &Vz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_sqr(&Vz, &Vz)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_sub(&Vz, &Q2kdz, &Vz)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_mod(&Vz, a, &Vz)) != MP_OKAY) goto LBL_LS_ERR; if (MP_IS_ZERO(&Vz)) { *result = MP_YES; goto LBL_LS_ERR; } /* Calculate Q^{d*2^r} for next r (final iteration irrelevant). */ if (r < (s - 1)) { - if ((err = mp_sqr(&Qkdz, &Qkdz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_mod(&Qkdz, a, &Qkdz)) != MP_OKAY) { - goto LBL_LS_ERR; - } - if ((err = mp_mul_2(&Qkdz, &Q2kdz)) != MP_OKAY) { - goto LBL_LS_ERR; - } + if ((err = mp_sqr(&Qkdz, &Qkdz)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_mod(&Qkdz, a, &Qkdz)) != MP_OKAY) goto LBL_LS_ERR; + if ((err = mp_mul_2(&Qkdz, &Q2kdz)) != MP_OKAY) goto LBL_LS_ERR; } } LBL_LS_ERR: diff --git a/libtommath/bn_mp_radix_size.c b/libtommath/bn_mp_radix_size.c index 6d7f912..339b21b 100644 --- a/libtommath/bn_mp_radix_size.c +++ b/libtommath/bn_mp_radix_size.c @@ -48,16 +48,18 @@ mp_err mp_radix_size(const mp_int *a, int radix, int *size) /* fetch out all of the digits */ while (!MP_IS_ZERO(&t)) { if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) { - mp_clear(&t); - return err; + goto LBL_ERR; } ++digs; } - mp_clear(&t); /* return digs + 1, the 1 is for the NULL byte that would be required. */ *size = digs + 1; - return MP_OKAY; + err = MP_OKAY; + +LBL_ERR: + mp_clear(&t); + return err; } #endif diff --git a/libtommath/bn_mp_read_signed_bin.c b/libtommath/bn_mp_read_signed_bin.c deleted file mode 100644 index 2e16399..0000000 --- a/libtommath/bn_mp_read_signed_bin.c +++ /dev/null @@ -1,25 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_READ_SIGNED_BIN_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* read signed bin, big endian, first byte is 0==positive or 1==negative */ -mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) -{ - mp_err err; - - /* read magnitude */ - if ((err = mp_read_unsigned_bin(a, b + 1, c - 1)) != MP_OKAY) { - return err; - } - - /* first byte is 0 for positive, non-zero for negative */ - if (b[0] == (unsigned char)0) { - a->sign = MP_ZPOS; - } else { - a->sign = MP_NEG; - } - - return MP_OKAY; -} -#endif diff --git a/libtommath/bn_mp_read_unsigned_bin.c b/libtommath/bn_mp_read_unsigned_bin.c deleted file mode 100644 index 17b273e..0000000 --- a/libtommath/bn_mp_read_unsigned_bin.c +++ /dev/null @@ -1,39 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_READ_UNSIGNED_BIN_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* reads a unsigned char array, assumes the msb is stored first [big endian] */ -mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) -{ - mp_err err; - - /* make sure there are at least two digits */ - if (a->alloc < 2) { - if ((err = mp_grow(a, 2)) != MP_OKAY) { - return err; - } - } - - /* zero the int */ - mp_zero(a); - - /* read the bytes in */ - while (c-- > 0) { - if ((err = mp_mul_2d(a, 8, a)) != MP_OKAY) { - return err; - } - -#ifndef MP_8BIT - a->dp[0] |= *b++; - a->used += 1; -#else - a->dp[0] = (*b & MP_MASK); - a->dp[1] |= ((*b++ >> 7) & 1u); - a->used += 2; -#endif - } - mp_clamp(a); - return MP_OKAY; -} -#endif diff --git a/libtommath/bn_mp_reduce.c b/libtommath/bn_mp_reduce.c index 5748550..3c669d4 100644 --- a/libtommath/bn_mp_reduce.c +++ b/libtommath/bn_mp_reduce.c @@ -26,21 +26,17 @@ mp_err mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu) if ((err = mp_mul(&q, mu, &q)) != MP_OKAY) { goto CLEANUP; } - } else { -#ifdef BN_S_MP_MUL_HIGH_DIGS_C + } else if (MP_HAS(S_MP_MUL_HIGH_DIGS)) { if ((err = s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) { goto CLEANUP; } -#elif defined(BN_S_MP_MUL_HIGH_DIGS_FAST_C) + } else if (MP_HAS(S_MP_MUL_HIGH_DIGS_FAST)) { if ((err = s_mp_mul_high_digs_fast(&q, mu, &q, um)) != MP_OKAY) { goto CLEANUP; } -#else - { - err = MP_VAL; - goto CLEANUP; - } -#endif + } else { + err = MP_VAL; + goto CLEANUP; } /* q3 = q2 / b**(k+1) */ diff --git a/libtommath/bn_mp_reduce_is_2k.c b/libtommath/bn_mp_reduce_is_2k.c index bb70b55..a9f4f9f 100644 --- a/libtommath/bn_mp_reduce_is_2k.c +++ b/libtommath/bn_mp_reduce_is_2k.c @@ -24,7 +24,7 @@ mp_bool mp_reduce_is_2k(const mp_int *a) return MP_NO; } iz <<= 1; - if (iz > MP_MASK) { + if (iz > MP_DIGIT_MAX) { ++iw; iz = 1; } diff --git a/libtommath/bn_mp_reduce_is_2k_l.c b/libtommath/bn_mp_reduce_is_2k_l.c index 36fc59f..4bc69be 100644 --- a/libtommath/bn_mp_reduce_is_2k_l.c +++ b/libtommath/bn_mp_reduce_is_2k_l.c @@ -15,7 +15,7 @@ mp_bool mp_reduce_is_2k_l(const mp_int *a) } else if (a->used > 1) { /* if more than half of the digits are -1 we're sold */ for (iy = ix = 0; ix < a->used; ix++) { - if (a->dp[ix] == MP_MASK) { + if (a->dp[ix] == MP_DIGIT_MAX) { ++iy; } } diff --git a/libtommath/bn_mp_root_u32.c b/libtommath/bn_mp_root_u32.c index d8c4e7a..ba65549 100644 --- a/libtommath/bn_mp_root_u32.c +++ b/libtommath/bn_mp_root_u32.c @@ -63,46 +63,32 @@ mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c) } /* Start value must be larger than root */ ilog2 += 2; - if ((err = mp_2expt(&t2,ilog2)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_2expt(&t2,ilog2)) != MP_OKAY) goto LBL_ERR; do { /* t1 = t2 */ - if ((err = mp_copy(&t2, &t1)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_copy(&t2, &t1)) != MP_OKAY) goto LBL_ERR; /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */ /* t3 = t1**(b-1) */ - if ((err = mp_expt_u32(&t1, b - 1u, &t3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_expt_u32(&t1, b - 1u, &t3)) != MP_OKAY) goto LBL_ERR; + /* numerator */ /* t2 = t1**b */ - if ((err = mp_mul(&t3, &t1, &t2)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_mul(&t3, &t1, &t2)) != MP_OKAY) goto LBL_ERR; /* t2 = t1**b - a */ - if ((err = mp_sub(&t2, &a_, &t2)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&t2, &a_, &t2)) != MP_OKAY) goto LBL_ERR; /* denominator */ /* t3 = t1**(b-1) * b */ - if ((err = mp_mul_d(&t3, b, &t3)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_mul_d(&t3, b, &t3)) != MP_OKAY) goto LBL_ERR; /* t3 = (t1**b - a)/(b * t1**(b-1)) */ - if ((err = mp_div(&t2, &t3, &t3, NULL)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_div(&t2, &t3, &t3, NULL)) != MP_OKAY) goto LBL_ERR; + + if ((err = mp_sub(&t1, &t3, &t2)) != MP_OKAY) goto LBL_ERR; - if ((err = mp_sub(&t1, &t3, &t2)) != MP_OKAY) { - goto LBL_ERR; - } /* Number of rounds is at most log_2(root). If it is more it got stuck, so break out of the loop and do the rest manually. @@ -115,31 +101,23 @@ mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c) /* result can be off by a few so check */ /* Loop beneath can overshoot by one if found root is smaller than actual root */ for (;;) { - if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) goto LBL_ERR; cmp = mp_cmp(&t2, &a_); if (cmp == MP_EQ) { err = MP_OKAY; goto LBL_ERR; } if (cmp == MP_LT) { - if ((err = mp_add_d(&t1, 1uL, &t1)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_add_d(&t1, 1uL, &t1)) != MP_OKAY) goto LBL_ERR; } else { break; } } /* correct overshoot from above or from recurrence */ for (;;) { - if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_expt_u32(&t1, b, &t2)) != MP_OKAY) goto LBL_ERR; if (mp_cmp(&t2, &a_) == MP_GT) { - if ((err = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) goto LBL_ERR; } else { break; } diff --git a/libtommath/bn_mp_sbin_size.c b/libtommath/bn_mp_sbin_size.c new file mode 100644 index 0000000..e0993d6 --- /dev/null +++ b/libtommath/bn_mp_sbin_size.c @@ -0,0 +1,11 @@ +#include "tommath_private.h" +#ifdef BN_MP_SBIN_SIZE_C +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +/* get the size for an signed equivalent */ +size_t mp_sbin_size(const mp_int *a) +{ + return 1u + mp_ubin_size(a); +} +#endif diff --git a/libtommath/bn_mp_signed_bin_size.c b/libtommath/bn_mp_signed_bin_size.c deleted file mode 100644 index 1a7f49c..0000000 --- a/libtommath/bn_mp_signed_bin_size.c +++ /dev/null @@ -1,11 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_SIGNED_BIN_SIZE_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* get the size for an signed equivalent */ -int mp_signed_bin_size(const mp_int *a) -{ - return 1 + mp_unsigned_bin_size(a); -} -#endif diff --git a/libtommath/bn_mp_sqr.c b/libtommath/bn_mp_sqr.c index d4c7d17..e0d0a73 100644 --- a/libtommath/bn_mp_sqr.c +++ b/libtommath/bn_mp_sqr.c @@ -7,35 +7,21 @@ mp_err mp_sqr(const mp_int *a, mp_int *b) { mp_err err; - -#ifdef BN_S_MP_TOOM_SQR_C - /* use Toom-Cook? */ - if (a->used >= MP_TOOM_SQR_CUTOFF) { + if (MP_HAS(S_MP_TOOM_SQR) && /* use Toom-Cook? */ + (a->used >= MP_TOOM_SQR_CUTOFF)) { err = s_mp_toom_sqr(a, b); - /* Karatsuba? */ - } else -#endif -#ifdef BN_S_MP_KARATSUBA_SQR_C - if (a->used >= MP_KARATSUBA_SQR_CUTOFF) { - err = s_mp_karatsuba_sqr(a, b); - } else -#endif - { -#ifdef BN_S_MP_SQR_FAST_C - /* can we use the fast comba multiplier? */ - if ((((a->used * 2) + 1) < MP_WARRAY) && - (a->used < (MP_MAXFAST / 2))) { - err = s_mp_sqr_fast(a, b); - } else -#endif - { -#ifdef BN_S_MP_SQR_C - err = s_mp_sqr(a, b); -#else - err = MP_VAL; -#endif - } - } + } else if (MP_HAS(S_MP_KARATSUBA_SQR) && /* Karatsuba? */ + (a->used >= MP_KARATSUBA_SQR_CUTOFF)) { + err = s_mp_karatsuba_sqr(a, b); + } else if (MP_HAS(S_MP_SQR_FAST) && /* can we use the fast comba multiplier? */ + (((a->used * 2) + 1) < MP_WARRAY) && + (a->used < (MP_MAXFAST / 2))) { + err = s_mp_sqr_fast(a, b); + } else if (MP_HAS(S_MP_SQR)) { + err = s_mp_sqr(a, b); + } else { + err = MP_VAL; + } b->sign = MP_ZPOS; return err; } diff --git a/libtommath/bn_mp_sqrmod.c b/libtommath/bn_mp_sqrmod.c index b3c44e5..626ea2c 100644 --- a/libtommath/bn_mp_sqrmod.c +++ b/libtommath/bn_mp_sqrmod.c @@ -14,10 +14,11 @@ mp_err mp_sqrmod(const mp_int *a, const mp_int *b, mp_int *c) } if ((err = mp_sqr(a, &t)) != MP_OKAY) { - mp_clear(&t); - return err; + goto LBL_ERR; } err = mp_mod(&t, b, c); + +LBL_ERR: mp_clear(&t); return err; } diff --git a/libtommath/bn_mp_sqrtmod_prime.c b/libtommath/bn_mp_sqrtmod_prime.c index f803760..a833ed7 100644 --- a/libtommath/bn_mp_sqrtmod_prime.c +++ b/libtommath/bn_mp_sqrtmod_prime.c @@ -61,7 +61,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) /* find a Z such that the Legendre symbol (Z|prime) == -1 */ mp_set_u32(&Z, 2u); /* Z = 2 */ - while (1) { + for (;;) { if ((err = mp_kronecker(&Z, prime, &legendre)) != MP_OKAY) goto cleanup; if (legendre == -1) break; if ((err = mp_add_d(&Z, 1uL, &Z)) != MP_OKAY) goto cleanup; @@ -81,10 +81,10 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret) /* M = S */ mp_set_u32(&two, 2u); - while (1) { + for (;;) { if ((err = mp_copy(&T, &t1)) != MP_OKAY) goto cleanup; i = 0; - while (1) { + for (;;) { if (mp_cmp_d(&t1, 1uL) == MP_EQ) break; if ((err = mp_exptmod(&t1, &two, prime, &t1)) != MP_OKAY) goto cleanup; i++; diff --git a/libtommath/bn_mp_submod.c b/libtommath/bn_mp_submod.c index 61c2d0f..5ebd374 100644 --- a/libtommath/bn_mp_submod.c +++ b/libtommath/bn_mp_submod.c @@ -14,10 +14,11 @@ mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d) } if ((err = mp_sub(a, b, &t)) != MP_OKAY) { - mp_clear(&t); - return err; + goto LBL_ERR; } err = mp_mod(&t, c, d); + +LBL_ERR: mp_clear(&t); return err; } diff --git a/libtommath/bn_mp_to_radix.c b/libtommath/bn_mp_to_radix.c new file mode 100644 index 0000000..0af0bb7 --- /dev/null +++ b/libtommath/bn_mp_to_radix.c @@ -0,0 +1,74 @@ +#include "tommath_private.h" +#ifdef BN_MP_TO_RADIX_C +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +/* stores a bignum as a ASCII string in a given radix (2..64) + * + * Stores upto maxlen-1 chars and always a NULL byte + */ +mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix) +{ + size_t digs; + mp_err err; + mp_int t; + mp_digit d; + char *_s = str; + + /* check range of the maxlen, radix */ + if ((maxlen < 2u) || (radix < 2) || (radix > 64)) { + return MP_VAL; + } + + /* quick out if its zero */ + if (MP_IS_ZERO(a)) { + *str++ = '0'; + *str = '\0'; + return MP_OKAY; + } + + if ((err = mp_init_copy(&t, a)) != MP_OKAY) { + return err; + } + + /* if it is negative output a - */ + if (t.sign == MP_NEG) { + /* we have to reverse our digits later... but not the - sign!! */ + ++_s; + + /* store the flag and mark the number as positive */ + *str++ = '-'; + t.sign = MP_ZPOS; + + /* subtract a char */ + --maxlen; + } + + digs = 0u; + while (!MP_IS_ZERO(&t)) { + if (--maxlen < 1u) { + /* no more room */ + err = MP_VAL; + break; + } + if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) { + goto LBL_ERR; + } + *str++ = mp_s_rmap[d]; + ++digs; + } + + /* reverse the digits of the string. In this case _s points + * to the first digit [exluding the sign] of the number + */ + s_mp_reverse((unsigned char *)_s, digs); + + /* append a NULL so the string is properly terminated */ + *str = '\0'; + +LBL_ERR: + mp_clear(&t); + return err; +} + +#endif diff --git a/libtommath/bn_mp_to_sbin.c b/libtommath/bn_mp_to_sbin.c new file mode 100644 index 0000000..e28dcce --- /dev/null +++ b/libtommath/bn_mp_to_sbin.c @@ -0,0 +1,23 @@ +#include "tommath_private.h" +#ifdef BN_MP_TO_SBIN_C +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +/* store in signed [big endian] format */ +mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) +{ + mp_err err; + if (maxlen == 0u) { + return MP_VAL; + } + + if ((err = mp_to_ubin(a, buf + 1, maxlen - 1u, written)) != MP_OKAY) { + return err; + } + if (written != NULL) { + (*written)++; + } + buf[0] = (a->sign == MP_ZPOS) ? (unsigned char)0 : (unsigned char)1; + return MP_OKAY; +} +#endif diff --git a/libtommath/bn_mp_to_signed_bin.c b/libtommath/bn_mp_to_signed_bin.c deleted file mode 100644 index 73f37ca..0000000 --- a/libtommath/bn_mp_to_signed_bin.c +++ /dev/null @@ -1,16 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TO_SIGNED_BIN_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* store in signed [big endian] format */ -mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b) -{ - mp_err err; - if ((err = mp_to_unsigned_bin(a, b + 1)) != MP_OKAY) { - return err; - } - b[0] = (a->sign == MP_ZPOS) ? (unsigned char)0 : (unsigned char)1; - return MP_OKAY; -} -#endif diff --git a/libtommath/bn_mp_to_signed_bin_n.c b/libtommath/bn_mp_to_signed_bin_n.c deleted file mode 100644 index 4c36cb5..0000000 --- a/libtommath/bn_mp_to_signed_bin_n.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TO_SIGNED_BIN_N_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* store in signed [big endian] format */ -mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) -{ - if (*outlen < (unsigned long)mp_signed_bin_size(a)) { - return MP_VAL; - } - *outlen = (unsigned long)mp_signed_bin_size(a); - return mp_to_signed_bin(a, b); -} -#endif diff --git a/libtommath/bn_mp_to_ubin.c b/libtommath/bn_mp_to_ubin.c new file mode 100644 index 0000000..d359986 --- /dev/null +++ b/libtommath/bn_mp_to_ubin.c @@ -0,0 +1,50 @@ +#include "tommath_private.h" +#ifdef BN_MP_TO_UBIN_C +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +/* store in unsigned [big endian] format */ +mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) +{ + size_t x; + mp_err err; + mp_int t; + + if (buf == NULL) { + return MP_MEM; + } + + if (maxlen == 0u) { + return MP_VAL; + } + + if ((err = mp_init_copy(&t, a)) != MP_OKAY) { + return err; + } + + x = 0u; + while (!MP_IS_ZERO(&t)) { + if (maxlen == 0u) { + err = MP_VAL; + goto LBL_ERR; + } + maxlen--; +#ifndef MP_8BIT + buf[x++] = (unsigned char)(t.dp[0] & 255u); +#else + buf[x++] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7)); +#endif + if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) { + goto LBL_ERR; + } + } + s_mp_reverse(buf, x); + + if (written != NULL) { + *written = x; + } +LBL_ERR: + mp_clear(&t); + return err; +} +#endif diff --git a/libtommath/bn_mp_to_unsigned_bin.c b/libtommath/bn_mp_to_unsigned_bin.c deleted file mode 100644 index 093b01a..0000000 --- a/libtommath/bn_mp_to_unsigned_bin.c +++ /dev/null @@ -1,33 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TO_UNSIGNED_BIN_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* store in unsigned [big endian] format */ -mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b) -{ - int x; - mp_err err; - mp_int t; - - if ((err = mp_init_copy(&t, a)) != MP_OKAY) { - return err; - } - - x = 0; - while (!MP_IS_ZERO(&t)) { -#ifndef MP_8BIT - b[x++] = (unsigned char)(t.dp[0] & 255u); -#else - b[x++] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7)); -#endif - if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) { - mp_clear(&t); - return err; - } - } - s_mp_reverse(b, x); - mp_clear(&t); - return MP_OKAY; -} -#endif diff --git a/libtommath/bn_mp_to_unsigned_bin_n.c b/libtommath/bn_mp_to_unsigned_bin_n.c deleted file mode 100644 index 3c08465..0000000 --- a/libtommath/bn_mp_to_unsigned_bin_n.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TO_UNSIGNED_BIN_N_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* store in unsigned [big endian] format */ -mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) -{ - if (*outlen < (unsigned long)mp_unsigned_bin_size(a)) { - return MP_VAL; - } - *outlen = (unsigned long)mp_unsigned_bin_size(a); - return mp_to_unsigned_bin(a, b); -} -#endif diff --git a/libtommath/bn_mp_toradix.c b/libtommath/bn_mp_toradix.c deleted file mode 100644 index 6322e91..0000000 --- a/libtommath/bn_mp_toradix.c +++ /dev/null @@ -1,60 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TORADIX_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* stores a bignum as a ASCII string in a given radix (2..64) */ -mp_err mp_toradix(const mp_int *a, char *str, int radix) -{ - mp_err err; - int digs; - mp_int t; - mp_digit d; - char *_s = str; - - /* check range of the radix */ - if ((radix < 2) || (radix > 64)) { - return MP_VAL; - } - - /* quick out if its zero */ - if (MP_IS_ZERO(a)) { - *str++ = '0'; - *str = '\0'; - return MP_OKAY; - } - - if ((err = mp_init_copy(&t, a)) != MP_OKAY) { - return err; - } - - /* if it is negative output a - */ - if (t.sign == MP_NEG) { - ++_s; - *str++ = '-'; - t.sign = MP_ZPOS; - } - - digs = 0; - while (!MP_IS_ZERO(&t)) { - if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) { - mp_clear(&t); - return err; - } - *str++ = mp_s_rmap[d]; - ++digs; - } - - /* reverse the digits of the string. In this case _s points - * to the first digit [exluding the sign] of the number] - */ - s_mp_reverse((unsigned char *)_s, digs); - - /* append a NULL so the string is properly terminated */ - *str = '\0'; - - mp_clear(&t); - return MP_OKAY; -} - -#endif diff --git a/libtommath/bn_mp_toradix_n.c b/libtommath/bn_mp_toradix_n.c deleted file mode 100644 index bb8af88..0000000 --- a/libtommath/bn_mp_toradix_n.c +++ /dev/null @@ -1,73 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_TORADIX_N_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* stores a bignum as a ASCII string in a given radix (2..64) - * - * Stores upto maxlen-1 chars and always a NULL byte - */ -mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) -{ - int digs; - mp_err err; - mp_int t; - mp_digit d; - char *_s = str; - - /* check range of the maxlen, radix */ - if ((maxlen < 2) || (radix < 2) || (radix > 64)) { - return MP_VAL; - } - - /* quick out if its zero */ - if (MP_IS_ZERO(a)) { - *str++ = '0'; - *str = '\0'; - return MP_OKAY; - } - - if ((err = mp_init_copy(&t, a)) != MP_OKAY) { - return err; - } - - /* if it is negative output a - */ - if (t.sign == MP_NEG) { - /* we have to reverse our digits later... but not the - sign!! */ - ++_s; - - /* store the flag and mark the number as positive */ - *str++ = '-'; - t.sign = MP_ZPOS; - - /* subtract a char */ - --maxlen; - } - - digs = 0; - while (!MP_IS_ZERO(&t)) { - if (--maxlen < 1) { - /* no more room */ - break; - } - if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) { - mp_clear(&t); - return err; - } - *str++ = mp_s_rmap[d]; - ++digs; - } - - /* reverse the digits of the string. In this case _s points - * to the first digit [exluding the sign] of the number - */ - s_mp_reverse((unsigned char *)_s, digs); - - /* append a NULL so the string is properly terminated */ - *str = '\0'; - - mp_clear(&t); - return MP_OKAY; -} - -#endif diff --git a/libtommath/bn_mp_ubin_size.c b/libtommath/bn_mp_ubin_size.c new file mode 100644 index 0000000..21230b4 --- /dev/null +++ b/libtommath/bn_mp_ubin_size.c @@ -0,0 +1,12 @@ +#include "tommath_private.h" +#ifdef BN_MP_UBIN_SIZE_C +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +/* get the size for an unsigned equivalent */ +size_t mp_ubin_size(const mp_int *a) +{ + size_t size = (size_t)mp_count_bits(a); + return (size / 8u) + (((size & 7u) != 0u) ? 1u : 0u); +} +#endif diff --git a/libtommath/bn_mp_unsigned_bin_size.c b/libtommath/bn_mp_unsigned_bin_size.c deleted file mode 100644 index 297de92..0000000 --- a/libtommath/bn_mp_unsigned_bin_size.c +++ /dev/null @@ -1,12 +0,0 @@ -#include "tommath_private.h" -#ifdef BN_MP_UNSIGNED_BIN_SIZE_C -/* LibTomMath, multiple-precision integer library -- Tom St Denis */ -/* SPDX-License-Identifier: Unlicense */ - -/* get the size for an unsigned equivalent */ -int mp_unsigned_bin_size(const mp_int *a) -{ - int size = mp_count_bits(a); - return (size / 8) + ((((unsigned)size & 7u) != 0u) ? 1 : 0); -} -#endif diff --git a/libtommath/bn_prime_tab.c b/libtommath/bn_prime_tab.c index 008e9c3..a6c07f8 100644 --- a/libtommath/bn_prime_tab.c +++ b/libtommath/bn_prime_tab.c @@ -44,7 +44,7 @@ const mp_digit ltm_prime_tab[] = { #endif }; -#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 301) +#if defined(__GNUC__) && __GNUC__ >= 4 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" const mp_digit *s_mp_prime_tab = ltm_prime_tab; diff --git a/libtommath/bn_s_mp_balance_mul.c b/libtommath/bn_s_mp_balance_mul.c index efc1809..7ece5d7 100644 --- a/libtommath/bn_s_mp_balance_mul.c +++ b/libtommath/bn_s_mp_balance_mul.c @@ -40,6 +40,7 @@ mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c) a0.dp[count] = A.dp[ j++ ]; a0.used++; } + mp_clamp(&a0); /* Multiply with b */ if ((err = mp_mul(&a0, &B, &tmp)) != MP_OKAY) { goto LBL_ERR; @@ -60,6 +61,7 @@ mp_err s_mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c) a0.dp[count] = A.dp[ j++ ]; a0.used++; } + mp_clamp(&a0); if ((err = mp_mul(&a0, &B, &tmp)) != MP_OKAY) { goto LBL_ERR; } diff --git a/libtommath/bn_s_mp_exptmod.c b/libtommath/bn_s_mp_exptmod.c index 5d5510f..c3bfa95 100644 --- a/libtommath/bn_s_mp_exptmod.c +++ b/libtommath/bn_s_mp_exptmod.c @@ -5,8 +5,10 @@ #ifdef MP_LOW_MEM # define TAB_SIZE 32 +# define MAX_WINSIZE 5 #else # define TAB_SIZE 256 +# define MAX_WINSIZE 0 #endif mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) @@ -35,11 +37,7 @@ mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y winsize = 8; } -#ifdef MP_LOW_MEM - if (winsize > 5) { - winsize = 5; - } -#endif + winsize = MAX_WINSIZE ? MP_MIN(MAX_WINSIZE, winsize) : winsize; /* init M array */ /* init first cell */ @@ -59,19 +57,13 @@ mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y } /* create mu, used for Barrett reduction */ - if ((err = mp_init(&mu)) != MP_OKAY) { - goto LBL_M; - } + if ((err = mp_init(&mu)) != MP_OKAY) goto LBL_M; if (redmode == 0) { - if ((err = mp_reduce_setup(&mu, P)) != MP_OKAY) { - goto LBL_MU; - } + if ((err = mp_reduce_setup(&mu, P)) != MP_OKAY) goto LBL_MU; redux = mp_reduce; } else { - if ((err = mp_reduce_2k_setup_l(P, &mu)) != MP_OKAY) { - goto LBL_MU; - } + if ((err = mp_reduce_2k_setup_l(P, &mu)) != MP_OKAY) goto LBL_MU; redux = mp_reduce_2k_l; } @@ -83,46 +75,32 @@ mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y * The first half of the table is not * computed though accept for M[0] and M[1] */ - if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) { - goto LBL_MU; - } + if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) goto LBL_MU; /* compute the value at M[1<<(winsize-1)] by squaring * M[1] (winsize-1) times */ - if ((err = mp_copy(&M[1], &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) { - goto LBL_MU; - } + if ((err = mp_copy(&M[1], &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) goto LBL_MU; for (x = 0; x < (winsize - 1); x++) { /* square it */ if ((err = mp_sqr(&M[(size_t)1 << (winsize - 1)], - &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) { - goto LBL_MU; - } + &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) goto LBL_MU; /* reduce modulo P */ - if ((err = redux(&M[(size_t)1 << (winsize - 1)], P, &mu)) != MP_OKAY) { - goto LBL_MU; - } + if ((err = redux(&M[(size_t)1 << (winsize - 1)], P, &mu)) != MP_OKAY) goto LBL_MU; } /* create upper table, that is M[x] = M[x-1] * M[1] (mod P) * for x = (2**(winsize - 1) + 1) to (2**winsize - 1) */ for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { - if ((err = mp_mul(&M[x - 1], &M[1], &M[x])) != MP_OKAY) { - goto LBL_MU; - } - if ((err = redux(&M[x], P, &mu)) != MP_OKAY) { - goto LBL_MU; - } + if ((err = mp_mul(&M[x - 1], &M[1], &M[x])) != MP_OKAY) goto LBL_MU; + if ((err = redux(&M[x], P, &mu)) != MP_OKAY) goto LBL_MU; } /* setup result */ - if ((err = mp_init(&res)) != MP_OKAY) { - goto LBL_MU; - } + if ((err = mp_init(&res)) != MP_OKAY) goto LBL_MU; mp_set(&res, 1uL); /* set initial mode and bit cnt */ @@ -160,12 +138,8 @@ mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y /* if the bit is zero and mode == 1 then we square */ if ((mode == 1) && (y == 0)) { - if ((err = mp_sqr(&res, &res)) != MP_OKAY) { - goto LBL_RES; - } - if ((err = redux(&res, P, &mu)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_sqr(&res, &res)) != MP_OKAY) goto LBL_RES; + if ((err = redux(&res, P, &mu)) != MP_OKAY) goto LBL_RES; continue; } @@ -177,21 +151,13 @@ mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y /* ok window is filled so square as required and multiply */ /* square first */ for (x = 0; x < winsize; x++) { - if ((err = mp_sqr(&res, &res)) != MP_OKAY) { - goto LBL_RES; - } - if ((err = redux(&res, P, &mu)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_sqr(&res, &res)) != MP_OKAY) goto LBL_RES; + if ((err = redux(&res, P, &mu)) != MP_OKAY) goto LBL_RES; } /* then multiply */ - if ((err = mp_mul(&res, &M[bitbuf], &res)) != MP_OKAY) { - goto LBL_RES; - } - if ((err = redux(&res, P, &mu)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_mul(&res, &M[bitbuf], &res)) != MP_OKAY) goto LBL_RES; + if ((err = redux(&res, P, &mu)) != MP_OKAY) goto LBL_RES; /* empty window and reset */ bitcpy = 0; @@ -204,22 +170,14 @@ mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y if ((mode == 2) && (bitcpy > 0)) { /* square then multiply if the bit is set */ for (x = 0; x < bitcpy; x++) { - if ((err = mp_sqr(&res, &res)) != MP_OKAY) { - goto LBL_RES; - } - if ((err = redux(&res, P, &mu)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_sqr(&res, &res)) != MP_OKAY) goto LBL_RES; + if ((err = redux(&res, P, &mu)) != MP_OKAY) goto LBL_RES; bitbuf <<= 1; if ((bitbuf & (1 << winsize)) != 0) { /* then multiply */ - if ((err = mp_mul(&res, &M[1], &res)) != MP_OKAY) { - goto LBL_RES; - } - if ((err = redux(&res, P, &mu)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_mul(&res, &M[1], &res)) != MP_OKAY) goto LBL_RES; + if ((err = redux(&res, P, &mu)) != MP_OKAY) goto LBL_RES; } } } diff --git a/libtommath/bn_s_mp_exptmod_fast.c b/libtommath/bn_s_mp_exptmod_fast.c index 43a2ba1..682ded8 100644 --- a/libtommath/bn_s_mp_exptmod_fast.c +++ b/libtommath/bn_s_mp_exptmod_fast.c @@ -13,8 +13,10 @@ #ifdef MP_LOW_MEM # define TAB_SIZE 32 +# define MAX_WINSIZE 5 #else # define TAB_SIZE 256 +# define MAX_WINSIZE 0 #endif mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) @@ -48,11 +50,7 @@ mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_i winsize = 8; } -#ifdef MP_LOW_MEM - if (winsize > 5) { - winsize = 5; - } -#endif + winsize = MAX_WINSIZE ? MP_MIN(MAX_WINSIZE, winsize) : winsize; /* init M array */ /* init first cell */ @@ -73,58 +71,46 @@ mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_i /* determine and setup reduction code */ if (redmode == 0) { -#ifdef BN_MP_MONTGOMERY_SETUP_C - /* now setup montgomery */ - if ((err = mp_montgomery_setup(P, &mp)) != MP_OKAY) { + if (MP_HAS(MP_MONTGOMERY_SETUP)) { + /* now setup montgomery */ + if ((err = mp_montgomery_setup(P, &mp)) != MP_OKAY) goto LBL_M; + } else { + err = MP_VAL; goto LBL_M; } -#else - err = MP_VAL; - goto LBL_M; -#endif /* automatically pick the comba one if available (saves quite a few calls/ifs) */ -#ifdef BN_S_MP_MONTGOMERY_REDUCE_FAST_C - if ((((P->used * 2) + 1) < MP_WARRAY) && + if (MP_HAS(S_MP_MONTGOMERY_REDUCE_FAST) && + (((P->used * 2) + 1) < MP_WARRAY) && (P->used < MP_MAXFAST)) { redux = s_mp_montgomery_reduce_fast; - } else -#endif - { -#ifdef BN_MP_MONTGOMERY_REDUCE_C + } else if (MP_HAS(MP_MONTGOMERY_REDUCE)) { /* use slower baseline Montgomery method */ redux = mp_montgomery_reduce; -#else + } else { err = MP_VAL; goto LBL_M; -#endif } } else if (redmode == 1) { -#if defined(BN_MP_DR_SETUP_C) && defined(BN_MP_DR_REDUCE_C) - /* setup DR reduction for moduli of the form B**k - b */ - mp_dr_setup(P, &mp); - redux = mp_dr_reduce; -#else - err = MP_VAL; - goto LBL_M; -#endif - } else { -#if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C) - /* setup DR reduction for moduli of the form 2**k - b */ - if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) { + if (MP_HAS(MP_DR_SETUP) && MP_HAS(MP_DR_REDUCE)) { + /* setup DR reduction for moduli of the form B**k - b */ + mp_dr_setup(P, &mp); + redux = mp_dr_reduce; + } else { + err = MP_VAL; goto LBL_M; } + } else if (MP_HAS(MP_REDUCE_2K_SETUP) && MP_HAS(MP_REDUCE_2K)) { + /* setup DR reduction for moduli of the form 2**k - b */ + if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) goto LBL_M; redux = mp_reduce_2k; -#else + } else { err = MP_VAL; goto LBL_M; -#endif } /* setup result */ - if ((err = mp_init_size(&res, P->alloc)) != MP_OKAY) { - goto LBL_M; - } + if ((err = mp_init_size(&res, P->alloc)) != MP_OKAY) goto LBL_M; /* create M table * @@ -134,49 +120,33 @@ mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_i */ if (redmode == 0) { -#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C - /* now we need R mod m */ - if ((err = mp_montgomery_calc_normalization(&res, P)) != MP_OKAY) { - goto LBL_RES; - } + if (MP_HAS(MP_MONTGOMERY_CALC_NORMALIZATION)) { + /* now we need R mod m */ + if ((err = mp_montgomery_calc_normalization(&res, P)) != MP_OKAY) goto LBL_RES; - /* now set M[1] to G * R mod m */ - if ((err = mp_mulmod(G, &res, P, &M[1])) != MP_OKAY) { + /* now set M[1] to G * R mod m */ + if ((err = mp_mulmod(G, &res, P, &M[1])) != MP_OKAY) goto LBL_RES; + } else { + err = MP_VAL; goto LBL_RES; } -#else - err = MP_VAL; - goto LBL_RES; -#endif } else { mp_set(&res, 1uL); - if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) goto LBL_RES; } /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */ - if ((err = mp_copy(&M[1], &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_copy(&M[1], &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) goto LBL_RES; for (x = 0; x < (winsize - 1); x++) { - if ((err = mp_sqr(&M[(size_t)1 << (winsize - 1)], &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) { - goto LBL_RES; - } - if ((err = redux(&M[(size_t)1 << (winsize - 1)], P, mp)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_sqr(&M[(size_t)1 << (winsize - 1)], &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) goto LBL_RES; + if ((err = redux(&M[(size_t)1 << (winsize - 1)], P, mp)) != MP_OKAY) goto LBL_RES; } /* create upper table */ for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { - if ((err = mp_mul(&M[x - 1], &M[1], &M[x])) != MP_OKAY) { - goto LBL_RES; - } - if ((err = redux(&M[x], P, mp)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_mul(&M[x - 1], &M[1], &M[x])) != MP_OKAY) goto LBL_RES; + if ((err = redux(&M[x], P, mp)) != MP_OKAY) goto LBL_RES; } /* set initial mode and bit cnt */ @@ -214,12 +184,8 @@ mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_i /* if the bit is zero and mode == 1 then we square */ if ((mode == 1) && (y == 0)) { - if ((err = mp_sqr(&res, &res)) != MP_OKAY) { - goto LBL_RES; - } - if ((err = redux(&res, P, mp)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_sqr(&res, &res)) != MP_OKAY) goto LBL_RES; + if ((err = redux(&res, P, mp)) != MP_OKAY) goto LBL_RES; continue; } @@ -231,21 +197,13 @@ mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_i /* ok window is filled so square as required and multiply */ /* square first */ for (x = 0; x < winsize; x++) { - if ((err = mp_sqr(&res, &res)) != MP_OKAY) { - goto LBL_RES; - } - if ((err = redux(&res, P, mp)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_sqr(&res, &res)) != MP_OKAY) goto LBL_RES; + if ((err = redux(&res, P, mp)) != MP_OKAY) goto LBL_RES; } /* then multiply */ - if ((err = mp_mul(&res, &M[bitbuf], &res)) != MP_OKAY) { - goto LBL_RES; - } - if ((err = redux(&res, P, mp)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_mul(&res, &M[bitbuf], &res)) != MP_OKAY) goto LBL_RES; + if ((err = redux(&res, P, mp)) != MP_OKAY) goto LBL_RES; /* empty window and reset */ bitcpy = 0; @@ -258,23 +216,15 @@ mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_i if ((mode == 2) && (bitcpy > 0)) { /* square then multiply if the bit is set */ for (x = 0; x < bitcpy; x++) { - if ((err = mp_sqr(&res, &res)) != MP_OKAY) { - goto LBL_RES; - } - if ((err = redux(&res, P, mp)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_sqr(&res, &res)) != MP_OKAY) goto LBL_RES; + if ((err = redux(&res, P, mp)) != MP_OKAY) goto LBL_RES; /* get next bit of the window */ bitbuf <<= 1; if ((bitbuf & (1 << winsize)) != 0) { /* then multiply */ - if ((err = mp_mul(&res, &M[1], &res)) != MP_OKAY) { - goto LBL_RES; - } - if ((err = redux(&res, P, mp)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = mp_mul(&res, &M[1], &res)) != MP_OKAY) goto LBL_RES; + if ((err = redux(&res, P, mp)) != MP_OKAY) goto LBL_RES; } } } @@ -286,9 +236,7 @@ mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_i * to reduce one more time to cancel out the factor * of R. */ - if ((err = redux(&res, P, mp)) != MP_OKAY) { - goto LBL_RES; - } + if ((err = redux(&res, P, mp)) != MP_OKAY) goto LBL_RES; } /* swap res with Y */ diff --git a/libtommath/bn_s_mp_invmod_fast.c b/libtommath/bn_s_mp_invmod_fast.c index be7813c..677d7ab 100644 --- a/libtommath/bn_s_mp_invmod_fast.c +++ b/libtommath/bn_s_mp_invmod_fast.c @@ -26,14 +26,10 @@ mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c) } /* x == modulus, y == value to invert */ - if ((err = mp_copy(b, &x)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_copy(b, &x)) != MP_OKAY) goto LBL_ERR; /* we need y = |a| */ - if ((err = mp_mod(a, b, &y)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_mod(a, b, &y)) != MP_OKAY) goto LBL_ERR; /* if one of x,y is zero return an error! */ if (MP_IS_ZERO(&x) || MP_IS_ZERO(&y)) { @@ -42,71 +38,49 @@ mp_err s_mp_invmod_fast(const mp_int *a, const mp_int *b, mp_int *c) } /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ - if ((err = mp_copy(&x, &u)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_copy(&y, &v)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_copy(&x, &u)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_copy(&y, &v)) != MP_OKAY) goto LBL_ERR; mp_set(&D, 1uL); top: /* 4. while u is even do */ while (MP_IS_EVEN(&u)) { /* 4.1 u = u/2 */ - if ((err = mp_div_2(&u, &u)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_div_2(&u, &u)) != MP_OKAY) goto LBL_ERR; + /* 4.2 if B is odd then */ if (MP_IS_ODD(&B)) { - if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) goto LBL_ERR; } /* B = B/2 */ - if ((err = mp_div_2(&B, &B)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_div_2(&B, &B)) != MP_OKAY) goto LBL_ERR; } /* 5. while v is even do */ while (MP_IS_EVEN(&v)) { /* 5.1 v = v/2 */ - if ((err = mp_div_2(&v, &v)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_div_2(&v, &v)) != MP_OKAY) goto LBL_ERR; + /* 5.2 if D is odd then */ if (MP_IS_ODD(&D)) { /* D = (D-x)/2 */ - if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) goto LBL_ERR; } /* D = D/2 */ - if ((err = mp_div_2(&D, &D)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_div_2(&D, &D)) != MP_OKAY) goto LBL_ERR; } /* 6. if u >= v then */ if (mp_cmp(&u, &v) != MP_LT) { /* u = u - v, B = B - D */ - if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) goto LBL_ERR; - if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) goto LBL_ERR; } else { /* v - v - u, D = D - B */ - if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) goto LBL_ERR; - if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) goto LBL_ERR; } /* if not zero goto step 4 */ @@ -125,16 +99,12 @@ top: /* b is now the inverse */ neg = a->sign; while (D.sign == MP_NEG) { - if ((err = mp_add(&D, b, &D)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_add(&D, b, &D)) != MP_OKAY) goto LBL_ERR; } /* too big */ while (mp_cmp_mag(&D, b) != MP_LT) { - if ((err = mp_sub(&D, b, &D)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&D, b, &D)) != MP_OKAY) goto LBL_ERR; } mp_exch(&D, c); diff --git a/libtommath/bn_s_mp_invmod_slow.c b/libtommath/bn_s_mp_invmod_slow.c index c03a3d7..4c5db33 100644 --- a/libtommath/bn_s_mp_invmod_slow.c +++ b/libtommath/bn_s_mp_invmod_slow.c @@ -21,12 +21,8 @@ mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c) } /* x = a, y = b */ - if ((err = mp_mod(a, b, &x)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_copy(b, &y)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_mod(a, b, &x)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_copy(b, &y)) != MP_OKAY) goto LBL_ERR; /* 2. [modified] if x,y are both even then return an error! */ if (MP_IS_EVEN(&x) && MP_IS_EVEN(&y)) { @@ -35,12 +31,8 @@ mp_err s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c) } /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ - if ((err = mp_copy(&x, &u)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_copy(&y, &v)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_copy(&x, &u)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_copy(&y, &v)) != MP_OKAY) goto LBL_ERR; mp_set(&A, 1uL); mp_set(&D, 1uL); @@ -48,80 +40,50 @@ top: /* 4. while u is even do */ while (MP_IS_EVEN(&u)) { /* 4.1 u = u/2 */ - if ((err = mp_div_2(&u, &u)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_div_2(&u, &u)) != MP_OKAY) goto LBL_ERR; + /* 4.2 if A or B is odd then */ if (MP_IS_ODD(&A) || MP_IS_ODD(&B)) { /* A = (A+y)/2, B = (B-x)/2 */ - if ((err = mp_add(&A, &y, &A)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_add(&A, &y, &A)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_sub(&B, &x, &B)) != MP_OKAY) goto LBL_ERR; } /* A = A/2, B = B/2 */ - if ((err = mp_div_2(&A, &A)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_div_2(&B, &B)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_div_2(&A, &A)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_div_2(&B, &B)) != MP_OKAY) goto LBL_ERR; } /* 5. while v is even do */ while (MP_IS_EVEN(&v)) { /* 5.1 v = v/2 */ - if ((err = mp_div_2(&v, &v)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_div_2(&v, &v)) != MP_OKAY) goto LBL_ERR; + /* 5.2 if C or D is odd then */ if (MP_IS_ODD(&C) || MP_IS_ODD(&D)) { /* C = (C+y)/2, D = (D-x)/2 */ - if ((err = mp_add(&C, &y, &C)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_add(&C, &y, &C)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_sub(&D, &x, &D)) != MP_OKAY) goto LBL_ERR; } /* C = C/2, D = D/2 */ - if ((err = mp_div_2(&C, &C)) != MP_OKAY) { - goto LBL_ERR; - } - if ((err = mp_div_2(&D, &D)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_div_2(&C, &C)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_div_2(&D, &D)) != MP_OKAY) goto LBL_ERR; } /* 6. if u >= v then */ if (mp_cmp(&u, &v) != MP_LT) { /* u = u - v, A = A - C, B = B - D */ - if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&u, &v, &u)) != MP_OKAY) goto LBL_ERR; - if ((err = mp_sub(&A, &C, &A)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&A, &C, &A)) != MP_OKAY) goto LBL_ERR; - if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&B, &D, &B)) != MP_OKAY) goto LBL_ERR; } else { /* v - v - u, C = C - A, D = D - B */ - if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&v, &u, &v)) != MP_OKAY) goto LBL_ERR; - if ((err = mp_sub(&C, &A, &C)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&C, &A, &C)) != MP_OKAY) goto LBL_ERR; - if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&D, &B, &D)) != MP_OKAY) goto LBL_ERR; } /* if not zero goto step 4 */ @@ -139,16 +101,12 @@ top: /* if its too low */ while (mp_cmp_d(&C, 0uL) == MP_LT) { - if ((err = mp_add(&C, b, &C)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_add(&C, b, &C)) != MP_OKAY) goto LBL_ERR; } /* too big */ while (mp_cmp_mag(&C, b) != MP_LT) { - if ((err = mp_sub(&C, b, &C)) != MP_OKAY) { - goto LBL_ERR; - } + if ((err = mp_sub(&C, b, &C)) != MP_OKAY) goto LBL_ERR; } /* C is now the inverse */ diff --git a/libtommath/bn_s_mp_mul_high_digs.c b/libtommath/bn_s_mp_mul_high_digs.c index e5e1ba4..2bb2a50 100644 --- a/libtommath/bn_s_mp_mul_high_digs.c +++ b/libtommath/bn_s_mp_mul_high_digs.c @@ -16,12 +16,11 @@ mp_err s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) mp_digit tmpx, *tmpt, *tmpy; /* can we use the fast multiplier? */ -#ifdef BN_S_MP_MUL_HIGH_DIGS_FAST_C - if (((a->used + b->used + 1) < MP_WARRAY) + if (MP_HAS(S_MP_MUL_HIGH_DIGS_FAST) + && ((a->used + b->used + 1) < MP_WARRAY) && (MP_MIN(a->used, b->used) < MP_MAXFAST)) { return s_mp_mul_high_digs_fast(a, b, c, digs); } -#endif if ((err = mp_init_size(&t, a->used + b->used + 1)) != MP_OKAY) { return err; diff --git a/libtommath/bn_s_mp_rand_platform.c b/libtommath/bn_s_mp_rand_platform.c index 1349b7c..07555db 100644 --- a/libtommath/bn_s_mp_rand_platform.c +++ b/libtommath/bn_s_mp_rand_platform.c @@ -8,11 +8,16 @@ * - Windows */ #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) -# define MP_ARC4RANDOM +#define BN_S_READ_ARC4RANDOM_C +static mp_err s_read_arc4random(void *p, size_t n) +{ + arc4random_buf(p, n); + return MP_OKAY; +} #endif #if defined(_WIN32) || defined(_WIN32_WCE) -#define MP_WIN_CSP +#define BN_S_READ_WINCSP_C #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0400 @@ -33,7 +38,7 @@ # pragma warning(pop) #endif -static mp_err s_read_win_csp(void *p, size_t n) +static mp_err s_read_wincsp(void *p, size_t n) { static HCRYPTPROV hProv = 0; if (hProv == 0) { @@ -50,9 +55,9 @@ static mp_err s_read_win_csp(void *p, size_t n) } #endif /* WIN32 */ -#if !defined(MP_WIN_CSP) && defined(__linux__) && defined(__GLIBC_PREREQ) +#if !defined(BN_S_READ_WINCSP_C) && defined(__linux__) && defined(__GLIBC_PREREQ) #if __GLIBC_PREREQ(2, 25) -#define MP_GETRANDOM +#define BN_S_READ_GETRANDOM_C #include #include @@ -78,7 +83,8 @@ static mp_err s_read_getrandom(void *p, size_t n) /* We assume all platforms besides windows provide "/dev/urandom". * In case yours doesn't, define MP_NO_DEV_URANDOM at compile-time. */ -#if !defined(MP_WIN_CSP) && !defined(MP_NO_DEV_URANDOM) +#if !defined(BN_S_READ_WINCSP_C) && !defined(MP_NO_DEV_URANDOM) +#define BN_S_READ_URANDOM_C #ifndef MP_DEV_URANDOM #define MP_DEV_URANDOM "/dev/urandom" #endif @@ -86,7 +92,7 @@ static mp_err s_read_getrandom(void *p, size_t n) #include #include -static mp_err s_read_dev_urandom(void *p, size_t n) +static mp_err s_read_urandom(void *p, size_t n) { int fd; char *q = (char *)p; @@ -115,6 +121,7 @@ static mp_err s_read_dev_urandom(void *p, size_t n) #endif #if defined(MP_PRNG_ENABLE_LTM_RNG) +#define BN_S_READ_LTM_RNG unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void)); void (*ltm_rng_callback)(void); @@ -128,37 +135,21 @@ static mp_err s_read_ltm_rng(void *p, size_t n) } #endif +mp_err s_read_arc4random(void *p, size_t n); +mp_err s_read_wincsp(void *p, size_t n); +mp_err s_read_getrandom(void *p, size_t n); +mp_err s_read_urandom(void *p, size_t n); +mp_err s_read_ltm_rng(void *p, size_t n); + mp_err s_mp_rand_platform(void *p, size_t n) { -#if defined(MP_ARC4RANDOM) - arc4random_buf(p, n); - return MP_OKAY; -#else - - mp_err res = MP_ERR; - -#if defined(MP_WIN_CSP) - res = s_read_win_csp(p, n); - if (res == MP_OKAY) return res; -#endif - -#if defined(MP_GETRANDOM) - res = s_read_getrandom(p, n); - if (res == MP_OKAY) return res; -#endif - -#if defined(MP_DEV_URANDOM) - res = s_read_dev_urandom(p, n); - if (res == MP_OKAY) return res; -#endif - -#if defined(MP_PRNG_ENABLE_LTM_RNG) - res = s_read_ltm_rng(p, n); - if (res == MP_OKAY) return res; -#endif - - return res; -#endif + mp_err err = MP_ERR; + if ((err != MP_OKAY) && MP_HAS(S_READ_ARC4RANDOM)) err = s_read_arc4random(p, n); + if ((err != MP_OKAY) && MP_HAS(S_READ_WINCSP)) err = s_read_wincsp(p, n); + if ((err != MP_OKAY) && MP_HAS(S_READ_GETRANDOM)) err = s_read_getrandom(p, n); + if ((err != MP_OKAY) && MP_HAS(S_READ_URANDOM)) err = s_read_urandom(p, n); + if ((err != MP_OKAY) && MP_HAS(S_READ_LTM_RNG)) err = s_read_ltm_rng(p, n); + return err; } #endif diff --git a/libtommath/bn_s_mp_reverse.c b/libtommath/bn_s_mp_reverse.c index 92b0f1f..c549e60 100644 --- a/libtommath/bn_s_mp_reverse.c +++ b/libtommath/bn_s_mp_reverse.c @@ -4,13 +4,13 @@ /* SPDX-License-Identifier: Unlicense */ /* reverse an array, used for radix code */ -void s_mp_reverse(unsigned char *s, int len) +void s_mp_reverse(unsigned char *s, size_t len) { - int ix, iy; + size_t ix, iy; unsigned char t; - ix = 0; - iy = len - 1; + ix = 0u; + iy = len - 1u; while (ix < iy) { t = s[ix]; s[ix] = s[iy]; diff --git a/libtommath/bn_s_mp_toom_mul.c b/libtommath/bn_s_mp_toom_mul.c index ce47f25..8efd803 100644 --- a/libtommath/bn_s_mp_toom_mul.c +++ b/libtommath/bn_s_mp_toom_mul.c @@ -43,226 +43,170 @@ mp_err s_mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c) B = MP_MIN(a->used, b->used) / 3; /** a = a2 * x^2 + a1 * x + a0; */ - if ((err = mp_init_size(&a0, B)) != MP_OKAY) { - goto LTM_ERRa0; - } + if ((err = mp_init_size(&a0, B)) != MP_OKAY) goto LBL_ERRa0; + for (count = 0; count < B; count++) { a0.dp[count] = a->dp[count]; a0.used++; } mp_clamp(&a0); - if ((err = mp_init_size(&a1, B)) != MP_OKAY) { - goto LTM_ERRa1; - } + if ((err = mp_init_size(&a1, B)) != MP_OKAY) goto LBL_ERRa1; for (; count < (2 * B); count++) { a1.dp[count - B] = a->dp[count]; a1.used++; } mp_clamp(&a1); - if ((err = mp_init_size(&a2, B + (a->used - (3 * B)))) != MP_OKAY) { - goto LTM_ERRa2; - } + if ((err = mp_init_size(&a2, B + (a->used - (3 * B)))) != MP_OKAY) goto LBL_ERRa2; for (; count < a->used; count++) { a2.dp[count - (2 * B)] = a->dp[count]; a2.used++; } + mp_clamp(&a2); /** b = b2 * x^2 + b1 * x + b0; */ - if ((err = mp_init_size(&b0, B)) != MP_OKAY) { - goto LTM_ERRb0; - } + if ((err = mp_init_size(&b0, B)) != MP_OKAY) goto LBL_ERRb0; for (count = 0; count < B; count++) { b0.dp[count] = b->dp[count]; b0.used++; } mp_clamp(&b0); - if ((err = mp_init_size(&b1, B)) != MP_OKAY) { - goto LTM_ERRb1; - } + if ((err = mp_init_size(&b1, B)) != MP_OKAY) goto LBL_ERRb1; for (; count < (2 * B); count++) { b1.dp[count - B] = b->dp[count]; b1.used++; } mp_clamp(&b1); - if ((err = mp_init_size(&b2, B + (b->used - (3 * B)))) != MP_OKAY) { - goto LTM_ERRb2; - } + if ((err = mp_init_size(&b2, B + (b->used - (3 * B)))) != MP_OKAY) goto LBL_ERRb2; for (; count < b->used; count++) { b2.dp[count - (2 * B)] = b->dp[count]; b2.used++; } + mp_clamp(&b2); /** \\ S1 = (a2+a1+a0) * (b2+b1+b0); */ /** T1 = a2 + a1; */ - if ((err = mp_add(&a2, &a1, &T1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(&a2, &a1, &T1)) != MP_OKAY) goto LBL_ERR; + /** S2 = T1 + a0; */ - if ((err = mp_add(&T1, &a0, &S2)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(&T1, &a0, &S2)) != MP_OKAY) goto LBL_ERR; + /** c = b2 + b1; */ - if ((err = mp_add(&b2, &b1, c)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(&b2, &b1, c)) != MP_OKAY) goto LBL_ERR; + /** S1 = c + b0; */ - if ((err = mp_add(c, &b0, &S1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(c, &b0, &S1)) != MP_OKAY) goto LBL_ERR; + /** S1 = S1 * S2; */ - if ((err = mp_mul(&S1, &S2, &S1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_mul(&S1, &S2, &S1)) != MP_OKAY) goto LBL_ERR; + /** \\S2 = (4*a2+2*a1+a0) * (4*b2+2*b1+b0); */ /** T1 = T1 + a2; */ - if ((err = mp_add(&T1, &a2, &T1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(&T1, &a2, &T1)) != MP_OKAY) goto LBL_ERR; + /** T1 = T1 << 1; */ - if ((err = mp_mul_2(&T1, &T1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_mul_2(&T1, &T1)) != MP_OKAY) goto LBL_ERR; + /** T1 = T1 + a0; */ - if ((err = mp_add(&T1, &a0, &T1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(&T1, &a0, &T1)) != MP_OKAY) goto LBL_ERR; + /** c = c + b2; */ - if ((err = mp_add(c, &b2, c)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(c, &b2, c)) != MP_OKAY) goto LBL_ERR; + /** c = c << 1; */ - if ((err = mp_mul_2(c, c)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_mul_2(c, c)) != MP_OKAY) goto LBL_ERR; + /** c = c + b0; */ - if ((err = mp_add(c, &b0, c)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(c, &b0, c)) != MP_OKAY) goto LBL_ERR; + /** S2 = T1 * c; */ - if ((err = mp_mul(&T1, c, &S2)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_mul(&T1, c, &S2)) != MP_OKAY) goto LBL_ERR; + /** \\S3 = (a2-a1+a0) * (b2-b1+b0); */ /** a1 = a2 - a1; */ - if ((err = mp_sub(&a2, &a1, &a1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&a2, &a1, &a1)) != MP_OKAY) goto LBL_ERR; + /** a1 = a1 + a0; */ - if ((err = mp_add(&a1, &a0, &a1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(&a1, &a0, &a1)) != MP_OKAY) goto LBL_ERR; + /** b1 = b2 - b1; */ - if ((err = mp_sub(&b2, &b1, &b1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&b2, &b1, &b1)) != MP_OKAY) goto LBL_ERR; + /** b1 = b1 + b0; */ - if ((err = mp_add(&b1, &b0, &b1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(&b1, &b0, &b1)) != MP_OKAY) goto LBL_ERR; + /** a1 = a1 * b1; */ - if ((err = mp_mul(&a1, &b1, &a1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_mul(&a1, &b1, &a1)) != MP_OKAY) goto LBL_ERR; + /** b1 = a2 * b2; */ - if ((err = mp_mul(&a2, &b2, &b1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_mul(&a2, &b2, &b1)) != MP_OKAY) goto LBL_ERR; + /** \\S2 = (S2 - S3)/3; */ /** S2 = S2 - a1; */ - if ((err = mp_sub(&S2, &a1, &S2)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&S2, &a1, &S2)) != MP_OKAY) goto LBL_ERR; + /** S2 = S2 / 3; \\ this is an exact division */ - if ((err = mp_div_3(&S2, &S2, NULL)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_div_3(&S2, &S2, NULL)) != MP_OKAY) goto LBL_ERR; + /** a1 = S1 - a1; */ - if ((err = mp_sub(&S1, &a1, &a1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&S1, &a1, &a1)) != MP_OKAY) goto LBL_ERR; + /** a1 = a1 >> 1; */ - if ((err = mp_div_2(&a1, &a1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_div_2(&a1, &a1)) != MP_OKAY) goto LBL_ERR; + /** a0 = a0 * b0; */ - if ((err = mp_mul(&a0, &b0, &a0)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_mul(&a0, &b0, &a0)) != MP_OKAY) goto LBL_ERR; + /** S1 = S1 - a0; */ - if ((err = mp_sub(&S1, &a0, &S1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&S1, &a0, &S1)) != MP_OKAY) goto LBL_ERR; + /** S2 = S2 - S1; */ - if ((err = mp_sub(&S2, &S1, &S2)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&S2, &S1, &S2)) != MP_OKAY) goto LBL_ERR; + /** S2 = S2 >> 1; */ - if ((err = mp_div_2(&S2, &S2)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_div_2(&S2, &S2)) != MP_OKAY) goto LBL_ERR; + /** S1 = S1 - a1; */ - if ((err = mp_sub(&S1, &a1, &S1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&S1, &a1, &S1)) != MP_OKAY) goto LBL_ERR; + /** S1 = S1 - b1; */ - if ((err = mp_sub(&S1, &b1, &S1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&S1, &b1, &S1)) != MP_OKAY) goto LBL_ERR; + /** T1 = b1 << 1; */ - if ((err = mp_mul_2(&b1, &T1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_mul_2(&b1, &T1)) != MP_OKAY) goto LBL_ERR; + /** S2 = S2 - T1; */ - if ((err = mp_sub(&S2, &T1, &S2)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&S2, &T1, &S2)) != MP_OKAY) goto LBL_ERR; + /** a1 = a1 - S2; */ - if ((err = mp_sub(&a1, &S2, &a1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&a1, &S2, &a1)) != MP_OKAY) goto LBL_ERR; + /** P = b1*x^4+ S2*x^3+ S1*x^2+ a1*x + a0; */ - if ((err = mp_lshd(&b1, 4 * B)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_lshd(&S2, 3 * B)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_add(&b1, &S2, &b1)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_lshd(&S1, 2 * B)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_add(&b1, &S1, &b1)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_lshd(&a1, 1 * B)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_add(&b1, &a1, &b1)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_add(&b1, &a0, c)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_lshd(&b1, 4 * B)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_lshd(&S2, 3 * B)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_add(&b1, &S2, &b1)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_lshd(&S1, 2 * B)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_add(&b1, &S1, &b1)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_lshd(&a1, 1 * B)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_add(&b1, &a1, &b1)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_add(&b1, &a0, c)) != MP_OKAY) goto LBL_ERR; /** a * b - P */ -LTM_ERR: +LBL_ERR: mp_clear(&b2); -LTM_ERRb2: +LBL_ERRb2: mp_clear(&b1); -LTM_ERRb1: +LBL_ERRb1: mp_clear(&b0); -LTM_ERRb0: +LBL_ERRb0: mp_clear(&a2); -LTM_ERRa2: +LBL_ERRa2: mp_clear(&a1); -LTM_ERRa1: +LBL_ERRa1: mp_clear(&a0); -LTM_ERRa0: +LBL_ERRa0: mp_clear_multi(&S1, &S2, &T1, NULL); return err; } diff --git a/libtommath/bn_s_mp_toom_sqr.c b/libtommath/bn_s_mp_toom_sqr.c index a6bf85e..9eaa9d0 100644 --- a/libtommath/bn_s_mp_toom_sqr.c +++ b/libtommath/bn_s_mp_toom_sqr.c @@ -34,17 +34,13 @@ mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b) B = a->used / 3; /** a = a2 * x^2 + a1 * x + a0; */ - if ((err = mp_init_size(&a0, B)) != MP_OKAY) { - goto LTM_ERRa0; - } + if ((err = mp_init_size(&a0, B)) != MP_OKAY) goto LBL_ERRa0; + a0.used = B; - if ((err = mp_init_size(&a1, B)) != MP_OKAY) { - goto LTM_ERRa1; - } + if ((err = mp_init_size(&a1, B)) != MP_OKAY) goto LBL_ERRa1; a1.used = B; - if ((err = mp_init_size(&a2, B + (a->used - (3 * B)))) != MP_OKAY) { - goto LTM_ERRa2; - } + if ((err = mp_init_size(&a2, B + (a->used - (3 * B)))) != MP_OKAY) goto LBL_ERRa2; + tmpa = a->dp; tmpc = a0.dp; for (count = 0; count < B; count++) { @@ -61,131 +57,87 @@ mp_err s_mp_toom_sqr(const mp_int *a, mp_int *b) } mp_clamp(&a0); mp_clamp(&a1); + mp_clamp(&a2); /** S0 = a0^2; */ - if ((err = mp_sqr(&a0, &S0)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sqr(&a0, &S0)) != MP_OKAY) goto LBL_ERR; + /** \\S1 = (a2 + a1 + a0)^2 */ /** \\S2 = (a2 - a1 + a0)^2 */ /** \\S1 = a0 + a2; */ /** a0 = a0 + a2; */ - if ((err = mp_add(&a0, &a2, &a0)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(&a0, &a2, &a0)) != MP_OKAY) goto LBL_ERR; /** \\S2 = S1 - a1; */ /** b = a0 - a1; */ - if ((err = mp_sub(&a0, &a1, b)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&a0, &a1, b)) != MP_OKAY) goto LBL_ERR; /** \\S1 = S1 + a1; */ /** a0 = a0 + a1; */ - if ((err = mp_add(&a0, &a1, &a0)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(&a0, &a1, &a0)) != MP_OKAY) goto LBL_ERR; /** \\S1 = S1^2; */ /** a0 = a0^2; */ - if ((err = mp_sqr(&a0, &a0)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sqr(&a0, &a0)) != MP_OKAY) goto LBL_ERR; /** \\S2 = S2^2; */ /** b = b^2; */ - if ((err = mp_sqr(b, b)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sqr(b, b)) != MP_OKAY) goto LBL_ERR; /** \\ S3 = 2 * a1 * a2 */ /** \\S3 = a1 * a2; */ /** a1 = a1 * a2; */ - if ((err = mp_mul(&a1, &a2, &a1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_mul(&a1, &a2, &a1)) != MP_OKAY) goto LBL_ERR; /** \\S3 = S3 << 1; */ /** a1 = a1 << 1; */ - if ((err = mp_mul_2(&a1, &a1)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_mul_2(&a1, &a1)) != MP_OKAY) goto LBL_ERR; /** \\S4 = a2^2; */ /** a2 = a2^2; */ - if ((err = mp_sqr(&a2, &a2)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sqr(&a2, &a2)) != MP_OKAY) goto LBL_ERR; /** \\ tmp = (S1 + S2)/2 */ /** \\tmp = S1 + S2; */ /** b = a0 + b; */ - if ((err = mp_add(&a0, b, b)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_add(&a0, b, b)) != MP_OKAY) goto LBL_ERR; /** \\tmp = tmp >> 1; */ /** b = b >> 1; */ - if ((err = mp_div_2(b, b)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_div_2(b, b)) != MP_OKAY) goto LBL_ERR; /** \\ S1 = S1 - tmp - S3 */ /** \\S1 = S1 - tmp; */ /** a0 = a0 - b; */ - if ((err = mp_sub(&a0, b, &a0)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&a0, b, &a0)) != MP_OKAY) goto LBL_ERR; /** \\S1 = S1 - S3; */ /** a0 = a0 - a1; */ - if ((err = mp_sub(&a0, &a1, &a0)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(&a0, &a1, &a0)) != MP_OKAY) goto LBL_ERR; /** \\S2 = tmp - S4 -S0 */ /** \\S2 = tmp - S4; */ /** b = b - a2; */ - if ((err = mp_sub(b, &a2, b)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(b, &a2, b)) != MP_OKAY) goto LBL_ERR; /** \\S2 = S2 - S0; */ /** b = b - S0; */ - if ((err = mp_sub(b, &S0, b)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_sub(b, &S0, b)) != MP_OKAY) goto LBL_ERR; /** \\P = S4*x^4 + S3*x^3 + S2*x^2 + S1*x + S0; */ /** P = a2*x^4 + a1*x^3 + b*x^2 + a0*x + S0; */ - if ((err = mp_lshd(&a2, 4 * B)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_lshd(&a1, 3 * B)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_lshd(b, 2 * B)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_lshd(&a0, 1 * B)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_add(&a2, &a1, &a2)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_add(&a2, b, b)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_add(b, &a0, b)) != MP_OKAY) { - goto LTM_ERR; - } - if ((err = mp_add(b, &S0, b)) != MP_OKAY) { - goto LTM_ERR; - } + if ((err = mp_lshd(&a2, 4 * B)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_lshd(&a1, 3 * B)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_lshd(b, 2 * B)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_lshd(&a0, 1 * B)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_add(&a2, &a1, &a2)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_add(&a2, b, b)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_add(b, &a0, b)) != MP_OKAY) goto LBL_ERR; + if ((err = mp_add(b, &S0, b)) != MP_OKAY) goto LBL_ERR; /** a^2 - P */ -LTM_ERR: +LBL_ERR: mp_clear(&a2); -LTM_ERRa2: +LBL_ERRa2: mp_clear(&a1); -LTM_ERRa1: +LBL_ERRa1: mp_clear(&a0); -LTM_ERRa0: +LBL_ERRa0: mp_clear(&S0); return err; diff --git a/libtommath/helper.pl b/libtommath/helper.pl index bde8cdf..e60c1a7 100755 --- a/libtommath/helper.pl +++ b/libtommath/helper.pl @@ -381,7 +381,7 @@ EOS # scan for mp_* and make classes my @deps = (); foreach my $line (split /\n/, $content) { - while ($line =~ /(fast_)?(s_)?mp\_[a-z_0-9]*(?=\()|(?<=\()mp\_[a-z_0-9]*(?=,)/g) { + while ($line =~ /(fast_)?(s_)?mp\_[a-z_0-9]*((?=\;)|(?=\())|(?<=\()mp\_[a-z_0-9]*(?=\()/g) { my $a = $&; next if $a eq "mp_err"; $a =~ tr/[a-z]/[A-Z]/; diff --git a/libtommath/libtommath_VS2008.vcproj b/libtommath/libtommath_VS2008.vcproj index bfe9bfd..6a1a294 100644 --- a/libtommath/libtommath_VS2008.vcproj +++ b/libtommath/libtommath_VS2008.vcproj @@ -449,6 +449,14 @@ > + + + + @@ -689,14 +697,6 @@ > - - - - @@ -737,6 +737,10 @@ > + + @@ -781,10 +785,6 @@ > - - @@ -817,31 +817,19 @@ > - - - - - - " diff --git a/libtommath/makefile.mingw b/libtommath/makefile.mingw index 793415d..2d334d0 100644 --- a/libtommath/makefile.mingw +++ b/libtommath/makefile.mingw @@ -34,34 +34,32 @@ bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cm bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o bn_mp_div.o bn_mp_div_2.o \ bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o bn_mp_dr_reduce.o bn_mp_dr_setup.o \ bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_u32.o bn_mp_exptmod.o bn_mp_exteuclid.o \ -bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o bn_mp_get_i32.o bn_mp_get_i64.o bn_mp_get_l.o \ -bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o bn_mp_get_mag_ul.o bn_mp_get_mag_ull.o \ -bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o bn_mp_init_i32.o \ -bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o \ -bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.o bn_mp_invmod.o bn_mp_is_square.o \ -bn_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o \ -bn_mp_mod_d.o bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o \ -bn_mp_mul.o bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_neg.o bn_mp_or.o \ -bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \ +bn_mp_fread.o bn_mp_from_sbin.o bn_mp_from_ubin.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o \ +bn_mp_get_i32.o bn_mp_get_i64.o bn_mp_get_l.o bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o \ +bn_mp_get_mag_ul.o bn_mp_get_mag_ull.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o \ +bn_mp_init_copy.o bn_mp_init_i32.o bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o \ +bn_mp_init_set.o bn_mp_init_size.o bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.o \ +bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o \ +bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o \ +bn_mp_montgomery_setup.o bn_mp_mul.o bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_neg.o \ +bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \ bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \ bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \ -bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.o bn_mp_reduce.o \ -bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \ -bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_root_u32.o bn_mp_rshd.o \ -bn_mp_set.o bn_mp_set_double.o bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o \ -bn_mp_set_u32.o bn_mp_set_u64.o bn_mp_set_ul.o bn_mp_set_ull.o bn_mp_shrink.o bn_mp_signed_bin_size.o \ -bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o bn_mp_sub_d.o \ -bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o \ -bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o \ -bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o \ -bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o \ -bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o \ -bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_prime_is_divisible.o \ -bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o \ -bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o +bn_mp_rand.o bn_mp_read_radix.o bn_mp_reduce.o bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o \ +bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o \ +bn_mp_reduce_setup.o bn_mp_root_u32.o bn_mp_rshd.o bn_mp_sbin_size.o bn_mp_set.o bn_mp_set_double.o \ +bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o bn_mp_set_u32.o bn_mp_set_u64.o \ +bn_mp_set_ul.o bn_mp_set_ull.o bn_mp_shrink.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o \ +bn_mp_sqrtmod_prime.o bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_radix.o bn_mp_to_sbin.o \ +bn_mp_to_ubin.o bn_mp_ubin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o \ +bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o \ +bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \ +bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \ +bn_s_mp_prime_is_divisible.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \ +bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o HEADERS_PUB=tommath.h -HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB) +HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB) #The default rule for make builds the libtommath.a library (static) default: $(LIBMAIN_S) @@ -82,14 +80,15 @@ $(LIBMAIN_D) $(LIBMAIN_I): $(OBJECTS) $(CC) -s -shared -o $(LIBMAIN_D) $^ -Wl,--enable-auto-import,--export-all -Wl,--out-implib=$(LIBMAIN_I) $(LTM_LDFLAGS) $(STRIP) -S $(LIBMAIN_D) -#Build test_standalone suite -test.exe: demo/main.c demo/opponent.c demo/test.c $(LIBMAIN_S) - $(CC) $(LTM_CFLAGS) $(LTM_LDFLAGS) $^ -DLTM_DEMO_TEST_VS_MTEST=0 -o $@ +#Build test suite +test.exe: demo/shared.o demo/test.o $(LIBMAIN_S) + $(CC) $(LTM_CFLAGS) $(LTM_LDFLAGS) $^ -o $@ @echo NOTICE: start the tests by launching test.exe test_standalone: test.exe + @echo test_standalone is deprecated, please use make-target 'test.exe' -all: $(LIBMAIN_S) test_standalone +all: $(LIBMAIN_S) test.exe tune: $(LIBNAME_S) $(MAKE) -C etc tune diff --git a/libtommath/makefile.msvc b/libtommath/makefile.msvc index e693834..8419b53 100644 --- a/libtommath/makefile.msvc +++ b/libtommath/makefile.msvc @@ -14,7 +14,7 @@ PREFIX = c:\devel CFLAGS = /Ox #Compilation flags -LTM_CFLAGS = /nologo /I./ /D_CRT_SECURE_NO_WARNINGS /D_CRT_NONSTDC_NO_DEPRECATE /D__STDC_WANT_SECURE_LIB__=1 /D_CRT_HAS_CXX17=0 /Wall /wd4146 /wd4127 /wd4710 /wd4711 /wd4820 /WX $(CFLAGS) +LTM_CFLAGS = /nologo /I./ /D_CRT_SECURE_NO_WARNINGS /D_CRT_NONSTDC_NO_DEPRECATE /D__STDC_WANT_SECURE_LIB__=1 /D_CRT_HAS_CXX17=0 /Wall /wd4146 /wd4127 /wd4710 /wd4711 /wd4820 /wd4003 /WX $(CFLAGS) LTM_LDFLAGS = advapi32.lib #Libraries to be created (this makefile builds only static libraries) @@ -26,34 +26,32 @@ bn_mp_and.obj bn_mp_clamp.obj bn_mp_clear.obj bn_mp_clear_multi.obj bn_mp_cmp.ob bn_mp_cnt_lsb.obj bn_mp_complement.obj bn_mp_copy.obj bn_mp_count_bits.obj bn_mp_decr.obj bn_mp_div.obj bn_mp_div_2.obj \ bn_mp_div_2d.obj bn_mp_div_3.obj bn_mp_div_d.obj bn_mp_dr_is_modulus.obj bn_mp_dr_reduce.obj bn_mp_dr_setup.obj \ bn_mp_error_to_string.obj bn_mp_exch.obj bn_mp_export.obj bn_mp_expt_u32.obj bn_mp_exptmod.obj bn_mp_exteuclid.obj \ -bn_mp_fread.obj bn_mp_fwrite.obj bn_mp_gcd.obj bn_mp_get_double.obj bn_mp_get_i32.obj bn_mp_get_i64.obj bn_mp_get_l.obj \ -bn_mp_get_ll.obj bn_mp_get_mag_u32.obj bn_mp_get_mag_u64.obj bn_mp_get_mag_ul.obj bn_mp_get_mag_ull.obj \ -bn_mp_grow.obj bn_mp_ilogb.obj bn_mp_import.obj bn_mp_incr.obj bn_mp_init.obj bn_mp_init_copy.obj bn_mp_init_i32.obj \ -bn_mp_init_i64.obj bn_mp_init_l.obj bn_mp_init_ll.obj bn_mp_init_multi.obj bn_mp_init_set.obj bn_mp_init_size.obj \ -bn_mp_init_u32.obj bn_mp_init_u64.obj bn_mp_init_ul.obj bn_mp_init_ull.obj bn_mp_invmod.obj bn_mp_is_square.obj \ -bn_mp_iseven.obj bn_mp_isodd.obj bn_mp_kronecker.obj bn_mp_lcm.obj bn_mp_lshd.obj bn_mp_mod.obj bn_mp_mod_2d.obj \ -bn_mp_mod_d.obj bn_mp_montgomery_calc_normalization.obj bn_mp_montgomery_reduce.obj bn_mp_montgomery_setup.obj \ -bn_mp_mul.obj bn_mp_mul_2.obj bn_mp_mul_2d.obj bn_mp_mul_d.obj bn_mp_mulmod.obj bn_mp_neg.obj bn_mp_or.obj \ -bn_mp_prime_fermat.obj bn_mp_prime_frobenius_underwood.obj bn_mp_prime_is_prime.obj \ +bn_mp_fread.obj bn_mp_from_sbin.obj bn_mp_from_ubin.obj bn_mp_fwrite.obj bn_mp_gcd.obj bn_mp_get_double.obj \ +bn_mp_get_i32.obj bn_mp_get_i64.obj bn_mp_get_l.obj bn_mp_get_ll.obj bn_mp_get_mag_u32.obj bn_mp_get_mag_u64.obj \ +bn_mp_get_mag_ul.obj bn_mp_get_mag_ull.obj bn_mp_grow.obj bn_mp_ilogb.obj bn_mp_import.obj bn_mp_incr.obj bn_mp_init.obj \ +bn_mp_init_copy.obj bn_mp_init_i32.obj bn_mp_init_i64.obj bn_mp_init_l.obj bn_mp_init_ll.obj bn_mp_init_multi.obj \ +bn_mp_init_set.obj bn_mp_init_size.obj bn_mp_init_u32.obj bn_mp_init_u64.obj bn_mp_init_ul.obj bn_mp_init_ull.obj \ +bn_mp_invmod.obj bn_mp_is_square.obj bn_mp_iseven.obj bn_mp_isodd.obj bn_mp_kronecker.obj bn_mp_lcm.obj bn_mp_lshd.obj \ +bn_mp_mod.obj bn_mp_mod_2d.obj bn_mp_mod_d.obj bn_mp_montgomery_calc_normalization.obj bn_mp_montgomery_reduce.obj \ +bn_mp_montgomery_setup.obj bn_mp_mul.obj bn_mp_mul_2.obj bn_mp_mul_2d.obj bn_mp_mul_d.obj bn_mp_mulmod.obj bn_mp_neg.obj \ +bn_mp_or.obj bn_mp_prime_fermat.obj bn_mp_prime_frobenius_underwood.obj bn_mp_prime_is_prime.obj \ bn_mp_prime_miller_rabin.obj bn_mp_prime_next_prime.obj bn_mp_prime_rabin_miller_trials.obj \ bn_mp_prime_rand.obj bn_mp_prime_strong_lucas_selfridge.obj bn_mp_radix_size.obj bn_mp_radix_smap.obj \ -bn_mp_rand.obj bn_mp_read_radix.obj bn_mp_read_signed_bin.obj bn_mp_read_unsigned_bin.obj bn_mp_reduce.obj \ -bn_mp_reduce_2k.obj bn_mp_reduce_2k_l.obj bn_mp_reduce_2k_setup.obj bn_mp_reduce_2k_setup_l.obj \ -bn_mp_reduce_is_2k.obj bn_mp_reduce_is_2k_l.obj bn_mp_reduce_setup.obj bn_mp_root_u32.obj bn_mp_rshd.obj \ -bn_mp_set.obj bn_mp_set_double.obj bn_mp_set_i32.obj bn_mp_set_i64.obj bn_mp_set_l.obj bn_mp_set_ll.obj \ -bn_mp_set_u32.obj bn_mp_set_u64.obj bn_mp_set_ul.obj bn_mp_set_ull.obj bn_mp_shrink.obj bn_mp_signed_bin_size.obj \ -bn_mp_signed_rsh.obj bn_mp_sqr.obj bn_mp_sqrmod.obj bn_mp_sqrt.obj bn_mp_sqrtmod_prime.obj bn_mp_sub.obj bn_mp_sub_d.obj \ -bn_mp_submod.obj bn_mp_to_signed_bin.obj bn_mp_to_signed_bin_n.obj bn_mp_to_unsigned_bin.obj \ -bn_mp_to_unsigned_bin_n.obj bn_mp_toradix.obj bn_mp_toradix_n.obj bn_mp_unsigned_bin_size.obj bn_mp_xor.obj \ -bn_mp_zero.obj bn_prime_tab.obj bn_s_mp_add.obj bn_s_mp_balance_mul.obj bn_s_mp_exptmod.obj bn_s_mp_exptmod_fast.obj \ -bn_s_mp_get_bit.obj bn_s_mp_invmod_fast.obj bn_s_mp_invmod_slow.obj bn_s_mp_karatsuba_mul.obj \ -bn_s_mp_karatsuba_sqr.obj bn_s_mp_montgomery_reduce_fast.obj bn_s_mp_mul_digs.obj bn_s_mp_mul_digs_fast.obj \ -bn_s_mp_mul_high_digs.obj bn_s_mp_mul_high_digs_fast.obj bn_s_mp_prime_is_divisible.obj \ -bn_s_mp_rand_jenkins.obj bn_s_mp_rand_platform.obj bn_s_mp_reverse.obj bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj \ -bn_s_mp_sub.obj bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj +bn_mp_rand.obj bn_mp_read_radix.obj bn_mp_reduce.obj bn_mp_reduce_2k.obj bn_mp_reduce_2k_l.obj \ +bn_mp_reduce_2k_setup.obj bn_mp_reduce_2k_setup_l.obj bn_mp_reduce_is_2k.obj bn_mp_reduce_is_2k_l.obj \ +bn_mp_reduce_setup.obj bn_mp_root_u32.obj bn_mp_rshd.obj bn_mp_sbin_size.obj bn_mp_set.obj bn_mp_set_double.obj \ +bn_mp_set_i32.obj bn_mp_set_i64.obj bn_mp_set_l.obj bn_mp_set_ll.obj bn_mp_set_u32.obj bn_mp_set_u64.obj \ +bn_mp_set_ul.obj bn_mp_set_ull.obj bn_mp_shrink.obj bn_mp_signed_rsh.obj bn_mp_sqr.obj bn_mp_sqrmod.obj bn_mp_sqrt.obj \ +bn_mp_sqrtmod_prime.obj bn_mp_sub.obj bn_mp_sub_d.obj bn_mp_submod.obj bn_mp_to_radix.obj bn_mp_to_sbin.obj \ +bn_mp_to_ubin.obj bn_mp_ubin_size.obj bn_mp_xor.obj bn_mp_zero.obj bn_prime_tab.obj bn_s_mp_add.obj \ +bn_s_mp_balance_mul.obj bn_s_mp_exptmod.obj bn_s_mp_exptmod_fast.obj bn_s_mp_get_bit.obj bn_s_mp_invmod_fast.obj \ +bn_s_mp_invmod_slow.obj bn_s_mp_karatsuba_mul.obj bn_s_mp_karatsuba_sqr.obj bn_s_mp_montgomery_reduce_fast.obj \ +bn_s_mp_mul_digs.obj bn_s_mp_mul_digs_fast.obj bn_s_mp_mul_high_digs.obj bn_s_mp_mul_high_digs_fast.obj \ +bn_s_mp_prime_is_divisible.obj bn_s_mp_rand_jenkins.obj bn_s_mp_rand_platform.obj bn_s_mp_reverse.obj \ +bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj bn_s_mp_sub.obj bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj HEADERS_PUB=tommath.h -HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB) +HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB) #The default rule for make builds the tommath.lib library (static) default: $(LIBMAIN_S) @@ -68,14 +66,15 @@ $(OBJECTS): $(HEADERS) $(LIBMAIN_S): $(OBJECTS) lib /out:$(LIBMAIN_S) $(OBJECTS) -#Build test_standalone suite -test.exe: $(LIBMAIN_S) demo/main.c demo/opponent.c demo/test.c - cl $(LTM_CFLAGS) $(TOBJECTS) $(LIBMAIN_S) $(LTM_LDFLAGS) demo/main.c demo/opponent.c demo/test.c /DLTM_DEMO_TEST_VS_MTEST=0 /Fe$@ +#Build test suite +test.exe: $(LIBMAIN_S) demo/shared.obj demo/test.obj + cl $(LTM_CFLAGS) $(TOBJECTS) $(LIBMAIN_S) $(LTM_LDFLAGS) demo/shared.c demo/test.c /Fe$@ @echo NOTICE: start the tests by launching test.exe test_standalone: test.exe + @echo test_standalone is deprecated, please use make-target 'test.exe' -all: $(LIBMAIN_S) test_standalone +all: $(LIBMAIN_S) test.exe tune: $(LIBMAIN_S) $(MAKE) -C etc tune diff --git a/libtommath/makefile.shared b/libtommath/makefile.shared index f000b46..a6dc2bb 100644 --- a/libtommath/makefile.shared +++ b/libtommath/makefile.shared @@ -28,43 +28,41 @@ bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cm bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o bn_mp_div.o bn_mp_div_2.o \ bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o bn_mp_dr_reduce.o bn_mp_dr_setup.o \ bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_u32.o bn_mp_exptmod.o bn_mp_exteuclid.o \ -bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o bn_mp_get_i32.o bn_mp_get_i64.o bn_mp_get_l.o \ -bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o bn_mp_get_mag_ul.o bn_mp_get_mag_ull.o \ -bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o bn_mp_init_i32.o \ -bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o \ -bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.o bn_mp_invmod.o bn_mp_is_square.o \ -bn_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o \ -bn_mp_mod_d.o bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o \ -bn_mp_mul.o bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_neg.o bn_mp_or.o \ -bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \ +bn_mp_fread.o bn_mp_from_sbin.o bn_mp_from_ubin.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o \ +bn_mp_get_i32.o bn_mp_get_i64.o bn_mp_get_l.o bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o \ +bn_mp_get_mag_ul.o bn_mp_get_mag_ull.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o \ +bn_mp_init_copy.o bn_mp_init_i32.o bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o \ +bn_mp_init_set.o bn_mp_init_size.o bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.o \ +bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o \ +bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o \ +bn_mp_montgomery_setup.o bn_mp_mul.o bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_neg.o \ +bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \ bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \ bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \ -bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.o bn_mp_reduce.o \ -bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \ -bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_root_u32.o bn_mp_rshd.o \ -bn_mp_set.o bn_mp_set_double.o bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o \ -bn_mp_set_u32.o bn_mp_set_u64.o bn_mp_set_ul.o bn_mp_set_ull.o bn_mp_shrink.o bn_mp_signed_bin_size.o \ -bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o bn_mp_sub_d.o \ -bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o \ -bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o \ -bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o \ -bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o \ -bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o \ -bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_prime_is_divisible.o \ -bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o \ -bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o +bn_mp_rand.o bn_mp_read_radix.o bn_mp_reduce.o bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o \ +bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o \ +bn_mp_reduce_setup.o bn_mp_root_u32.o bn_mp_rshd.o bn_mp_sbin_size.o bn_mp_set.o bn_mp_set_double.o \ +bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o bn_mp_set_u32.o bn_mp_set_u64.o \ +bn_mp_set_ul.o bn_mp_set_ull.o bn_mp_shrink.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o \ +bn_mp_sqrtmod_prime.o bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_radix.o bn_mp_to_sbin.o \ +bn_mp_to_ubin.o bn_mp_ubin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o \ +bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o \ +bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \ +bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \ +bn_s_mp_prime_is_divisible.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \ +bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o #END_INS objs: $(OBJECTS) -.c.o: - $(LTCOMPILE) $(CFLAGS) $(LDFLAGS) -o $@ -c $< +.c.o: $(HEADERS) + $(LTCOMPILE) $(LTM_CFLAGS) $(LTM_LDFLAGS) -o $@ -c $< LOBJECTS = $(OBJECTS:.o=.lo) $(LIBNAME): $(OBJECTS) - $(LTLINK) $(LDFLAGS) $(LOBJECTS) -o $(LIBNAME) -rpath $(LIBPATH) -version-info $(VERSION_SO) $(LIBTOOLFLAGS) + $(LTLINK) $(LTM_LDFLAGS) $(LOBJECTS) -o $(LIBNAME) -rpath $(LIBPATH) -version-info $(VERSION_SO) $(LTM_LIBTOOLFLAGS) install: $(LIBNAME) install -d $(DESTDIR)$(LIBPATH) @@ -80,27 +78,21 @@ uninstall: rm $(HEADERS_PUB:%=$(DESTDIR)$(INCPATH)/%) rm $(DESTDIR)$(LIBPATH)/pkgconfig/libtommath.pc -test: $(LIBNAME) - $(LTCOMPILE) $(CFLAGS) -c demo/main.c -o demo/main.o - $(LTCOMPILE) $(CFLAGS) -c demo/opponent.c -o demo/opponent.o - $(LTCOMPILE) $(CFLAGS) -c demo/test.c -o demo/test.o - $(LTLINK) $(LDFLAGS) -o test demo/main.o demo/opponent.o demo/test.o $(LIBNAME) +test_standalone: test + @echo "test_standalone is deprecated, please use make-target 'test'" -test_standalone: $(LIBNAME) - $(LTCOMPILE) $(CFLAGS) -c demo/main.c -o demo/main.o - $(LTCOMPILE) $(CFLAGS) -c demo/opponent.c -o demo/opponent.o - $(LTCOMPILE) $(CFLAGS) -c demo/test.c -o demo/test.o - $(LTLINK) $(LDFLAGS) -o test demo/main.o demo/opponent.o demo/test.o $(LIBNAME) +test mtest_opponent: demo/shared.o $(LIBNAME) | demo/test.o demo/mtest_opponent.o + $(LTLINK) $(LTM_LDFLAGS) demo/$@.o $^ -o $@ .PHONY: mtest mtest: - cd mtest ; $(CC) $(CFLAGS) -O0 mtest.c $(LDFLAGS) -o mtest + cd mtest ; $(CC) $(LTM_CFLAGS) -O0 mtest.c $(LTM_LDFLAGS) -o mtest timing: $(LIBNAME) demo/timing.c - $(LTLINK) $(CFLAGS) $(LDFLAGS) -DTIMER demo/timing.c $(LIBNAME) -o timing + $(LTLINK) $(LTM_CFLAGS) $(LTM_LDFLAGS) -DTIMER demo/timing.c $(LIBNAME) -o timing tune: $(LIBNAME) - $(LTCOMPILE) $(CFLAGS) -c etc/tune.c -o etc/tune.o - $(LTLINK) $(LDFLAGS) -o etc/tune etc/tune.o $(LIBNAME) + $(LTCOMPILE) $(LTM_CFLAGS) -c etc/tune.c -o etc/tune.o + $(LTLINK) $(LTM_LDFLAGS) -o etc/tune etc/tune.o $(LIBNAME) cd etc/; /bin/sh tune_it.sh; cd .. $(MAKE) -f makefile.shared diff --git a/libtommath/makefile.unix b/libtommath/makefile.unix index 5a9767d..be008b0 100644 --- a/libtommath/makefile.unix +++ b/libtommath/makefile.unix @@ -35,34 +35,32 @@ bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cm bn_mp_cnt_lsb.o bn_mp_complement.o bn_mp_copy.o bn_mp_count_bits.o bn_mp_decr.o bn_mp_div.o bn_mp_div_2.o \ bn_mp_div_2d.o bn_mp_div_3.o bn_mp_div_d.o bn_mp_dr_is_modulus.o bn_mp_dr_reduce.o bn_mp_dr_setup.o \ bn_mp_error_to_string.o bn_mp_exch.o bn_mp_export.o bn_mp_expt_u32.o bn_mp_exptmod.o bn_mp_exteuclid.o \ -bn_mp_fread.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o bn_mp_get_i32.o bn_mp_get_i64.o bn_mp_get_l.o \ -bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o bn_mp_get_mag_ul.o bn_mp_get_mag_ull.o \ -bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o bn_mp_init_copy.o bn_mp_init_i32.o \ -bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o bn_mp_init_set.o bn_mp_init_size.o \ -bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.o bn_mp_invmod.o bn_mp_is_square.o \ -bn_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o bn_mp_mod.o bn_mp_mod_2d.o \ -bn_mp_mod_d.o bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o bn_mp_montgomery_setup.o \ -bn_mp_mul.o bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_neg.o bn_mp_or.o \ -bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \ +bn_mp_fread.o bn_mp_from_sbin.o bn_mp_from_ubin.o bn_mp_fwrite.o bn_mp_gcd.o bn_mp_get_double.o \ +bn_mp_get_i32.o bn_mp_get_i64.o bn_mp_get_l.o bn_mp_get_ll.o bn_mp_get_mag_u32.o bn_mp_get_mag_u64.o \ +bn_mp_get_mag_ul.o bn_mp_get_mag_ull.o bn_mp_grow.o bn_mp_ilogb.o bn_mp_import.o bn_mp_incr.o bn_mp_init.o \ +bn_mp_init_copy.o bn_mp_init_i32.o bn_mp_init_i64.o bn_mp_init_l.o bn_mp_init_ll.o bn_mp_init_multi.o \ +bn_mp_init_set.o bn_mp_init_size.o bn_mp_init_u32.o bn_mp_init_u64.o bn_mp_init_ul.o bn_mp_init_ull.o \ +bn_mp_invmod.o bn_mp_is_square.o bn_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_lshd.o \ +bn_mp_mod.o bn_mp_mod_2d.o bn_mp_mod_d.o bn_mp_montgomery_calc_normalization.o bn_mp_montgomery_reduce.o \ +bn_mp_montgomery_setup.o bn_mp_mul.o bn_mp_mul_2.o bn_mp_mul_2d.o bn_mp_mul_d.o bn_mp_mulmod.o bn_mp_neg.o \ +bn_mp_or.o bn_mp_prime_fermat.o bn_mp_prime_frobenius_underwood.o bn_mp_prime_is_prime.o \ bn_mp_prime_miller_rabin.o bn_mp_prime_next_prime.o bn_mp_prime_rabin_miller_trials.o \ bn_mp_prime_rand.o bn_mp_prime_strong_lucas_selfridge.o bn_mp_radix_size.o bn_mp_radix_smap.o \ -bn_mp_rand.o bn_mp_read_radix.o bn_mp_read_signed_bin.o bn_mp_read_unsigned_bin.o bn_mp_reduce.o \ -bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o \ -bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o bn_mp_reduce_setup.o bn_mp_root_u32.o bn_mp_rshd.o \ -bn_mp_set.o bn_mp_set_double.o bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o \ -bn_mp_set_u32.o bn_mp_set_u64.o bn_mp_set_ul.o bn_mp_set_ull.o bn_mp_shrink.o bn_mp_signed_bin_size.o \ -bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o bn_mp_sqrtmod_prime.o bn_mp_sub.o bn_mp_sub_d.o \ -bn_mp_submod.o bn_mp_to_signed_bin.o bn_mp_to_signed_bin_n.o bn_mp_to_unsigned_bin.o \ -bn_mp_to_unsigned_bin_n.o bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o \ -bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o \ -bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o \ -bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o \ -bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o bn_s_mp_prime_is_divisible.o \ -bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o \ -bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o +bn_mp_rand.o bn_mp_read_radix.o bn_mp_reduce.o bn_mp_reduce_2k.o bn_mp_reduce_2k_l.o \ +bn_mp_reduce_2k_setup.o bn_mp_reduce_2k_setup_l.o bn_mp_reduce_is_2k.o bn_mp_reduce_is_2k_l.o \ +bn_mp_reduce_setup.o bn_mp_root_u32.o bn_mp_rshd.o bn_mp_sbin_size.o bn_mp_set.o bn_mp_set_double.o \ +bn_mp_set_i32.o bn_mp_set_i64.o bn_mp_set_l.o bn_mp_set_ll.o bn_mp_set_u32.o bn_mp_set_u64.o \ +bn_mp_set_ul.o bn_mp_set_ull.o bn_mp_shrink.o bn_mp_signed_rsh.o bn_mp_sqr.o bn_mp_sqrmod.o bn_mp_sqrt.o \ +bn_mp_sqrtmod_prime.o bn_mp_sub.o bn_mp_sub_d.o bn_mp_submod.o bn_mp_to_radix.o bn_mp_to_sbin.o \ +bn_mp_to_ubin.o bn_mp_ubin_size.o bn_mp_xor.o bn_mp_zero.o bn_prime_tab.o bn_s_mp_add.o \ +bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_get_bit.o bn_s_mp_invmod_fast.o \ +bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \ +bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \ +bn_s_mp_prime_is_divisible.o bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o \ +bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o HEADERS_PUB=tommath.h -HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB) +HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB) #The default rule for make builds the libtommath.a library (static) default: $(LIBMAIN_S) @@ -81,13 +79,14 @@ $(LIBMAIN_S): $(OBJECTS) $(RANLIB) $@ #Build test_standalone suite -test: demo/main.c demo/opponent.c demo/test.c $(LIBMAIN_S) - $(CC) $(LTM_CFLAGS) $(LTM_LDFLAGS) $^ -DLTM_DEMO_TEST_VS_MTEST=0 -o $@ +test: demo/shared.o demo/test.o $(LIBMAIN_S) + $(CC) $(LTM_CFLAGS) $(LTM_LDFLAGS) $^ -o $@ @echo "NOTICE: start the tests by: ./test" test_standalone: test + @echo "test_standalone is deprecated, please use make-target 'test'" -all: $(LIBMAIN_S) test_standalone +all: $(LIBMAIN_S) test tune: $(LIBMAIN_S) $(MAKE) -C etc tune diff --git a/libtommath/makefile_include.mk b/libtommath/makefile_include.mk index 9303ffc..df0adf8 100644 --- a/libtommath/makefile_include.mk +++ b/libtommath/makefile_include.mk @@ -47,60 +47,67 @@ else endif endif -CFLAGS += -I./ -Wall -Wsign-compare -Wextra -Wshadow +LTM_CFLAGS += -I./ -Wall -Wsign-compare -Wextra -Wshadow ifdef SANITIZER -CFLAGS += -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero +LTM_CFLAGS += -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero endif ifndef NO_ADDTL_WARNINGS # additional warnings -CFLAGS += -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align -CFLAGS += -Wstrict-prototypes -Wpointer-arith +LTM_CFLAGS += -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align +LTM_CFLAGS += -Wstrict-prototypes -Wpointer-arith endif ifdef CONV_WARNINGS -CFLAGS += -std=c89 -Wconversion -Wsign-conversion +LTM_CFLAGS += -std=c89 -Wconversion -Wsign-conversion ifeq ($(CONV_WARNINGS), strict) -CFLAGS += -DMP_USE_ENUMS -Wc++-compat +LTM_CFLAGS += -DMP_USE_ENUMS -Wc++-compat endif else -CFLAGS += -Wsystem-headers +LTM_CFLAGS += -Wsystem-headers endif ifdef COMPILE_DEBUG #debug -CFLAGS += -g3 +LTM_CFLAGS += -g3 endif ifdef COMPILE_SIZE #for size -CFLAGS += -Os +LTM_CFLAGS += -Os else ifndef IGNORE_SPEED #for speed -CFLAGS += -O3 -funroll-loops +LTM_CFLAGS += -O3 -funroll-loops #x86 optimizations [should be valid for any GCC install though] -CFLAGS += -fomit-frame-pointer +LTM_CFLAGS += -fomit-frame-pointer endif endif # COMPILE_SIZE ifneq ($(findstring clang,$(CC)),) -CFLAGS += -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header +LTM_CFLAGS += -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header endif ifneq ($(findstring mingw,$(CC)),) -CFLAGS += -Wno-shadow +LTM_CFLAGS += -Wno-shadow endif ifeq ($(PLATFORM), Darwin) -CFLAGS += -Wno-nullability-completeness +LTM_CFLAGS += -Wno-nullability-completeness endif ifeq ($(PLATFORM), CYGWIN) LIBTOOLFLAGS += -no-undefined endif +# add in the standard FLAGS +LTM_CFLAGS += $(CFLAGS) +LTM_LFLAGS += $(LFLAGS) +LTM_LDFLAGS += $(LDFLAGS) +LTM_LIBTOOLFLAGS += $(LIBTOOLFLAGS) + + ifeq ($(PLATFORM),FreeBSD) _ARCH := $(shell sysctl -b hw.machine_arch) else @@ -117,9 +124,7 @@ else endif HEADERS_PUB=tommath.h -HEADERS=tommath_private.h tommath_class.h tommath_superclass.h $(HEADERS_PUB) - -test_standalone: CFLAGS+=-DLTM_DEMO_TEST_VS_MTEST=0 +HEADERS=tommath_private.h tommath_class.h tommath_superclass.h tommath_cutoffs.h $(HEADERS_PUB) #LIBPATH The directory for libtommath to be installed to. #INCPATH The directory to install the header files for libtommath. @@ -132,9 +137,9 @@ DATAPATH ?= $(PREFIX)/share/doc/libtommath/pdf #make the code coverage of the library # -coverage: CFLAGS += -fprofile-arcs -ftest-coverage -DTIMING_NO_LOGS -coverage: LFLAGS += -lgcov -coverage: LDFLAGS += -lgcov +coverage: LTM_CFLAGS += -fprofile-arcs -ftest-coverage -DTIMING_NO_LOGS +coverage: LTM_LFLAGS += -lgcov +coverage: LTM_LDFLAGS += -lgcov coverage: $(COVERAGE) $(COVERAGE_APP) diff --git a/libtommath/tommath.def b/libtommath/tommath.def index 8418843..6ad2b89 100644 --- a/libtommath/tommath.def +++ b/libtommath/tommath.def @@ -38,6 +38,8 @@ EXPORTS mp_exptmod mp_exteuclid mp_fread + mp_from_sbin + mp_from_ubin mp_fwrite mp_gcd mp_get_double @@ -101,8 +103,6 @@ EXPORTS mp_radix_size mp_rand mp_read_radix - mp_read_signed_bin - mp_read_unsigned_bin mp_reduce mp_reduce_2k mp_reduce_2k_l @@ -113,6 +113,7 @@ EXPORTS mp_reduce_setup mp_root_u32 mp_rshd + mp_sbin_size mp_set mp_set_double mp_set_i32 @@ -127,7 +128,6 @@ EXPORTS mp_set_ul mp_set_ull mp_shrink - mp_signed_bin_size mp_signed_rsh mp_sqr mp_sqrmod @@ -136,12 +136,9 @@ EXPORTS mp_sub mp_sub_d mp_submod - mp_to_signed_bin - mp_to_signed_bin_n - mp_to_unsigned_bin - mp_to_unsigned_bin_n - mp_toradix - mp_toradix_n - mp_unsigned_bin_size + mp_to_radix + mp_to_sbin + mp_to_ubin + mp_ubin_size mp_xor mp_zero diff --git a/libtommath/tommath.h b/libtommath/tommath.h index 1c00ade..8c67c0e 100644 --- a/libtommath/tommath.h +++ b/libtommath/tommath.h @@ -17,6 +17,14 @@ # include #endif +#ifdef MP_8BIT +# ifdef _MSC_VER +# pragma message("8-bit (MP_8BIT) support is deprecated and will be dropped completely in the next version.") +# else +# warning "8-bit (MP_8BIT) support is deprecated and will be dropped completely in the next version." +# endif +#endif + #ifdef __cplusplus extern "C" { #endif @@ -206,7 +214,7 @@ TOOM_SQR_CUTOFF; # endif #endif -#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 301) +#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 405) # define MP_DEPRECATED(x) __attribute__((deprecated("replaced by " #x))) # define PRIVATE_MP_DEPRECATED_PRAGMA(s) _Pragma(#s) # define MP_DEPRECATED_PRAGMA(s) PRIVATE_MP_DEPRECATED_PRAGMA(GCC warning s) @@ -517,7 +525,7 @@ mp_err mp_lcm(const mp_int *a, const mp_int *b, mp_int *c) MP_WUR; */ mp_err mp_root_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR; MP_DEPRECATED(mp_root_u32) mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c) MP_WUR; -MP_DEPRECATED(mp_n_root_ex) mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR; +MP_DEPRECATED(mp_root_u32) mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR; /* special sqrt algo */ mp_err mp_sqrt(const mp_int *arg, mp_int *ret) MP_WUR; @@ -683,24 +691,34 @@ mp_err mp_ilogb(const mp_int *a, uint32_t base, mp_int *c) MP_WUR; /* c = a**b */ mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR; MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c) MP_WUR; -MP_DEPRECATED(mp_expt_d) mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR; +MP_DEPRECATED(mp_expt_u32) mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) MP_WUR; /* ---> radix conversion <--- */ int mp_count_bits(const mp_int *a) MP_WUR; -int mp_unsigned_bin_size(const mp_int *a) MP_WUR; -mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) MP_WUR; -mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b) MP_WUR; -mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR; -int mp_signed_bin_size(const mp_int *a) MP_WUR; -mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) MP_WUR; -mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b) MP_WUR; -mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR; +MP_DEPRECATED(mp_ubin_size) int mp_unsigned_bin_size(const mp_int *a) MP_WUR; +MP_DEPRECATED(mp_from_ubin) mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) MP_WUR; +MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b) MP_WUR; +MP_DEPRECATED(mp_to_ubin) mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR; + +MP_DEPRECATED(mp_sbin_size) int mp_signed_bin_size(const mp_int *a) MP_WUR; +MP_DEPRECATED(mp_from_sbin) mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) MP_WUR; +MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b) MP_WUR; +MP_DEPRECATED(mp_to_sbin) mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) MP_WUR; + +size_t mp_ubin_size(const mp_int *a) MP_WUR; +mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR; +mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR; + +size_t mp_sbin_size(const mp_int *a) MP_WUR; +mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) MP_WUR; +mp_err mp_to_sbin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written) MP_WUR; mp_err mp_read_radix(mp_int *a, const char *str, int radix) MP_WUR; -mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR; -mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) MP_WUR; +MP_DEPRECATED(mp_to_radix) mp_err mp_toradix(const mp_int *a, char *str, int radix) MP_WUR; +MP_DEPRECATED(mp_to_radix) mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) MP_WUR; +mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, int radix) MP_WUR; mp_err mp_radix_size(const mp_int *a, int radix, int *size) MP_WUR; #ifndef MP_NO_FILE @@ -715,10 +733,15 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) MP_WUR; #define mp_mag_size(mp) (MP_DEPRECATED_PRAGMA("replaced by mp_unsigned_bin_size") mp_unsigned_bin_size(mp)) #define mp_tomag(mp, str) (MP_DEPRECATED_PRAGMA("replaced by mp_to_unsigned_bin") mp_to_unsigned_bin((mp), (str))) -#define mp_tobinary(M, S) mp_toradix((M), (S), 2) -#define mp_tooctal(M, S) mp_toradix((M), (S), 8) -#define mp_todecimal(M, S) mp_toradix((M), (S), 10) -#define mp_tohex(M, S) mp_toradix((M), (S), 16) +#define mp_tobinary(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_binary") mp_toradix((M), (S), 2)) +#define mp_tooctal(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_octal") mp_toradix((M), (S), 8)) +#define mp_todecimal(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_decimal") mp_toradix((M), (S), 10)) +#define mp_tohex(M, S) (MP_DEPRECATED_PRAGMA("replaced by mp_to_hex") mp_toradix((M), (S), 16)) + +#define mp_to_binary(M, S, N) mp_to_radix((M), (S), (N), 2) +#define mp_to_octal(M, S, N) mp_to_radix((M), (S), (N), 8) +#define mp_to_decimal(M, S, N) mp_to_radix((M), (S), (N), 10) +#define mp_to_hex(M, S, N) mp_to_radix((M), (S), (N), 16) #ifdef __cplusplus } diff --git a/libtommath/tommath_class.h b/libtommath/tommath_class.h index ae4405f..a60a757 100644 --- a/libtommath/tommath_class.h +++ b/libtommath/tommath_class.h @@ -45,6 +45,8 @@ # define BN_MP_EXPTMOD_C # define BN_MP_EXTEUCLID_C # define BN_MP_FREAD_C +# define BN_MP_FROM_SBIN_C +# define BN_MP_FROM_UBIN_C # define BN_MP_FWRITE_C # define BN_MP_GCD_C # define BN_MP_GET_DOUBLE_C @@ -105,8 +107,6 @@ # define BN_MP_RADIX_SMAP_C # define BN_MP_RAND_C # define BN_MP_READ_RADIX_C -# define BN_MP_READ_SIGNED_BIN_C -# define BN_MP_READ_UNSIGNED_BIN_C # define BN_MP_REDUCE_C # define BN_MP_REDUCE_2K_C # define BN_MP_REDUCE_2K_L_C @@ -117,6 +117,7 @@ # define BN_MP_REDUCE_SETUP_C # define BN_MP_ROOT_U32_C # define BN_MP_RSHD_C +# define BN_MP_SBIN_SIZE_C # define BN_MP_SET_C # define BN_MP_SET_DOUBLE_C # define BN_MP_SET_I32_C @@ -128,7 +129,6 @@ # define BN_MP_SET_UL_C # define BN_MP_SET_ULL_C # define BN_MP_SHRINK_C -# define BN_MP_SIGNED_BIN_SIZE_C # define BN_MP_SIGNED_RSH_C # define BN_MP_SQR_C # define BN_MP_SQRMOD_C @@ -137,13 +137,10 @@ # define BN_MP_SUB_C # define BN_MP_SUB_D_C # define BN_MP_SUBMOD_C -# define BN_MP_TO_SIGNED_BIN_C -# define BN_MP_TO_SIGNED_BIN_N_C -# define BN_MP_TO_UNSIGNED_BIN_C -# define BN_MP_TO_UNSIGNED_BIN_N_C -# define BN_MP_TORADIX_C -# define BN_MP_TORADIX_N_C -# define BN_MP_UNSIGNED_BIN_SIZE_C +# define BN_MP_TO_RADIX_C +# define BN_MP_TO_SBIN_C +# define BN_MP_TO_UBIN_C +# define BN_MP_UBIN_SIZE_C # define BN_MP_XOR_C # define BN_MP_ZERO_C # define BN_PRIME_TAB_C @@ -188,6 +185,8 @@ # define BN_MP_EXPT_D_C # define BN_MP_EXPT_D_EX_C # define BN_MP_EXPT_U32_C +# define BN_MP_FROM_SBIN_C +# define BN_MP_FROM_UBIN_C # define BN_MP_GET_BIT_C # define BN_MP_GET_INT_C # define BN_MP_GET_LONG_C @@ -208,12 +207,16 @@ # define BN_MP_PRIME_IS_DIVISIBLE_C # define BN_MP_PRIME_RANDOM_EX_C # define BN_MP_RAND_DIGIT_C +# define BN_MP_READ_SIGNED_BIN_C +# define BN_MP_READ_UNSIGNED_BIN_C # define BN_MP_ROOT_U32_C +# define BN_MP_SBIN_SIZE_C # define BN_MP_SET_INT_C # define BN_MP_SET_LONG_C # define BN_MP_SET_LONG_LONG_C # define BN_MP_SET_U32_C # define BN_MP_SET_U64_C +# define BN_MP_SIGNED_BIN_SIZE_C # define BN_MP_SIGNED_RSH_C # define BN_MP_TC_AND_C # define BN_MP_TC_DIV_2D_C @@ -221,6 +224,15 @@ # define BN_MP_TC_XOR_C # define BN_MP_TOOM_MUL_C # define BN_MP_TOOM_SQR_C +# define BN_MP_TORADIX_C +# define BN_MP_TORADIX_N_C +# define BN_MP_TO_RADIX_C +# define BN_MP_TO_SBIN_C +# define BN_MP_TO_UBIN_C +# define BN_MP_TO_UNSIGNED_BIN_C +# define BN_MP_TO_UNSIGNED_BIN_N_C +# define BN_MP_UBIN_SIZE_C +# define BN_MP_UNSIGNED_BIN_SIZE_C # define BN_MP_XOR_C # define BN_S_MP_BALANCE_MUL_C # define BN_S_MP_EXPTMOD_FAST_C @@ -403,10 +415,9 @@ #if defined(BN_MP_EXPTMOD_C) # define BN_MP_ABS_C -# define BN_MP_CLEAR_C # define BN_MP_CLEAR_MULTI_C # define BN_MP_DR_IS_MODULUS_C -# define BN_MP_INIT_C +# define BN_MP_INIT_MULTI_C # define BN_MP_INVMOD_C # define BN_MP_REDUCE_IS_2K_C # define BN_MP_REDUCE_IS_2K_L_C @@ -432,9 +443,20 @@ # define BN_MP_ZERO_C #endif +#if defined(BN_MP_FROM_SBIN_C) +# define BN_MP_FROM_UBIN_C +#endif + +#if defined(BN_MP_FROM_UBIN_C) +# define BN_MP_CLAMP_C +# define BN_MP_GROW_C +# define BN_MP_MUL_2D_C +# define BN_MP_ZERO_C +#endif + #if defined(BN_MP_FWRITE_C) # define BN_MP_RADIX_SIZE_C -# define BN_MP_TORADIX_C +# define BN_MP_TO_RADIX_C #endif #if defined(BN_MP_GCD_C) @@ -775,9 +797,9 @@ #if defined(BN_MP_PRIME_RAND_C) # define BN_MP_ADD_D_C # define BN_MP_DIV_2_C +# define BN_MP_FROM_UBIN_C # define BN_MP_MUL_2_C # define BN_MP_PRIME_IS_PRIME_C -# define BN_MP_READ_UNSIGNED_BIN_C # define BN_MP_SUB_D_C # define BN_S_MP_PRIME_RANDOM_EX_C # define BN_S_MP_RAND_CB_C @@ -826,6 +848,7 @@ # define BN_MP_GROW_C # define BN_MP_RAND_SOURCE_C # define BN_MP_ZERO_C +# define BN_S_MP_RAND_PLATFORM_C # define BN_S_MP_RAND_SOURCE_C #endif @@ -835,17 +858,6 @@ # define BN_MP_ZERO_C #endif -#if defined(BN_MP_READ_SIGNED_BIN_C) -# define BN_MP_READ_UNSIGNED_BIN_C -#endif - -#if defined(BN_MP_READ_UNSIGNED_BIN_C) -# define BN_MP_CLAMP_C -# define BN_MP_GROW_C -# define BN_MP_MUL_2D_C -# define BN_MP_ZERO_C -#endif - #if defined(BN_MP_REDUCE_C) # define BN_MP_ADD_C # define BN_MP_CLEAR_C @@ -860,6 +872,7 @@ # define BN_MP_SUB_C # define BN_S_MP_MUL_DIGS_C # define BN_S_MP_MUL_HIGH_DIGS_C +# define BN_S_MP_MUL_HIGH_DIGS_FAST_C # define BN_S_MP_SUB_C #endif @@ -935,6 +948,10 @@ # define BN_MP_ZERO_C #endif +#if defined(BN_MP_SBIN_SIZE_C) +# define BN_MP_UBIN_SIZE_C +#endif + #if defined(BN_MP_SET_C) #endif @@ -975,10 +992,6 @@ #if defined(BN_MP_SHRINK_C) #endif -#if defined(BN_MP_SIGNED_BIN_SIZE_C) -# define BN_MP_UNSIGNED_BIN_SIZE_C -#endif - #if defined(BN_MP_SIGNED_RSH_C) # define BN_MP_ADD_D_C # define BN_MP_DIV_2D_C @@ -1049,42 +1062,25 @@ # define BN_MP_SUB_C #endif -#if defined(BN_MP_TO_SIGNED_BIN_C) -# define BN_MP_TO_UNSIGNED_BIN_C -#endif - -#if defined(BN_MP_TO_SIGNED_BIN_N_C) -# define BN_MP_SIGNED_BIN_SIZE_C -# define BN_MP_TO_SIGNED_BIN_C -#endif - -#if defined(BN_MP_TO_UNSIGNED_BIN_C) +#if defined(BN_MP_TO_RADIX_C) # define BN_MP_CLEAR_C -# define BN_MP_DIV_2D_C +# define BN_MP_DIV_D_C # define BN_MP_INIT_COPY_C # define BN_S_MP_REVERSE_C #endif -#if defined(BN_MP_TO_UNSIGNED_BIN_N_C) -# define BN_MP_TO_UNSIGNED_BIN_C -# define BN_MP_UNSIGNED_BIN_SIZE_C +#if defined(BN_MP_TO_SBIN_C) +# define BN_MP_TO_UBIN_C #endif -#if defined(BN_MP_TORADIX_C) +#if defined(BN_MP_TO_UBIN_C) # define BN_MP_CLEAR_C -# define BN_MP_DIV_D_C -# define BN_MP_INIT_COPY_C -# define BN_S_MP_REVERSE_C -#endif - -#if defined(BN_MP_TORADIX_N_C) -# define BN_MP_CLEAR_C -# define BN_MP_DIV_D_C +# define BN_MP_DIV_2D_C # define BN_MP_INIT_COPY_C # define BN_S_MP_REVERSE_C #endif -#if defined(BN_MP_UNSIGNED_BIN_SIZE_C) +#if defined(BN_MP_UBIN_SIZE_C) # define BN_MP_COUNT_BITS_C #endif @@ -1106,6 +1102,7 @@ #if defined(BN_S_MP_BALANCE_MUL_C) # define BN_MP_ADD_C +# define BN_MP_CLAMP_C # define BN_MP_CLEAR_C # define BN_MP_CLEAR_MULTI_C # define BN_MP_EXCH_C @@ -1123,7 +1120,9 @@ # define BN_MP_INIT_C # define BN_MP_MOD_C # define BN_MP_MUL_C +# define BN_MP_REDUCE_2K_L_C # define BN_MP_REDUCE_2K_SETUP_L_C +# define BN_MP_REDUCE_C # define BN_MP_REDUCE_SETUP_C # define BN_MP_SET_C # define BN_MP_SQR_C @@ -1133,17 +1132,21 @@ # define BN_MP_CLEAR_C # define BN_MP_COPY_C # define BN_MP_COUNT_BITS_C +# define BN_MP_DR_REDUCE_C # define BN_MP_DR_SETUP_C # define BN_MP_EXCH_C # define BN_MP_INIT_SIZE_C # define BN_MP_MOD_C # define BN_MP_MONTGOMERY_CALC_NORMALIZATION_C +# define BN_MP_MONTGOMERY_REDUCE_C # define BN_MP_MONTGOMERY_SETUP_C # define BN_MP_MULMOD_C # define BN_MP_MUL_C +# define BN_MP_REDUCE_2K_C # define BN_MP_REDUCE_2K_SETUP_C # define BN_MP_SET_C # define BN_MP_SQR_C +# define BN_S_MP_MONTGOMERY_REDUCE_FAST_C #endif #if defined(BN_S_MP_GET_BIT_C) diff --git a/libtommath/tommath_private.h b/libtommath/tommath_private.h index 3271d7e..ff6f7cf 100644 --- a/libtommath/tommath_private.h +++ b/libtommath/tommath_private.h @@ -17,7 +17,7 @@ * * On Win32 a .def file must be used to specify the exported symbols. */ -#if defined (MP_PRIVATE_SYMBOLS) && __GNUC__ >= 4 +#if defined (MP_PRIVATE_SYMBOLS) && defined(__GNUC__) && __GNUC__ >= 4 # define MP_PRIVATE __attribute__ ((visibility ("hidden"))) #else # define MP_PRIVATE @@ -140,6 +140,11 @@ extern void *MP_CALLOC(size_t nmemb, size_t size); extern void MP_FREE(void *mem, size_t size); #endif +/* feature detection macro */ +#define MP_STRINGIZE(x) MP__STRINGIZE(x) +#define MP__STRINGIZE(x) ""#x"" +#define MP_HAS(x) (sizeof(MP_STRINGIZE(BN_##x##_C)) == 1u) + /* TODO: Remove private_mp_word as soon as deprecated mp_word is removed from tommath. */ #undef mp_word typedef private_mp_word mp_word; @@ -198,7 +203,7 @@ MP_PRIVATE mp_err s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_i MP_PRIVATE mp_err s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) MP_WUR; MP_PRIVATE mp_err s_mp_rand_platform(void *p, size_t n) MP_WUR; MP_PRIVATE mp_err s_mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat); -MP_PRIVATE void s_mp_reverse(unsigned char *s, int len); +MP_PRIVATE void s_mp_reverse(unsigned char *s, size_t len); MP_PRIVATE mp_err s_mp_prime_is_divisible(const mp_int *a, mp_bool *result); /* TODO: jenkins prng is not thread safe as of now */ diff --git a/libtommath/tommath_superclass.h b/libtommath/tommath_superclass.h index cf17866..d88bce9 100644 --- a/libtommath/tommath_superclass.h +++ b/libtommath/tommath_superclass.h @@ -4,10 +4,13 @@ /* super class file for PK algos */ /* default ... include all MPI */ +#ifndef LTM_NOTHING #define LTM_ALL +#endif /* RSA only (does not support DH/DSA/ECC) */ /* #define SC_RSA_1 */ +/* #define SC_RSA_1_WITH_TESTS */ /* For reference.... On an Athlon64 optimizing for speed... @@ -15,61 +18,93 @@ */ +#ifdef SC_RSA_1_WITH_TESTS +# define BN_MP_ERROR_TO_STRING_C +# define BN_MP_FREAD_C +# define BN_MP_FWRITE_C +# define BN_MP_INCR_C +# define BN_MP_ISEVEN_C +# define BN_MP_ISODD_C +# define BN_MP_NEG_C +# define BN_MP_PRIME_FROBENIUS_UNDERWOOD_C +# define BN_MP_RADIX_SIZE_C +# define BN_MP_RAND_C +# define BN_MP_REDUCE_C +# define BN_MP_REDUCE_2K_L_C +# define BN_MP_FROM_SBIN_C +# define BN_MP_ROOT_U32_C +# define BN_MP_SET_L_C +# define BN_MP_SET_UL_C +# define BN_MP_SBIN_SIZE_C +# define BN_MP_TO_RADIX_C +# define BN_MP_TO_SBIN_C +# define BN_S_MP_RAND_JENKINS_C +# define BN_S_MP_RAND_PLATFORM_C +#endif + /* Works for RSA only, mpi.o is 68KiB */ -#ifdef SC_RSA_1 -# define BN_MP_SHRINK_C -# define BN_MP_LCM_C -# define BN_MP_PRIME_RANDOM_EX_C -# define BN_MP_INVMOD_C -# define BN_MP_GCD_C -# define BN_MP_MOD_C -# define BN_MP_MULMOD_C +#if defined(SC_RSA_1) || defined (SC_RSA_1_WITH_TESTS) +# define BN_CUTOFFS_C # define BN_MP_ADDMOD_C +# define BN_MP_CLEAR_MULTI_C # define BN_MP_EXPTMOD_C -# define BN_MP_SET_INT_C +# define BN_MP_GCD_C # define BN_MP_INIT_MULTI_C -# define BN_MP_CLEAR_MULTI_C -# define BN_MP_UNSIGNED_BIN_SIZE_C -# define BN_MP_TO_UNSIGNED_BIN_C +# define BN_MP_INVMOD_C +# define BN_MP_LCM_C +# define BN_MP_MOD_C # define BN_MP_MOD_D_C +# define BN_MP_MULMOD_C +# define BN_MP_PRIME_IS_PRIME_C # define BN_MP_PRIME_RABIN_MILLER_TRIALS_C -# define BN_S_MP_REVERSE_C +# define BN_MP_PRIME_RAND_C +# define BN_MP_RADIX_SMAP_C +# define BN_MP_SET_INT_C +# define BN_MP_SHRINK_C +# define BN_MP_TO_UNSIGNED_BIN_C +# define BN_MP_UNSIGNED_BIN_SIZE_C # define BN_PRIME_TAB_C +# define BN_S_MP_REVERSE_C /* other modifiers */ # define BN_MP_DIV_SMALL /* Slower division, not critical */ + /* here we are on the last pass so we turn things off. The functions classes are still there * but we remove them specifically from the build. This also invokes tweaks in functions * like removing support for even moduli, etc... */ # ifdef LTM_LAST -# undef BN_S_MP_TOOM_MUL_C -# undef BN_S_MP_TOOM_SQR_C -# undef BN_S_MP_KARATSUBA_MUL_C -# undef BN_S_MP_KARATSUBA_SQR_C -# undef BN_MP_REDUCE_C -# undef BN_MP_REDUCE_SETUP_C # undef BN_MP_DR_IS_MODULUS_C # undef BN_MP_DR_SETUP_C # undef BN_MP_DR_REDUCE_C -# undef BN_MP_REDUCE_IS_2K_C +# undef BN_MP_DIV_3_C # undef BN_MP_REDUCE_2K_SETUP_C # undef BN_MP_REDUCE_2K_C +# undef BN_MP_REDUCE_IS_2K_C +# undef BN_MP_REDUCE_SETUP_C +# undef BN_S_MP_BALANCE_MUL_C # undef BN_S_MP_EXPTMOD_C -# undef BN_MP_DIV_3_C +# undef BN_S_MP_INVMOD_FAST_C +# undef BN_S_MP_KARATSUBA_MUL_C +# undef BN_S_MP_KARATSUBA_SQR_C # undef BN_S_MP_MUL_HIGH_DIGS_C # undef BN_S_MP_MUL_HIGH_DIGS_FAST_C -# undef BN_S_MP_INVMOD_FAST_C +# undef BN_S_MP_TOOM_MUL_C +# undef BN_S_MP_TOOM_SQR_C + +# ifndef SC_RSA_1_WITH_TESTS +# undef BN_MP_REDUCE_C +# endif /* To safely undefine these you have to make sure your RSA key won't exceed the Comba threshold * which is roughly 255 digits [7140 bits for 32-bit machines, 15300 bits for 64-bit machines] * which means roughly speaking you can handle upto 2536-bit RSA keys with these defined without * trouble. */ +# undef BN_MP_MONTGOMERY_REDUCE_C # undef BN_S_MP_MUL_DIGS_C # undef BN_S_MP_SQR_C -# undef BN_MP_MONTGOMERY_REDUCE_C # endif #endif -- cgit v0.12