Merge remote-tracking branch 'remotes/original/master' into develop
This commit is contained in:
		
						commit
						c92ccd3eec
					
				
							
								
								
									
										1
									
								
								.gitattributes
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitattributes
									
									
									
									
										vendored
									
									
								
							| @ -1,4 +1,5 @@ | |||||||
| /.gitattributes export-ignore | /.gitattributes export-ignore | ||||||
| /.gitignore     export-ignore | /.gitignore     export-ignore | ||||||
|  | /.travis.yml    export-ignore | ||||||
| 
 | 
 | ||||||
| /**             export-subst | /**             export-subst | ||||||
|  | |||||||
							
								
								
									
										33
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										33
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @ -6,16 +6,18 @@ | |||||||
| *.gcno | *.gcno | ||||||
| *.gcov | *.gcov | ||||||
| *.lib | *.lib | ||||||
| Debug/ | [Dd]ebug/ | ||||||
| Release/ | [Rr]elease/ | ||||||
|  | /MSVC_* | ||||||
| .libs/ | .libs/ | ||||||
| .coveralls.yml | .coveralls.yml | ||||||
| coverage*/ | coverage*/ | ||||||
| coverage.info | coverage.info | ||||||
|  | pre_gen/* | ||||||
| 
 | 
 | ||||||
| # suppress output of build process and *nix/windows test executables | # suppress output of build process and *nix/windows test executables | ||||||
| ltmtest | timing | ||||||
| ltmtest.exe | timing.exe | ||||||
| test | test | ||||||
| test.exe | test.exe | ||||||
| mtest | mtest | ||||||
| @ -31,6 +33,25 @@ mtest.exe | |||||||
| # ignore user specific settings | # ignore user specific settings | ||||||
| *.user | *.user | ||||||
| *.suo | *.suo | ||||||
|  | *.userosscache | ||||||
|  | *.sln.docstates | ||||||
|  | *.userprefs | ||||||
|  | # cache/options directory | ||||||
|  | .vs/ | ||||||
|  | # Backup & report files from converting an old project file to a newer Visual Studio version | ||||||
|  | _UpgradeReport_Files/ | ||||||
|  | Backup*/ | ||||||
|  | UpgradeLog*.XML | ||||||
|  | UpgradeLog*.htm | ||||||
|  | # Visual Studio 6 build log + workspace options file | ||||||
|  | *.plg | ||||||
|  | *.opt | ||||||
|  | # visual studio profiler | ||||||
|  | *.psess | ||||||
|  | *.vsp | ||||||
|  | *.vspx | ||||||
|  | *.sap | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| # ignore stuff generated by "make manual" and "make poster" | # ignore stuff generated by "make manual" and "make poster" | ||||||
| *.aux | *.aux | ||||||
| @ -56,7 +77,3 @@ test_*.txt | |||||||
| *.asc | *.asc | ||||||
| *.tar.xz | *.tar.xz | ||||||
| *.zip | *.zip | ||||||
| 
 |  | ||||||
| .idea/ |  | ||||||
| cmake-build-*/ |  | ||||||
| out/ |  | ||||||
							
								
								
									
										31
									
								
								.travis.yml
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								.travis.yml
									
									
									
									
									
								
							| @ -1,18 +1,35 @@ | |||||||
| language: c | language: c | ||||||
| compiler: |  | ||||||
|   - gcc |  | ||||||
| 
 | 
 | ||||||
| script: | install: | ||||||
|   - make travis_mtest |     - sudo apt-get update -qq | ||||||
|   - head -n 5 test.log |     - sudo apt-get install gcc-multilib | ||||||
|   - tail -n 2 test.log |  | ||||||
|   - ./testme.sh --with-cc=gcc --with-low-mp |  | ||||||
| 
 | 
 | ||||||
|  | matrix: | ||||||
|  |   fast_finish: true | ||||||
| branches: | branches: | ||||||
|   only: |   only: | ||||||
|     - master |     - master | ||||||
|     - develop |     - develop | ||||||
|     - /^release\/.*$/ |     - /^release\/.*$/ | ||||||
| 
 | 
 | ||||||
|  | compiler: | ||||||
|  |   - gcc | ||||||
|  |   - clang | ||||||
|  | script: | ||||||
|  |   - ./testme.sh --with-cc=$CC ${BUILDOPTIONS} | ||||||
|  | env: | ||||||
|  |   - | | ||||||
|  |     BUILDOPTIONS="--test-vs-mtest=333333" | ||||||
|  |   - | | ||||||
|  |     BUILDOPTIONS="--test-vs-mtest=333333 --mtest-real-rand" | ||||||
|  |   - | | ||||||
|  |     BUILDOPTIONS="--with-low-mp" | ||||||
|  |   - | | ||||||
|  |     BUILDOPTIONS="--with-m64 --with-m32 --with-mx32" | ||||||
|  | 
 | ||||||
|  | after_failure: | ||||||
|  |   - cat test_*.log | ||||||
|  |   - cat gcc_errors_*.log | ||||||
|  | 
 | ||||||
| notifications: | notifications: | ||||||
|   irc: "chat.freenode.net#libtom-notifications" |   irc: "chat.freenode.net#libtom-notifications" | ||||||
|  | |||||||
							
								
								
									
										157
									
								
								CMakeLists.txt
									
									
									
									
									
								
							
							
						
						
									
										157
									
								
								CMakeLists.txt
									
									
									
									
									
								
							| @ -1,166 +1,21 @@ | |||||||
| cmake_minimum_required(VERSION 3.6) | cmake_minimum_required(VERSION 3.6) | ||||||
| project(libtommath) | project(libtommath C) | ||||||
| 
 | 
 | ||||||
| set(CMAKE_INCLUDE_CURRENT_DIR ON) | set(CMAKE_INCLUDE_CURRENT_DIR ON) | ||||||
| 
 | 
 | ||||||
| set(SOURCE_FILES | file(GLOB SOURCE_FILES ./bn*.c) | ||||||
| #    demo/demo.c | list(APPEND SOURCE_FILES | ||||||
| #    demo/timing.c |  | ||||||
| #    etc/2kprime.c |  | ||||||
| #    etc/drprime.c |  | ||||||
| #    etc/makefile.icc |  | ||||||
| #    etc/mersenne.c |  | ||||||
| #    etc/mont.c |  | ||||||
| #    etc/pprime.c |  | ||||||
| #    etc/tune.c |  | ||||||
| #    mtest/logtab.h |  | ||||||
| #    mtest/mpi-config.h |  | ||||||
| #    mtest/mpi-types.h |  | ||||||
| #    mtest/mpi.c |  | ||||||
| #    mtest/mpi.h |  | ||||||
| #    mtest/mtest.c |  | ||||||
| #    pre_gen/mpi.c |  | ||||||
|     bn_error.c |  | ||||||
|     bn_fast_mp_invmod.c |  | ||||||
|     bn_fast_mp_montgomery_reduce.c |  | ||||||
|     bn_fast_s_mp_mul_digs.c |  | ||||||
|     bn_fast_s_mp_mul_high_digs.c |  | ||||||
|     bn_fast_s_mp_sqr.c |  | ||||||
|     bn_mp_2expt.c |  | ||||||
|     bn_mp_abs.c |  | ||||||
|     bn_mp_add.c |  | ||||||
|     bn_mp_add_d.c |  | ||||||
|     bn_mp_addmod.c |  | ||||||
|     bn_mp_and.c |  | ||||||
|     bn_mp_clamp.c |  | ||||||
|     bn_mp_clear.c |  | ||||||
|     bn_mp_clear_multi.c |  | ||||||
|     bn_mp_cmp.c |  | ||||||
|     bn_mp_cmp_d.c |  | ||||||
|     bn_mp_cmp_mag.c |  | ||||||
|     bn_mp_cnt_lsb.c |  | ||||||
|     bn_mp_copy.c |  | ||||||
|     bn_mp_count_bits.c |  | ||||||
|     bn_mp_div.c |  | ||||||
|     bn_mp_div_2.c |  | ||||||
|     bn_mp_div_2d.c |  | ||||||
|     bn_mp_div_3.c |  | ||||||
|     bn_mp_div_d.c |  | ||||||
|     bn_mp_dr_is_modulus.c |  | ||||||
|     bn_mp_dr_reduce.c |  | ||||||
|     bn_mp_dr_setup.c |  | ||||||
|     bn_mp_exch.c |  | ||||||
|     bn_mp_export.c |  | ||||||
|     bn_mp_expt_d.c |  | ||||||
|     bn_mp_expt_d_ex.c |  | ||||||
|     bn_mp_exptmod.c |  | ||||||
|     bn_mp_exptmod_fast.c |  | ||||||
|     bn_mp_exteuclid.c |  | ||||||
|     bn_mp_fread.c |  | ||||||
|     bn_mp_fwrite.c |  | ||||||
|     bn_mp_gcd.c |  | ||||||
|     bn_mp_get_int.c |  | ||||||
|     bn_mp_get_long.c |  | ||||||
|     bn_mp_get_long_long.c |  | ||||||
|     bn_mp_grow.c |  | ||||||
|     bn_mp_import.c |  | ||||||
|     bn_mp_init.c |  | ||||||
|     bn_mp_init_copy.c |  | ||||||
|     bn_mp_init_multi.c |  | ||||||
|     bn_mp_init_set.c |  | ||||||
|     bn_mp_init_set_int.c |  | ||||||
|     bn_mp_init_size.c |  | ||||||
|     bn_mp_invmod.c |  | ||||||
|     bn_mp_invmod_slow.c |  | ||||||
|     bn_mp_is_square.c |  | ||||||
|     bn_mp_jacobi.c |  | ||||||
|     bn_mp_karatsuba_mul.c |  | ||||||
|     bn_mp_karatsuba_sqr.c |  | ||||||
|     bn_mp_lcm.c |  | ||||||
|     bn_mp_lshd.c |  | ||||||
|     bn_mp_mod.c |  | ||||||
|     bn_mp_mod_2d.c |  | ||||||
|     bn_mp_mod_d.c |  | ||||||
|     bn_mp_montgomery_calc_normalization.c |  | ||||||
|     bn_mp_montgomery_reduce.c |  | ||||||
|     bn_mp_montgomery_setup.c |  | ||||||
|     bn_mp_mul.c |  | ||||||
|     bn_mp_mul_2.c |  | ||||||
|     bn_mp_mul_2d.c |  | ||||||
|     bn_mp_mul_d.c |  | ||||||
|     bn_mp_mulmod.c |  | ||||||
|     bn_mp_n_root.c |  | ||||||
|     bn_mp_n_root_ex.c |  | ||||||
|     bn_mp_neg.c |  | ||||||
|     bn_mp_or.c |  | ||||||
|     bn_mp_prime_fermat.c |  | ||||||
|     bn_mp_prime_is_divisible.c |  | ||||||
|     bn_mp_prime_is_prime.c |  | ||||||
|     bn_mp_prime_miller_rabin.c |  | ||||||
|     bn_mp_prime_next_prime.c |  | ||||||
|     bn_mp_prime_rabin_miller_trials.c |  | ||||||
|     bn_mp_prime_random_ex.c |  | ||||||
|     bn_mp_radix_size.c |  | ||||||
|     bn_mp_radix_smap.c |  | ||||||
|     bn_mp_rand.c |  | ||||||
|     bn_mp_read_radix.c |  | ||||||
|     bn_mp_read_signed_bin.c |  | ||||||
|     bn_mp_read_unsigned_bin.c |  | ||||||
|     bn_mp_reduce.c |  | ||||||
|     bn_mp_reduce_2k.c |  | ||||||
|     bn_mp_reduce_2k_l.c |  | ||||||
|     bn_mp_reduce_2k_setup.c |  | ||||||
|     bn_mp_reduce_2k_setup_l.c |  | ||||||
|     bn_mp_reduce_is_2k.c |  | ||||||
|     bn_mp_reduce_is_2k_l.c |  | ||||||
|     bn_mp_reduce_setup.c |  | ||||||
|     bn_mp_rshd.c |  | ||||||
|     bn_mp_set.c |  | ||||||
|     bn_mp_set_int.c |  | ||||||
|     bn_mp_set_long.c |  | ||||||
|     bn_mp_set_long_long.c |  | ||||||
|     bn_mp_shrink.c |  | ||||||
|     bn_mp_signed_bin_size.c |  | ||||||
|     bn_mp_sqr.c |  | ||||||
|     bn_mp_sqrmod.c |  | ||||||
|     bn_mp_sqrt.c |  | ||||||
|     bn_mp_sqrtmod_prime.c |  | ||||||
|     bn_mp_sub.c |  | ||||||
|     bn_mp_sub_d.c |  | ||||||
|     bn_mp_submod.c |  | ||||||
|     bn_mp_to_signed_bin.c |  | ||||||
|     bn_mp_to_signed_bin_n.c |  | ||||||
|     bn_mp_to_unsigned_bin.c |  | ||||||
|     bn_mp_to_unsigned_bin_n.c |  | ||||||
|     bn_mp_toom_mul.c |  | ||||||
|     bn_mp_toom_sqr.c |  | ||||||
|     bn_mp_toradix.c |  | ||||||
|     bn_mp_toradix_n.c |  | ||||||
|     bn_mp_unsigned_bin_size.c |  | ||||||
|     bn_mp_xor.c |  | ||||||
|     bn_mp_zero.c |  | ||||||
|     bn_prime_tab.c |  | ||||||
|     bn_reverse.c |  | ||||||
|     bn_s_mp_add.c |  | ||||||
|     bn_s_mp_exptmod.c |  | ||||||
|     bn_s_mp_mul_digs.c |  | ||||||
|     bn_s_mp_mul_high_digs.c |  | ||||||
|     bn_s_mp_sqr.c |  | ||||||
|     bn_s_mp_sub.c |  | ||||||
|     bncore.c |  | ||||||
|     makefile.icc |  | ||||||
|     tommath.h |     tommath.h | ||||||
|     tommath_class.h |     tommath_class.h | ||||||
|     tommath_private.h |     tommath_private.h | ||||||
|     tommath_superclass.h) |     tommath_superclass.h | ||||||
| 
 | ) | ||||||
| add_definitions(-DALL_FUNCTIONS) |  | ||||||
| 
 | 
 | ||||||
| add_library(tommathShared SHARED ${SOURCE_FILES}) | add_library(tommathShared SHARED ${SOURCE_FILES}) | ||||||
| add_library(tommathStatic STATIC ${SOURCE_FILES}) | add_library(tommathStatic STATIC ${SOURCE_FILES}) | ||||||
| 
 | 
 | ||||||
| # Install library | # Install library | ||||||
| install(TARGETS tommathShared DESTINATION lib/) | install(TARGETS tommathStatic tommathShared DESTINATION lib/) | ||||||
| # Install library headers | # Install library headers | ||||||
| file(GLOB HEADERS *.h) | file(GLOB HEADERS *.h) | ||||||
| install(FILES ${HEADERS} DESTINATION include/) | install(FILES ${HEADERS} DESTINATION include/) | ||||||
							
								
								
									
										45
									
								
								LICENSE
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								LICENSE
									
									
									
									
									
								
							| @ -1,29 +1,26 @@ | |||||||
| LibTomMath is licensed under DUAL licensing terms. |                           The LibTom license | ||||||
| 
 | 
 | ||||||
| Choose and use the license of your needs. | This is free and unencumbered software released into the public domain. | ||||||
| 
 | 
 | ||||||
