-
Notifications
You must be signed in to change notification settings - Fork 0
/
modularExponentiationStore.cc
126 lines (103 loc) · 3 KB
/
modularExponentiationStore.cc
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
/*
* Program to determine if storing modular exponentation computations to a file
* could improve attack performance.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gmp.h>
#include <sys/time.h>
#include "include/types.h"
#include "include/randomhelpers.h"
#include "include/elgamal.h"
void usage (char *argv0) {
printf ("Usage: %s [-r] bits cryptosystemFilePath\n", argv0);
}
int main (int argc, char **argv) {
int firstArg = 1;
bool read = false;
if (argc == 4) {
if (strcmp (argv[1], "-r") == 0) {
firstArg = 2;
read = true;
} else {
usage (argv[0]);
exit (EXIT_FAILURE);
}
} else if (argc != 3) {
usage (argv[0]);
exit (EXIT_FAILURE);
}
// process bits argument
char *endptr;
unsigned long bits = strtoul (argv[firstArg], &endptr, 10);
if (*endptr != '\0') {
usage (argv[0]);
exit (EXIT_FAILURE);
}
// process cryptosystem argument
ElgamalCryptosystem *e = new ElgamalCryptosystem ();
FILE *f = fopen (argv[firstArg+1], "r");
if (f == NULL) {
perror (NULL);
exit (EXIT_FAILURE);
}
e->read (f);
//e->print ();
fclose (f);
size_t max = (1ul << bits);
mpz_t delta1;
mpz_t tmp, exp;
mpz_init_set_ui (delta1, 0);
mpz_init2 (tmp, mpz_sizeinbase (e->prime, 2));
mpz_init_set (exp, e->baseOrder);
timeval start, end;
gettimeofday (&start, NULL);
if (read) {
f = fopen ("tmpmodexp.bin", "r");
if (f == NULL) {
perror ("Failed to open tmp file, exiting");
exit (EXIT_FAILURE);
}
for (size_t i = 0; i < max; i++) {
if (!mpz_inp_raw (tmp, f)) {
fprintf (stderr, "Read failed at %zu\n", i);
}
}
} else {
f = fopen ("tmpmodexp.bin", "w");
if (f == NULL) {
perror ("Failed to open tmp file, exiting");
exit (EXIT_FAILURE);
}
//printf ("Generating table...\n");
for (size_t i = 0; i < max; i++) {
mpz_add_ui (delta1, delta1, 1);
mpz_powm (tmp, delta1, exp, e->prime);
if (!mpz_out_raw (f, tmp)) {
fprintf (stderr, "Write failed at %zu\n", i);
}
}
}
if (f != NULL)
fclose (f);
gettimeofday (&end, NULL);
long sdiff = end.tv_sec - start.tv_sec;
long udiff = 0;
if (start.tv_usec > end.tv_usec) {
sdiff--;
udiff = 1000000l - start.tv_usec + end.tv_usec;
} else {
udiff = end.tv_usec - start.tv_usec;
}
ldiv_t min = ldiv (sdiff, 60);
ldiv_t msec = ldiv (udiff, 1000);
printf ("[%s %lu %s]: %ldmin %lds %ldms %ldus (%ld.%6ld tot sec)\n",
read ? "read": "write", bits, argv[firstArg+1],
min.quot, min.rem, msec.quot, msec.rem, sdiff, udiff);
mpz_clear (delta1);
mpz_clear (tmp);
mpz_clear (exp);
delete e;
return EXIT_SUCCESS;
}