Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions bn_mp_grow.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ int mp_grow(mp_int *a, int size)

/* if the alloc size is smaller alloc more ram */
if (a->alloc < size) {
/* ensure there are always at least MP_PREC digits extra on top */
size += (MP_PREC * 2) - (size % MP_PREC);

/* reallocate the array a->dp
*
* We store the return in a temporary variable
Expand Down
16 changes: 4 additions & 12 deletions bn_mp_ilogb.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ int mp_ilogb(mp_int *a, mp_digit base, mp_int *c)
}
if (base == 2u) {
cmp = mp_count_bits(a) - 1;
if ((err = mp_set_int(c, (unsigned long)cmp)) != MP_OKAY) {
goto LBL_ERR;
}
mp_set_int(c, (unsigned long)cmp);
return err;
}
if (a->used == 1) {
Expand Down Expand Up @@ -165,21 +163,15 @@ int mp_ilogb(mp_int *a, mp_digit base, mp_int *c)
mp_exch(&bracket_mid, &bracket_low);
}
if (cmp == MP_EQ) {
if ((err = mp_set_int(c, (unsigned long)mid)) != MP_OKAY) {
goto LBL_ERR;
}
mp_set_int(c, (unsigned long)mid);
goto LBL_END;
}
}

if (mp_cmp(&bracket_high, a) == MP_EQ) {
if ((err = mp_set_int(c, (unsigned long)high)) != MP_OKAY) {
goto LBL_ERR;
}
mp_set_int(c, (unsigned long)high);
} else {
if ((err = mp_set_int(c, (unsigned long)low)) != MP_OKAY) {
goto LBL_ERR;
}
mp_set_int(c, (unsigned long)low);
}

LBL_END:
Expand Down
3 changes: 1 addition & 2 deletions bn_mp_init_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
/* init an mp_init for a given size */
int mp_init_size(mp_int *a, int size)
{
/* pad size so there are always extra digits */
size += (MP_PREC * 2) - (size % MP_PREC);
size = MP_MAX(MP_MIN_PREC, size);

/* alloc mem */
a->dp = (mp_digit *) MP_CALLOC((size_t)size, sizeof(mp_digit));
Expand Down
12 changes: 3 additions & 9 deletions bn_mp_prime_frobenius_underwood.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ int mp_prime_frobenius_underwood(const mp_int *N, int *result)
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;
}
mp_set_long(&T1z, (unsigned long)a);

if ((e = mp_sqr(&T1z, &T1z)) != MP_OKAY) {
goto LBL_FU_ERR;
Expand Down Expand Up @@ -74,9 +72,7 @@ int mp_prime_frobenius_underwood(const mp_int *N, int *result)
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;
}
mp_set_long(&T1z, (unsigned long)((a+4)*((2*a)+5)));

if ((e = mp_gcd(N, &T1z, &T1z)) != MP_OKAY) {
goto LBL_FU_ERR;
Expand Down Expand Up @@ -165,9 +161,7 @@ int mp_prime_frobenius_underwood(const mp_int *N, int *result)
}
}

if ((e = mp_set_long(&T1z, (unsigned long)((2 * a) + 5))) != MP_OKAY) {
goto LBL_FU_ERR;
}
mp_set_long(&T1z, (unsigned long)((2 * a) + 5));
if ((e = mp_mod(&T1z, N, &T1z)) != MP_OKAY) {
goto LBL_FU_ERR;
}
Expand Down
24 changes: 6 additions & 18 deletions bn_mp_prime_strong_lucas_selfridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ static int s_mp_mul_si(const mp_int *a, long d, mp_int *c)
* 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;
}
mp_set_long(&t, (unsigned long) d);
if ((err = mp_mul(a, &t, c)) != MP_OKAY) {
goto LBL_MPMULSI_ERR;
}
Expand Down Expand Up @@ -95,9 +93,7 @@ int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)
for (;;) {
Ds = sign * D;
sign = -sign;
if ((e = mp_set_long(&Dz, (unsigned long)D)) != MP_OKAY) {
goto LBL_LS_ERR;
}
mp_set_long(&Dz, (unsigned long)D);
if ((e = mp_gcd(a, &Dz, &gcd)) != MP_OKAY) {
goto LBL_LS_ERR;
}
Expand Down Expand Up @@ -193,31 +189,23 @@ int mp_prime_strong_lucas_selfridge(const mp_int *a, int *result)

if (Q < 0) {
Q = -Q;
if ((e = mp_set_long(&Qmz, (unsigned long)Q)) != MP_OKAY) {
goto LBL_LS_ERR;
}
mp_set_long(&Qmz, (unsigned long)Q);
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;
}
mp_set_long(&Qkdz, (unsigned long)Q);
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;
}
mp_set_long(&Qmz, (unsigned long)Q);
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;
}
mp_set_long(&Qkdz, (unsigned long)Q);
}

