Skip to content
Merged
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
7 changes: 0 additions & 7 deletions bn_mp_from_ubin.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,8 @@ mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size)
if ((err = mp_mul_2d(a, 8, a)) != MP_OKAY) {
return err;
}

#ifndef MP_8BIT
a->dp[0] |= *buf++;
a->used += 1;
#else
a->dp[0] = (*buf & MP_MASK);
a->dp[1] |= ((*buf++ >> 7) & 1u);
a->used += 2;
#endif
}
mp_clamp(a);
return MP_OKAY;
Expand Down
4 changes: 1 addition & 3 deletions bn_mp_montgomery_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ mp_err mp_montgomery_setup(const mp_int *n, mp_digit *rho)

x = (((b + 2u) & 4u) << 1) + b; /* here x*a==1 mod 2**4 */
x *= 2u - (b * x); /* here x*a==1 mod 2**8 */
#if !defined(MP_8BIT)
x *= 2u - (b * x); /* here x*a==1 mod 2**16 */
#endif
#if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
#if defined(MP_64BIT) || !(defined(MP_16BIT))
x *= 2u - (b * x); /* here x*a==1 mod 2**32 */
#endif
#ifdef MP_64BIT
Expand Down
5 changes: 1 addition & 4 deletions bn_mp_prime_frobenius_underwood.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
#ifndef LTM_USE_ONLY_MR

#ifdef MP_8BIT
/*
* floor of positive solution of
* (2^16)-1 = (a+4)*(2*a+5)
Expand All @@ -19,10 +18,8 @@
* 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

mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
{
mp_int T1z, T2z, Np1z, sz, tz;
Expand Down
35 changes: 1 addition & 34 deletions bn_mp_prime_is_prime.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
return MP_OKAY;
}
}
#ifdef MP_8BIT
/* The search in the loop above was exhaustive in this case */
if ((a->used == 1) && (MP_PRIME_TAB_SIZE >= 31)) {
return MP_OKAY;
}
#endif

/* first perform trial division */
if ((err = s_mp_prime_is_divisible(a, &res)) != MP_OKAY) {
return err;
Expand Down Expand Up @@ -112,7 +105,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
* 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)
#ifdef LTM_USE_FROBENIUS_TEST
err = mp_prime_frobenius_underwood(a, &res);
if ((err != MP_OKAY) && (err != MP_ITER)) {
goto LBL_B;
Expand Down Expand Up @@ -240,20 +233,6 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
* 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 ((MP_SIZEOF_BITS(unsigned int)/2) >= MP_SIZEOF_BITS(mp_digit)) {
if ((err = mp_rand(&b, 1)) != MP_OKAY) {
goto LBL_B;
}
fips_rand <<= MP_SIZEOF_BITS(mp_digit);
fips_rand |= (unsigned int) b.dp[0];
fips_rand &= mask;
}
#endif
if (fips_rand > (unsigned int)(INT_MAX - MP_DIGIT_BIT)) {
len = INT_MAX / MP_DIGIT_BIT;
} else {
Expand All @@ -264,18 +243,6 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
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;
}
Expand Down
7 changes: 0 additions & 7 deletions bn_mp_prime_strong_lucas_selfridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
*/
#ifndef LTM_USE_ONLY_MR

/*
* 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
Expand Down Expand Up @@ -286,4 +280,3 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result)
}
#endif
#endif
#endif
4 changes: 0 additions & 4 deletions bn_mp_to_ubin.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *wr
}

for (x = count; x --> 0u;) {
#ifndef MP_8BIT
buf[x] = (unsigned char)(t.dp[0] & 255u);
#else
buf[x] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7));
#endif
if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
goto LBL_ERR;
}
Expand Down
5 changes: 1 addition & 4 deletions bn_prime_tab.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const mp_digit s_mp_prime_tab[] = {
0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F,
#ifndef MP_8BIT
0x0083,
0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F, 0x0083,
0x0089, 0x008B, 0x0095, 0x0097, 0x009D, 0x00A3, 0x00A7, 0x00AD,
0x00B3, 0x00B5, 0x00BF, 0x00C1, 0x00C5, 0x00C7, 0x00D3, 0x00DF,
0x00E3, 0x00E5, 0x00E9, 0x00EF, 0x00F1, 0x00FB, 0x0101, 0x0107,
Expand Down Expand Up @@ -41,7 +39,6 @@ const mp_digit s_mp_prime_tab[] = {
0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
#endif
};

#endif
3 changes: 0 additions & 3 deletions demo/shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ void ndraw(mp_int *a, const char *name)

void print_header(void)
{
#ifdef MP_8BIT
printf("Digit size 8 Bit \n");
#endif
#ifdef MP_16BIT
printf("Digit size 16 Bit \n");
#endif
Expand Down
11 changes: 0 additions & 11 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,6 @@ static int test_mp_prime_is_prime(void)

}
/* Check regarding problem #143 */
#ifndef MP_8BIT
mp_read_radix(&a,
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF",
16);
Expand All @@ -1041,8 +1040,6 @@ static int test_mp_prime_is_prime(void)
putchar('\n');
goto LBL_ERR;
}
#endif

printf("\n\n");

