diff options
author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2019-10-17 11:09:10 (GMT) |
---|---|---|
committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2019-10-17 11:09:10 (GMT) |
commit | 6893ec40c9e47ef1cf2eb15fc4e656e44ff94fba (patch) | |
tree | 4947a9fe266f7401ce5b4b638f28647a14e1f6d3 | |
parent | e5c5cd920bb372946311de4f524c340154324119 (diff) | |
parent | 6dd152f0407fefebbdd94e2aa2e3c9fd7e23ad6e (diff) | |
download | tcl-6893ec40c9e47ef1cf2eb15fc4e656e44ff94fba.zip tcl-6893ec40c9e47ef1cf2eb15fc4e656e44ff94fba.tar.gz tcl-6893ec40c9e47ef1cf2eb15fc4e656e44ff94fba.tar.bz2 |
Merge libtommath
79 files changed, 1488 insertions, 1789 deletions
diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..e9a67c8 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,39 @@ +# Set the default behavior, in case people don't have core.autocrlf set. +* eol=lf +* text=auto + +# Explicitly declare text files you want to always be normalized and converted +# to native line endings on checkout. +*.3 text +*.c text +*.css text +*.enc text +*.h text +*.htm text +*.html text +*.java text +*.js text +*.json text +*.n text +*.svg text +*.ts text +*.tcl text +*.test text + +# Declare files that will always have CRLF line endings on checkout. +*.bat eol=crlf +*.sln eol=crlf +*.vc eol=crlf + +# Denote all files that are truly binary and should not be modified. +*.a binary +*.dll binary +*.exe binary +*.gif binary +*.gz binary +*.jpg binary +*.lib binary +*.pdf binary +*.png binary +*.xlsx binary +*.zip binary diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62a7a5a --- /dev/null +++ b/.gitignore @@ -0,0 +1,54 @@ +*.a +*.dll +*.dylib +*.exe +*.exp +*.lib +*.o +*.obj +*.pdb +*.res +*.sl +*.so +*/Makefile +*/config.cache +*/config.log +*/config.status +*/tclConfig.sh +*/tclsh* +*/tcltest* +*/versions.vc +*/version.vc +*/libtcl.vfs +*/libtcl_*.zip +html +libtommath/bn.ilg +libtommath/bn.ind +libtommath/pretty.build +libtommath/tommath.src +libtommath/*.log +libtommath/*.pdf +libtommath/*.pl +libtommath/*.sh +libtommath/doc/* +libtommath/tombc/* +libtommath/pre_gen/* +libtommath/pics/* +libtommath/mtest/* +libtommath/logs/* +libtommath/etc/* +libtommath/demo/* +libtommath/*.out +libtommath/*.tex +unix/autoMkindex.tcl +unix/dltest.marker +unix/tcl.pc +unix/tclIndex +unix/pkgs/* +win/Debug* +win/Release* +win/*.manifest +win/pkgs/* +win/coffbase.txt +win/tcl.hpj +win/nmhlp-out.txt 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/appveyor.yml b/libtommath/appveyor.yml index c4a13a0..332cbdc 100644 --- a/libtommath/appveyor.yml +++ b/libtommath/appveyor.yml @@ -1,18 +1,18 @@ -version: 1.1.0-{build}
-branches:
- only:
- - master
- - develop
- - /^release/
- - /^travis/
-image:
-- Visual Studio 2017
-- Visual Studio 2015
-build_script:
-- cmd: >-
- if "Visual Studio 2017"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
- if "Visual Studio 2015"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64
- if "Visual Studio 2015"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86_amd64
- nmake -f makefile.msvc all
-test_script:
-- cmd: test.exe
+version: 1.2.0-{build} +branches: + only: + - master + - develop + - /^release/ + - /^travis/ +image: +- Visual Studio 2017 +- Visual Studio 2015 +build_script: +- cmd: >- + if "Visual Studio 2017"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat" + if "Visual Studio 2015"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64 + if "Visual Studio 2015"=="%APPVEYOR_BUILD_WORKER_IMAGE%" call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86_amd64 + nmake -f makefile.msvc all +test_script: +- cmd: test.exe diff --git a/libtommath/bn_deprecated.c b/libtommath/bn_deprecated.c index 4beafe6..2056b20 100644 --- a/libtommath/bn_deprecated.c +++ b/libtommath/bn_deprecated.c @@ -113,7 +113,9 @@ mp_err mp_toom_sqr(const mp_int *a, mp_int *b) #ifdef S_MP_REVERSE_C void bn_reverse(unsigned char *s, int len) { - s_mp_reverse(s, len); + if (len > 0) { + s_mp_reverse(s, (size_t)len); + } } #endif #ifdef BN_MP_TC_AND_C @@ -229,4 +231,91 @@ 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) +{ + size_t n = mp_ubin_size(a); + if (*outlen < (unsigned long)n) { + return MP_VAL; + } + *outlen = (unsigned long)n; + return mp_to_ubin(a, b, n, 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_signed_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_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen) +{ + size_t n = mp_sbin_size(a); + if (*outlen < (unsigned long)n) { + return MP_VAL; + } + *outlen = (unsigned long)n; + return mp_to_sbin(a, b, n, 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, NULL, 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, NULL, radix); +} +#endif +#ifdef BN_MP_IMPORT_C +mp_err mp_import(mp_int *rop, size_t count, int order, size_t size, int endian, size_t nails, + const void *op) +{ + return mp_unpack(rop, count, order, size, endian, nails, op); +} +#endif +#ifdef BN_MP_EXPORT_C +mp_err mp_export(void *rop, size_t *countp, int order, size_t size, + int endian, size_t nails, const mp_int *op) +{ + return mp_pack(rop, SIZE_MAX, countp, order, size, endian, nails, op); +} +#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)<<ix))) { ix++; @@ -43,12 +43,10 @@ mp_err mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) return MP_OKAY; } -#ifdef BN_MP_DIV_3_C /* three? */ - if (b == 3u) { + if (MP_HAS(MP_DIV_3) && (b == 3u)) { return mp_div_3(a, c, d); } -#endif /* no easy answer [c'est la vie]. Just division */ if ((err = mp_init_size(&q, a->used)) != MP_OKAY) { diff --git a/libtommath/bn_mp_expt_u32.c b/libtommath/bn_mp_expt_u32.c index 4ec725e..67c8fd2 100644 --- a/libtommath/bn_mp_expt_u32.c +++ b/libtommath/bn_mp_expt_u32.c @@ -4,7 +4,7 @@ /* SPDX-License-Identifier: Unlicense */ /* calculate c = a**b using a square-multiply algorithm */ -mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) +mp_err mp_expt_u32(const mp_int *a, unsigned int b, mp_int *c) { mp_err err; @@ -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_read_signed_bin.c b/libtommath/bn_mp_from_sbin.c index 2e16399..20e4597 100644 --- a/libtommath/bn_mp_read_signed_bin.c +++ b/libtommath/bn_mp_from_sbin.c @@ -1,20 +1,20 @@ #include "tommath_private.h" -#ifdef BN_MP_READ_SIGNED_BIN_C +#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_read_signed_bin(mp_int *a, const unsigned char *b, int c) +mp_err mp_from_sbin(mp_int *a, const unsigned char *buf, size_t size) { mp_err err; /* read magnitude */ - if ((err = mp_read_unsigned_bin(a, b + 1, c - 1)) != MP_OKAY) { + 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 (b[0] == (unsigned char)0) { + if (buf[0] == (unsigned char)0) { a->sign = MP_ZPOS; } else { a->sign = MP_NEG; diff --git a/libtommath/bn_mp_read_unsigned_bin.c b/libtommath/bn_mp_from_ubin.c index 17b273e..7f73cbc 100644 --- a/libtommath/bn_mp_read_unsigned_bin.c +++ b/libtommath/bn_mp_from_ubin.c @@ -1,10 +1,10 @@ #include "tommath_private.h" -#ifdef BN_MP_READ_UNSIGNED_BIN_C +#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_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) +mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size) { mp_err err; @@ -19,17 +19,17 @@ mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) mp_zero(a); /* read the bytes in */ - while (c-- > 0) { + while (size-- > 0u) { if ((err = mp_mul_2d(a, 8, a)) != MP_OKAY) { return err; } #ifndef MP_8BIT - a->dp[0] |= *b++; + a->dp[0] |= *buf++; a->used += 1; #else - a->dp[0] = (*b & MP_MASK); - a->dp[1] |= ((*b++ >> 7) & 1u); + a->dp[0] = (*buf & MP_MASK); + a->dp[1] |= ((*buf++ >> 7) & 1u); a->used += 2; #endif } diff --git a/libtommath/bn_mp_fwrite.c b/libtommath/bn_mp_fwrite.c index 2a59755..abe2e67 100644 --- a/libtommath/bn_mp_fwrite.c +++ b/libtommath/bn_mp_fwrite.c @@ -9,9 +9,15 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream) char *buf; mp_err err; int len; + size_t written; - if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { - return err; + /* TODO: this function is not in this PR */ + if (MP_HAS(MP_RADIX_SIZE_OVERESTIMATE)) { + /* if ((err = mp_radix_size_overestimate(&t, base, &len)) != MP_OKAY) goto LBL_ERR; */ + } else { + if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { + return err; + } } buf = (char *) MP_MALLOC((size_t)len); @@ -19,18 +25,20 @@ 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, &written, 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; + if (fwrite(buf, written, 1uL, stream) != 1uL) { + 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_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_init.c b/libtommath/bn_mp_init.c index 2eb7924..a4c9175 100644 --- a/libtommath/bn_mp_init.c +++ b/libtommath/bn_mp_init.c @@ -7,7 +7,8 @@ mp_err mp_init(mp_int *a) { /* allocate memory required and clear it */ - a->dp = (mp_digit *) MP_CALLOC((size_t)MP_PREC, sizeof(mp_digit)); + a->dp = (mp_digit *) MP_MALLOC((size_t)MP_PREC * sizeof(mp_digit)); + MP_ZERO_DIGITS(a->dp, MP_PREC); if (a->dp == NULL) { return MP_MEM; } diff --git a/libtommath/bn_mp_init_size.c b/libtommath/bn_mp_init_size.c index d622687..215de5a 100644 --- a/libtommath/bn_mp_init_size.c +++ b/libtommath/bn_mp_init_size.c @@ -9,7 +9,8 @@ mp_err mp_init_size(mp_int *a, int size) size = MP_MAX(MP_MIN_PREC, size); /* alloc mem */ - a->dp = (mp_digit *) MP_CALLOC((size_t)size, sizeof(mp_digit)); + a->dp = (mp_digit *) MP_MALLOC((size_t)size * sizeof(mp_digit)); + MP_ZERO_DIGITS(a->dp, size); if (a->dp == NULL) { return MP_MEM; } 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_ilogb.c b/libtommath/bn_mp_log_u32.c index 3d23d38..ddb78cf 100644 --- a/libtommath/bn_mp_ilogb.c +++ b/libtommath/bn_mp_log_u32.c @@ -1,12 +1,12 @@ #include "tommath_private.h" -#ifdef BN_MP_ILOGB_C +#ifdef BN_MP_LOG_U32_C /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ /* Compute log_{base}(a) */ static mp_word s_pow(mp_word base, mp_word exponent) { - mp_word result = 1uLL; + mp_word result = 1; while (exponent != 0u) { if ((exponent & 1u) == 1u) { result *= base; @@ -20,7 +20,7 @@ static mp_word s_pow(mp_word base, mp_word exponent) static mp_digit s_digit_ilogb(mp_digit base, mp_digit n) { - mp_word bracket_low = 1uLL, bracket_mid, bracket_high, N; + mp_word bracket_low = 1, bracket_mid, bracket_high, N; mp_digit ret, high = 1uL, low = 0uL, mid; if (n < base) { @@ -70,17 +70,19 @@ static mp_digit s_digit_ilogb(mp_digit base, mp_digit n) as is the output of mp_bitcount. With the same problem: max size is INT_MAX * MP_DIGIT not INT_MAX only! */ -mp_err mp_ilogb(const mp_int *a, uint32_t base, mp_int *c) +mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) { mp_err err; mp_ord cmp; - unsigned int high, low, mid; + uint32_t high, low, mid; mp_int bracket_low, bracket_high, bracket_mid, t, bi_base; err = MP_OKAY; + if (a->sign == MP_NEG) { return MP_VAL; } + if (MP_IS_ZERO(a)) { return MP_VAL; } @@ -88,23 +90,26 @@ mp_err mp_ilogb(const mp_int *a, uint32_t base, mp_int *c) if (base < 2u) { return MP_VAL; } - if (base == 2u) { - mp_set_u32(c, (uint32_t)(mp_count_bits(a) - 1)); - return err; + + /* A small shortcut for bases that are powers of two. */ + if ((base & (base - 1u)) == 0u) { + int y, bit_count; + for (y=0; (y < 7) && ((base & 1u) == 0u); y++) { + base >>= 1; + } + bit_count = mp_count_bits(a) - 1; + *c = (uint32_t)(bit_count/y); + return MP_OKAY; } + if (a->used == 1) { - mp_set(c, s_digit_ilogb(base, a->dp[0])); + *c = (uint32_t)s_digit_ilogb(base, a->dp[0]); return err; } cmp = mp_cmp_d(a, base); - - if (cmp == MP_LT) { - mp_zero(c); - return err; - } - if (cmp == MP_EQ) { - mp_set(c, 1uL); + if ((cmp == MP_LT) || (cmp == MP_EQ)) { + *c = cmp == MP_EQ; return err; } @@ -140,11 +145,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; } @@ -161,16 +162,12 @@ mp_err mp_ilogb(const mp_int *a, uint32_t base, mp_int *c) mp_exch(&bracket_mid, &bracket_low); } if (cmp == MP_EQ) { - mp_set_u32(c, mid); + *c = mid; goto LBL_END; } } - if (mp_cmp(&bracket_high, a) == MP_EQ) { - mp_set_u32(c, high); - } else { - mp_set_u32(c, low); - } + *c = (mp_cmp(&bracket_high, a) == MP_EQ) ? high : low; LBL_END: LBL_ERR: 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_export.c b/libtommath/bn_mp_pack.c index c9de48b..6e00b6f 100644 --- a/libtommath/bn_mp_export.c +++ b/libtommath/bn_mp_pack.c @@ -1,49 +1,46 @@ #include "tommath_private.h" -#ifdef BN_MP_EXPORT_C +#ifdef BN_MP_PACK_C /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ /* based on gmp's mpz_export. * see http://gmplib.org/manual/Integer-Import-and-Export.html */ -mp_err mp_export(void *rop, size_t *countp, int order, size_t size, - int endian, size_t nails, const mp_int *op) +mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size_t size, + mp_endian endian, size_t nails, const mp_int *op) { mp_err err; - size_t odd_nails, nail_bytes, i, j, bits, count; + size_t odd_nails, nail_bytes, i, j, count; unsigned char odd_nail_mask; mp_int t; + count = mp_pack_count(op, nails, size); + + if (count > maxcount) { + return MP_BUF; + } + if ((err = mp_init_copy(&t, op)) != MP_OKAY) { return err; } - if (endian == 0) { - union { - unsigned int i; - char c[4]; - } lint; - lint.i = 0x01020304; - - endian = (lint.c[0] == '\x04') ? -1 : 1; + if (endian == MP_NATIVE_ENDIAN) { + MP_GET_ENDIANNESS(endian); } odd_nails = (nails % 8u); odd_nail_mask = 0xff; - for (i = 0; i < odd_nails; ++i) { + for (i = 0u; i < odd_nails; ++i) { odd_nail_mask ^= (unsigned char)(1u << (7u - i)); } nail_bytes = nails / 8u; - bits = (size_t)mp_count_bits(&t); - count = (bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u); - - for (i = 0; i < count; ++i) { - for (j = 0; j < size; ++j) { + for (i = 0u; i < count; ++i) { + for (j = 0u; j < size; ++j) { unsigned char *byte = (unsigned char *)rop + - (((order == -1) ? i : ((count - 1u) - i)) * size) + - ((endian == -1) ? j : ((size - 1u) - j)); + (((order == MP_LSB_FIRST) ? i : ((count - 1u) - i)) * size) + + ((endian == MP_LITTLE_ENDIAN) ? j : ((size - 1u) - j)); if (j >= (size - nail_bytes)) { *byte = 0; @@ -53,19 +50,20 @@ 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; + if (written != NULL) { + *written = count; } + err = MP_OKAY; - return MP_OKAY; +LBL_ERR: + mp_clear(&t); + return err; } #endif diff --git a/libtommath/bn_mp_pack_count.c b/libtommath/bn_mp_pack_count.c new file mode 100644 index 0000000..dfecdf9 --- /dev/null +++ b/libtommath/bn_mp_pack_count.c @@ -0,0 +1,12 @@ +#include "tommath_private.h" +#ifdef BN_MP_PACK_COUNT_C +/* LibTomMath, multiple-precision integer library -- Tom St Denis */ +/* SPDX-License-Identifier: Unlicense */ + +size_t mp_pack_count(const mp_int *a, size_t nails, size_t size) +{ + size_t bits = (size_t)mp_count_bits(a); + return ((bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u)); +} + +#endif diff --git a/libtommath/bn_mp_prime_frobenius_underwood.c b/libtommath/bn_mp_prime_frobenius_underwood.c index e2f9f1f..253e8d5 100644 --- a/libtommath/bn_mp_prime_frobenius_underwood.c +++ b/libtommath/bn_mp_prime_frobenius_underwood.c @@ -7,7 +7,7 @@ /* * See file bn_mp_prime_is_prime.c or the documentation in doc/bn.tex for the details */ -#ifndef LTM_USE_FIPS_ONLY +#ifndef LTM_USE_ONLY_MR #ifdef MP_8BIT /* @@ -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_is_prime.c b/libtommath/bn_mp_prime_is_prime.c index 0c5131e..7f9fc0b 100644 --- a/libtommath/bn_mp_prime_is_prime.c +++ b/libtommath/bn_mp_prime_is_prime.c @@ -102,10 +102,10 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result) /* * Both, the Frobenius-Underwood test and the the Lucas-Selfridge test are quite - * slow so if speed is an issue, define LTM_USE_FIPS_ONLY to use M-R tests with + * slow so if speed is an issue, define LTM_USE_ONLY_MR to use M-R tests with * bases 2, 3 and t random bases. */ -#ifndef LTM_USE_FIPS_ONLY +#ifndef LTM_USE_ONLY_MR if (t >= 0) { /* * Use a Frobenius-Underwood test instead of the Lucas-Selfridge test for 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_rabin_miller_trials.c b/libtommath/bn_mp_prime_rabin_miller_trials.c index 0b3bab3..8bbaf6c 100644 --- a/libtommath/bn_mp_prime_rabin_miller_trials.c +++ b/libtommath/bn_mp_prime_rabin_miller_trials.c @@ -6,23 +6,29 @@ static const struct { int k, t; } sizes[] = { - { 80, -1 }, /* Use deterministic algorithm for size <= 80 bits */ - { 81, 39 }, - { 96, 37 }, - { 128, 32 }, - { 160, 27 }, - { 192, 21 }, - { 256, 16 }, - { 384, 10 }, - { 512, 7 }, - { 640, 6 }, - { 768, 5 }, - { 896, 4 }, - { 1024, 4 }, - { 2048, 2 } /* For bigger keysizes use always at least 2 Rounds */ + { 80, -1 }, /* Use deterministic algorithm for size <= 80 bits */ + { 81, 37 }, /* max. error = 2^(-96)*/ + { 96, 32 }, /* max. error = 2^(-96)*/ + { 128, 40 }, /* max. error = 2^(-112)*/ + { 160, 35 }, /* max. error = 2^(-112)*/ + { 256, 27 }, /* max. error = 2^(-128)*/ + { 384, 16 }, /* max. error = 2^(-128)*/ + { 512, 18 }, /* max. error = 2^(-160)*/ + { 768, 11 }, /* max. error = 2^(-160)*/ + { 896, 10 }, /* max. error = 2^(-160)*/ + { 1024, 12 }, /* max. error = 2^(-192)*/ + { 1536, 8 }, /* max. error = 2^(-192)*/ + { 2048, 6 }, /* max. error = 2^(-192)*/ + { 3072, 4 }, /* max. error = 2^(-192)*/ + { 4096, 5 }, /* max. error = 2^(-256)*/ + { 5120, 4 }, /* max. error = 2^(-256)*/ + { 6144, 4 }, /* max. error = 2^(-256)*/ + { 8192, 3 }, /* max. error = 2^(-256)*/ + { 9216, 3 }, /* max. error = 2^(-256)*/ + { 10240, 2 } /* For bigger keysizes use always at least 2 Rounds */ }; -/* returns # of RM trials required for a given bit size and max. error of 2^(-96)*/ +/* returns # of RM trials required for a given bit size */ int mp_prime_rabin_miller_trials(int size) { int x; 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..b50bbcd 100644 --- a/libtommath/bn_mp_prime_strong_lucas_selfridge.c +++ b/libtommath/bn_mp_prime_strong_lucas_selfridge.c @@ -7,7 +7,7 @@ /* * See file bn_mp_prime_is_prime.c or the documentation in doc/bn.tex for the details */ -#ifndef LTM_USE_FIPS_ONLY +#ifndef LTM_USE_ONLY_MR /* * 8-bit is just too small. You can try the Frobenius test @@ -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..b96f487 100644 --- a/libtommath/bn_mp_radix_size.c +++ b/libtommath/bn_mp_radix_size.c @@ -3,11 +3,11 @@ /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ -/* returns size of ASCII reprensentation */ +/* returns size of ASCII representation */ mp_err mp_radix_size(const mp_int *a, int radix, int *size) { mp_err err; - int digs; + int digs; mp_int t; mp_digit d; @@ -25,7 +25,7 @@ mp_err mp_radix_size(const mp_int *a, int radix, int *size) /* special case for binary */ if (radix == 2) { - *size = mp_count_bits(a) + ((a->sign == MP_NEG) ? 1 : 0) + 1; + *size = (mp_count_bits(a) + ((a->sign == MP_NEG) ? 1 : 0) + 1); return MP_OKAY; } @@ -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_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_signed_bin_size.c b/libtommath/bn_mp_sbin_size.c index 1a7f49c..e0993d6 100644 --- a/libtommath/bn_mp_signed_bin_size.c +++ b/libtommath/bn_mp_sbin_size.c @@ -1,11 +1,11 @@ #include "tommath_private.h" -#ifdef BN_MP_SIGNED_BIN_SIZE_C +#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 */ -int mp_signed_bin_size(const mp_int *a) +size_t mp_sbin_size(const mp_int *a) { - return 1 + mp_unsigned_bin_size(a); + return 1u + mp_ubin_size(a); } #endif diff --git a/libtommath/bn_mp_set_double.c b/libtommath/bn_mp_set_double.c index a42fc70..fea5691 100644 --- a/libtommath/bn_mp_set_double.c +++ b/libtommath/bn_mp_set_double.c @@ -6,17 +6,17 @@ #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) mp_err mp_set_double(mp_int *a, double b) { - uint64_t frac; + unsigned long long frac; int exp; mp_err err; union { double dbl; - uint64_t bits; + unsigned long long bits; } cast; cast.dbl = b; exp = (int)((unsigned)(cast.bits >> 52) & 0x7FFu); - frac = (cast.bits & ((1uLL << 52) - 1uLL)) | (1uLL << 52); + frac = (cast.bits & ((1uLL << 52) - 1)) | (1uLL << 52); if (exp == 0x7FF) { /* +-inf, NaN */ return MP_VAL; 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_toradix_n.c b/libtommath/bn_mp_to_radix.c index bb8af88..7fa86ca 100644 --- a/libtommath/bn_mp_toradix_n.c +++ b/libtommath/bn_mp_to_radix.c @@ -1,22 +1,26 @@ #include "tommath_private.h" -#ifdef BN_MP_TORADIX_N_C +#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 + * Stores upto "size - 1" chars and always a NULL byte, puts the number of characters + * written, including the '\0', in "written". */ -mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) +mp_err mp_to_radix(const mp_int *a, char *str, size_t maxlen, size_t *written, int radix) { - int digs; + size_t 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)) { + /* check range of radix and size*/ + if (maxlen < 2u) { + return MP_BUF; + } + if ((radix < 2) || (radix > 64)) { return MP_VAL; } @@ -24,6 +28,9 @@ mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) if (MP_IS_ZERO(a)) { *str++ = '0'; *str = '\0'; + if (written != NULL) { + *written = 2u; + } return MP_OKAY; } @@ -43,21 +50,19 @@ mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) /* subtract a char */ --maxlen; } - - digs = 0; + digs = 0u; while (!MP_IS_ZERO(&t)) { - if (--maxlen < 1) { + if (--maxlen < 1u) { /* no more room */ - break; + err = MP_BUF; + goto LBL_ERR; } if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) { - mp_clear(&t); - return err; + 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 */ @@ -65,9 +70,15 @@ mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen) /* append a NULL so the string is properly terminated */ *str = '\0'; + digs++; + + if (written != NULL) { + *written = (a->sign == MP_NEG) ? (digs + 1u): digs; + } +LBL_ERR: mp_clear(&t); - return MP_OKAY; + 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..dbaf53e --- /dev/null +++ b/libtommath/bn_mp_to_sbin.c @@ -0,0 +1,22 @@ +#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_BUF; + } + 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..1681ca7 --- /dev/null +++ b/libtommath/bn_mp_to_ubin.c @@ -0,0 +1,41 @@ +#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, count; + mp_err err; + mp_int t; + + count = mp_ubin_size(a); + if (count > maxlen) { + return MP_BUF; + } + + if ((err = mp_init_copy(&t, a)) != MP_OKAY) { + return err; + } + + for (x = count; x --> 0u;) { +#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; + } + } + + if (written != NULL) { + *written = count; + } + +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_unsigned_bin_size.c b/libtommath/bn_mp_ubin_size.c index 297de92..21230b4 100644 --- a/libtommath/bn_mp_unsigned_bin_size.c +++ b/libtommath/bn_mp_ubin_size.c @@ -1,12 +1,12 @@ #include "tommath_private.h" -#ifdef BN_MP_UNSIGNED_BIN_SIZE_C +#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 */ -int mp_unsigned_bin_size(const mp_int *a) +size_t mp_ubin_size(const mp_int *a) { - int size = mp_count_bits(a); - return (size / 8) + ((((unsigned)size & 7u) != 0u) ? 1 : 0); + size_t size = (size_t)mp_count_bits(a); + return (size / 8u) + (((size & 7u) != 0u) ? 1u : 0u); } #endif diff --git a/libtommath/bn_mp_import.c b/libtommath/bn_mp_unpack.c index bd83b96..d4eb90e 100644 --- a/libtommath/bn_mp_import.c +++ b/libtommath/bn_mp_unpack.c @@ -1,13 +1,13 @@ #include "tommath_private.h" -#ifdef BN_MP_IMPORT_C +#ifdef BN_MP_UNPACK_C /* LibTomMath, multiple-precision integer library -- Tom St Denis */ /* SPDX-License-Identifier: Unlicense */ /* based on gmp's mpz_import. * see http://gmplib.org/manual/Integer-Import-and-Export.html */ -mp_err mp_import(mp_int *rop, size_t count, int order, size_t size, - int endian, size_t nails, const void *op) +mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size, + mp_endian endian, size_t nails, const void *op) { mp_err err; size_t odd_nails, nail_bytes, i, j; @@ -15,14 +15,8 @@ mp_err mp_import(mp_int *rop, size_t count, int order, size_t size, mp_zero(rop); - if (endian == 0) { - union { - unsigned int i; - char c[4]; - } lint; - lint.i = 0x01020304; - - endian = (lint.c[0] == '\x04') ? -1 : 1; + if (endian == MP_NATIVE_ENDIAN) { + MP_GET_ENDIANNESS(endian); } odd_nails = (nails % 8u); @@ -35,8 +29,8 @@ mp_err mp_import(mp_int *rop, size_t count, int order, size_t size, for (i = 0; i < count; ++i) { for (j = 0; j < (size - nail_bytes); ++j) { unsigned char byte = *((const unsigned char *)op + - (((order == 1) ? i : ((count - 1u) - i)) * size) + - ((endian == 1) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes))); + (((order == MP_MSB_FIRST) ? i : ((count - 1u) - i)) * size) + + ((endian == MP_BIG_ENDIAN) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes))); if ((err = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) { return err; 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_montgomery_reduce_fast.c b/libtommath/bn_s_mp_montgomery_reduce_fast.c index 843ad12..3f0c672 100644 --- a/libtommath/bn_s_mp_montgomery_reduce_fast.c +++ b/libtommath/bn_s_mp_montgomery_reduce_fast.c @@ -119,7 +119,7 @@ mp_err s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho) /* alias for next word, where the carry goes */ _W = W + ++ix; - for (; ix <= ((n->used * 2) + 1); ix++) { + for (; ix < ((n->used * 2) + 1); ix++) { *_W++ += *_W1++ >> (mp_word)MP_DIGIT_BIT; } 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 <sys/random.h> #include <errno.h> @@ -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 <errno.h> #include <unistd.h> -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/changes.txt b/libtommath/changes.txt index aa0c64c..c49429d 100644 --- a/libtommath/changes.txt +++ b/libtommath/changes.txt @@ -1,3 +1,35 @@ +XXX XXth, 2019 +v1.2.0 + -- A huge refactoring of the library happened - renaming, + deprecating and replacing existing functions by improved API's. + + All deprecated functions, macros and symbols are only marked as such + so this version is still API and ABI compatible to v1.x. + + -- Daniel Mendler was pushing for those changes and contributing a load of patches, + refactorings, code reviews and whatnotelse. + -- Christoph Zurnieden re-worked internals of the library, improved the performance, + did code reviews and wrote documentation. + -- Francois Perrad did some refactoring and took again care of linting the sources and + provided all fixes. + -- Jan Nijtmans, Karel Miko and Joachim Breitner contributed various patches. + + -- Private symbols can now be hidden for the shared library builds, disabled by default. + -- All API's follow a single code style, are prefixed the same etc. + -- Unified, safer and improved API's + -- Less magic numbers - return values (where appropriate) and most flags are now enums, + this was implemented in a backwards compatible way where return values were int. + -- API's with return values are now by default marked as "warn on unsused result", this + can be disabled if required (which will most likely hide bugs), c.f. MP_WUR in tommath.h + -- Provide a whole set of setters&getters for different primitive types (long, uint32_t, etc.) + -- All those primitive setters are now optimized. + -- It's possible to automatically tune the cutoff values for Karatsuba&Toom-Cook + -- Added mp_decr() and mp_incr() + -- Added mp_log_u32() + -- Improved prime-checking + -- Improved Toom-Cook multiplication + + Jan 28th, 2019 v1.1.0 -- Christoph Zurnieden contributed FIPS 186.4 compliant 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..67cc89b 100644 --- a/libtommath/libtommath_VS2008.vcproj +++ b/libtommath/libtommath_VS2008.vcproj @@ -429,10 +429,6 @@ >
</File>
<File
- RelativePath="bn_mp_export.c"
- >
- </File>
- <File
RelativePath="bn_mp_expt_u32.c"
>
</File>
@@ -449,6 +445,14 @@ >
</File>
<File
+ RelativePath="bn_mp_from_sbin.c"
+ >
+ </File>
+ <File
+ RelativePath="bn_mp_from_ubin.c"
+ >
+ </File>
+ <File
RelativePath="bn_mp_fwrite.c"
>
</File>
@@ -497,14 +501,6 @@ >
</File>
<File
- RelativePath="bn_mp_ilogb.c"
- >
- </File>
- <File
- RelativePath="bn_mp_import.c"
- >
- </File>
- <File
RelativePath="bn_mp_incr.c"
>
</File>
@@ -585,6 +581,10 @@ >
</File>
<File
+ RelativePath="bn_mp_log_u32.c"
+ >
+ </File>
+ <File
RelativePath="bn_mp_lshd.c"
>
</File>
@@ -641,6 +641,14 @@ >
</File>
<File
+ RelativePath="bn_mp_pack.c"
+ >
+ </File>
+ <File
+ RelativePath="bn_mp_pack_count.c"
+ >
+ </File>
+ <File
RelativePath="bn_mp_prime_fermat.c"
>
</File>
@@ -689,14 +697,6 @@ >
</File>
<File
- RelativePath="bn_mp_read_signed_bin.c"
- >
- </File>
- <File
- RelativePath="bn_mp_read_unsigned_bin.c"
- >
- </File>
- <File
RelativePath="bn_mp_reduce.c"
>
</File>
@@ -737,6 +737,10 @@ >
</File>
<File
+ RelativePath="bn_mp_sbin_size.c"
+ >
+ </File>
+ <File
RelativePath="bn_mp_set.c"
>
</File>
@@ -781,10 +785,6 @@ >
</File>
<File
- RelativePath="bn_mp_signed_bin_size.c"
- >
- </File>
- <File
RelativePath="bn_mp_signed_rsh.c"
>
</File>
@@ -817,31 +817,23 @@ >
</File>
<File
- RelativePath="bn_mp_to_signed_bin.c"
- >
- </File>
- <File
- RelativePath="bn_mp_to_signed_bin_n.c"
- >
- </File>
- <File
- RelativePath="bn_mp_to_unsigned_bin.c"
+ RelativePath="bn_mp_to_radix.c"
>
</File>
<File
- RelativePath="bn_mp_to_unsigned_bin_n.c"
+ RelativePath="bn_mp_to_sbin.c"
>
</File>
<File
- RelativePath="bn_mp_toradix.c"
+ RelativePath="bn_mp_to_ubin.c"
>
</File>
<File
- RelativePath="bn_mp_toradix_n.c"
+ RelativePath="bn_mp_ubin_size.c"
>
</File>
<File
- RelativePath="bn_mp_unsigned_bin_size.c"
+ RelativePath="bn_mp_unpack.c"
>
</File>
<File
diff --git a/libtommath/makefile b/libtommath/makefile index ea4df4c..df296de 100644 --- a/libtommath/makefile +++ b/libtommath/makefile @@ -17,11 +17,11 @@ coverage: LIBNAME:=-Wl,--whole-archive $(LIBNAME) -Wl,--no-whole-archive include makefile_include.mk -%.o: %.c +%.o: %.c $(HEADERS) ifneq ($V,1) @echo " * ${CC} $@" endif - ${silent} ${CC} -c ${CFLAGS} $< -o $@ + ${silent} ${CC} -c ${LTM_CFLAGS} $< -o $@ LCOV_ARGS=--directory . @@ -30,27 +30,26 @@ OBJECTS=bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o bn_mp_cmp_mag.o \ 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_error_to_string.o bn_mp_exch.o bn_mp_expt_u32.o bn_mp_exptmod.o bn_mp_exteuclid.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_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_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_log_u32.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_pack.o bn_mp_pack_count.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_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_unpack.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 \ @@ -59,8 +58,6 @@ bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o #END_INS -$(OBJECTS): $(HEADERS) - $(LIBNAME): $(OBJECTS) $(AR) $(ARFLAGS) $@ $(OBJECTS) $(RANLIB) $@ @@ -80,11 +77,11 @@ profiled: #make a single object profiled library profiled_single: perl gen.pl - $(CC) $(CFLAGS) -fprofile-arcs -DTESTING -c mpi.c -o mpi.o - $(CC) $(CFLAGS) -DTESTING -DTIMER demo/timing.c mpi.o -lgcov -o timing + $(CC) $(LTM_CFLAGS) -fprofile-arcs -DTESTING -c mpi.c -o mpi.o + $(CC) $(LTM_CFLAGS) -DTESTING -DTIMER demo/timing.c mpi.o -lgcov -o timing ./timing rm -f *.o timing - $(CC) $(CFLAGS) -fbranch-probabilities -DTESTING -c mpi.c -o mpi.o + $(CC) $(LTM_CFLAGS) -fbranch-probabilities -DTESTING -c mpi.c -o mpi.o $(AR) $(ARFLAGS) $(LIBNAME) mpi.o ranlib $(LIBNAME) @@ -98,21 +95,27 @@ uninstall: rm $(DESTDIR)$(LIBPATH)/$(LIBNAME) rm $(HEADERS_PUB:%=$(DESTDIR)$(INCPATH)/%) -test: demo/main.o demo/opponent.o demo/test.o $(LIBNAME) - $(CC) $(CFLAGS) $^ $(LFLAGS) -o test +test_standalone: test + @echo "test_standalone is deprecated, please use make-target 'test'" + +DEMOS=test mtest_opponent + +define DEMO_template +$(1): demo/$(1).o demo/shared.o $$(LIBNAME) + $$(CC) $$(LTM_CFLAGS) $$(LTM_LFLAGS) $$^ -o $$@ +endef -test_standalone: demo/main.o demo/opponent.o demo/test.o $(LIBNAME) - $(CC) $(CFLAGS) $^ $(LFLAGS) -o test +$(foreach demo, $(strip $(DEMOS)), $(eval $(call DEMO_template,$(demo)))) .PHONY: mtest mtest: - cd mtest ; $(CC) $(CFLAGS) -O0 mtest.c $(LFLAGS) -o mtest + cd mtest ; $(CC) $(LTM_CFLAGS) -O0 mtest.c $(LTM_LFLAGS) -o mtest timing: $(LIBNAME) demo/timing.c - $(CC) $(CFLAGS) -DTIMER demo/timing.c $(LIBNAME) $(LFLAGS) -o timing + $(CC) $(LTM_CFLAGS) -DTIMER demo/timing.c $(LIBNAME) $(LTM_LFLAGS) -o timing tune: $(LIBNAME) - $(MAKE) -C etc tune + $(MAKE) -C etc tune CFLAGS="$(LTM_CFLAGS)" $(MAKE) # You have to create a file .coveralls.yml with the content "repo_token: <the token>" @@ -130,7 +133,7 @@ pre_gen: sed -e 's/[[:blank:]]*$$//' mpi.c > pre_gen/mpi.c rm mpi.c -zipup: clean astyle new_file manual poster docs +zipup: clean astyle new_file manual poster @# Update the index, so diff-index won't fail in case the pdf has been created. @# As the pdf creation modifies the tex files, git sometimes detects the @# modified files, but misses that it's put back to its original version. @@ -142,12 +145,11 @@ zipup: clean astyle new_file manual poster docs @echo 'fixme check' -@(find libtommath-$(VERSION)/ -type f | xargs grep 'FIXM[E]') && echo '############## BEWARE: the "fixme" marker was found !!! ##############' || true mkdir -p libtommath-$(VERSION)/doc - cp doc/bn.pdf doc/tommath.pdf doc/poster.pdf libtommath-$(VERSION)/doc/ + cp doc/bn.pdf doc/poster.pdf libtommath-$(VERSION)/doc/ $(MAKE) -C libtommath-$(VERSION)/ pre_gen tar -c libtommath-$(VERSION)/ | xz -6e -c - > ltm-$(VERSION).tar.xz zip -9rq ltm-$(VERSION).zip libtommath-$(VERSION) cp doc/bn.pdf bn-$(VERSION).pdf - cp doc/tommath.pdf tommath-$(VERSION).pdf rm -rf libtommath-$(VERSION) gpg -b -a ltm-$(VERSION).tar.xz gpg -b -a ltm-$(VERSION).zip diff --git a/libtommath/makefile.mingw b/libtommath/makefile.mingw index 793415d..7eee57d 100644 --- a/libtommath/makefile.mingw +++ b/libtommath/makefile.mingw @@ -33,27 +33,26 @@ OBJECTS=bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o bn_mp_cmp_mag.o \ 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_error_to_string.o bn_mp_exch.o bn_mp_expt_u32.o bn_mp_exptmod.o bn_mp_exteuclid.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_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_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_log_u32.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_pack.o bn_mp_pack_count.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_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_unpack.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 \ @@ -61,7 +60,7 @@ bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o b 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 +81,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..d282e93 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) @@ -25,27 +25,26 @@ OBJECTS=bn_cutoffs.obj bn_deprecated.obj bn_mp_2expt.obj bn_mp_abs.obj bn_mp_add bn_mp_and.obj bn_mp_clamp.obj bn_mp_clear.obj bn_mp_clear_multi.obj bn_mp_cmp.obj bn_mp_cmp_d.obj bn_mp_cmp_mag.obj \ 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_error_to_string.obj bn_mp_exch.obj bn_mp_expt_u32.obj bn_mp_exptmod.obj bn_mp_exteuclid.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_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_mp_iseven.obj bn_mp_isodd.obj bn_mp_kronecker.obj bn_mp_lcm.obj bn_mp_log_u32.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_pack.obj bn_mp_pack_count.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_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_unpack.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 \ @@ -53,7 +52,7 @@ bn_s_mp_rand_jenkins.obj bn_s_mp_rand_platform.obj bn_s_mp_reverse.obj bn_s_mp_s 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 +67,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..6802107 100644 --- a/libtommath/makefile.shared +++ b/libtommath/makefile.shared @@ -27,27 +27,26 @@ OBJECTS=bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o bn_mp_cmp_mag.o \ 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_error_to_string.o bn_mp_exch.o bn_mp_expt_u32.o bn_mp_exptmod.o bn_mp_exteuclid.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_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_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_log_u32.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_pack.o bn_mp_pack_count.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_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_unpack.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 \ @@ -58,13 +57,13 @@ bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o 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 +79,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..4cefc7e 100644 --- a/libtommath/makefile.unix +++ b/libtommath/makefile.unix @@ -21,7 +21,7 @@ RANLIB = ranlib CFLAGS = -O2 LDFLAGS = -VERSION = 1.1.0 +VERSION = 1.2.0 #Compilation flags LTM_CFLAGS = -I. $(CFLAGS) @@ -34,27 +34,26 @@ OBJECTS=bn_cutoffs.o bn_deprecated.o bn_mp_2expt.o bn_mp_abs.o bn_mp_add.o bn_mp bn_mp_and.o bn_mp_clamp.o bn_mp_clear.o bn_mp_clear_multi.o bn_mp_cmp.o bn_mp_cmp_d.o bn_mp_cmp_mag.o \ 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_error_to_string.o bn_mp_exch.o bn_mp_expt_u32.o bn_mp_exptmod.o bn_mp_exteuclid.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_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_mp_iseven.o bn_mp_isodd.o bn_mp_kronecker.o bn_mp_lcm.o bn_mp_log_u32.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_pack.o bn_mp_pack_count.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_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_unpack.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 \ @@ -62,7 +61,7 @@ bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o b 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 +80,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..a4b36b5 100644 --- a/libtommath/makefile_include.mk +++ b/libtommath/makefile_include.mk @@ -3,9 +3,9 @@ # #version of library -VERSION=1.1.0-develop -VERSION_PC=1.1.0 -VERSION_SO=2:0:1 +VERSION=1.2.0-rc1 +VERSION_PC=1.2.0 +VERSION_SO=3:0:1 PLATFORM := $(shell uname | sed -e 's/_.*//') @@ -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) @@ -153,8 +158,9 @@ cleancov-clean: cleancov: cleancov-clean clean clean: - rm -f *.gcda *.gcno *.gcov *.bat *.o *.a *.obj *.lib *.exe *.dll etclib/*.o demo/test.o demo/main.o demo/opponent.o test timing mpitest mtest/mtest mtest/mtest.exe tuning_list\ - *.idx *.toc *.log *.aux *.dvi *.lof *.ind *.ilg *.ps *.log *.s mpi.c *.da *.dyn *.dpi tommath.tex `find . -type f | grep [~] | xargs` *.lo *.la - rm -rf .libs/ + rm -f *.gcda *.gcno *.gcov *.bat *.o *.a *.obj *.lib *.exe *.dll etclib/*.o \ + demo/*.o test timing mtest_opponent mtest/mtest mtest/mtest.exe tuning_list \ + *.s mpi.c *.da *.dyn *.dpi tommath.tex `find . -type f | grep [~] | xargs` *.lo *.la + rm -rf .libs/ demo/.libs ${MAKE} -C etc/ clean MAKE=${MAKE} ${MAKE} -C doc/ clean MAKE=${MAKE} diff --git a/libtommath/tommath.def b/libtommath/tommath.def index 8418843..229fae4 100644 --- a/libtommath/tommath.def +++ b/libtommath/tommath.def @@ -33,11 +33,12 @@ EXPORTS mp_dr_setup mp_error_to_string mp_exch - mp_export mp_expt_u32 mp_exptmod mp_exteuclid mp_fread + mp_from_sbin + mp_from_ubin mp_fwrite mp_gcd mp_get_double @@ -53,8 +54,6 @@ EXPORTS mp_get_mag_ul mp_get_mag_ull mp_grow - mp_ilogb - mp_import mp_incr mp_init mp_init_copy @@ -76,6 +75,7 @@ EXPORTS mp_isodd mp_kronecker mp_lcm + mp_log_u32 mp_lshd mp_mod mp_mod_2d @@ -90,6 +90,8 @@ EXPORTS mp_mulmod mp_neg mp_or + mp_pack + mp_pack_count mp_prime_fermat mp_prime_frobenius_underwood mp_prime_is_prime @@ -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,10 @@ 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_unpack mp_xor mp_zero diff --git a/libtommath/tommath.h b/libtommath/tommath.h index 302b4ed..e9e59be 100644 --- a/libtommath/tommath.h +++ b/libtommath/tommath.h @@ -19,12 +19,20 @@ # include <stdio.h> #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 /* MS Visual C++ doesn't have a 128bit type for words, so fall back to 32bit MPI's (where words are 64bit) */ -#if (defined(_MSC_VER) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__)) && !defined(MP_64BIT) +#if (defined(_WIN32) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__)) && !defined(MP_64BIT) # define MP_32BIT #endif @@ -111,25 +119,35 @@ typedef unsigned long long private_mp_word; #ifdef MP_USE_ENUMS typedef enum { - MP_ZPOS = 0, - MP_NEG = 1 + MP_ZPOS = 0, /* positive */ + MP_NEG = 1 /* negative */ } mp_sign; typedef enum { - MP_LT = -1, - MP_EQ = 0, - MP_GT = 1 + MP_LT = -1, /* less than */ + MP_EQ = 0, /* equal */ + MP_GT = 1 /* greater than */ } mp_ord; typedef enum { MP_NO = 0, MP_YES = 1 } mp_bool; typedef enum { - MP_OKAY = 0, - MP_ERR = -1, - MP_MEM = -2, - MP_VAL = -3, - MP_ITER = -4 + MP_OKAY = 0, /* no error */ + MP_ERR = -1, /* unknown error */ + MP_MEM = -2, /* out of mem */ + MP_VAL = -3, /* invalid input */ + MP_ITER = -4, /* maximum iterations reached */ + MP_BUF = -5, /* buffer overflow, supplied buffer too small */ } mp_err; +typedef enum { + MP_LSB_FIRST = -1, + MP_MSB_FIRST = 1 +} mp_order; +typedef enum { + MP_LITTLE_ENDIAN = -1, + MP_NATIVE_ENDIAN = 0, + MP_BIG_ENDIAN = 1 +} mp_endian; #else typedef int mp_sign; #define MP_ZPOS 0 /* positive integer */ @@ -139,15 +157,23 @@ typedef int mp_ord; #define MP_EQ 0 /* equal to */ #define MP_GT 1 /* greater than */ typedef int mp_bool; -#define MP_YES 1 /* yes response */ -#define MP_NO 0 /* no response */ +#define MP_YES 1 +#define MP_NO 0 typedef int mp_err; -#define MP_OKAY 0 /* ok result */ +#define MP_OKAY 0 /* no error */ #define MP_ERR -1 /* unknown error */ #define MP_MEM -2 /* out of mem */ #define MP_VAL -3 /* invalid input */ #define MP_RANGE (MP_DEPRECATED_PRAGMA("MP_RANGE has been deprecated in favor of MP_VAL") MP_VAL) -#define MP_ITER -4 /* Max. iterations reached */ +#define MP_ITER -4 /* maximum iterations reached */ +#define MP_BUF -5 /* buffer overflow, supplied buffer too small */ +typedef int mp_order; +#define MP_LSB_FIRST -1 +#define MP_MSB_FIRST 1 +typedef int mp_endian; +#define MP_LITTLE_ENDIAN -1 +#define MP_NATIVE_ENDIAN 0 +#define MP_BIG_ENDIAN 1 #endif /* tunable cutoffs */ @@ -176,7 +202,7 @@ TOOM_SQR_CUTOFF; #endif /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */ -#define PRIVATE_MP_WARRAY (int)(1uLL << (((CHAR_BIT * sizeof(private_mp_word)) - (2 * MP_DIGIT_BIT)) + 1)) +#define PRIVATE_MP_WARRAY (int)(1 << (((CHAR_BIT * (int)sizeof(private_mp_word)) - (2 * MP_DIGIT_BIT)) + 1)) #define MP_WARRAY (MP_DEPRECATED_PRAGMA("MP_WARRAY is an internal macro") PRIVATE_MP_WARRAY) #if defined(__GNUC__) && __GNUC__ >= 4 @@ -208,7 +234,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) @@ -277,8 +303,8 @@ void mp_zero(mp_int *a); double mp_get_double(const mp_int *a) MP_WUR; mp_err mp_set_double(mp_int *a, double b) MP_WUR; -#ifndef MP_NO_STDINT /* get integer, set integer and init with integer (int32_t) */ +#ifndef MP_NO_STDINT int32_t mp_get_i32(const mp_int *a) MP_WUR; void mp_set_i32(mp_int *a, int32_t b); mp_err mp_init_i32(mp_int *a, int32_t b) MP_WUR; @@ -347,11 +373,24 @@ mp_err mp_init_copy(mp_int *a, const mp_int *b) MP_WUR; /* trim unused digits */ void mp_clamp(mp_int *a); -/* import binary data */ -mp_err mp_import(mp_int *rop, size_t count, int order, size_t size, int endian, size_t nails, const void *op) MP_WUR; /* export binary data */ -mp_err mp_export(void *rop, size_t *countp, int order, size_t size, int endian, size_t nails, const mp_int *op) MP_WUR; +MP_DEPRECATED(mp_pack) mp_err mp_export(void *rop, size_t *countp, int order, size_t size, + int endian, size_t nails, const mp_int *op) MP_WUR; + +/* import binary data */ +MP_DEPRECATED(mp_unpack) mp_err mp_import(mp_int *rop, size_t count, int order, + size_t size, int endian, size_t nails, + const void *op) MP_WUR; + +/* unpack binary data */ +mp_err mp_unpack(mp_int *rop, size_t count, mp_order order, size_t size, mp_endian endian, + size_t nails, const void *op) MP_WUR; + +/* pack binary data */ +size_t mp_pack_count(const mp_int *a, size_t nails, size_t size) MP_WUR; +mp_err mp_pack(void *rop, size_t maxcount, size_t *written, mp_order order, size_t size, + mp_endian endian, size_t nails, const mp_int *op) MP_WUR; /* ---> digit manipulation <--- */ @@ -523,7 +562,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; #endif 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; @@ -685,32 +724,40 @@ mp_err mp_prime_rand(mp_int *a, int t, int size, int flags) MP_WUR; /* Integer logarithm to integer base */ #ifndef MP_NO_STDINT -mp_err mp_ilogb(const mp_int *a, uint32_t base, mp_int *c) MP_WUR; -#endif +mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c) MP_WUR; /* c = a**b */ -#ifndef MP_NO_STDINT mp_err mp_expt_u32(const mp_int *a, uint32_t b, mp_int *c) MP_WUR; #endif 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, size_t *written, int radix) MP_WUR; mp_err mp_radix_size(const mp_int *a, int radix, int *size) MP_WUR; #ifndef MP_NO_FILE @@ -725,10 +772,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), NULL, 2) +#define mp_to_octal(M, S, N) mp_to_radix((M), (S), (N), NULL, 8) +#define mp_to_decimal(M, S, N) mp_to_radix((M), (S), (N), NULL, 10) +#define mp_to_hex(M, S, N) mp_to_radix((M), (S), (N), NULL, 16) #ifdef __cplusplus } diff --git a/libtommath/tommath_class.h b/libtommath/tommath_class.h index ae4405f..52ba585 100644 --- a/libtommath/tommath_class.h +++ b/libtommath/tommath_class.h @@ -40,11 +40,12 @@ # define BN_MP_DR_SETUP_C # define BN_MP_ERROR_TO_STRING_C # define BN_MP_EXCH_C -# define BN_MP_EXPORT_C # define BN_MP_EXPT_U32_C # 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 @@ -57,8 +58,6 @@ # define BN_MP_GET_MAG_UL_C # define BN_MP_GET_MAG_ULL_C # define BN_MP_GROW_C -# define BN_MP_ILOGB_C -# define BN_MP_IMPORT_C # define BN_MP_INCR_C # define BN_MP_INIT_C # define BN_MP_INIT_COPY_C @@ -79,6 +78,7 @@ # define BN_MP_ISODD_C # define BN_MP_KRONECKER_C # define BN_MP_LCM_C +# define BN_MP_LOG_U32_C # define BN_MP_LSHD_C # define BN_MP_MOD_C # define BN_MP_MOD_2D_C @@ -93,6 +93,8 @@ # define BN_MP_MULMOD_C # define BN_MP_NEG_C # define BN_MP_OR_C +# define BN_MP_PACK_C +# define BN_MP_PACK_COUNT_C # define BN_MP_PRIME_FERMAT_C # define BN_MP_PRIME_FROBENIUS_UNDERWOOD_C # define BN_MP_PRIME_IS_PRIME_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,11 @@ # 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_UNPACK_C # define BN_MP_XOR_C # define BN_MP_ZERO_C # define BN_PRIME_TAB_C @@ -184,10 +182,13 @@ # define BN_MP_AND_C # define BN_MP_BALANCE_MUL_C # define BN_MP_CMP_D_C +# define BN_MP_EXPORT_C # define BN_MP_EXPTMOD_FAST_C # 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 @@ -195,6 +196,7 @@ # define BN_MP_GET_MAG_U32_C # define BN_MP_GET_MAG_ULL_C # define BN_MP_GET_MAG_UL_C +# define BN_MP_IMPORT_C # define BN_MP_INIT_SET_INT_C # define BN_MP_INIT_U32_C # define BN_MP_INVMOD_SLOW_C @@ -205,15 +207,20 @@ # define BN_MP_N_ROOT_C # define BN_MP_N_ROOT_EX_C # define BN_MP_OR_C +# define BN_MP_PACK_C # 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 +228,18 @@ # 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_SIGNED_BIN_C +# define BN_MP_TO_SIGNED_BIN_N_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_UNPACK_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 @@ -386,13 +405,6 @@ #if defined(BN_MP_EXCH_C) #endif -#if defined(BN_MP_EXPORT_C) -# define BN_MP_CLEAR_C -# define BN_MP_COUNT_BITS_C -# define BN_MP_DIV_2D_C -# define BN_MP_INIT_COPY_C -#endif - #if defined(BN_MP_EXPT_U32_C) # define BN_MP_CLEAR_C # define BN_MP_INIT_COPY_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) @@ -483,28 +505,6 @@ #if defined(BN_MP_GROW_C) #endif -#if defined(BN_MP_ILOGB_C) -# define BN_MP_CLEAR_MULTI_C -# define BN_MP_CMP_C -# define BN_MP_CMP_D_C -# define BN_MP_COPY_C -# define BN_MP_COUNT_BITS_C -# define BN_MP_EXCH_C -# define BN_MP_EXPT_U32_C -# define BN_MP_INIT_MULTI_C -# define BN_MP_MUL_C -# define BN_MP_SET_C -# define BN_MP_SET_U32_C -# define BN_MP_SQR_C -# define BN_MP_ZERO_C -#endif - -#if defined(BN_MP_IMPORT_C) -# define BN_MP_CLAMP_C -# define BN_MP_MUL_2D_C -# define BN_MP_ZERO_C -#endif - #if defined(BN_MP_INCR_C) # define BN_MP_ADD_D_C # define BN_MP_DECR_C @@ -616,6 +616,20 @@ # define BN_MP_MUL_C #endif +#if defined(BN_MP_LOG_U32_C) +# define BN_MP_CLEAR_MULTI_C +# define BN_MP_CMP_C +# define BN_MP_CMP_D_C +# define BN_MP_COPY_C +# define BN_MP_COUNT_BITS_C +# define BN_MP_EXCH_C +# define BN_MP_EXPT_U32_C +# define BN_MP_INIT_MULTI_C +# define BN_MP_MUL_C +# define BN_MP_SET_C +# define BN_MP_SQR_C +#endif + #if defined(BN_MP_LSHD_C) # define BN_MP_GROW_C #endif @@ -699,6 +713,17 @@ # define BN_MP_GROW_C #endif +#if defined(BN_MP_PACK_C) +# define BN_MP_CLEAR_C +# define BN_MP_DIV_2D_C +# define BN_MP_INIT_COPY_C +# define BN_MP_PACK_COUNT_C +#endif + +#if defined(BN_MP_PACK_COUNT_C) +# define BN_MP_COUNT_BITS_C +#endif + #if defined(BN_MP_PRIME_FERMAT_C) # define BN_MP_CLEAR_C # define BN_MP_CMP_C @@ -775,9 +800,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 +851,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 +861,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 +875,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 +951,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 +995,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,43 +1065,32 @@ # 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_DIV_2D_C # define BN_MP_INIT_COPY_C -# define BN_S_MP_REVERSE_C +# define BN_MP_UBIN_SIZE_C #endif -#if defined(BN_MP_TORADIX_N_C) -# define BN_MP_CLEAR_C -# define BN_MP_DIV_D_C -# define BN_MP_INIT_COPY_C -# define BN_S_MP_REVERSE_C +#if defined(BN_MP_UBIN_SIZE_C) +# define BN_MP_COUNT_BITS_C #endif -#if defined(BN_MP_UNSIGNED_BIN_SIZE_C) -# define BN_MP_COUNT_BITS_C +#if defined(BN_MP_UNPACK_C) +# define BN_MP_CLAMP_C +# define BN_MP_MUL_2D_C +# define BN_MP_ZERO_C #endif #if defined(BN_MP_XOR_C) @@ -1106,6 +1111,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 +1129,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 +1141,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 4523a96..a3b4679 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,14 +203,12 @@ 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 */ MP_PRIVATE mp_err s_mp_rand_jenkins(void *p, size_t n) MP_WUR; -#ifndef MP_NO_STDINT MP_PRIVATE void s_mp_rand_jenkins_init(uint64_t seed); -#endif extern MP_PRIVATE const char *const mp_s_rmap; extern MP_PRIVATE const unsigned char mp_s_rmap_reverse[]; @@ -233,6 +236,13 @@ MP_DEPRECATED(s_mp_toom_mul) mp_err mp_toom_mul(const mp_int *a, const mp_int *b MP_DEPRECATED(s_mp_toom_sqr) mp_err mp_toom_sqr(const mp_int *a, mp_int *b); MP_DEPRECATED(s_mp_reverse) void bn_reverse(unsigned char *s, int len); +#define MP_GET_ENDIANNESS(x) \ + do{\ + int16_t n = 0x1; \ + char *p = (char *)&n; \ + x = (p[0] == '\x01') ? MP_LITTLE_ENDIAN : MP_BIG_ENDIAN; \ + } while (0) + /* code-generating macros */ #define MP_SET_UNSIGNED(name, type) \ void name(mp_int * a, type b) \ 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 |