Skip to content

Commit

Permalink
Merge pull request #38 from levitte/constify-math
Browse files Browse the repository at this point in the history
Constify all math functions
  • Loading branch information
sjaeckel authored Sep 19, 2024
2 parents 5510bbe + f050a73 commit ab5814d
Show file tree
Hide file tree
Showing 84 changed files with 1,206 additions and 1,170 deletions.
58 changes: 32 additions & 26 deletions doc/tfm.tex
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ \subsection{Initialize Copy}

\index{fp\_init\_copy}
\begin{verbatim}
void fp_init_copy(fp_int *a, fp_int *b)
void fp_init_copy(fp_int *a, const fp_int *b)
\end{verbatim}

This will initialize $a$ as a copy of $b$. Note that for compatibility with LibTomMath the function
Expand All @@ -303,9 +303,9 @@ \section{Odds and Evens}

\index{fp\_iszero} \index{fp\_iseven} \index{fp\_isodd}
\begin{verbatim}
int fp_iszero(fp_int *a);
int fp_iseven(fp_int *a);
int fp_isodd(fp_int *a);
int fp_iszero(const fp_int *a);
int fp_iseven(const fp_int *a);
int fp_isodd(const fp_int *a);
\end{verbatim}

These will return \textbf{FP\_YES} if the answer to their respective questions is yes. Otherwise they
Expand All @@ -317,9 +317,10 @@ \section{Sign Manipulation}

\index{fp\_neg} \index{fp\_abs}
\begin{verbatim}
void fp_neg(fp_int *a, fp_int *b);
void fp_abs(fp_int *a, fp_int *b);
void fp_neg(fp_int *a, const fp_int *b);
void fp_abs(fp_int *a, const fp_int *b);
\end{verbatim}

This will compute the negation (or absolute) of $a$ and store the result in $b$. Note that these
are implemented as macros and as such you should avoid using ++ or --~-- operators on the input
operand.
Expand All @@ -329,9 +330,10 @@ \section{Comparisons}

\index{fp\_cmp} \index{fp\_cmp\_mag}
\begin{verbatim}
int fp_cmp(fp_int *a, fp_int *b);
int fp_cmp_mag(fp_int *a, fp_int *b);
int fp_cmp(const fp_int *a, const fp_int *b);
int fp_cmp_mag(const fp_int *a, const fp_int *b);
\end{verbatim}

These will compare $a$ to $b$. They will return \textbf{FP\_GT} if $a$ is larger than $b$,
\textbf{FP\_EQ} if they are equal and \textbf{FP\_LT} if $a$ is less than $b$.

Expand All @@ -352,13 +354,14 @@ \section{Shifting}

\index{fp\_div\_2d} \index{fp\_mod\_2d} \index{fp\_mul\_2d} \index{fp\_div\_2} \index{fp\_mul\_2}
\begin{verbatim}
void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d);
void fp_mod_2d(fp_int *a, int b, fp_int *c);
void fp_mul_2d(fp_int *a, int b, fp_int *c);
void fp_mul_2(fp_int *a, fp_int *c);
void fp_div_2(fp_int *a, fp_int *c);
void fp_2expt(fp_int *a, int b);
void fp_div_2d(const fp_int *a, int b, fp_int *c, fp_int *d);
void fp_mod_2d(const fp_int *a, int b, fp_int *c);
void fp_mul_2d(const fp_int *a, int b, fp_int *c);
void fp_mul_2(const fp_int *a, fp_int *c);
void fp_div_2(const fp_int *a, fp_int *c);
void fp_2expt(const fp_int *a, int b);
\end{verbatim}

fp\_div\_2d() will divide $a$ by $2^b$ and store the quotient in $c$ and remainder in $d$. Either of
$c$ or $d$ can be \textbf{NULL} if their value is not required. fp\_mod\_2d() is a shortcut to
compute the remainder directly. fp\_mul\_2d() will multiply $a$ by $2^b$ and store the result in $c$.
Expand All @@ -370,8 +373,9 @@ \section{Shifting}

