Skip to content

Commit

Permalink
Merge pull request #536 from libtom/some-improvements
Browse files Browse the repository at this point in the history
Some improvements
  • Loading branch information
sjaeckel authored Oct 3, 2022
2 parents f3b8879 + 4de4e4f commit 96f9edf
Show file tree
Hide file tree
Showing 27 changed files with 54 additions and 39 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ endif()
# What compiler do we have and what are their...uhm... peculiarities
if(CMAKE_C_COMPILER_ID MATCHES "(C|c?)lang")
list(APPEND LTM_C_FLAGS -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header)
# Clang requires at least '-O1' for dead code eliminiation
# Clang requires at least '-O1' for dead code elimination
set(CMAKE_C_FLAGS_DEBUG "-O1 ${CMAKE_C_FLAGS_DEBUG}")
endif()
if(CMAKE_C_COMPILER MATCHES "mingw")
Expand Down Expand Up @@ -103,11 +103,17 @@ target_link_options(${PROJECT_NAME} BEFORE PRIVATE
${LTM_LD_FLAGS}
)

set(PUBLIC_HEADERS tommath.h)
set(C89 False CACHE BOOL "(Usually maintained automatically) Enable when the library is in c89 mode to package the correct header files on install")
if(C89)
list(APPEND PUBLIC_HEADERS tommath_c89.h)
endif()

set_target_properties(${PROJECT_NAME} PROPERTIES
OUTPUT_NAME tommath
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
PUBLIC_HEADER tommath.h
PUBLIC_HEADER "${PUBLIC_HEADERS}"
)

option(COMPILE_LTO "Build with LTO enabled")
Expand Down
2 changes: 1 addition & 1 deletion astylerc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# usage:
# astyle --options=astylerc *.[ch]

# Do not create backup, annonying in the times of git
# Do not create backup, annoying in the times of git
suffix=none

## Bracket Style Options
Expand Down
8 changes: 4 additions & 4 deletions changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ v1.2.0
-- Unified, safer and improved API's
-- Less magic numbers - return values (where appropriate) and most flags are now enums,
this was implemented in a backwards compatible way where return values were int.
-- API's with return values are now by default marked as "warn on unsused result", this
-- API's with return values are now by default marked as "warn on unused result", this
can be disabled if required (which will most likely hide bugs), c.f. MP_WUR in tommath.h
-- Provide a whole set of setters&getters for different primitive types (long, uint32_t, etc.)
-- All those primitive setters are now optimized.
Expand Down Expand Up @@ -412,8 +412,8 @@ v0.13 -- tons of minor speed-ups in low level add, sub, mul_2 and div_2 which p
Jan 17th, 2003
v0.12 -- re-wrote the majority of the makefile so its more portable and will
install via "make install" on most *nix platforms
-- Re-packaged all the source as seperate files. Means the library a single
file packagage any more. Instead of just adding "bn.c" you have to add
-- Re-packaged all the source as separate files. Means the library a single
file package any more. Instead of just adding "bn.c" you have to add
libtommath.a
-- Renamed "bn.h" to "tommath.h"
-- Changes to the manual to reflect all of this
Expand Down Expand Up @@ -444,7 +444,7 @@ v0.08 -- Sped up the multipliers by moving the inner loop variables into a smal
-- add etc/pprime.c program which makes numbers which are provably prime.

Jan 1st, 2003
v0.07 -- Removed alot of heap operations from core functions to speed them up
v0.07 -- Removed a lot of heap operations from core functions to speed them up
-- Added a root finding function [and mp_sqrt macro like from MPI]
-- Added more to manual

Expand Down
3 changes: 3 additions & 0 deletions demo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ target_link_options(${LTM_TEST} BEFORE PUBLIC
# CTest
#-----------------------------------------------------------------------------
add_test(NAME ${LTM_TEST} COMMAND ${LTM_TEST})

find_program(MEMORYCHECK_COMMAND valgrind)
set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full")
2 changes: 1 addition & 1 deletion demo/mtest_opponent.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static int mtest_opponent(void)
#endif

for (;;) {
/* randomly clear and re-init one variable, this has the affect of triming the alloc space */
/* randomly clear and re-init one variable, this has the effect of trimming the alloc space */
switch (abs(rand()) % 7) {
case 0:
mp_clear(&a);
Expand Down
8 changes: 4 additions & 4 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ static int test_mp_decr(void)
default(realprecision,1000);
for(n=3,100,r = floor(a^(1/n));printf("\"" r "\", "))
All numbers as strings to simplifiy things, especially for the
All numbers as strings to simplify things, especially for the
low-mp branch.
*/

Expand Down Expand Up @@ -2166,7 +2166,7 @@ static int test_mp_pack_unpack(void)
uint8_t *buf = NULL;

mp_order order = MP_LSB_FIRST;
mp_endian endianess = MP_NATIVE_ENDIAN;
mp_endian endianness = MP_NATIVE_ENDIAN;

DOR(mp_init_multi(&a, &b, NULL));
DO(mp_rand(&a, 15));
Expand All @@ -2180,9 +2180,9 @@ static int test_mp_pack_unpack(void)
}

DO(mp_pack((void *)buf, count, &written, order, 1uL,
endianess, 0uL, &a));
endianness, 0uL, &a));
DO(mp_unpack(&b, count, order, 1uL,
endianess, 0uL, (const void *)buf));
endianness, 0uL, (const void *)buf));

