Merge branch 'release/1.1.0'
This commit is contained in:
commit
08549ad6bc
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -1,4 +1,5 @@
|
|||||||
/.gitattributes export-ignore
|
/.gitattributes export-ignore
|
||||||
/.gitignore export-ignore
|
/.gitignore export-ignore
|
||||||
|
/.travis.yml export-ignore
|
||||||
|
|
||||||
/** export-subst
|
/** export-subst
|
||||||
|
29
.gitignore
vendored
29
.gitignore
vendored
@ -6,16 +6,18 @@
|
|||||||
*.gcno
|
*.gcno
|
||||||
*.gcov
|
*.gcov
|
||||||
*.lib
|
*.lib
|
||||||
Debug/
|
[Dd]ebug/
|
||||||
Release/
|
[Rr]elease/
|
||||||
|
/MSVC_*
|
||||||
.libs/
|
.libs/
|
||||||
.coveralls.yml
|
.coveralls.yml
|
||||||
coverage*/
|
coverage*/
|
||||||
coverage.info
|
coverage.info
|
||||||
|
pre_gen/*
|
||||||
|
|
||||||
# suppress output of build process and *nix/windows test executables
|
# suppress output of build process and *nix/windows test executables
|
||||||
ltmtest
|
timing
|
||||||
ltmtest.exe
|
timing.exe
|
||||||
test
|
test
|
||||||
test.exe
|
test.exe
|
||||||
mtest
|
mtest
|
||||||
@ -31,6 +33,25 @@ mtest.exe
|
|||||||
# ignore user specific settings
|
# ignore user specific settings
|
||||||
*.user
|
*.user
|
||||||
*.suo
|
*.suo
|
||||||
|
*.userosscache
|
||||||
|
*.sln.docstates
|
||||||
|
*.userprefs
|
||||||
|
# cache/options directory
|
||||||
|
.vs/
|
||||||
|
# Backup & report files from converting an old project file to a newer Visual Studio version
|
||||||
|
_UpgradeReport_Files/
|
||||||
|
Backup*/
|
||||||
|
UpgradeLog*.XML
|
||||||
|
UpgradeLog*.htm
|
||||||
|
# Visual Studio 6 build log + workspace options file
|
||||||
|
*.plg
|
||||||
|
*.opt
|
||||||
|
# visual studio profiler
|
||||||
|
*.psess
|
||||||
|
*.vsp
|
||||||
|
*.vspx
|
||||||
|
*.sap
|
||||||
|
|
||||||
|
|
||||||
# ignore stuff generated by "make manual" and "make poster"
|
# ignore stuff generated by "make manual" and "make poster"
|
||||||
*.aux
|
*.aux
|
||||||
|
31
.travis.yml
31
.travis.yml
@ -1,18 +1,35 @@
|
|||||||
language: c
|
language: c
|
||||||
compiler:
|
|
||||||
- gcc
|
|
||||||
|
|
||||||
script:
|
install:
|
||||||
- make travis_mtest
|
- sudo apt-get update -qq
|
||||||
- head -n 5 test.log
|
- sudo apt-get install gcc-multilib
|
||||||
- tail -n 2 test.log
|
|
||||||
- ./testme.sh --with-cc=gcc --with-low-mp
|
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
fast_finish: true
|
||||||
branches:
|
branches:
|
||||||
only:
|
only:
|
||||||
- master
|
- master
|
||||||
- develop
|
- develop
|
||||||
- /^release\/.*$/
|
- /^release\/.*$/
|
||||||
|
|
||||||
|
compiler:
|
||||||
|
- gcc
|
||||||
|
- clang
|
||||||
|
script:
|
||||||
|
- ./testme.sh --with-cc=$CC ${BUILDOPTIONS}
|
||||||
|
env:
|
||||||
|
- |
|
||||||
|
BUILDOPTIONS="--test-vs-mtest=333333"
|
||||||
|
- |
|
||||||
|
BUILDOPTIONS="--test-vs-mtest=333333 --mtest-real-rand"
|
||||||
|
- |
|
||||||
|
BUILDOPTIONS="--with-low-mp"
|
||||||
|
- |
|
||||||
|
BUILDOPTIONS="--with-m64 --with-m32 --with-mx32"
|
||||||
|
|
||||||
|
after_failure:
|
||||||
|
- cat test_*.log
|
||||||
|
- cat gcc_errors_*.log
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
irc: "chat.freenode.net#libtom-notifications"
|
irc: "chat.freenode.net#libtom-notifications"
|
||||||
|
45
LICENSE
45
LICENSE
@ -1,29 +1,26 @@
|
|||||||
LibTomMath is licensed under DUAL licensing terms.
|
The LibTom license
|
||||||
|
|
||||||
Choose and use the license of your needs.
|
This is free and unencumbered software released into the public domain.
|
||||||
|
|
||||||
[LICENSE #1]
|
Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||||
|
distribute this software, either in source code form or as a compiled
|
||||||
|
binary, for any purpose, commercial or non-commercial, and by any
|
||||||
|
means.
|
||||||
|
|
||||||
LibTomMath is public domain. As should all quality software be.
|
In jurisdictions that recognize copyright laws, the author or authors
|
||||||
|
of this software dedicate any and all copyright interest in the
|
||||||
|
software to the public domain. We make this dedication for the benefit
|
||||||
|
of the public at large and to the detriment of our heirs and
|
||||||
|
successors. We intend this dedication to be an overt act of
|
||||||
|
relinquishment in perpetuity of all present and future rights to this
|
||||||
|
software under copyright law.
|
||||||
|
|
||||||
Tom St Denis
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||||
|
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||||
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
[/LICENSE #1]
|
For more information, please refer to <http://unlicense.org/>
|
||||||
|
|
||||||
[LICENSE #2]
|
|
||||||
|
|
||||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
||||||
Version 2, December 2004
|
|
||||||
|
|
||||||
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
|
||||||
|
|
||||||
Everyone is permitted to copy and distribute verbatim or modified
|
|
||||||
copies of this license document, and changing it is allowed as long
|
|
||||||
as the name is changed.
|
|
||||||
|
|
||||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
||||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
|
||||||
|
|
||||||
0. You just DO WHAT THE FUCK YOU WANT TO.
|
|
||||||
|
|
||||||
[/LICENSE #2]
|
|
||||||
|
16
README.md
16
README.md
@ -1,8 +1,16 @@
|
|||||||
[](https://travis-ci.org/libtom/libtommath)
|
# libtommath
|
||||||
|
|
||||||
[](https://travis-ci.org/libtom/libtommath)
|
This is the git repository for [LibTomMath](http://www.libtom.net/LibTomMath/), a free open source portable number theoretic multiple-precision integer (MPI) library written entirely in C.
|
||||||
|
|
||||||
This is the git repository for [LibTomMath](http://www.libtom.org/), a free open source portable number theoretic multiple-precision integer (MPI) library written entirely in C.
|
## Build Status
|
||||||
|
|
||||||
|
master: [](https://travis-ci.org/libtom/libtommath)
|
||||||
|
|
||||||
|
develop: [](https://travis-ci.org/libtom/libtommath)
|
||||||
|
|
||||||
|
API/ABI changes: [check here](https://abi-laboratory.pro/tracker/timeline/libtommath/)
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
The `develop` branch contains the in-development version. Stable releases are tagged.
|
The `develop` branch contains the in-development version. Stable releases are tagged.
|
||||||
|
|
||||||
@ -10,6 +18,8 @@ Documentation is built from the LaTeX file `bn.tex`. There is also limited docum
|
|||||||
|
|
||||||
The project can be build by using `make`. Along with the usual `make`, `make clean` and `make install`, there are several other build targets, see the makefile for details. There are also makefiles for certain specific platforms.
|
The project can be build by using `make`. Along with the usual `make`, `make clean` and `make install`, there are several other build targets, see the makefile for details. There are also makefiles for certain specific platforms.
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
Tests are located in `demo/` and can be built in two flavors.
|
Tests are located in `demo/` and can be built in two flavors.
|
||||||
* `make test` creates a test binary that is intended to be run against `mtest`. `mtest` can be built with `make mtest` and test execution is done like `./mtest/mtest | ./test`. `mtest` is creating test vectors using an alternative MPI library and `test` is consuming these vectors to verify correct behavior of ltm
|
* `make test` creates a test binary that is intended to be run against `mtest`. `mtest` can be built with `make mtest` and test execution is done like `./mtest/mtest | ./test`. `mtest` is creating test vectors using an alternative MPI library and `test` is consuming these vectors to verify correct behavior of ltm
|
||||||
* `make test_standalone` creates a stand-alone test binary that executes several test routines.
|
* `make test_standalone` creates a stand-alone test binary that executes several test routines.
|
||||||
|
27
astylerc
Normal file
27
astylerc
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
# Artistic Style, see http://astyle.sourceforge.net/
|
||||||
|
# full documentation, see: http://astyle.sourceforge.net/astyle.html
|
||||||
|
#
|
||||||
|
# usage:
|
||||||
|
# astyle --options=astylerc *.[ch]
|
||||||
|
|
||||||
|
## Bracket Style Options
|
||||||
|
style=kr
|
||||||
|
|
||||||
|
## Tab Options
|
||||||
|
indent=spaces=3
|
||||||
|
|
||||||
|
## Bracket Modify Options
|
||||||
|
|
||||||
|
## Indentation Options
|
||||||
|
min-conditional-indent=0
|
||||||
|
|
||||||
|
## Padding Options
|
||||||
|
pad-header
|
||||||
|
unpad-paren
|
||||||
|
align-pointer=name
|
||||||
|
|
||||||
|
## Formatting Options
|
||||||
|
break-after-logical
|
||||||
|
max-code-length=120
|
||||||
|
convert-tabs
|
||||||
|
mode=c
|
11
bn_error.c
11
bn_error.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_ERROR_C
|
#ifdef BN_ERROR_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static const struct {
|
static const struct {
|
||||||
@ -27,10 +24,10 @@ static const struct {
|
|||||||
/* return a char * string for a given code */
|
/* return a char * string for a given code */
|
||||||
const char *mp_error_to_string(int code)
|
const char *mp_error_to_string(int code)
|
||||||
{
|
{
|
||||||
int x;
|
size_t x;
|
||||||
|
|
||||||
/* scan the lookup table for the given message */
|
/* scan the lookup table for the given message */
|
||||||
for (x = 0; x < (int)(sizeof(msgs) / sizeof(msgs[0])); x++) {
|
for (x = 0; x < (sizeof(msgs) / sizeof(msgs[0])); x++) {
|
||||||
if (msgs[x].code == code) {
|
if (msgs[x].code == code) {
|
||||||
return msgs[x].msg;
|
return msgs[x].msg;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_FAST_MP_INVMOD_C
|
#ifdef BN_FAST_MP_INVMOD_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* computes the modular inverse via binary extended euclidean algorithm,
|
/* computes the modular inverse via binary extended euclidean algorithm,
|
||||||
@ -21,7 +18,7 @@
|
|||||||
* Based on slow invmod except this is optimized for the case where b is
|
* Based on slow invmod except this is optimized for the case where b is
|
||||||
* odd as per HAC Note 14.64 on pp. 610
|
* odd as per HAC Note 14.64 on pp. 610
|
||||||
*/
|
*/
|
||||||
int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
int fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
mp_int x, y, u, v, B, D;
|
mp_int x, y, u, v, B, D;
|
||||||
int res, neg;
|
int res, neg;
|
||||||
@ -46,6 +43,12 @@ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* if one of x,y is zero return an error! */
|
||||||
|
if ((mp_iszero(&x) == MP_YES) || (mp_iszero(&y) == MP_YES)) {
|
||||||
|
res = MP_VAL;
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
|
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
|
||||||
if ((res = mp_copy(&x, &u)) != MP_OKAY) {
|
if ((res = mp_copy(&x, &u)) != MP_OKAY) {
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
@ -53,7 +56,7 @@ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
if ((res = mp_copy(&y, &v)) != MP_OKAY) {
|
if ((res = mp_copy(&y, &v)) != MP_OKAY) {
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
mp_set (&D, 1);
|
mp_set(&D, 1uL);
|
||||||
|
|
||||||
top:
|
top:
|
||||||
/* 4. while u is even do */
|
/* 4. while u is even do */
|
||||||
@ -122,7 +125,7 @@ top:
|
|||||||
/* now a = C, b = D, gcd == g*v */
|
/* now a = C, b = D, gcd == g*v */
|
||||||
|
|
||||||
/* if v != 1 then there is no inverse */
|
/* if v != 1 then there is no inverse */
|
||||||
if (mp_cmp_d (&v, 1) != MP_EQ) {
|
if (mp_cmp_d(&v, 1uL) != MP_EQ) {
|
||||||
res = MP_VAL;
|
res = MP_VAL;
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
@ -134,11 +137,20 @@ top:
|
|||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* too big */
|
||||||
|
while (mp_cmp_mag(&D, b) != MP_LT) {
|
||||||
|
if ((res = mp_sub(&D, b, &D)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mp_exch(&D, c);
|
mp_exch(&D, c);
|
||||||
c->sign = neg;
|
c->sign = neg;
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
|
|
||||||
LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
|
LBL_ERR:
|
||||||
|
mp_clear_multi(&x, &y, &u, &v, &B, &D, NULL);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
|
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* computes xR**-1 == x (mod N) via Montgomery Reduction
|
/* computes xR**-1 == x (mod N) via Montgomery Reduction
|
||||||
@ -23,11 +20,15 @@
|
|||||||
*
|
*
|
||||||
* Based on Algorithm 14.32 on pp.601 of HAC.
|
* Based on Algorithm 14.32 on pp.601 of HAC.
|
||||||
*/
|
*/
|
||||||
int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
int fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
|
||||||
{
|
{
|
||||||
int ix, res, olduse;
|
int ix, res, olduse;
|
||||||
mp_word W[MP_WARRAY];
|
mp_word W[MP_WARRAY];
|
||||||
|
|
||||||
|
if (x->used > (int)MP_WARRAY) {
|
||||||
|
return MP_VAL;
|
||||||
|
}
|
||||||
|
|
||||||
/* get old used count */
|
/* get old used count */
|
||||||
olduse = x->used;
|
olduse = x->used;
|
||||||
|
|
||||||
@ -73,7 +74,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
|||||||
* that W[ix-1] have the carry cleared (see after the inner loop)
|
* that W[ix-1] have the carry cleared (see after the inner loop)
|
||||||
*/
|
*/
|
||||||
mp_digit mu;
|
mp_digit mu;
|
||||||
mu = (mp_digit) (((W[ix] & MP_MASK) * rho) & MP_MASK);
|
mu = ((W[ix] & MP_MASK) * rho) & MP_MASK;
|
||||||
|
|
||||||
/* a = a + mu * m * b**i
|
/* a = a + mu * m * b**i
|
||||||
*
|
*
|
||||||
@ -102,12 +103,12 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
|||||||
|
|
||||||
/* inner loop */
|
/* inner loop */
|
||||||
for (iy = 0; iy < n->used; iy++) {
|
for (iy = 0; iy < n->used; iy++) {
|
||||||
*_W++ += ((mp_word)mu) * ((mp_word)*tmpn++);
|
*_W++ += (mp_word)mu * (mp_word)*tmpn++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now fix carry for next digit, W[ix+1] */
|
/* now fix carry for next digit, W[ix+1] */
|
||||||
W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT);
|
W[ix + 1] += W[ix] >> (mp_word)DIGIT_BIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now we have to propagate the carries and
|
/* now we have to propagate the carries and
|
||||||
@ -127,7 +128,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
|||||||
_W = W + ++ix;
|
_W = W + ++ix;
|
||||||
|
|
||||||
for (; ix <= ((n->used * 2) + 1); ix++) {
|
for (; ix <= ((n->used * 2) + 1); ix++) {
|
||||||
*_W++ += *_W1++ >> ((mp_word) DIGIT_BIT);
|
*_W++ += *_W1++ >> (mp_word)DIGIT_BIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* copy out, A = A/b**n
|
/* copy out, A = A/b**n
|
||||||
@ -144,7 +145,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
|||||||
_W = W + n->used;
|
_W = W + n->used;
|
||||||
|
|
||||||
for (ix = 0; ix < (n->used + 1); ix++) {
|
for (ix = 0; ix < (n->used + 1); ix++) {
|
||||||
*tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
|
*tmpx++ = *_W++ & (mp_word)MP_MASK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* zero oldused digits, if the input a was larger than
|
/* zero oldused digits, if the input a was larger than
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Fast (comba) multiplier
|
/* Fast (comba) multiplier
|
||||||
@ -31,7 +28,7 @@
|
|||||||
* Based on Algorithm 14.12 on pp.595 of HAC.
|
* Based on Algorithm 14.12 on pp.595 of HAC.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
int fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
|
||||||
{
|
{
|
||||||
int olduse, res, pa, ix, iz;
|
int olduse, res, pa, ix, iz;
|
||||||
mp_digit W[MP_WARRAY];
|
mp_digit W[MP_WARRAY];
|
||||||
@ -69,15 +66,15 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
|||||||
|
|
||||||
/* execute loop */
|
/* execute loop */
|
||||||
for (iz = 0; iz < iy; ++iz) {
|
for (iz = 0; iz < iy; ++iz) {
|
||||||
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
|
_W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* store term */
|
/* store term */
|
||||||
W[ix] = ((mp_digit)_W) & MP_MASK;
|
W[ix] = (mp_digit)_W & MP_MASK;
|
||||||
|
|
||||||
/* make next carry */
|
/* make next carry */
|
||||||
_W = _W >> ((mp_word)DIGIT_BIT);
|
_W = _W >> (mp_word)DIGIT_BIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setup dest */
|
/* setup dest */
|
||||||
@ -87,7 +84,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
|||||||
{
|
{
|
||||||
mp_digit *tmpc;
|
mp_digit *tmpc;
|
||||||
tmpc = c->dp;
|
tmpc = c->dp;
|
||||||
for (ix = 0; ix < (pa + 1); ix++) {
|
for (ix = 0; ix < pa; ix++) {
|
||||||
/* now extract the previous digit [below the carry] */
|
/* now extract the previous digit [below the carry] */
|
||||||
*tmpc++ = W[ix];
|
*tmpc++ = W[ix];
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
|
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* this is a modified version of fast_s_mul_digs that only produces
|
/* this is a modified version of fast_s_mul_digs that only produces
|
||||||
@ -24,7 +21,7 @@
|
|||||||
*
|
*
|
||||||
* Based on Algorithm 14.12 on pp.595 of HAC.
|
* Based on Algorithm 14.12 on pp.595 of HAC.
|
||||||
*/
|
*/
|
||||||
int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
int fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
|
||||||
{
|
{
|
||||||
int olduse, res, pa, ix, iz;
|
int olduse, res, pa, ix, iz;
|
||||||
mp_digit W[MP_WARRAY];
|
mp_digit W[MP_WARRAY];
|
||||||
@ -60,14 +57,14 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
|||||||
|
|
||||||
/* execute loop */
|
/* execute loop */
|
||||||
for (iz = 0; iz < iy; iz++) {
|
for (iz = 0; iz < iy; iz++) {
|
||||||
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
|
_W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* store term */
|
/* store term */
|
||||||
W[ix] = ((mp_digit)_W) & MP_MASK;
|
W[ix] = (mp_digit)_W & MP_MASK;
|
||||||
|
|
||||||
/* make next carry */
|
/* make next carry */
|
||||||
_W = _W >> ((mp_word)DIGIT_BIT);
|
_W = _W >> (mp_word)DIGIT_BIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setup dest */
|
/* setup dest */
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_FAST_S_MP_SQR_C
|
#ifdef BN_FAST_S_MP_SQR_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* the jist of squaring...
|
/* the jist of squaring...
|
||||||
@ -25,7 +22,7 @@
|
|||||||
After that loop you do the squares and add them in.
|
After that loop you do the squares and add them in.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int fast_s_mp_sqr (mp_int * a, mp_int * b)
|
int fast_s_mp_sqr(const mp_int *a, mp_int *b)
|
||||||
{
|
{
|
||||||
int olduse, res, pa, ix, iz;
|
int olduse, res, pa, ix, iz;
|
||||||
mp_digit W[MP_WARRAY], *tmpx;
|
mp_digit W[MP_WARRAY], *tmpx;
|
||||||
@ -70,22 +67,22 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
|
|||||||
|
|
||||||
/* execute loop */
|
/* execute loop */
|
||||||
for (iz = 0; iz < iy; iz++) {
|
for (iz = 0; iz < iy; iz++) {
|
||||||
_W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
|
_W += (mp_word)*tmpx++ * (mp_word)*tmpy--;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* double the inner product and add carry */
|
/* double the inner product and add carry */
|
||||||
_W = _W + _W + W1;
|
_W = _W + _W + W1;
|
||||||
|
|
||||||
/* even columns have the square term in them */
|
/* even columns have the square term in them */
|
||||||
if ((ix&1) == 0) {
|
if (((unsigned)ix & 1u) == 0u) {
|
||||||
_W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]);
|
_W += (mp_word)a->dp[ix>>1] * (mp_word)a->dp[ix>>1];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* store it */
|
/* store it */
|
||||||
W[ix] = (mp_digit)(_W & MP_MASK);
|
W[ix] = _W & MP_MASK;
|
||||||
|
|
||||||
/* make next carry */
|
/* make next carry */
|
||||||
W1 = _W >> ((mp_word)DIGIT_BIT);
|
W1 = _W >> (mp_word)DIGIT_BIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setup dest */
|
/* setup dest */
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_2EXPT_C
|
#ifdef BN_MP_2EXPT_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* computes a = 2**b
|
/* computes a = 2**b
|
||||||
@ -20,8 +17,7 @@
|
|||||||
* Simple algorithm which zeroes the int, grows it then just sets one bit
|
* Simple algorithm which zeroes the int, grows it then just sets one bit
|
||||||
* as required.
|
* as required.
|
||||||
*/
|
*/
|
||||||
int
|
int mp_2expt(mp_int *a, int b)
|
||||||
mp_2expt (mp_int * a, int b)
|
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
@ -37,7 +33,7 @@ mp_2expt (mp_int * a, int b)
|
|||||||
a->used = (b / DIGIT_BIT) + 1;
|
a->used = (b / DIGIT_BIT) + 1;
|
||||||
|
|
||||||
/* put the single bit in its place */
|
/* put the single bit in its place */
|
||||||
a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);
|
a->dp[b / DIGIT_BIT] = (mp_digit)1 << (mp_digit)(b % DIGIT_BIT);
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
10
bn_mp_abs.c
10
bn_mp_abs.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_ABS_C
|
#ifdef BN_MP_ABS_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,18 +9,14 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* b = |a|
|
/* b = |a|
|
||||||
*
|
*
|
||||||
* Simple function copies the input and fixes the sign to positive
|
* Simple function copies the input and fixes the sign to positive
|
||||||
*/
|
*/
|
||||||
int
|
int mp_abs(const mp_int *a, mp_int *b)
|
||||||
mp_abs (mp_int * a, mp_int * b)
|
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_ADD_C
|
#ifdef BN_MP_ADD_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* high level addition (handles signs) */
|
/* high level addition (handles signs) */
|
||||||
int mp_add (mp_int * a, mp_int * b, mp_int * c)
|
int mp_add(const mp_int *a, const mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
int sa, sb, res;
|
int sa, sb, res;
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_ADD_D_C
|
#ifdef BN_MP_ADD_D_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,15 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* single digit addition */
|
/* single digit addition */
|
||||||
int
|
int mp_add_d(const mp_int *a, mp_digit b, mp_int *c)
|
||||||
mp_add_d (mp_int * a, mp_digit b, mp_int * c)
|
|
||||||
{
|
{
|
||||||
int res, ix, oldused;
|
int res, ix, oldused;
|
||||||
mp_digit *tmpa, *tmpc, mu;
|
mp_digit *tmpa, *tmpc, mu;
|
||||||
@ -31,14 +27,15 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
|
|||||||
|
|
||||||
/* if a is negative and |a| >= b, call c = |a| - b */
|
/* if a is negative and |a| >= b, call c = |a| - b */
|
||||||
if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) {
|
if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) {
|
||||||
|
mp_int a_ = *a;
|
||||||
/* temporarily fix sign of a */
|
/* temporarily fix sign of a */
|
||||||
a->sign = MP_ZPOS;
|
a_.sign = MP_ZPOS;
|
||||||
|
|
||||||
/* c = |a| - b */
|
/* c = |a| - b */
|
||||||
res = mp_sub_d(a, b, c);
|
res = mp_sub_d(&a_, b, c);
|
||||||
|
|
||||||
/* fix sign */
|
/* fix sign */
|
||||||
a->sign = c->sign = MP_NEG;
|
c->sign = MP_NEG;
|
||||||
|
|
||||||
/* clamp */
|
/* clamp */
|
||||||
mp_clamp(c);
|
mp_clamp(c);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_ADDMOD_C
|
#ifdef BN_MP_ADDMOD_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,15 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* d = a + b (mod c) */
|
/* d = a + b (mod c) */
|
||||||
int
|
int mp_addmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
|
||||||
mp_addmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
mp_int t;
|
mp_int t;
|
||||||
|
13
bn_mp_and.c
13
bn_mp_and.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_AND_C
|
#ifdef BN_MP_AND_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,18 +9,15 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* AND two ints together */
|
/* AND two ints together */
|
||||||
int
|
int mp_and(const mp_int *a, const mp_int *b, mp_int *c)
|
||||||
mp_and (mp_int * a, mp_int * b, mp_int * c)
|
|
||||||
{
|
{
|
||||||
int res, ix, px;
|
int res, ix, px;
|
||||||
mp_int t, *x;
|
mp_int t;
|
||||||
|
const mp_int *x;
|
||||||
|
|
||||||
if (a->used > b->used) {
|
if (a->used > b->used) {
|
||||||
if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
|
if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_CLAMP_C
|
#ifdef BN_MP_CLAMP_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* trim unused digits
|
/* trim unused digits
|
||||||
@ -22,13 +19,12 @@
|
|||||||
* Typically very fast. Also fixes the sign if there
|
* Typically very fast. Also fixes the sign if there
|
||||||
* are no more leading digits
|
* are no more leading digits
|
||||||
*/
|
*/
|
||||||
void
|
void mp_clamp(mp_int *a)
|
||||||
mp_clamp (mp_int * a)
|
|
||||||
{
|
{
|
||||||
/* decrease used while the most significant digit is
|
/* decrease used while the most significant digit is
|
||||||
* zero.
|
* zero.
|
||||||
*/
|
*/
|
||||||
while ((a->used > 0) && (a->dp[a->used - 1] == 0)) {
|
while ((a->used > 0) && (a->dp[a->used - 1] == 0u)) {
|
||||||
--(a->used);
|
--(a->used);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_CLEAR_C
|
#ifdef BN_MP_CLEAR_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,15 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* clear one (frees) */
|
/* clear one (frees) */
|
||||||
void
|
void mp_clear(mp_int *a)
|
||||||
mp_clear (mp_int * a)
|
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_CLEAR_MULTI_C
|
#ifdef BN_MP_CLEAR_MULTI_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,11 +9,9 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
void mp_clear_multi(mp_int *mp, ...)
|
void mp_clear_multi(mp_int *mp, ...)
|
||||||
|
10
bn_mp_cmp.c
10
bn_mp_cmp.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_CMP_C
|
#ifdef BN_MP_CMP_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,15 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* compare two ints (signed)*/
|
/* compare two ints (signed)*/
|
||||||
int
|
int mp_cmp(const mp_int *a, const mp_int *b)
|
||||||
mp_cmp (mp_int * a, mp_int * b)
|
|
||||||
{
|
{
|
||||||
/* compare based on sign */
|
/* compare based on sign */
|
||||||
if (a->sign != b->sign) {
|
if (a->sign != b->sign) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_CMP_D_C
|
#ifdef BN_MP_CMP_D_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* compare a digit */
|
/* compare a digit */
|
||||||
int mp_cmp_d(mp_int * a, mp_digit b)
|
int mp_cmp_d(const mp_int *a, mp_digit b)
|
||||||
{
|
{
|
||||||
/* compare based on sign */
|
/* compare based on sign */
|
||||||
if (a->sign == MP_NEG) {
|
if (a->sign == MP_NEG) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_CMP_MAG_C
|
#ifdef BN_MP_CMP_MAG_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* compare maginitude of two ints (unsigned) */
|
/* compare maginitude of two ints (unsigned) */
|
||||||
int mp_cmp_mag (mp_int * a, mp_int * b)
|
int mp_cmp_mag(const mp_int *a, const mp_int *b)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
mp_digit *tmpa, *tmpb;
|
mp_digit *tmpa, *tmpb;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_CNT_LSB_C
|
#ifdef BN_MP_CNT_LSB_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static const int lnz[16] = {
|
static const int lnz[16] = {
|
||||||
@ -20,7 +17,7 @@ static const int lnz[16] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* Counts the number of lsbs which are zero before the first zero bit */
|
/* Counts the number of lsbs which are zero before the first zero bit */
|
||||||
int mp_cnt_lsb(mp_int *a)
|
int mp_cnt_lsb(const mp_int *a)
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
mp_digit q, qq;
|
mp_digit q, qq;
|
||||||
@ -31,17 +28,17 @@ int mp_cnt_lsb(mp_int *a)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* scan lower digits until non-zero */
|
/* scan lower digits until non-zero */
|
||||||
for (x = 0; (x < a->used) && (a->dp[x] == 0); x++) {}
|
for (x = 0; (x < a->used) && (a->dp[x] == 0u); x++) {}
|
||||||
q = a->dp[x];
|
q = a->dp[x];
|
||||||
x *= DIGIT_BIT;
|
x *= DIGIT_BIT;
|
||||||
|
|
||||||
/* now scan this digit until a 1 is found */
|
/* now scan this digit until a 1 is found */
|
||||||
if ((q & 1) == 0) {
|
if ((q & 1u) == 0u) {
|
||||||
do {
|
do {
|
||||||
qq = q & 15;
|
qq = q & 15u;
|
||||||
x += lnz[qq];
|
x += lnz[qq];
|
||||||
q >>= 4;
|
q >>= 4;
|
||||||
} while (qq == 0);
|
} while (qq == 0u);
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
25
bn_mp_complement.c
Normal file
25
bn_mp_complement.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#include "tommath_private.h"
|
||||||
|
#ifdef BN_MP_COMPLEMENT_C
|
||||||
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
|
*
|
||||||
|
* LibTomMath is a library that provides multiple-precision
|
||||||
|
* integer arithmetic as well as number theoretic functionality.
|
||||||
|
*
|
||||||
|
* The library was designed directly after the MPI library by
|
||||||
|
* Michael Fromberger but has been written from scratch with
|
||||||
|
* additional optimizations in place.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Unlicense
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* b = ~a */
|
||||||
|
int mp_complement(const mp_int *a, mp_int *b)
|
||||||
|
{
|
||||||
|
int res = mp_neg(a, b);
|
||||||
|
return (res == MP_OKAY) ? mp_sub_d(b, 1uL, b) : res;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ref: $Format:%D$ */
|
||||||
|
/* git commit: $Format:%H$ */
|
||||||
|
/* commit time: $Format:%ai$ */
|
10
bn_mp_copy.c
10
bn_mp_copy.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_COPY_C
|
#ifdef BN_MP_COPY_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,15 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* copy, b = a */
|
/* copy, b = a */
|
||||||
int
|
int mp_copy(const mp_int *a, mp_int *b)
|
||||||
mp_copy (mp_int * a, mp_int * b)
|
|
||||||
{
|
{
|
||||||
int res, n;
|
int res, n;
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_COUNT_BITS_C
|
#ifdef BN_MP_COUNT_BITS_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,15 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* returns the number of bits in an int */
|
/* returns the number of bits in an int */
|
||||||
int
|
int mp_count_bits(const mp_int *a)
|
||||||
mp_count_bits (mp_int * a)
|
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
mp_digit q;
|
mp_digit q;
|
||||||
@ -32,9 +28,9 @@ mp_count_bits (mp_int * a)
|
|||||||
|
|
||||||
/* take the last digit and count the bits in it */
|
/* take the last digit and count the bits in it */
|
||||||
q = a->dp[a->used - 1];
|
q = a->dp[a->used - 1];
|
||||||
while (q > ((mp_digit) 0)) {
|
while (q > (mp_digit)0) {
|
||||||
++r;
|
++r;
|
||||||
q >>= ((mp_digit) 1);
|
q >>= (mp_digit)1;
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
52
bn_mp_div.c
52
bn_mp_div.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_DIV_C
|
#ifdef BN_MP_DIV_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,16 +9,13 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef BN_MP_DIV_SMALL
|
#ifdef BN_MP_DIV_SMALL
|
||||||
|
|
||||||
/* slower bit-bang division... also smaller */
|
/* slower bit-bang division... also smaller */
|
||||||
int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
|
||||||
{
|
{
|
||||||
mp_int ta, tb, tq, q;
|
mp_int ta, tb, tq, q;
|
||||||
int res, n, n2;
|
int res, n, n2;
|
||||||
@ -47,7 +44,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mp_set(&tq, 1);
|
mp_set(&tq, 1uL);
|
||||||
n = mp_count_bits(a) - mp_count_bits(b);
|
n = mp_count_bits(a) - mp_count_bits(b);
|
||||||
if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
|
if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
|
||||||
((res = mp_abs(b, &tb)) != MP_OKAY) ||
|
((res = mp_abs(b, &tb)) != MP_OKAY) ||
|
||||||
@ -100,7 +97,7 @@ LBL_ERR:
|
|||||||
* The overall algorithm is as described as
|
* The overall algorithm is as described as
|
||||||
* 14.20 from HAC but fixed to treat these cases.
|
* 14.20 from HAC but fixed to treat these cases.
|
||||||
*/
|
*/
|
||||||
int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
int mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
|
||||||
{
|
{
|
||||||
mp_int q, x, y, t1, t2;
|
mp_int q, x, y, t1, t2;
|
||||||
int res, n, t, i, norm, neg;
|
int res, n, t, i, norm, neg;
|
||||||
@ -150,7 +147,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
|
|
||||||
/* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */
|
/* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */
|
||||||
norm = mp_count_bits(&y) % DIGIT_BIT;
|
norm = mp_count_bits(&y) % DIGIT_BIT;
|
||||||
if (norm < (int)(DIGIT_BIT-1)) {
|
if (norm < (DIGIT_BIT - 1)) {
|
||||||
norm = (DIGIT_BIT - 1) - norm;
|
norm = (DIGIT_BIT - 1) - norm;
|
||||||
if ((res = mp_mul_2d(&x, norm, &x)) != MP_OKAY) {
|
if ((res = mp_mul_2d(&x, norm, &x)) != MP_OKAY) {
|
||||||
goto LBL_Y;
|
goto LBL_Y;
|
||||||
@ -190,16 +187,16 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
/* step 3.1 if xi == yt then set q{i-t-1} to b-1,
|
/* step 3.1 if xi == yt then set q{i-t-1} to b-1,
|
||||||
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
|
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
|
||||||
if (x.dp[i] == y.dp[t]) {
|
if (x.dp[i] == y.dp[t]) {
|
||||||
q.dp[(i - t) - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
|
q.dp[(i - t) - 1] = ((mp_digit)1 << (mp_digit)DIGIT_BIT) - (mp_digit)1;
|
||||||
} else {
|
} else {
|
||||||
mp_word tmp;
|
mp_word tmp;
|
||||||
tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT);
|
tmp = (mp_word)x.dp[i] << (mp_word)DIGIT_BIT;
|
||||||
tmp |= ((mp_word) x.dp[i - 1]);
|
tmp |= (mp_word)x.dp[i - 1];
|
||||||
tmp /= ((mp_word) y.dp[t]);
|
tmp /= (mp_word)y.dp[t];
|
||||||
if (tmp > (mp_word)MP_MASK) {
|
if (tmp > (mp_word)MP_MASK) {
|
||||||
tmp = MP_MASK;
|
tmp = MP_MASK;
|
||||||
}
|
}
|
||||||
q.dp[(i - t) - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
|
q.dp[(i - t) - 1] = (mp_digit)(tmp & (mp_word)MP_MASK);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* while (q{i-t-1} * (yt * b + y{t-1})) >
|
/* while (q{i-t-1} * (yt * b + y{t-1})) >
|
||||||
@ -207,13 +204,13 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
|
|
||||||
do q{i-t-1} -= 1;
|
do q{i-t-1} -= 1;
|
||||||
*/
|
*/
|
||||||
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] + 1) & MP_MASK;
|
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] + 1uL) & (mp_digit)MP_MASK;
|
||||||
do {
|
do {
|
||||||
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1) & MP_MASK;
|
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1uL) & (mp_digit)MP_MASK;
|
||||||
|
|
||||||
/* find left hand */
|
/* find left hand */
|
||||||
mp_zero(&t1);
|
mp_zero(&t1);
|
||||||
t1.dp[0] = ((t - 1) < 0) ? 0 : y.dp[t - 1];
|
t1.dp[0] = ((t - 1) < 0) ? 0u : y.dp[t - 1];
|
||||||
t1.dp[1] = y.dp[t];
|
t1.dp[1] = y.dp[t];
|
||||||
t1.used = 2;
|
t1.used = 2;
|
||||||
if ((res = mp_mul_d(&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) {
|
if ((res = mp_mul_d(&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) {
|
||||||
@ -221,8 +218,8 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* find right hand */
|
/* find right hand */
|
||||||
t2.dp[0] = ((i - 2) < 0) ? 0 : x.dp[i - 2];
|
t2.dp[0] = ((i - 2) < 0) ? 0u : x.dp[i - 2];
|
||||||
t2.dp[1] = ((i - 1) < 0) ? 0 : x.dp[i - 1];
|
t2.dp[1] = ((i - 1) < 0) ? 0u : x.dp[i - 1];
|
||||||
t2.dp[2] = x.dp[i];
|
t2.dp[2] = x.dp[i];
|
||||||
t2.used = 3;
|
t2.used = 3;
|
||||||
} while (mp_cmp_mag(&t1, &t2) == MP_GT);
|
} while (mp_cmp_mag(&t1, &t2) == MP_GT);
|
||||||
@ -252,7 +249,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
goto LBL_Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1UL) & MP_MASK;
|
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1uL) & MP_MASK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,11 +275,16 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
|||||||
|
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
|
|
||||||
LBL_Y:mp_clear (&y);
|
LBL_Y:
|
||||||
LBL_X:mp_clear (&x);
|
mp_clear(&y);
|
||||||
LBL_T2:mp_clear (&t2);
|
LBL_X:
|
||||||
LBL_T1:mp_clear (&t1);
|
mp_clear(&x);
|
||||||
LBL_Q:mp_clear (&q);
|
LBL_T2:
|
||||||
|
mp_clear(&t2);
|
||||||
|
LBL_T1:
|
||||||
|
mp_clear(&t1);
|
||||||
|
LBL_Q:
|
||||||
|
mp_clear(&q);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_DIV_2_C
|
#ifdef BN_MP_DIV_2_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* b = a/2 */
|
/* b = a/2 */
|
||||||
int mp_div_2(mp_int * a, mp_int * b)
|
int mp_div_2(const mp_int *a, mp_int *b)
|
||||||
{
|
{
|
||||||
int x, res, oldused;
|
int x, res, oldused;
|
||||||
|
|
||||||
@ -42,7 +39,7 @@ int mp_div_2(mp_int * a, mp_int * b)
|
|||||||
r = 0;
|
r = 0;
|
||||||
for (x = b->used - 1; x >= 0; x--) {
|
for (x = b->used - 1; x >= 0; x--) {
|
||||||
/* get the carry for the next iteration */
|
/* get the carry for the next iteration */
|
||||||
rr = *tmpa & 1;
|
rr = *tmpa & 1u;
|
||||||
|
|
||||||
/* shift the current digit, add in carry and store */
|
/* shift the current digit, add in carry and store */
|
||||||
*tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
|
*tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_DIV_2D_C
|
#ifdef BN_MP_DIV_2D_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* shift right by a certain bit count (store quotient in c, optional remainder in d) */
|
/* shift right by a certain bit count (store quotient in c, optional remainder in d) */
|
||||||
int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
|
int mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d)
|
||||||
{
|
{
|
||||||
mp_digit D, r, rr;
|
mp_digit D, r, rr;
|
||||||
int x, res;
|
int x, res;
|
||||||
@ -44,20 +41,20 @@ int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* shift by as many digits in the bit count */
|
/* shift by as many digits in the bit count */
|
||||||
if (b >= (int)DIGIT_BIT) {
|
if (b >= DIGIT_BIT) {
|
||||||
mp_rshd(c, b / DIGIT_BIT);
|
mp_rshd(c, b / DIGIT_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* shift any bit count < DIGIT_BIT */
|
/* shift any bit count < DIGIT_BIT */
|
||||||
D = (mp_digit)(b % DIGIT_BIT);
|
D = (mp_digit)(b % DIGIT_BIT);
|
||||||
if (D != 0) {
|
if (D != 0u) {
|
||||||
mp_digit *tmpc, mask, shift;
|
mp_digit *tmpc, mask, shift;
|
||||||
|
|
||||||
/* mask */
|
/* mask */
|
||||||
mask = (((mp_digit)1) << D) - 1;
|
mask = ((mp_digit)1 << D) - 1uL;
|
||||||
|
|
||||||
/* shift for lsb */
|
/* shift for lsb */
|
||||||
shift = DIGIT_BIT - D;
|
shift = (mp_digit)DIGIT_BIT - D;
|
||||||
|
|
||||||
/* alias */
|
/* alias */
|
||||||
tmpc = c->dp + (c->used - 1);
|
tmpc = c->dp + (c->used - 1);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_DIV_3_C
|
#ifdef BN_MP_DIV_3_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,15 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* divide by three (based on routine from MPI and the GMP manual) */
|
/* divide by three (based on routine from MPI and the GMP manual) */
|
||||||
int
|
int mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
|
||||||
mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
|
||||||
{
|
{
|
||||||
mp_int q;
|
mp_int q;
|
||||||
mp_word w, t;
|
mp_word w, t;
|
||||||
@ -25,7 +21,7 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
|||||||
int res, ix;
|
int res, ix;
|
||||||
|
|
||||||
/* b = 2**DIGIT_BIT / 3 */
|
/* b = 2**DIGIT_BIT / 3 */
|
||||||
b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3);
|
b = ((mp_word)1 << (mp_word)DIGIT_BIT) / (mp_word)3;
|
||||||
|
|
||||||
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
|
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
@ -35,11 +31,11 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
|||||||
q.sign = a->sign;
|
q.sign = a->sign;
|
||||||
w = 0;
|
w = 0;
|
||||||
for (ix = a->used - 1; ix >= 0; ix--) {
|
for (ix = a->used - 1; ix >= 0; ix--) {
|
||||||
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
|
w = (w << (mp_word)DIGIT_BIT) | (mp_word)a->dp[ix];
|
||||||
|
|
||||||
if (w >= 3) {
|
if (w >= 3u) {
|
||||||
/* multiply w by [1/3] */
|
/* multiply w by [1/3] */
|
||||||
t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT);
|
t = (w * (mp_word)b) >> (mp_word)DIGIT_BIT;
|
||||||
|
|
||||||
/* now subtract 3 * [w/3] from w, to get the remainder */
|
/* now subtract 3 * [w/3] from w, to get the remainder */
|
||||||
w -= t+t+t;
|
w -= t+t+t;
|
||||||
@ -47,9 +43,9 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
|||||||
/* fixup the remainder as required since
|
/* fixup the remainder as required since
|
||||||
* the optimization is not exact.
|
* the optimization is not exact.
|
||||||
*/
|
*/
|
||||||
while (w >= 3) {
|
while (w >= 3u) {
|
||||||
t += 1;
|
t += 1u;
|
||||||
w -= 3;
|
w -= 3u;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
t = 0;
|
t = 0;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_DIV_D_C
|
#ifdef BN_MP_DIV_D_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int s_is_power_of_two(mp_digit b, int *p)
|
static int s_is_power_of_two(mp_digit b, int *p)
|
||||||
@ -20,12 +17,12 @@ static int s_is_power_of_two(mp_digit b, int *p)
|
|||||||
int x;
|
int x;
|
||||||
|
|
||||||
/* fast return if no power of two */
|
/* fast return if no power of two */
|
||||||
if ((b == 0) || ((b & (b-1)) != 0)) {
|
if ((b == 0u) || ((b & (b-1u)) != 0u)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (x = 0; x < DIGIT_BIT; x++) {
|
for (x = 0; x < DIGIT_BIT; x++) {
|
||||||
if (b == (((mp_digit)1)<<x)) {
|
if (b == ((mp_digit)1<<(mp_digit)x)) {
|
||||||
*p = x;
|
*p = x;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -34,7 +31,7 @@ static int s_is_power_of_two(mp_digit b, int *p)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* single digit division (based on routine from MPI) */
|
/* single digit division (based on routine from MPI) */
|
||||||
int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
|
int mp_div_d(const mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
|
||||||
{
|
{
|
||||||
mp_int q;
|
mp_int q;
|
||||||
mp_word w;
|
mp_word w;
|
||||||
@ -42,12 +39,12 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
|
|||||||
int res, ix;
|
int res, ix;
|
||||||
|
|
||||||
/* cannot divide by zero */
|
/* cannot divide by zero */
|
||||||
if (b == 0) {
|
if (b == 0u) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* quick outs */
|
/* quick outs */
|
||||||
if ((b == 1) || (mp_iszero(a) == MP_YES)) {
|
if ((b == 1u) || (mp_iszero(a) == MP_YES)) {
|
||||||
if (d != NULL) {
|
if (d != NULL) {
|
||||||
*d = 0;
|
*d = 0;
|
||||||
}
|
}
|
||||||
@ -60,7 +57,7 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
|
|||||||
/* power of two ? */
|
/* power of two ? */
|
||||||
if (s_is_power_of_two(b, &ix) == 1) {
|
if (s_is_power_of_two(b, &ix) == 1) {
|
||||||
if (d != NULL) {
|
if (d != NULL) {
|
||||||
*d = a->dp[0] & ((((mp_digit)1)<<ix) - 1);
|
*d = a->dp[0] & (((mp_digit)1<<(mp_digit)ix) - 1uL);
|
||||||
}
|
}
|
||||||
if (c != NULL) {
|
if (c != NULL) {
|
||||||
return mp_div_2d(a, ix, c, NULL);
|
return mp_div_2d(a, ix, c, NULL);
|
||||||
@ -70,7 +67,7 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
|
|||||||
|
|
||||||
#ifdef BN_MP_DIV_3_C
|
#ifdef BN_MP_DIV_3_C
|
||||||
/* three? */
|
/* three? */
|
||||||
if (b == 3) {
|
if (b == 3u) {
|
||||||
return mp_div_3(a, c, d);
|
return mp_div_3(a, c, d);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -84,15 +81,15 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
|
|||||||
q.sign = a->sign;
|
q.sign = a->sign;
|
||||||
w = 0;
|
w = 0;
|
||||||
for (ix = a->used - 1; ix >= 0; ix--) {
|
for (ix = a->used - 1; ix >= 0; ix--) {
|
||||||
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
|
w = (w << (mp_word)DIGIT_BIT) | (mp_word)a->dp[ix];
|
||||||
|
|
||||||
if (w >= b) {
|
if (w >= b) {
|
||||||
t = (mp_digit)(w / b);
|
t = (mp_digit)(w / b);
|
||||||
w -= ((mp_word)t) * ((mp_word)b);
|
w -= (mp_word)t * (mp_word)b;
|
||||||
} else {
|
} else {
|
||||||
t = 0;
|
t = 0;
|
||||||
}
|
}
|
||||||
q.dp[ix] = (mp_digit)t;
|
q.dp[ix] = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (d != NULL) {
|
if (d != NULL) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_DR_IS_MODULUS_C
|
#ifdef BN_MP_DR_IS_MODULUS_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* determines if a number is a valid DR modulus */
|
/* determines if a number is a valid DR modulus */
|
||||||
int mp_dr_is_modulus(mp_int *a)
|
int mp_dr_is_modulus(const mp_int *a)
|
||||||
{
|
{
|
||||||
int ix;
|
int ix;
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_DR_REDUCE_C
|
#ifdef BN_MP_DR_REDUCE_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
|
/* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
|
||||||
@ -29,8 +26,7 @@
|
|||||||
*
|
*
|
||||||
* Input x must be in the range 0 <= x <= (n-1)**2
|
* Input x must be in the range 0 <= x <= (n-1)**2
|
||||||
*/
|
*/
|
||||||
int
|
int mp_dr_reduce(mp_int *x, const mp_int *n, mp_digit k)
|
||||||
mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
|
|
||||||
{
|
{
|
||||||
int err, i, m;
|
int err, i, m;
|
||||||
mp_word r;
|
mp_word r;
|
||||||
@ -62,7 +58,7 @@ top:
|
|||||||
|
|
||||||
/* compute (x mod B**m) + k * [x/B**m] inline and inplace */
|
/* compute (x mod B**m) + k * [x/B**m] inline and inplace */
|
||||||
for (i = 0; i < m; i++) {
|
for (i = 0; i < m; i++) {
|
||||||
r = (((mp_word)*tmpx2++) * (mp_word)k) + *tmpx1 + mu;
|
r = ((mp_word)*tmpx2++ * (mp_word)k) + *tmpx1 + mu;
|
||||||
*tmpx1++ = (mp_digit)(r & MP_MASK);
|
*tmpx1++ = (mp_digit)(r & MP_MASK);
|
||||||
mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
|
mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_DR_SETUP_C
|
#ifdef BN_MP_DR_SETUP_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,20 +9,16 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* determines the setup value */
|
/* determines the setup value */
|
||||||
void mp_dr_setup(mp_int *a, mp_digit *d)
|
void mp_dr_setup(const mp_int *a, mp_digit *d)
|
||||||
{
|
{
|
||||||
/* the casts are required if DIGIT_BIT is one less than
|
/* the casts are required if DIGIT_BIT is one less than
|
||||||
* the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
|
* the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
|
||||||
*/
|
*/
|
||||||
*d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) -
|
*d = (mp_digit)(((mp_word)1 << (mp_word)DIGIT_BIT) - (mp_word)a->dp[0]);
|
||||||
((mp_word)a->dp[0]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
10
bn_mp_exch.c
10
bn_mp_exch.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_EXCH_C
|
#ifdef BN_MP_EXCH_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,17 +9,13 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* swap the elements of two integers, for cases where you can't simply swap the
|
/* swap the elements of two integers, for cases where you can't simply swap the
|
||||||
* mp_int pointers around
|
* mp_int pointers around
|
||||||
*/
|
*/
|
||||||
void
|
void mp_exch(mp_int *a, mp_int *b)
|
||||||
mp_exch (mp_int * a, mp_int * b)
|
|
||||||
{
|
{
|
||||||
mp_int t;
|
mp_int t;
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_EXPORT_C
|
#ifdef BN_MP_EXPORT_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,17 +9,15 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* based on gmp's mpz_export.
|
/* based on gmp's mpz_export.
|
||||||
* see http://gmplib.org/manual/Integer-Import-and-Export.html
|
* see http://gmplib.org/manual/Integer-Import-and-Export.html
|
||||||
*/
|
*/
|
||||||
int mp_export(void *rop, size_t *countp, int order, size_t size,
|
int mp_export(void *rop, size_t *countp, int order, size_t size,
|
||||||
int endian, size_t nails, mp_int* op) {
|
int endian, size_t nails, const mp_int *op)
|
||||||
|
{
|
||||||
int result;
|
int result;
|
||||||
size_t odd_nails, nail_bytes, i, j, bits, count;
|
size_t odd_nails, nail_bytes, i, j, bits, count;
|
||||||
unsigned char odd_nail_mask;
|
unsigned char odd_nail_mask;
|
||||||
@ -37,35 +35,33 @@ int mp_export(void* rop, size_t* countp, int order, size_t size,
|
|||||||
} lint;
|
} lint;
|
||||||
lint.i = 0x01020304;
|
lint.i = 0x01020304;
|
||||||
|
|
||||||
endian = (lint.c[0] == 4) ? -1 : 1;
|
endian = (lint.c[0] == '\x04') ? -1 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
odd_nails = (nails % 8);
|
odd_nails = (nails % 8u);
|
||||||
odd_nail_mask = 0xff;
|
odd_nail_mask = 0xff;
|
||||||
for (i = 0; i < odd_nails; ++i) {
|
for (i = 0; i < odd_nails; ++i) {
|
||||||
odd_nail_mask ^= (1 << (7 - i));
|
odd_nail_mask ^= (unsigned char)(1u << (7u - i));
|
||||||
}
|
}
|
||||||
nail_bytes = nails / 8;
|
nail_bytes = nails / 8u;
|
||||||
|
|
||||||
bits = mp_count_bits(&t);
|
bits = (size_t)mp_count_bits(&t);
|
||||||
count = (bits / ((size * 8) - nails)) + (((bits % ((size * 8) - nails)) != 0) ? 1 : 0);
|
count = (bits / ((size * 8u) - nails)) + (((bits % ((size * 8u) - nails)) != 0u) ? 1u : 0u);
|
||||||
|
|
||||||
for (i = 0; i < count; ++i) {
|
for (i = 0; i < count; ++i) {
|
||||||
for (j = 0; j < size; ++j) {
|
for (j = 0; j < size; ++j) {
|
||||||
unsigned char* byte = (
|
unsigned char *byte = (unsigned char *)rop +
|
||||||
(unsigned char*)rop +
|
(((order == -1) ? i : ((count - 1u) - i)) * size) +
|
||||||
(((order == -1) ? i : ((count - 1) - i)) * size) +
|
((endian == -1) ? j : ((size - 1u) - j));
|
||||||
((endian == -1) ? j : ((size - 1) - j))
|
|
||||||
);
|
|
||||||
|
|
||||||
if (j >= (size - nail_bytes)) {
|
if (j >= (size - nail_bytes)) {
|
||||||
*byte = 0;
|
*byte = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
*byte = (unsigned char)((j == ((size - nail_bytes) - 1)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFF));
|
*byte = (unsigned char)((j == ((size - nail_bytes) - 1u)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFFuL));
|
||||||
|
|
||||||
if ((result = mp_div_2d(&t, ((j == ((size - nail_bytes) - 1)) ? (8 - odd_nails) : 8), &t, NULL)) != MP_OKAY) {
|
if ((result = mp_div_2d(&t, (j == ((size - nail_bytes) - 1u)) ? (int)(8u - odd_nails) : 8, &t, NULL)) != MP_OKAY) {
|
||||||
mp_clear(&t);
|
mp_clear(&t);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_EXPT_D_C
|
#ifdef BN_MP_EXPT_D_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* wrapper function for mp_expt_d_ex() */
|
/* wrapper function for mp_expt_d_ex() */
|
||||||
int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
|
int mp_expt_d(const mp_int *a, mp_digit b, mp_int *c)
|
||||||
{
|
{
|
||||||
return mp_expt_d_ex(a, b, c, 0);
|
return mp_expt_d_ex(a, b, c, 0);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_EXPT_D_EX_C
|
#ifdef BN_MP_EXPT_D_EX_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* calculate c = a**b using a square-multiply algorithm */
|
/* calculate c = a**b using a square-multiply algorithm */
|
||||||
int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
int mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
unsigned int x;
|
unsigned int x;
|
||||||
@ -28,12 +25,12 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* set initial result */
|
/* set initial result */
|
||||||
mp_set (c, 1);
|
mp_set(c, 1uL);
|
||||||
|
|
||||||
if (fast != 0) {
|
if (fast != 0) {
|
||||||
while (b > 0) {
|
while (b > 0u) {
|
||||||
/* if the bit is set multiply */
|
/* if the bit is set multiply */
|
||||||
if ((b & 1) != 0) {
|
if ((b & 1u) != 0u) {
|
||||||
if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
|
if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
|
||||||
mp_clear(&g);
|
mp_clear(&g);
|
||||||
return res;
|
return res;
|
||||||
@ -41,7 +38,7 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* square */
|
/* square */
|
||||||
if (b > 1) {
|
if (b > 1u) {
|
||||||
if ((res = mp_sqr(&g, &g)) != MP_OKAY) {
|
if ((res = mp_sqr(&g, &g)) != MP_OKAY) {
|
||||||
mp_clear(&g);
|
mp_clear(&g);
|
||||||
return res;
|
return res;
|
||||||
@ -51,9 +48,8 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
|||||||
/* shift to next bit */
|
/* shift to next bit */
|
||||||
b >>= 1;
|
b >>= 1;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
for (x = 0; x < (unsigned)DIGIT_BIT; x++) {
|
||||||
for (x = 0; x < DIGIT_BIT; x++) {
|
|
||||||
/* square */
|
/* square */
|
||||||
if ((res = mp_sqr(c, c)) != MP_OKAY) {
|
if ((res = mp_sqr(c, c)) != MP_OKAY) {
|
||||||
mp_clear(&g);
|
mp_clear(&g);
|
||||||
@ -61,7 +57,7 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if the bit is set multiply */
|
/* if the bit is set multiply */
|
||||||
if ((b & (mp_digit) (((mp_digit)1) << (DIGIT_BIT - 1))) != 0) {
|
if ((b & ((mp_digit)1 << (DIGIT_BIT - 1))) != 0u) {
|
||||||
if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
|
if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
|
||||||
mp_clear(&g);
|
mp_clear(&g);
|
||||||
return res;
|
return res;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_EXPTMOD_C
|
#ifdef BN_MP_EXPTMOD_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@ -21,7 +18,7 @@
|
|||||||
* embedded in the normal function but that wasted alot of stack space
|
* embedded in the normal function but that wasted alot of stack space
|
||||||
* for nothing (since 99% of the time the Montgomery code would be called)
|
* for nothing (since 99% of the time the Montgomery code would be called)
|
||||||
*/
|
*/
|
||||||
int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
int mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
|
||||||
{
|
{
|
||||||
int dr;
|
int dr;
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_EXPTMOD_FAST_C
|
#ifdef BN_MP_EXPTMOD_FAST_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
|
/* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
|
||||||
@ -29,7 +26,7 @@
|
|||||||
# define TAB_SIZE 256
|
# define TAB_SIZE 256
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
int mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode)
|
||||||
{
|
{
|
||||||
mp_int M[TAB_SIZE], res;
|
mp_int M[TAB_SIZE], res;
|
||||||
mp_digit buf, mp;
|
mp_digit buf, mp;
|
||||||
@ -39,7 +36,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
|||||||
* one of many reduction algorithms without modding the guts of
|
* one of many reduction algorithms without modding the guts of
|
||||||
* the code with if statements everywhere.
|
* the code with if statements everywhere.
|
||||||
*/
|
*/
|
||||||
int (*redux)(mp_int*,mp_int*,mp_digit);
|
int (*redux)(mp_int *x, const mp_int *n, mp_digit rho);
|
||||||
|
|
||||||
/* find window size */
|
/* find window size */
|
||||||
x = mp_count_bits(X);
|
x = mp_count_bits(X);
|
||||||
@ -96,7 +93,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
|||||||
|
|
||||||
/* automatically pick the comba one if available (saves quite a few calls/ifs) */
|
/* automatically pick the comba one if available (saves quite a few calls/ifs) */
|
||||||
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
|
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
|
||||||
if ((((P->used * 2) + 1) < MP_WARRAY) &&
|
if ((((P->used * 2) + 1) < (int)MP_WARRAY) &&
|
||||||
(P->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
(P->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
||||||
redux = fast_mp_montgomery_reduce;
|
redux = fast_mp_montgomery_reduce;
|
||||||
} else
|
} else
|
||||||
@ -160,22 +157,22 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
|||||||
goto LBL_RES;
|
goto LBL_RES;
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
mp_set(&res, 1);
|
mp_set(&res, 1uL);
|
||||||
if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
|
if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
|
||||||
goto LBL_RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
|
/* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
|
||||||
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
if ((err = mp_copy(&M[1], &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) {
|
||||||
goto LBL_RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (x = 0; x < (winsize - 1); x++) {
|
for (x = 0; x < (winsize - 1); x++) {
|
||||||
if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
if ((err = mp_sqr(&M[(size_t)1 << (winsize - 1)], &M[(size_t)1 << (winsize - 1)])) != MP_OKAY) {
|
||||||
goto LBL_RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) {
|
if ((err = redux(&M[(size_t)1 << (winsize - 1)], P, mp)) != MP_OKAY) {
|
||||||
goto LBL_RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -305,7 +302,8 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
|||||||
/* swap res with Y */
|
/* swap res with Y */
|
||||||
mp_exch(&res, Y);
|
mp_exch(&res, Y);
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
LBL_RES:mp_clear (&res);
|
LBL_RES:
|
||||||
|
mp_clear(&res);
|
||||||
LBL_M:
|
LBL_M:
|
||||||
mp_clear(&M[1]);
|
mp_clear(&M[1]);
|
||||||
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_EXTEUCLID_C
|
#ifdef BN_MP_EXTEUCLID_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,16 +9,13 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Extended euclidean algorithm of (a, b) produces
|
/* Extended euclidean algorithm of (a, b) produces
|
||||||
a*u1 + b*u2 = u3
|
a*u1 + b*u2 = u3
|
||||||
*/
|
*/
|
||||||
int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
|
int mp_exteuclid(const mp_int *a, const mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
|
||||||
{
|
{
|
||||||
mp_int u1, u2, u3, v1, v2, v3, t1, t2, t3, q, tmp;
|
mp_int u1, u2, u3, v1, v2, v3, t1, t2, t3, q, tmp;
|
||||||
int err;
|
int err;
|
||||||
@ -28,48 +25,90 @@ int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* initialize, (u1,u2,u3) = (1,0,a) */
|
/* initialize, (u1,u2,u3) = (1,0,a) */
|
||||||
mp_set(&u1, 1);
|
mp_set(&u1, 1uL);
|
||||||
if ((err = mp_copy(a, &u3)) != MP_OKAY) { goto LBL_ERR; }
|
if ((err = mp_copy(a, &u3)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
/* initialize, (v1,v2,v3) = (0,1,b) */
|
/* initialize, (v1,v2,v3) = (0,1,b) */
|
||||||
mp_set(&v2, 1);
|
mp_set(&v2, 1uL);
|
||||||
if ((err = mp_copy(b, &v3)) != MP_OKAY) { goto LBL_ERR; }
|
if ((err = mp_copy(b, &v3)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
/* loop while v3 != 0 */
|
/* loop while v3 != 0 */
|
||||||
while (mp_iszero(&v3) == MP_NO) {
|
while (mp_iszero(&v3) == MP_NO) {
|
||||||
/* q = u3/v3 */
|
/* q = u3/v3 */
|
||||||
if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { goto LBL_ERR; }
|
if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
/* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */
|
/* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */
|
||||||
if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; }
|
if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) {
|
||||||
if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { goto LBL_ERR; }
|
goto LBL_ERR;
|
||||||
if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; }
|
}
|
||||||
if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { goto LBL_ERR; }
|
if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) {
|
||||||
if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; }
|
goto LBL_ERR;
|
||||||
if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { goto LBL_ERR; }
|
}
|
||||||
|
if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
/* (u1,u2,u3) = (v1,v2,v3) */
|
/* (u1,u2,u3) = (v1,v2,v3) */
|
||||||
if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { goto LBL_ERR; }
|
if ((err = mp_copy(&v1, &u1)) != MP_OKAY) {
|
||||||
if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { goto LBL_ERR; }
|
goto LBL_ERR;
|
||||||
if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { goto LBL_ERR; }
|
}
|
||||||
|
if ((err = mp_copy(&v2, &u2)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
if ((err = mp_copy(&v3, &u3)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
/* (v1,v2,v3) = (t1,t2,t3) */
|
/* (v1,v2,v3) = (t1,t2,t3) */
|
||||||
if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { goto LBL_ERR; }
|
if ((err = mp_copy(&t1, &v1)) != MP_OKAY) {
|
||||||
if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { goto LBL_ERR; }
|
goto LBL_ERR;
|
||||||
if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { goto LBL_ERR; }
|
}
|
||||||
|
if ((err = mp_copy(&t2, &v2)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
if ((err = mp_copy(&t3, &v3)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* make sure U3 >= 0 */
|
/* make sure U3 >= 0 */
|
||||||
if (u3.sign == MP_NEG) {
|
if (u3.sign == MP_NEG) {
|
||||||
if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { goto LBL_ERR; }
|
if ((err = mp_neg(&u1, &u1)) != MP_OKAY) {
|
||||||
if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { goto LBL_ERR; }
|
goto LBL_ERR;
|
||||||
if ((err = mp_neg(&u3, &u3)) != MP_OKAY) { goto LBL_ERR; }
|
}
|
||||||
|
if ((err = mp_neg(&u2, &u2)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
|
if ((err = mp_neg(&u3, &u3)) != MP_OKAY) {
|
||||||
|
goto LBL_ERR;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* copy result out */
|
/* copy result out */
|
||||||
if (U1 != NULL) { mp_exch(U1, &u1); }
|
if (U1 != NULL) {
|
||||||
if (U2 != NULL) { mp_exch(U2, &u2); }
|
mp_exch(U1, &u1);
|
||||||
if (U3 != NULL) { mp_exch(U3, &u3); }
|
}
|
||||||
|
if (U2 != NULL) {
|
||||||
|
mp_exch(U2, &u2);
|
||||||
|
}
|
||||||
|
if (U3 != NULL) {
|
||||||
|
mp_exch(U3, &u3);
|
||||||
|
}
|
||||||
|
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
LBL_ERR:
|
LBL_ERR:
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_FREAD_C
|
#ifdef BN_MP_FREAD_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef LTM_NO_FILE
|
#ifndef LTM_NO_FILE
|
||||||
@ -20,13 +17,14 @@
|
|||||||
int mp_fread(mp_int *a, int radix, FILE *stream)
|
int mp_fread(mp_int *a, int radix, FILE *stream)
|
||||||
{
|
{
|
||||||
int err, ch, neg, y;
|
int err, ch, neg, y;
|
||||||
|
unsigned pos;
|
||||||
|
|
||||||
/* clear a */
|
/* clear a */
|
||||||
mp_zero(a);
|
mp_zero(a);
|
||||||
|
|
||||||
/* if first digit is - then set negative */
|
/* if first digit is - then set negative */
|
||||||
ch = fgetc(stream);
|
ch = fgetc(stream);
|
||||||
if (ch == '-') {
|
if (ch == (int)'-') {
|
||||||
neg = MP_NEG;
|
neg = MP_NEG;
|
||||||
ch = fgetc(stream);
|
ch = fgetc(stream);
|
||||||
} else {
|
} else {
|
||||||
@ -34,27 +32,28 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
/* find y in the radix map */
|
pos = (unsigned)(ch - (int)'(');
|
||||||
for (y = 0; y < radix; y++) {
|
if (mp_s_rmap_reverse_sz < pos) {
|
||||||
if (mp_s_rmap[y] == ch) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (y == radix) {
|
y = (int)mp_s_rmap_reverse[pos];
|
||||||
|
|
||||||
|
if ((y == 0xff) || (y >= radix)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* shift up and add */
|
/* shift up and add */
|
||||||
if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) {
|
if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
|
if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
ch = fgetc(stream);
|
ch = fgetc(stream);
|
||||||
}
|
}
|
||||||
if (mp_cmp_d(a, 0) != MP_EQ) {
|
if (mp_cmp_d(a, 0uL) != MP_EQ) {
|
||||||
a->sign = neg;
|
a->sign = neg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_FWRITE_C
|
#ifdef BN_MP_FWRITE_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef LTM_NO_FILE
|
#ifndef LTM_NO_FILE
|
||||||
int mp_fwrite(mp_int *a, int radix, FILE *stream)
|
int mp_fwrite(const mp_int *a, int radix, FILE *stream)
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf;
|
||||||
int err, len, x;
|
int err, len, x;
|
||||||
@ -25,7 +22,7 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream)
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = OPT_CAST(char) XMALLOC (len);
|
buf = OPT_CAST(char) XMALLOC((size_t)len);
|
||||||
if (buf == NULL) {
|
if (buf == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
}
|
}
|
||||||
@ -36,7 +33,7 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (x = 0; x < len; x++) {
|
for (x = 0; x < len; x++) {
|
||||||
if (fputc(buf[x], stream) == EOF) {
|
if (fputc((int)buf[x], stream) == EOF) {
|
||||||
XFREE(buf);
|
XFREE(buf);
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
15
bn_mp_gcd.c
15
bn_mp_gcd.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_GCD_C
|
#ifdef BN_MP_GCD_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Greatest Common Divisor using the binary method */
|
/* Greatest Common Divisor using the binary method */
|
||||||
int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
int mp_gcd(const mp_int *a, const mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
mp_int u, v;
|
mp_int u, v;
|
||||||
int k, u_lsb, v_lsb, res;
|
int k, u_lsb, v_lsb, res;
|
||||||
@ -94,8 +91,10 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
}
|
}
|
||||||
c->sign = MP_ZPOS;
|
c->sign = MP_ZPOS;
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
LBL_V:mp_clear (&u);
|
LBL_V:
|
||||||
LBL_U:mp_clear (&v);
|
mp_clear(&u);
|
||||||
|
LBL_U:
|
||||||
|
mp_clear(&v);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
54
bn_mp_get_bit.c
Normal file
54
bn_mp_get_bit.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include "tommath_private.h"
|
||||||
|
#ifdef BN_MP_GET_BIT_C
|
||||||
|
|
||||||
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
|
*
|
||||||
|
* LibTomMath is a library that provides multiple-precision
|
||||||
|
* integer arithmetic as well as number theoretic functionality.
|
||||||
|
*
|
||||||
|
* The library was designed directly after the MPI library by
|
||||||
|
* Michael Fromberger but has been written from scratch with
|
||||||
|
* additional optimizations in place.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Unlicense
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Checks the bit at position b and returns MP_YES
|
||||||
|
if the bit is 1, MP_NO if it is 0 and MP_VAL
|
||||||
|
in case of error */
|
||||||
|
int mp_get_bit(const mp_int *a, int b)
|
||||||
|
{
|
||||||
|
int limb;
|
||||||
|
mp_digit bit, isset;
|
||||||
|
|
||||||
|
if (b < 0) {
|
||||||
|
return MP_VAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
limb = b / DIGIT_BIT;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Zero is a special value with the member "used" set to zero.
|
||||||
|
* Needs to be tested before the check for the upper boundary
|
||||||
|
* otherwise (limb >= a->used) would be true for a = 0
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (mp_iszero(a) != MP_NO) {
|
||||||
|
return MP_NO;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (limb >= a->used) {
|
||||||
|
return MP_VAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bit = (mp_digit)(1) << (b % DIGIT_BIT);
|
||||||
|
|
||||||
|
isset = a->dp[limb] & bit;
|
||||||
|
return (isset != 0u) ? MP_YES : MP_NO;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ref: $Format:%D$ */
|
||||||
|
/* git commit: $Format:%H$ */
|
||||||
|
/* commit time: $Format:%ai$ */
|
31
bn_mp_get_double.c
Normal file
31
bn_mp_get_double.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include "tommath_private.h"
|
||||||
|
#ifdef BN_MP_GET_DOUBLE_C
|
||||||
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
|
*
|
||||||
|
* LibTomMath is a library that provides multiple-precision
|
||||||
|
* integer arithmetic as well as number theoretic functionality.
|
||||||
|
*
|
||||||
|
* The library was designed directly after the MPI library by
|
||||||
|
* Michael Fromberger but has been written from scratch with
|
||||||
|
* additional optimizations in place.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Unlicense
|
||||||
|
*/
|
||||||
|
|
||||||
|
double mp_get_double(const mp_int *a)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
double d = 0.0, fac = 1.0;
|
||||||
|
for (i = 0; i < DIGIT_BIT; ++i) {
|
||||||
|
fac *= 2.0;
|
||||||
|
}
|
||||||
|
for (i = USED(a); i --> 0;) {
|
||||||
|
d = (d * fac) + (double)DIGIT(a, i);
|
||||||
|
}
|
||||||
|
return (mp_isneg(a) != MP_NO) ? -d : d;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ref: $Format:%D$ */
|
||||||
|
/* git commit: $Format:%H$ */
|
||||||
|
/* commit time: $Format:%ai$ */
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_GET_INT_C
|
#ifdef BN_MP_GET_INT_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* get the lower 32-bits of an mp_int */
|
/* get the lower 32-bits of an mp_int */
|
||||||
unsigned long mp_get_int(mp_int * a)
|
unsigned long mp_get_int(const mp_int *a)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
mp_min_u32 res;
|
mp_min_u32 res;
|
||||||
@ -26,7 +23,7 @@ unsigned long mp_get_int(mp_int * a)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* get number of digits of the lsb we have to read */
|
/* get number of digits of the lsb we have to read */
|
||||||
i = MIN(a->used,(int)(((sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
|
i = MIN(a->used, ((((int)sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
|
||||||
|
|
||||||
/* get most significant digit of result */
|
/* get most significant digit of result */
|
||||||
res = DIGIT(a, i);
|
res = DIGIT(a, i);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_GET_LONG_C
|
#ifdef BN_MP_GET_LONG_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* get the lower unsigned long of an mp_int, platform dependent */
|
/* get the lower unsigned long of an mp_int, platform dependent */
|
||||||
unsigned long mp_get_long(mp_int * a)
|
unsigned long mp_get_long(const mp_int *a)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
unsigned long res;
|
unsigned long res;
|
||||||
@ -26,7 +23,7 @@ unsigned long mp_get_long(mp_int * a)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* get number of digits of the lsb we have to read */
|
/* get number of digits of the lsb we have to read */
|
||||||
i = MIN(a->used,(int)(((sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
|
i = MIN(a->used, ((((int)sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
|
||||||
|
|
||||||
/* get most significant digit of result */
|
/* get most significant digit of result */
|
||||||
res = DIGIT(a, i);
|
res = DIGIT(a, i);
|
||||||
@ -39,3 +36,7 @@ unsigned long mp_get_long(mp_int * a)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* ref: $Format:%D$ */
|
||||||
|
/* git commit: $Format:%H$ */
|
||||||
|
/* commit time: $Format:%ai$ */
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_GET_LONG_LONG_C
|
#ifdef BN_MP_GET_LONG_LONG_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* get the lower unsigned long long of an mp_int, platform dependent */
|
/* get the lower unsigned long long of an mp_int, platform dependent */
|
||||||
unsigned long long mp_get_long_long (mp_int * a)
|
unsigned long long mp_get_long_long(const mp_int *a)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
unsigned long long res;
|
unsigned long long res;
|
||||||
@ -26,7 +23,7 @@ unsigned long long mp_get_long_long (mp_int * a)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* get number of digits of the lsb we have to read */
|
/* get number of digits of the lsb we have to read */
|
||||||
i = MIN(a->used,(int)(((sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
|
i = MIN(a->used, ((((int)sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
|
||||||
|
|
||||||
/* get most significant digit of result */
|
/* get most significant digit of result */
|
||||||
res = DIGIT(a, i);
|
res = DIGIT(a, i);
|
||||||
@ -39,3 +36,7 @@ unsigned long long mp_get_long_long (mp_int * a)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* ref: $Format:%D$ */
|
||||||
|
/* git commit: $Format:%H$ */
|
||||||
|
/* commit time: $Format:%ai$ */
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_GROW_C
|
#ifdef BN_MP_GROW_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* grow as required */
|
/* grow as required */
|
||||||
@ -32,7 +29,7 @@ int mp_grow (mp_int * a, int size)
|
|||||||
* in case the operation failed we don't want
|
* in case the operation failed we don't want
|
||||||
* to overwrite the dp member of a.
|
* to overwrite the dp member of a.
|
||||||
*/
|
*/
|
||||||
tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size);
|
tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof(mp_digit) * (size_t)size);
|
||||||
if (tmp == NULL) {
|
if (tmp == NULL) {
|
||||||
/* reallocation failed but "a" is still valid [can be freed] */
|
/* reallocation failed but "a" is still valid [can be freed] */
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_IMPORT_C
|
#ifdef BN_MP_IMPORT_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,17 +9,15 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* based on gmp's mpz_import.
|
/* based on gmp's mpz_import.
|
||||||
* see http://gmplib.org/manual/Integer-Import-and-Export.html
|
* see http://gmplib.org/manual/Integer-Import-and-Export.html
|
||||||
*/
|
*/
|
||||||
int mp_import(mp_int *rop, size_t count, int order, size_t size,
|
int mp_import(mp_int *rop, size_t count, int order, size_t size,
|
||||||
int endian, size_t nails, const void* op) {
|
int endian, size_t nails, const void *op)
|
||||||
|
{
|
||||||
int result;
|
int result;
|
||||||
size_t odd_nails, nail_bytes, i, j;
|
size_t odd_nails, nail_bytes, i, j;
|
||||||
unsigned char odd_nail_mask;
|
unsigned char odd_nail_mask;
|
||||||
@ -33,30 +31,27 @@ int mp_import(mp_int* rop, size_t count, int order, size_t size,
|
|||||||
} lint;
|
} lint;
|
||||||
lint.i = 0x01020304;
|
lint.i = 0x01020304;
|
||||||
|
|
||||||
endian = (lint.c[0] == 4) ? -1 : 1;
|
endian = (lint.c[0] == '\x04') ? -1 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
odd_nails = (nails % 8);
|
odd_nails = (nails % 8u);
|
||||||
odd_nail_mask = 0xff;
|
odd_nail_mask = 0xff;
|
||||||
for (i = 0; i < odd_nails; ++i) {
|
for (i = 0; i < odd_nails; ++i) {
|
||||||
odd_nail_mask ^= (1 << (7 - i));
|
odd_nail_mask ^= (unsigned char)(1u << (7u - i));
|
||||||
}
|
}
|
||||||
nail_bytes = nails / 8;
|
nail_bytes = nails / 8u;
|
||||||
|
|
||||||
for (i = 0; i < count; ++i) {
|
for (i = 0; i < count; ++i) {
|
||||||
for (j = 0; j < (size - nail_bytes); ++j) {
|
for (j = 0; j < (size - nail_bytes); ++j) {
|
||||||
unsigned char byte = *(
|
unsigned char byte = *((unsigned char *)op +
|
||||||
(unsigned char*)op +
|
(((order == 1) ? i : ((count - 1u) - i)) * size) +
|
||||||
(((order == 1) ? i : ((count - 1) - i)) * size) +
|
((endian == 1) ? (j + nail_bytes) : (((size - 1u) - j) - nail_bytes)));
|
||||||
((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes))
|
|
||||||
);
|
|
||||||
|
|
||||||
if (
|
if ((result = mp_mul_2d(rop, (j == 0u) ? (int)(8u - odd_nails) : 8, rop)) != MP_OKAY) {
|
||||||
(result = mp_mul_2d(rop, ((j == 0) ? (8 - odd_nails) : 8), rop)) != MP_OKAY) {
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
rop->dp[0] |= (j == 0) ? (byte & odd_nail_mask) : byte;
|
rop->dp[0] |= (j == 0u) ? (mp_digit)(byte & odd_nail_mask) : (mp_digit)byte;
|
||||||
rop->used += 1;
|
rop->used += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_INIT_C
|
#ifdef BN_MP_INIT_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* init a new mp_int */
|
/* init a new mp_int */
|
||||||
@ -21,7 +18,7 @@ int mp_init (mp_int * a)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* allocate memory required and clear it */
|
/* allocate memory required and clear it */
|
||||||
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC);
|
a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * (size_t)MP_PREC);
|
||||||
if (a->dp == NULL) {
|
if (a->dp == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_INIT_COPY_C
|
#ifdef BN_MP_INIT_COPY_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* creates "a" then copies b into it */
|
/* creates "a" then copies b into it */
|
||||||
int mp_init_copy (mp_int * a, mp_int * b)
|
int mp_init_copy(mp_int *a, const mp_int *b)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_INIT_MULTI_C
|
#ifdef BN_MP_INIT_MULTI_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,11 +9,9 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
int mp_init_multi(mp_int *mp, ...)
|
int mp_init_multi(mp_int *mp, ...)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_INIT_SET_C
|
#ifdef BN_MP_INIT_SET_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* initialize and set a digit */
|
/* initialize and set a digit */
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_INIT_SET_INT_C
|
#ifdef BN_MP_INIT_SET_INT_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* initialize and set a digit */
|
/* initialize and set a digit */
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_INIT_SIZE_C
|
#ifdef BN_MP_INIT_SIZE_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* init an mp_init for a given size */
|
/* init an mp_init for a given size */
|
||||||
@ -24,7 +21,7 @@ int mp_init_size (mp_int * a, int size)
|
|||||||
size += (MP_PREC * 2) - (size % MP_PREC);
|
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||||
|
|
||||||
/* alloc mem */
|
/* alloc mem */
|
||||||
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size);
|
a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof(mp_digit) * (size_t)size);
|
||||||
if (a->dp == NULL) {
|
if (a->dp == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_INVMOD_C
|
#ifdef BN_MP_INVMOD_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,23 +9,20 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* hac 14.61, pp608 */
|
/* hac 14.61, pp608 */
|
||||||
int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
int mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
/* b cannot be negative */
|
/* b cannot be negative and has to be >1 */
|
||||||
if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) {
|
if ((b->sign == MP_NEG) || (mp_cmp_d(b, 1uL) != MP_GT)) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef BN_FAST_MP_INVMOD_C
|
#ifdef BN_FAST_MP_INVMOD_C
|
||||||
/* if the modulus is odd we can use a faster routine instead */
|
/* if the modulus is odd we can use a faster routine instead */
|
||||||
if ((mp_isodd(b) == MP_YES) && (mp_cmp_d(b, 1) != MP_EQ)) {
|
if ((mp_isodd(b) == MP_YES)) {
|
||||||
return fast_mp_invmod(a, b, c);
|
return fast_mp_invmod(a, b, c);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_INVMOD_SLOW_C
|
#ifdef BN_MP_INVMOD_SLOW_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* hac 14.61, pp608 */
|
/* hac 14.61, pp608 */
|
||||||
int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
|
int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
mp_int x, y, u, v, A, B, C, D;
|
mp_int x, y, u, v, A, B, C, D;
|
||||||
int res;
|
int res;
|
||||||
@ -53,8 +50,8 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
if ((res = mp_copy(&y, &v)) != MP_OKAY) {
|
if ((res = mp_copy(&y, &v)) != MP_OKAY) {
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
mp_set (&A, 1);
|
mp_set(&A, 1uL);
|
||||||
mp_set (&D, 1);
|
mp_set(&D, 1uL);
|
||||||
|
|
||||||
top:
|
top:
|
||||||
/* 4. while u is even do */
|
/* 4. while u is even do */
|
||||||
@ -143,13 +140,13 @@ top:
|
|||||||
/* now a = C, b = D, gcd == g*v */
|
/* now a = C, b = D, gcd == g*v */
|
||||||
|
|
||||||
/* if v != 1 then there is no inverse */
|
/* if v != 1 then there is no inverse */
|
||||||
if (mp_cmp_d (&v, 1) != MP_EQ) {
|
if (mp_cmp_d(&v, 1uL) != MP_EQ) {
|
||||||
res = MP_VAL;
|
res = MP_VAL;
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if its too low */
|
/* if its too low */
|
||||||
while (mp_cmp_d(&C, 0) == MP_LT) {
|
while (mp_cmp_d(&C, 0uL) == MP_LT) {
|
||||||
if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
|
if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
@ -165,7 +162,8 @@ top:
|
|||||||
/* C is now the inverse */
|
/* C is now the inverse */
|
||||||
mp_exch(&C, c);
|
mp_exch(&C, c);
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
|
LBL_ERR:
|
||||||
|
mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_IS_SQUARE_C
|
#ifdef BN_MP_IS_SQUARE_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Check if remainders are possible squares - fast exclude non-squares */
|
/* Check if remainders are possible squares - fast exclude non-squares */
|
||||||
@ -38,7 +35,7 @@ static const char rem_105[105] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* Store non-zero to ret if arg is square, and zero if not */
|
/* Store non-zero to ret if arg is square, and zero if not */
|
||||||
int mp_is_square(mp_int *arg,int *ret)
|
int mp_is_square(const mp_int *arg, int *ret)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
mp_digit c;
|
mp_digit c;
|
||||||
@ -58,15 +55,15 @@ int mp_is_square(mp_int *arg,int *ret)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* First check mod 128 (suppose that DIGIT_BIT is at least 7) */
|
/* First check mod 128 (suppose that DIGIT_BIT is at least 7) */
|
||||||
if (rem_128[127 & DIGIT(arg,0)] == 1) {
|
if (rem_128[127u & DIGIT(arg, 0)] == (char)1) {
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Next check mod 105 (3*5*7) */
|
/* Next check mod 105 (3*5*7) */
|
||||||
if ((res = mp_mod_d(arg,105,&c)) != MP_OKAY) {
|
if ((res = mp_mod_d(arg, 105uL, &c)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
if (rem_105[c] == 1) {
|
if (rem_105[c] == (char)1) {
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,31 +72,32 @@ int mp_is_square(mp_int *arg,int *ret)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
if ((res = mp_mod(arg, &t, &t)) != MP_OKAY) {
|
if ((res = mp_mod(arg, &t, &t)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
r = mp_get_int(&t);
|
r = mp_get_int(&t);
|
||||||
/* Check for other prime modules, note it's not an ERROR but we must
|
/* Check for other prime modules, note it's not an ERROR but we must
|
||||||
* free "t" so the easiest way is to goto ERR. We know that res
|
* free "t" so the easiest way is to goto LBL_ERR. We know that res
|
||||||
* is already equal to MP_OKAY from the mp_mod call
|
* is already equal to MP_OKAY from the mp_mod call
|
||||||
*/
|
*/
|
||||||
if (((1L<<(r%11)) & 0x5C4L) != 0L) goto ERR;
|
if (((1uL<<(r%11uL)) & 0x5C4uL) != 0uL) goto LBL_ERR;
|
||||||
if (((1L<<(r%13)) & 0x9E4L) != 0L) goto ERR;
|
if (((1uL<<(r%13uL)) & 0x9E4uL) != 0uL) goto LBL_ERR;
|
||||||
if (((1L<<(r%17)) & 0x5CE8L) != 0L) goto ERR;
|
if (((1uL<<(r%17uL)) & 0x5CE8uL) != 0uL) goto LBL_ERR;
|
||||||
if (((1L<<(r%19)) & 0x4F50CL) != 0L) goto ERR;
|
if (((1uL<<(r%19uL)) & 0x4F50CuL) != 0uL) goto LBL_ERR;
|
||||||
if (((1L<<(r%23)) & 0x7ACCA0L) != 0L) goto ERR;
|
if (((1uL<<(r%23uL)) & 0x7ACCA0uL) != 0uL) goto LBL_ERR;
|
||||||
if (((1L<<(r%29)) & 0xC2EDD0CL) != 0L) goto ERR;
|
if (((1uL<<(r%29uL)) & 0xC2EDD0CuL) != 0uL) goto LBL_ERR;
|
||||||
if (((1L<<(r%31)) & 0x6DE2B848L) != 0L) goto ERR;
|
if (((1uL<<(r%31uL)) & 0x6DE2B848uL) != 0uL) goto LBL_ERR;
|
||||||
|
|
||||||
/* Final check - is sqr(sqrt(arg)) == arg ? */
|
/* Final check - is sqr(sqrt(arg)) == arg ? */
|
||||||
if ((res = mp_sqrt(arg, &t)) != MP_OKAY) {
|
if ((res = mp_sqrt(arg, &t)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sqr(&t, &t)) != MP_OKAY) {
|
if ((res = mp_sqr(&t, &t)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
*ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO;
|
*ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO;
|
||||||
ERR:mp_clear(&t);
|
LBL_ERR:
|
||||||
|
mp_clear(&t);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_JACOBI_C
|
#ifdef BN_MP_JACOBI_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,106 +9,25 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* computes the jacobi c = (a | n) (or Legendre if n is prime)
|
/* computes the jacobi c = (a | n) (or Legendre if n is prime)
|
||||||
* HAC pp. 73 Algorithm 2.149
|
* Kept for legacy reasons, please use mp_kronecker() instead
|
||||||
* HAC is wrong here, as the special case of (0 | 1) is not
|
|
||||||
* handled correctly.
|
|
||||||
*/
|
*/
|
||||||
int mp_jacobi (mp_int * a, mp_int * n, int *c)
|
int mp_jacobi(const mp_int *a, const mp_int *n, int *c)
|
||||||
{
|
{
|
||||||
mp_int a1, p1;
|
|
||||||
int k, s, r, res;
|
|
||||||
mp_digit residue;
|
|
||||||
|
|
||||||
/* if a < 0 return MP_VAL */
|
/* if a < 0 return MP_VAL */
|
||||||
if (mp_isneg(a) == MP_YES) {
|
if (mp_isneg(a) == MP_YES) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if n <= 0 return MP_VAL */
|
/* if n <= 0 return MP_VAL */
|
||||||
if (mp_cmp_d(n, 0) != MP_GT) {
|
if (mp_cmp_d(n, 0uL) != MP_GT) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* step 1. handle case of a == 0 */
|
return mp_kronecker(a, n, c);
|
||||||
if (mp_iszero (a) == MP_YES) {
|
|
||||||
/* special case of a == 0 and n == 1 */
|
|
||||||
if (mp_cmp_d (n, 1) == MP_EQ) {
|
|
||||||
*c = 1;
|
|
||||||
} else {
|
|
||||||
*c = 0;
|
|
||||||
}
|
|
||||||
return MP_OKAY;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* step 2. if a == 1, return 1 */
|
|
||||||
if (mp_cmp_d (a, 1) == MP_EQ) {
|
|
||||||
*c = 1;
|
|
||||||
return MP_OKAY;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* default */
|
|
||||||
s = 0;
|
|
||||||
|
|
||||||
/* step 3. write a = a1 * 2**k */
|
|
||||||
if ((res = mp_init_copy (&a1, a)) != MP_OKAY) {
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((res = mp_init (&p1)) != MP_OKAY) {
|
|
||||||
goto LBL_A1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* divide out larger power of two */
|
|
||||||
k = mp_cnt_lsb(&a1);
|
|
||||||
if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) {
|
|
||||||
goto LBL_P1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* step 4. if e is even set s=1 */
|
|
||||||
if ((k & 1) == 0) {
|
|
||||||
s = 1;
|
|
||||||
} else {
|
|
||||||
/* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */
|
|
||||||
residue = n->dp[0] & 7;
|
|
||||||
|
|
||||||
if ((residue == 1) || (residue == 7)) {
|
|
||||||
s = 1;
|
|
||||||
} else if ((residue == 3) || (residue == 5)) {
|
|
||||||
s = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* step 5. if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */
|
|
||||||
if ( ((n->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) {
|
|
||||||
s = -s;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* if a1 == 1 we're done */
|
|
||||||
if (mp_cmp_d (&a1, 1) == MP_EQ) {
|
|
||||||
*c = s;
|
|
||||||
} else {
|
|
||||||
/* n1 = n mod a1 */
|
|
||||||
if ((res = mp_mod (n, &a1, &p1)) != MP_OKAY) {
|
|
||||||
goto LBL_P1;
|
|
||||||
}
|
|
||||||
if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) {
|
|
||||||
goto LBL_P1;
|
|
||||||
}
|
|
||||||
*c = s * r;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* done */
|
|
||||||
res = MP_OKAY;
|
|
||||||
LBL_P1:mp_clear (&p1);
|
|
||||||
LBL_A1:mp_clear (&a1);
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_KARATSUBA_MUL_C
|
#ifdef BN_MP_KARATSUBA_MUL_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* c = |a| * |b| using Karatsuba Multiplication using
|
/* c = |a| * |b| using Karatsuba Multiplication using
|
||||||
@ -44,7 +41,7 @@
|
|||||||
* Generally though the overhead of this method doesn't pay off
|
* Generally though the overhead of this method doesn't pay off
|
||||||
* until a certain size (N ~ 80) is reached.
|
* until a certain size (N ~ 80) is reached.
|
||||||
*/
|
*/
|
||||||
int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
|
int mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
|
mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
|
||||||
int B, err;
|
int B, err;
|
||||||
@ -60,7 +57,7 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
|
|
||||||
/* init copy all the temps */
|
/* init copy all the temps */
|
||||||
if (mp_init_size(&x0, B) != MP_OKAY)
|
if (mp_init_size(&x0, B) != MP_OKAY)
|
||||||
goto ERR;
|
goto LBL_ERR;
|
||||||
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
|
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
|
||||||
goto X0;
|
goto X0;
|
||||||
if (mp_init_size(&y0, B) != MP_OKAY)
|
if (mp_init_size(&y0, B) != MP_OKAY)
|
||||||
@ -150,14 +147,21 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
/* Algorithm succeeded set the return code to MP_OKAY */
|
/* Algorithm succeeded set the return code to MP_OKAY */
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
|
|
||||||
X1Y1:mp_clear (&x1y1);
|
X1Y1:
|
||||||
X0Y0:mp_clear (&x0y0);
|
mp_clear(&x1y1);
|
||||||
T1:mp_clear (&t1);
|
X0Y0:
|
||||||
Y1:mp_clear (&y1);
|
mp_clear(&x0y0);
|
||||||
Y0:mp_clear (&y0);
|
T1:
|
||||||
X1:mp_clear (&x1);
|
mp_clear(&t1);
|
||||||
X0:mp_clear (&x0);
|
Y1:
|
||||||
ERR:
|
mp_clear(&y1);
|
||||||
|
Y0:
|
||||||
|
mp_clear(&y0);
|
||||||
|
X1:
|
||||||
|
mp_clear(&x1);
|
||||||
|
X0:
|
||||||
|
mp_clear(&x0);
|
||||||
|
LBL_ERR:
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_KARATSUBA_SQR_C
|
#ifdef BN_MP_KARATSUBA_SQR_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Karatsuba squaring, computes b = a*a using three
|
/* Karatsuba squaring, computes b = a*a using three
|
||||||
@ -22,7 +19,7 @@
|
|||||||
* is essentially the same algorithm but merely
|
* is essentially the same algorithm but merely
|
||||||
* tuned to perform recursive squarings.
|
* tuned to perform recursive squarings.
|
||||||
*/
|
*/
|
||||||
int mp_karatsuba_sqr (mp_int * a, mp_int * b)
|
int mp_karatsuba_sqr(const mp_int *a, mp_int *b)
|
||||||
{
|
{
|
||||||
mp_int x0, x1, t1, t2, x0x0, x1x1;
|
mp_int x0, x1, t1, t2, x0x0, x1x1;
|
||||||
int B, err;
|
int B, err;
|
||||||
@ -37,7 +34,7 @@ int mp_karatsuba_sqr (mp_int * a, mp_int * b)
|
|||||||
|
|
||||||
/* init copy all the temps */
|
/* init copy all the temps */
|
||||||
if (mp_init_size(&x0, B) != MP_OKAY)
|
if (mp_init_size(&x0, B) != MP_OKAY)
|
||||||
goto ERR;
|
goto LBL_ERR;
|
||||||
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
|
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
|
||||||
goto X0;
|
goto X0;
|
||||||
|
|
||||||
@ -105,13 +102,19 @@ int mp_karatsuba_sqr (mp_int * a, mp_int * b)
|
|||||||
|
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
|
|
||||||
X1X1:mp_clear (&x1x1);
|
X1X1:
|
||||||
X0X0:mp_clear (&x0x0);
|
mp_clear(&x1x1);
|
||||||
T2:mp_clear (&t2);
|
X0X0:
|
||||||
T1:mp_clear (&t1);
|
mp_clear(&x0x0);
|
||||||
X1:mp_clear (&x1);
|
T2:
|
||||||
X0:mp_clear (&x0);
|
mp_clear(&t2);
|
||||||
ERR:
|
T1:
|
||||||
|
mp_clear(&t1);
|
||||||
|
X1:
|
||||||
|
mp_clear(&x1);
|
||||||
|
X0:
|
||||||
|
mp_clear(&x0);
|
||||||
|
LBL_ERR:
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
144
bn_mp_kronecker.c
Normal file
144
bn_mp_kronecker.c
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
#include "tommath_private.h"
|
||||||
|
#ifdef BN_MP_KRONECKER_C
|
||||||
|
|
||||||
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
|
*
|
||||||
|
* LibTomMath is a library that provides multiple-precision
|
||||||
|
* integer arithmetic as well as number theoretic functionality.
|
||||||
|
*
|
||||||
|
* The library was designed directly after the MPI library by
|
||||||
|
* Michael Fromberger but has been written from scratch with
|
||||||
|
* additional optimizations in place.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Unlicense
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
Kronecker symbol (a|p)
|
||||||
|
Straightforward implementation of algorithm 1.4.10 in
|
||||||
|
Henri Cohen: "A Course in Computational Algebraic Number Theory"
|
||||||
|
|
||||||
|
@book{cohen2013course,
|
||||||
|
title={A course in computational algebraic number theory},
|
||||||
|
author={Cohen, Henri},
|
||||||
|
volume={138},
|
||||||
|
year={2013},
|
||||||
|
publisher={Springer Science \& Business Media}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
int mp_kronecker(const mp_int *a, const mp_int *p, int *c)
|
||||||
|
{
|
||||||
|
mp_int a1, p1, r;
|
||||||
|
|
||||||
|
int e = MP_OKAY;
|
||||||
|
int v, k;
|
||||||
|
|
||||||
|
static const int table[8] = {0, 1, 0, -1, 0, -1, 0, 1};
|
||||||
|
|
||||||
|
if (mp_iszero(p) != MP_NO) {
|
||||||
|
if ((a->used == 1) && (a->dp[0] == 1u)) {
|
||||||
|
*c = 1;
|
||||||
|
return e;
|
||||||
|
} else {
|
||||||
|
*c = 0;
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((mp_iseven(a) != MP_NO) && (mp_iseven(p) != MP_NO)) {
|
||||||
|
*c = 0;
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((e = mp_init_copy(&a1, a)) != MP_OKAY) {
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
if ((e = mp_init_copy(&p1, p)) != MP_OKAY) {
|
||||||
|
goto LBL_KRON_0;
|
||||||
|
}
|
||||||
|
|
||||||
|
v = mp_cnt_lsb(&p1);
|
||||||
|
if ((e = mp_div_2d(&p1, v, &p1, NULL)) != MP_OKAY) {
|
||||||
|
goto LBL_KRON_1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((v & 0x1) == 0) {
|
||||||
|
k = 1;
|
||||||
|
} else {
|
||||||
|
k = table[a->dp[0] & 7u];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p1.sign == MP_NEG) {
|
||||||
|
p1.sign = MP_ZPOS;
|
||||||
|
if (a1.sign == MP_NEG) {
|
||||||
|
k = -k;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((e = mp_init(&r)) != MP_OKAY) {
|
||||||
|
goto LBL_KRON_1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
if (mp_iszero(&a1) != MP_NO) {
|
||||||
|
if (mp_cmp_d(&p1, 1uL) == MP_EQ) {
|
||||||
|
*c = k;
|
||||||
|
goto LBL_KRON;
|
||||||
|
} else {
|
||||||
|
*c = 0;
|
||||||
|
goto LBL_KRON;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
v = mp_cnt_lsb(&a1);
|
||||||
|
if ((e = mp_div_2d(&a1, v, &a1, NULL)) != MP_OKAY) {
|
||||||
|
goto LBL_KRON;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((v & 0x1) == 1) {
|
||||||
|
k = k * table[p1.dp[0] & 7u];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a1.sign == MP_NEG) {
|
||||||
|
/*
|
||||||
|
* Compute k = (-1)^((a1)*(p1-1)/4) * k
|
||||||
|
* a1.dp[0] + 1 cannot overflow because the MSB
|
||||||
|
* of the type mp_digit is not set by definition
|
||||||
|
*/
|
||||||
|
if (((a1.dp[0] + 1u) & p1.dp[0] & 2u) != 0u) {
|
||||||
|
k = -k;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* compute k = (-1)^((a1-1)*(p1-1)/4) * k */
|
||||||
|
if ((a1.dp[0] & p1.dp[0] & 2u) != 0u) {
|
||||||
|
k = -k;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((e = mp_copy(&a1, &r)) != MP_OKAY) {
|
||||||
|
goto LBL_KRON;
|
||||||
|
}
|
||||||
|
r.sign = MP_ZPOS;
|
||||||
|
if ((e = mp_mod(&p1, &r, &a1)) != MP_OKAY) {
|
||||||
|
goto LBL_KRON;
|
||||||
|
}
|
||||||
|
if ((e = mp_copy(&r, &p1)) != MP_OKAY) {
|
||||||
|
goto LBL_KRON;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LBL_KRON:
|
||||||
|
mp_clear(&r);
|
||||||
|
LBL_KRON_1:
|
||||||
|
mp_clear(&p1);
|
||||||
|
LBL_KRON_0:
|
||||||
|
mp_clear(&a1);
|
||||||
|
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ref: $Format:%D$ */
|
||||||
|
/* git commit: $Format:%H$ */
|
||||||
|
/* commit time: $Format:%ai$ */
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_LCM_C
|
#ifdef BN_MP_LCM_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* computes least common multiple as |a*b|/(a, b) */
|
/* computes least common multiple as |a*b|/(a, b) */
|
||||||
int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
|
int mp_lcm(const mp_int *a, const mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
mp_int t1, t2;
|
mp_int t1, t2;
|
||||||
|
11
bn_mp_lshd.c
11
bn_mp_lshd.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_LSHD_C
|
#ifdef BN_MP_LSHD_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* shift left a certain amount of digits */
|
/* shift left a certain amount of digits */
|
||||||
@ -24,6 +21,10 @@ int mp_lshd (mp_int * a, int b)
|
|||||||
if (b <= 0) {
|
if (b <= 0) {
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
/* no need to shift 0 around */
|
||||||
|
if (mp_iszero(a) == MP_YES) {
|
||||||
|
return MP_OKAY;
|
||||||
|
}
|
||||||
|
|
||||||
/* grow to fit the new digits */
|
/* grow to fit the new digits */
|
||||||
if (a->alloc < (a->used + b)) {
|
if (a->alloc < (a->used + b)) {
|
||||||
|
10
bn_mp_mod.c
10
bn_mp_mod.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_MOD_C
|
#ifdef BN_MP_MOD_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,15 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */
|
/* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */
|
||||||
int
|
int mp_mod(const mp_int *a, const mp_int *b, mp_int *c)
|
||||||
mp_mod (mp_int * a, mp_int * b, mp_int * c)
|
|
||||||
{
|
{
|
||||||
mp_int t;
|
mp_int t;
|
||||||
int res;
|
int res;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_MOD_2D_C
|
#ifdef BN_MP_MOD_2D_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,15 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* calc a value mod 2**b */
|
/* calc a value mod 2**b */
|
||||||
int
|
int mp_mod_2d(const mp_int *a, int b, mp_int *c)
|
||||||
mp_mod_2d (mp_int * a, int b, mp_int * c)
|
|
||||||
{
|
{
|
||||||
int x, res;
|
int x, res;
|
||||||
|
|
||||||
@ -28,7 +24,7 @@ mp_mod_2d (mp_int * a, int b, mp_int * c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if the modulus is larger than the value than return */
|
/* if the modulus is larger than the value than return */
|
||||||
if (b >= (int) (a->used * DIGIT_BIT)) {
|
if (b >= (a->used * DIGIT_BIT)) {
|
||||||
res = mp_copy(a, c);
|
res = mp_copy(a, c);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -44,7 +40,7 @@ mp_mod_2d (mp_int * a, int b, mp_int * c)
|
|||||||
}
|
}
|
||||||
/* clear the digit that is not completely outside/inside the modulus */
|
/* clear the digit that is not completely outside/inside the modulus */
|
||||||
c->dp[b / DIGIT_BIT] &=
|
c->dp[b / DIGIT_BIT] &=
|
||||||
(mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
|
((mp_digit)1 << (mp_digit)(b % DIGIT_BIT)) - (mp_digit)1;
|
||||||
mp_clamp(c);
|
mp_clamp(c);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_MOD_D_C
|
#ifdef BN_MP_MOD_D_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,10 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int
|
int mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c)
|
||||||
mp_mod_d (mp_int * a, mp_digit b, mp_digit * c)
|
|
||||||
{
|
{
|
||||||
return mp_div_d(a, b, NULL, c);
|
return mp_div_d(a, b, NULL, c);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
|
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -21,7 +18,7 @@
|
|||||||
* The method is slightly modified to shift B unconditionally upto just under
|
* The method is slightly modified to shift B unconditionally upto just under
|
||||||
* the leading bit of b. This saves alot of multiple precision shifting.
|
* the leading bit of b. This saves alot of multiple precision shifting.
|
||||||
*/
|
*/
|
||||||
int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
|
int mp_montgomery_calc_normalization(mp_int *a, const mp_int *b)
|
||||||
{
|
{
|
||||||
int x, bits, res;
|
int x, bits, res;
|
||||||
|
|
||||||
@ -33,7 +30,7 @@ int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mp_set(a, 1);
|
mp_set(a, 1uL);
|
||||||
bits = 1;
|
bits = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_MONTGOMERY_REDUCE_C
|
#ifdef BN_MP_MONTGOMERY_REDUCE_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,15 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* computes xR**-1 == x (mod N) via Montgomery Reduction */
|
/* computes xR**-1 == x (mod N) via Montgomery Reduction */
|
||||||
int
|
int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
|
||||||
mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
|
||||||
{
|
{
|
||||||
int ix, res, digs;
|
int ix, res, digs;
|
||||||
mp_digit mu;
|
mp_digit mu;
|
||||||
@ -29,9 +25,10 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
|||||||
* are fixed up in the inner loop.
|
* are fixed up in the inner loop.
|
||||||
*/
|
*/
|
||||||
digs = (n->used * 2) + 1;
|
digs = (n->used * 2) + 1;
|
||||||
if ((digs < MP_WARRAY) &&
|
if ((digs < (int)MP_WARRAY) &&
|
||||||
|
(x->used <= (int)MP_WARRAY) &&
|
||||||
(n->used <
|
(n->used <
|
||||||
(1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
(int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
|
||||||
return fast_mp_montgomery_reduce(x, n, rho);
|
return fast_mp_montgomery_reduce(x, n, rho);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,16 +73,16 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
|||||||
(mp_word)u + (mp_word)*tmpx;
|
(mp_word)u + (mp_word)*tmpx;
|
||||||
|
|
||||||
/* get carry */
|
/* get carry */
|
||||||
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
|
u = (mp_digit)(r >> (mp_word)DIGIT_BIT);
|
||||||
|
|
||||||
/* fix digit */
|
/* fix digit */
|
||||||
*tmpx++ = (mp_digit)(r & ((mp_word) MP_MASK));
|
*tmpx++ = (mp_digit)(r & (mp_word)MP_MASK);
|
||||||
}
|
}
|
||||||
/* At this point the ix'th digit of x should be zero */
|
/* At this point the ix'th digit of x should be zero */
|
||||||
|
|
||||||
|
|
||||||
/* propagate carries upwards as required*/
|
/* propagate carries upwards as required*/
|
||||||
while (u != 0) {
|
while (u != 0u) {
|
||||||
*tmpx += u;
|
*tmpx += u;
|
||||||
u = *tmpx >> DIGIT_BIT;
|
u = *tmpx >> DIGIT_BIT;
|
||||||
*tmpx++ &= MP_MASK;
|
*tmpx++ &= MP_MASK;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,15 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* setups the montgomery reduction stuff */
|
/* setups the montgomery reduction stuff */
|
||||||
int
|
int mp_montgomery_setup(const mp_int *n, mp_digit *rho)
|
||||||
mp_montgomery_setup (mp_int * n, mp_digit * rho)
|
|
||||||
{
|
{
|
||||||
mp_digit x, b;
|
mp_digit x, b;
|
||||||
|
|
||||||
@ -31,24 +27,24 @@ mp_montgomery_setup (mp_int * n, mp_digit * rho)
|
|||||||
*/
|
*/
|
||||||
b = n->dp[0];
|
b = n->dp[0];
|
||||||
|
|
||||||
if ((b & 1) == 0) {
|
if ((b & 1u) == 0u) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
|
x = (((b + 2u) & 4u) << 1) + b; /* here x*a==1 mod 2**4 */
|
||||||
x *= 2 - (b * x); /* here x*a==1 mod 2**8 */
|
x *= 2u - (b * x); /* here x*a==1 mod 2**8 */
|
||||||
#if !defined(MP_8BIT)
|
#if !defined(MP_8BIT)
|
||||||
x *= 2 - (b * x); /* here x*a==1 mod 2**16 */
|
x *= 2u - (b * x); /* here x*a==1 mod 2**16 */
|
||||||
#endif
|
#endif
|
||||||
#if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
|
#if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
|
||||||
x *= 2 - (b * x); /* here x*a==1 mod 2**32 */
|
x *= 2u - (b * x); /* here x*a==1 mod 2**32 */
|
||||||
#endif
|
#endif
|
||||||
#ifdef MP_64BIT
|
#ifdef MP_64BIT
|
||||||
x *= 2 - (b * x); /* here x*a==1 mod 2**64 */
|
x *= 2u - (b * x); /* here x*a==1 mod 2**64 */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* rho = -1/m mod b */
|
/* rho = -1/m mod b */
|
||||||
*rho = (mp_digit)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;
|
*rho = (mp_digit)(((mp_word)1 << (mp_word)DIGIT_BIT) - x) & MP_MASK;
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
13
bn_mp_mul.c
13
bn_mp_mul.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_MUL_C
|
#ifdef BN_MP_MUL_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* high level multiplication (handles sign) */
|
/* high level multiplication (handles sign) */
|
||||||
int mp_mul (mp_int * a, mp_int * b, mp_int * c)
|
int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
int res, neg;
|
int res, neg;
|
||||||
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
|
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
|
||||||
@ -43,9 +40,9 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
int digs = a->used + b->used + 1;
|
int digs = a->used + b->used + 1;
|
||||||
|
|
||||||
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
||||||
if ((digs < MP_WARRAY) &&
|
if ((digs < (int)MP_WARRAY) &&
|
||||||
(MIN(a->used, b->used) <=
|
(MIN(a->used, b->used) <=
|
||||||
(1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
(int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
|
||||||
res = fast_s_mp_mul_digs(a, b, c, digs);
|
res = fast_s_mp_mul_digs(a, b, c, digs);
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_MUL_2_C
|
#ifdef BN_MP_MUL_2_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* b = a*2 */
|
/* b = a*2 */
|
||||||
int mp_mul_2(mp_int * a, mp_int * b)
|
int mp_mul_2(const mp_int *a, mp_int *b)
|
||||||
{
|
{
|
||||||
int x, res, oldused;
|
int x, res, oldused;
|
||||||
|
|
||||||
@ -46,10 +43,10 @@ int mp_mul_2(mp_int * a, mp_int * b)
|
|||||||
/* get what will be the *next* carry bit from the
|
/* get what will be the *next* carry bit from the
|
||||||
* MSB of the current digit
|
* MSB of the current digit
|
||||||
*/
|
*/
|
||||||
rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
|
rr = *tmpa >> (mp_digit)(DIGIT_BIT - 1);
|
||||||
|
|
||||||
/* now shift up this digit, add in the carry [from the previous] */
|
/* now shift up this digit, add in the carry [from the previous] */
|
||||||
*tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
|
*tmpb++ = ((*tmpa++ << 1uL) | r) & MP_MASK;
|
||||||
|
|
||||||
/* copy the carry that would be from the source
|
/* copy the carry that would be from the source
|
||||||
* digit into the next iteration
|
* digit into the next iteration
|
||||||
@ -58,7 +55,7 @@ int mp_mul_2(mp_int * a, mp_int * b)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* new leading digit? */
|
/* new leading digit? */
|
||||||
if (r != 0) {
|
if (r != 0u) {
|
||||||
/* add a MSB which is always 1 at this point */
|
/* add a MSB which is always 1 at this point */
|
||||||
*tmpb = 1;
|
*tmpb = 1;
|
||||||
++(b->used);
|
++(b->used);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_MUL_2D_C
|
#ifdef BN_MP_MUL_2D_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* shift left by a certain bit count */
|
/* shift left by a certain bit count */
|
||||||
int mp_mul_2d (mp_int * a, int b, mp_int * c)
|
int mp_mul_2d(const mp_int *a, int b, mp_int *c)
|
||||||
{
|
{
|
||||||
mp_digit d;
|
mp_digit d;
|
||||||
int res;
|
int res;
|
||||||
@ -28,14 +25,14 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c->alloc < (int)(c->used + (b / DIGIT_BIT) + 1)) {
|
if (c->alloc < (c->used + (b / DIGIT_BIT) + 1)) {
|
||||||
if ((res = mp_grow(c, c->used + (b / DIGIT_BIT) + 1)) != MP_OKAY) {
|
if ((res = mp_grow(c, c->used + (b / DIGIT_BIT) + 1)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* shift by as many digits in the bit count */
|
/* shift by as many digits in the bit count */
|
||||||
if (b >= (int)DIGIT_BIT) {
|
if (b >= DIGIT_BIT) {
|
||||||
if ((res = mp_lshd(c, b / DIGIT_BIT)) != MP_OKAY) {
|
if ((res = mp_lshd(c, b / DIGIT_BIT)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -43,15 +40,15 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
|
|||||||
|
|
||||||
/* shift any bit count < DIGIT_BIT */
|
/* shift any bit count < DIGIT_BIT */
|
||||||
d = (mp_digit)(b % DIGIT_BIT);
|
d = (mp_digit)(b % DIGIT_BIT);
|
||||||
if (d != 0) {
|
if (d != 0u) {
|
||||||
mp_digit *tmpc, shift, mask, r, rr;
|
mp_digit *tmpc, shift, mask, r, rr;
|
||||||
int x;
|
int x;
|
||||||
|
|
||||||
/* bitmask for carries */
|
/* bitmask for carries */
|
||||||
mask = (((mp_digit)1) << d) - 1;
|
mask = ((mp_digit)1 << d) - (mp_digit)1;
|
||||||
|
|
||||||
/* shift for msbs */
|
/* shift for msbs */
|
||||||
shift = DIGIT_BIT - d;
|
shift = (mp_digit)DIGIT_BIT - d;
|
||||||
|
|
||||||
/* alias */
|
/* alias */
|
||||||
tmpc = c->dp;
|
tmpc = c->dp;
|
||||||
@ -71,7 +68,7 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* set final carry */
|
/* set final carry */
|
||||||
if (r != 0) {
|
if (r != 0u) {
|
||||||
c->dp[(c->used)++] = r;
|
c->dp[(c->used)++] = r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_MUL_D_C
|
#ifdef BN_MP_MUL_D_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,15 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* multiply by a digit */
|
/* multiply by a digit */
|
||||||
int
|
int mp_mul_d(const mp_int *a, mp_digit b, mp_int *c)
|
||||||
mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
|
|
||||||
{
|
{
|
||||||
mp_digit u, *tmpa, *tmpc;
|
mp_digit u, *tmpa, *tmpc;
|
||||||
mp_word r;
|
mp_word r;
|
||||||
@ -51,10 +47,10 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
|
|||||||
r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b);
|
r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b);
|
||||||
|
|
||||||
/* mask off higher bits to get a single digit */
|
/* mask off higher bits to get a single digit */
|
||||||
*tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));
|
*tmpc++ = (mp_digit)(r & (mp_word)MP_MASK);
|
||||||
|
|
||||||
/* send carry into next iteration */
|
/* send carry into next iteration */
|
||||||
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
|
u = (mp_digit)(r >> (mp_word)DIGIT_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* store final carry [if any] and increment ix offset */
|
/* store final carry [if any] and increment ix offset */
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_MULMOD_C
|
#ifdef BN_MP_MULMOD_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* d = a * b (mod c) */
|
/* d = a * b (mod c) */
|
||||||
int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
int mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *c, mp_int *d)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
mp_int t;
|
mp_int t;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_N_ROOT_C
|
#ifdef BN_MP_N_ROOT_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,16 +9,13 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* wrapper function for mp_n_root_ex()
|
/* wrapper function for mp_n_root_ex()
|
||||||
* computes c = (a)**(1/b) such that (c)**b <= a and (c+1)**b > a
|
* computes c = (a)**(1/b) such that (c)**b <= a and (c+1)**b > a
|
||||||
*/
|
*/
|
||||||
int mp_n_root (mp_int * a, mp_digit b, mp_int * c)
|
int mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
|
||||||
{
|
{
|
||||||
return mp_n_root_ex(a, b, c, 0);
|
return mp_n_root_ex(a, b, c, 0);
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_N_ROOT_EX_C
|
#ifdef BN_MP_N_ROOT_EX_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* find the n'th root of an integer
|
/* find the n'th root of an integer
|
||||||
@ -25,13 +22,13 @@
|
|||||||
* each step involves a fair bit. This is not meant to
|
* each step involves a fair bit. This is not meant to
|
||||||
* find huge roots [square and cube, etc].
|
* find huge roots [square and cube, etc].
|
||||||
*/
|
*/
|
||||||
int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
int mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
|
||||||
{
|
{
|
||||||
mp_int t1, t2, t3;
|
mp_int t1, t2, t3, a_;
|
||||||
int res, neg;
|
int res;
|
||||||
|
|
||||||
/* input must be positive if b is even */
|
/* input must be positive if b is even */
|
||||||
if (((b & 1) == 0) && (a->sign == MP_NEG)) {
|
if (((b & 1u) == 0u) && (a->sign == MP_NEG)) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,11 +45,11 @@ int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if a is negative fudge the sign but keep track */
|
/* if a is negative fudge the sign but keep track */
|
||||||
neg = a->sign;
|
a_ = *a;
|
||||||
a->sign = MP_ZPOS;
|
a_.sign = MP_ZPOS;
|
||||||
|
|
||||||
/* t2 = 2 */
|
/* t2 = 2 */
|
||||||
mp_set (&t2, 2);
|
mp_set(&t2, 2uL);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
/* t1 = t2 */
|
/* t1 = t2 */
|
||||||
@ -63,7 +60,7 @@ int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
|||||||
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
|
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
|
||||||
|
|
||||||
/* t3 = t1**(b-1) */
|
/* t3 = t1**(b-1) */
|
||||||
if ((res = mp_expt_d_ex (&t1, b - 1, &t3, fast)) != MP_OKAY) {
|
if ((res = mp_expt_d_ex(&t1, b - 1u, &t3, fast)) != MP_OKAY) {
|
||||||
goto LBL_T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +71,7 @@ int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* t2 = t1**b - a */
|
/* t2 = t1**b - a */
|
||||||
if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) {
|
if ((res = mp_sub(&t2, &a_, &t2)) != MP_OKAY) {
|
||||||
goto LBL_T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,8 +97,8 @@ int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
|||||||
goto LBL_T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mp_cmp (&t2, a) == MP_GT) {
|
if (mp_cmp(&t2, &a_) == MP_GT) {
|
||||||
if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) {
|
if ((res = mp_sub_d(&t1, 1uL, &t1)) != MP_OKAY) {
|
||||||
goto LBL_T3;
|
goto LBL_T3;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -109,20 +106,20 @@ int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* reset the sign of a first */
|
|
||||||
a->sign = neg;
|
|
||||||
|
|
||||||
/* set the result */
|
/* set the result */
|
||||||
mp_exch(&t1, c);
|
mp_exch(&t1, c);
|
||||||
|
|
||||||
/* set the sign of the result */
|
/* set the sign of the result */
|
||||||
c->sign = neg;
|
c->sign = a->sign;
|
||||||
|
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
|
|
||||||
LBL_T3:mp_clear (&t3);
|
LBL_T3:
|
||||||
LBL_T2:mp_clear (&t2);
|
mp_clear(&t3);
|
||||||
LBL_T1:mp_clear (&t1);
|
LBL_T2:
|
||||||
|
mp_clear(&t2);
|
||||||
|
LBL_T1:
|
||||||
|
mp_clear(&t1);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_NEG_C
|
#ifdef BN_MP_NEG_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* b = -a */
|
/* b = -a */
|
||||||
int mp_neg (mp_int * a, mp_int * b)
|
int mp_neg(const mp_int *a, mp_int *b)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
if (a != b) {
|
if (a != b) {
|
||||||
|
12
bn_mp_or.c
12
bn_mp_or.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_OR_C
|
#ifdef BN_MP_OR_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,17 +9,15 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* OR two ints together */
|
/* OR two ints together */
|
||||||
int mp_or (mp_int * a, mp_int * b, mp_int * c)
|
int mp_or(const mp_int *a, const mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
int res, ix, px;
|
int res, ix, px;
|
||||||
mp_int t, *x;
|
mp_int t;
|
||||||
|
const mp_int *x;
|
||||||
|
|
||||||
if (a->used > b->used) {
|
if (a->used > b->used) {
|
||||||
if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
|
if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_PRIME_FERMAT_C
|
#ifdef BN_MP_PRIME_FERMAT_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* performs one Fermat test.
|
/* performs one Fermat test.
|
||||||
@ -23,7 +20,7 @@
|
|||||||
*
|
*
|
||||||
* Sets result to 1 if the congruence holds, or zero otherwise.
|
* Sets result to 1 if the congruence holds, or zero otherwise.
|
||||||
*/
|
*/
|
||||||
int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
|
int mp_prime_fermat(const mp_int *a, const mp_int *b, int *result)
|
||||||
{
|
{
|
||||||
mp_int t;
|
mp_int t;
|
||||||
int err;
|
int err;
|
||||||
@ -32,7 +29,7 @@ int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
|
|||||||
*result = MP_NO;
|
*result = MP_NO;
|
||||||
|
|
||||||
/* ensure b > 1 */
|
/* ensure b > 1 */
|
||||||
if (mp_cmp_d(b, 1) != MP_GT) {
|
if (mp_cmp_d(b, 1uL) != MP_GT) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +49,8 @@ int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
LBL_T:mp_clear (&t);
|
LBL_T:
|
||||||
|
mp_clear(&t);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
198
bn_mp_prime_frobenius_underwood.c
Normal file
198
bn_mp_prime_frobenius_underwood.c
Normal file
@ -0,0 +1,198 @@
|
|||||||
|
#include "tommath_private.h"
|
||||||
|
#ifdef BN_MP_PRIME_FROBENIUS_UNDERWOOD_C
|
||||||
|
|
||||||
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
|
*
|
||||||
|
* LibTomMath is a library that provides multiple-precision
|
||||||
|
* integer arithmetic as well as number theoretic functionality.
|
||||||
|
*
|
||||||
|
* The library was designed directly after the MPI library by
|
||||||
|
* Michael Fromberger but has been written from scratch with
|
||||||
|
* additional optimizations in place.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Unlicense
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* See file bn_mp_prime_is_prime.c or the documentation in doc/bn.tex for the details
|
||||||
|
*/
|
||||||
|
#ifndef LTM_USE_FIPS_ONLY
|
||||||
|
|
||||||
|
#ifdef MP_8BIT
|
||||||
|
/*
|
||||||
|
* floor of positive solution of
|
||||||
|
* (2^16)-1 = (a+4)*(2*a+5)
|
||||||
|
* TODO: Both values are smaller than N^(1/4), would have to use a bigint
|
||||||
|
* for a instead but any a biger than about 120 are already so rare that
|
||||||
|
* it is possible to ignore them and still get enough pseudoprimes.
|
||||||
|
* But it is still a restriction of the set of available pseudoprimes
|
||||||
|
* which makes this implementation less secure if used stand-alone.
|
||||||
|
*/
|
||||||
|
#define LTM_FROBENIUS_UNDERWOOD_A 177
|
||||||
|
#else
|
||||||
|
#define LTM_FROBENIUS_UNDERWOOD_A 32764
|
||||||
|
#endif
|
||||||
|
int mp_prime_frobenius_underwood(const mp_int *N, int *result)
|
||||||
|
{
|
||||||
|
mp_int T1z, T2z, Np1z, sz, tz;
|
||||||
|
|
||||||
|
int a, ap2, length, i, j, isset;
|
||||||
|
int e;
|
||||||
|
|
||||||
|
*result = MP_NO;
|
||||||
|
|
||||||
|
if ((e = mp_init_multi(&T1z, &T2z, &Np1z, &sz, &tz, NULL)) != MP_OKAY) {
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (a = 0; a < LTM_FROBENIUS_UNDERWOOD_A; a++) {
|
||||||
|
/* TODO: That's ugly! No, really, it is! */
|
||||||
|
if ((a==2) || (a==4) || (a==7) || (a==8) || (a==10) ||
|
||||||
|
(a==14) || (a==18) || (a==23) || (a==26) || (a==28)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
/* (32764^2 - 4) < 2^31, no bigint for >MP_8BIT needed) */
|
||||||
|
if ((e = mp_set_long(&T1z, (unsigned long)a)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((e = mp_sqr(&T1z, &T1z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((e = mp_sub_d(&T1z, 4uL, &T1z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((e = mp_kronecker(&T1z, N, &j)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j == -1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j == 0) {
|
||||||
|
/* composite */
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Tell it a composite and set return value accordingly */
|
||||||
|
if (a >= LTM_FROBENIUS_UNDERWOOD_A) {
|
||||||
|
e = MP_ITER;
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
/* Composite if N and (a+4)*(2*a+5) are not coprime */
|
||||||
|
if ((e = mp_set_long(&T1z, (unsigned long)((a+4)*((2*a)+5)))) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((e = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!((T1z.used == 1) && (T1z.dp[0] == 1u))) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
ap2 = a + 2;
|
||||||
|
if ((e = mp_add_d(N, 1uL, &Np1z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
mp_set(&sz, 1uL);
|
||||||
|
mp_set(&tz, 2uL);
|
||||||
|
length = mp_count_bits(&Np1z);
|
||||||
|
|
||||||
|
for (i = length - 2; i >= 0; i--) {
|
||||||
|
/*
|
||||||
|
* temp = (sz*(a*sz+2*tz))%N;
|
||||||
|
* tz = ((tz-sz)*(tz+sz))%N;
|
||||||
|
* sz = temp;
|
||||||
|
*/
|
||||||
|
if ((e = mp_mul_2(&tz, &T2z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* a = 0 at about 50% of the cases (non-square and odd input) */
|
||||||
|
if (a != 0) {
|
||||||
|
if ((e = mp_mul_d(&sz, (mp_digit)a, &T1z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_add(&T1z, &T2z, &T2z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((e = mp_mul(&T2z, &sz, &T1z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_sub(&tz, &sz, &T2z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_add(&sz, &tz, &sz)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mul(&sz, &T2z, &tz)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mod(&tz, N, &tz)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mod(&T1z, N, &sz)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
if ((isset = mp_get_bit(&Np1z, i)) == MP_VAL) {
|
||||||
|
e = isset;
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
if (isset == MP_YES) {
|
||||||
|
/*
|
||||||
|
* temp = (a+2) * sz + tz
|
||||||
|
* tz = 2 * tz - sz
|
||||||
|
* sz = temp
|
||||||
|
*/
|
||||||
|
if (a == 0) {
|
||||||
|
if ((e = mp_mul_2(&sz, &T1z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ((e = mp_mul_d(&sz, (mp_digit)ap2, &T1z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((e = mp_add(&T1z, &tz, &T1z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mul_2(&tz, &T2z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_sub(&T2z, &sz, &tz)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
mp_exch(&sz, &T1z);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((e = mp_set_long(&T1z, (unsigned long)((2 * a) + 5))) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mod(&T1z, N, &T1z)) != MP_OKAY) {
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
if ((mp_iszero(&sz) != MP_NO) && (mp_cmp(&tz, &T1z) == MP_EQ)) {
|
||||||
|
*result = MP_YES;
|
||||||
|
goto LBL_FU_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
LBL_FU_ERR:
|
||||||
|
mp_clear_multi(&tz, &sz, &Np1z, &T2z, &T1z, NULL);
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ref: $Format:%D$ */
|
||||||
|
/* git commit: $Format:%H$ */
|
||||||
|
/* commit time: $Format:%ai$ */
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_PRIME_IS_DIVISIBLE_C
|
#ifdef BN_MP_PRIME_IS_DIVISIBLE_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* determines if an integers is divisible by one
|
/* determines if an integers is divisible by one
|
||||||
@ -20,7 +17,7 @@
|
|||||||
*
|
*
|
||||||
* sets result to 0 if not, 1 if yes
|
* sets result to 0 if not, 1 if yes
|
||||||
*/
|
*/
|
||||||
int mp_prime_is_divisible (mp_int * a, int *result)
|
int mp_prime_is_divisible(const mp_int *a, int *result)
|
||||||
{
|
{
|
||||||
int err, ix;
|
int err, ix;
|
||||||
mp_digit res;
|
mp_digit res;
|
||||||
@ -35,7 +32,7 @@ int mp_prime_is_divisible (mp_int * a, int *result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* is the residue zero? */
|
/* is the residue zero? */
|
||||||
if (res == 0) {
|
if (res == 0u) {
|
||||||
*result = MP_YES;
|
*result = MP_YES;
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_PRIME_IS_PRIME_C
|
#ifdef BN_MP_PRIME_IS_PRIME_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,39 +9,72 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* performs a variable number of rounds of Miller-Rabin
|
/* portable integer log of two with small footprint */
|
||||||
*
|
static unsigned int s_floor_ilog2(int value)
|
||||||
* Probability of error after t rounds is no more than
|
{
|
||||||
|
unsigned int r = 0;
|
||||||
|
while ((value >>= 1) != 0) {
|
||||||
|
r++;
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
*
|
|
||||||
* Sets result to 1 if probably prime, 0 otherwise
|
int mp_prime_is_prime(const mp_int *a, int t, int *result)
|
||||||
*/
|
|
||||||
int mp_prime_is_prime (mp_int * a, int t, int *result)
|
|
||||||
{
|
{
|
||||||
mp_int b;
|
mp_int b;
|
||||||
int ix, err, res;
|
int ix, err, res, p_max = 0, size_a, len;
|
||||||
|
unsigned int fips_rand, mask;
|
||||||
|
|
||||||
/* default to no */
|
/* default to no */
|
||||||
*result = MP_NO;
|
*result = MP_NO;
|
||||||
|
|
||||||
/* valid value of t? */
|
/* valid value of t? */
|
||||||
if ((t <= 0) || (t > PRIME_SIZE)) {
|
if (t > PRIME_SIZE) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Some shortcuts */
|
||||||
|
/* N > 3 */
|
||||||
|
if (a->used == 1) {
|
||||||
|
if ((a->dp[0] == 0u) || (a->dp[0] == 1u)) {
|
||||||
|
*result = 0;
|
||||||
|
return MP_OKAY;
|
||||||
|
}
|
||||||
|
if (a->dp[0] == 2u) {
|
||||||
|
*result = 1;
|
||||||
|
return MP_OKAY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* N must be odd */
|
||||||
|
if (mp_iseven(a) == MP_YES) {
|
||||||
|
return MP_OKAY;
|
||||||
|
}
|
||||||
|
/* N is not a perfect square: floor(sqrt(N))^2 != N */
|
||||||
|
if ((err = mp_is_square(a, &res)) != MP_OKAY) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
if (res != 0) {
|
||||||
|
return MP_OKAY;
|
||||||
|
}
|
||||||
|
|
||||||
/* is the input equal to one of the primes in the table? */
|
/* is the input equal to one of the primes in the table? */
|
||||||
for (ix = 0; ix < PRIME_SIZE; ix++) {
|
for (ix = 0; ix < PRIME_SIZE; ix++) {
|
||||||
if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) {
|
if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) {
|
||||||
*result = 1;
|
*result = MP_YES;
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#ifdef MP_8BIT
|
||||||
|
/* The search in the loop above was exhaustive in this case */
|
||||||
|
if ((a->used == 1) && (PRIME_SIZE >= 31)) {
|
||||||
|
return MP_OKAY;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* first perform trial division */
|
/* first perform trial division */
|
||||||
if ((err = mp_prime_is_divisible(a, &res)) != MP_OKAY) {
|
if ((err = mp_prime_is_divisible(a, &res)) != MP_OKAY) {
|
||||||
@ -53,29 +86,283 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
|
|||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now perform the miller-rabin rounds */
|
/*
|
||||||
if ((err = mp_init (&b)) != MP_OKAY) {
|
Run the Miller-Rabin test with base 2 for the BPSW test.
|
||||||
|
*/
|
||||||
|
if ((err = mp_init_set(&b, 2uL)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ix = 0; ix < t; ix++) {
|
|
||||||
/* set the prime */
|
|
||||||
mp_set (&b, ltm_prime_tab[ix]);
|
|
||||||
|
|
||||||
if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
|
if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
|
||||||
goto LBL_B;
|
goto LBL_B;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res == MP_NO) {
|
if (res == MP_NO) {
|
||||||
goto LBL_B;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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 (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 */
|
/* passed the test */
|
||||||
*result = MP_YES;
|
*result = MP_YES;
|
||||||
LBL_B:mp_clear (&b);
|
LBL_B:
|
||||||
|
mp_clear(&b);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* ref: $Format:%D$ */
|
/* ref: $Format:%D$ */
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_PRIME_MILLER_RABIN_C
|
#ifdef BN_MP_PRIME_MILLER_RABIN_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Miller-Rabin test of "a" to the base of "b" as described in
|
/* Miller-Rabin test of "a" to the base of "b" as described in
|
||||||
@ -22,7 +19,7 @@
|
|||||||
* Randomly the chance of error is no more than 1/4 and often
|
* Randomly the chance of error is no more than 1/4 and often
|
||||||
* very much lower.
|
* very much lower.
|
||||||
*/
|
*/
|
||||||
int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
int mp_prime_miller_rabin(const mp_int *a, const mp_int *b, int *result)
|
||||||
{
|
{
|
||||||
mp_int n1, y, r;
|
mp_int n1, y, r;
|
||||||
int s, j, err;
|
int s, j, err;
|
||||||
@ -31,7 +28,7 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
|||||||
*result = MP_NO;
|
*result = MP_NO;
|
||||||
|
|
||||||
/* ensure b > 1 */
|
/* ensure b > 1 */
|
||||||
if (mp_cmp_d(b, 1) != MP_GT) {
|
if (mp_cmp_d(b, 1uL) != MP_GT) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +36,7 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
|||||||
if ((err = mp_init_copy(&n1, a)) != MP_OKAY) {
|
if ((err = mp_init_copy(&n1, a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) {
|
if ((err = mp_sub_d(&n1, 1uL, &n1)) != MP_OKAY) {
|
||||||
goto LBL_N1;
|
goto LBL_N1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +64,7 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if y != 1 and y != n1 do */
|
/* if y != 1 and y != n1 do */
|
||||||
if ((mp_cmp_d (&y, 1) != MP_EQ) && (mp_cmp (&y, &n1) != MP_EQ)) {
|
if ((mp_cmp_d(&y, 1uL) != MP_EQ) && (mp_cmp(&y, &n1) != MP_EQ)) {
|
||||||
j = 1;
|
j = 1;
|
||||||
/* while j <= s-1 and y != n1 */
|
/* while j <= s-1 and y != n1 */
|
||||||
while ((j <= (s - 1)) && (mp_cmp(&y, &n1) != MP_EQ)) {
|
while ((j <= (s - 1)) && (mp_cmp(&y, &n1) != MP_EQ)) {
|
||||||
@ -76,7 +73,7 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if y == 1 then composite */
|
/* if y == 1 then composite */
|
||||||
if (mp_cmp_d (&y, 1) == MP_EQ) {
|
if (mp_cmp_d(&y, 1uL) == MP_EQ) {
|
||||||
goto LBL_Y;
|
goto LBL_Y;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,9 +88,12 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
|||||||
|
|
||||||
/* probably prime now */
|
/* probably prime now */
|
||||||
*result = MP_YES;
|
*result = MP_YES;
|
||||||
LBL_Y:mp_clear (&y);
|
LBL_Y:
|
||||||
LBL_R:mp_clear (&r);
|
mp_clear(&y);
|
||||||
LBL_N1:mp_clear (&n1);
|
LBL_R:
|
||||||
|
mp_clear(&r);
|
||||||
|
LBL_N1:
|
||||||
|
mp_clear(&n1);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_PRIME_NEXT_PRIME_C
|
#ifdef BN_MP_PRIME_NEXT_PRIME_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* finds the next prime after the number "a" using "t" trials
|
/* finds the next prime after the number "a" using "t" trials
|
||||||
@ -26,11 +23,6 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
mp_digit res_tab[PRIME_SIZE], step, kstep;
|
mp_digit res_tab[PRIME_SIZE], step, kstep;
|
||||||
mp_int b;
|
mp_int b;
|
||||||
|
|
||||||
/* ensure t is valid */
|
|
||||||
if ((t <= 0) || (t > PRIME_SIZE)) {
|
|
||||||
return MP_VAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* force positive */
|
/* force positive */
|
||||||
a->sign = MP_ZPOS;
|
a->sign = MP_ZPOS;
|
||||||
|
|
||||||
@ -46,10 +38,10 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
* however, the prime must be
|
* however, the prime must be
|
||||||
* congruent to 3 mod 4
|
* congruent to 3 mod 4
|
||||||
*/
|
*/
|
||||||
if ((ltm_prime_tab[x + 1] & 3) != 3) {
|
if ((ltm_prime_tab[x + 1] & 3u) != 3u) {
|
||||||
/* scan upwards for a prime congruent to 3 mod 4 */
|
/* scan upwards for a prime congruent to 3 mod 4 */
|
||||||
for (y = x + 1; y < PRIME_SIZE; y++) {
|
for (y = x + 1; y < PRIME_SIZE; y++) {
|
||||||
if ((ltm_prime_tab[y] & 3) == 3) {
|
if ((ltm_prime_tab[y] & 3u) == 3u) {
|
||||||
mp_set(a, ltm_prime_tab[y]);
|
mp_set(a, ltm_prime_tab[y]);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
@ -62,8 +54,8 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* at this point a maybe 1 */
|
/* at this point a maybe 1 */
|
||||||
if (mp_cmp_d(a, 1) == MP_EQ) {
|
if (mp_cmp_d(a, 1uL) == MP_EQ) {
|
||||||
mp_set(a, 2);
|
mp_set(a, 2uL);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
/* fall through to the sieve */
|
/* fall through to the sieve */
|
||||||
@ -80,13 +72,15 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
|
|
||||||
if (bbs_style == 1) {
|
if (bbs_style == 1) {
|
||||||
/* if a mod 4 != 3 subtract the correct value to make it so */
|
/* if a mod 4 != 3 subtract the correct value to make it so */
|
||||||
if ((a->dp[0] & 3) != 3) {
|
if ((a->dp[0] & 3u) != 3u) {
|
||||||
if ((err = mp_sub_d(a, (a->dp[0] & 3) + 1, a)) != MP_OKAY) { return err; };
|
if ((err = mp_sub_d(a, (a->dp[0] & 3u) + 1u, a)) != MP_OKAY) {
|
||||||
|
return err;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (mp_iseven(a) == MP_YES) {
|
if (mp_iseven(a) == MP_YES) {
|
||||||
/* force odd */
|
/* force odd */
|
||||||
if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) {
|
if ((err = mp_sub_d(a, 1uL, a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,11 +119,11 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* set flag if zero */
|
/* set flag if zero */
|
||||||
if (res_tab[x] == 0) {
|
if (res_tab[x] == 0u) {
|
||||||
y = 1;
|
y = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while ((y == 1) && (step < ((((mp_digit)1) << DIGIT_BIT) - kstep)));
|
} while ((y == 1) && (step < (((mp_digit)1 << DIGIT_BIT) - kstep)));
|
||||||
|
|
||||||
/* add the step */
|
/* add the step */
|
||||||
if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
|
if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
|
||||||
@ -137,21 +131,13 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* if didn't pass sieve and step == MAX then skip test */
|
/* if didn't pass sieve and step == MAX then skip test */
|
||||||
if ((y == 1) && (step >= ((((mp_digit)1) << DIGIT_BIT) - kstep))) {
|
if ((y == 1) && (step >= (((mp_digit)1 << DIGIT_BIT) - kstep))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* is this prime? */
|
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
|
||||||
for (x = 0; x < t; x++) {
|
|
||||||
mp_set(&b, ltm_prime_tab[x]);
|
|
||||||
if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
|
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
if (res == MP_NO) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res == MP_YES) {
|
if (res == MP_YES) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_PRIME_RABIN_MILLER_TRIALS_C
|
#ifdef BN_MP_PRIME_RABIN_MILLER_TRIALS_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,27 +9,31 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
static const struct {
|
static const struct {
|
||||||
int k, t;
|
int k, t;
|
||||||
} sizes[] = {
|
} sizes[] = {
|
||||||
{ 128, 28 },
|
{ 80, -1 }, /* Use deterministic algorithm for size <= 80 bits */
|
||||||
|
{ 81, 39 },
|
||||||
|
{ 96, 37 },
|
||||||
|
{ 128, 32 },
|
||||||
|
{ 160, 27 },
|
||||||
|
{ 192, 21 },
|
||||||
{ 256, 16 },
|
{ 256, 16 },
|
||||||
{ 384, 10 },
|
{ 384, 10 },
|
||||||
{ 512, 7 },
|
{ 512, 7 },
|
||||||
{ 640, 6 },
|
{ 640, 6 },
|
||||||
{ 768, 5 },
|
{ 768, 5 },
|
||||||
{ 896, 4 },
|
{ 896, 4 },
|
||||||
{ 1024, 4 }
|
{ 1024, 4 },
|
||||||
|
{ 2048, 2 },
|
||||||
|
{ 4096, 1 },
|
||||||
};
|
};
|
||||||
|
|
||||||
/* returns # of RM trials required for a given bit size */
|
/* returns # of RM trials required for a given bit size and max. error of 2^(-96)*/
|
||||||
int mp_prime_rabin_miller_trials(int size)
|
int mp_prime_rabin_miller_trials(int size)
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_PRIME_RANDOM_EX_C
|
#ifdef BN_MP_PRIME_RANDOM_EX_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* makes a truly random prime of a given size (bits),
|
/* makes a truly random prime of a given size (bits),
|
||||||
@ -49,7 +46,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
|||||||
bsize = (size>>3) + ((size&7)?1:0);
|
bsize = (size>>3) + ((size&7)?1:0);
|
||||||
|
|
||||||
/* we need a buffer of bsize bytes */
|
/* we need a buffer of bsize bytes */
|
||||||
tmp = OPT_CAST(unsigned char) XMALLOC(bsize);
|
tmp = OPT_CAST(unsigned char) XMALLOC((size_t)bsize);
|
||||||
if (tmp == NULL) {
|
if (tmp == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
}
|
}
|
||||||
@ -86,28 +83,42 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
|||||||
tmp[bsize-1] |= maskOR_lsb;
|
tmp[bsize-1] |= maskOR_lsb;
|
||||||
|
|
||||||
/* read it in */
|
/* read it in */
|
||||||
if ((err = mp_read_unsigned_bin(a, tmp, bsize)) != MP_OKAY) { goto error; }
|
if ((err = mp_read_unsigned_bin(a, tmp, bsize)) != MP_OKAY) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
/* is it prime? */
|
/* is it prime? */
|
||||||
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; }
|
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
if (res == MP_NO) {
|
if (res == MP_NO) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((flags & LTM_PRIME_SAFE) != 0) {
|
if ((flags & LTM_PRIME_SAFE) != 0) {
|
||||||
/* see if (a-1)/2 is prime */
|
/* see if (a-1)/2 is prime */
|
||||||
if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) { goto error; }
|
if ((err = mp_sub_d(a, 1uL, a)) != MP_OKAY) {
|
||||||
if ((err = mp_div_2(a, a)) != MP_OKAY) { goto error; }
|
goto error;
|
||||||
|
}
|
||||||
|
if ((err = mp_div_2(a, a)) != MP_OKAY) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
/* is it prime? */
|
/* is it prime? */
|
||||||
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; }
|
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} while (res == MP_NO);
|
} while (res == MP_NO);
|
||||||
|
|
||||||
if ((flags & LTM_PRIME_SAFE) != 0) {
|
if ((flags & LTM_PRIME_SAFE) != 0) {
|
||||||
/* restore a to the original value */
|
/* restore a to the original value */
|
||||||
if ((err = mp_mul_2(a, a)) != MP_OKAY) { goto error; }
|
if ((err = mp_mul_2(a, a)) != MP_OKAY) {
|
||||||
if ((err = mp_add_d(a, 1, a)) != MP_OKAY) { goto error; }
|
goto error;
|
||||||
|
}
|
||||||
|
if ((err = mp_add_d(a, 1uL, a)) != MP_OKAY) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
|
411
bn_mp_prime_strong_lucas_selfridge.c
Normal file
411
bn_mp_prime_strong_lucas_selfridge.c
Normal file
@ -0,0 +1,411 @@
|
|||||||
|
#include "tommath_private.h"
|
||||||
|
#ifdef BN_MP_PRIME_STRONG_LUCAS_SELFRIDGE_C
|
||||||
|
|
||||||
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
|
*
|
||||||
|
* LibTomMath is a library that provides multiple-precision
|
||||||
|
* integer arithmetic as well as number theoretic functionality.
|
||||||
|
*
|
||||||
|
* The library was designed directly after the MPI library by
|
||||||
|
* Michael Fromberger but has been written from scratch with
|
||||||
|
* additional optimizations in place.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Unlicense
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* See file bn_mp_prime_is_prime.c or the documentation in doc/bn.tex for the details
|
||||||
|
*/
|
||||||
|
#ifndef LTM_USE_FIPS_ONLY
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 8-bit is just too small. You can try the Frobenius test
|
||||||
|
* but that frobenius test can fail, too, for the same reason.
|
||||||
|
*/
|
||||||
|
#ifndef MP_8BIT
|
||||||
|
|
||||||
|
/*
|
||||||
|
* multiply bigint a with int d and put the result in c
|
||||||
|
* Like mp_mul_d() but with a signed long as the small input
|
||||||
|
*/
|
||||||
|
static int s_mp_mul_si(const mp_int *a, long d, mp_int *c)
|
||||||
|
{
|
||||||
|
mp_int t;
|
||||||
|
int err, neg = 0;
|
||||||
|
|
||||||
|
if ((err = mp_init(&t)) != MP_OKAY) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
if (d < 0) {
|
||||||
|
neg = 1;
|
||||||
|
d = -d;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* mp_digit might be smaller than a long, which excludes
|
||||||
|
* the use of mp_mul_d() here.
|
||||||
|
*/
|
||||||
|
if ((err = mp_set_long(&t, (unsigned long) d)) != MP_OKAY) {
|
||||||
|
goto LBL_MPMULSI_ERR;
|
||||||
|
}
|
||||||
|
if ((err = mp_mul(a, &t, c)) != MP_OKAY) {
|
||||||
|
goto LBL_MPMULSI_ERR;
|
||||||
|
}
|
||||||
|
if (neg == 1) {
|
||||||
|
c->sign = (a->sign == MP_NEG) ? MP_ZPOS: MP_NEG;
|
||||||
|
}
|
||||||
|
LBL_MPMULSI_ERR:
|
||||||
|
mp_clear(&t);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Strong Lucas-Selfridge test.
|
||||||
|
returns MP_YES if it is a strong L-S prime, MP_NO if it is composite
|
||||||
|
|
||||||
|
Code ported from Thomas Ray Nicely's implementation of the BPSW test
|
||||||
|
at http://www.trnicely.net/misc/bpsw.html
|
||||||
|
|
||||||
|
Freeware copyright (C) 2016 Thomas R. Nicely <http://www.trnicely.net>.
|
||||||
|
Released into the public domain by the author, who disclaims any legal
|
||||||
|
liability arising from its use
|
||||||
|
|
||||||
|
The multi-line comments are made by Thomas R. Nicely and are copied verbatim.
|
||||||
|
Additional comments marked "CZ" (without the quotes) are by the code-portist.
|
||||||
|
|
||||||
|
(If that name sounds familiar, he is the guy who found the fdiv bug in the
|
||||||
|
Pentium (P5x, I think) Intel processor)
|
||||||
|
*/
|
||||||
|
int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
|
||||||
|
{
|
||||||
|
/* CZ TODO: choose better variable names! */
|
||||||
|
mp_int Dz, gcd, Np1, Uz, Vz, U2mz, V2mz, Qmz, Q2mz, Qkdz, T1z, T2z, T3z, T4z, Q2kdz;
|
||||||
|
/* CZ TODO: Some of them need the full 32 bit, hence the (temporary) exclusion of MP_8BIT */
|
||||||
|
int32_t D, Ds, J, sign, P, Q, r, s, u, Nbits;
|
||||||
|
int e;
|
||||||
|
int isset, oddness;
|
||||||
|
|
||||||
|
*result = MP_NO;
|
||||||
|
/*
|
||||||
|
Find the first element D in the sequence {5, -7, 9, -11, 13, ...}
|
||||||
|
such that Jacobi(D,N) = -1 (Selfridge's algorithm). Theory
|
||||||
|
indicates that, if N is not a perfect square, D will "nearly
|
||||||
|
always" be "small." Just in case, an overflow trap for D is
|
||||||
|
included.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ((e = mp_init_multi(&Dz, &gcd, &Np1, &Uz, &Vz, &U2mz, &V2mz, &Qmz, &Q2mz, &Qkdz, &T1z, &T2z, &T3z, &T4z, &Q2kdz,
|
||||||
|
NULL)) != MP_OKAY) {
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
D = 5;
|
||||||
|
sign = 1;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
Ds = sign * D;
|
||||||
|
sign = -sign;
|
||||||
|
if ((e = mp_set_long(&Dz, (unsigned long)D)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_gcd(a, &Dz, &gcd)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
/* if 1 < GCD < N then N is composite with factor "D", and
|
||||||
|
Jacobi(D,N) is technically undefined (but often returned
|
||||||
|
as zero). */
|
||||||
|
if ((mp_cmp_d(&gcd, 1uL) == MP_GT) && (mp_cmp(&gcd, a) == MP_LT)) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if (Ds < 0) {
|
||||||
|
Dz.sign = MP_NEG;
|
||||||
|
}
|
||||||
|
if ((e = mp_kronecker(&Dz, a, &J)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (J == -1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
D += 2;
|
||||||
|
|
||||||
|
if (D > (INT_MAX - 2)) {
|
||||||
|
e = MP_VAL;
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
P = 1; /* Selfridge's choice */
|
||||||
|
Q = (1 - Ds) / 4; /* Required so D = P*P - 4*Q */
|
||||||
|
|
||||||
|
/* NOTE: The conditions (a) N does not divide Q, and
|
||||||
|
(b) D is square-free or not a perfect square, are included by
|
||||||
|
some authors; e.g., "Prime numbers and computer methods for
|
||||||
|
factorization," Hans Riesel (2nd ed., 1994, Birkhauser, Boston),
|
||||||
|
p. 130. For this particular application of Lucas sequences,
|
||||||
|
these conditions were found to be immaterial. */
|
||||||
|
|
||||||
|
/* Now calculate N - Jacobi(D,N) = N + 1 (even), and calculate the
|
||||||
|
odd positive integer d and positive integer s for which
|
||||||
|
N + 1 = 2^s*d (similar to the step for N - 1 in Miller's test).
|
||||||
|
The strong Lucas-Selfridge test then returns N as a strong
|
||||||
|
Lucas probable prime (slprp) if any of the following
|
||||||
|
conditions is met: U_d=0, V_d=0, V_2d=0, V_4d=0, V_8d=0,
|
||||||
|
V_16d=0, ..., etc., ending with V_{2^(s-1)*d}=V_{(N+1)/2}=0
|
||||||
|
(all equalities mod N). Thus d is the highest index of U that
|
||||||
|
must be computed (since V_2m is independent of U), compared
|
||||||
|
to U_{N+1} for the standard Lucas-Selfridge test; and no
|
||||||
|
index of V beyond (N+1)/2 is required, just as in the
|
||||||
|
standard Lucas-Selfridge test. However, the quantity Q^d must
|
||||||
|
be computed for use (if necessary) in the latter stages of
|
||||||
|
the test. The result is that the strong Lucas-Selfridge test
|
||||||
|
has a running time only slightly greater (order of 10 %) than
|
||||||
|
that of the standard Lucas-Selfridge test, while producing
|
||||||
|
only (roughly) 30 % as many pseudoprimes (and every strong
|
||||||
|
Lucas pseudoprime is also a standard Lucas pseudoprime). Thus
|
||||||
|
the evidence indicates that the strong Lucas-Selfridge test is
|
||||||
|
more effective than the standard Lucas-Selfridge test, and a
|
||||||
|
Baillie-PSW test based on the strong Lucas-Selfridge test
|
||||||
|
should be more reliable. */
|
||||||
|
|
||||||
|
if ((e = mp_add_d(a, 1uL, &Np1)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
s = mp_cnt_lsb(&Np1);
|
||||||
|
|
||||||
|
/* CZ
|
||||||
|
* This should round towards zero because
|
||||||
|
* Thomas R. Nicely used GMP's mpz_tdiv_q_2exp()
|
||||||
|
* and mp_div_2d() is equivalent. Additionally:
|
||||||
|
* dividing an even number by two does not produce
|
||||||
|
* any leftovers.
|
||||||
|
*/
|
||||||
|
if ((e = mp_div_2d(&Np1, s, &Dz, NULL)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
/* We must now compute U_d and V_d. Since d is odd, the accumulated
|
||||||
|
values U and V are initialized to U_1 and V_1 (if the target
|
||||||
|
index were even, U and V would be initialized instead to U_0=0
|
||||||
|
and V_0=2). The values of U_2m and V_2m are also initialized to
|
||||||
|
U_1 and V_1; the FOR loop calculates in succession U_2 and V_2,
|
||||||
|
U_4 and V_4, U_8 and V_8, etc. If the corresponding bits
|
||||||
|
(1, 2, 3, ...) of t are on (the zero bit having been accounted
|
||||||
|
for in the initialization of U and V), these values are then
|
||||||
|
combined with the previous totals for U and V, using the
|
||||||
|
composition formulas for addition of indices. */
|
||||||
|
|
||||||
|
mp_set(&Uz, 1uL); /* U=U_1 */
|
||||||
|
mp_set(&Vz, (mp_digit)P); /* V=V_1 */
|
||||||
|
mp_set(&U2mz, 1uL); /* U_1 */
|
||||||
|
mp_set(&V2mz, (mp_digit)P); /* V_1 */
|
||||||
|
|
||||||
|
if (Q < 0) {
|
||||||
|
Q = -Q;
|
||||||
|
if ((e = mp_set_long(&Qmz, (unsigned long)Q)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
/* Initializes calculation of Q^d */
|
||||||
|
if ((e = mp_set_long(&Qkdz, (unsigned long)Q)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
Qmz.sign = MP_NEG;
|
||||||
|
Q2mz.sign = MP_NEG;
|
||||||
|
Qkdz.sign = MP_NEG;
|
||||||
|
Q = -Q;
|
||||||
|
} else {
|
||||||
|
if ((e = mp_set_long(&Qmz, (unsigned long)Q)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
/* Initializes calculation of Q^d */
|
||||||
|
if ((e = mp_set_long(&Qkdz, (unsigned long)Q)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Nbits = mp_count_bits(&Dz);
|
||||||
|
|
||||||
|
for (u = 1; u < Nbits; u++) { /* zero bit off, already accounted for */
|
||||||
|
/* Formulas for doubling of indices (carried out mod N). Note that
|
||||||
|
* the indices denoted as "2m" are actually powers of 2, specifically
|
||||||
|
* 2^(ul-1) beginning each loop and 2^ul ending each loop.
|
||||||
|
*
|
||||||
|
* U_2m = U_m*V_m
|
||||||
|
* V_2m = V_m*V_m - 2*Q^m
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ((e = mp_mul(&U2mz, &V2mz, &U2mz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mod(&U2mz, a, &U2mz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_sqr(&V2mz, &V2mz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_sub(&V2mz, &Q2mz, &V2mz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mod(&V2mz, a, &V2mz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
/* Must calculate powers of Q for use in V_2m, also for Q^d later */
|
||||||
|
if ((e = mp_sqr(&Qmz, &Qmz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
/* prevents overflow */ /* CZ still necessary without a fixed prealloc'd mem.? */
|
||||||
|
if ((e = mp_mod(&Qmz, a, &Qmz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mul_2(&Qmz, &Q2mz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((isset = mp_get_bit(&Dz, u)) == MP_VAL) {
|
||||||
|
e = isset;
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if (isset == MP_YES) {
|
||||||
|
/* Formulas for addition of indices (carried out mod N);
|
||||||
|
*
|
||||||
|
* U_(m+n) = (U_m*V_n + U_n*V_m)/2
|
||||||
|
* V_(m+n) = (V_m*V_n + D*U_m*U_n)/2
|
||||||
|
*
|
||||||
|
* Be careful with division by 2 (mod N)!
|
||||||
|
*/
|
||||||
|
if ((e = mp_mul(&U2mz, &Vz, &T1z)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mul(&Uz, &V2mz, &T2z)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mul(&V2mz, &Vz, &T3z)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mul(&U2mz, &Uz, &T4z)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = s_mp_mul_si(&T4z, (long)Ds, &T4z)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_add(&T1z, &T2z, &Uz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if (mp_isodd(&Uz) != MP_NO) {
|
||||||
|
if ((e = mp_add(&Uz, a, &Uz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* CZ
|
||||||
|
* This should round towards negative infinity because
|
||||||
|
* Thomas R. Nicely used GMP's mpz_fdiv_q_2exp().
|
||||||
|
* But mp_div_2() does not do so, it is truncating instead.
|
||||||
|
*/
|
||||||
|
oddness = mp_isodd(&Uz);
|
||||||
|
if ((e = mp_div_2(&Uz, &Uz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((Uz.sign == MP_NEG) && (oddness != MP_NO)) {
|
||||||
|
if ((e = mp_sub_d(&Uz, 1uL, &Uz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((e = mp_add(&T3z, &T4z, &Vz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if (mp_isodd(&Vz) != MP_NO) {
|
||||||
|
if ((e = mp_add(&Vz, a, &Vz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
oddness = mp_isodd(&Vz);
|
||||||
|
if ((e = mp_div_2(&Vz, &Vz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((Vz.sign == MP_NEG) && (oddness != MP_NO)) {
|
||||||
|
if ((e = mp_sub_d(&Vz, 1uL, &Vz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((e = mp_mod(&Uz, a, &Uz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mod(&Vz, a, &Vz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
/* Calculating Q^d for later use */
|
||||||
|
if ((e = mp_mul(&Qkdz, &Qmz, &Qkdz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mod(&Qkdz, a, &Qkdz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If U_d or V_d is congruent to 0 mod N, then N is a prime or a
|
||||||
|
strong Lucas pseudoprime. */
|
||||||
|
if ((mp_iszero(&Uz) != MP_NO) || (mp_iszero(&Vz) != MP_NO)) {
|
||||||
|
*result = MP_YES;
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* NOTE: Ribenboim ("The new book of prime number records," 3rd ed.,
|
||||||
|
1995/6) omits the condition V0 on p.142, but includes it on
|
||||||
|
p. 130. The condition is NECESSARY; otherwise the test will
|
||||||
|
return false negatives---e.g., the primes 29 and 2000029 will be
|
||||||
|
returned as composite. */
|
||||||
|
|
||||||
|
/* Otherwise, we must compute V_2d, V_4d, V_8d, ..., V_{2^(s-1)*d}
|
||||||
|
by repeated use of the formula V_2m = V_m*V_m - 2*Q^m. If any of
|
||||||
|
these are congruent to 0 mod N, then N is a prime or a strong
|
||||||
|
Lucas pseudoprime. */
|
||||||
|
|
||||||
|
/* Initialize 2*Q^(d*2^r) for V_2m */
|
||||||
|
if ((e = mp_mul_2(&Qkdz, &Q2kdz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (r = 1; r < s; r++) {
|
||||||
|
if ((e = mp_sqr(&Vz, &Vz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_sub(&Vz, &Q2kdz, &Vz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mod(&Vz, a, &Vz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if (mp_iszero(&Vz) != MP_NO) {
|
||||||
|
*result = MP_YES;
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
/* Calculate Q^{d*2^r} for next r (final iteration irrelevant). */
|
||||||
|
if (r < (s - 1)) {
|
||||||
|
if ((e = mp_sqr(&Qkdz, &Qkdz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mod(&Qkdz, a, &Qkdz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
if ((e = mp_mul_2(&Qkdz, &Q2kdz)) != MP_OKAY) {
|
||||||
|
goto LBL_LS_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LBL_LS_ERR:
|
||||||
|
mp_clear_multi(&Q2kdz, &T4z, &T3z, &T2z, &T1z, &Qkdz, &Q2mz, &Qmz, &V2mz, &U2mz, &Vz, &Uz, &Np1, &gcd, &Dz, NULL);
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ref: $Format:%D$ */
|
||||||
|
/* git commit: $Format:%H$ */
|
||||||
|
/* commit time: $Format:%ai$ */
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_RADIX_SIZE_C
|
#ifdef BN_MP_RADIX_SIZE_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* returns size of ASCII reprensentation */
|
/* returns size of ASCII reprensentation */
|
||||||
int mp_radix_size (mp_int * a, int radix, int *size)
|
int mp_radix_size(const mp_int *a, int radix, int *size)
|
||||||
{
|
{
|
||||||
int res, digs;
|
int res, digs;
|
||||||
mp_int t;
|
mp_int t;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_RADIX_SMAP_C
|
#ifdef BN_MP_RADIX_SMAP_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,25 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* chars used in radix conversions */
|
/* chars used in radix conversions */
|
||||||
const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
|
const char *const mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
|
||||||
|
const uint8_t mp_s_rmap_reverse[] = {
|
||||||
|
0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, /* ()*+,-./ */
|
||||||
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 01234567 */
|
||||||
|
0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 89:;<=>? */
|
||||||
|
0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, /* @ABCDEFG */
|
||||||
|
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, /* HIJKLMNO */
|
||||||
|
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, /* PQRSTUVW */
|
||||||
|
0x21, 0x22, 0x23, 0xff, 0xff, 0xff, 0xff, 0xff, /* XYZ[\]^_ */
|
||||||
|
0xff, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, /* `abcdefg */
|
||||||
|
0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, /* hijklmno */
|
||||||
|
0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, /* pqrstuvw */
|
||||||
|
0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, /* xyz{|}~. */
|
||||||
|
};
|
||||||
|
const size_t mp_s_rmap_reverse_sz = sizeof(mp_s_rmap_reverse);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* ref: $Format:%D$ */
|
/* ref: $Format:%D$ */
|
||||||
|
192
bn_mp_rand.c
192
bn_mp_rand.c
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_RAND_C
|
#ifdef BN_MP_RAND_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,40 +9,177 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if MP_GEN_RANDOM_MAX == 0xffffffff
|
/* First the OS-specific special cases
|
||||||
|
* - *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
|
#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
|
|
||||||
|
|
||||||
/* makes a pseudo-random int of a given size */
|
static int s_read_arc4random(mp_digit *p)
|
||||||
static mp_digit s_gen_random(void)
|
|
||||||
{
|
{
|
||||||
mp_digit d = 0, msk = 0;
|
mp_digit d = 0, msk = 0;
|
||||||
do {
|
do {
|
||||||
d <<= MP_GEN_RANDOM_SHIFT;
|
d <<= MP_GEN_RANDOM_SHIFT;
|
||||||
d |= ((mp_digit) MP_GEN_RANDOM());
|
d |= ((mp_digit) arc4random());
|
||||||
msk <<= MP_GEN_RANDOM_SHIFT;
|
msk <<= MP_GEN_RANDOM_SHIFT;
|
||||||
msk |= (MP_MASK & MP_GEN_RANDOM_MAX);
|
msk |= (MP_MASK & MP_GEN_RANDOM_MAX);
|
||||||
} while ((MP_MASK & msk) != MP_MASK);
|
} while ((MP_MASK & msk) != MP_MASK);
|
||||||
d &= MP_MASK;
|
*p = d;
|
||||||
return d;
|
return MP_OKAY;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(_WIN32) || defined(_WIN32_WCE)
|
||||||
|
#define MP_WIN_CSP
|
||||||
|
|
||||||
|
#ifndef _WIN32_WINNT
|
||||||
|
#define _WIN32_WINNT 0x0400
|
||||||
|
#endif
|
||||||
|
#ifdef _WIN32_WCE
|
||||||
|
#define UNDER_CE
|
||||||
|
#define ARM
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#include <windows.h>
|
||||||
|
#include <wincrypt.h>
|
||||||
|
|
||||||
|
static HCRYPTPROV hProv = 0;
|
||||||
|
|
||||||
|
static void s_cleanup_win_csp(void)
|
||||||
|
{
|
||||||
|
CryptReleaseContext(hProv, 0);
|
||||||
|
hProv = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
static int s_read_win_csp(mp_digit *p)
|
||||||
mp_rand (mp_int * a, int digits)
|
{
|
||||||
|
int ret = -1;
|
||||||
|
if (hProv == 0) {
|
||||||
|
if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
|
||||||
|
(CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
|
||||||
|
!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
|
||||||
|
CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) {
|
||||||
|
hProv = 0;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
atexit(s_cleanup_win_csp);
|
||||||
|
}
|
||||||
|
if (CryptGenRandom(hProv, sizeof(*p), (void *)p) == TRUE) {
|
||||||
|
ret = MP_OKAY;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
#endif /* WIN32 */
|
||||||
|
|
||||||
|
#if !defined(MP_WIN_CSP) && defined(__linux__) && defined(__GLIBC_PREREQ)
|
||||||
|
#if __GLIBC_PREREQ(2, 25)
|
||||||
|
#define MP_GETRANDOM
|
||||||
|
#include <sys/random.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
static int s_read_getrandom(mp_digit *p)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
do {
|
||||||
|
ret = getrandom(p, sizeof(*p), 0);
|
||||||
|
} while ((ret == -1) && (errno == EINTR));
|
||||||
|
if (ret == sizeof(*p)) return MP_OKAY;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* We assume all platforms besides windows provide "/dev/urandom".
|
||||||
|
* In case yours doesn't, define MP_NO_DEV_URANDOM at compile-time.
|
||||||
|
*/
|
||||||
|
#if !defined(MP_WIN_CSP) && !defined(MP_NO_DEV_URANDOM)
|
||||||
|
#ifndef MP_DEV_URANDOM
|
||||||
|
#define MP_DEV_URANDOM "/dev/urandom"
|
||||||
|
#endif
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
static int s_read_dev_urandom(mp_digit *p)
|
||||||
|
{
|
||||||
|
ssize_t r;
|
||||||
|
int fd;
|
||||||
|
do {
|
||||||
|
fd = open(MP_DEV_URANDOM, O_RDONLY);
|
||||||
|
} while ((fd == -1) && (errno == EINTR));
|
||||||
|
if (fd == -1) return -1;
|
||||||
|
do {
|
||||||
|
r = read(fd, p, sizeof(*p));
|
||||||
|
} while ((r == -1) && (errno == EINTR));
|
||||||
|
close(fd);
|
||||||
|
if (r != sizeof(*p)) return -1;
|
||||||
|
return MP_OKAY;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(MP_PRNG_ENABLE_LTM_RNG)
|
||||||
|
unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
|
||||||
|
void (*ltm_rng_callback)(void);
|
||||||
|
|
||||||
|
static int s_read_ltm_rng(mp_digit *p)
|
||||||
|
{
|
||||||
|
unsigned long ret;
|
||||||
|
if (ltm_rng == NULL) return -1;
|
||||||
|
ret = ltm_rng((void *)p, sizeof(*p), ltm_rng_callback);
|
||||||
|
if (ret != sizeof(*p)) return -1;
|
||||||
|
return MP_OKAY;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int s_rand_digit(mp_digit *p)
|
||||||
|
{
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
#if defined(MP_ARC4RANDOM)
|
||||||
|
ret = s_read_arc4random(p);
|
||||||
|
if (ret == MP_OKAY) return ret;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(MP_WIN_CSP)
|
||||||
|
ret = s_read_win_csp(p);
|
||||||
|
if (ret == MP_OKAY) return ret;
|
||||||
|
#else
|
||||||
|
|
||||||
|
#if defined(MP_GETRANDOM)
|
||||||
|
ret = s_read_getrandom(p);
|
||||||
|
if (ret == MP_OKAY) return ret;
|
||||||
|
#endif
|
||||||
|
#if defined(MP_DEV_URANDOM)
|
||||||
|
ret = s_read_dev_urandom(p);
|
||||||
|
if (ret == MP_OKAY) return ret;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* MP_WIN_CSP */
|
||||||
|
|
||||||
|
#if defined(MP_PRNG_ENABLE_LTM_RNG)
|
||||||
|
ret = s_read_ltm_rng(p);
|
||||||
|
if (ret == MP_OKAY) return ret;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* makes a pseudo-random int of a given size */
|
||||||
|
int mp_rand_digit(mp_digit *r)
|
||||||
|
{
|
||||||
|
int ret = s_rand_digit(r);
|
||||||
|
*r &= MP_MASK;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mp_rand(mp_int *a, int digits)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
mp_digit d;
|
mp_digit d;
|
||||||
@ -54,8 +191,10 @@ mp_rand (mp_int * a, int digits)
|
|||||||
|
|
||||||
/* first place a random non-zero digit */
|
/* first place a random non-zero digit */
|
||||||
do {
|
do {
|
||||||
d = s_gen_random();
|
if (mp_rand_digit(&d) != MP_OKAY) {
|
||||||
} while (d == 0);
|
return MP_VAL;
|
||||||
|
}
|
||||||
|
} while (d == 0u);
|
||||||
|
|
||||||
if ((res = mp_add_d(a, d, a)) != MP_OKAY) {
|
if ((res = mp_add_d(a, d, a)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
@ -66,7 +205,10 @@ mp_rand (mp_int * a, int digits)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_add_d (a, s_gen_random(), a)) != MP_OKAY) {
|
if (mp_rand_digit(&d) != MP_OKAY) {
|
||||||
|
return MP_VAL;
|
||||||
|
}
|
||||||
|
if ((res = mp_add_d(a, d, a)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_READ_RADIX_C
|
#ifdef BN_MP_READ_RADIX_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,16 +9,14 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* read a string [ASCII] in a given radix */
|
/* read a string [ASCII] in a given radix */
|
||||||
int mp_read_radix(mp_int *a, const char *str, int radix)
|
int mp_read_radix(mp_int *a, const char *str, int radix)
|
||||||
{
|
{
|
||||||
int y, res, neg;
|
int y, res, neg;
|
||||||
|
unsigned pos;
|
||||||
char ch;
|
char ch;
|
||||||
|
|
||||||
/* zero the digit bignum */
|
/* zero the digit bignum */
|
||||||
@ -49,29 +47,34 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
|||||||
* [e.g. in hex]
|
* [e.g. in hex]
|
||||||
*/
|
*/
|
||||||
ch = (radix <= 36) ? (char)toupper((int)*str) : *str;
|
ch = (radix <= 36) ? (char)toupper((int)*str) : *str;
|
||||||
for (y = 0; y < 64; y++) {
|
pos = (unsigned)(ch - '(');
|
||||||
if (ch == mp_s_rmap[y]) {
|
if (mp_s_rmap_reverse_sz < pos) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
y = (int)mp_s_rmap_reverse[pos];
|
||||||
|
|
||||||
/* if the char was found in the map
|
/* if the char was found in the map
|
||||||
* and is less than the given radix add it
|
* and is less than the given radix add it
|
||||||
* to the number, otherwise exit the loop.
|
* to the number, otherwise exit the loop.
|
||||||
*/
|
*/
|
||||||
if (y < radix) {
|
if ((y == 0xff) || (y >= radix)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
if ((res = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
|
if ((res = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
if ((res = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
|
if ((res = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
++str;
|
++str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* if an illegal character was found, fail. */
|
||||||
|
if (!((*str == '\0') || (*str == '\r') || (*str == '\n'))) {
|
||||||
|
mp_zero(a);
|
||||||
|
return MP_VAL;
|
||||||
|
}
|
||||||
|
|
||||||
/* set the sign only if a != 0 */
|
/* set the sign only if a != 0 */
|
||||||
if (mp_iszero(a) != MP_YES) {
|
if (mp_iszero(a) != MP_YES) {
|
||||||
a->sign = neg;
|
a->sign = neg;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_READ_SIGNED_BIN_C
|
#ifdef BN_MP_READ_SIGNED_BIN_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* read signed bin, big endian, first byte is 0==positive or 1==negative */
|
/* read signed bin, big endian, first byte is 0==positive or 1==negative */
|
||||||
@ -26,7 +23,7 @@ int mp_read_signed_bin (mp_int * a, const unsigned char *b, int c)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* first byte is 0 for positive, non-zero for negative */
|
/* first byte is 0 for positive, non-zero for negative */
|
||||||
if (b[0] == 0) {
|
if (b[0] == (unsigned char)0) {
|
||||||
a->sign = MP_ZPOS;
|
a->sign = MP_ZPOS;
|
||||||
} else {
|
} else {
|
||||||
a->sign = MP_NEG;
|
a->sign = MP_NEG;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_READ_UNSIGNED_BIN_C
|
#ifdef BN_MP_READ_UNSIGNED_BIN_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,10 +9,7 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* reads a unsigned char array, assumes the msb is stored first [big endian] */
|
/* reads a unsigned char array, assumes the msb is stored first [big endian] */
|
||||||
@ -41,7 +38,7 @@ int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
|
|||||||
a->used += 1;
|
a->used += 1;
|
||||||
#else
|
#else
|
||||||
a->dp[0] = (*b & MP_MASK);
|
a->dp[0] = (*b & MP_MASK);
|
||||||
a->dp[1] |= ((*b++ >> 7U) & 1);
|
a->dp[1] |= ((*b++ >> 7) & 1u);
|
||||||
a->used += 2;
|
a->used += 2;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_REDUCE_C
|
#ifdef BN_MP_REDUCE_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,17 +9,14 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* reduces x mod m, assumes 0 < x < m**2, mu is
|
/* reduces x mod m, assumes 0 < x < m**2, mu is
|
||||||
* precomputed via mp_reduce_setup.
|
* precomputed via mp_reduce_setup.
|
||||||
* From HAC pp.604 Algorithm 14.42
|
* From HAC pp.604 Algorithm 14.42
|
||||||
*/
|
*/
|
||||||
int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
int mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
|
||||||
{
|
{
|
||||||
mp_int q;
|
mp_int q;
|
||||||
int res, um = m->used;
|
int res, um = m->used;
|
||||||
@ -33,7 +30,7 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
|||||||
mp_rshd(&q, um - 1);
|
mp_rshd(&q, um - 1);
|
||||||
|
|
||||||
/* according to HAC this optimization is ok */
|
/* according to HAC this optimization is ok */
|
||||||
if (((mp_digit) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
|
if ((mp_digit)um > ((mp_digit)1 << (DIGIT_BIT - 1))) {
|
||||||
if ((res = mp_mul(&q, mu, &q)) != MP_OKAY) {
|
if ((res = mp_mul(&q, mu, &q)) != MP_OKAY) {
|
||||||
goto CLEANUP;
|
goto CLEANUP;
|
||||||
}
|
}
|
||||||
@ -73,8 +70,8 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* If x < 0, add b**(k+1) to it */
|
/* If x < 0, add b**(k+1) to it */
|
||||||
if (mp_cmp_d (x, 0) == MP_LT) {
|
if (mp_cmp_d(x, 0uL) == MP_LT) {
|
||||||
mp_set (&q, 1);
|
mp_set(&q, 1uL);
|
||||||
if ((res = mp_lshd(&q, um + 1)) != MP_OKAY)
|
if ((res = mp_lshd(&q, um + 1)) != MP_OKAY)
|
||||||
goto CLEANUP;
|
goto CLEANUP;
|
||||||
if ((res = mp_add(x, &q, x)) != MP_OKAY)
|
if ((res = mp_add(x, &q, x)) != MP_OKAY)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <tommath_private.h>
|
#include "tommath_private.h"
|
||||||
#ifdef BN_MP_REDUCE_2K_C
|
#ifdef BN_MP_REDUCE_2K_C
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
||||||
*
|
*
|
||||||
@ -9,14 +9,11 @@
|
|||||||
* Michael Fromberger but has been written from scratch with
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* SPDX-License-Identifier: Unlicense
|
||||||
* guarantee it works.
|
|
||||||
*
|
|
||||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* reduces a modulo n where n is of the form 2**p - d */
|
/* reduces a modulo n where n is of the form 2**p - d */
|
||||||
int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
|
int mp_reduce_2k(mp_int *a, const mp_int *n, mp_digit d)
|
||||||
{
|
{
|
||||||
mp_int q;
|
mp_int q;
|
||||||
int p, res;
|
int p, res;
|
||||||
@ -29,29 +26,29 @@ int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
|
|||||||
top:
|
top:
|
||||||
/* q = a/2**p, a = a mod 2**p */
|
/* q = a/2**p, a = a mod 2**p */
|
||||||
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
|
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (d != 1) {
|
if (d != 1u) {
|
||||||
/* q = q * d */
|
/* q = q * d */
|
||||||
if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
|
if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* a = a + q */
|
/* a = a + q */
|
||||||
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
|
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mp_cmp_mag(a, n) != MP_LT) {
|
if (mp_cmp_mag(a, n) != MP_LT) {
|
||||||
if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
|
if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
goto top;
|
goto top;
|
||||||
}
|
}
|
||||||
|
|
||||||
ERR:
|
LBL_ERR:
|
||||||
mp_clear(&q);
|
mp_clear(&q);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user