Skip to content

Commit 800ec1e

Browse files
authored
Merge pull request #349 from czurnieden/sans_eight
Remove support for 8-bit (MP_8BIT)
2 parents 4d6a968 + 78588ed commit 800ec1e

13 files changed

+14
-94
lines changed

bn_mp_from_ubin.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,8 @@ mp_err mp_from_ubin(mp_int *a, const unsigned char *buf, size_t size)
2323
if ((err = mp_mul_2d(a, 8, a)) != MP_OKAY) {
2424
return err;
2525
}
26-
27-
#ifndef MP_8BIT
2826
a->dp[0] |= *buf++;
2927
a->used += 1;
30-
#else
31-
a->dp[0] = (*buf & MP_MASK);
32-
a->dp[1] |= ((*buf++ >> 7) & 1u);
33-
a->used += 2;
34-
#endif
3528
}
3629
mp_clamp(a);
3730
return MP_OKAY;

bn_mp_montgomery_setup.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ mp_err mp_montgomery_setup(const mp_int *n, mp_digit *rho)
2424

2525
x = (((b + 2u) & 4u) << 1) + b; /* here x*a==1 mod 2**4 */
2626
x *= 2u - (b * x); /* here x*a==1 mod 2**8 */
27-
#if !defined(MP_8BIT)
2827
x *= 2u - (b * x); /* here x*a==1 mod 2**16 */
29-
#endif
30-
#if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
28+
#if defined(MP_64BIT) || !(defined(MP_16BIT))
3129
x *= 2u - (b * x); /* here x*a==1 mod 2**32 */
3230
#endif
3331
#ifdef MP_64BIT

bn_mp_prime_frobenius_underwood.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
#ifndef LTM_USE_ONLY_MR
1111