if (mp_cmp(&a, &b) != MP_EQ) {
fprintf(stderr, "pack/unpack cycle failed\n");
Expand Down
6 changes: 3 additions & 3 deletions doc/bn.tex
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ \subsubsection{OpenBSD}
At this time two versions of \texttt{libtool} are installed and both are named \texttt{libtool},
unfortunately but GNU \texttt{libtool} has been placed in \texttt{/usr/local/bin/} and the native
version in \texttt{/usr/bin/}. The path might be different in other versions of OpenBSD but both
programms differ in the output of \texttt{libtool --version}
programs differ in the output of \texttt{libtool --version}
\begin{alltt}
$ /usr/local/bin/libtool --version
libtool (GNU libtool) 2.4.2
Expand Down Expand Up @@ -519,7 +519,7 @@ \subsection{Single Initialization}
\end{alltt}

This function expects a pointer to an \texttt{mp\_int} structure and will initialize the members
ofthe structure so the \texttt{mp\_int} represents the default integer which is zero. If the
of the structure so the \texttt{mp\_int} represents the default integer which is zero. If the
functions returns \texttt{MP\_OKAY} then the \texttt{mp\_int} is ready to be used by the other
LibTomMath functions.

Expand Down Expand Up @@ -2213,7 +2213,7 @@ \subsection{Required Number of Tests}
Determining the probability needed to pick the right column is a bit harder. Fips 186.4, for
example has $2^{-80}$ for $512$ bit large numbers, $2^{-112}$ for $1024$ bits, and $2^{128}$ for
$1536$ bits. It can be seen in table \ref{table:millerrabinrunsp1} that those combinations follow
the diagonal from $(512,2^{-80})$ downwards and to the right to gain a lower probabilty of getting
the diagonal from $(512,2^{-80})$ downwards and to the right to gain a lower probability of getting
a composite declared a pseudoprime for the same amount of work or less.

If this version of the library has the strong Lucas--Selfridge and/or the Frobenius--Underwood test
Expand Down
2 changes: 1 addition & 1 deletion doc/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ manual: mandvi

# The file latexindent.pl is in several LaTeX distributions, if not:
# https://ctan.org/pkg/latexindent
# Its configuraion is well documented
# Its configuration is well documented
# http://mirrors.ctan.org/support/latexindent/documentation/latexindent.pdf
pretty:
latexindent -s -w -m -l=.latexindent.yaml bn.tex
Expand Down
4 changes: 2 additions & 2 deletions logs/before_after.dem
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ plot 'sqr-before.log' smooth bezier title "Squaring (without Karatsuba) (before)
set output "expt-ba.png"
plot 'expt-before.log' smooth bezier title "Exptmod (Montgomery) (before)", \
'expt-after.log' smooth bezier title "Exptmod (Montgomery) (after)", \
'expt_dr-before.log' smooth bezier title "Exptmod (Dimminished Radix) (before)", \
'expt_dr-after.log' smooth bezier title "Exptmod (Dimminished Radix) (after)", \
'expt_dr-before.log' smooth bezier title "Exptmod (Diminished Radix) (before)", \
'expt_dr-after.log' smooth bezier title "Exptmod (Diminished Radix) (after)", \
'expt_2k-before.log' smooth bezier title "Exptmod (2k Reduction) (before)", \
'expt_2k-after.log' smooth bezier title "Exptmod (2k Reduction) (after)", \
'expt_2kl-before.log' smooth bezier title "Exptmod (2k-l Reduction) (before)", \
Expand Down
2 changes: 1 addition & 1 deletion logs/graphs.dem
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set output "mult.png"
plot 'sqr.log' smooth bezier title "Squaring (without Karatsuba)", 'sqr_kara.log' smooth bezier title "Squaring (Karatsuba)", 'mult.log' smooth bezier title "Multiplication (without Karatsuba)", 'mult_kara.log' smooth bezier title "Multiplication (Karatsuba)"

set output "expt.png"
plot 'expt.log' smooth bezier title "Exptmod (Montgomery)", 'expt_dr.log' smooth bezier title "Exptmod (Dimminished Radix)", 'expt_2k.log' smooth bezier title "Exptmod (2k Reduction)"
plot 'expt.log' smooth bezier title "Exptmod (Montgomery)", 'expt_dr.log' smooth bezier title "Exptmod (Diminished Radix)", 'expt_2k.log' smooth bezier title "Exptmod (2k Reduction)"

set output "invmod.png"
plot 'invmod.log' smooth bezier title "Modular Inverse"
Expand Down
8 changes: 5 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ zipup: clean astyle new_file docs
@# As the pdf creation modifies the tex files, git sometimes detects the
@# modified files, but misses that it's put back to its original version.
@git update-index --refresh
@git diff-index --quiet HEAD -- || ( echo "FAILURE: uncommited changes or not a git" && exit 1 )
@git diff-index --quiet HEAD -- || ( echo "FAILURE: uncommitted changes or not a git" && exit 1 )
rm -rf libtommath-$(VERSION) ltm-$(VERSION).*
@# files/dirs excluded from "git archive" are defined in .gitattributes
git archive --format=tar --prefix=libtommath-$(VERSION)/ HEAD | tar x
Expand Down Expand Up @@ -173,7 +173,8 @@ c89:
-e 's/uint\([0-9][0-9]*\)_t/mp_u\1/g' \
-e 's/int\([0-9][0-9]*\)_t/mp_i\1/g' \
-e 's/__func__/MP_FUNCTION_NAME/g' \
*.c tommath.h tommath_private.h demo/*.c demo/*.h etc/*.c
-e 's/set(C89 False/set(C89 True/g' \
*.c tommath.h tommath_private.h demo/*.c demo/*.h etc/*.c CMakeLists.txt

c99:
@echo "Applying substitutions for c99 compatibility..."
Expand All @@ -196,7 +197,8 @@ c99:
-e 's/mp_u\([0-9][0-9]*\)/uint\1_t/g' \
-e 's/mp_i\([0-9][0-9]*\)/int\1_t/g' \
-e 's/MP_FUNCTION_NAME/__func__/g' \
*.c tommath.h tommath_private.h demo/*.c demo/*.h etc/*.c
-e 's/set(C89 True/set(C89 False/g' \
*.c tommath.h tommath_private.h demo/*.c demo/*.h etc/*.c CMakeLists.txt

astyle:
@echo " * run astyle on all sources"
Expand Down
4 changes: 4 additions & 0 deletions makefile_include.mk
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ endif # COMPILE_SIZE

ifneq ($(findstring clang,$(CC)),)
LTM_CFLAGS += -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header
ifdef IGNORE_SPEED
#for dead code eliminiation
LTM_CFLAGS += -O1
endif
endif
ifneq ($(findstring mingw,$(CC)),)
LTM_CFLAGS += -Wno-shadow
Expand Down
2 changes: 1 addition & 1 deletion mp_2expt.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mp_err mp_2expt(mp_int *a, int b)
/* zero a as per default */
mp_zero(a);

/* grow a to accomodate the single bit */
/* grow a to accommodate the single bit */
if ((err = mp_grow(a, (b / MP_DIGIT_BIT) + 1)) != MP_OKAY) {
return err;
}
Expand Down
2 changes: 1 addition & 1 deletion mp_clamp.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* trim unused digits
*
* This is used to ensure that leading zero digits are
* trimed and the leading "used" digit will be non-zero
* trimmed and the leading "used" digit will be non-zero
* Typically very fast. Also fixes the sign if there
* are no more leading digits
*/
Expand Down
2 changes: 1 addition & 1 deletion mp_cmp_mag.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* compare maginitude of two ints (unsigned) */
/* compare magnitude of two ints (unsigned) */
mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b)
{
int n;
Expand Down
2 changes: 1 addition & 1 deletion mp_exptmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/* this is a shell function that calls either the normal or Montgomery
* exptmod functions. Originally the call to the montgomery code was
* embedded in the normal function but that wasted alot of stack space
* embedded in the normal function but that wasted a lot of stack space
* for nothing (since 99% of the time the Montgomery code would be called)
*/
mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
Expand Down
2 changes: 1 addition & 1 deletion mp_lshd.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mp_err mp_lshd(mp_int *a, int b)
a->used += b;

/* much like mp_rshd this is implemented using a sliding window
* except the window goes the otherway around. Copying from
* except the window goes the other way around. Copying from
* the bottom to the top. see mp_rshd.c for more info.
*/
for (x = a->used; x --> b;) {
Expand Down
2 changes: 1 addition & 1 deletion mp_montgomery_calc_normalization.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* shifts with subtractions when the result is greater than b.
*
* 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 a lot of multiple precision shifting.
*/
mp_err mp_montgomery_calc_normalization(mp_int *a, const mp_int *b)
{
Expand Down
2 changes: 1 addition & 1 deletion mp_mul_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mp_err mp_mul_2(const mp_int *a, mp_int *b)
int x, oldused;
mp_digit r;

/* grow to accomodate result */
/* grow to accommodate result */
if ((err = mp_grow(b, a->used + 1)) != MP_OKAY) {
return err;
}
Expand Down
6 changes: 3 additions & 3 deletions mp_prime_is_prime.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
* 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
* it gets used for small embedded 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.
*/
Expand Down Expand Up @@ -213,12 +213,12 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, bool *result)
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
If the BPSW test and/or the additional 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
To preemptively answer the dangling question: no, a witness does not
need to be prime.
*/
for (ix = 0; ix < t; ix++) {
Expand Down
2 changes: 1 addition & 1 deletion mp_reduce_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/* SPDX-License-Identifier: Unlicense */

/* pre-calculate the value required for Barrett reduction
* For a given modulus "b" it calulates the value required in "a"
* For a given modulus "b" it calculates the value required in "a"
*/
mp_err mp_reduce_setup(mp_int *a, const mp_int *b)
{
Expand Down
2 changes: 1 addition & 1 deletion mtest/mpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ void mp_exch(mp_int *mp1, mp_int *mp2)
Release the storage used by an mp_int, and void its fields so that
if someone calls mp_clear() again for the same int later, we won't
get tollchocked.
get tolchocked.
*/

void mp_clear(mp_int *mp)
Expand Down
2 changes: 1 addition & 1 deletion s_mp_montgomery_reduce_comba.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mp_err s_mp_montgomery_reduce_comba(mp_int *x, const mp_int *n, mp_digit rho)
/* a = a + mu * m * b**i
*
* This is computed in place and on the fly. The multiplication
* by b**i is handled by offseting which columns the results
* by b**i is handled by offsetting which columns the results
* are added to.
*
* Note the comba method normally doesn't handle carries in the
Expand Down
2 changes: 1 addition & 1 deletion s_mp_mul_comba.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mp_err s_mp_mul_comba(const mp_int *a, const mp_int *b, mp_int *c, int digs)
ty = MP_MIN(b->used-1, ix);
tx = ix - ty;

/* this is the number of times the loop will iterrate, essentially
/* this is the number of times the loop will iterate, essentially
while (tx++ < a->used && ty-- >= 0) { ... }
*/
iy = MP_MIN(a->used-tx, ty+1);
Expand Down
2 changes: 1 addition & 1 deletion s_mp_mul_high_comba.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mp_err s_mp_mul_high_comba(const mp_int *a, const mp_int *b, mp_int *c, int digs
ty = MP_MIN(b->used-1, ix);
tx = ix - ty;

/* this is the number of times the loop will iterrate, essentially its
/* this is the number of times the loop will iterate, essentially its
while (tx++ < a->used && ty-- >= 0) { ... }
*/
iy = MP_MIN(a->used-tx, ty+1);
Expand Down
2 changes: 1 addition & 1 deletion s_mp_mul_karatsuba.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* are saved. Note also that the call to mp_mul can end up back
* in this function if the a0, a1, b0, or b1 are above the threshold.
* This is known as divide-and-conquer and leads to the famous
* O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than
* O(N**lg(3)) or O(N**1.584) work which is asymptotically lower than
* the standard O(N**2) that the baseline/comba methods use.
* Generally though the overhead of this method doesn't pay off
* until a certain size (N ~ 80) is reached.
Expand Down
2 changes: 1 addition & 1 deletion s_mp_sqr_comba.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mp_err s_mp_sqr_comba(const mp_int *a, mp_int *b)
ty = MP_MIN(a->used-1, ix);
tx = ix - ty;

/* this is the number of times the loop will iterrate, essentially
/* this is the number of times the loop will iterate, essentially
while (tx++ < a->used && ty-- >= 0) { ... }
*/
iy = MP_MIN(a->used-tx, ty+1);
Expand Down

0 comments on commit 96f9edf

Please sign in to comment.