mp_clear_multi(&a, &b, NULL);
Expand Down Expand Up @@ -2040,17 +2037,9 @@ static int test_mp_root_u32(void)
if ((e = mp_init_multi(&a, &c, &r, NULL)) != MP_OKAY) {
return EXIT_FAILURE;
}
#ifdef MP_8BIT
for (i = 0; i < 1; i++) {
#else
for (i = 0; i < 10; i++) {
#endif
mp_read_radix(&a, input[i], 64);
#ifdef MP_8BIT
for (j = 3; j < 10; j++) {
#else
for (j = 3; j < 100; j++) {
#endif
mp_root_u32(&a, (uint32_t)j, &c);
mp_read_radix(&r, root[i][j-3], 10);
if (mp_cmp(&r, &c) != MP_EQ) {
Expand Down
6 changes: 2 additions & 4 deletions doc/bn.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2035,9 +2035,7 @@ \section{Frobenius (Underwood) Test}
\begin{alltt}
int mp_prime_frobenius_underwood(const mp_int *N, int *result)
\end{alltt}
Performs the variant of the Frobenius test as described by Paul Underwood. The single internal use is in
\texttt{mp\_prime\_is\_prime} for \texttt{MP\_8BIT} only but can be included at build-time for all other sizes
if the preprocessor macro \texttt{LTM\_USE\_FROBENIUS\_TEST} is defined.
Performs the variant of the Frobenius test as described by Paul Underwood. It can be included at build-time if the preprocessor macro \texttt{LTM\_USE\_FROBENIUS\_TEST} is defined and will be used instead of the Lucas-Selfridge test.

It returns \texttt{MP\_ITER} if the number of iterations is exhausted, assumes a composite as the input and sets \texttt{result} accordingly. This will reduce the set of available pseudoprimes by a very small amount: test with large datasets (more than $10^{10}$ numbers, both randomly chosen and sequences of odd numbers with a random start point) found only 31 (thirty-one) numbers with $a > 120$ and none at all with just an additional simple check for divisors $d < 2^8$.

Expand All @@ -2053,7 +2051,7 @@ \section{Primality Testing}
\begin{alltt}
int mp_prime_is_prime (mp_int * a, int t, int *result)
\end{alltt}
This will perform a trial division followed by two rounds of Miller-Rabin with bases 2 and 3 and a Lucas-Selfridge test. The Lucas-Selfridge test is replaced with a Frobenius-Underwood for \texttt{MP\_8BIT}. The Frobenius-Underwood test for all other sizes is available as a compile-time option with the preprocessor macro \texttt{LTM\_USE\_FROBENIUS\_TEST}. See file
This will perform a trial division followed by two rounds of Miller-Rabin with bases 2 and 3 and a Lucas-Selfridge test. The Frobenius-Underwood is available as a compile-time option with the preprocessor macro \texttt{LTM\_USE\_FROBENIUS\_TEST}. See file
\texttt{bn\_mp\_prime\_is\_prime.c} for the necessary details. It shall be noted that both functions are much slower than
the Miller-Rabin test and if speed is an essential issue, the macro \texttt{LTM\_USE\_ONLY\_MR} switches both functions, the Frobenius-Underwood test and the Lucas-Selfridge test off and their code will not even be compiled into the library.

Expand Down
4 changes: 0 additions & 4 deletions mtest/mtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ mulmod

*/

#ifdef MP_8BIT
#define THE_MASK 127
#else
#define THE_MASK 32767
#endif

#include <stdio.h>
#include <stdlib.h>
Expand Down
2 changes: 0 additions & 2 deletions testme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,11 @@ do
then
_runvalgrind "$i $a" "$CFLAGS"
[ "$WITH_LOW_MP" != "1" ] && continue
_runvalgrind "$i $a" "-DMP_8BIT $CFLAGS"
_runvalgrind "$i $a" "-DMP_16BIT $CFLAGS"
_runvalgrind "$i $a" "-DMP_32BIT $CFLAGS"
else
_runtest "$i $a" "$CFLAGS"
[ "$WITH_LOW_MP" != "1" ] && continue
_runtest "$i $a" "-DMP_8BIT $CFLAGS"
_runtest "$i $a" "-DMP_16BIT $CFLAGS"
_runtest "$i $a" "-DMP_32BIT $CFLAGS"
fi
Expand Down
15 changes: 8 additions & 7 deletions tommath.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

#include <stdint.h>
#include <stddef.h>
#ifdef MP_8BIT
# error "Support of 8-bit architectures has been dropped in this version of LTM."
#endif


#ifndef MP_NO_FILE
# include <stdio.h>
Expand Down Expand Up @@ -35,7 +39,7 @@ extern "C" {
defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \
defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \
defined(__LP64__) || defined(_LP64) || defined(__64BIT__)
# if !(defined(MP_64BIT) || defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
# if !(defined(MP_64BIT) || defined(MP_32BIT) || defined(MP_16BIT))
# if defined(__GNUC__) && !defined(__hppa)
/* we support 128bit integers only via: __attribute__((mode(TI))) */
# define MP_64BIT
Expand All @@ -47,7 +51,7 @@ extern "C" {
#endif

#ifdef MP_DIGIT_BIT
# error Defining MP_DIGIT_BIT is disallowed, use MP_8/16/31/32/64BIT
# error Defining MP_DIGIT_BIT is disallowed, use MP_16/31/32/64BIT
#endif

/* some default configurations.
Expand All @@ -59,11 +63,8 @@ extern "C" {
* [any size beyond that is ok provided it doesn't overflow the data type]
*/

#ifdef MP_8BIT
typedef uint8_t mp_digit;
typedef uint16_t private_mp_word;
# define MP_DIGIT_BIT 7
#elif defined(MP_16BIT)

#if defined(MP_16BIT)
typedef uint16_t mp_digit;
typedef uint32_t private_mp_word;
# define MP_DIGIT_BIT 15
Expand Down