diff --git a/CMakeLists.txt b/CMakeLists.txt index 63b62ac9b..8f8524912 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") @@ -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") diff --git a/astylerc b/astylerc index c5ff77940..029f35849 100644 --- a/astylerc +++ b/astylerc @@ -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 diff --git a/changes.txt b/changes.txt index 1b3a7a3a4..565500b51 100644 --- a/changes.txt +++ b/changes.txt @@ -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. @@ -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 @@ -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 diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt index 06ff898b4..c44b7c23e 100644 --- a/demo/CMakeLists.txt +++ b/demo/CMakeLists.txt @@ -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") diff --git a/demo/mtest_opponent.c b/demo/mtest_opponent.c index 25d9b5b90..abd7c1614 100644 --- a/demo/mtest_opponent.c +++ b/demo/mtest_opponent.c @@ -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); diff --git a/demo/test.c b/demo/test.c index c87f97c00..16fef5570 100644 --- a/demo/test.c +++ b/demo/test.c @@ -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. */ @@ -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)); @@ -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"); diff --git a/doc/bn.tex b/doc/bn.tex index 3e01e32df..566b3be32 100644 --- a/doc/bn.tex +++ b/doc/bn.tex @@ -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 @@ -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. @@ -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 diff --git a/doc/makefile b/doc/makefile index 84c48e4b2..a8c6a6539 100644 --- a/doc/makefile +++ b/doc/makefile @@ -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 diff --git a/logs/before_after.dem b/logs/before_after.dem index edb59da49..33721ad20 100644 --- a/logs/before_after.dem +++ b/logs/before_after.dem @@ -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)", \ diff --git a/logs/graphs.dem b/logs/graphs.dem index 538e5c075..fc8ef1bc9 100644 --- a/logs/graphs.dem +++ b/logs/graphs.dem @@ -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" diff --git a/makefile b/makefile index 7fa04d2ca..666274378 100644 --- a/makefile +++ b/makefile @@ -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 @@ -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..." @@ -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" diff --git a/makefile_include.mk b/makefile_include.mk index dca5c3bc9..f88d18be1 100644 --- a/makefile_include.mk +++ b/makefile_include.mk @@ -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 diff --git a/mp_2expt.c b/mp_2expt.c index 66e857478..4a5fc0063 100644 --- a/mp_2expt.c +++ b/mp_2expt.c @@ -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; } diff --git a/mp_clamp.c b/mp_clamp.c index ae59c4016..463f22dc0 100644 --- a/mp_clamp.c +++ b/mp_clamp.c @@ -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 */ diff --git a/mp_cmp_mag.c b/mp_cmp_mag.c index e5e502b8c..06f22e7ce 100644 --- a/mp_cmp_mag.c +++ b/mp_cmp_mag.c @@ -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; diff --git a/mp_exptmod.c b/mp_exptmod.c index b8a5dccc2..eaab861eb 100644 --- a/mp_exptmod.c +++ b/mp_exptmod.c @@ -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) diff --git a/mp_lshd.c b/mp_lshd.c index bfa8af88b..90014e480 100644 --- a/mp_lshd.c +++ b/mp_lshd.c @@ -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;) { diff --git a/mp_montgomery_calc_normalization.c b/mp_montgomery_calc_normalization.c index cc07799dc..bbb3adbc1 100644 --- a/mp_montgomery_calc_normalization.c +++ b/mp_montgomery_calc_normalization.c @@ -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) { diff --git a/mp_mul_2.c b/mp_mul_2.c index 7d7084b31..459fbd29d 100644 --- a/mp_mul_2.c +++ b/mp_mul_2.c @@ -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; } diff --git a/mp_prime_is_prime.c b/mp_prime_is_prime.c index 7d73864c7..bb24f5944 100644 --- a/mp_prime_is_prime.c +++ b/mp_prime_is_prime.c @@ -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. */ @@ -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++) { diff --git a/mp_reduce_setup.c b/mp_reduce_setup.c index e12056e1e..2ce5b96f0 100644 --- a/mp_reduce_setup.c +++ b/mp_reduce_setup.c @@ -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) { diff --git a/mtest/mpi.c b/mtest/mpi.c index faf09d8df..f9cc4290d 100644 --- a/mtest/mpi.c +++ b/mtest/mpi.c @@ -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) diff --git a/s_mp_montgomery_reduce_comba.c b/s_mp_montgomery_reduce_comba.c index 6f249c49f..7472caf34 100644 --- a/s_mp_montgomery_reduce_comba.c +++ b/s_mp_montgomery_reduce_comba.c @@ -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 diff --git a/s_mp_mul_comba.c b/s_mp_mul_comba.c index 07dd7913d..1afa1fc68 100644 --- a/s_mp_mul_comba.c +++ b/s_mp_mul_comba.c @@ -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); diff --git a/s_mp_mul_high_comba.c b/s_mp_mul_high_comba.c index 317346dfa..74960aca6 100644 --- a/s_mp_mul_high_comba.c +++ b/s_mp_mul_high_comba.c @@ -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); diff --git a/s_mp_mul_karatsuba.c b/s_mp_mul_karatsuba.c index bf9271f3b..b46529837 100644 --- a/s_mp_mul_karatsuba.c +++ b/s_mp_mul_karatsuba.c @@ -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. diff --git a/s_mp_sqr_comba.c b/s_mp_sqr_comba.c index cb88dcc9e..1bcc1f93f 100644 --- a/s_mp_sqr_comba.c +++ b/s_mp_sqr_comba.c @@ -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);