\index{fp\_cnt\_lsb}
\begin{verbatim}
int fp_cnt_lsb(fp_int *a);
int fp_cnt_lsb(const fp_int *a);
\end{verbatim}

This will return the number of adjacent least significant bits that are zero. This is equivalent
to the number of times two evenly divides $a$.

Expand All @@ -381,12 +385,12 @@ \section{Basic Algebra}

\index{fp\_add} \index{fp\_sub} \index{fp\_mul} \index{fp\_sqr} \index{fp\_div} \index{fp\_mod}
\begin{verbatim}
void fp_add(fp_int *a, fp_int *b, fp_int *c);
void fp_sub(fp_int *a, fp_int *b, fp_int *c);
void fp_mul(fp_int *a, fp_int *b, fp_int *c);
void fp_sqr(fp_int *a, fp_int *b);
int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
int fp_mod(fp_int *a, fp_int *b, fp_int *c);
void fp_add(const fp_int *a, const fp_int *b, fp_int *c);
void fp_sub(const fp_int *a, const fp_int *b, fp_int *c);
void fp_mul(const fp_int *a, const fp_int *b, fp_int *c);
void fp_sqr(const fp_int *a, fp_int *b);
int fp_div(const fp_int *a, const fp_int *b, fp_int *c, fp_int *d);
int fp_mod(const fp_int *a, const fp_int *b, fp_int *c);
\end{verbatim}

The functions fp\_add(), fp\_sub() and fp\_mul() perform their respective operations on $a$ and
Expand All @@ -402,8 +406,9 @@ \section{Modular Exponentiation}

\index{fp\_exptmod}
\begin{verbatim}
int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
int fp_exptmod(const fp_int *a, const fp_int *b, const fp_int *c, fp_int *d);
\end{verbatim}

This computes $d \equiv a^b \mbox{ (mod }c\mbox{)}$ for any odd $c$ and $b$. $b$ may be negative so long as
$a^{-1} \mbox{ (mod }c\mbox{)}$ exists. The initial value of $a$ may be larger than $c$. The size of $c$ must be
half of the maximum precision used during the build of the library. For example, by default $c$ must be less
Expand All @@ -416,9 +421,9 @@ \section{Number Theoretic}

\index{fp\_invmod} \index{fp\_gcd} \index{fp\_lcm}
\begin{verbatim}
int fp_invmod(fp_int *a, fp_int *b, fp_int *c);
void fp_gcd(fp_int *a, fp_int *b, fp_int *c);
void fp_lcm(fp_int *a, fp_int *b, fp_int *c);
int fp_invmod(const fp_int *a, const fp_int *b, fp_int *c);
void fp_gcd(const fp_int *a, const fp_int *b, fp_int *c);
void fp_lcm(const fp_int *a, const fp_int *b, fp_int *c);
\end{verbatim}

The fp\_invmod() function will find the modular inverse of $a$ modulo an odd modulus $b$ and store
Expand All @@ -432,8 +437,9 @@ \section{Prime Numbers}
\index{fp\_isprime}
\index{fp\_isprime\_ex}
\begin{verbatim}
int fp_isprime_ex(fp_int *a, int t);
int fp_isprime_ex(const fp_int *a, int t);
\end{verbatim}

This will return \textbf{FP\_YES} if $a$ is probably prime. It uses 256 trial divisions and
$t$ rounds of Rabin-Miller testing. Note that this routine performs modular exponentiations
which means that $a$ must be in a valid range of precision.
Expand Down
2 changes: 1 addition & 1 deletion src/addsub/fp_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* SPDX-License-Identifier: Unlicense */
#include <tfm_private.h>

