aboutsummaryrefslogtreecommitdiff
path: root/lib/libmpa/mpa_primetest.c
blob: 070dca2116aad32d480fc4f7b449ef5a71b1da55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 */
#include "mpa.h"

#define USE_PRIME_TABLE

#if defined(USE_PRIME_TABLE)
#include "mpa_primetable.h"
#endif

#define DEF_COMPOSITE   0
#define DEF_PRIME       1
#define PROB_PRIME     -1

/* Product of all primes < 1000 */
static const mpa_num_base const_small_prime_factors = {
	44,
	44,
	{0x2ED42696, 0x2BBFA177, 0x4820594F, 0xF73F4841,
	 0xBFAC313A, 0xCAC3EB81, 0xF6F26BF8, 0x7FAB5061,
	 0x59746FB7, 0xF71377F6, 0x3B19855B, 0xCBD03132,
	 0xBB92EF1B, 0x3AC3152C, 0xE87C8273, 0xC0AE0E69,
	 0x74A9E295, 0x448CCE86, 0x63CA1907, 0x8A0BF944,
	 0xF8CC3BE0, 0xC26F0AF5, 0xC501C02F, 0x6579441A,
	 0xD1099CDA, 0x6BC76A00, 0xC81A3228, 0xBFB1AB25,
	 0x70FA3841, 0x51B3D076, 0xCC2359ED, 0xD9EE0769,
	 0x75E47AF0, 0xD45FF31E, 0x52CCE4F6, 0x04DBC891,
	 0x96658ED2, 0x1753EFE5, 0x3AE4A5A6, 0x8FD4A97F,
	 0x8B15E7EB, 0x0243C3E1, 0xE0F0C31D, 0x0000000B}
};

/*
 * If n is less than this number (341550071728321 decimal) the Miller-Rabin
 * test (using specific bases) constitutes a primality proof.
 */
static const mpa_num_base const_miller_rabin_proof_limit = {
	2,
	2,
	{0x52B2C8C1, 0x000136A3}
};

static const mpa_num_base const_two = {
	1,
	1,
	{0x00000002}
};

/* foward declarations */
static int is_small_prime(mpanum n);
static int has_small_factors(mpanum n, mpa_scratch_mem pool);
static int primality_test_miller_rabin(mpanum n, int conf_level,
				      mpa_scratch_mem pool);

/*------------------------------------------------------------
 *
 *  mpa_is_prob_prime
 *
 * Returns:
 *   0 if n is definitely composite
 *   1 if n is definitely prime
 *  -1 if n is composite with a probability less than 2^(-conf_level)
 *
 */
int mpa_is_prob_prime(mpanum n, int conf_level, mpa_scratch_mem pool)
{
	int result = 0;

	/* Check if it's a small prime */
	result = is_small_prime(n);
	if (result != PROB_PRIME)
		goto cleanup;

	/* Test if n is divisible by any prime < 1000 */
	if (has_small_factors(n, pool)) {
		result = DEF_COMPOSITE;
		goto cleanup;
	}
	/* Check with Miller Rabin */
	result = primality_test_miller_rabin(n, conf_level, pool);

cleanup:
	return result;
}

#if defined(USE_PRIME_TABLE)
/*------------------------------------------------------------
 *
 *  check_table
 *
 */
static uint32_t check_table(uint32_t v)
{
	return (PRIME_TABLE[v >> 5] >> (v & 0x1f)) & 1;
}
#endif

/*------------------------------------------------------------
 *
 *  is_small_prime
 *
 *  Returns 1 if n is prime,
    Returns 0 if n is composite
 *  Returns -1 if we cannot decide
 *
 */
static int is_small_prime(mpanum n)
{
	mpa_word_t v;

	/* If n is larger than a mpa_word_t, we can only decide if */
	/* n is even. If it's odd we cannot tell. */
	if (__mpanum_size(n) > 1)
		return ((mpa_parity(n) == MPA_EVEN_PARITY) ? 0 : -1);

	v = mpa_get_word(n);	/* will convert negative n:s to positive v:s. */
	if ((v | 1) == 1)	/* 0 and 1 are not prime */
		return DEF_COMPOSITE;
	if (v == 2)		/* 2 is prime */
		return DEF_PRIME;
	if ((v & 1) == 0)
		return DEF_COMPOSITE;	/* but no other even number */

#if defined(USE_PRIME_TABLE)
	if (mpa_cmp_short(n, MAX_TABULATED_PRIME) > 0)
		return -1;
	v = (v - 3) >> 1;
	return check_table(v);
#else
	return -1;
#endif
}

