Merge remote-tracking branch 'remotes/original/master' into develop

This commit is contained in:
WolverinDEV 2019-07-03 18:27:11 +02:00
commit c92ccd3eec
191 changed files with 14663 additions and 20965 deletions

1
.gitattributes vendored
View File

@ -1,4 +1,5 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/** export-subst

33
.gitignore vendored
View File

@ -6,16 +6,18 @@
*.gcno
*.gcov
*.lib
Debug/
Release/
[Dd]ebug/
[Rr]elease/
/MSVC_*
.libs/
.coveralls.yml
coverage*/
coverage.info
pre_gen/*
# suppress output of build process and *nix/windows test executables
ltmtest
ltmtest.exe
timing
timing.exe
test
test.exe
mtest
@ -31,6 +33,25 @@ mtest.exe
# ignore user specific settings
*.user
*.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"
*.aux
@ -56,7 +77,3 @@ test_*.txt
*.asc
*.tar.xz
*.zip
.idea/
cmake-build-*/
out/

View File

@ -1,18 +1,35 @@
language: c
compiler:
- gcc
script:
- make travis_mtest
- head -n 5 test.log
- tail -n 2 test.log
- ./testme.sh --with-cc=gcc --with-low-mp
install:
- sudo apt-get update -qq
- sudo apt-get install gcc-multilib
matrix:
fast_finish: true
branches:
only:
- master
- develop
- /^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:
irc: "chat.freenode.net#libtom-notifications"

View File

@ -1,166 +1,21 @@
cmake_minimum_required(VERSION 3.6)
project(libtommath)
project(libtommath C)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(SOURCE_FILES
# demo/demo.c
# 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
file(GLOB SOURCE_FILES ./bn*.c)
list(APPEND SOURCE_FILES
tommath.h
tommath_class.h
tommath_private.h
tommath_superclass.h)
add_definitions(-DALL_FUNCTIONS)
tommath_superclass.h
)
add_library(tommathShared SHARED ${SOURCE_FILES})
add_library(tommathStatic STATIC ${SOURCE_FILES})
# Install library
install(TARGETS tommathShared DESTINATION lib/)
install(TARGETS tommathStatic tommathShared DESTINATION lib/)
# Install library headers
file(GLOB HEADERS *.h)
install(FILES ${HEADERS} DESTINATION include/)

45
LICENSE
View File

@ -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]
[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]
For more information, please refer to <http://unlicense.org/>

View File

@ -1,8 +1,16 @@
[![Build Status - master](https://travis-ci.org/libtom/libtommath.png?branch=master)](https://travis-ci.org/libtom/libtommath)
# libtommath
[![Build Status - develop](https://travis-ci.org/libtom/libtommath.png?branch=develop)](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: [![Build Status](https://api.travis-ci.org/libtom/libtommath.png?branch=master)](https://travis-ci.org/libtom/libtommath)
develop: [![Build Status](https://api.travis-ci.org/libtom/libtommath.png?branch=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.
@ -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.
## Testing
Tests are located in `demo/` and can be built in two flavors.
* `make test` creates a test binary that is intended to be run against `mtest`. `mtest` can be built with `make mtest` and test execution is done like `./mtest/mtest | ./test`. `mtest` is creating test vectors using an alternative MPI library and `test` is consuming these vectors to verify correct behavior of ltm
* `make test_standalone` creates a stand-alone test binary that executes several test routines.

View File

@ -1,6 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_ERROR_C)
#include "tommath_private.h"
#ifdef BN_ERROR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -10,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
static const struct {
@ -28,10 +24,10 @@ static const struct {
/* return a char * string for a given code */
const char *mp_error_to_string(int code)
{
int x;
size_t x;
/* 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) {
return msgs[x].msg;
}

View File

@ -1,6 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_FAST_MP_INVMOD_C)
#include "tommath_private.h"
#ifdef BN_FAST_MP_INVMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -10,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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;
}
/* 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 */
if ((res = mp_copy(&x, &u)) != MP_OKAY) {
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) {
goto LBL_ERR;
}
mp_set(&D, 1);
mp_set(&D, 1uL);
top:
/* 4. while u is even do */
@ -123,7 +125,7 @@ top:
/* now a = C, b = D, gcd == g*v */
/* 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;
goto LBL_ERR;
}
@ -135,6 +137,14 @@ top:
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);
c->sign = neg;
res = MP_OKAY;

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_FAST_MP_MONTGOMERY_REDUCE_C)
#include "tommath_private.h"
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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;
mp_word W[MP_WARRAY];
if (x->used > (int)MP_WARRAY) {
return MP_VAL;
}
/* get old used count */
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)
*/
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
*
@ -102,12 +103,12 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
/* inner loop */
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] */
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
@ -127,7 +128,7 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
_W = W + ++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
@ -144,7 +145,7 @@ int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
_W = W + n->used;
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

View File

@ -1,6 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_FAST_S_MP_MUL_DIGS_C)
#include "tommath_private.h"
#ifdef BN_FAST_S_MP_MUL_DIGS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -10,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
for (iz = 0; iz < iy; ++iz) {
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
_W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
}
/* store term */
W[ix] = ((mp_digit)_W) & MP_MASK;
W[ix] = (mp_digit)_W & MP_MASK;
/* make next carry */
_W = _W >> ((mp_word)DIGIT_BIT);
_W = _W >> (mp_word)DIGIT_BIT;
}
/* 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;
tmpc = c->dp;
for (ix = 0; ix < (pa + 1); ix++) {
for (ix = 0; ix < pa; ix++) {
/* now extract the previous digit [below the carry] */
*tmpc++ = W[ix];
}

View File

@ -1,6 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_FAST_S_MP_MUL_HIGH_DIGS_C)
#include "tommath_private.h"
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -10,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
for (iz = 0; iz < iy; iz++) {
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
_W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
}
/* store term */
W[ix] = ((mp_digit)_W) & MP_MASK;
W[ix] = (mp_digit)_W & MP_MASK;
/* make next carry */
_W = _W >> ((mp_word)DIGIT_BIT);
_W = _W >> (mp_word)DIGIT_BIT;
}
/* setup dest */

View File

@ -1,6 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_FAST_S_MP_SQR_C)
#include "tommath_private.h"
#ifdef BN_FAST_S_MP_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -10,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* the jist of squaring...
@ -71,22 +67,22 @@ int fast_s_mp_sqr(const mp_int *a, mp_int *b)
/* execute loop */
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 */
_W = _W + _W + W1;
/* even columns have the square term in them */
if ((ix&1) == 0) {
_W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]);
if (((unsigned)ix & 1u) == 0u) {
_W += (mp_word)a->dp[ix>>1] * (mp_word)a->dp[ix>>1];
}
/* store it */
W[ix] = (mp_digit)(_W & MP_MASK);
W[ix] = _W & MP_MASK;
/* make next carry */
W1 = _W >> ((mp_word)DIGIT_BIT);
W1 = _W >> (mp_word)DIGIT_BIT;
}
/* setup dest */