void fp_add(fp_int *a, fp_int *b, fp_int *c)
void fp_add(const fp_int *a, const fp_int *b, fp_int *c)
{
int sa, sb;

Expand Down
2 changes: 1 addition & 1 deletion src/addsub/fp_add_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* c = a + b */
void fp_add_d(fp_int *a, fp_digit b, fp_int *c)
void fp_add_d(const fp_int *a, fp_digit b, fp_int *c)
{
fp_int tmp;
fp_set(&tmp, b);
Expand Down
2 changes: 1 addition & 1 deletion src/addsub/fp_addmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* d = a + b (mod c) */
int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
int fp_addmod(const fp_int *a, const fp_int *b, const fp_int *c, fp_int *d)
{
fp_int tmp;
fp_zero(&tmp);
Expand Down
2 changes: 1 addition & 1 deletion src/addsub/fp_cmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* SPDX-License-Identifier: Unlicense */
#include <tfm_private.h>

int fp_cmp(fp_int *a, fp_int *b)
int fp_cmp(const fp_int *a, const fp_int *b)
{
if (a->sign == FP_NEG && b->sign == FP_ZPOS) {
return FP_LT;
Expand Down
2 changes: 1 addition & 1 deletion src/addsub/fp_cmp_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* compare against a single digit */
int fp_cmp_d(fp_int *a, fp_digit b)
int fp_cmp_d(const fp_int *a, fp_digit b)
{
/* compare based on sign */
if ((b && a->used == 0) || a->sign == FP_NEG) {
Expand Down
2 changes: 1 addition & 1 deletion src/addsub/fp_cmp_mag.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* SPDX-License-Identifier: Unlicense */
#include <tfm_private.h>

int fp_cmp_mag(fp_int *a, fp_int *b)
int fp_cmp_mag(const fp_int *a, const fp_int *b)
{
int x;

Expand Down
2 changes: 1 addition & 1 deletion src/addsub/fp_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* c = a - b */
void fp_sub(fp_int *a, fp_int *b, fp_int *c)
void fp_sub(const fp_int *a, const fp_int *b, fp_int *c)
{
int sa, sb;

Expand Down
2 changes: 1 addition & 1 deletion src/addsub/fp_sub_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* c = a - b */
void fp_sub_d(fp_int *a, fp_digit b, fp_int *c)
void fp_sub_d(const fp_int *a, fp_digit b, fp_int *c)
{
fp_int tmp;
fp_set(&tmp, b);
Expand Down
2 changes: 1 addition & 1 deletion src/addsub/fp_submod.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* d = a - b (mod c) */
int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
int fp_submod(const fp_int *a, const fp_int *b, const fp_int *c, fp_int *d)
{
fp_int tmp;
fp_zero(&tmp);
Expand Down
2 changes: 1 addition & 1 deletion src/addsub/s_fp_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* unsigned addition */
void s_fp_add(fp_int *a, fp_int *b, fp_int *c)
void s_fp_add(const fp_int *a, const fp_int *b, fp_int *c)
{
int x, y, oldused;
register fp_word t;
Expand Down
2 changes: 1 addition & 1 deletion src/addsub/s_fp_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* unsigned subtraction ||a|| >= ||b|| ALWAYS! */
void s_fp_sub(fp_int *a, fp_int *b, fp_int *c)
void s_fp_sub(const fp_int *a, const fp_int *b, fp_int *c)
{
int x, oldbused, oldused;
fp_word t;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/fp_radix_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* SPDX-License-Identifier: Unlicense */
#include <tfm_private.h>

int fp_radix_size(fp_int *a, int radix, int *size)
int fp_radix_size(const fp_int *a, int radix, int *size)
{
fp_int t;
fp_digit d;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/fp_signed_bin_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* SPDX-License-Identifier: Unlicense */
#include <tfm_private.h>

int fp_signed_bin_size(fp_int *a)
int fp_signed_bin_size(const fp_int *a)
{
return 1 + fp_unsigned_bin_size (a);
}
2 changes: 1 addition & 1 deletion src/bin/fp_to_signed_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* SPDX-License-Identifier: Unlicense */
#include <tfm_private.h>

void fp_to_signed_bin(fp_int *a, unsigned char *b)
void fp_to_signed_bin(const fp_int *a, unsigned char *b)
{
fp_to_unsigned_bin (a, b + 1);
b[0] = (unsigned char) ((a->sign == FP_ZPOS) ? 0 : 1);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/fp_to_unsigned_bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* SPDX-License-Identifier: Unlicense */
#include <tfm_private.h>

void fp_to_unsigned_bin(fp_int *a, unsigned char *b)
void fp_to_unsigned_bin(const fp_int *a, unsigned char *b)
{
int x;
fp_int t;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/fp_toradix.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* Return: FP_VAL on error, FP_OKAY on success.
*/
int fp_toradix(fp_int *a, char *str, int radix)
int fp_toradix(const fp_int *a, char *str, int radix)
{
return fp_toradix_n(a, str, radix, INT_MAX);
}
2 changes: 1 addition & 1 deletion src/bin/fp_toradix_n.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* SPDX-License-Identifier: Unlicense */
#include <tfm_private.h>

int fp_toradix_n(fp_int *a, char *str, int radix, int maxlen)
int fp_toradix_n(const fp_int *a, char *str, int radix, int maxlen)
{
int digs;
fp_int t;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/fp_unsigned_bin_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* SPDX-License-Identifier: Unlicense */
#include <tfm_private.h>

int fp_unsigned_bin_size(fp_int *a)
int fp_unsigned_bin_size(const fp_int *a)
{
int size = fp_count_bits (a);
return (size / 8 + ((size & 7) != 0 ? 1 : 0));
Expand Down
2 changes: 1 addition & 1 deletion src/bit/fp_cnt_lsb.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static const int lnz[16] = {
};

/* Counts the number of lsbs which are zero before the first zero bit */
int fp_cnt_lsb(fp_int *a)
int fp_cnt_lsb(const fp_int *a)
{
int x;
fp_digit q, qq;
Expand Down
2 changes: 1 addition & 1 deletion src/bit/fp_count_bits.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* SPDX-License-Identifier: Unlicense */
#include <tfm_private.h>

int fp_count_bits (fp_int * a)
int fp_count_bits (const fp_int * a)
{
int r;
fp_digit q;
Expand Down
5 changes: 3 additions & 2 deletions src/bit/fp_div_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
#include <tfm_private.h>

/* b = a/2 */
void fp_div_2(fp_int * a, fp_int * b)
void fp_div_2(const fp_int * a, fp_int * b)
{
int x, oldused;

oldused = b->used;
b->used = a->used;
{
register fp_digit r, rr, *tmpa, *tmpb;
register const fp_digit *tmpa;
register fp_digit r, rr, *tmpb;

/* source alias */
tmpa = a->dp + b->used - 1;
Expand Down
2 changes: 1 addition & 1 deletion src/bit/fp_div_2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* c = a / 2**b */
void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d)
void fp_div_2d(const fp_int *a, int b, fp_int *c, fp_int *d)
{
fp_digit D, r, rr;
int x;
Expand Down
2 changes: 1 addition & 1 deletion src/bit/fp_mod_2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* c = a mod 2**d */
void fp_mod_2d(fp_int *a, int b, fp_int *c)
void fp_mod_2d(const fp_int *a, int b, fp_int *c)
{
int x;

Expand Down
2 changes: 1 addition & 1 deletion src/divide/fp_div.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* a/b => cb + d == a */
int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
int fp_div(const fp_int *a, const fp_int *b, fp_int *c, fp_int *d)
{
fp_int q, x, y, t1, t2;
int n, t, i, norm, neg;
Expand Down
2 changes: 1 addition & 1 deletion src/divide/fp_div_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static int s_is_power_of_two(fp_digit b, int *p)
}

/* a/b => cb + d == a */
int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d)
int fp_div_d(const fp_int *a, fp_digit b, fp_int *c, fp_digit *d)
{
fp_int q;
fp_word w;
Expand Down
2 changes: 1 addition & 1 deletion src/divide/fp_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* c = a mod b, 0 <= c < b */
int fp_mod(fp_int *a, fp_int *b, fp_int *c)
int fp_mod(const fp_int *a, const fp_int *b, fp_int *c)
{
fp_int t;
int err;
Expand Down
2 changes: 1 addition & 1 deletion src/divide/fp_mod_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <tfm_private.h>

/* c = a mod b, 0 <= c < b */
int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c)
int fp_mod_d(const fp_int *a, fp_digit b, fp_digit *c)
{
return fp_div_d(a, b, NULL, c);
}
Loading

0 comments on commit ab5814d

Please sign in to comment.