- 0EB9FD7240D16B287C06A319D1D170AEA9AA046B4F443FF1339F509D8E7B42317425FD369CEDB6E7E3D898700BA917B0B56A83305D730A5CD7D5BD09F4809986
+ 25364B546A9EC5983239A7C783B316F704C814EBEE5778CFA595F14F802E64930B7234EE536E0F69E519BF36B558BEA432D62A85D670D0FF62556CC12C6A1DC6
eucrypt/smg_rsa/tests/tests.c
(4 . 6)(4 . 7)
139 #include <stdlib.h>
140 #include <unistd.h>
141 #include <time.h>
142 #include <stdio.h>
143
144 void err(char *msg)
145 {
(30 . 6)(31 . 43)
147 printf("ENTROPY source timing: %d kB in %ld seconds, at an average speed of %f kB/s over %d runs of %d octets each\n", nruns*noctets, diff, kbps, nruns, noctets);
148 }
149
150 void test_entropy_output(unsigned int noctets, char * filename) {
151 FILE * out;
152 int source;
153 unsigned int nread, total_read, to_read;
154 const int buffer_length = 1000;
155 unsigned char buffer[buffer_length];
156
157 source = open_entropy_source(ENTROPY_SOURCE);
158 if (source <= 0)
159 err("unable to access entropy source!");
160
161 out = fopen(filename, "wb");
162 if ( !out )
163 err("unable to open output file for test_entropy_output!");
164
165 printf("TEST_ENTROPY_SOURCE: reading %u octets from %s ", noctets, ENTROPY_SOURCE);
166 total_read = 0;
167 while (total_read < noctets) {
168 to_read = noctets - total_read;
169 if (to_read > buffer_length)
170 to_read = buffer_length;
171
172 nread = get_random_octets_from(to_read, buffer, source);
173 if (nread > 0) {
174 total_read = total_read + nread;
175 fwrite(buffer, 1, nread, out);
176 fflush(out);
177 printf(".");
178 fflush(stdout);
179 }
180 }
181 printf("done.\n");
182
183 fclose(out);
184 close(source);
185 }
186
187 void test_is_composite(int nruns, char *hex_number, int expected) {
188 int i;
189 int output;
(52 . 49)(90 . 161)
191 close(source);
192 }
193
194 void time_mr(int nruns) {
195 struct timespec tstart, tend;
196 long int diff;
197 int i;
198 MPI prime;
199 unsigned int noctets = KEY_LENGTH_OCTETS / 2;
200 unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
201
202 int entropy_source = open_entropy_source(ENTROPY_SOURCE);
203 if (entropy_source <= 0)
204 err("can't open entropy source!");
205
206 /* first generate a prime of half key length, to make sure M-R will run max number of iterations */
207 printf("Generating a prime number of %d octets length for M-R timing test\n", noctets);
208 prime = mpi_alloc(nlimbs);
209 gen_random_prime(noctets, prime);
210
211 printf("Running timing test for Miller-Rabin with %d repetitions and %d witnesses on prime number ", nruns, M_R_ITERATIONS);
212 mpi_print(stdout, prime, 1);
213 printf("\n");
214 /* now do the actual runs and time it all */
215 clock_gettime(CLOCK_MONOTONIC, &tstart);
216 for (i=0; i<nruns; i++) {
217 if (is_composite(prime, M_R_ITERATIONS, entropy_source))
218 printf("FAIL");
219 else printf(".");
220 fflush(stdout);
221 }
222 clock_gettime(CLOCK_MONOTONIC, &tend);
223
224 diff = tend.tv_sec-tstart.tv_sec;
225 printf("\nTimings on prime number %d octets long, %d runs of MR with %d iterations (witnesses checked) each\n", \
226 noctets, nruns, M_R_ITERATIONS);
227 printf("Total time: %ld seconds\nTime per MR run: %f seconds\nTime per MR iteration: %f seconds\n",\
228 diff, diff / (1.0*nruns), diff / (1.0*nruns * M_R_ITERATIONS));
229
230 mpi_free(prime);
231 close(entropy_source);
232 }
233
234 void test_rpng(int nruns) {
235 unsigned int noctets = KEY_LENGTH_OCTETS / 2;
236 unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
237 int entropy_source = open_entropy_source(ENTROPY_SOURCE);
238 if (entropy_source <= 0)
239 err("can't open entropy source!");
240
241 MPI prime = mpi_alloc(nlimbs);
242 int i;
243
244 printf("TEST: random prime number generator with %d runs\n", nruns);
245 for (i = 0;i < nruns; i++) {
246 gen_random_prime(noctets, prime);
247 printf("Run %d: ", i+1);
248 mpi_print(stdout, prime, 1);
249 if (is_composite(prime, M_R_ITERATIONS, entropy_source))
250 printf(" **FAIL**\n");
251 else
252 printf(" **PASS**\n");
253 }
254
255 mpi_free(prime);
256 close(entropy_source);
257 }
258
259 void time_rpng(int nruns) {
260 struct timespec tstart, tend;
261 long int diff;
262
263 unsigned int noctets = KEY_LENGTH_OCTETS / 2;
264 unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
265
266 int entropy_source = open_entropy_source(ENTROPY_SOURCE);
267 if (entropy_source <= 0)
268 err("can't open entropy source!");
269
270 MPI prime = mpi_alloc(nlimbs);
271 int i;
272
273 printf("TIMING: random prime number generator with %d runs\n", nruns);
274 clock_gettime(CLOCK_MONOTONIC, &tstart);
275 for (i = 0;i < nruns; i++) {
276 gen_random_prime(noctets, prime);
277 }
278 clock_gettime(CLOCK_MONOTONIC, &tend);
279
280 diff = tend.tv_sec-tstart.tv_sec;
281
282 printf("TOTAL: %ld seconds\n", diff);
283 printf("Average: %f seconds to generate one random prime of %d octets length\n", diff / (1.0*nruns), noctets);
284 mpi_free(prime);
285 close(entropy_source);
286 }
287
288 int main(int ac, char **av)
289 {
290 int nruns;
291 int id;
292
293 if (ac<2) {
294 printf("Usage: %s number_of_runs [testID]\n", av[0]);
295 printf("Usage: %s number_of_runs/octets [testID]\n", av[0]);
296 return -1;
297 }
298 nruns = atoi(av[1]);
299
300 if (ac < 3)
301 id = 0;
302 id = -1;
303 else
304 id = atoi(av[2]);
305
306 if (id == 0 || id == 1) {
307 printf("Timing entropy source...\n");
308 time_entropy_source(nruns,4096);
309 }
310
311 if (id == 0 || id == 2) {
312
313 /* a few primes (decimal): 65537, 116447, 411949103, 20943302231 */
314 test_is_composite(nruns, "0x10001", 0);
315 test_is_composite(nruns, "0x1C6DF", 0);
316 test_is_composite(nruns, "0x188DD82F", 0);
317 test_is_composite(nruns, "0x4E0516E57", 0);
318 /* a few mersenne primes (decimal): 2^13 - 1 = 8191, 2^17 - 1 = 131071, 2^31 - 1 = 2147483647 */
319 test_is_composite(nruns, "0x1FFF", 0);
320 test_is_composite(nruns, "0x1FFFF", 0);
321 test_is_composite(nruns, "0x7FFFFFFF", 0);
322 /* a few carmichael numbers, in decimal: 561, 60977817398996785 */
323 test_is_composite(nruns, "0x231", 1);
324 test_is_composite(nruns, "0xD8A300793EEF31", 1);
325 /* an even number */
326 test_is_composite(nruns, "0x15A9E672864B1E", 1);
327 /* a phuctor-found non-prime public exponent: 170141183460469231731687303715884105731 */
328 test_is_composite(nruns, "0x80000000000000000000000000000003", 1);
329 switch ( id ) {
330 case 0:
331 printf("Timing entropy source...\n");
332 time_entropy_source(nruns, 4096);
333 break;
334 case 1:
335 test_entropy_output(nruns, "entropy_source_output.txt");
336 break;
337 case 2:
338 /* tests on miller-rabin */
339 /* a few primes (decimal): 65537, 116447, 411949103, 20943302231 */
340 test_is_composite(nruns, "0x10001", 0);
341 test_is_composite(nruns, "0x1C6DF", 0);
342 test_is_composite(nruns, "0x188DD82F", 0);
343 test_is_composite(nruns, "0x4E0516E57", 0);
344 /* a few mersenne primes (decimal): 2^13 - 1 = 8191, 2^17 - 1 = 131071, 2^31 - 1 = 2147483647 */
345 test_is_composite(nruns, "0x1FFF", 0);
346 test_is_composite(nruns, "0x1FFFF", 0);
347 test_is_composite(nruns, "0x7FFFFFFF", 0);
348 /* a few carmichael numbers, in decimal: 561, 60977817398996785 */
349 test_is_composite(nruns, "0x231", 1);
350 test_is_composite(nruns, "0xD8A300793EEF31", 1);
351 /* an even number */
352 test_is_composite(nruns, "0x15A9E672864B1E", 1);
353 /* a phuctor-found non-prime public exponent: 170141183460469231731687303715884105731 */
354 test_is_composite(nruns, "0x80000000000000000000000000000003", 1);
355 break;
356 case 3:
357 time_mr(nruns);
358 break;
359 case 4:
360 test_rpng(nruns);
361 break;
362 case 5:
363 time_rpng(nruns);
364 break;
365 default:
366 printf("Current test ids:\n");
367 printf("0 for timing entropy source\n");
368 printf("1 for entropy output test\n");
369 printf("2 for is_composite (Miller-Rabin) test\n");
370 printf("3 for timing Miller-Rabin\n");
371 printf("4 for random prime number generator test\n");
372 printf("5 for timing random prime number generator\n");
373 }
374
375 if (id > 2)
376 printf("Current test ids: 0 for all, 1 for entropy source test only, 2 for is_composite test only.");
377
378 return 0;
379 }