Nbits = mp_count_bits(&Dz);
Expand Down
5 changes: 1 addition & 4 deletions bn_mp_set_double.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ int mp_set_double(mp_int *a, double b)
}
exp -= 1023 + 52;

res = mp_set_long_long(a, frac);
if (res != MP_OKAY) {
return res;
}
mp_set_long_long(a, frac);

res = (exp < 0) ? mp_div_2d(a, -exp, a, NULL) : mp_mul_2d(a, exp, a);
if (res != MP_OKAY) {
Expand Down
13 changes: 4 additions & 9 deletions bn_mp_shrink.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@
int mp_shrink(mp_int *a)
{
mp_digit *tmp;
int used = 1;

if (a->used > 0) {
used = a->used;
}

if (a->alloc != used) {
int alloc = MP_MAX(MP_MIN_PREC, a->used);
if (a->alloc != alloc) {
if ((tmp = (mp_digit *) MP_REALLOC(a->dp,
(size_t)a->alloc * sizeof(mp_digit),
(size_t)used * sizeof(mp_digit))) == NULL) {
(size_t)alloc * sizeof(mp_digit))) == NULL) {
return MP_MEM;
}
a->dp = tmp;
a->alloc = used;
a->alloc = alloc;
}
return MP_OKAY;
}
Expand Down
4 changes: 2 additions & 2 deletions bn_mp_sqrtmod_prime.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
}