/*------------------------------------------------------------
 *
 *  has_small_factors
 *
 *  returns 1 if n has small factors
 *  returns 0 if not.
 */
static int has_small_factors(mpanum n, mpa_scratch_mem pool)
{
	const mpa_num_base *factors = &const_small_prime_factors;
	int result;
	mpanum res;

	mpa_alloc_static_temp_var(&res, pool);
	mpa_gcd(res, n, (const mpanum)factors, pool);
	result = (mpa_cmp_short(res, 1) == 0) ? 0 : 1;
	mpa_free_static_temp_var(&res, pool);

	return result;
}

/*------------------------------------------------------------
 *
 *  primality_test_miller_rabin
 *
 */
static int primality_test_miller_rabin(mpanum n, int conf_level,
				      mpa_scratch_mem pool)
{
	int result;
	bool proof_version;
	static const int32_t proof_a[7] = { 2, 3, 5, 7, 11, 13, 17 };
	int cnt;
	int idx;
	int t;
	int e = 0;
	int cmp_one;
	mpanum a;
	mpanum q;
	mpanum n_minus_1;
	mpanum b;
	mpanum r_modn;
	mpanum r2_modn;
	mpa_word_t n_inv;

	mpa_alloc_static_temp_var(&r_modn, pool);
	mpa_alloc_static_temp_var(&r2_modn, pool);

	if (mpa_compute_fmm_context(n, r_modn, r2_modn, &n_inv, pool) == -1) {
		result = DEF_COMPOSITE;
		goto cleanup_short;
	}

	mpa_alloc_static_temp_var(&a, pool);
	mpa_alloc_static_temp_var(&q, pool);
	mpa_alloc_static_temp_var(&n_minus_1, pool);
	mpa_alloc_static_temp_var(&b, pool);

	proof_version =
	    (mpa_cmp(n, (mpanum) &const_miller_rabin_proof_limit) < 0);

	if (proof_version)
		cnt = 7;
	else	/* MR has 1/4 chance in failing a composite */
		cnt = (conf_level + 1) / 2;

	mpa_sub_word(n_minus_1, n, 1, pool);
	mpa_set(q, n_minus_1);
	t = 0;
	/* calculate q such that n - 1 = 2^t * q where q is odd */
	while (mpa_is_even(q)) {
		mpa_shift_right(q, q, 1);
		t++;
	}

	result = PROB_PRIME;
	for (idx = 0; idx < cnt && result == PROB_PRIME; idx++) {
		if (proof_version) {
			mpa_set_S32(a, proof_a[idx]);
			if (mpa_cmp(n, a) == 0) {
				result = DEF_PRIME;
				continue;
			}
		} else {
			/*
			 * Get random a, 1 < a < N by
			 * asking for a random in range 0 <= x < N - 2
			 * and then add 2 to it.
			 */
			mpa_sub_word(n_minus_1, n_minus_1, 1, pool);
			/* n_minus_1 is now N - 2 ! */
			mpa_get_random(a, n_minus_1);
			mpa_add_word(n_minus_1, n_minus_1, 1, pool);
			/* and a is now 2 <= a < N */
			mpa_add_word(a, a, 2, pool);
		}

		mpa_exp_mod(b, a, q, n, r_modn, r2_modn, n_inv, pool);
		e = 0;

inner_loop:
		cmp_one = mpa_cmp_short(b, 1);
		if ((cmp_one == 0) && (e > 0)) {
			result = DEF_COMPOSITE;
			continue;
		}

		if ((mpa_cmp(b, n_minus_1) == 0) ||
		    ((cmp_one == 0) && (e == 0))) {
			/* probably prime, try another a */
			continue;
		}

		e++;
		if (e < t) {
			mpa_exp_mod(b, b, (mpanum) &const_two, n, r_modn,
				    r2_modn, n_inv, pool);
			goto inner_loop;
		}
		result = DEF_COMPOSITE;
	}

	if (result == PROB_PRIME && proof_version)
		result = DEF_PRIME;

	mpa_free_static_temp_var(&a, pool);
	mpa_free_static_temp_var(&q, pool);
	mpa_free_static_temp_var(&n_minus_1, pool);
	mpa_free_static_temp_var(&b, pool);
cleanup_short:
	mpa_free_static_temp_var(&r_modn, pool);
	mpa_free_static_temp_var(&r2_modn, pool);

	return result;
}