12-
#ifdef MP_8BIT
1312
/*
1413
* floor of positive solution of
1514
* (2^16)-1 = (a+4)*(2*a+5)
@@ -19,10 +18,8 @@
1918
* But it is still a restriction of the set of available pseudoprimes
2019
* which makes this implementation less secure if used stand-alone.
2120
*/
22-
#define LTM_FROBENIUS_UNDERWOOD_A 177
23-
#else
2421
#define LTM_FROBENIUS_UNDERWOOD_A 32764
25-
#endif
22+
2623
mp_err mp_prime_frobenius_underwood(const mp_int *N, mp_bool *result)
2724
{
2825
mp_int T1z, T2z, Np1z, sz, tz;

bn_mp_prime_is_prime.c

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
5757
return MP_OKAY;
5858
}
5959
}
60-
#ifdef MP_8BIT
61-
/* The search in the loop above was exhaustive in this case */
62-
if ((a->used == 1) && (MP_PRIME_TAB_SIZE >= 31)) {
63-
return MP_OKAY;
64-
}
65-
#endif
66-
6760
/* first perform trial division */
6861
if ((err = s_mp_prime_is_divisible(a, &res)) != MP_OKAY) {
6962
return err;
@@ -112,7 +105,7 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
112105
* MP_8BIT (It is unknown if the Lucas-Selfridge test works with 16-bit
113106
* integers but the necesssary analysis is on the todo-list).
114107
*/
115-
#if defined (MP_8BIT) || defined (LTM_USE_FROBENIUS_TEST)
108+
#ifdef LTM_USE_FROBENIUS_TEST
116109
err = mp_prime_frobenius_underwood(a, &res);
117110
if ((err != MP_OKAY) && (err != MP_ITER)) {
118111
goto LBL_B;
@@ -240,20 +233,6 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
240233
* an unsigned int and "mask" on the other side is most probably not.
241234
*/
242235
fips_rand = (unsigned int)(b.dp[0] & (mp_digit) mask);
243-
#ifdef MP_8BIT
244-
/*
245-
* One 8-bit digit is too small, so concatenate two if the size of
246-
* unsigned int allows for it.
247-
*/
248-
if ((MP_SIZEOF_BITS(unsigned int)/2) >= MP_SIZEOF_BITS(mp_digit)) {
249-
if ((err = mp_rand(&b, 1)) != MP_OKAY) {
250-
goto LBL_B;
251-
}
252-
fips_rand <<= MP_SIZEOF_BITS(mp_digit);
253-
fips_rand |= (unsigned int) b.dp[0];
254-
fips_rand &= mask;
255-
}
256-
#endif
257236
if (fips_rand > (unsigned int)(INT_MAX - MP_DIGIT_BIT)) {
258237
len = INT_MAX / MP_DIGIT_BIT;
259238
} else {
@@ -264,18 +243,6 @@ mp_err mp_prime_is_prime(const mp_int *a, int t, mp_bool *result)
264243
ix--;
265244
continue;
266245
}
267-
/*
268-
* As mentioned above, one 8-bit digit is too small and
269-
* although it can only happen in the unlikely case that
270-
* an "unsigned int" is smaller than 16 bit a simple test
271-
* is cheap and the correction even cheaper.
272-
*/
273-
#ifdef MP_8BIT
274-
/* All "a" < 2^8 have been caught before */
275-
if (len == 1) {
276-
len++;
277-
}
278-
#endif
279246
if ((err = mp_rand(&b, len)) != MP_OKAY) {
280247
goto LBL_B;
281248
}

bn_mp_prime_strong_lucas_selfridge.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
*/
1010
#ifndef LTM_USE_ONLY_MR
1111

12-
/*
13-
* 8-bit is just too small. You can try the Frobenius test
14-
* but that frobenius test can fail, too, for the same reason.
15-
*/
16-
#ifndef MP_8BIT
17-
1812
/*
1913
* multiply bigint a with int d and put the result in c
2014
* Like mp_mul_d() but with a signed long as the small input
@@ -286,4 +280,3 @@ mp_err mp_prime_strong_lucas_selfridge(const mp_int *a, mp_bool *result)
286280
}
287281
#endif
288282
#endif
289-
#endif

bn_mp_to_ubin.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *wr
2020
}
2121

2222
for (x = count; x --> 0u;) {
23-
#ifndef MP_8BIT
2423
buf[x] = (unsigned char)(t.dp[0] & 255u);
25-
#else
26-
buf[x] = (unsigned char)(t.dp[0] | ((t.dp[1] & 1u) << 7));
27-
#endif
2824
if ((err = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
2925
goto LBL_ERR;
3026
}

bn_prime_tab.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ const mp_digit s_mp_prime_tab[] = {
77
0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
88
0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
99
0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
10-
0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F,
11-
#ifndef MP_8BIT
12-
0x0083,
10+
0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F, 0x0083,
1311
0x0089, 0x008B, 0x0095, 0x0097, 0x009D, 0x00A3, 0x00A7, 0x00AD,
1412
0x00B3, 0x00B5, 0x00BF, 0x00C1, 0x00C5, 0x00C7, 0x00D3, 0x00DF,
1513
0x00E3, 0x00E5, 0x00E9, 0x00EF, 0x00F1, 0x00FB, 0x0101, 0x0107,
@@ -41,7 +39,6 @@ const mp_digit s_mp_prime_tab[] = {
4139
0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
4240
0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
4341
0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
44-
#endif
4542
};
4643

4744
#endif

demo/shared.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ void ndraw(mp_int *a, const char *name)
2323

2424
void print_header(void)
2525
{
26-
#ifdef MP_8BIT
27-
printf("Digit size 8 Bit \n");
28-
#endif
2926
#ifdef MP_16BIT
3027
printf("Digit size 16 Bit \n");
3128
#endif

demo/test.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,6 @@ static int test_mp_prime_is_prime(void)
10221022

10231023
}
10241024
/* Check regarding problem #143 */
1025-
#ifndef MP_8BIT
10261025
mp_read_radix(&a,
10271026
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF",
10281027
16);
@@ -1041,8 +1040,6 @@ static int test_mp_prime_is_prime(void)
10411040
putchar('\n');
10421041
goto LBL_ERR;
10431042
}
1044-
#endif
1045-
10461043
printf("\n\n");
10471044

10481045
mp_clear_multi(&a, &b, NULL);
@@ -2040,17 +2037,9 @@ static int test_mp_root_u32(void)
20402037
if ((e = mp_init_multi(&a, &c, &r, NULL)) != MP_OKAY) {
20412038
return EXIT_FAILURE;
20422039
}
2043-
#ifdef MP_8BIT
2044-
for (i = 0; i < 1; i++) {
2045-
#else
20462040
for (i = 0; i < 10; i++) {
2047-
#endif
20482041
mp_read_radix(&a, input[i], 64);
2049-
#ifdef MP_8BIT
2050-
for (j = 3; j < 10; j++) {
2051-
#else
20522042
for (j = 3; j < 100; j++) {
2053-
#endif
20542043
mp_root_u32(&a, (uint32_t)j, &c);
20552044
mp_read_radix(&r, root[i][j-3], 10);
20562045
if (mp_cmp(&r, &c) != MP_EQ) {

doc/bn.tex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,9 +2035,7 @@ \section{Frobenius (Underwood) Test}
20352035
\begin{alltt}
20362036
int mp_prime_frobenius_underwood(const mp_int *N, int *result)
20372037
\end{alltt}
2038-
Performs the variant of the Frobenius test as described by Paul Underwood. The single internal use is in
2039-
\texttt{mp\_prime\_is\_prime} for \texttt{MP\_8BIT} only but can be included at build-time for all other sizes
2040-
if the preprocessor macro \texttt{LTM\_USE\_FROBENIUS\_TEST} is defined.
2038+
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.
20412039
20422040
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$.
20432041
@@ -2053,7 +2051,7 @@ \section{Primality Testing}
20532051
\begin{alltt}
20542052
int mp_prime_is_prime (mp_int * a, int t, int *result)
20552053
\end{alltt}
2056-
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
2054+
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
20572055
\texttt{bn\_mp\_prime\_is\_prime.c} for the necessary details. It shall be noted that both functions are much slower than
20582056
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.
20592057

mtest/mtest.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ mulmod
2828
2929
*/
3030

31-
#ifdef MP_8BIT
32-
#define THE_MASK 127
33-
#else
3431
#define THE_MASK 32767
35-
#endif
3632

3733
#include <stdio.h>
3834
#include <stdlib.h>

testme.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,11 @@ do
378378
then
379379
_runvalgrind "$i $a" "$CFLAGS"
380380
[ "$WITH_LOW_MP" != "1" ] && continue
381-
_runvalgrind "$i $a" "-DMP_8BIT $CFLAGS"
382381
_runvalgrind "$i $a" "-DMP_16BIT $CFLAGS"
383382
_runvalgrind "$i $a" "-DMP_32BIT $CFLAGS"
384383
else
385384
_runtest "$i $a" "$CFLAGS"
386385
[ "$WITH_LOW_MP" != "1" ] && continue
387-
_runtest "$i $a" "-DMP_8BIT $CFLAGS"
388386
_runtest "$i $a" "-DMP_16BIT $CFLAGS"
389387
_runtest "$i $a" "-DMP_32BIT $CFLAGS"
390388
fi

tommath.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
#include <stdint.h>
88
#include <stddef.h>
9+
#ifdef MP_8BIT
10+
# error "Support of 8-bit architectures has been dropped in this version of LTM."
11+
#endif
12+
913

1014
#ifndef MP_NO_FILE
1115
# include <stdio.h>
@@ -35,7 +39,7 @@ extern "C" {
3539
defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \
3640
defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \
3741
defined(__LP64__) || defined(_LP64) || defined(__64BIT__)
38-
# if !(defined(MP_64BIT) || defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
42+
# if !(defined(MP_64BIT) || defined(MP_32BIT) || defined(MP_16BIT))
3943
# if defined(__GNUC__) && !defined(__hppa)
4044
/* we support 128bit integers only via: __attribute__((mode(TI))) */
4145
# define MP_64BIT
@@ -47,7 +51,7 @@ extern "C" {
4751
#endif
4852

4953
#ifdef MP_DIGIT_BIT
50-
# error Defining MP_DIGIT_BIT is disallowed, use MP_8/16/31/32/64BIT
54+
# error Defining MP_DIGIT_BIT is disallowed, use MP_16/31/32/64BIT
5155
#endif
5256

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

62-
#ifdef MP_8BIT
63-
typedef uint8_t mp_digit;
64-
typedef uint16_t private_mp_word;
65-
# define MP_DIGIT_BIT 7
66-
#elif defined(MP_16BIT)
66+
67+
#if defined(MP_16BIT)
6768
typedef uint16_t mp_digit;
6869
typedef uint32_t private_mp_word;
6970
# define MP_DIGIT_BIT 15

0 commit comments

Comments
 (0)