/* find a Z such that the Legendre symbol (Z|prime) == -1 */
if ((res = mp_set_int(&Z, 2uL)) != MP_OKAY) goto cleanup;
mp_set_int(&Z, 2uL);
/* Z = 2 */
while (1) {
if ((res = mp_jacobi(&Z, prime, &legendre)) != MP_OKAY) goto cleanup;
Expand All @@ -78,7 +78,7 @@ int mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
/* T = n ^ Q mod prime */
if ((res = mp_copy(&S, &M)) != MP_OKAY) goto cleanup;
/* M = S */
if ((res = mp_set_int(&two, 2uL)) != MP_OKAY) goto cleanup;
mp_set_int(&two, 2uL);

res = MP_VAL;
while (1) {
Expand Down
10 changes: 2 additions & 8 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,10 +599,7 @@ static int test_mp_get_long(void)
t = ~0UL;
printf(" t = 0x%lx i = %d\r", t, i);
do {
if (mp_set_long(&a, t) != MP_OKAY) {
printf("\nmp_set_long() error!");
goto LBL_ERR;
}
mp_set_long(&a, t);
s = mp_get_long(&a);
if (s != t) {
printf("\nmp_get_long() bad result! 0x%lx != 0x%lx", s, t);
Expand Down Expand Up @@ -635,10 +632,7 @@ static int test_mp_get_long_long(void)
r = ~0ULL;
printf(" r = 0x%llx i = %d\r", r, i);
do {
if (mp_set_long_long(&a, r) != MP_OKAY) {
printf("\nmp_set_long_long() error!");
goto LBL_ERR;
}
mp_set_long_long(&a, r);
q = mp_get_long_long(&a);
if (q != r) {
printf("\nmp_get_long_long() bad result! 0x%llx != 0x%llx", q, r);
Expand Down
23 changes: 5 additions & 18 deletions doc/bn.tex
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ \subsubsection{OpenBSD}
cc -I./ -Wall -Wsign-compare -Wextra -Wshadow -Wsystem-headers -Wdeclaration-afo...
cc -I./ -Wall -Wsign-compare -Wextra -Wshadow -Wsystem-headers -Wdeclaration-afo...
cc -I./ -Wall -Wsign-compare -Wextra -Wshadow -Wsystem-headers -Wdeclaration-afo...
libtool --mode=link --tag=CC cc bn_error.lo bn_s_mp_invmod_fast.lo bn_fast_mp_mo
libtool --mode=link --tag=CC cc bn_error.lo bn_s_mp_invmod_fast.lo bn_fast_mp_mo
libtool: link: cc bn_error.lo bn_s_mp_invmod_fast.lo bn_s_mp_montgomery_reduce_fast0
bn_error.lo: file not recognized: File format not recognized
cc: error: linker command failed with exit code 1 (use -v to see invocation)
Expand All @@ -187,7 +187,7 @@ \subsubsection{OpenBSD}
\end{alltt}
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}
\begin{alltt}
$ /usr/local/bin/libtool --version
$ /usr/local/bin/libtool --version
libtool (GNU libtool) 2.4.2
Written by Gordon Matzigkeit <gord@gnu.ai.mit.edu>, 1996

Expand Down Expand Up @@ -856,11 +856,7 @@ \subsection{Long Constants}
\}

/* set the number to 654321 (note this is bigger than 127) */
if ((result = mp_set_int(&number, 654321)) != MP_OKAY) \{
printf("Error setting the value of the number. \%s",
mp_error_to_string(result));
return EXIT_FAILURE;
\}
mp_set_int(&number, 654321);

printf("number == \%lu", mp_get_int(&number));

Expand Down Expand Up @@ -1396,17 +1392,8 @@ \section{Multiplication}
\}

/* set the terms */
if ((result = mp_set_int(&number, 257)) != MP_OKAY) \{
printf("Error setting number1. \%s",
mp_error_to_string(result));
return EXIT_FAILURE;
\}

if ((result = mp_set_int(&number2, 1023)) != MP_OKAY) \{
printf("Error setting number2. \%s",
mp_error_to_string(result));
return EXIT_FAILURE;
\}
mp_set_int(&number, 257);
mp_set_int(&number2, 1023);

/* multiply them */
if ((result = mp_mul(&number1, &number2,
Expand Down
4 changes: 1 addition & 3 deletions doc/tommath.src
Original file line number Diff line number Diff line change
Expand Up @@ -947,9 +947,7 @@ correct no further memory re-allocations are required to work with the mp\_int.

EXAM,bn_mp_init_size.c

The number of digits $b$ requested is padded (line @22,MP_PREC@) by first augmenting it to the next multiple of
\textbf{MP\_PREC} and then adding \textbf{MP\_PREC} to the result. If the memory can be successfully allocated the
mp\_int is placed in a default state representing the integer zero. Otherwise, the error code \textbf{MP\_MEM} will be
If the memory can be successfully allocated the mp\_int is placed in a default state representing the integer zero. Otherwise, the error code \textbf{MP\_MEM} will be
returned (line @27,return@).

The digits are allocated with the malloc() function (line @27,XMALLOC@) and set to zero afterwards (line @38,for@). The
Expand Down
2 changes: 2 additions & 0 deletions tommath.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ TOOM_SQR_CUTOFF;
#ifndef MP_PREC
# ifndef MP_LOW_MEM
# define MP_PREC 32 /* default digits of precision */
# elif defined(MP_8BIT)
# define MP_PREC 16 /* default digits of precision */
# else
# define MP_PREC 8 /* default digits of precision */
# endif
Expand Down
21 changes: 10 additions & 11 deletions tommath_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ extern void MP_FREE(void *mem, size_t size);
#define MP_IS_EVEN(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
#define MP_IS_ODD(a) (((a)->used > 0) && (((a)->dp[0] & 1u) == 1u))

/* Minimum number of available digits in mp_int, MP_PREC >= MP_MIN_PREC */
#define MP_MIN_PREC ((CHAR_BIT * (int)sizeof(long long) + MP_DIGIT_BIT - 1) / MP_DIGIT_BIT)

/* lowlevel functions, do not call! */
int s_mp_add(const mp_int *a, const mp_int *b, mp_int *c);
int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c);
Expand Down Expand Up @@ -101,18 +104,14 @@ extern const size_t mp_s_rmap_reverse_sz;
int func_name (mp_int * a, type b) \
{ \
int x = 0; \
int new_size = (((CHAR_BIT * sizeof(type)) + MP_DIGIT_BIT) - 1) / MP_DIGIT_BIT; \
int res = mp_grow(a, new_size); \
if (res == MP_OKAY) { \
mp_zero(a); \
while (b != 0u) { \
a->dp[x++] = ((mp_digit)b & MP_MASK); \
if ((CHAR_BIT * sizeof (b)) <= MP_DIGIT_BIT) { break; } \
b >>= (((CHAR_BIT * sizeof (b)) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
} \
a->used = x; \
mp_zero(a); \
while (b != 0u) { \
a->dp[x++] = ((mp_digit)b & MP_MASK); \
if ((CHAR_BIT * sizeof (b)) <= MP_DIGIT_BIT) { break; } \
b >>= (((CHAR_BIT * sizeof (b)) <= MP_DIGIT_BIT) ? 0 : MP_DIGIT_BIT); \
} \
return res; \
a->used = x; \
return MP_OKAY; \
}

/* deprecated functions */
Expand Down