View File

@ -1,6 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_2EXPT_C)
#include "tommath_private.h"
#ifdef BN_MP_2EXPT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -10,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* computes a = 2**b
@ -37,7 +33,7 @@ int mp_2expt(mp_int *a, int b)
a->used = (b / DIGIT_BIT) + 1;
/* 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;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_ABS_C)
#include "tommath_private.h"
#ifdef BN_MP_ABS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* b = |a|

View File

@ -1,6 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_ADD_C)
#include "tommath_private.h"
#ifdef BN_MP_ADD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -10,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* high level addition (handles signs) */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_ADD_D_C)
#include "tommath_private.h"
#ifdef BN_MP_ADD_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* single digit addition */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_ADDMOD_C)
#include "tommath_private.h"
#ifdef BN_MP_ADDMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* d = a + b (mod c) */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_AND_C)
#include "tommath_private.h"
#ifdef BN_MP_AND_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* AND two ints together */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_CLAMP_C)
#include "tommath_private.h"
#ifdef BN_MP_CLAMP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* trim unused digits
@ -27,7 +24,7 @@ void mp_clamp(mp_int *a)
/* decrease used while the most significant digit is
* zero.
*/
while ((a->used > 0) && (a->dp[a->used - 1] == 0)) {
while ((a->used > 0) && (a->dp[a->used - 1] == 0u)) {
--(a->used);
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_CLEAR_C)
#include "tommath_private.h"
#ifdef BN_MP_CLEAR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* clear one (frees) */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_CLEAR_MULTI_C)
#include "tommath_private.h"
#ifdef BN_MP_CLEAR_MULTI_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,11 +9,9 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
#include <stdarg.h>
void mp_clear_multi(mp_int *mp, ...)

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_CMP_C)
#include "tommath_private.h"
#ifdef BN_MP_CMP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* compare two ints (signed)*/

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_CMP_D_C)
#include "tommath_private.h"
#ifdef BN_MP_CMP_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* compare a digit */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_CMP_MAG_C)
#include "tommath_private.h"
#ifdef BN_MP_CMP_MAG_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* compare maginitude of two ints (unsigned) */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_CNT_LSB_C)
#include "tommath_private.h"
#ifdef BN_MP_CNT_LSB_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
static const int lnz[16] = {
@ -31,17 +28,17 @@ int mp_cnt_lsb(const mp_int *a)
}
/* 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];
x *= DIGIT_BIT;
/* now scan this digit until a 1 is found */
if ((q & 1) == 0) {
if ((q & 1u) == 0u) {
do {
qq = q & 15;
qq = q & 15u;
x += lnz[qq];
q >>= 4;
} while (qq == 0);
} while (qq == 0u);
}
return x;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_COPY_C)
#include "tommath_private.h"
#ifdef BN_MP_COPY_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* copy, b = a */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_COUNT_BITS_C)
#include "tommath_private.h"
#ifdef BN_MP_COUNT_BITS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
q = a->dp[a->used - 1];
while (q > ((mp_digit) 0)) {
while (q > (mp_digit)0) {
++r;
q >>= ((mp_digit) 1);
q >>= (mp_digit)1;
}
return r;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_DIV_C)
#include "tommath_private.h"
#ifdef BN_MP_DIV_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
#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);
if (((res = mp_abs(a, &ta)) != 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] */
norm = mp_count_bits(&y) % DIGIT_BIT;
if (norm < (int)(DIGIT_BIT-1)) {
norm = (DIGIT_BIT-1) - norm;
if (norm < (DIGIT_BIT - 1)) {
norm = (DIGIT_BIT - 1) - norm;
if ((res = mp_mul_2d(&x, norm, &x)) != MP_OKAY) {
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,
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
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 {
mp_word tmp;
tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT);
tmp |= ((mp_word) x.dp[i - 1]);
tmp /= ((mp_word) y.dp[t]);
if (tmp > (mp_word) MP_MASK) {
tmp = (mp_word)x.dp[i] << (mp_word)DIGIT_BIT;
tmp |= (mp_word)x.dp[i - 1];
tmp /= (mp_word)y.dp[t];
if (tmp > (mp_word)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})) >
@ -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;
*/
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 {
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 */
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.used = 2;
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 */
t2.dp[0] = ((i - 2) < 0) ? 0 : x.dp[i - 2];
t2.dp[1] = ((i - 1) < 0) ? 0 : x.dp[i - 1];
t2.dp[0] = ((i - 2) < 0) ? 0u : x.dp[i - 2];
t2.dp[1] = ((i - 1) < 0) ? 0u : x.dp[i - 1];
t2.dp[2] = x.dp[i];
t2.used = 3;
} 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;
}
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;
}
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_DIV_2_C)
#include "tommath_private.h"
#ifdef BN_MP_DIV_2_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* b = a/2 */
@ -42,7 +39,7 @@ int mp_div_2(const mp_int *a, mp_int *b)
r = 0;
for (x = b->used - 1; x >= 0; x--) {
/* get the carry for the next iteration */
rr = *tmpa & 1;
rr = *tmpa & 1u;
/* shift the current digit, add in carry and store */
*tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_DIV_2D_C)
#include "tommath_private.h"
#ifdef BN_MP_DIV_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
if (b >= (int)DIGIT_BIT) {
if (b >= DIGIT_BIT) {
mp_rshd(c, b / DIGIT_BIT);
}
/* shift any bit count < DIGIT_BIT */
D = (mp_digit)(b % DIGIT_BIT);
if (D != 0) {
if (D != 0u) {
mp_digit *tmpc, mask, shift;
/* mask */
mask = (((mp_digit)1) << D) - 1;
mask = ((mp_digit)1 << D) - 1uL;
/* shift for lsb */
shift = DIGIT_BIT - D;
shift = (mp_digit)DIGIT_BIT - D;
/* alias */
tmpc = c->dp + (c->used - 1);

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_DIV_3_C)
#include "tommath_private.h"
#ifdef BN_MP_DIV_3_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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;
/* 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) {
return res;
@ -34,11 +31,11 @@ int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
q.sign = a->sign;
w = 0;
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] */
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 */
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
* the optimization is not exact.
*/
while (w >= 3) {
t += 1;
w -= 3;
while (w >= 3u) {
t += 1u;
w -= 3u;
}
} else {
t = 0;

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_DIV_D_C)
#include "tommath_private.h"
#ifdef BN_MP_DIV_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
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;
/* fast return if no power of two */
if ((b == 0) || ((b & (b-1)) != 0)) {
if ((b == 0u) || ((b & (b-1u)) != 0u)) {
return 0;
}
for (x = 0; x < DIGIT_BIT; x++) {
if (b == (((mp_digit)1)<<x)) {
if (b == ((mp_digit)1<<(mp_digit)x)) {
*p = x;
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;
/* cannot divide by zero */
if (b == 0) {
if (b == 0u) {
return MP_VAL;
}
/* quick outs */
if ((b == 1) || (mp_iszero(a) == MP_YES)) {
if ((b == 1u) || (mp_iszero(a) == MP_YES)) {
if (d != NULL) {
*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 ? */
if (s_is_power_of_two(b, &ix) == 1) {
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) {
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
/* three? */
if (b == 3) {
if (b == 3u) {
return mp_div_3(a, c, d);
}
#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;
w = 0;
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) {
t = (mp_digit)(w / b);
w -= ((mp_word)t) * ((mp_word)b);
w -= (mp_word)t * (mp_word)b;
} else {
t = 0;
}
q.dp[ix] = (mp_digit)t;
q.dp[ix] = t;
}
if (d != NULL) {

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_DR_IS_MODULUS_C)
#include "tommath_private.h"
#ifdef BN_MP_DR_IS_MODULUS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* determines if a number is a valid DR modulus */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_DR_REDUCE_C)
#include "tommath_private.h"
#ifdef BN_MP_DR_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
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);
mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_DR_SETUP_C)
#include "tommath_private.h"
#ifdef BN_MP_DR_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 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

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_EXCH_C)
#include "tommath_private.h"
#ifdef BN_MP_EXCH_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* swap the elements of two integers, for cases where you can't simply swap the

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_EXPORT_C)
#include "tommath_private.h"
#ifdef BN_MP_EXPORT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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.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;
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);
count = (bits / ((size * 8) - nails)) + (((bits % ((size * 8) - nails)) != 0) ? 1 : 0);
bits = (size_t)mp_count_bits(&t);
count = (bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u);
for (i = 0; i < count; ++i) {
for (j = 0; j < size; ++j) {
unsigned char *byte = (unsigned char *)rop +
(((order == -1) ? i : ((count - 1) - i)) * size) +
((endian == -1) ? j : ((size - 1) - j));
(((order == -1) ? i : ((count - 1u) - i)) * size) +
((endian == -1) ? j : ((size - 1u) - j));
if (j >= (size - nail_bytes)) {
*byte = 0;
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);
return result;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_EXPT_D_C)
#include "tommath_private.h"
#ifdef BN_MP_EXPT_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* wrapper function for mp_expt_d_ex() */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_EXPT_D_EX_C)
#include "tommath_private.h"
#ifdef BN_MP_EXPT_D_EX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
mp_set(c, 1);
mp_set(c, 1uL);
if (fast != 0) {
while (b > 0) {
while (b > 0u) {
/* if the bit is set multiply */
if ((b & 1) != 0) {
if ((b & 1u) != 0u) {
if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
mp_clear(&g);
return res;
@ -41,7 +38,7 @@ int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
}
/* square */
if (b > 1) {
if (b > 1u) {
if ((res = mp_sqr(&g, &g)) != MP_OKAY) {
mp_clear(&g);
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;
}
} else {
for (x = 0; x < DIGIT_BIT; x++) {
for (x = 0; x < (unsigned)DIGIT_BIT; x++) {
/* square */
if ((res = mp_sqr(c, c)) != MP_OKAY) {
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 ((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) {
mp_clear(&g);
return res;

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_EXPTMOD_C)
#include "tommath_private.h"
#ifdef BN_MP_EXPTMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
@ -21,15 +18,6 @@
* 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)
*/
#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 dr;

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_EXPTMOD_FAST_C)
#include "tommath_private.h"
#ifdef BN_MP_EXPTMOD_FAST_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
@ -29,14 +26,6 @@
# define TAB_SIZE 256
#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)
{
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
* 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 */
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) */
#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))))) {
redux = fast_mp_montgomery_reduce;
} 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;
#endif
} else {
mp_set(&res, 1);
mp_set(&res, 1uL);
if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
goto LBL_RES;
}
}
/* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
if ((err = mp_copy(&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
if ((err = mp_copy(&M[1], &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) {
goto LBL_RES;
}
for (x = 0; x < (winsize - 1); x++) {
if ((err = mp_sqr(&M[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;
}
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;
}
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_EXTEUCLID_C)
#include "tommath_private.h"
#ifdef BN_MP_EXTEUCLID_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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) */
mp_set(&u1, 1);
if ((err = mp_copy(a, &u3)) != MP_OKAY) {
mp_set(&u1, 1uL);
if ((err = mp_copy(a, &u3)) != MP_OKAY) {
goto LBL_ERR;
}
/* initialize, (v1,v2,v3) = (0,1,b) */
mp_set(&v2, 1);
if ((err = mp_copy(b, &v3)) != MP_OKAY) {
mp_set(&v2, 1uL);
if ((err = mp_copy(b, &v3)) != MP_OKAY) {
goto LBL_ERR;
}
/* loop while v3 != 0 */
while (mp_iszero(&v3) == MP_NO) {
/* 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;
}
/* (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;
}
if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) {
if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) {
if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) {
if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) {
if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) {
if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) {
goto LBL_ERR;
}
/* (u1,u2,u3) = (v1,v2,v3) */
if ((err = mp_copy(&v1, &u1)) != MP_OKAY) {
if ((err = mp_copy(&v1, &u1)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_copy(&v2, &u2)) != MP_OKAY) {
if ((err = mp_copy(&v2, &u2)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_copy(&v3, &u3)) != MP_OKAY) {
if ((err = mp_copy(&v3, &u3)) != MP_OKAY) {
goto LBL_ERR;
}
/* (v1,v2,v3) = (t1,t2,t3) */
if ((err = mp_copy(&t1, &v1)) != MP_OKAY) {
if ((err = mp_copy(&t1, &v1)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_copy(&t2, &v2)) != MP_OKAY) {
if ((err = mp_copy(&t2, &v2)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_copy(&t3, &v3)) != MP_OKAY) {
if ((err = mp_copy(&t3, &v3)) != MP_OKAY) {
goto LBL_ERR;
}
}
/* make sure U3 >= 0 */
if (u3.sign == MP_NEG) {
if ((err = mp_neg(&u1, &u1)) != MP_OKAY) {
if ((err = mp_neg(&u1, &u1)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_neg(&u2, &u2)) != MP_OKAY) {
if ((err = mp_neg(&u2, &u2)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_neg(&u3, &u3)) != MP_OKAY) {
if ((err = mp_neg(&u3, &u3)) != MP_OKAY) {
goto LBL_ERR;
}
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_FREAD_C)
#include "tommath_private.h"
#ifdef BN_MP_FREAD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
#ifndef LTM_NO_FILE
@ -20,13 +17,14 @@
int mp_fread(mp_int *a, int radix, FILE *stream)
{
int err, ch, neg, y;
unsigned pos;
/* clear a */
mp_zero(a);
/* if first digit is - then set negative */
ch = fgetc(stream);
if (ch == '-') {
if (ch == (int)'-') {
neg = MP_NEG;
ch = fgetc(stream);
} else {
@ -34,27 +32,28 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
}
for (;;) {
/* find y in the radix map */
for (y = 0; y < radix; y++) {
if (mp_s_rmap[y] == ch) {
break;
}
pos = (unsigned)(ch - (int)'(');
if (mp_s_rmap_reverse_sz < pos) {
break;
}
if (y == radix) {
y = (int)mp_s_rmap_reverse[pos];
if ((y == 0xff) || (y >= radix)) {
break;
}
/* 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;
}
if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
return err;
}
ch = fgetc(stream);
}
if (mp_cmp_d(a, 0) != MP_EQ) {
if (mp_cmp_d(a, 0uL) != MP_EQ) {
a->sign = neg;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_FWRITE_C)
#include "tommath_private.h"
#ifdef BN_MP_FWRITE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
#ifndef LTM_NO_FILE
@ -25,7 +22,7 @@ int mp_fwrite(const mp_int *a, int radix, FILE *stream)
return err;
}
buf = OPT_CAST(char) XMALLOC(len);
buf = OPT_CAST(char) XMALLOC((size_t)len);
if (buf == NULL) {
return MP_MEM;
}
@ -36,7 +33,7 @@ int mp_fwrite(const mp_int *a, int radix, FILE *stream)
}
for (x = 0; x < len; x++) {
if (fputc(buf[x], stream) == EOF) {
if (fputc((int)buf[x], stream) == EOF) {
XFREE(buf);
return MP_VAL;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_GCD_C)
#include "tommath_private.h"
#ifdef BN_MP_GCD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* Greatest Common Divisor using the binary method */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_GET_INT_C)
#include "tommath_private.h"
#ifdef BN_MP_GET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
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 */
res = DIGIT(a, i);

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_GET_LONG_C)
#include "tommath_private.h"
#ifdef BN_MP_GET_LONG_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
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 */
res = DIGIT(a, i);
@ -39,3 +36,7 @@ unsigned long mp_get_long(const mp_int *a)
return res;
}
#endif
/* ref: $Format:%D$ */
/* git commit: $Format:%H$ */
/* commit time: $Format:%ai$ */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_GET_LONG_LONG_C)
#include "tommath_private.h"
#ifdef BN_MP_GET_LONG_LONG_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
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 */
res = DIGIT(a, i);
@ -39,3 +36,7 @@ unsigned long long mp_get_long_long(const mp_int *a)
return res;
}
#endif
/* ref: $Format:%D$ */
/* git commit: $Format:%H$ */
/* commit time: $Format:%ai$ */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_GROW_C)
#include "tommath_private.h"
#ifdef BN_MP_GROW_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* grow as required */
@ -32,7 +29,7 @@ int mp_grow(mp_int *a, int size)
* in case the operation failed we don't want
* 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) {
/* reallocation failed but "a" is still valid [can be freed] */
return MP_MEM;

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_IMPORT_C)
#include "tommath_private.h"
#ifdef BN_MP_IMPORT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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.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;
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 (j = 0; j < (size - nail_bytes); ++j) {
unsigned char byte = *((unsigned char *)op +
(((order == 1) ? i : ((count - 1) - i)) * size) +
((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes)));
(((order == 1) ? i : ((count - 1u) - i)) * size) +
((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;
}
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;
}
}

View File

@ -1,6 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_INIT_C)
#include "tommath_private.h"
#ifdef BN_MP_INIT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -10,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* init a new mp_int */
@ -22,7 +18,7 @@ int mp_init(mp_int *a)
int i;
/* 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) {
return MP_MEM;
}

View File

@ -1,6 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_INIT_COPY_C)
#include "tommath_private.h"
#ifdef BN_MP_INIT_COPY_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -10,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* creates "a" then copies b into it */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_INIT_MULTI_C)
#include "tommath_private.h"
#ifdef BN_MP_INIT_MULTI_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,11 +9,9 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
#include <stdarg.h>
int mp_init_multi(mp_int *mp, ...)

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_INIT_SET_C)
#include "tommath_private.h"
#ifdef BN_MP_INIT_SET_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* initialize and set a digit */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_INIT_SET_INT_C)
#include "tommath_private.h"
#ifdef BN_MP_INIT_SET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* initialize and set a digit */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_INIT_SIZE_C)
#include "tommath_private.h"
#ifdef BN_MP_INIT_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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);
/* 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) {
return MP_MEM;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_INVMOD_C)
#include "tommath_private.h"
#ifdef BN_MP_INVMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,26 +9,20 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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)
{
/* b cannot be negative */
if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) {
/* b cannot be negative and has to be >1 */
if ((b->sign == MP_NEG) || (mp_cmp_d(b, 1uL) != MP_GT)) {
return MP_VAL;
}
#ifdef BN_FAST_MP_INVMOD_C
/* 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);
}
#endif

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_INVMOD_SLOW_C)
#include "tommath_private.h"
#ifdef BN_MP_INVMOD_SLOW_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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) {
goto LBL_ERR;
}
mp_set(&A, 1);
mp_set(&D, 1);
mp_set(&A, 1uL);
mp_set(&D, 1uL);
top:
/* 4. while u is even do */
@ -143,13 +140,13 @@ top:
/* now a = C, b = D, gcd == g*v */
/* 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;
goto LBL_ERR;
}
/* 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) {
goto LBL_ERR;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_IS_SQUARE_C)
#include "tommath_private.h"
#ifdef BN_MP_IS_SQUARE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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) */
if (rem_128[127 & DIGIT(arg, 0)] == 1) {
if (rem_128[127u & DIGIT(arg, 0)] == (char)1) {
return MP_OKAY;
}
/* 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;
}
if (rem_105[c] == 1) {
if (rem_105[c] == (char)1) {
return MP_OKAY;
}
@ -75,31 +72,31 @@ int mp_is_square(const mp_int *arg, int *ret)
return res;
}
if ((res = mp_mod(arg, &t, &t)) != MP_OKAY) {
goto ERR;
goto LBL_ERR;
}
r = mp_get_int(&t);
/* 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
*/
if (((1L<<(r%11)) & 0x5C4L) != 0L) goto ERR;
if (((1L<<(r%13)) & 0x9E4L) != 0L) goto ERR;
if (((1L<<(r%17)) & 0x5CE8L) != 0L) goto ERR;
if (((1L<<(r%19)) & 0x4F50CL) != 0L) goto ERR;
if (((1L<<(r%23)) & 0x7ACCA0L) != 0L) goto ERR;
if (((1L<<(r%29)) & 0xC2EDD0CL) != 0L) goto ERR;
if (((1L<<(r%31)) & 0x6DE2B848L) != 0L) goto ERR;
if (((1uL<<(r%11uL)) & 0x5C4uL) != 0uL) goto LBL_ERR;
if (((1uL<<(r%13uL)) & 0x9E4uL) != 0uL) goto LBL_ERR;
if (((1uL<<(r%17uL)) & 0x5CE8uL) != 0uL) goto LBL_ERR;
if (((1uL<<(r%19uL)) & 0x4F50CuL) != 0uL) goto LBL_ERR;
if (((1uL<<(r%23uL)) & 0x7ACCA0uL) != 0uL) goto LBL_ERR;
if (((1uL<<(r%29uL)) & 0xC2EDD0CuL) != 0uL) goto LBL_ERR;
if (((1uL<<(r%31uL)) & 0x6DE2B848uL) != 0uL) goto LBL_ERR;
/* Final check - is sqr(sqrt(arg)) == arg ? */
if ((res = mp_sqrt(arg, &t)) != MP_OKAY) {
goto ERR;
goto LBL_ERR;
}
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;
ERR:
LBL_ERR:
mp_clear(&t);
return res;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_JACOBI_C)
#include "tommath_private.h"
#ifdef BN_MP_JACOBI_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,108 +9,25 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* computes the jacobi c = (a | n) (or Legendre if n is prime)
* HAC pp. 73 Algorithm 2.149
* HAC is wrong here, as the special case of (0 | 1) is not
* handled correctly.
* Kept for legacy reasons, please use mp_kronecker() instead
*/
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 (mp_isneg(a) == MP_YES) {
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;
}
/* step 1. handle case of a == 0 */
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;
return mp_kronecker(a, n, c);
}
#endif

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_KARATSUBA_MUL_C)
#include "tommath_private.h"
#ifdef BN_MP_KARATSUBA_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
if (mp_init_size(&x0, B) != MP_OKAY)
goto ERR;
goto LBL_ERR;
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
goto X0;
if (mp_init_size(&y0, B) != MP_OKAY)
@ -164,7 +161,7 @@ X1:
mp_clear(&x1);
X0:
mp_clear(&x0);
ERR:
LBL_ERR:
return err;
}
#endif

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_KARATSUBA_SQR_C)
#include "tommath_private.h"
#ifdef BN_MP_KARATSUBA_SQR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
if (mp_init_size(&x0, B) != MP_OKAY)
goto ERR;
goto LBL_ERR;
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
goto X0;
@ -117,7 +114,7 @@ X1:
mp_clear(&x1);
X0:
mp_clear(&x0);
ERR:
LBL_ERR:
return err;
}
#endif

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_LCM_C)
#include "tommath_private.h"
#ifdef BN_MP_LCM_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* computes least common multiple as |a*b|/(a, b) */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_LSHD_C)
#include "tommath_private.h"
#ifdef BN_MP_LSHD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* shift left a certain amount of digits */
@ -24,6 +21,10 @@ int mp_lshd(mp_int *a, int b)
if (b <= 0) {
return MP_OKAY;
}
/* no need to shift 0 around */
if (mp_iszero(a) == MP_YES) {
return MP_OKAY;
}
/* grow to fit the new digits */
if (a->alloc < (a->used + b)) {

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_MOD_C)
#include "tommath_private.h"
#ifdef BN_MP_MOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_MOD_2D_C)
#include "tommath_private.h"
#ifdef BN_MP_MOD_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 (b >= (int)(a->used * DIGIT_BIT)) {
if (b >= (a->used * DIGIT_BIT)) {
res = mp_copy(a, c);
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 */
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);
return MP_OKAY;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_MOD_D_C)
#include "tommath_private.h"
#ifdef BN_MP_MOD_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
int mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c)

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_MONTGOMERY_CALC_NORMALIZATION_C)
#include "tommath_private.h"
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/*
@ -33,7 +30,7 @@ int mp_montgomery_calc_normalization(mp_int *a, const mp_int *b)
return res;
}
} else {
mp_set(a, 1);
mp_set(a, 1uL);
bits = 1;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_MONTGOMERY_REDUCE_C)
#include "tommath_private.h"
#ifdef BN_MP_MONTGOMERY_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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.
*/
digs = (n->used * 2) + 1;
if ((digs < MP_WARRAY) &&
if ((digs < (int)MP_WARRAY) &&
(x->used <= (int)MP_WARRAY) &&
(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);
}
@ -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++) {
/* compute product and sum */
r = ((mp_word)mu * (mp_word)*tmpn++) +
(mp_word) u + (mp_word) *tmpx;
(mp_word)u + (mp_word)*tmpx;
/* get carry */
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
u = (mp_digit)(r >> (mp_word)DIGIT_BIT);
/* 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 */
/* propagate carries upwards as required*/
while (u != 0) {
while (u != 0u) {
*tmpx += u;
u = *tmpx >> DIGIT_BIT;
*tmpx++ &= MP_MASK;

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_MONTGOMERY_SETUP_C)
#include "tommath_private.h"
#ifdef BN_MP_MONTGOMERY_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* setups the montgomery reduction stuff */
@ -30,24 +27,24 @@ int mp_montgomery_setup(const mp_int *n, mp_digit *rho)
*/
b = n->dp[0];
if ((b & 1) == 0) {
if ((b & 1u) == 0u) {
return MP_VAL;
}
x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
x *= 2 - (b * x); /* here x*a==1 mod 2**8 */
x = (((b + 2u) & 4u) << 1) + b; /* here x*a==1 mod 2**4 */
x *= 2u - (b * x); /* here x*a==1 mod 2**8 */
#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
#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
#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
/* 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;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_MUL_C)
#include "tommath_private.h"
#ifdef BN_MP_MUL_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,18 +9,10 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 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;
#ifdef BN_FAST_S_MP_MUL_DIGS_C
if ((digs < MP_WARRAY) &&
if ((digs < (int)MP_WARRAY) &&
(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);
} else
#endif

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_MUL_2_C)
#include "tommath_private.h"
#ifdef BN_MP_MUL_2_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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
* 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] */
*tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
*tmpb++ = ((*tmpa++ << 1uL) | r) & MP_MASK;
/* copy the carry that would be from the source
* digit into the next iteration
@ -58,7 +55,7 @@ int mp_mul_2(const mp_int *a, mp_int *b)
}
/* new leading digit? */
if (r != 0) {
if (r != 0u) {
/* add a MSB which is always 1 at this point */
*tmpb = 1;
++(b->used);

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_MUL_2D_C)
#include "tommath_private.h"
#ifdef BN_MP_MUL_2D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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) {
return res;
}
}
/* 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) {
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 */
d = (mp_digit)(b % DIGIT_BIT);
if (d != 0) {
if (d != 0u) {
mp_digit *tmpc, shift, mask, r, rr;
int x;
/* bitmask for carries */
mask = (((mp_digit)1) << d) - 1;
mask = ((mp_digit)1 << d) - (mp_digit)1;
/* shift for msbs */
shift = DIGIT_BIT - d;
shift = (mp_digit)DIGIT_BIT - d;
/* alias */
tmpc = c->dp;
@ -71,7 +68,7 @@ int mp_mul_2d(const mp_int *a, int b, mp_int *c)
}
/* set final carry */
if (r != 0) {
if (r != 0u) {
c->dp[(c->used)++] = r;
}
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_MUL_D_C)
#include "tommath_private.h"
#ifdef BN_MP_MUL_D_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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);
/* 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 */
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 */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_MULMOD_C)
#include "tommath_private.h"
#ifdef BN_MP_MULMOD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* d = a * b (mod c) */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_N_ROOT_C)
#include "tommath_private.h"
#ifdef BN_MP_N_ROOT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* wrapper function for mp_n_root_ex()

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_N_ROOT_EX_C)
#include "tommath_private.h"
#ifdef BN_MP_N_ROOT_EX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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;
/* 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;
}
@ -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;
/* t2 = 2 */
mp_set(&t2, 2);
mp_set(&t2, 2uL);
do {
/* 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))) */
/* 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;
}
@ -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 ((res = mp_sub_d(&t1, 1, &t1)) != MP_OKAY) {
if ((res = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) {
goto LBL_T3;
}
} else {

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_NEG_C)
#include "tommath_private.h"
#ifdef BN_MP_NEG_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* b = -a */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_OR_C)
#include "tommath_private.h"
#ifdef BN_MP_OR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* OR two ints together */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_FERMAT_C)
#include "tommath_private.h"
#ifdef BN_MP_PRIME_FERMAT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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;
/* ensure b > 1 */
if (mp_cmp_d(b, 1) != MP_GT) {
if (mp_cmp_d(b, 1uL) != MP_GT) {
return MP_VAL;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_IS_DIVISIBLE_C)
#include "tommath_private.h"
#ifdef BN_MP_PRIME_IS_DIVISIBLE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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? */
if (res == 0) {
if (res == 0u) {
*result = MP_YES;
return MP_OKAY;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_IS_PRIME_C)
#include "tommath_private.h"
#ifdef BN_MP_PRIME_IS_PRIME_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,39 +9,72 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* performs a variable number of rounds of Miller-Rabin
*
* Probability of error after t rounds is no more than
/* portable integer log of two with small footprint */
static unsigned int s_floor_ilog2(int value)
{
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)
{
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 */
*result = MP_NO;
/* valid value of t? */
if ((t <= 0) || (t > PRIME_SIZE)) {
if (t > PRIME_SIZE) {
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? */
for (ix = 0; ix < PRIME_SIZE; ix++) {
if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) {
*result = 1;
*result = MP_YES;
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 */
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;
}
/* 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;
}
for (ix = 0; ix < t; ix++) {
/* set the prime */
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;
}
/*
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;
}
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;
}
/* 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 */
@ -77,6 +362,7 @@ LBL_B:
mp_clear(&b);
return err;
}
#endif
/* ref: $Format:%D$ */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_MILLER_RABIN_C)
#include "tommath_private.h"
#ifdef BN_MP_PRIME_MILLER_RABIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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;
/* ensure b > 1 */
if (mp_cmp_d(b, 1) != MP_GT) {
if (mp_cmp_d(b, 1uL) != MP_GT) {
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) {
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;
}
@ -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 ((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;
/* while j <= s-1 and y != n1 */
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 (mp_cmp_d(&y, 1) == MP_EQ) {
if (mp_cmp_d(&y, 1uL) == MP_EQ) {
goto LBL_Y;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_NEXT_PRIME_C)
#include "tommath_private.h"
#ifdef BN_MP_PRIME_NEXT_PRIME_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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_int b;
/* ensure t is valid */
if ((t <= 0) || (t > PRIME_SIZE)) {
return MP_VAL;
}
/* force positive */
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
* 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 */
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]);
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 */
if (mp_cmp_d(a, 1) == MP_EQ) {
mp_set(a, 2);
if (mp_cmp_d(a, 1uL) == MP_EQ) {
mp_set(a, 2uL);
return MP_OKAY;
}
/* 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 a mod 4 != 3 subtract the correct value to make it so */
if ((a->dp[0] & 3) != 3) {
if ((err = mp_sub_d(a, (a->dp[0] & 3) + 1, a)) != MP_OKAY) {
if ((a->dp[0] & 3u) != 3u) {
if ((err = mp_sub_d(a, (a->dp[0] & 3u) + 1u, a)) != MP_OKAY) {
return err;
};
}
} else {
if (mp_iseven(a) == MP_YES) {
/* force odd */
if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) {
if ((err = mp_sub_d(a, 1uL, a)) != MP_OKAY) {
return err;
}
}
@ -127,11 +119,11 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
}
/* set flag if zero */
if (res_tab[x] == 0) {
if (res_tab[x] == 0u) {
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 */
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 ((y == 1) && (step >= ((((mp_digit)1) << DIGIT_BIT) - kstep))) {
if ((y == 1) && (step >= (((mp_digit)1 << DIGIT_BIT) - kstep))) {
continue;
}
/* is this prime? */
for (x = 0; x < t; x++) {
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 ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
goto LBL_ERR;
}
if (res == MP_YES) {
break;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_RABIN_MILLER_TRIALS_C)
#include "tommath_private.h"
#ifdef BN_MP_PRIME_RABIN_MILLER_TRIALS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,27 +9,31 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
static const struct {
int k, t;
} sizes[] = {
{ 128, 28 },
{ 80, -1 }, /* Use deterministic algorithm for size <= 80 bits */
{ 81, 39 },
{ 96, 37 },
{ 128, 32 },
{ 160, 27 },
{ 192, 21 },
{ 256, 16 },
{ 384, 10 },
{ 512, 7 },
{ 640, 6 },
{ 768, 5 },
{ 896, 4 },
{ 1024, 4 }
{ 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 x;

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_PRIME_RANDOM_EX_C)
#include "tommath_private.h"
#ifdef BN_MP_PRIME_RANDOM_EX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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);
/* 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) {
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;
/* 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;
}
/* 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;
}
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) {
/* 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;
}
if ((err = mp_div_2(a, a)) != MP_OKAY) {
if ((err = mp_div_2(a, a)) != MP_OKAY) {
goto error;
}
/* 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;
}
}
@ -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) {
/* 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;
}
if ((err = mp_add_d(a, 1, a)) != MP_OKAY) {
if ((err = mp_add_d(a, 1uL, a)) != MP_OKAY) {
goto error;
}
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined( BN_MP_RADIX_SIZE_C)
#include "tommath_private.h"
#ifdef BN_MP_RADIX_SIZE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* returns size of ASCII reprensentation */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_RADIX_SMAP_C)
#include "tommath_private.h"
#ifdef BN_MP_RADIX_SMAP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,14 +9,25 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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
/* ref: $Format:%D$ */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_RAND_C)
#include "tommath_private.h"
#ifdef BN_MP_RAND_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,36 +9,174 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
#if MP_GEN_RANDOM_MAX == 0xffffffff
#define MP_GEN_RANDOM_SHIFT 32
#elif MP_GEN_RANDOM_MAX == 32767
/* SHRT_MAX */
#define MP_GEN_RANDOM_SHIFT 15
#elif MP_GEN_RANDOM_MAX == 2147483647
/* INT_MAX */
#define MP_GEN_RANDOM_SHIFT 31
#elif !defined(MP_GEN_RANDOM_SHIFT)
#error Thou shalt define their own valid MP_GEN_RANDOM_SHIFT
#endif
/* First the OS-specific special cases
* - *BSD
* - Windows
*/
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
#define MP_ARC4RANDOM
#define MP_GEN_RANDOM_MAX 0xffffffffu
#define MP_GEN_RANDOM_SHIFT 32
/* makes a pseudo-random int of a given size */
static mp_digit s_gen_random(void)
static int s_read_arc4random(mp_digit *p)
{
mp_digit d = 0, msk = 0;
do {
d <<= MP_GEN_RANDOM_SHIFT;
d |= ((mp_digit) MP_GEN_RANDOM());
d |= ((mp_digit) arc4random());
msk <<= MP_GEN_RANDOM_SHIFT;
msk |= (MP_MASK & MP_GEN_RANDOM_MAX);
} while ((MP_MASK & msk) != MP_MASK);
d &= MP_MASK;
return d;
*p = 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)
@ -53,8 +191,10 @@ int mp_rand(mp_int *a, int digits)
/* first place a random non-zero digit */
do {
d = s_gen_random();
} while (d == 0);
if (mp_rand_digit(&d) != MP_OKAY) {
return MP_VAL;
}
} while (d == 0u);
if ((res = mp_add_d(a, d, a)) != MP_OKAY) {
return res;
@ -65,7 +205,10 @@ int mp_rand(mp_int *a, int digits)
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;
}
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_READ_RADIX_C)
#include "tommath_private.h"
#ifdef BN_MP_READ_RADIX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,16 +9,14 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* read a string [ASCII] in a given radix */
int mp_read_radix(mp_int *a, const char *str, int radix)
{
int y, res, neg;
unsigned pos;
char ch;
/* zero the digit bignum */
@ -49,31 +47,30 @@ int mp_read_radix(mp_int *a, const char *str, int radix)
* [e.g. in hex]
*/
ch = (radix <= 36) ? (char)toupper((int)*str) : *str;
for (y = 0; y < 64; y++) {
if (ch == mp_s_rmap[y]) {
break;
}
pos = (unsigned)(ch - '(');
if (mp_s_rmap_reverse_sz < pos) {
break;
}
y = (int)mp_s_rmap_reverse[pos];
/* if the char was found in the map
* and is less than the given radix add it
* to the number, otherwise exit the loop.
*/
if (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 {
if ((y == 0xff) || (y >= radix)) {
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;
}
/* if an illegal character was found, fail. */
if (!(*str == '\0' || *str == '\r' || *str == '\n')) {
if (!((*str == '\0') || (*str == '\r') || (*str == '\n'))) {
mp_zero(a);
return MP_VAL;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_READ_SIGNED_BIN_C)
#include "tommath_private.h"
#ifdef BN_MP_READ_SIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
if (b[0] == 0) {
if (b[0] == (unsigned char)0) {
a->sign = MP_ZPOS;
} else {
a->sign = MP_NEG;

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_READ_UNSIGNED_BIN_C)
#include "tommath_private.h"
#ifdef BN_MP_READ_UNSIGNED_BIN_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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;
#else
a->dp[0] = (*b & MP_MASK);
a->dp[1] |= ((*b++ >> 7U) & 1);
a->dp[1] |= ((*b++ >> 7) & 1u);
a->used += 2;
#endif
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_C)
#include "tommath_private.h"
#ifdef BN_MP_REDUCE_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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);
/* 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) {
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 (mp_cmp_d(x, 0) == MP_LT) {
mp_set(&q, 1);
if (mp_cmp_d(x, 0uL) == MP_LT) {
mp_set(&q, 1uL);
if ((res = mp_lshd(&q, um + 1)) != MP_OKAY)
goto CLEANUP;
if ((res = mp_add(x, &q, x)) != MP_OKAY)

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_2K_C)
#include "tommath_private.h"
#ifdef BN_MP_REDUCE_2K_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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:
/* q = a/2**p, a = a mod 2**p */
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 */
if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
goto ERR;
goto LBL_ERR;
}
}
/* a = a + q */
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
goto ERR;
goto LBL_ERR;
}
if (mp_cmp_mag(a, n) != MP_LT) {
if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
goto ERR;
goto LBL_ERR;
}
goto top;
}
ERR:
LBL_ERR:
mp_clear(&q);
return res;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_2K_L_C)
#include "tommath_private.h"
#ifdef BN_MP_REDUCE_2K_L_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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:
/* q = a/2**p, a = a mod 2**p */
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
goto ERR;
goto LBL_ERR;
}
/* q = q * d */
if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
goto ERR;
goto LBL_ERR;
}
/* a = a + q */
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
goto ERR;
goto LBL_ERR;
}
if (mp_cmp_mag(a, n) != MP_LT) {
if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
goto ERR;
goto LBL_ERR;
}
goto top;
}
ERR:
LBL_ERR:
mp_clear(&q);
return res;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_2K_SETUP_C)
#include "tommath_private.h"
#ifdef BN_MP_REDUCE_2K_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* determines the setup value */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_2K_SETUP_L_C)
#include "tommath_private.h"
#ifdef BN_MP_REDUCE_2K_SETUP_L_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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) {
goto ERR;
goto LBL_ERR;
}
if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) {
goto ERR;
goto LBL_ERR;
}
ERR:
LBL_ERR:
mp_clear(&tmp);
return res;
}

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_IS_2K_C)
#include "tommath_private.h"
#ifdef BN_MP_REDUCE_IS_2K_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* 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 */
for (ix = DIGIT_BIT; ix < iy; ix++) {
if ((a->dp[iw] & iz) == 0) {
if ((a->dp[iw] & iz) == 0u) {
return MP_NO;
}
iz <<= 1;

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_IS_2K_L_C)
#include "tommath_private.h"
#ifdef BN_MP_REDUCE_IS_2K_L_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* determines if reduce_2k_l can be used */

View File

@ -1,5 +1,5 @@
#include <tommath_private.h>
#if defined(ALL_FUNCTIONS) || defined(BN_MP_REDUCE_SETUP_C)
#include "tommath_private.h"
#ifdef BN_MP_REDUCE_SETUP_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
@ -9,10 +9,7 @@
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
* SPDX-License-Identifier: Unlicense
*/
/* pre-calculate the value required for Barrett reduction

Some files were not shown because too many files have changed in this diff Show More