| [LICENSE #1] | Anyone is free to copy, modify, publish, use, compile, sell, or | ||||||
|  | distribute this software, either in source code form or as a compiled | ||||||
|  | binary, for any purpose, commercial or non-commercial, and by any | ||||||
|  | means. | ||||||
| 
 | 
 | ||||||
| LibTomMath is public domain.  As should all quality software be. | In jurisdictions that recognize copyright laws, the author or authors | ||||||
|  | of this software dedicate any and all copyright interest in the | ||||||
|  | software to the public domain. We make this dedication for the benefit | ||||||
|  | of the public at large and to the detriment of our heirs and | ||||||
|  | successors. We intend this dedication to be an overt act of | ||||||
|  | relinquishment in perpetuity of all present and future rights to this | ||||||
|  | software under copyright law. | ||||||
| 
 | 
 | ||||||
| Tom St Denis | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||||
|  | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||||||
|  | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||||||
|  | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||||||
|  | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||||||
|  | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||||||
|  | OTHER DEALINGS IN THE SOFTWARE. | ||||||
| 
 | 
 | ||||||
| [/LICENSE #1] | For more information, please refer to <http://unlicense.org/> | ||||||
| 
 |  | ||||||
| [LICENSE #2] |  | ||||||
| 
 |  | ||||||
|             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE |  | ||||||
|                     Version 2, December 2004 |  | ||||||
| 
 |  | ||||||
|  Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> |  | ||||||
| 
 |  | ||||||
|  Everyone is permitted to copy and distribute verbatim or modified |  | ||||||
|  copies of this license document, and changing it is allowed as long |  | ||||||
|  as the name is changed. |  | ||||||
| 
 |  | ||||||
|             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE |  | ||||||
|    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION |  | ||||||
| 
 |  | ||||||
|   0. You just DO WHAT THE FUCK YOU WANT TO.  |  | ||||||
| 
 |  | ||||||
| [/LICENSE #2] |  | ||||||
|  | |||||||
							
								
								
									
										16
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								README.md
									
									
									
									
									
								
							| @ -1,8 +1,16 @@ | |||||||
| [](https://travis-ci.org/libtom/libtommath) | # libtommath | ||||||
| 
 | 
 | ||||||
| [](https://travis-ci.org/libtom/libtommath) | This is the git repository for [LibTomMath](http://www.libtom.net/LibTomMath/), a free open source portable number theoretic multiple-precision integer (MPI) library written entirely in C. | ||||||
| 
 | 
 | ||||||
| This is the git repository for [LibTomMath](http://www.libtom.org/), a free open source portable number theoretic multiple-precision integer (MPI) library written entirely in C. | ## Build Status | ||||||
|  | 
 | ||||||
|  | master: [](https://travis-ci.org/libtom/libtommath) | ||||||
|  | 
 | ||||||
|  | develop: [](https://travis-ci.org/libtom/libtommath) | ||||||
|  | 
 | ||||||
|  | API/ABI changes: [check here](https://abi-laboratory.pro/tracker/timeline/libtommath/) | ||||||
|  | 
 | ||||||
|  | ## Summary | ||||||
| 
 | 
 | ||||||
| The `develop` branch contains the in-development version. Stable releases are tagged. | The `develop` branch contains the in-development version. Stable releases are tagged. | ||||||
| 
 | 
 | ||||||
| @ -10,6 +18,8 @@ Documentation is built from the LaTeX file `bn.tex`. There is also limited docum | |||||||
| 
 | 
 | ||||||
| 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. | 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` 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_standalone` creates a stand-alone test binary that executes several test routines. | ||||||
|  | |||||||
							
								
								
									
										14
									
								
								bn_error.c
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								bn_error.c
									
									
									
									
									
								
							| @ -1,6 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| 
 | #ifdef BN_ERROR_C | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_ERROR_C) |  | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -10,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| static const struct { | static const struct { | ||||||
| @ -28,10 +24,10 @@ static const struct { | |||||||
| /* return a char * string for a given code */ | /* return a char * string for a given code */ | ||||||
| const char *mp_error_to_string(int code) | const char *mp_error_to_string(int code) | ||||||
| { | { | ||||||
|    int x; |    size_t x; | ||||||
| 
 | 
 | ||||||
|    /* scan the lookup table for the given message */ |    /* scan the lookup table for the given message */ | ||||||
|    for (x = 0; x < (int)(sizeof(msgs) / sizeof(msgs[0])); x++) { |    for (x = 0; x < (sizeof(msgs) / sizeof(msgs[0])); x++) { | ||||||
|       if (msgs[x].code == code) { |       if (msgs[x].code == code) { | ||||||
|          return msgs[x].msg; |          return msgs[x].msg; | ||||||
|       } |       } | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| 
 | #ifdef BN_FAST_MP_INVMOD_C | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_FAST_MP_INVMOD_C) |  | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -10,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* computes the modular inverse via binary extended euclidean algorithm,
 | /* computes the modular inverse via binary extended euclidean algorithm,
 | ||||||
| @ -47,6 +43,12 @@ int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) | |||||||
|       goto LBL_ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|  |    /* if one of x,y is zero return an error! */ | ||||||
|  |    if ((mp_iszero(&x) == MP_YES) || (mp_iszero(&y) == MP_YES)) { | ||||||
|  |       res = MP_VAL; | ||||||
|  |       goto LBL_ERR; | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|    /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ |    /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ | ||||||
|    if ((res = mp_copy(&x, &u)) != MP_OKAY) { |    if ((res = mp_copy(&x, &u)) != MP_OKAY) { | ||||||
|       goto LBL_ERR; |       goto LBL_ERR; | ||||||
| @ -54,7 +56,7 @@ int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) | |||||||
|    if ((res = mp_copy(&y, &v)) != MP_OKAY) { |    if ((res = mp_copy(&y, &v)) != MP_OKAY) { | ||||||
|       goto LBL_ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
|    mp_set(&D, 1); |    mp_set(&D, 1uL); | ||||||
| 
 | 
 | ||||||
| top: | top: | ||||||
|    /* 4.  while u is even do */ |    /* 4.  while u is even do */ | ||||||
| @ -123,7 +125,7 @@ top: | |||||||
|    /* now a = C, b = D, gcd == g*v */ |    /* now a = C, b = D, gcd == g*v */ | ||||||
| 
 | 
 | ||||||
|    /* if v != 1 then there is no inverse */ |    /* if v != 1 then there is no inverse */ | ||||||
|    if (mp_cmp_d(&v, 1) != MP_EQ) { |    if (mp_cmp_d(&v, 1uL) != MP_EQ) { | ||||||
|       res = MP_VAL; |       res = MP_VAL; | ||||||
|       goto LBL_ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| @ -135,6 +137,14 @@ top: | |||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
|  | 
 | ||||||
|  |    /* too big */ | ||||||
|  |    while (mp_cmp_mag(&D, b) != MP_LT) { | ||||||
|  |       if ((res = mp_sub(&D, b, &D)) != MP_OKAY) { | ||||||
|  |          goto LBL_ERR; | ||||||
|  |       } | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|    mp_exch(&D, c); |    mp_exch(&D, c); | ||||||
|    c->sign = neg; |    c->sign = neg; | ||||||
|    res = MP_OKAY; |    res = MP_OKAY; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_FAST_MP_MONTGOMERY_REDUCE_C) | #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* computes xR**-1 == x (mod N) via Montgomery Reduction
 | /* computes xR**-1 == x (mod N) via Montgomery Reduction
 | ||||||
| @ -28,6 +25,10 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) | |||||||
|    int     ix, res, olduse; |    int     ix, res, olduse; | ||||||
|    mp_word W[MP_WARRAY]; |    mp_word W[MP_WARRAY]; | ||||||
| 
 | 
 | ||||||
|  |    if (x->used > (int)MP_WARRAY) { | ||||||
|  |       return MP_VAL; | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|    /* get old used count */ |    /* get old used count */ | ||||||
|    olduse = x->used; |    olduse = x->used; | ||||||
| 
 | 
 | ||||||
| @ -73,7 +74,7 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) | |||||||
|        * that W[ix-1] have  the carry cleared (see after the inner loop) |        * that W[ix-1] have  the carry cleared (see after the inner loop) | ||||||
|        */ |        */ | ||||||
|       mp_digit mu; |       mp_digit mu; | ||||||
|       mu = (mp_digit)(((W[ix] & MP_MASK) * rho) & MP_MASK); |       mu = ((W[ix] & MP_MASK) * rho) & MP_MASK; | ||||||
| 
 | 
 | ||||||
|       /* a = a + mu * m * b**i
 |       /* a = a + mu * m * b**i
 | ||||||
|        * |        * | ||||||
| @ -102,12 +103,12 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) | |||||||
| 
 | 
 | ||||||
|          /* inner loop */ |          /* inner loop */ | ||||||
|          for (iy = 0; iy < n->used; iy++) { |          for (iy = 0; iy < n->used; iy++) { | ||||||
|             *_W++ += ((mp_word)mu) * ((mp_word)*tmpn++); |             *_W++ += (mp_word)mu * (mp_word)*tmpn++; | ||||||
|          } |          } | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* now fix carry for next digit, W[ix+1] */ |       /* now fix carry for next digit, W[ix+1] */ | ||||||
|       W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT); |       W[ix + 1] += W[ix] >> (mp_word)DIGIT_BIT; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* now we have to propagate the carries and
 |    /* now we have to propagate the carries and
 | ||||||
| @ -127,7 +128,7 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) | |||||||
|       _W = W + ++ix; |       _W = W + ++ix; | ||||||
| 
 | 
 | ||||||
|       for (; ix <= ((n->used * 2) + 1); ix++) { |       for (; ix <= ((n->used * 2) + 1); ix++) { | ||||||
|          *_W++ += *_W1++ >> ((mp_word) DIGIT_BIT); |          *_W++ += *_W1++ >> (mp_word)DIGIT_BIT; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* copy out, A = A/b**n
 |       /* copy out, A = A/b**n
 | ||||||
| @ -144,7 +145,7 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) | |||||||
|       _W = W + n->used; |       _W = W + n->used; | ||||||
| 
 | 
 | ||||||
|       for (ix = 0; ix < (n->used + 1); ix++) { |       for (ix = 0; ix < (n->used + 1); ix++) { | ||||||
|          *tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK)); |          *tmpx++ = *_W++ & (mp_word)MP_MASK; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* zero oldused digits, if the input a was larger than
 |       /* zero oldused digits, if the input a was larger than
 | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| 
 | #ifdef BN_FAST_S_MP_MUL_DIGS_C | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_FAST_S_MP_MUL_DIGS_C) |  | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -10,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* Fast (comba) multiplier
 | /* Fast (comba) multiplier
 | ||||||
| @ -70,15 +66,15 @@ int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) | |||||||
| 
 | 
 | ||||||
|       /* execute loop */ |       /* execute loop */ | ||||||
|       for (iz = 0; iz < iy; ++iz) { |       for (iz = 0; iz < iy; ++iz) { | ||||||
|          _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); |          _W += (mp_word)*tmpx++ * (mp_word)*tmpy--; | ||||||
| 
 | 
 | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* store term */ |       /* store term */ | ||||||
|       W[ix] = ((mp_digit)_W) & MP_MASK; |       W[ix] = (mp_digit)_W & MP_MASK; | ||||||
| 
 | 
 | ||||||
|       /* make next carry */ |       /* make next carry */ | ||||||
|       _W = _W >> ((mp_word)DIGIT_BIT); |       _W = _W >> (mp_word)DIGIT_BIT; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* setup dest */ |    /* setup dest */ | ||||||
| @ -88,7 +84,7 @@ int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs) | |||||||
|    { |    { | ||||||
|       mp_digit *tmpc; |       mp_digit *tmpc; | ||||||
|       tmpc = c->dp; |       tmpc = c->dp; | ||||||
|       for (ix = 0; ix < (pa + 1); ix++) { |       for (ix = 0; ix < pa; ix++) { | ||||||
|          /* now extract the previous digit [below the carry] */ |          /* now extract the previous digit [below the carry] */ | ||||||
|          *tmpc++ = W[ix]; |          *tmpc++ = W[ix]; | ||||||
|       } |       } | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| 
 | #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_FAST_S_MP_MUL_HIGH_DIGS_C) |  | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -10,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* this is a modified version of fast_s_mul_digs that only produces
 | /* this is a modified version of fast_s_mul_digs that only produces
 | ||||||
| @ -61,14 +57,14 @@ int fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int dig | |||||||
| 
 | 
 | ||||||
|       /* execute loop */ |       /* execute loop */ | ||||||
|       for (iz = 0; iz < iy; iz++) { |       for (iz = 0; iz < iy; iz++) { | ||||||
|          _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); |          _W += (mp_word)*tmpx++ * (mp_word)*tmpy--; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* store term */ |       /* store term */ | ||||||
|       W[ix] = ((mp_digit)_W) & MP_MASK; |       W[ix] = (mp_digit)_W & MP_MASK; | ||||||
| 
 | 
 | ||||||
|       /* make next carry */ |       /* make next carry */ | ||||||
|       _W = _W >> ((mp_word)DIGIT_BIT); |       _W = _W >> (mp_word)DIGIT_BIT; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* setup dest */ |    /* setup dest */ | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| 
 | #ifdef BN_FAST_S_MP_SQR_C | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_FAST_S_MP_SQR_C) |  | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -10,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* the jist of squaring...
 | /* the jist of squaring...
 | ||||||
| @ -71,22 +67,22 @@ int fast_s_mp_sqr(const mp_int *a, mp_int *b) | |||||||
| 
 | 
 | ||||||
|       /* execute loop */ |       /* execute loop */ | ||||||
|       for (iz = 0; iz < iy; iz++) { |       for (iz = 0; iz < iy; iz++) { | ||||||
|          _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--); |          _W += (mp_word)*tmpx++ * (mp_word)*tmpy--; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* double the inner product and add carry */ |       /* double the inner product and add carry */ | ||||||
|       _W = _W + _W + W1; |       _W = _W + _W + W1; | ||||||
| 
 | 
 | ||||||
|       /* even columns have the square term in them */ |       /* even columns have the square term in them */ | ||||||
|       if ((ix&1) == 0) { |       if (((unsigned)ix & 1u) == 0u) { | ||||||
|          _W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]); |          _W += (mp_word)a->dp[ix>>1] * (mp_word)a->dp[ix>>1]; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* store it */ |       /* store it */ | ||||||
|       W[ix] = (mp_digit)(_W & MP_MASK); |       W[ix] = _W & MP_MASK; | ||||||
| 
 | 
 | ||||||
|       /* make next carry */ |       /* make next carry */ | ||||||
|       W1 = _W >> ((mp_word)DIGIT_BIT); |       W1 = _W >> (mp_word)DIGIT_BIT; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* setup dest */ |    /* setup dest */ | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| 
 | #ifdef BN_MP_2EXPT_C | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_2EXPT_C) |  | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -10,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* computes a = 2**b
 | /* computes a = 2**b
 | ||||||
| @ -37,7 +33,7 @@ int mp_2expt(mp_int *a, int b) | |||||||
|    a->used = (b / DIGIT_BIT) + 1; |    a->used = (b / DIGIT_BIT) + 1; | ||||||
| 
 | 
 | ||||||
|    /* put the single bit in its place */ |    /* put the single bit in its place */ | ||||||
|    a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT); |    a->dp[b / DIGIT_BIT] = (mp_digit)1 << (mp_digit)(b % DIGIT_BIT); | ||||||
| 
 | 
 | ||||||
|    return MP_OKAY; |    return MP_OKAY; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_ABS_C) | #ifdef BN_MP_ABS_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* b = |a|
 | /* b = |a|
 | ||||||
|  | |||||||
							
								
								
									
										10
									
								
								bn_mp_add.c
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								bn_mp_add.c
									
									
									
									
									
								
							| @ -1,6 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| 
 | #ifdef BN_MP_ADD_C | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_ADD_C) |  | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -10,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* high level addition (handles signs) */ | /* high level addition (handles signs) */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_ADD_D_C) | #ifdef BN_MP_ADD_D_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* single digit addition */ | /* single digit addition */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_ADDMOD_C) | #ifdef BN_MP_ADDMOD_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* d = a + b (mod c) */ | /* d = a + b (mod c) */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_AND_C) | #ifdef BN_MP_AND_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* AND two ints together */ | /* AND two ints together */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_CLAMP_C) | #ifdef BN_MP_CLAMP_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* trim unused digits
 | /* trim unused digits
 | ||||||
| @ -27,7 +24,7 @@ void mp_clamp(mp_int *a) | |||||||
|    /* decrease used while the most significant digit is
 |    /* decrease used while the most significant digit is
 | ||||||
|     * zero. |     * zero. | ||||||
|     */ |     */ | ||||||
|    while ((a->used > 0) && (a->dp[a->used - 1] == 0)) { |    while ((a->used > 0) && (a->dp[a->used - 1] == 0u)) { | ||||||
|       --(a->used); |       --(a->used); | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_CLEAR_C) | #ifdef BN_MP_CLEAR_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* clear one (frees)  */ | /* clear one (frees)  */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_CLEAR_MULTI_C) | #ifdef BN_MP_CLEAR_MULTI_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,11 +9,9 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
|  | 
 | ||||||
| #include <stdarg.h> | #include <stdarg.h> | ||||||
| 
 | 
 | ||||||
| void mp_clear_multi(mp_int *mp, ...) | void mp_clear_multi(mp_int *mp, ...) | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_CMP_C) | #ifdef BN_MP_CMP_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* compare two ints (signed)*/ | /* compare two ints (signed)*/ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_CMP_D_C) | #ifdef BN_MP_CMP_D_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* compare a digit */ | /* compare a digit */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_CMP_MAG_C) | #ifdef BN_MP_CMP_MAG_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* compare maginitude of two ints (unsigned) */ | /* compare maginitude of two ints (unsigned) */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_CNT_LSB_C) | #ifdef BN_MP_CNT_LSB_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| static const int lnz[16] = { | static const int lnz[16] = { | ||||||
| @ -31,17 +28,17 @@ int mp_cnt_lsb(const mp_int *a) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* scan lower digits until non-zero */ |    /* scan lower digits until non-zero */ | ||||||
|    for (x = 0; (x < a->used) && (a->dp[x] == 0); x++) {} |    for (x = 0; (x < a->used) && (a->dp[x] == 0u); x++) {} | ||||||
|    q = a->dp[x]; |    q = a->dp[x]; | ||||||
|    x *= DIGIT_BIT; |    x *= DIGIT_BIT; | ||||||
| 
 | 
 | ||||||
|    /* now scan this digit until a 1 is found */ |    /* now scan this digit until a 1 is found */ | ||||||
|    if ((q & 1) == 0) { |    if ((q & 1u) == 0u) { | ||||||
|       do { |       do { | ||||||
|          qq  = q & 15; |          qq  = q & 15u; | ||||||
|          x  += lnz[qq]; |          x  += lnz[qq]; | ||||||
|          q >>= 4; |          q >>= 4; | ||||||
|       } while (qq == 0); |       } while (qq == 0u); | ||||||
|    } |    } | ||||||
|    return x; |    return x; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_COPY_C) | #ifdef BN_MP_COPY_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* copy, b = a */ | /* copy, b = a */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_COUNT_BITS_C) | #ifdef BN_MP_COUNT_BITS_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* returns the number of bits in an int */ | /* returns the number of bits in an int */ | ||||||
| @ -31,9 +28,9 @@ int mp_count_bits(const mp_int *a) | |||||||
| 
 | 
 | ||||||
|    /* take the last digit and count the bits in it */ |    /* take the last digit and count the bits in it */ | ||||||
|    q = a->dp[a->used - 1]; |    q = a->dp[a->used - 1]; | ||||||
|    while (q > ((mp_digit) 0)) { |    while (q > (mp_digit)0) { | ||||||
|       ++r; |       ++r; | ||||||
|       q >>= ((mp_digit) 1); |       q >>= (mp_digit)1; | ||||||
|    } |    } | ||||||
|    return r; |    return r; | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										39
									
								
								bn_mp_div.c
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								bn_mp_div.c
									
									
									
									
									
								
							| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_DIV_C) | #ifdef BN_MP_DIV_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #ifdef BN_MP_DIV_SMALL | #ifdef BN_MP_DIV_SMALL | ||||||
| @ -47,7 +44,7 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|    mp_set(&tq, 1); |    mp_set(&tq, 1uL); | ||||||
|    n = mp_count_bits(a) - mp_count_bits(b); |    n = mp_count_bits(a) - mp_count_bits(b); | ||||||
|    if (((res = mp_abs(a, &ta)) != MP_OKAY) || |    if (((res = mp_abs(a, &ta)) != MP_OKAY) || | ||||||
|        ((res = mp_abs(b, &tb)) != MP_OKAY) || |        ((res = mp_abs(b, &tb)) != MP_OKAY) || | ||||||
| @ -150,8 +147,8 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) | |||||||
| 
 | 
 | ||||||
|    /* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */ |    /* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */ | ||||||
|    norm = mp_count_bits(&y) % DIGIT_BIT; |    norm = mp_count_bits(&y) % DIGIT_BIT; | ||||||
|    if (norm < (int)(DIGIT_BIT-1)) { |    if (norm < (DIGIT_BIT - 1)) { | ||||||
|       norm = (DIGIT_BIT-1) - norm; |       norm = (DIGIT_BIT - 1) - norm; | ||||||
|       if ((res = mp_mul_2d(&x, norm, &x)) != MP_OKAY) { |       if ((res = mp_mul_2d(&x, norm, &x)) != MP_OKAY) { | ||||||
|          goto LBL_Y; |          goto LBL_Y; | ||||||
|       } |       } | ||||||
| @ -190,16 +187,16 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) | |||||||
|       /* step 3.1 if xi == yt then set q{i-t-1} to b-1,
 |       /* step 3.1 if xi == yt then set q{i-t-1} to b-1,
 | ||||||
|        * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */ |        * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */ | ||||||
|       if (x.dp[i] == y.dp[t]) { |       if (x.dp[i] == y.dp[t]) { | ||||||
|          q.dp[(i - t) - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1); |          q.dp[(i - t) - 1] = ((mp_digit)1 << (mp_digit)DIGIT_BIT) - (mp_digit)1; | ||||||
|       } else { |       } else { | ||||||
|          mp_word tmp; |          mp_word tmp; | ||||||
|          tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT); |          tmp = (mp_word)x.dp[i] << (mp_word)DIGIT_BIT; | ||||||
|          tmp |= ((mp_word) x.dp[i - 1]); |          tmp |= (mp_word)x.dp[i - 1]; | ||||||
|          tmp /= ((mp_word) y.dp[t]); |          tmp /= (mp_word)y.dp[t]; | ||||||
|          if (tmp > (mp_word) MP_MASK) { |          if (tmp > (mp_word)MP_MASK) { | ||||||
|             tmp = MP_MASK; |             tmp = MP_MASK; | ||||||
|          } |          } | ||||||
|          q.dp[(i - t) - 1] = (mp_digit)(tmp & (mp_word)(MP_MASK)); |          q.dp[(i - t) - 1] = (mp_digit)(tmp & (mp_word)MP_MASK); | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* while (q{i-t-1} * (yt * b + y{t-1})) >
 |       /* while (q{i-t-1} * (yt * b + y{t-1})) >
 | ||||||
| @ -207,13 +204,13 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) | |||||||
| 
 | 
 | ||||||
|          do q{i-t-1} -= 1; |          do q{i-t-1} -= 1; | ||||||
|       */ |       */ | ||||||
|       q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] + 1) & MP_MASK; |       q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] + 1uL) & (mp_digit)MP_MASK; | ||||||
|       do { |       do { | ||||||
|          q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1) & MP_MASK; |          q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1uL) & (mp_digit)MP_MASK; | ||||||
| 
 | 
 | ||||||
|          /* find left hand */ |          /* find left hand */ | ||||||
|          mp_zero(&t1); |          mp_zero(&t1); | ||||||
|          t1.dp[0] = ((t - 1) < 0) ? 0 : y.dp[t - 1]; |          t1.dp[0] = ((t - 1) < 0) ? 0u : y.dp[t - 1]; | ||||||
|          t1.dp[1] = y.dp[t]; |          t1.dp[1] = y.dp[t]; | ||||||
|          t1.used = 2; |          t1.used = 2; | ||||||
|          if ((res = mp_mul_d(&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) { |          if ((res = mp_mul_d(&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) { | ||||||
| @ -221,8 +218,8 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) | |||||||
|          } |          } | ||||||
| 
 | 
 | ||||||
|          /* find right hand */ |          /* find right hand */ | ||||||
|          t2.dp[0] = ((i - 2) < 0) ? 0 : x.dp[i - 2]; |          t2.dp[0] = ((i - 2) < 0) ? 0u : x.dp[i - 2]; | ||||||
|          t2.dp[1] = ((i - 1) < 0) ? 0 : x.dp[i - 1]; |          t2.dp[1] = ((i - 1) < 0) ? 0u : x.dp[i - 1]; | ||||||
|          t2.dp[2] = x.dp[i]; |          t2.dp[2] = x.dp[i]; | ||||||
|          t2.used = 3; |          t2.used = 3; | ||||||
|       } while (mp_cmp_mag(&t1, &t2) == MP_GT); |       } while (mp_cmp_mag(&t1, &t2) == MP_GT); | ||||||
| @ -252,7 +249,7 @@ int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d) | |||||||
|             goto LBL_Y; |             goto LBL_Y; | ||||||
|          } |          } | ||||||
| 
 | 
 | ||||||
|          q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1UL) & MP_MASK; |          q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1uL) & MP_MASK; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_DIV_2_C) | #ifdef BN_MP_DIV_2_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* b = a/2 */ | /* b = a/2 */ | ||||||
| @ -42,7 +39,7 @@ int mp_div_2(const mp_int *a, mp_int *b) | |||||||
|       r = 0; |       r = 0; | ||||||
|       for (x = b->used - 1; x >= 0; x--) { |       for (x = b->used - 1; x >= 0; x--) { | ||||||
|          /* get the carry for the next iteration */ |          /* get the carry for the next iteration */ | ||||||
|          rr = *tmpa & 1; |          rr = *tmpa & 1u; | ||||||
| 
 | 
 | ||||||
|          /* shift the current digit, add in carry and store */ |          /* shift the current digit, add in carry and store */ | ||||||
|          *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1)); |          *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1)); | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_DIV_2D_C) | #ifdef BN_MP_DIV_2D_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* shift right by a certain bit count (store quotient in c, optional remainder in d) */ | /* shift right by a certain bit count (store quotient in c, optional remainder in d) */ | ||||||
| @ -44,20 +41,20 @@ int mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* shift by as many digits in the bit count */ |    /* shift by as many digits in the bit count */ | ||||||
|    if (b >= (int)DIGIT_BIT) { |    if (b >= DIGIT_BIT) { | ||||||
|       mp_rshd(c, b / DIGIT_BIT); |       mp_rshd(c, b / DIGIT_BIT); | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* shift any bit count < DIGIT_BIT */ |    /* shift any bit count < DIGIT_BIT */ | ||||||
|    D = (mp_digit)(b % DIGIT_BIT); |    D = (mp_digit)(b % DIGIT_BIT); | ||||||
|    if (D != 0) { |    if (D != 0u) { | ||||||
|       mp_digit *tmpc, mask, shift; |       mp_digit *tmpc, mask, shift; | ||||||
| 
 | 
 | ||||||
|       /* mask */ |       /* mask */ | ||||||
|       mask = (((mp_digit)1) << D) - 1; |       mask = ((mp_digit)1 << D) - 1uL; | ||||||
| 
 | 
 | ||||||
|       /* shift for lsb */ |       /* shift for lsb */ | ||||||
|       shift = DIGIT_BIT - D; |       shift = (mp_digit)DIGIT_BIT - D; | ||||||
| 
 | 
 | ||||||
|       /* alias */ |       /* alias */ | ||||||
|       tmpc = c->dp + (c->used - 1); |       tmpc = c->dp + (c->used - 1); | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_DIV_3_C) | #ifdef BN_MP_DIV_3_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* divide by three (based on routine from MPI and the GMP manual) */ | /* divide by three (based on routine from MPI and the GMP manual) */ | ||||||
| @ -24,7 +21,7 @@ int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) | |||||||
|    int      res, ix; |    int      res, ix; | ||||||
| 
 | 
 | ||||||
|    /* b = 2**DIGIT_BIT / 3 */ |    /* b = 2**DIGIT_BIT / 3 */ | ||||||
|    b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3); |    b = ((mp_word)1 << (mp_word)DIGIT_BIT) / (mp_word)3; | ||||||
| 
 | 
 | ||||||
|    if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { |    if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { | ||||||
|       return res; |       return res; | ||||||
| @ -34,11 +31,11 @@ int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) | |||||||
|    q.sign = a->sign; |    q.sign = a->sign; | ||||||
|    w = 0; |    w = 0; | ||||||
|    for (ix = a->used - 1; ix >= 0; ix--) { |    for (ix = a->used - 1; ix >= 0; ix--) { | ||||||
|       w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]); |       w = (w << (mp_word)DIGIT_BIT) | (mp_word)a->dp[ix]; | ||||||
| 
 | 
 | ||||||
|       if (w >= 3) { |       if (w >= 3u) { | ||||||
|          /* multiply w by [1/3] */ |          /* multiply w by [1/3] */ | ||||||
|          t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT); |          t = (w * (mp_word)b) >> (mp_word)DIGIT_BIT; | ||||||
| 
 | 
 | ||||||
|          /* now subtract 3 * [w/3] from w, to get the remainder */ |          /* now subtract 3 * [w/3] from w, to get the remainder */ | ||||||
|          w -= t+t+t; |          w -= t+t+t; | ||||||
| @ -46,9 +43,9 @@ int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d) | |||||||
|          /* fixup the remainder as required since
 |          /* fixup the remainder as required since
 | ||||||
|           * the optimization is not exact. |           * the optimization is not exact. | ||||||
|           */ |           */ | ||||||
|          while (w >= 3) { |          while (w >= 3u) { | ||||||
|             t += 1; |             t += 1u; | ||||||
|             w -= 3; |             w -= 3u; | ||||||
|          } |          } | ||||||
|       } else { |       } else { | ||||||
|          t = 0; |          t = 0; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_DIV_D_C) | #ifdef BN_MP_DIV_D_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| static int s_is_power_of_two(mp_digit b, int *p) | static int s_is_power_of_two(mp_digit b, int *p) | ||||||
| @ -20,12 +17,12 @@ static int s_is_power_of_two(mp_digit b, int *p) | |||||||
|    int x; |    int x; | ||||||
| 
 | 
 | ||||||
|    /* fast return if no power of two */ |    /* fast return if no power of two */ | ||||||
|    if ((b == 0) || ((b & (b-1)) != 0)) { |    if ((b == 0u) || ((b & (b-1u)) != 0u)) { | ||||||
|       return 0; |       return 0; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    for (x = 0; x < DIGIT_BIT; x++) { |    for (x = 0; x < DIGIT_BIT; x++) { | ||||||
|       if (b == (((mp_digit)1)<<x)) { |       if (b == ((mp_digit)1<<(mp_digit)x)) { | ||||||
|          *p = x; |          *p = x; | ||||||
|          return 1; |          return 1; | ||||||
|       } |       } | ||||||
| @ -42,12 +39,12 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) | |||||||
|    int     res, ix; |    int     res, ix; | ||||||
| 
 | 
 | ||||||
|    /* cannot divide by zero */ |    /* cannot divide by zero */ | ||||||
|    if (b == 0) { |    if (b == 0u) { | ||||||
|       return MP_VAL; |       return MP_VAL; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* quick outs */ |    /* quick outs */ | ||||||
|    if ((b == 1) || (mp_iszero(a) == MP_YES)) { |    if ((b == 1u) || (mp_iszero(a) == MP_YES)) { | ||||||
|       if (d != NULL) { |       if (d != NULL) { | ||||||
|          *d = 0; |          *d = 0; | ||||||
|       } |       } | ||||||
| @ -60,7 +57,7 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) | |||||||
|    /* power of two ? */ |    /* power of two ? */ | ||||||
|    if (s_is_power_of_two(b, &ix) == 1) { |    if (s_is_power_of_two(b, &ix) == 1) { | ||||||
|       if (d != NULL) { |       if (d != NULL) { | ||||||
|          *d = a->dp[0] & ((((mp_digit)1)<<ix) - 1); |          *d = a->dp[0] & (((mp_digit)1<<(mp_digit)ix) - 1uL); | ||||||
|       } |       } | ||||||
|       if (c != NULL) { |       if (c != NULL) { | ||||||
|          return mp_div_2d(a, ix, c, NULL); |          return mp_div_2d(a, ix, c, NULL); | ||||||
| @ -70,7 +67,7 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) | |||||||
| 
 | 
 | ||||||
| #ifdef BN_MP_DIV_3_C | #ifdef BN_MP_DIV_3_C | ||||||
|    /* three? */ |    /* three? */ | ||||||
|    if (b == 3) { |    if (b == 3u) { | ||||||
|       return mp_div_3(a, c, d); |       return mp_div_3(a, c, d); | ||||||
|    } |    } | ||||||
| #endif | #endif | ||||||
| @ -84,15 +81,15 @@ int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d) | |||||||
|    q.sign = a->sign; |    q.sign = a->sign; | ||||||
|    w = 0; |    w = 0; | ||||||
|    for (ix = a->used - 1; ix >= 0; ix--) { |    for (ix = a->used - 1; ix >= 0; ix--) { | ||||||
|       w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]); |       w = (w << (mp_word)DIGIT_BIT) | (mp_word)a->dp[ix]; | ||||||
| 
 | 
 | ||||||
|       if (w >= b) { |       if (w >= b) { | ||||||
|          t = (mp_digit)(w / b); |          t = (mp_digit)(w / b); | ||||||
|          w -= ((mp_word)t) * ((mp_word)b); |          w -= (mp_word)t * (mp_word)b; | ||||||
|       } else { |       } else { | ||||||
|          t = 0; |          t = 0; | ||||||
|       } |       } | ||||||
|       q.dp[ix] = (mp_digit)t; |       q.dp[ix] = t; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    if (d != NULL) { |    if (d != NULL) { | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_DR_IS_MODULUS_C) | #ifdef BN_MP_DR_IS_MODULUS_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* determines if a number is a valid DR modulus */ | /* determines if a number is a valid DR modulus */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_DR_REDUCE_C) | #ifdef BN_MP_DR_REDUCE_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
 | /* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
 | ||||||
| @ -61,7 +58,7 @@ top: | |||||||
| 
 | 
 | ||||||
|    /* compute (x mod B**m) + k * [x/B**m] inline and inplace */ |    /* compute (x mod B**m) + k * [x/B**m] inline and inplace */ | ||||||
|    for (i = 0; i < m; i++) { |    for (i = 0; i < m; i++) { | ||||||
|       r         = (((mp_word)*tmpx2++) * (mp_word)k) + *tmpx1 + mu; |       r         = ((mp_word)*tmpx2++ * (mp_word)k) + *tmpx1 + mu; | ||||||
|       *tmpx1++  = (mp_digit)(r & MP_MASK); |       *tmpx1++  = (mp_digit)(r & MP_MASK); | ||||||
|       mu        = (mp_digit)(r >> ((mp_word)DIGIT_BIT)); |       mu        = (mp_digit)(r >> ((mp_word)DIGIT_BIT)); | ||||||
|    } |    } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_DR_SETUP_C) | #ifdef BN_MP_DR_SETUP_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* determines the setup value */ | /* determines the setup value */ | ||||||
| @ -21,7 +18,7 @@ void mp_dr_setup(const mp_int *a, mp_digit *d) | |||||||
|    /* the casts are required if DIGIT_BIT is one less than
 |    /* the casts are required if DIGIT_BIT is one less than
 | ||||||
|     * the number of bits in a mp_digit [e.g. DIGIT_BIT==31] |     * the number of bits in a mp_digit [e.g. DIGIT_BIT==31] | ||||||
|     */ |     */ | ||||||
|    *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - ((mp_word)a->dp[0])); |    *d = (mp_digit)(((mp_word)1 << (mp_word)DIGIT_BIT) - (mp_word)a->dp[0]); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_EXCH_C) | #ifdef BN_MP_EXCH_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* swap the elements of two integers, for cases where you can't simply swap the
 | /* swap the elements of two integers, for cases where you can't simply swap the
 | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_EXPORT_C) | #ifdef BN_MP_EXPORT_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* based on gmp's mpz_export.
 | /* based on gmp's mpz_export.
 | ||||||
| @ -38,33 +35,33 @@ int mp_export(void *rop, size_t *countp, int order, size_t size, | |||||||
|       } lint; |       } lint; | ||||||
|       lint.i = 0x01020304; |       lint.i = 0x01020304; | ||||||
| 
 | 
 | ||||||
|       endian = (lint.c[0] == 4) ? -1 : 1; |       endian = (lint.c[0] == '\x04') ? -1 : 1; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    odd_nails = (nails % 8); |    odd_nails = (nails % 8u); | ||||||
|    odd_nail_mask = 0xff; |    odd_nail_mask = 0xff; | ||||||
|    for (i = 0; i < odd_nails; ++i) { |    for (i = 0; i < odd_nails; ++i) { | ||||||
|       odd_nail_mask ^= (1 << (7 - i)); |       odd_nail_mask ^= (unsigned char)(1u << (7u - i)); | ||||||
|    } |    } | ||||||
|    nail_bytes = nails / 8; |    nail_bytes = nails / 8u; | ||||||
| 
 | 
 | ||||||
|    bits = mp_count_bits(&t); |    bits = (size_t)mp_count_bits(&t); | ||||||
|    count = (bits / ((size * 8) - nails)) + (((bits % ((size * 8) - nails)) != 0) ? 1 : 0); |    count = (bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u); | ||||||
| 
 | 
 | ||||||
|    for (i = 0; i < count; ++i) { |    for (i = 0; i < count; ++i) { | ||||||
|       for (j = 0; j < size; ++j) { |       for (j = 0; j < size; ++j) { | ||||||
|          unsigned char *byte = (unsigned char *)rop + |          unsigned char *byte = (unsigned char *)rop + | ||||||
|                                (((order == -1) ? i : ((count - 1) - i)) * size) + |                                (((order == -1) ? i : ((count - 1u) - i)) * size) + | ||||||
|                                ((endian == -1) ? j : ((size - 1) - j)); |                                ((endian == -1) ? j : ((size - 1u) - j)); | ||||||
| 
 | 
 | ||||||
|          if (j >= (size - nail_bytes)) { |          if (j >= (size - nail_bytes)) { | ||||||
|             *byte = 0; |             *byte = 0; | ||||||
|             continue; |             continue; | ||||||
|          } |          } | ||||||
| 
 | 
 | ||||||
|          *byte = (unsigned char)((j == ((size - nail_bytes) - 1)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFF)); |          *byte = (unsigned char)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL)); | ||||||
| 
 | 
 | ||||||
|          if ((result = mp_div_2d(&t, ((j == ((size - nail_bytes) - 1)) ? (8 - odd_nails) : 8), &t, NULL)) != MP_OKAY) { |          if ((result = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) { | ||||||
|             mp_clear(&t); |             mp_clear(&t); | ||||||
|             return result; |             return result; | ||||||
|          } |          } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_EXPT_D_C) | #ifdef BN_MP_EXPT_D_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* wrapper function for mp_expt_d_ex() */ | /* wrapper function for mp_expt_d_ex() */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_EXPT_D_EX_C) | #ifdef BN_MP_EXPT_D_EX_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* calculate c = a**b  using a square-multiply algorithm */ | /* calculate c = a**b  using a square-multiply algorithm */ | ||||||
| @ -28,12 +25,12 @@ int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* set initial result */ |    /* set initial result */ | ||||||
|    mp_set(c, 1); |    mp_set(c, 1uL); | ||||||
| 
 | 
 | ||||||
|    if (fast != 0) { |    if (fast != 0) { | ||||||
|       while (b > 0) { |       while (b > 0u) { | ||||||
|          /* if the bit is set multiply */ |          /* if the bit is set multiply */ | ||||||
|          if ((b & 1) != 0) { |          if ((b & 1u) != 0u) { | ||||||
|             if ((res = mp_mul(c, &g, c)) != MP_OKAY) { |             if ((res = mp_mul(c, &g, c)) != MP_OKAY) { | ||||||
|                mp_clear(&g); |                mp_clear(&g); | ||||||
|                return res; |                return res; | ||||||
| @ -41,7 +38,7 @@ int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) | |||||||
|          } |          } | ||||||
| 
 | 
 | ||||||
|          /* square */ |          /* square */ | ||||||
|          if (b > 1) { |          if (b > 1u) { | ||||||
|             if ((res = mp_sqr(&g, &g)) != MP_OKAY) { |             if ((res = mp_sqr(&g, &g)) != MP_OKAY) { | ||||||
|                mp_clear(&g); |                mp_clear(&g); | ||||||
|                return res; |                return res; | ||||||
| @ -52,7 +49,7 @@ int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) | |||||||
|          b >>= 1; |          b >>= 1; | ||||||
|       } |       } | ||||||
|    } else { |    } else { | ||||||
|       for (x = 0; x < DIGIT_BIT; x++) { |       for (x = 0; x < (unsigned)DIGIT_BIT; x++) { | ||||||
|          /* square */ |          /* square */ | ||||||
|          if ((res = mp_sqr(c, c)) != MP_OKAY) { |          if ((res = mp_sqr(c, c)) != MP_OKAY) { | ||||||
|             mp_clear(&g); |             mp_clear(&g); | ||||||
| @ -60,7 +57,7 @@ int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) | |||||||
|          } |          } | ||||||
| 
 | 
 | ||||||
|          /* if the bit is set multiply */ |          /* if the bit is set multiply */ | ||||||
|          if ((b & (mp_digit)(((mp_digit)1) << (DIGIT_BIT - 1))) != 0) { |          if ((b & ((mp_digit)1 << (DIGIT_BIT - 1))) != 0u) { | ||||||
|             if ((res = mp_mul(c, &g, c)) != MP_OKAY) { |             if ((res = mp_mul(c, &g, c)) != MP_OKAY) { | ||||||
|                mp_clear(&g); |                mp_clear(&g); | ||||||
|                return res; |                return res; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_EXPTMOD_C) | #ifdef BN_MP_EXPTMOD_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @ -21,15 +18,6 @@ | |||||||
|  * embedded in the normal function but that wasted alot of stack space |  * embedded in the normal function but that wasted alot of stack space | ||||||
|  * for nothing (since 99% of the time the Montgomery code would be called) |  * for nothing (since 99% of the time the Montgomery code would be called) | ||||||
|  */ |  */ | ||||||
| 
 |  | ||||||
| #define BN_MP_REDUCE_IS_2K_L_C |  | ||||||
| #define BN_MP_REDUCE_2K_L_C |  | ||||||
| #define BN_S_MP_EXPTMOD_C |  | ||||||
| #define BN_MP_DR_IS_MODULUS_C |  | ||||||
| #define BN_MP_REDUCE_IS_2K_C |  | ||||||
| #define BN_MP_EXPTMOD_FAST_C |  | ||||||
| #define BN_MP_INVMOD_C |  | ||||||
| 
 |  | ||||||
| int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) | int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y) | ||||||
| { | { | ||||||
|    int dr; |    int dr; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_EXPTMOD_FAST_C) | #ifdef BN_MP_EXPTMOD_FAST_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
 | /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
 | ||||||
| @ -29,14 +26,6 @@ | |||||||
| #   define TAB_SIZE 256 | #   define TAB_SIZE 256 | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| #define BN_MP_MONTGOMERY_SETUP_C |  | ||||||
| #define BN_FAST_MP_MONTGOMERY_REDUCE_C |  | ||||||
| #define BN_MP_MONTGOMERY_REDUCE_C |  | ||||||
| #define BN_MP_DR_SETUP_C |  | ||||||
| #define BN_MP_DR_REDUCE_C |  | ||||||
| #define BN_MP_REDUCE_2K_SETUP_C |  | ||||||
| #define BN_MP_REDUCE_2K_C |  | ||||||
| #define BN_MP_MONTGOMERY_CALC_NORMALIZATION_C |  | ||||||
| int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) | int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode) | ||||||
| { | { | ||||||
|    mp_int  M[TAB_SIZE], res; |    mp_int  M[TAB_SIZE], res; | ||||||
| @ -47,7 +36,7 @@ int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y | |||||||
|     * one of many reduction algorithms without modding the guts of |     * one of many reduction algorithms without modding the guts of | ||||||
|     * the code with if statements everywhere. |     * the code with if statements everywhere. | ||||||
|     */ |     */ | ||||||
|    int (*redux)(mp_int *,const mp_int *,mp_digit); |    int (*redux)(mp_int *x, const mp_int *n, mp_digit rho); | ||||||
| 
 | 
 | ||||||
|    /* find window size */ |    /* find window size */ | ||||||
|    x = mp_count_bits(X); |    x = mp_count_bits(X); | ||||||
| @ -104,7 +93,7 @@ int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y | |||||||
| 
 | 
 | ||||||
|       /* automatically pick the comba one if available (saves quite a few calls/ifs) */ |       /* automatically pick the comba one if available (saves quite a few calls/ifs) */ | ||||||
| #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C | #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C | ||||||
|       if ((((P->used * 2) + 1) < MP_WARRAY) && |       if ((((P->used * 2) + 1) < (int)MP_WARRAY) && | ||||||
|           (P->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { |           (P->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { | ||||||
|          redux = fast_mp_montgomery_reduce; |          redux = fast_mp_montgomery_reduce; | ||||||
|       } else |       } else | ||||||
| @ -168,22 +157,22 @@ int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y | |||||||
|       goto LBL_RES; |       goto LBL_RES; | ||||||
| #endif | #endif | ||||||
|    } else { |    } else { | ||||||
|       mp_set(&res, 1); |       mp_set(&res, 1uL); | ||||||
|       if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) { |       if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) { | ||||||
|          goto LBL_RES; |          goto LBL_RES; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */ |    /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */ | ||||||
|    if ((err = mp_copy(&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { |    if ((err = mp_copy(&M[1], &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) { | ||||||
|       goto LBL_RES; |       goto LBL_RES; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    for (x = 0; x < (winsize - 1); x++) { |    for (x = 0; x < (winsize - 1); x++) { | ||||||
|       if ((err = mp_sqr(&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) { |       if ((err = mp_sqr(&M[(size_t)1 << (winsize - 1)], &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) { | ||||||
|          goto LBL_RES; |          goto LBL_RES; | ||||||
|       } |       } | ||||||
|       if ((err = redux(&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) { |       if ((err = redux(&M[(size_t)1 << (winsize - 1)], P, mp)) != MP_OKAY) { | ||||||
|          goto LBL_RES; |          goto LBL_RES; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_EXTEUCLID_C) | #ifdef BN_MP_EXTEUCLID_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* Extended euclidean algorithm of (a, b) produces
 | /* Extended euclidean algorithm of (a, b) produces
 | ||||||
| @ -28,76 +25,76 @@ int mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_in | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* initialize, (u1,u2,u3) = (1,0,a) */ |    /* initialize, (u1,u2,u3) = (1,0,a) */ | ||||||
|    mp_set(&u1, 1); |    mp_set(&u1, 1uL); | ||||||
|    if ((err = mp_copy(a, &u3)) != MP_OKAY)                                        { |    if ((err = mp_copy(a, &u3)) != MP_OKAY) { | ||||||
|       goto LBL_ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* initialize, (v1,v2,v3) = (0,1,b) */ |    /* initialize, (v1,v2,v3) = (0,1,b) */ | ||||||
|    mp_set(&v2, 1); |    mp_set(&v2, 1uL); | ||||||
|    if ((err = mp_copy(b, &v3)) != MP_OKAY)                                        { |    if ((err = mp_copy(b, &v3)) != MP_OKAY) { | ||||||
|       goto LBL_ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* loop while v3 != 0 */ |    /* loop while v3 != 0 */ | ||||||
|    while (mp_iszero(&v3) == MP_NO) { |    while (mp_iszero(&v3) == MP_NO) { | ||||||
|       /* q = u3/v3 */ |       /* q = u3/v3 */ | ||||||
|       if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY)                         { |       if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */ |       /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */ | ||||||
|       if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY)                              { |       if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY)                             { |       if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY)                              { |       if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY)                             { |       if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY)                              { |       if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY)                             { |       if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* (u1,u2,u3) = (v1,v2,v3) */ |       /* (u1,u2,u3) = (v1,v2,v3) */ | ||||||
|       if ((err = mp_copy(&v1, &u1)) != MP_OKAY)                                  { |       if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       if ((err = mp_copy(&v2, &u2)) != MP_OKAY)                                  { |       if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       if ((err = mp_copy(&v3, &u3)) != MP_OKAY)                                  { |       if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* (v1,v2,v3) = (t1,t2,t3) */ |       /* (v1,v2,v3) = (t1,t2,t3) */ | ||||||
|       if ((err = mp_copy(&t1, &v1)) != MP_OKAY)                                  { |       if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       if ((err = mp_copy(&t2, &v2)) != MP_OKAY)                                  { |       if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       if ((err = mp_copy(&t3, &v3)) != MP_OKAY)                                  { |       if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* make sure U3 >= 0 */ |    /* make sure U3 >= 0 */ | ||||||
|    if (u3.sign == MP_NEG) { |    if (u3.sign == MP_NEG) { | ||||||
|       if ((err = mp_neg(&u1, &u1)) != MP_OKAY)                                   { |       if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       if ((err = mp_neg(&u2, &u2)) != MP_OKAY)                                   { |       if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       if ((err = mp_neg(&u3, &u3)) != MP_OKAY)                                   { |       if ((err = mp_neg(&u3, &u3)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_FREAD_C) | #ifdef BN_MP_FREAD_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #ifndef LTM_NO_FILE | #ifndef LTM_NO_FILE | ||||||
| @ -20,13 +17,14 @@ | |||||||
| int mp_fread(mp_int *a, int radix, FILE *stream) | int mp_fread(mp_int *a, int radix, FILE *stream) | ||||||
| { | { | ||||||
|    int err, ch, neg, y; |    int err, ch, neg, y; | ||||||
|  |    unsigned pos; | ||||||
| 
 | 
 | ||||||
|    /* clear a */ |    /* clear a */ | ||||||
|    mp_zero(a); |    mp_zero(a); | ||||||
| 
 | 
 | ||||||
|    /* if first digit is - then set negative */ |    /* if first digit is - then set negative */ | ||||||
|    ch = fgetc(stream); |    ch = fgetc(stream); | ||||||
|    if (ch == '-') { |    if (ch == (int)'-') { | ||||||
|       neg = MP_NEG; |       neg = MP_NEG; | ||||||
|       ch = fgetc(stream); |       ch = fgetc(stream); | ||||||
|    } else { |    } else { | ||||||
| @ -34,27 +32,28 @@ int mp_fread(mp_int *a, int radix, FILE *stream) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    for (;;) { |    for (;;) { | ||||||
|       /* find y in the radix map */ |       pos = (unsigned)(ch - (int)'('); | ||||||
|       for (y = 0; y < radix; y++) { |       if (mp_s_rmap_reverse_sz < pos) { | ||||||
|          if (mp_s_rmap[y] == ch) { |          break; | ||||||
|             break; |  | ||||||
|          } |  | ||||||
|       } |       } | ||||||
|       if (y == radix) { | 
 | ||||||
|  |       y = (int)mp_s_rmap_reverse[pos]; | ||||||
|  | 
 | ||||||
|  |       if ((y == 0xff) || (y >= radix)) { | ||||||
|          break; |          break; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* shift up and add */ |       /* shift up and add */ | ||||||
|       if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) { |       if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) { | ||||||
|          return err; |          return err; | ||||||
|       } |       } | ||||||
|       if ((err = mp_add_d(a, y, a)) != MP_OKAY) { |       if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) { | ||||||
|          return err; |          return err; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       ch = fgetc(stream); |       ch = fgetc(stream); | ||||||
|    } |    } | ||||||
|    if (mp_cmp_d(a, 0) != MP_EQ) { |    if (mp_cmp_d(a, 0uL) != MP_EQ) { | ||||||
|       a->sign = neg; |       a->sign = neg; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_FWRITE_C) | #ifdef BN_MP_FWRITE_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #ifndef LTM_NO_FILE | #ifndef LTM_NO_FILE | ||||||
| @ -25,7 +22,7 @@ int mp_fwrite(const mp_int *a, int radix, FILE *stream) | |||||||
|       return err; |       return err; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    buf = OPT_CAST(char) XMALLOC(len); |    buf = OPT_CAST(char) XMALLOC((size_t)len); | ||||||
|    if (buf == NULL) { |    if (buf == NULL) { | ||||||
|       return MP_MEM; |       return MP_MEM; | ||||||
|    } |    } | ||||||
| @ -36,7 +33,7 @@ int mp_fwrite(const mp_int *a, int radix, FILE *stream) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    for (x = 0; x < len; x++) { |    for (x = 0; x < len; x++) { | ||||||
|       if (fputc(buf[x], stream) == EOF) { |       if (fputc((int)buf[x], stream) == EOF) { | ||||||
|          XFREE(buf); |          XFREE(buf); | ||||||
|          return MP_VAL; |          return MP_VAL; | ||||||
|       } |       } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_GCD_C) | #ifdef BN_MP_GCD_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* Greatest Common Divisor using the binary method */ | /* Greatest Common Divisor using the binary method */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_GET_INT_C) | #ifdef BN_MP_GET_INT_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* get the lower 32-bits of an mp_int */ | /* get the lower 32-bits of an mp_int */ | ||||||
| @ -26,7 +23,7 @@ unsigned long mp_get_int(const mp_int *a) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* get number of digits of the lsb we have to read */ |    /* get number of digits of the lsb we have to read */ | ||||||
|    i = MIN(a->used, (int)(((sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1; |    i = MIN(a->used, ((((int)sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1; | ||||||
| 
 | 
 | ||||||
|    /* get most significant digit of result */ |    /* get most significant digit of result */ | ||||||
|    res = DIGIT(a, i); |    res = DIGIT(a, i); | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_GET_LONG_C) | #ifdef BN_MP_GET_LONG_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* get the lower unsigned long of an mp_int, platform dependent */ | /* get the lower unsigned long of an mp_int, platform dependent */ | ||||||
| @ -26,7 +23,7 @@ unsigned long mp_get_long(const mp_int *a) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* get number of digits of the lsb we have to read */ |    /* get number of digits of the lsb we have to read */ | ||||||
|    i = MIN(a->used, (int)(((sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1; |    i = MIN(a->used, ((((int)sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1; | ||||||
| 
 | 
 | ||||||
|    /* get most significant digit of result */ |    /* get most significant digit of result */ | ||||||
|    res = DIGIT(a, i); |    res = DIGIT(a, i); | ||||||
| @ -39,3 +36,7 @@ unsigned long mp_get_long(const mp_int *a) | |||||||
|    return res; |    return res; | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|  | 
 | ||||||
|  | /* ref:         $Format:%D$ */ | ||||||
|  | /* git commit:  $Format:%H$ */ | ||||||
|  | /* commit time: $Format:%ai$ */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_GET_LONG_LONG_C) | #ifdef BN_MP_GET_LONG_LONG_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* get the lower unsigned long long of an mp_int, platform dependent */ | /* get the lower unsigned long long of an mp_int, platform dependent */ | ||||||
| @ -26,7 +23,7 @@ unsigned long long mp_get_long_long(const mp_int *a) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* get number of digits of the lsb we have to read */ |    /* get number of digits of the lsb we have to read */ | ||||||
|    i = MIN(a->used, (int)(((sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1; |    i = MIN(a->used, ((((int)sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1; | ||||||
| 
 | 
 | ||||||
|    /* get most significant digit of result */ |    /* get most significant digit of result */ | ||||||
|    res = DIGIT(a, i); |    res = DIGIT(a, i); | ||||||
| @ -39,3 +36,7 @@ unsigned long long mp_get_long_long(const mp_int *a) | |||||||
|    return res; |    return res; | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|  | 
 | ||||||
|  | /* ref:         $Format:%D$ */ | ||||||
|  | /* git commit:  $Format:%H$ */ | ||||||
|  | /* commit time: $Format:%ai$ */ | ||||||
|  | |||||||
							
								
								
									
										11
									
								
								bn_mp_grow.c
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								bn_mp_grow.c
									
									
									
									
									
								
							| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_GROW_C) | #ifdef BN_MP_GROW_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* grow as required */ | /* grow as required */ | ||||||
| @ -32,7 +29,7 @@ int mp_grow(mp_int *a, int size) | |||||||
|        * in case the operation failed we don't want |        * in case the operation failed we don't want | ||||||
|        * to overwrite the dp member of a. |        * to overwrite the dp member of a. | ||||||
|        */ |        */ | ||||||
|       tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof(mp_digit) * size); |       tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof(mp_digit) * (size_t)size); | ||||||
|       if (tmp == NULL) { |       if (tmp == NULL) { | ||||||
|          /* reallocation failed but "a" is still valid [can be freed] */ |          /* reallocation failed but "a" is still valid [can be freed] */ | ||||||
|          return MP_MEM; |          return MP_MEM; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_IMPORT_C) | #ifdef BN_MP_IMPORT_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* based on gmp's mpz_import.
 | /* based on gmp's mpz_import.
 | ||||||
| @ -34,27 +31,27 @@ int mp_import(mp_int *rop, size_t count, int order, size_t size, | |||||||
|       } lint; |       } lint; | ||||||
|       lint.i = 0x01020304; |       lint.i = 0x01020304; | ||||||
| 
 | 
 | ||||||
|       endian = (lint.c[0] == 4) ? -1 : 1; |       endian = (lint.c[0] == '\x04') ? -1 : 1; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    odd_nails = (nails % 8); |    odd_nails = (nails % 8u); | ||||||
|    odd_nail_mask = 0xff; |    odd_nail_mask = 0xff; | ||||||
|    for (i = 0; i < odd_nails; ++i) { |    for (i = 0; i < odd_nails; ++i) { | ||||||
|       odd_nail_mask ^= (1 << (7 - i)); |       odd_nail_mask ^= (unsigned char)(1u << (7u - i)); | ||||||
|    } |    } | ||||||
|    nail_bytes = nails / 8; |    nail_bytes = nails / 8u; | ||||||
| 
 | 
 | ||||||
|    for (i = 0; i < count; ++i) { |    for (i = 0; i < count; ++i) { | ||||||
|       for (j = 0; j < (size - nail_bytes); ++j) { |       for (j = 0; j < (size - nail_bytes); ++j) { | ||||||
|          unsigned char byte = *((unsigned char *)op + |          unsigned char byte = *((unsigned char *)op + | ||||||
|                                 (((order == 1) ? i : ((count - 1) - i)) * size) + |                                 (((order == 1) ? i : ((count - 1u) - i)) * size) + | ||||||
|                                 ((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes))); |                                 ((endian == 1) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes))); | ||||||
| 
 | 
 | ||||||
|          if ((result = mp_mul_2d(rop, ((j == 0) ? (8 - odd_nails) : 8), rop)) != MP_OKAY) { |          if ((result = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) { | ||||||
|             return result; |             return result; | ||||||
|          } |          } | ||||||
| 
 | 
 | ||||||
|          rop->dp[0] |= (j == 0) ? (byte & odd_nail_mask) : byte; |          rop->dp[0] |= (j == 0u) ? (mp_digit)(byte & odd_nail_mask) : (mp_digit)byte; | ||||||
|          rop->used  += 1; |          rop->used  += 1; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
|  | |||||||
							
								
								
									
										12
									
								
								bn_mp_init.c
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								bn_mp_init.c
									
									
									
									
									
								
							| @ -1,6 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| 
 | #ifdef BN_MP_INIT_C | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_INIT_C) |  | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -10,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* init a new mp_int */ | /* init a new mp_int */ | ||||||
| @ -22,7 +18,7 @@ int mp_init(mp_int *a) | |||||||
|    int i; |    int i; | ||||||
| 
 | 
 | ||||||
|    /* allocate memory required and clear it */ |    /* allocate memory required and clear it */ | ||||||
|    a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * MP_PREC); |    a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * (size_t)MP_PREC); | ||||||
|    if (a->dp == NULL) { |    if (a->dp == NULL) { | ||||||
|       return MP_MEM; |       return MP_MEM; | ||||||
|    } |    } | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| 
 | #ifdef BN_MP_INIT_COPY_C | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_INIT_COPY_C) |  | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -10,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* creates "a" then copies b into it */ | /* creates "a" then copies b into it */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_INIT_MULTI_C) | #ifdef BN_MP_INIT_MULTI_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,11 +9,9 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
|  | 
 | ||||||
| #include <stdarg.h> | #include <stdarg.h> | ||||||
| 
 | 
 | ||||||
| int mp_init_multi(mp_int *mp, ...) | int mp_init_multi(mp_int *mp, ...) | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_INIT_SET_C) | #ifdef BN_MP_INIT_SET_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* initialize and set a digit */ | /* initialize and set a digit */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_INIT_SET_INT_C) | #ifdef BN_MP_INIT_SET_INT_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* initialize and set a digit */ | /* initialize and set a digit */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_INIT_SIZE_C) | #ifdef BN_MP_INIT_SIZE_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* init an mp_init for a given size */ | /* init an mp_init for a given size */ | ||||||
| @ -24,7 +21,7 @@ int mp_init_size(mp_int *a, int size) | |||||||
|    size += (MP_PREC * 2) - (size % MP_PREC); |    size += (MP_PREC * 2) - (size % MP_PREC); | ||||||
| 
 | 
 | ||||||
|    /* alloc mem */ |    /* alloc mem */ | ||||||
|    a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * size); |    a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * (size_t)size); | ||||||
|    if (a->dp == NULL) { |    if (a->dp == NULL) { | ||||||
|       return MP_MEM; |       return MP_MEM; | ||||||
|    } |    } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_INVMOD_C) | #ifdef BN_MP_INVMOD_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,26 +9,20 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* hac 14.61, pp608 */ | /* hac 14.61, pp608 */ | ||||||
| 
 |  | ||||||
| #define BN_FAST_MP_INVMOD_C |  | ||||||
| #define BN_MP_INVMOD_SLOW_C |  | ||||||
| int mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) | int mp_invmod(const mp_int *a, const mp_int *b, mp_int *c) | ||||||
| { | { | ||||||
|    /* b cannot be negative */ |    /* b cannot be negative and has to be >1 */ | ||||||
|    if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) { |    if ((b->sign == MP_NEG) || (mp_cmp_d(b, 1uL) != MP_GT)) { | ||||||
|       return MP_VAL; |       return MP_VAL; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
| #ifdef BN_FAST_MP_INVMOD_C | #ifdef BN_FAST_MP_INVMOD_C | ||||||
|    /* if the modulus is odd we can use a faster routine instead */ |    /* if the modulus is odd we can use a faster routine instead */ | ||||||
|    if ((mp_isodd(b) == MP_YES) && (mp_cmp_d(b, 1) != MP_EQ)) { |    if ((mp_isodd(b) == MP_YES)) { | ||||||
|       return fast_mp_invmod(a, b, c); |       return fast_mp_invmod(a, b, c); | ||||||
|    } |    } | ||||||
| #endif | #endif | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_INVMOD_SLOW_C) | #ifdef BN_MP_INVMOD_SLOW_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* hac 14.61, pp608 */ | /* hac 14.61, pp608 */ | ||||||
| @ -53,8 +50,8 @@ int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c) | |||||||
|    if ((res = mp_copy(&y, &v)) != MP_OKAY) { |    if ((res = mp_copy(&y, &v)) != MP_OKAY) { | ||||||
|       goto LBL_ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
|    mp_set(&A, 1); |    mp_set(&A, 1uL); | ||||||
|    mp_set(&D, 1); |    mp_set(&D, 1uL); | ||||||
| 
 | 
 | ||||||
| top: | top: | ||||||
|    /* 4.  while u is even do */ |    /* 4.  while u is even do */ | ||||||
| @ -143,13 +140,13 @@ top: | |||||||
|    /* now a = C, b = D, gcd == g*v */ |    /* now a = C, b = D, gcd == g*v */ | ||||||
| 
 | 
 | ||||||
|    /* if v != 1 then there is no inverse */ |    /* if v != 1 then there is no inverse */ | ||||||
|    if (mp_cmp_d(&v, 1) != MP_EQ) { |    if (mp_cmp_d(&v, 1uL) != MP_EQ) { | ||||||
|       res = MP_VAL; |       res = MP_VAL; | ||||||
|       goto LBL_ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* if its too low */ |    /* if its too low */ | ||||||
|    while (mp_cmp_d(&C, 0) == MP_LT) { |    while (mp_cmp_d(&C, 0uL) == MP_LT) { | ||||||
|       if ((res = mp_add(&C, b, &C)) != MP_OKAY) { |       if ((res = mp_add(&C, b, &C)) != MP_OKAY) { | ||||||
|          goto LBL_ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_IS_SQUARE_C) | #ifdef BN_MP_IS_SQUARE_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* Check if remainders are possible squares - fast exclude non-squares */ | /* Check if remainders are possible squares - fast exclude non-squares */ | ||||||
| @ -58,15 +55,15 @@ int mp_is_square(const mp_int *arg, int *ret) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* First check mod 128 (suppose that DIGIT_BIT is at least 7) */ |    /* First check mod 128 (suppose that DIGIT_BIT is at least 7) */ | ||||||
|    if (rem_128[127 & DIGIT(arg, 0)] == 1) { |    if (rem_128[127u & DIGIT(arg, 0)] == (char)1) { | ||||||
|       return MP_OKAY; |       return MP_OKAY; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* Next check mod 105 (3*5*7) */ |    /* Next check mod 105 (3*5*7) */ | ||||||
|    if ((res = mp_mod_d(arg, 105, &c)) != MP_OKAY) { |    if ((res = mp_mod_d(arg, 105uL, &c)) != MP_OKAY) { | ||||||
|       return res; |       return res; | ||||||
|    } |    } | ||||||
|    if (rem_105[c] == 1) { |    if (rem_105[c] == (char)1) { | ||||||
|       return MP_OKAY; |       return MP_OKAY; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
| @ -75,31 +72,31 @@ int mp_is_square(const mp_int *arg, int *ret) | |||||||
|       return res; |       return res; | ||||||
|    } |    } | ||||||
|    if ((res = mp_mod(arg, &t, &t)) != MP_OKAY) { |    if ((res = mp_mod(arg, &t, &t)) != MP_OKAY) { | ||||||
|       goto ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
|    r = mp_get_int(&t); |    r = mp_get_int(&t); | ||||||
|    /* Check for other prime modules, note it's not an ERROR but we must
 |    /* Check for other prime modules, note it's not an ERROR but we must
 | ||||||
|     * free "t" so the easiest way is to goto ERR.  We know that res |     * free "t" so the easiest way is to goto LBL_ERR.  We know that res | ||||||
|     * is already equal to MP_OKAY from the mp_mod call |     * is already equal to MP_OKAY from the mp_mod call | ||||||
|     */ |     */ | ||||||
|    if (((1L<<(r%11)) & 0x5C4L) != 0L)       goto ERR; |    if (((1uL<<(r%11uL)) & 0x5C4uL) != 0uL)         goto LBL_ERR; | ||||||
|    if (((1L<<(r%13)) & 0x9E4L) != 0L)       goto ERR; |    if (((1uL<<(r%13uL)) & 0x9E4uL) != 0uL)         goto LBL_ERR; | ||||||
|    if (((1L<<(r%17)) & 0x5CE8L) != 0L)      goto ERR; |    if (((1uL<<(r%17uL)) & 0x5CE8uL) != 0uL)        goto LBL_ERR; | ||||||
|    if (((1L<<(r%19)) & 0x4F50CL) != 0L)     goto ERR; |    if (((1uL<<(r%19uL)) & 0x4F50CuL) != 0uL)       goto LBL_ERR; | ||||||
|    if (((1L<<(r%23)) & 0x7ACCA0L) != 0L)    goto ERR; |    if (((1uL<<(r%23uL)) & 0x7ACCA0uL) != 0uL)      goto LBL_ERR; | ||||||
|    if (((1L<<(r%29)) & 0xC2EDD0CL) != 0L)   goto ERR; |    if (((1uL<<(r%29uL)) & 0xC2EDD0CuL) != 0uL)     goto LBL_ERR; | ||||||
|    if (((1L<<(r%31)) & 0x6DE2B848L) != 0L)  goto ERR; |    if (((1uL<<(r%31uL)) & 0x6DE2B848uL) != 0uL)    goto LBL_ERR; | ||||||
| 
 | 
 | ||||||
|    /* Final check - is sqr(sqrt(arg)) == arg ? */ |    /* Final check - is sqr(sqrt(arg)) == arg ? */ | ||||||
|    if ((res = mp_sqrt(arg, &t)) != MP_OKAY) { |    if ((res = mp_sqrt(arg, &t)) != MP_OKAY) { | ||||||
|       goto ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
|    if ((res = mp_sqr(&t, &t)) != MP_OKAY) { |    if ((res = mp_sqr(&t, &t)) != MP_OKAY) { | ||||||
|       goto ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    *ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO; |    *ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO; | ||||||
| ERR: | LBL_ERR: | ||||||
|    mp_clear(&t); |    mp_clear(&t); | ||||||
|    return res; |    return res; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_JACOBI_C) | #ifdef BN_MP_JACOBI_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,108 +9,25 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* computes the jacobi c = (a | n) (or Legendre if n is prime)
 | /* computes the jacobi c = (a | n) (or Legendre if n is prime)
 | ||||||
|  * HAC pp. 73 Algorithm 2.149 |  * Kept for legacy reasons, please use mp_kronecker() instead | ||||||
|  * HAC is wrong here, as the special case of (0 | 1) is not |  | ||||||
|  * handled correctly. |  | ||||||
|  */ |  */ | ||||||
| int mp_jacobi(const mp_int *a, const mp_int *n, int *c) | int mp_jacobi(const mp_int *a, const mp_int *n, int *c) | ||||||
| { | { | ||||||
|    mp_int  a1, p1; |  | ||||||
|    int     k, s, r, res; |  | ||||||
|    mp_digit residue; |  | ||||||
| 
 |  | ||||||
|    /* if a < 0 return MP_VAL */ |    /* if a < 0 return MP_VAL */ | ||||||
|    if (mp_isneg(a) == MP_YES) { |    if (mp_isneg(a) == MP_YES) { | ||||||
|       return MP_VAL; |       return MP_VAL; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* if n <= 0 return MP_VAL */ |    /* if n <= 0 return MP_VAL */ | ||||||
|    if (mp_cmp_d(n, 0) != MP_GT) { |    if (mp_cmp_d(n, 0uL) != MP_GT) { | ||||||
|       return MP_VAL; |       return MP_VAL; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* step 1. handle case of a == 0 */ |    return mp_kronecker(a, n, c); | ||||||
|    if (mp_iszero(a) == MP_YES) { |  | ||||||
|       /* special case of a == 0 and n == 1 */ |  | ||||||
|       if (mp_cmp_d(n, 1) == MP_EQ) { |  | ||||||
|          *c = 1; |  | ||||||
|       } else { |  | ||||||
|          *c = 0; |  | ||||||
|       } |  | ||||||
|       return MP_OKAY; |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    /* step 2.  if a == 1, return 1 */ |  | ||||||
|    if (mp_cmp_d(a, 1) == MP_EQ) { |  | ||||||
|       *c = 1; |  | ||||||
|       return MP_OKAY; |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    /* default */ |  | ||||||
|    s = 0; |  | ||||||
| 
 |  | ||||||
|    /* step 3.  write a = a1 * 2**k  */ |  | ||||||
|    if ((res = mp_init_copy(&a1, a)) != MP_OKAY) { |  | ||||||
|       return res; |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    if ((res = mp_init(&p1)) != MP_OKAY) { |  | ||||||
|       goto LBL_A1; |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    /* divide out larger power of two */ |  | ||||||
|    k = mp_cnt_lsb(&a1); |  | ||||||
|    if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) { |  | ||||||
|       goto LBL_P1; |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    /* step 4.  if e is even set s=1 */ |  | ||||||
|    if ((k & 1) == 0) { |  | ||||||
|       s = 1; |  | ||||||
|    } else { |  | ||||||
|       /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */ |  | ||||||
|       residue = n->dp[0] & 7; |  | ||||||
| 
 |  | ||||||
|       if ((residue == 1) || (residue == 7)) { |  | ||||||
|          s = 1; |  | ||||||
|       } else if ((residue == 3) || (residue == 5)) { |  | ||||||
|          s = -1; |  | ||||||
|       } |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    /* step 5.  if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */ |  | ||||||
|    if (((n->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) { |  | ||||||
|       s = -s; |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    /* if a1 == 1 we're done */ |  | ||||||
|    if (mp_cmp_d(&a1, 1) == MP_EQ) { |  | ||||||
|       *c = s; |  | ||||||
|    } else { |  | ||||||
|       /* n1 = n mod a1 */ |  | ||||||
|       if ((res = mp_mod(n, &a1, &p1)) != MP_OKAY) { |  | ||||||
|          goto LBL_P1; |  | ||||||
|       } |  | ||||||
|       if ((res = mp_jacobi(&p1, &a1, &r)) != MP_OKAY) { |  | ||||||
|          goto LBL_P1; |  | ||||||
|       } |  | ||||||
|       *c = s * r; |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    /* done */ |  | ||||||
|    res = MP_OKAY; |  | ||||||
| LBL_P1: |  | ||||||
|    mp_clear(&p1); |  | ||||||
| LBL_A1: |  | ||||||
|    mp_clear(&a1); |  | ||||||
|    return res; |  | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_KARATSUBA_MUL_C) | #ifdef BN_MP_KARATSUBA_MUL_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* c = |a| * |b| using Karatsuba Multiplication using
 | /* c = |a| * |b| using Karatsuba Multiplication using
 | ||||||
| @ -60,7 +57,7 @@ int mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c) | |||||||
| 
 | 
 | ||||||
|    /* init copy all the temps */ |    /* init copy all the temps */ | ||||||
|    if (mp_init_size(&x0, B) != MP_OKAY) |    if (mp_init_size(&x0, B) != MP_OKAY) | ||||||
|       goto ERR; |       goto LBL_ERR; | ||||||
|    if (mp_init_size(&x1, a->used - B) != MP_OKAY) |    if (mp_init_size(&x1, a->used - B) != MP_OKAY) | ||||||
|       goto X0; |       goto X0; | ||||||
|    if (mp_init_size(&y0, B) != MP_OKAY) |    if (mp_init_size(&y0, B) != MP_OKAY) | ||||||
| @ -164,7 +161,7 @@ X1: | |||||||
|    mp_clear(&x1); |    mp_clear(&x1); | ||||||
| X0: | X0: | ||||||
|    mp_clear(&x0); |    mp_clear(&x0); | ||||||
| ERR: | LBL_ERR: | ||||||
|    return err; |    return err; | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_KARATSUBA_SQR_C) | #ifdef BN_MP_KARATSUBA_SQR_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* Karatsuba squaring, computes b = a*a using three
 | /* Karatsuba squaring, computes b = a*a using three
 | ||||||
| @ -37,7 +34,7 @@ int mp_karatsuba_sqr(const mp_int *a, mp_int *b) | |||||||
| 
 | 
 | ||||||
|    /* init copy all the temps */ |    /* init copy all the temps */ | ||||||
|    if (mp_init_size(&x0, B) != MP_OKAY) |    if (mp_init_size(&x0, B) != MP_OKAY) | ||||||
|       goto ERR; |       goto LBL_ERR; | ||||||
|    if (mp_init_size(&x1, a->used - B) != MP_OKAY) |    if (mp_init_size(&x1, a->used - B) != MP_OKAY) | ||||||
|       goto X0; |       goto X0; | ||||||
| 
 | 
 | ||||||
| @ -117,7 +114,7 @@ X1: | |||||||
|    mp_clear(&x1); |    mp_clear(&x1); | ||||||
| X0: | X0: | ||||||
|    mp_clear(&x0); |    mp_clear(&x0); | ||||||
| ERR: | LBL_ERR: | ||||||
|    return err; |    return err; | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_LCM_C) | #ifdef BN_MP_LCM_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* computes least common multiple as |a*b|/(a, b) */ | /* computes least common multiple as |a*b|/(a, b) */ | ||||||
|  | |||||||
							
								
								
									
										13
									
								
								bn_mp_lshd.c
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								bn_mp_lshd.c
									
									
									
									
									
								
							| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_LSHD_C) | #ifdef BN_MP_LSHD_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* shift left a certain amount of digits */ | /* shift left a certain amount of digits */ | ||||||
| @ -24,6 +21,10 @@ int mp_lshd(mp_int *a, int b) | |||||||
|    if (b <= 0) { |    if (b <= 0) { | ||||||
|       return MP_OKAY; |       return MP_OKAY; | ||||||
|    } |    } | ||||||
|  |    /* no need to shift 0 around */ | ||||||
|  |    if (mp_iszero(a) == MP_YES) { | ||||||
|  |       return MP_OKAY; | ||||||
|  |    } | ||||||
| 
 | 
 | ||||||
|    /* grow to fit the new digits */ |    /* grow to fit the new digits */ | ||||||
|    if (a->alloc < (a->used + b)) { |    if (a->alloc < (a->used + b)) { | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_MOD_C) | #ifdef BN_MP_MOD_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */ | /* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_MOD_2D_C) | #ifdef BN_MP_MOD_2D_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* calc a value mod 2**b */ | /* calc a value mod 2**b */ | ||||||
| @ -27,7 +24,7 @@ int mp_mod_2d(const mp_int *a, int b, mp_int *c) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* if the modulus is larger than the value than return */ |    /* if the modulus is larger than the value than return */ | ||||||
|    if (b >= (int)(a->used * DIGIT_BIT)) { |    if (b >= (a->used * DIGIT_BIT)) { | ||||||
|       res = mp_copy(a, c); |       res = mp_copy(a, c); | ||||||
|       return res; |       return res; | ||||||
|    } |    } | ||||||
| @ -43,7 +40,7 @@ int mp_mod_2d(const mp_int *a, int b, mp_int *c) | |||||||
|    } |    } | ||||||
|    /* clear the digit that is not completely outside/inside the modulus */ |    /* clear the digit that is not completely outside/inside the modulus */ | ||||||
|    c->dp[b / DIGIT_BIT] &= |    c->dp[b / DIGIT_BIT] &= | ||||||
|       (mp_digit)((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1)); |       ((mp_digit)1 << (mp_digit)(b % DIGIT_BIT)) - (mp_digit)1; | ||||||
|    mp_clamp(c); |    mp_clamp(c); | ||||||
|    return MP_OKAY; |    return MP_OKAY; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_MOD_D_C) | #ifdef BN_MP_MOD_D_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| int mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c) | int mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c) | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_MONTGOMERY_CALC_NORMALIZATION_C) | #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /*
 | /*
 | ||||||
| @ -33,7 +30,7 @@ int mp_montgomery_calc_normalization(mp_int *a, const mp_int *b) | |||||||
|          return res; |          return res; | ||||||
|       } |       } | ||||||
|    } else { |    } else { | ||||||
|       mp_set(a, 1); |       mp_set(a, 1uL); | ||||||
|       bits = 1; |       bits = 1; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_MONTGOMERY_REDUCE_C) | #ifdef BN_MP_MONTGOMERY_REDUCE_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* computes xR**-1 == x (mod N) via Montgomery Reduction */ | /* computes xR**-1 == x (mod N) via Montgomery Reduction */ | ||||||
| @ -28,9 +25,10 @@ int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) | |||||||
|     * are fixed up in the inner loop. |     * are fixed up in the inner loop. | ||||||
|     */ |     */ | ||||||
|    digs = (n->used * 2) + 1; |    digs = (n->used * 2) + 1; | ||||||
|    if ((digs < MP_WARRAY) && |    if ((digs < (int)MP_WARRAY) && | ||||||
|  |        (x->used <= (int)MP_WARRAY) && | ||||||
|        (n->used < |        (n->used < | ||||||
|         (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { |         (int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) { | ||||||
|       return fast_mp_montgomery_reduce(x, n, rho); |       return fast_mp_montgomery_reduce(x, n, rho); | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
| @ -72,19 +70,19 @@ int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho) | |||||||
|          for (iy = 0; iy < n->used; iy++) { |          for (iy = 0; iy < n->used; iy++) { | ||||||
|             /* compute product and sum */ |             /* compute product and sum */ | ||||||
|             r       = ((mp_word)mu * (mp_word)*tmpn++) + |             r       = ((mp_word)mu * (mp_word)*tmpn++) + | ||||||
|                       (mp_word) u + (mp_word) *tmpx; |                       (mp_word)u + (mp_word)*tmpx; | ||||||
| 
 | 
 | ||||||
|             /* get carry */ |             /* get carry */ | ||||||
|             u       = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); |             u       = (mp_digit)(r >> (mp_word)DIGIT_BIT); | ||||||
| 
 | 
 | ||||||
|             /* fix digit */ |             /* fix digit */ | ||||||
|             *tmpx++ = (mp_digit)(r & ((mp_word) MP_MASK)); |             *tmpx++ = (mp_digit)(r & (mp_word)MP_MASK); | ||||||
|          } |          } | ||||||
|          /* At this point the ix'th digit of x should be zero */ |          /* At this point the ix'th digit of x should be zero */ | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|          /* propagate carries upwards as required*/ |          /* propagate carries upwards as required*/ | ||||||
|          while (u != 0) { |          while (u != 0u) { | ||||||
|             *tmpx   += u; |             *tmpx   += u; | ||||||
|             u        = *tmpx >> DIGIT_BIT; |             u        = *tmpx >> DIGIT_BIT; | ||||||
|             *tmpx++ &= MP_MASK; |             *tmpx++ &= MP_MASK; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_MONTGOMERY_SETUP_C) | #ifdef BN_MP_MONTGOMERY_SETUP_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* setups the montgomery reduction stuff */ | /* setups the montgomery reduction stuff */ | ||||||
| @ -30,24 +27,24 @@ int mp_montgomery_setup(const mp_int *n, mp_digit *rho) | |||||||
|     */ |     */ | ||||||
|    b = n->dp[0]; |    b = n->dp[0]; | ||||||
| 
 | 
 | ||||||
|    if ((b & 1) == 0) { |    if ((b & 1u) == 0u) { | ||||||
|       return MP_VAL; |       return MP_VAL; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */ |    x = (((b + 2u) & 4u) << 1) + b; /* here x*a==1 mod 2**4 */ | ||||||
|    x *= 2 - (b * x);             /* here x*a==1 mod 2**8 */ |    x *= 2u - (b * x);              /* here x*a==1 mod 2**8 */ | ||||||
| #if !defined(MP_8BIT) | #if !defined(MP_8BIT) | ||||||
|    x *= 2 - (b * x);             /* here x*a==1 mod 2**16 */ |    x *= 2u - (b * x);              /* here x*a==1 mod 2**16 */ | ||||||
| #endif | #endif | ||||||
| #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT)) | #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT)) | ||||||
|    x *= 2 - (b * x);             /* here x*a==1 mod 2**32 */ |    x *= 2u - (b * x);              /* here x*a==1 mod 2**32 */ | ||||||
| #endif | #endif | ||||||
| #ifdef MP_64BIT | #ifdef MP_64BIT | ||||||
|    x *= 2 - (b * x);             /* here x*a==1 mod 2**64 */ |    x *= 2u - (b * x);              /* here x*a==1 mod 2**64 */ | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|    /* rho = -1/m mod b */ |    /* rho = -1/m mod b */ | ||||||
|    *rho = (mp_digit)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK; |    *rho = (mp_digit)(((mp_word)1 << (mp_word)DIGIT_BIT) - x) & MP_MASK; | ||||||
| 
 | 
 | ||||||
|    return MP_OKAY; |    return MP_OKAY; | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										18
									
								
								bn_mp_mul.c
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								bn_mp_mul.c
									
									
									
									
									
								
							| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_MUL_C) | #ifdef BN_MP_MUL_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,18 +9,10 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* high level multiplication (handles sign) */ | /* high level multiplication (handles sign) */ | ||||||
| 
 |  | ||||||
| #define BN_MP_TOOM_MUL_C |  | ||||||
| #define BN_MP_KARATSUBA_MUL_C |  | ||||||
| #define BN_FAST_S_MP_MUL_DIGS_C |  | ||||||
| #define BN_S_MP_MUL_DIGS_C |  | ||||||
| int mp_mul(const mp_int *a, const mp_int *b, mp_int *c) | int mp_mul(const mp_int *a, const mp_int *b, mp_int *c) | ||||||
| { | { | ||||||
|    int     res, neg; |    int     res, neg; | ||||||
| @ -48,9 +40,9 @@ int mp_mul(const mp_int *a, const mp_int *b, mp_int *c) | |||||||
|          int     digs = a->used + b->used + 1; |          int     digs = a->used + b->used + 1; | ||||||
| 
 | 
 | ||||||
| #ifdef BN_FAST_S_MP_MUL_DIGS_C | #ifdef BN_FAST_S_MP_MUL_DIGS_C | ||||||
|          if ((digs < MP_WARRAY) && |          if ((digs < (int)MP_WARRAY) && | ||||||
|              (MIN(a->used, b->used) <= |              (MIN(a->used, b->used) <= | ||||||
|               (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) { |               (int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) { | ||||||
|             res = fast_s_mp_mul_digs(a, b, c, digs); |             res = fast_s_mp_mul_digs(a, b, c, digs); | ||||||
|          } else |          } else | ||||||
| #endif | #endif | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_MUL_2_C) | #ifdef BN_MP_MUL_2_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* b = a*2 */ | /* b = a*2 */ | ||||||
| @ -46,10 +43,10 @@ int mp_mul_2(const mp_int *a, mp_int *b) | |||||||
|          /* get what will be the *next* carry bit from the
 |          /* get what will be the *next* carry bit from the
 | ||||||
|           * MSB of the current digit |           * MSB of the current digit | ||||||
|           */ |           */ | ||||||
|          rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1)); |          rr = *tmpa >> (mp_digit)(DIGIT_BIT - 1); | ||||||
| 
 | 
 | ||||||
|          /* now shift up this digit, add in the carry [from the previous] */ |          /* now shift up this digit, add in the carry [from the previous] */ | ||||||
|          *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK; |          *tmpb++ = ((*tmpa++ << 1uL) | r) & MP_MASK; | ||||||
| 
 | 
 | ||||||
|          /* copy the carry that would be from the source
 |          /* copy the carry that would be from the source
 | ||||||
|           * digit into the next iteration |           * digit into the next iteration | ||||||
| @ -58,7 +55,7 @@ int mp_mul_2(const mp_int *a, mp_int *b) | |||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* new leading digit? */ |       /* new leading digit? */ | ||||||
|       if (r != 0) { |       if (r != 0u) { | ||||||
|          /* add a MSB which is always 1 at this point */ |          /* add a MSB which is always 1 at this point */ | ||||||
|          *tmpb = 1; |          *tmpb = 1; | ||||||
|          ++(b->used); |          ++(b->used); | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_MUL_2D_C) | #ifdef BN_MP_MUL_2D_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* shift left by a certain bit count */ | /* shift left by a certain bit count */ | ||||||
| @ -28,14 +25,14 @@ int mp_mul_2d(const mp_int *a, int b, mp_int *c) | |||||||
|       } |       } | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    if (c->alloc < (int)(c->used + (b / DIGIT_BIT) + 1)) { |    if (c->alloc < (c->used + (b / DIGIT_BIT) + 1)) { | ||||||
|       if ((res = mp_grow(c, c->used + (b / DIGIT_BIT) + 1)) != MP_OKAY) { |       if ((res = mp_grow(c, c->used + (b / DIGIT_BIT) + 1)) != MP_OKAY) { | ||||||
|          return res; |          return res; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* shift by as many digits in the bit count */ |    /* shift by as many digits in the bit count */ | ||||||
|    if (b >= (int)DIGIT_BIT) { |    if (b >= DIGIT_BIT) { | ||||||
|       if ((res = mp_lshd(c, b / DIGIT_BIT)) != MP_OKAY) { |       if ((res = mp_lshd(c, b / DIGIT_BIT)) != MP_OKAY) { | ||||||
|          return res; |          return res; | ||||||
|       } |       } | ||||||
| @ -43,15 +40,15 @@ int mp_mul_2d(const mp_int *a, int b, mp_int *c) | |||||||
| 
 | 
 | ||||||
|    /* shift any bit count < DIGIT_BIT */ |    /* shift any bit count < DIGIT_BIT */ | ||||||
|    d = (mp_digit)(b % DIGIT_BIT); |    d = (mp_digit)(b % DIGIT_BIT); | ||||||
|    if (d != 0) { |    if (d != 0u) { | ||||||
|       mp_digit *tmpc, shift, mask, r, rr; |       mp_digit *tmpc, shift, mask, r, rr; | ||||||
|       int x; |       int x; | ||||||
| 
 | 
 | ||||||
|       /* bitmask for carries */ |       /* bitmask for carries */ | ||||||
|       mask = (((mp_digit)1) << d) - 1; |       mask = ((mp_digit)1 << d) - (mp_digit)1; | ||||||
| 
 | 
 | ||||||
|       /* shift for msbs */ |       /* shift for msbs */ | ||||||
|       shift = DIGIT_BIT - d; |       shift = (mp_digit)DIGIT_BIT - d; | ||||||
| 
 | 
 | ||||||
|       /* alias */ |       /* alias */ | ||||||
|       tmpc = c->dp; |       tmpc = c->dp; | ||||||
| @ -71,7 +68,7 @@ int mp_mul_2d(const mp_int *a, int b, mp_int *c) | |||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* set final carry */ |       /* set final carry */ | ||||||
|       if (r != 0) { |       if (r != 0u) { | ||||||
|          c->dp[(c->used)++] = r; |          c->dp[(c->used)++] = r; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_MUL_D_C) | #ifdef BN_MP_MUL_D_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* multiply by a digit */ | /* multiply by a digit */ | ||||||
| @ -50,10 +47,10 @@ int mp_mul_d(const mp_int *a, mp_digit b, mp_int *c) | |||||||
|       r       = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b); |       r       = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b); | ||||||
| 
 | 
 | ||||||
|       /* mask off higher bits to get a single digit */ |       /* mask off higher bits to get a single digit */ | ||||||
|       *tmpc++ = (mp_digit)(r & ((mp_word)MP_MASK)); |       *tmpc++ = (mp_digit)(r & (mp_word)MP_MASK); | ||||||
| 
 | 
 | ||||||
|       /* send carry into next iteration */ |       /* send carry into next iteration */ | ||||||
|       u       = (mp_digit)(r >> ((mp_word)DIGIT_BIT)); |       u       = (mp_digit)(r >> (mp_word)DIGIT_BIT); | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* store final carry [if any] and increment ix offset  */ |    /* store final carry [if any] and increment ix offset  */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_MULMOD_C) | #ifdef BN_MP_MULMOD_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* d = a * b (mod c) */ | /* d = a * b (mod c) */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_N_ROOT_C) | #ifdef BN_MP_N_ROOT_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* wrapper function for mp_n_root_ex()
 | /* wrapper function for mp_n_root_ex()
 | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_N_ROOT_EX_C) | #ifdef BN_MP_N_ROOT_EX_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* find the n'th root of an integer
 | /* find the n'th root of an integer
 | ||||||
| @ -31,7 +28,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) | |||||||
|    int     res; |    int     res; | ||||||
| 
 | 
 | ||||||
|    /* input must be positive if b is even */ |    /* input must be positive if b is even */ | ||||||
|    if (((b & 1) == 0) && (a->sign == MP_NEG)) { |    if (((b & 1u) == 0u) && (a->sign == MP_NEG)) { | ||||||
|       return MP_VAL; |       return MP_VAL; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
| @ -52,7 +49,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) | |||||||
|    a_.sign = MP_ZPOS; |    a_.sign = MP_ZPOS; | ||||||
| 
 | 
 | ||||||
|    /* t2 = 2 */ |    /* t2 = 2 */ | ||||||
|    mp_set(&t2, 2); |    mp_set(&t2, 2uL); | ||||||
| 
 | 
 | ||||||
|    do { |    do { | ||||||
|       /* t1 = t2 */ |       /* t1 = t2 */ | ||||||
| @ -63,7 +60,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) | |||||||
|       /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */ |       /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */ | ||||||
| 
 | 
 | ||||||
|       /* t3 = t1**(b-1) */ |       /* t3 = t1**(b-1) */ | ||||||
|       if ((res = mp_expt_d_ex(&t1, b - 1, &t3, fast)) != MP_OKAY) { |       if ((res = mp_expt_d_ex(&t1, b - 1u, &t3, fast)) != MP_OKAY) { | ||||||
|          goto LBL_T3; |          goto LBL_T3; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
| @ -101,7 +98,7 @@ int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast) | |||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       if (mp_cmp(&t2, &a_) == MP_GT) { |       if (mp_cmp(&t2, &a_) == MP_GT) { | ||||||
|          if ((res = mp_sub_d(&t1, 1, &t1)) != MP_OKAY) { |          if ((res = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) { | ||||||
|             goto LBL_T3; |             goto LBL_T3; | ||||||
|          } |          } | ||||||
|       } else { |       } else { | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_NEG_C) | #ifdef BN_MP_NEG_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* b = -a */ | /* b = -a */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_OR_C) | #ifdef BN_MP_OR_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* OR two ints together */ | /* OR two ints together */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_FERMAT_C) | #ifdef BN_MP_PRIME_FERMAT_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* performs one Fermat test.
 | /* performs one Fermat test.
 | ||||||
| @ -32,7 +29,7 @@ int mp_prime_fermat(const mp_int *a, const mp_int *b, int *result) | |||||||
|    *result = MP_NO; |    *result = MP_NO; | ||||||
| 
 | 
 | ||||||
|    /* ensure b > 1 */ |    /* ensure b > 1 */ | ||||||
|    if (mp_cmp_d(b, 1) != MP_GT) { |    if (mp_cmp_d(b, 1uL) != MP_GT) { | ||||||
|       return MP_VAL; |       return MP_VAL; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_IS_DIVISIBLE_C) | #ifdef BN_MP_PRIME_IS_DIVISIBLE_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* determines if an integers is divisible by one
 | /* determines if an integers is divisible by one
 | ||||||
| @ -35,7 +32,7 @@ int mp_prime_is_divisible(const mp_int *a, int *result) | |||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* is the residue zero? */ |       /* is the residue zero? */ | ||||||
|       if (res == 0) { |       if (res == 0u) { | ||||||
|          *result = MP_YES; |          *result = MP_YES; | ||||||
|          return MP_OKAY; |          return MP_OKAY; | ||||||
|       } |       } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_IS_PRIME_C) | #ifdef BN_MP_PRIME_IS_PRIME_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,39 +9,72 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* performs a variable number of rounds of Miller-Rabin
 | /* portable integer log of two with small footprint */ | ||||||
|  * | static unsigned int s_floor_ilog2(int value) | ||||||
|  * Probability of error after t rounds is no more than | { | ||||||
|  |    unsigned int r = 0; | ||||||
|  |    while ((value >>= 1) != 0) { | ||||||
|  |       r++; | ||||||
|  |    } | ||||||
|  |    return r; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
|  * |  | ||||||
|  * Sets result to 1 if probably prime, 0 otherwise |  | ||||||
|  */ |  | ||||||
| int mp_prime_is_prime(const mp_int *a, int t, int *result) | int mp_prime_is_prime(const mp_int *a, int t, int *result) | ||||||
| { | { | ||||||
|    mp_int  b; |    mp_int  b; | ||||||
|    int     ix, err, res; |    int     ix, err, res, p_max = 0, size_a, len; | ||||||
|  |    unsigned int fips_rand, mask; | ||||||
| 
 | 
 | ||||||
|    /* default to no */ |    /* default to no */ | ||||||
|    *result = MP_NO; |    *result = MP_NO; | ||||||
| 
 | 
 | ||||||
|    /* valid value of t? */ |    /* valid value of t? */ | ||||||
|    if ((t <= 0) || (t > PRIME_SIZE)) { |    if (t > PRIME_SIZE) { | ||||||
|       return MP_VAL; |       return MP_VAL; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|  |    /* Some shortcuts */ | ||||||
|  |    /* N > 3 */ | ||||||
|  |    if (a->used == 1) { | ||||||
|  |       if ((a->dp[0] == 0u) || (a->dp[0] == 1u)) { | ||||||
|  |          *result = 0; | ||||||
|  |          return MP_OKAY; | ||||||
|  |       } | ||||||
|  |       if (a->dp[0] == 2u) { | ||||||
|  |          *result = 1; | ||||||
|  |          return MP_OKAY; | ||||||
|  |       } | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|  |    /* N must be odd */ | ||||||
|  |    if (mp_iseven(a) == MP_YES) { | ||||||
|  |       return MP_OKAY; | ||||||
|  |    } | ||||||
|  |    /* N is not a perfect square: floor(sqrt(N))^2 != N */ | ||||||
|  |    if ((err = mp_is_square(a, &res)) != MP_OKAY) { | ||||||
|  |       return err; | ||||||
|  |    } | ||||||
|  |    if (res != 0) { | ||||||
|  |       return MP_OKAY; | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|    /* is the input equal to one of the primes in the table? */ |    /* is the input equal to one of the primes in the table? */ | ||||||
|    for (ix = 0; ix < PRIME_SIZE; ix++) { |    for (ix = 0; ix < PRIME_SIZE; ix++) { | ||||||
|       if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) { |       if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) { | ||||||
|          *result = 1; |          *result = MP_YES; | ||||||
|          return MP_OKAY; |          return MP_OKAY; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
|  | #ifdef MP_8BIT | ||||||
|  |    /* The search in the loop above was exhaustive in this case */ | ||||||
|  |    if ((a->used == 1) && (PRIME_SIZE >= 31)) { | ||||||
|  |       return MP_OKAY; | ||||||
|  |    } | ||||||
|  | #endif | ||||||
| 
 | 
 | ||||||
|    /* first perform trial division */ |    /* first perform trial division */ | ||||||
|    if ((err = mp_prime_is_divisible(a, &res)) != MP_OKAY) { |    if ((err = mp_prime_is_divisible(a, &res)) != MP_OKAY) { | ||||||
| @ -53,22 +86,274 @@ int mp_prime_is_prime(const mp_int *a, int t, int *result) | |||||||
|       return MP_OKAY; |       return MP_OKAY; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* now perform the miller-rabin rounds */ |    /*
 | ||||||
|    if ((err = mp_init(&b)) != MP_OKAY) { |        Run the Miller-Rabin test with base 2 for the BPSW test. | ||||||
|  |     */ | ||||||
|  |    if ((err = mp_init_set(&b, 2uL)) != MP_OKAY) { | ||||||
|       return err; |       return err; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    for (ix = 0; ix < t; ix++) { |    if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { | ||||||
|       /* set the prime */ |       goto LBL_B; | ||||||
|       mp_set(&b, ltm_prime_tab[ix]); |    } | ||||||
|  |    if (res == MP_NO) { | ||||||
|  |       goto LBL_B; | ||||||
|  |    } | ||||||
|  |    /*
 | ||||||
|  |       Rumours have it that Mathematica does a second M-R test with base 3. | ||||||
|  |       Other rumours have it that their strong L-S test is slightly different. | ||||||
|  |       It does not hurt, though, beside a bit of extra runtime. | ||||||
|  |    */ | ||||||
|  |    b.dp[0]++; | ||||||
|  |    if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { | ||||||
|  |       goto LBL_B; | ||||||
|  |    } | ||||||
|  |    if (res == MP_NO) { | ||||||
|  |       goto LBL_B; | ||||||
|  |    } | ||||||
| 
 | 
 | ||||||
|       if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { |    /*
 | ||||||
|  |     * 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 | ||||||
|  |     * bases 2, 3 and t random bases. | ||||||
|  |     */ | ||||||
|  | #ifndef LTM_USE_FIPS_ONLY | ||||||
|  |    if (t >= 0) { | ||||||
|  |       /*
 | ||||||
|  |        * Use a Frobenius-Underwood test instead of the Lucas-Selfridge test for | ||||||
|  |        * MP_8BIT (It is unknown if the Lucas-Selfridge test works with 16-bit | ||||||
|  |        * integers but the necesssary analysis is on the todo-list). | ||||||
|  |        */ | ||||||
|  | #if defined (MP_8BIT) || defined (LTM_USE_FROBENIUS_TEST) | ||||||
|  |       err = mp_prime_frobenius_underwood(a, &res); | ||||||
|  |       if ((err != MP_OKAY) && (err != MP_ITER)) { | ||||||
|  |          goto LBL_B; | ||||||
|  |       } | ||||||
|  |       if (res == MP_NO) { | ||||||
|  |          goto LBL_B; | ||||||
|  |       } | ||||||
|  | #else | ||||||
|  |       if ((err = mp_prime_strong_lucas_selfridge(a, &res)) != MP_OKAY) { | ||||||
|  |          goto LBL_B; | ||||||
|  |       } | ||||||
|  |       if (res == MP_NO) { | ||||||
|  |          goto LBL_B; | ||||||
|  |       } | ||||||
|  | #endif | ||||||
|  |    } | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  |    /* run at least one Miller-Rabin test with a random base */ | ||||||
|  |    if (t == 0) { | ||||||
|  |       t = 1; | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|  |    /*
 | ||||||
|  |       abs(t) extra rounds of M-R to extend the range of primes it can find if t < 0. | ||||||
|  |       Only recommended if the input range is known to be < 3317044064679887385961981 | ||||||
|  | 
 | ||||||
|  |       It uses the bases for a deterministic M-R test if input < 3317044064679887385961981 | ||||||
|  |       The caller has to check the size. | ||||||
|  | 
 | ||||||
|  |       Not for cryptographic use because with known bases strong M-R pseudoprimes can | ||||||
|  |       be constructed. Use at least one M-R test with a random base (t >= 1). | ||||||
|  | 
 | ||||||
|  |       The 1119 bit large number | ||||||
|  | 
 | ||||||
|  |       80383745745363949125707961434194210813883768828755814583748891752229742737653\ | ||||||
|  |       33652186502336163960045457915042023603208766569966760987284043965408232928738\ | ||||||
|  |       79185086916685732826776177102938969773947016708230428687109997439976544144845\ | ||||||
|  |       34115587245063340927902227529622941498423068816854043264575340183297861112989\ | ||||||
|  |       60644845216191652872597534901 | ||||||
|  | 
 | ||||||
|  |       has been constructed by F. Arnault (F. Arnault, "Rabin-Miller primality test: | ||||||
|  |       composite numbers which pass it.",  Mathematics of Computation, 1995, 64. Jg., | ||||||
|  |       Nr. 209, S. 355-361), is a semiprime with the two factors | ||||||
|  | 
 | ||||||
|  |       40095821663949960541830645208454685300518816604113250877450620473800321707011\ | ||||||
|  |       96242716223191597219733582163165085358166969145233813917169287527980445796800\ | ||||||
|  |       452592031836601 | ||||||
|  | 
 | ||||||
|  |       20047910831974980270915322604227342650259408302056625438725310236900160853505\ | ||||||
|  |       98121358111595798609866791081582542679083484572616906958584643763990222898400\ | ||||||
|  |       226296015918301 | ||||||
|  | 
 | ||||||
|  |       and it is a strong pseudoprime to all forty-six prime M-R bases up to 200 | ||||||
|  | 
 | ||||||
|  |       It does not fail the strong Bailley-PSP test as implemented here, it is just | ||||||
|  |       given as an example, if not the reason to use the BPSW-test instead of M-R-tests | ||||||
|  |       with a sequence of primes 2...n. | ||||||
|  | 
 | ||||||
|  |    */ | ||||||
|  |    if (t < 0) { | ||||||
|  |       t = -t; | ||||||
|  |       /*
 | ||||||
|  |           Sorenson, Jonathan; Webster, Jonathan (2015). | ||||||
|  |            "Strong Pseudoprimes to Twelve Prime Bases". | ||||||
|  |        */ | ||||||
|  |       /* 0x437ae92817f9fc85b7e5 = 318665857834031151167461 */ | ||||||
|  |       if ((err =   mp_read_radix(&b, "437ae92817f9fc85b7e5", 16)) != MP_OKAY) { | ||||||
|          goto LBL_B; |          goto LBL_B; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       if (res == MP_NO) { |       if (mp_cmp(a, &b) == MP_LT) { | ||||||
|  |          p_max = 12; | ||||||
|  |       } else { | ||||||
|  |          /* 0x2be6951adc5b22410a5fd = 3317044064679887385961981 */ | ||||||
|  |          if ((err = mp_read_radix(&b, "2be6951adc5b22410a5fd", 16)) != MP_OKAY) { | ||||||
|  |             goto LBL_B; | ||||||
|  |          } | ||||||
|  | 
 | ||||||
|  |          if (mp_cmp(a, &b) == MP_LT) { | ||||||
|  |             p_max = 13; | ||||||
|  |          } else { | ||||||
|  |             err = MP_VAL; | ||||||
|  |             goto LBL_B; | ||||||
|  |          } | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       /* for compatibility with the current API (well, compatible within a sign's width) */ | ||||||
|  |       if (p_max < t) { | ||||||
|  |          p_max = t; | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       if (p_max > PRIME_SIZE) { | ||||||
|  |          err = MP_VAL; | ||||||
|          goto LBL_B; |          goto LBL_B; | ||||||
|       } |       } | ||||||
|  |       /* we did bases 2 and 3  already, skip them */ | ||||||
|  |       for (ix = 2; ix < p_max; ix++) { | ||||||
|  |          mp_set(&b, ltm_prime_tab[ix]); | ||||||
|  |          if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { | ||||||
|  |             goto LBL_B; | ||||||
|  |          } | ||||||
|  |          if (res == MP_NO) { | ||||||
|  |             goto LBL_B; | ||||||
|  |          } | ||||||
|  |       } | ||||||
|  |    } | ||||||
|  |    /*
 | ||||||
|  |        Do "t" M-R tests with random bases between 3 and "a". | ||||||
|  |        See Fips 186.4 p. 126ff | ||||||
|  |    */ | ||||||
|  |    else if (t > 0) { | ||||||
|  |       /*
 | ||||||
|  |        * The mp_digit's have a defined bit-size but the size of the | ||||||
|  |        * array a.dp is a simple 'int' and this library can not assume full | ||||||
|  |        * compliance to the current C-standard (ISO/IEC 9899:2011) because | ||||||
|  |        * it gets used for small embeded processors, too. Some of those MCUs | ||||||
|  |        * have compilers that one cannot call standard compliant by any means. | ||||||
|  |        * Hence the ugly type-fiddling in the following code. | ||||||
|  |        */ | ||||||
|  |       size_a = mp_count_bits(a); | ||||||
|  |       mask = (1u << s_floor_ilog2(size_a)) - 1u; | ||||||
|  |       /*
 | ||||||
|  |          Assuming the General Rieman hypothesis (never thought to write that in a | ||||||
|  |          comment) the upper bound can be lowered to  2*(log a)^2. | ||||||
|  |          E. Bach, "Explicit bounds for primality testing and related problems," | ||||||
|  |          Math. Comp. 55 (1990), 355-380. | ||||||
|  | 
 | ||||||
|  |             size_a = (size_a/10) * 7; | ||||||
|  |             len = 2 * (size_a * size_a); | ||||||
|  | 
 | ||||||
|  |          E.g.: a number of size 2^2048 would be reduced to the upper limit | ||||||
|  | 
 | ||||||
|  |             floor(2048/10)*7 = 1428 | ||||||
|  |             2 * 1428^2       = 4078368 | ||||||
|  | 
 | ||||||
|  |          (would have been ~4030331.9962 with floats and natural log instead) | ||||||
|  |          That number is smaller than 2^28, the default bit-size of mp_digit. | ||||||
|  |       */ | ||||||
|  | 
 | ||||||
|  |       /*
 | ||||||
|  |         How many tests, you might ask? Dana Jacobsen of Math::Prime::Util fame | ||||||
|  |         does exactly 1. In words: one. Look at the end of _GMP_is_prime() in | ||||||
|  |         Math-Prime-Util-GMP-0.50/primality.c if you do not believe it. | ||||||
|  | 
 | ||||||
|  |         The function mp_rand() goes to some length to use a cryptographically | ||||||
|  |         good PRNG. That also means that the chance to always get the same base | ||||||
|  |         in the loop is non-zero, although very low. | ||||||
|  |         If the BPSW test and/or the addtional Frobenious test have been | ||||||
|  |         performed instead of just the Miller-Rabin test with the bases 2 and 3, | ||||||
|  |         a single extra test should suffice, so such a very unlikely event | ||||||
|  |         will not do much harm. | ||||||
|  | 
 | ||||||
|  |         To preemptivly answer the dangling question: no, a witness does not | ||||||
|  |         need to be prime. | ||||||
|  |       */ | ||||||
|  |       for (ix = 0; ix < t; ix++) { | ||||||
|  |          /* mp_rand() guarantees the first digit to be non-zero */ | ||||||
|  |          if ((err = mp_rand(&b, 1)) != MP_OKAY) { | ||||||
|  |             goto LBL_B; | ||||||
|  |          } | ||||||
|  |          /*
 | ||||||
|  |           * Reduce digit before casting because mp_digit might be bigger than | ||||||
|  |           * an unsigned int and "mask" on the other side is most probably not. | ||||||
|  |           */ | ||||||
|  |          fips_rand = (unsigned int)(b.dp[0] & (mp_digit) mask); | ||||||
|  | #ifdef MP_8BIT | ||||||
|  |          /*
 | ||||||
|  |           * One 8-bit digit is too small, so concatenate two if the size of | ||||||
|  |           * unsigned int allows for it. | ||||||
|  |           */ | ||||||
|  |          if (((sizeof(unsigned int) * CHAR_BIT)/2) >= (sizeof(mp_digit) * CHAR_BIT)) { | ||||||
|  |             if ((err = mp_rand(&b, 1)) != MP_OKAY) { | ||||||
|  |                goto LBL_B; | ||||||
|  |             } | ||||||
|  |             fips_rand <<= sizeof(mp_digit) * CHAR_BIT; | ||||||
|  |             fips_rand |= (unsigned int) b.dp[0]; | ||||||
|  |             fips_rand &= mask; | ||||||
|  |          } | ||||||
|  | #endif | ||||||
|  |          if (fips_rand > (unsigned int)(INT_MAX - DIGIT_BIT)) { | ||||||
|  |             len = INT_MAX / DIGIT_BIT; | ||||||
|  |          } else { | ||||||
|  |             len = (((int)fips_rand + DIGIT_BIT) / DIGIT_BIT); | ||||||
|  |          } | ||||||
|  |          /*  Unlikely. */ | ||||||
|  |          if (len < 0) { | ||||||
|  |             ix--; | ||||||
|  |             continue; | ||||||
|  |          } | ||||||
|  |          /*
 | ||||||
|  |           * As mentioned above, one 8-bit digit is too small and | ||||||
|  |           * although it can only happen in the unlikely case that | ||||||
|  |           * an "unsigned int" is smaller than 16 bit a simple test | ||||||
|  |           * is cheap and the correction even cheaper. | ||||||
|  |           */ | ||||||
|  | #ifdef MP_8BIT | ||||||
|  |          /* All "a" < 2^8 have been caught before */ | ||||||
|  |          if (len == 1) { | ||||||
|  |             len++; | ||||||
|  |          } | ||||||
|  | #endif | ||||||
|  |          if ((err = mp_rand(&b, len)) != MP_OKAY) { | ||||||
|  |             goto LBL_B; | ||||||
|  |          } | ||||||
|  |          /*
 | ||||||
|  |           * That number might got too big and the witness has to be | ||||||
|  |           * smaller than or equal to "a" | ||||||
|  |           */ | ||||||
|  |          len = mp_count_bits(&b); | ||||||
|  |          if (len > size_a) { | ||||||
|  |             len = len - size_a; | ||||||
|  |             if ((err = mp_div_2d(&b, len, &b, NULL)) != MP_OKAY) { | ||||||
|  |                goto LBL_B; | ||||||
|  |             } | ||||||
|  |          } | ||||||
|  | 
 | ||||||
|  |          /* Although the chance for b <= 3 is miniscule, try again. */ | ||||||
|  |          if (mp_cmp_d(&b, 3uL) != MP_GT) { | ||||||
|  |             ix--; | ||||||
|  |             continue; | ||||||
|  |          } | ||||||
|  |          if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { | ||||||
|  |             goto LBL_B; | ||||||
|  |          } | ||||||
|  |          if (res == MP_NO) { | ||||||
|  |             goto LBL_B; | ||||||
|  |          } | ||||||
|  |       } | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* passed the test */ |    /* passed the test */ | ||||||
| @ -77,6 +362,7 @@ LBL_B: | |||||||
|    mp_clear(&b); |    mp_clear(&b); | ||||||
|    return err; |    return err; | ||||||
| } | } | ||||||
|  | 
 | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| /* ref:         $Format:%D$ */ | /* ref:         $Format:%D$ */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_MILLER_RABIN_C) | #ifdef BN_MP_PRIME_MILLER_RABIN_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* Miller-Rabin test of "a" to the base of "b" as described in
 | /* Miller-Rabin test of "a" to the base of "b" as described in
 | ||||||
| @ -31,7 +28,7 @@ int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result) | |||||||
|    *result = MP_NO; |    *result = MP_NO; | ||||||
| 
 | 
 | ||||||
|    /* ensure b > 1 */ |    /* ensure b > 1 */ | ||||||
|    if (mp_cmp_d(b, 1) != MP_GT) { |    if (mp_cmp_d(b, 1uL) != MP_GT) { | ||||||
|       return MP_VAL; |       return MP_VAL; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
| @ -39,7 +36,7 @@ int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result) | |||||||
|    if ((err = mp_init_copy(&n1, a)) != MP_OKAY) { |    if ((err = mp_init_copy(&n1, a)) != MP_OKAY) { | ||||||
|       return err; |       return err; | ||||||
|    } |    } | ||||||
|    if ((err = mp_sub_d(&n1, 1, &n1)) != MP_OKAY) { |    if ((err = mp_sub_d(&n1, 1uL, &n1)) != MP_OKAY) { | ||||||
|       goto LBL_N1; |       goto LBL_N1; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
| @ -67,7 +64,7 @@ int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* if y != 1 and y != n1 do */ |    /* if y != 1 and y != n1 do */ | ||||||
|    if ((mp_cmp_d(&y, 1) != MP_EQ) && (mp_cmp(&y, &n1) != MP_EQ)) { |    if ((mp_cmp_d(&y, 1uL) != MP_EQ) && (mp_cmp(&y, &n1) != MP_EQ)) { | ||||||
|       j = 1; |       j = 1; | ||||||
|       /* while j <= s-1 and y != n1 */ |       /* while j <= s-1 and y != n1 */ | ||||||
|       while ((j <= (s - 1)) && (mp_cmp(&y, &n1) != MP_EQ)) { |       while ((j <= (s - 1)) && (mp_cmp(&y, &n1) != MP_EQ)) { | ||||||
| @ -76,7 +73,7 @@ int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result) | |||||||
|          } |          } | ||||||
| 
 | 
 | ||||||
|          /* if y == 1 then composite */ |          /* if y == 1 then composite */ | ||||||
|          if (mp_cmp_d(&y, 1) == MP_EQ) { |          if (mp_cmp_d(&y, 1uL) == MP_EQ) { | ||||||
|             goto LBL_Y; |             goto LBL_Y; | ||||||
|          } |          } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_NEXT_PRIME_C) | #ifdef BN_MP_PRIME_NEXT_PRIME_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* finds the next prime after the number "a" using "t" trials
 | /* finds the next prime after the number "a" using "t" trials
 | ||||||
| @ -26,11 +23,6 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) | |||||||
|    mp_digit res_tab[PRIME_SIZE], step, kstep; |    mp_digit res_tab[PRIME_SIZE], step, kstep; | ||||||
|    mp_int   b; |    mp_int   b; | ||||||
| 
 | 
 | ||||||
|    /* ensure t is valid */ |  | ||||||
|    if ((t <= 0) || (t > PRIME_SIZE)) { |  | ||||||
|       return MP_VAL; |  | ||||||
|    } |  | ||||||
| 
 |  | ||||||
|    /* force positive */ |    /* force positive */ | ||||||
|    a->sign = MP_ZPOS; |    a->sign = MP_ZPOS; | ||||||
| 
 | 
 | ||||||
| @ -46,10 +38,10 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) | |||||||
|                 * however, the prime must be |                 * however, the prime must be | ||||||
|                 * congruent to 3 mod 4 |                 * congruent to 3 mod 4 | ||||||
|                 */ |                 */ | ||||||
|                if ((ltm_prime_tab[x + 1] & 3) != 3) { |                if ((ltm_prime_tab[x + 1] & 3u) != 3u) { | ||||||
|                   /* scan upwards for a prime congruent to 3 mod 4 */ |                   /* scan upwards for a prime congruent to 3 mod 4 */ | ||||||
|                   for (y = x + 1; y < PRIME_SIZE; y++) { |                   for (y = x + 1; y < PRIME_SIZE; y++) { | ||||||
|                      if ((ltm_prime_tab[y] & 3) == 3) { |                      if ((ltm_prime_tab[y] & 3u) == 3u) { | ||||||
|                         mp_set(a, ltm_prime_tab[y]); |                         mp_set(a, ltm_prime_tab[y]); | ||||||
|                         return MP_OKAY; |                         return MP_OKAY; | ||||||
|                      } |                      } | ||||||
| @ -62,8 +54,8 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) | |||||||
|          } |          } | ||||||
|       } |       } | ||||||
|       /* at this point a maybe 1 */ |       /* at this point a maybe 1 */ | ||||||
|       if (mp_cmp_d(a, 1) == MP_EQ) { |       if (mp_cmp_d(a, 1uL) == MP_EQ) { | ||||||
|          mp_set(a, 2); |          mp_set(a, 2uL); | ||||||
|          return MP_OKAY; |          return MP_OKAY; | ||||||
|       } |       } | ||||||
|       /* fall through to the sieve */ |       /* fall through to the sieve */ | ||||||
| @ -80,15 +72,15 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) | |||||||
| 
 | 
 | ||||||
|    if (bbs_style == 1) { |    if (bbs_style == 1) { | ||||||
|       /* if a mod 4 != 3 subtract the correct value to make it so */ |       /* if a mod 4 != 3 subtract the correct value to make it so */ | ||||||
|       if ((a->dp[0] & 3) != 3) { |       if ((a->dp[0] & 3u) != 3u) { | ||||||
|          if ((err = mp_sub_d(a, (a->dp[0] & 3) + 1, a)) != MP_OKAY) { |          if ((err = mp_sub_d(a, (a->dp[0] & 3u) + 1u, a)) != MP_OKAY) { | ||||||
|             return err; |             return err; | ||||||
|          }; |          }; | ||||||
|       } |       } | ||||||
|    } else { |    } else { | ||||||
|       if (mp_iseven(a) == MP_YES) { |       if (mp_iseven(a) == MP_YES) { | ||||||
|          /* force odd */ |          /* force odd */ | ||||||
|          if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) { |          if ((err = mp_sub_d(a, 1uL, a)) != MP_OKAY) { | ||||||
|             return err; |             return err; | ||||||
|          } |          } | ||||||
|       } |       } | ||||||
| @ -127,11 +119,11 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) | |||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             /* set flag if zero */ |             /* set flag if zero */ | ||||||
|             if (res_tab[x] == 0) { |             if (res_tab[x] == 0u) { | ||||||
|                y = 1; |                y = 1; | ||||||
|             } |             } | ||||||
|          } |          } | ||||||
|       } while ((y == 1) && (step < ((((mp_digit)1) << DIGIT_BIT) - kstep))); |       } while ((y == 1) && (step < (((mp_digit)1 << DIGIT_BIT) - kstep))); | ||||||
| 
 | 
 | ||||||
|       /* add the step */ |       /* add the step */ | ||||||
|       if ((err = mp_add_d(a, step, a)) != MP_OKAY) { |       if ((err = mp_add_d(a, step, a)) != MP_OKAY) { | ||||||
| @ -139,21 +131,13 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) | |||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* if didn't pass sieve and step == MAX then skip test */ |       /* if didn't pass sieve and step == MAX then skip test */ | ||||||
|       if ((y == 1) && (step >= ((((mp_digit)1) << DIGIT_BIT) - kstep))) { |       if ((y == 1) && (step >= (((mp_digit)1 << DIGIT_BIT) - kstep))) { | ||||||
|          continue; |          continue; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* is this prime? */ |       if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { | ||||||
|       for (x = 0; x < t; x++) { |          goto LBL_ERR; | ||||||
|          mp_set(&b, ltm_prime_tab[x]); |  | ||||||
|          if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { |  | ||||||
|             goto LBL_ERR; |  | ||||||
|          } |  | ||||||
|          if (res == MP_NO) { |  | ||||||
|             break; |  | ||||||
|          } |  | ||||||
|       } |       } | ||||||
| 
 |  | ||||||
|       if (res == MP_YES) { |       if (res == MP_YES) { | ||||||
|          break; |          break; | ||||||
|       } |       } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_RABIN_MILLER_TRIALS_C) | #ifdef BN_MP_PRIME_RABIN_MILLER_TRIALS_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,27 +9,31 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| static const struct { | static const struct { | ||||||
|    int k, t; |    int k, t; | ||||||
| } sizes[] = { | } sizes[] = { | ||||||
|    {   128,    28 }, |    {    80,    -1 }, /* Use deterministic algorithm for size <= 80 bits */ | ||||||
|  |    {    81,    39 }, | ||||||
|  |    {    96,    37 }, | ||||||
|  |    {   128,    32 }, | ||||||
|  |    {   160,    27 }, | ||||||
|  |    {   192,    21 }, | ||||||
|    {   256,    16 }, |    {   256,    16 }, | ||||||
|    {   384,    10 }, |    {   384,    10 }, | ||||||
|    {   512,     7 }, |    {   512,     7 }, | ||||||
|    {   640,     6 }, |    {   640,     6 }, | ||||||
|    {   768,     5 }, |    {   768,     5 }, | ||||||
|    {   896,     4 }, |    {   896,     4 }, | ||||||
|    {  1024,     4 } |    {  1024,     4 }, | ||||||
|  |    {  2048,     2 }, | ||||||
|  |    {  4096,     1 }, | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| /* returns # of RM trials required for a given bit size */ | /* returns # of RM trials required for a given bit size and max. error of 2^(-96)*/ | ||||||
| int mp_prime_rabin_miller_trials(int size) | int mp_prime_rabin_miller_trials(int size) | ||||||
| { | { | ||||||
|    int x; |    int x; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_RANDOM_EX_C) | #ifdef BN_MP_PRIME_RANDOM_EX_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* makes a truly random prime of a given size (bits),
 | /* makes a truly random prime of a given size (bits),
 | ||||||
| @ -49,7 +46,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback | |||||||
|    bsize = (size>>3) + ((size&7)?1:0); |    bsize = (size>>3) + ((size&7)?1:0); | ||||||
| 
 | 
 | ||||||
|    /* we need a buffer of bsize bytes */ |    /* we need a buffer of bsize bytes */ | ||||||
|    tmp = OPT_CAST(unsigned char) XMALLOC(bsize); |    tmp = OPT_CAST(unsigned char) XMALLOC((size_t)bsize); | ||||||
|    if (tmp == NULL) { |    if (tmp == NULL) { | ||||||
|       return MP_MEM; |       return MP_MEM; | ||||||
|    } |    } | ||||||
| @ -86,12 +83,12 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback | |||||||
|       tmp[bsize-1]             |= maskOR_lsb; |       tmp[bsize-1]             |= maskOR_lsb; | ||||||
| 
 | 
 | ||||||
|       /* read it in */ |       /* read it in */ | ||||||
|       if ((err = mp_read_unsigned_bin(a, tmp, bsize)) != MP_OKAY)     { |       if ((err = mp_read_unsigned_bin(a, tmp, bsize)) != MP_OKAY) { | ||||||
|          goto error; |          goto error; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       /* is it prime? */ |       /* is it prime? */ | ||||||
|       if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY)           { |       if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { | ||||||
|          goto error; |          goto error; | ||||||
|       } |       } | ||||||
|       if (res == MP_NO) { |       if (res == MP_NO) { | ||||||
| @ -100,15 +97,15 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback | |||||||
| 
 | 
 | ||||||
|       if ((flags & LTM_PRIME_SAFE) != 0) { |       if ((flags & LTM_PRIME_SAFE) != 0) { | ||||||
|          /* see if (a-1)/2 is prime */ |          /* see if (a-1)/2 is prime */ | ||||||
|          if ((err = mp_sub_d(a, 1, a)) != MP_OKAY)                    { |          if ((err = mp_sub_d(a, 1uL, a)) != MP_OKAY) { | ||||||
|             goto error; |             goto error; | ||||||
|          } |          } | ||||||
|          if ((err = mp_div_2(a, a)) != MP_OKAY)                       { |          if ((err = mp_div_2(a, a)) != MP_OKAY) { | ||||||
|             goto error; |             goto error; | ||||||
|          } |          } | ||||||
| 
 | 
 | ||||||
|          /* is it prime? */ |          /* is it prime? */ | ||||||
|          if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY)        { |          if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { | ||||||
|             goto error; |             goto error; | ||||||
|          } |          } | ||||||
|       } |       } | ||||||
| @ -116,10 +113,10 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback | |||||||
| 
 | 
 | ||||||
|    if ((flags & LTM_PRIME_SAFE) != 0) { |    if ((flags & LTM_PRIME_SAFE) != 0) { | ||||||
|       /* restore a to the original value */ |       /* restore a to the original value */ | ||||||
|       if ((err = mp_mul_2(a, a)) != MP_OKAY)                          { |       if ((err = mp_mul_2(a, a)) != MP_OKAY) { | ||||||
|          goto error; |          goto error; | ||||||
|       } |       } | ||||||
|       if ((err = mp_add_d(a, 1, a)) != MP_OKAY)                       { |       if ((err = mp_add_d(a, 1uL, a)) != MP_OKAY) { | ||||||
|          goto error; |          goto error; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined( BN_MP_RADIX_SIZE_C) | #ifdef BN_MP_RADIX_SIZE_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* returns size of ASCII reprensentation */ | /* returns size of ASCII reprensentation */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_RADIX_SMAP_C) | #ifdef BN_MP_RADIX_SMAP_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,14 +9,25 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* chars used in radix conversions */ | /* chars used in radix conversions */ | ||||||
| const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; | const char *const mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; | ||||||
|  | const uint8_t mp_s_rmap_reverse[] = { | ||||||
|  |    0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, /* ()*+,-./ */ | ||||||
|  |    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 01234567 */ | ||||||
|  |    0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 89:;<=>? */ | ||||||
|  |    0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, /* @ABCDEFG */ | ||||||
|  |    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, /* HIJKLMNO */ | ||||||
|  |    0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, /* PQRSTUVW */ | ||||||
|  |    0x21, 0x22, 0x23, 0xff, 0xff, 0xff, 0xff, 0xff, /* XYZ[\]^_ */ | ||||||
|  |    0xff, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, /* `abcdefg */ | ||||||
|  |    0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, /* hijklmno */ | ||||||
|  |    0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, /* pqrstuvw */ | ||||||
|  |    0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, /* xyz{|}~. */ | ||||||
|  | }; | ||||||
|  | const size_t mp_s_rmap_reverse_sz = sizeof(mp_s_rmap_reverse); | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| /* ref:         $Format:%D$ */ | /* ref:         $Format:%D$ */ | ||||||
|  | |||||||
							
								
								
									
										193
									
								
								bn_mp_rand.c
									
									
									
									
									
								
							
							
						
						
									
										193
									
								
								bn_mp_rand.c
									
									
									
									
									
								
							| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_RAND_C) | #ifdef BN_MP_RAND_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,36 +9,174 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #if MP_GEN_RANDOM_MAX == 0xffffffff | /* First the OS-specific special cases
 | ||||||
| #define MP_GEN_RANDOM_SHIFT  32 |  * - *BSD | ||||||
| #elif MP_GEN_RANDOM_MAX == 32767 |  * - Windows | ||||||
| /* SHRT_MAX */ |  */ | ||||||
| #define MP_GEN_RANDOM_SHIFT  15 | #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) | ||||||
| #elif MP_GEN_RANDOM_MAX == 2147483647 | #define MP_ARC4RANDOM | ||||||
| /* INT_MAX */ | #define MP_GEN_RANDOM_MAX     0xffffffffu | ||||||
| #define MP_GEN_RANDOM_SHIFT  31 | #define MP_GEN_RANDOM_SHIFT   32 | ||||||
| #elif !defined(MP_GEN_RANDOM_SHIFT) |  | ||||||
| #error Thou shalt define their own valid MP_GEN_RANDOM_SHIFT |  | ||||||
| #endif |  | ||||||
| 
 | 
 | ||||||
| /* makes a pseudo-random int of a given size */ | static int s_read_arc4random(mp_digit *p) | ||||||
| static mp_digit s_gen_random(void) |  | ||||||
| { | { | ||||||
|    mp_digit d = 0, msk = 0; |    mp_digit d = 0, msk = 0; | ||||||
|    do { |    do { | ||||||
|       d <<= MP_GEN_RANDOM_SHIFT; |       d <<= MP_GEN_RANDOM_SHIFT; | ||||||
|       d |= ((mp_digit) MP_GEN_RANDOM()); |       d |= ((mp_digit) arc4random()); | ||||||
|       msk <<= MP_GEN_RANDOM_SHIFT; |       msk <<= MP_GEN_RANDOM_SHIFT; | ||||||
|       msk |= (MP_MASK & MP_GEN_RANDOM_MAX); |       msk |= (MP_MASK & MP_GEN_RANDOM_MAX); | ||||||
|    } while ((MP_MASK & msk) != MP_MASK); |    } while ((MP_MASK & msk) != MP_MASK); | ||||||
|    d &= MP_MASK; |    *p = d; | ||||||
|    return d; |    return MP_OKAY; | ||||||
|  | } | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | #if defined(_WIN32) || defined(_WIN32_WCE) | ||||||
|  | #define MP_WIN_CSP | ||||||
|  | 
 | ||||||
|  | #ifndef _WIN32_WINNT | ||||||
|  | #define _WIN32_WINNT 0x0400 | ||||||
|  | #endif | ||||||
|  | #ifdef _WIN32_WCE | ||||||
|  | #define UNDER_CE | ||||||
|  | #define ARM | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | #define WIN32_LEAN_AND_MEAN | ||||||
|  | #include <windows.h> | ||||||
|  | #include <wincrypt.h> | ||||||
|  | 
 | ||||||
|  | static HCRYPTPROV hProv = 0; | ||||||
|  | 
 | ||||||
|  | static void s_cleanup_win_csp(void) | ||||||
|  | { | ||||||
|  |    CryptReleaseContext(hProv, 0); | ||||||
|  |    hProv = 0; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | static int s_read_win_csp(mp_digit *p) | ||||||
|  | { | ||||||
|  |    int ret = -1; | ||||||
|  |    if (hProv == 0) { | ||||||
|  |       if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, | ||||||
|  |                                (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) && | ||||||
|  |           !CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, | ||||||
|  |                                CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) { | ||||||
|  |          hProv = 0; | ||||||
|  |          return ret; | ||||||
|  |       } | ||||||
|  |       atexit(s_cleanup_win_csp); | ||||||
|  |    } | ||||||
|  |    if (CryptGenRandom(hProv, sizeof(*p), (void *)p) == TRUE) { | ||||||
|  |       ret = MP_OKAY; | ||||||
|  |    } | ||||||
|  |    return ret; | ||||||
|  | } | ||||||
|  | #endif /* WIN32 */ | ||||||
|  | 
 | ||||||
|  | #if !defined(MP_WIN_CSP) && defined(__linux__) && defined(__GLIBC_PREREQ) | ||||||
|  | #if __GLIBC_PREREQ(2, 25) | ||||||
|  | #define MP_GETRANDOM | ||||||
|  | #include <sys/random.h> | ||||||
|  | #include <errno.h> | ||||||
|  | 
 | ||||||
|  | static int s_read_getrandom(mp_digit *p) | ||||||
|  | { | ||||||
|  |    int ret; | ||||||
|  |    do { | ||||||
|  |       ret = getrandom(p, sizeof(*p), 0); | ||||||
|  |    } while ((ret == -1) && (errno == EINTR)); | ||||||
|  |    if (ret == sizeof(*p)) return MP_OKAY; | ||||||
|  |    return -1; | ||||||
|  | } | ||||||
|  | #endif | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | /* 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) | ||||||
|  | #ifndef MP_DEV_URANDOM | ||||||
|  | #define MP_DEV_URANDOM "/dev/urandom" | ||||||
|  | #endif | ||||||
|  | #include <fcntl.h> | ||||||
|  | #include <errno.h> | ||||||
|  | #include <unistd.h> | ||||||
|  | 
 | ||||||
|  | static int s_read_dev_urandom(mp_digit *p) | ||||||
|  | { | ||||||
|  |    ssize_t r; | ||||||
|  |    int fd; | ||||||
|  |    do { | ||||||
|  |       fd = open(MP_DEV_URANDOM, O_RDONLY); | ||||||
|  |    } while ((fd == -1) && (errno == EINTR)); | ||||||
|  |    if (fd == -1) return -1; | ||||||
|  |    do { | ||||||
|  |       r = read(fd, p, sizeof(*p)); | ||||||
|  |    } while ((r == -1) && (errno == EINTR)); | ||||||
|  |    close(fd); | ||||||
|  |    if (r != sizeof(*p)) return -1; | ||||||
|  |    return MP_OKAY; | ||||||
|  | } | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | #if defined(MP_PRNG_ENABLE_LTM_RNG) | ||||||
|  | unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void)); | ||||||
|  | void (*ltm_rng_callback)(void); | ||||||
|  | 
 | ||||||
|  | static int s_read_ltm_rng(mp_digit *p) | ||||||
|  | { | ||||||
|  |    unsigned long ret; | ||||||
|  |    if (ltm_rng == NULL) return -1; | ||||||
|  |    ret = ltm_rng((void *)p, sizeof(*p), ltm_rng_callback); | ||||||
|  |    if (ret != sizeof(*p)) return -1; | ||||||
|  |    return MP_OKAY; | ||||||
|  | } | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | static int s_rand_digit(mp_digit *p) | ||||||
|  | { | ||||||
|  |    int ret = -1; | ||||||
|  | 
 | ||||||
|  | #if defined(MP_ARC4RANDOM) | ||||||
|  |    ret = s_read_arc4random(p); | ||||||
|  |    if (ret == MP_OKAY) return ret; | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | #if defined(MP_WIN_CSP) | ||||||
|  |    ret = s_read_win_csp(p); | ||||||
|  |    if (ret == MP_OKAY) return ret; | ||||||
|  | #else | ||||||
|  | 
 | ||||||
|  | #if defined(MP_GETRANDOM) | ||||||
|  |    ret = s_read_getrandom(p); | ||||||
|  |    if (ret == MP_OKAY) return ret; | ||||||
|  | #endif | ||||||
|  | #if defined(MP_DEV_URANDOM) | ||||||
|  |    ret = s_read_dev_urandom(p); | ||||||
|  |    if (ret == MP_OKAY) return ret; | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  | #endif /* MP_WIN_CSP */ | ||||||
|  | 
 | ||||||
|  | #if defined(MP_PRNG_ENABLE_LTM_RNG) | ||||||
|  |    ret = s_read_ltm_rng(p); | ||||||
|  |    if (ret == MP_OKAY) return ret; | ||||||
|  | #endif | ||||||
|  | 
 | ||||||
|  |    return ret; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /* makes a pseudo-random int of a given size */ | ||||||
|  | int mp_rand_digit(mp_digit *r) | ||||||
|  | { | ||||||
|  |    int ret = s_rand_digit(r); | ||||||
|  |    *r &= MP_MASK; | ||||||
|  |    return ret; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int mp_rand(mp_int *a, int digits) | int mp_rand(mp_int *a, int digits) | ||||||
| @ -53,8 +191,10 @@ int mp_rand(mp_int *a, int digits) | |||||||
| 
 | 
 | ||||||
|    /* first place a random non-zero digit */ |    /* first place a random non-zero digit */ | ||||||
|    do { |    do { | ||||||
|       d = s_gen_random(); |       if (mp_rand_digit(&d) != MP_OKAY) { | ||||||
|    } while (d == 0); |          return MP_VAL; | ||||||
|  |       } | ||||||
|  |    } while (d == 0u); | ||||||
| 
 | 
 | ||||||
|    if ((res = mp_add_d(a, d, a)) != MP_OKAY) { |    if ((res = mp_add_d(a, d, a)) != MP_OKAY) { | ||||||
|       return res; |       return res; | ||||||
| @ -65,7 +205,10 @@ int mp_rand(mp_int *a, int digits) | |||||||
|          return res; |          return res; | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       if ((res = mp_add_d(a, s_gen_random(), a)) != MP_OKAY) { |       if (mp_rand_digit(&d) != MP_OKAY) { | ||||||
|  |          return MP_VAL; | ||||||
|  |       } | ||||||
|  |       if ((res = mp_add_d(a, d, a)) != MP_OKAY) { | ||||||
|          return res; |          return res; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_READ_RADIX_C) | #ifdef BN_MP_READ_RADIX_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,16 +9,14 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* read a string [ASCII] in a given radix */ | /* read a string [ASCII] in a given radix */ | ||||||
| int mp_read_radix(mp_int *a, const char *str, int radix) | int mp_read_radix(mp_int *a, const char *str, int radix) | ||||||
| { | { | ||||||
|    int     y, res, neg; |    int     y, res, neg; | ||||||
|  |    unsigned pos; | ||||||
|    char    ch; |    char    ch; | ||||||
| 
 | 
 | ||||||
|    /* zero the digit bignum */ |    /* zero the digit bignum */ | ||||||
| @ -49,31 +47,30 @@ int mp_read_radix(mp_int *a, const char *str, int radix) | |||||||
|        * [e.g. in hex] |        * [e.g. in hex] | ||||||
|        */ |        */ | ||||||
|       ch = (radix <= 36) ? (char)toupper((int)*str) : *str; |       ch = (radix <= 36) ? (char)toupper((int)*str) : *str; | ||||||
|       for (y = 0; y < 64; y++) { |       pos = (unsigned)(ch - '('); | ||||||
|          if (ch == mp_s_rmap[y]) { |       if (mp_s_rmap_reverse_sz < pos) { | ||||||
|             break; |          break; | ||||||
|          } |  | ||||||
|       } |       } | ||||||
|  |       y = (int)mp_s_rmap_reverse[pos]; | ||||||
| 
 | 
 | ||||||
|       /* if the char was found in the map
 |       /* if the char was found in the map
 | ||||||
|        * and is less than the given radix add it |        * and is less than the given radix add it | ||||||
|        * to the number, otherwise exit the loop. |        * to the number, otherwise exit the loop. | ||||||
|        */ |        */ | ||||||
|       if (y < radix) { |       if ((y == 0xff) || (y >= radix)) { | ||||||
|          if ((res = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) { |  | ||||||
|             return res; |  | ||||||
|          } |  | ||||||
|          if ((res = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) { |  | ||||||
|             return res; |  | ||||||
|          } |  | ||||||
|       } else { |  | ||||||
|          break; |          break; | ||||||
|       } |       } | ||||||
|  |       if ((res = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) { | ||||||
|  |          return res; | ||||||
|  |       } | ||||||
|  |       if ((res = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) { | ||||||
|  |          return res; | ||||||
|  |       } | ||||||
|       ++str; |       ++str; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* if an illegal character was found, fail. */ |    /* if an illegal character was found, fail. */ | ||||||
|    if (!(*str == '\0' || *str == '\r' || *str == '\n')) { |    if (!((*str == '\0') || (*str == '\r') || (*str == '\n'))) { | ||||||
|       mp_zero(a); |       mp_zero(a); | ||||||
|       return MP_VAL; |       return MP_VAL; | ||||||
|    } |    } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_READ_SIGNED_BIN_C) | #ifdef BN_MP_READ_SIGNED_BIN_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* read signed bin, big endian, first byte is 0==positive or 1==negative */ | /* read signed bin, big endian, first byte is 0==positive or 1==negative */ | ||||||
| @ -26,7 +23,7 @@ int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* first byte is 0 for positive, non-zero for negative */ |    /* first byte is 0 for positive, non-zero for negative */ | ||||||
|    if (b[0] == 0) { |    if (b[0] == (unsigned char)0) { | ||||||
|       a->sign = MP_ZPOS; |       a->sign = MP_ZPOS; | ||||||
|    } else { |    } else { | ||||||
|       a->sign = MP_NEG; |       a->sign = MP_NEG; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_READ_UNSIGNED_BIN_C) | #ifdef BN_MP_READ_UNSIGNED_BIN_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* reads a unsigned char array, assumes the msb is stored first [big endian] */ | /* reads a unsigned char array, assumes the msb is stored first [big endian] */ | ||||||
| @ -41,7 +38,7 @@ int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c) | |||||||
|       a->used += 1; |       a->used += 1; | ||||||
| #else | #else | ||||||
|       a->dp[0] = (*b & MP_MASK); |       a->dp[0] = (*b & MP_MASK); | ||||||
|       a->dp[1] |= ((*b++ >> 7U) & 1); |       a->dp[1] |= ((*b++ >> 7) & 1u); | ||||||
|       a->used += 2; |       a->used += 2; | ||||||
| #endif | #endif | ||||||
|    } |    } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_C) | #ifdef BN_MP_REDUCE_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* reduces x mod m, assumes 0 < x < m**2, mu is
 | /* reduces x mod m, assumes 0 < x < m**2, mu is
 | ||||||
| @ -33,7 +30,7 @@ int mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu) | |||||||
|    mp_rshd(&q, um - 1); |    mp_rshd(&q, um - 1); | ||||||
| 
 | 
 | ||||||
|    /* according to HAC this optimization is ok */ |    /* according to HAC this optimization is ok */ | ||||||
|    if (((mp_digit) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) { |    if ((mp_digit)um > ((mp_digit)1 << (DIGIT_BIT - 1))) { | ||||||
|       if ((res = mp_mul(&q, mu, &q)) != MP_OKAY) { |       if ((res = mp_mul(&q, mu, &q)) != MP_OKAY) { | ||||||
|          goto CLEANUP; |          goto CLEANUP; | ||||||
|       } |       } | ||||||
| @ -73,8 +70,8 @@ int mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* If x < 0, add b**(k+1) to it */ |    /* If x < 0, add b**(k+1) to it */ | ||||||
|    if (mp_cmp_d(x, 0) == MP_LT) { |    if (mp_cmp_d(x, 0uL) == MP_LT) { | ||||||
|       mp_set(&q, 1); |       mp_set(&q, 1uL); | ||||||
|       if ((res = mp_lshd(&q, um + 1)) != MP_OKAY) |       if ((res = mp_lshd(&q, um + 1)) != MP_OKAY) | ||||||
|          goto CLEANUP; |          goto CLEANUP; | ||||||
|       if ((res = mp_add(x, &q, x)) != MP_OKAY) |       if ((res = mp_add(x, &q, x)) != MP_OKAY) | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_2K_C) | #ifdef BN_MP_REDUCE_2K_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* reduces a modulo n where n is of the form 2**p - d */ | /* reduces a modulo n where n is of the form 2**p - d */ | ||||||
| @ -29,29 +26,29 @@ int mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d) | |||||||
| top: | top: | ||||||
|    /* q = a/2**p, a = a mod 2**p */ |    /* q = a/2**p, a = a mod 2**p */ | ||||||
|    if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { |    if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { | ||||||
|       goto ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    if (d != 1) { |    if (d != 1u) { | ||||||
|       /* q = q * d */ |       /* q = q * d */ | ||||||
|       if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) { |       if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) { | ||||||
|          goto ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* a = a + q */ |    /* a = a + q */ | ||||||
|    if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { |    if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { | ||||||
|       goto ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    if (mp_cmp_mag(a, n) != MP_LT) { |    if (mp_cmp_mag(a, n) != MP_LT) { | ||||||
|       if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { |       if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { | ||||||
|          goto ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       goto top; |       goto top; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
| ERR: | LBL_ERR: | ||||||
|    mp_clear(&q); |    mp_clear(&q); | ||||||
|    return res; |    return res; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_2K_L_C) | #ifdef BN_MP_REDUCE_2K_L_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* reduces a modulo n where n is of the form 2**p - d
 | /* reduces a modulo n where n is of the form 2**p - d
 | ||||||
| @ -32,27 +29,27 @@ int mp_reduce_2k_l(mp_int *a, const mp_int *n, const mp_int *d) | |||||||
| top: | top: | ||||||
|    /* q = a/2**p, a = a mod 2**p */ |    /* q = a/2**p, a = a mod 2**p */ | ||||||
|    if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { |    if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { | ||||||
|       goto ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* q = q * d */ |    /* q = q * d */ | ||||||
|    if ((res = mp_mul(&q, d, &q)) != MP_OKAY) { |    if ((res = mp_mul(&q, d, &q)) != MP_OKAY) { | ||||||
|       goto ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    /* a = a + q */ |    /* a = a + q */ | ||||||
|    if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { |    if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { | ||||||
|       goto ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    if (mp_cmp_mag(a, n) != MP_LT) { |    if (mp_cmp_mag(a, n) != MP_LT) { | ||||||
|       if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { |       if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { | ||||||
|          goto ERR; |          goto LBL_ERR; | ||||||
|       } |       } | ||||||
|       goto top; |       goto top; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
| ERR: | LBL_ERR: | ||||||
|    mp_clear(&q); |    mp_clear(&q); | ||||||
|    return res; |    return res; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_2K_SETUP_C) | #ifdef BN_MP_REDUCE_2K_SETUP_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* determines the setup value */ | /* determines the setup value */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_2K_SETUP_L_C) | #ifdef BN_MP_REDUCE_2K_SETUP_L_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* determines the setup value */ | /* determines the setup value */ | ||||||
| @ -26,14 +23,14 @@ int mp_reduce_2k_setup_l(const mp_int *a, mp_int *d) | |||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) { |    if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) { | ||||||
|       goto ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
|    if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) { |    if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) { | ||||||
|       goto ERR; |       goto LBL_ERR; | ||||||
|    } |    } | ||||||
| 
 | 
 | ||||||
| ERR: | LBL_ERR: | ||||||
|    mp_clear(&tmp); |    mp_clear(&tmp); | ||||||
|    return res; |    return res; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_IS_2K_C) | #ifdef BN_MP_REDUCE_IS_2K_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* determines if mp_reduce_2k can be used */ | /* determines if mp_reduce_2k can be used */ | ||||||
| @ -32,7 +29,7 @@ int mp_reduce_is_2k(const mp_int *a) | |||||||
| 
 | 
 | ||||||
|       /* Test every bit from the second digit up, must be 1 */ |       /* Test every bit from the second digit up, must be 1 */ | ||||||
|       for (ix = DIGIT_BIT; ix < iy; ix++) { |       for (ix = DIGIT_BIT; ix < iy; ix++) { | ||||||
|          if ((a->dp[iw] & iz) == 0) { |          if ((a->dp[iw] & iz) == 0u) { | ||||||
|             return MP_NO; |             return MP_NO; | ||||||
|          } |          } | ||||||
|          iz <<= 1; |          iz <<= 1; | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_IS_2K_L_C) | #ifdef BN_MP_REDUCE_IS_2K_L_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* determines if reduce_2k_l can be used */ | /* determines if reduce_2k_l can be used */ | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| #include <tommath_private.h> | #include "tommath_private.h" | ||||||
| #if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_SETUP_C) | #ifdef BN_MP_REDUCE_SETUP_C | ||||||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | /* LibTomMath, multiple-precision integer library -- Tom St Denis
 | ||||||
|  * |  * | ||||||
|  * LibTomMath is a library that provides multiple-precision |  * LibTomMath is a library that provides multiple-precision | ||||||
| @ -9,10 +9,7 @@ | |||||||
|  * Michael Fromberger but has been written from scratch with |  * Michael Fromberger but has been written from scratch with | ||||||
|  * additional optimizations in place. |  * additional optimizations in place. | ||||||
|  * |  * | ||||||
|  * The library is free for all purposes without any express |  * SPDX-License-Identifier: Unlicense | ||||||
|  * guarantee it works. |  | ||||||
|  * |  | ||||||
|  * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
 |  | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| /* pre-calculate the value required for Barrett reduction
 | /* pre-calculate the value required for Barrett reduction
 | ||||||
|  | |||||||
Some files were not shown because too many files have changed in this diff Show More
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user