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
|
/*
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017 J08nY
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* @author J08nY <johny@neuromancer.sk>
* @license GPL v2.0
* @version 0.2
*/
#include <time.h>
#include "cli.h"
#include "field.h"
#include "curve.h"
#include "output.h"
const char *argp_program_version = "ecgen 0.2\n"
"Copyright (C) 2017 J08nY\n"
"License GPLv2: GNU GPL version 2 (or later) <http://gnu.org/licenses/gpl.html>\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.";
const char *argp_program_bug_address = "<johny@neuromancer.sk>";
static struct argp argp = {options, parse_opt, args_doc, doc};
static struct arguments args;
static FILE *in;
static FILE *out;
bool init() {
// Init PARI, 1GB stack, 1M primes
pari_init(1000000000, 1000000);
init_gp();
// Init PARI PRNG
pari_ulong seed = 0;
// Try urandom first
FILE *rand = fopen("/dev/urandom", "rb");
if (rand) {
fread(&seed, sizeof(char), sizeof(pari_ulong), rand);
fclose(rand);
}
// Try worse methods later
if (seed == 0) {
struct timespec t;
if (!clock_gettime(CLOCK_REALTIME, &t)) {
seed = (pari_ulong) t.tv_nsec;
} else {
seed = (pari_ulong) time(NULL);
}
}
pari_sp ltop = avma;
setrand(utoi(seed));
avma = ltop;
//set datadir if specified
if (args.datadir) {
default0("datadir", args.datadir);
}
//open outfile
out = stdout;
if (args.output) {
out = fopen(args.output, args.append ? "a" : "w");
if (!out) {
//fallback to stdout and output err
out = stdout;
perror("Failed to open output file.");
}
}
//open infile
in = stdin;
if (args.input) {
in = fopen(args.input, "r");
if (!in) {
//fallback to stdin or quit?
in = stdin;
perror("Failed to open input file.");
}
}
return true;
}
int quit(int status) {
pari_close();
if (out != NULL && out != stdout) {
fclose(out);
}
if (in != NULL && in != stdout) {
fclose(in);
}
return status;
}
int main(int argc, char *argv[]) {
// Parse cli args
memset(&args, 0, sizeof(args));
argp_parse(&argp, argc, argv, 0, 0, &args);
if (!init()) {
return quit(1);
}
if (args.random) {
GEN field = gen_0;
switch (args.field) {
case FIELD_PRIME:
field = field_primer(args.bits);
break;
case FIELD_BINARY:
field = field_binaryr(args.bits);
break;
}
GEN curve = curve_random(field);
GEN ord = ellcard(curve, NULL);
GEN f = field_params(field);
GEN a = gtovec(field_elementi(ell_get_a4(curve)));
GEN b = gtovec(field_elementi(ell_get_a6(curve)));
GEN o = gconcat(gconcat(gconcat(f, a), b), gtovec(ord));
output_csv(out, ',', 'x', o);
} else {
fprintf(stderr, "Currently unsupported.");
}
return quit(0);
}
|