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
|
/*
* ecgen, tool for generating Elliptic curve domain parameters
* Copyright (C) 2017 J08nY
*/
#include "output.h"
#include <parson/parson.h>
#include "math/field.h"
#include "math/curve.h"
FILE *out;
FILE *debug;
char *output_scsv(curve_t *curve, config_t *config) {
pari_sp ltop = avma;
GEN vector = curve_params(curve);
long len = glength(vector);
char *params[len];
size_t lengths[len];
size_t total = 0;
for (long i = 0; i < len; ++i) {
params[i] = pari_sprintf("%P#x", gel(vector, i + 1));
lengths[i] = strlen(params[i]);
total += lengths[i];
}
char *result = (char *) malloc(total + len);
if (!result) {
perror("Couldn't malloc.");
exit(1);
}
size_t offset = 0;
for (long i = 0; i < len; ++i) {
memcpy(result + offset, params[i], lengths[i]);
free(params[i]);
offset += lengths[i];
if (i != len - 1) {
result[offset] = ',';
offset++;
}
}
memset(result + offset, 0, 1);
avma = ltop;
return result;
}
void output_fcsv(FILE *out, curve_t *curve, config_t *config) {
char *string = output_scsv(curve, config);
fprintf(out, "%s\n", string);
free(string);
}
void output_csv(curve_t *curve, config_t *config) {
output_fcsv(out, curve, config);
}
JSON_Value *output_jjson(curve_t *curve, config_t *config) {
pari_sp ltop = avma;
// root object/value is curve
JSON_Value *root_value = json_value_init_object();
JSON_Object *root_object = json_value_get_object(root_value);
switch (config->field) {
case FIELD_PRIME: {
char *prime = pari_sprintf("%P#x", curve->field);
json_object_dotset_string(root_object, "field.p", prime);
pari_free(prime);
break;
}
case FIELD_BINARY: {
GEN field = field_params(curve->field);
char *e1 = pari_sprintf("%P#x", gel(field, 1));
char *e2 = pari_sprintf("%P#x", gel(field, 2));
char *e3 = pari_sprintf("%P#x", gel(field, 3));
char *m = pari_sprintf("%#lx", config->bits); // maybe not?
json_object_dotset_string(root_object, "field.m", m);
json_object_dotset_string(root_object, "field.e1", e1);
json_object_dotset_string(root_object, "field.e2", e2);
json_object_dotset_string(root_object, "field.e3", e3);
pari_free(m);
pari_free(e1);
pari_free(e2);
pari_free(e3);
break;
}
default:
fprintf(stderr, "Error, field has unknown amount of elements.\n");
exit(1);
}
char *a = pari_sprintf("%P#x", field_elementi(curve->a));
json_object_set_string(root_object, "a", a);
pari_free(a);
char *b = pari_sprintf("%P#x", field_elementi(curve->b));
json_object_set_string(root_object, "b", b);
pari_free(b);
char *order = pari_sprintf("%P#x", curve->order);
json_object_set_string(root_object, "order", order);
pari_free(order);
if (curve->npoints) {
JSON_Value *points_value = json_value_init_array();
JSON_Array *points_array = json_value_get_array(points_value);
for (size_t i = 0; i < curve->npoints; ++i) {
JSON_Value *point_value = json_value_init_object();
JSON_Object *point_object = json_value_get_object(point_value);
char *x = pari_sprintf("%P#x", field_elementi(gel(curve->points[i]->point, 1)));
json_object_set_string(point_object, "x", x);
pari_free(x);
char *y = pari_sprintf("%P#x", field_elementi(gel(curve->points[i]->point, 2)));
json_object_set_string(point_object, "y", y);
pari_free(y);
char *p_order = pari_sprintf("%P#x", curve->points[i]->order);
json_object_set_string(point_object, "order", p_order);
pari_free(p_order);
json_array_append_value(points_array, point_value);
}
json_object_set_value(root_object, "points", points_value);
}
avma = ltop;
return root_value;
}
char *output_sjson(curve_t *curve, config_t *config) {
JSON_Value *root_value = output_jjson(curve, config);
char *result = json_serialize_to_string_pretty(root_value);
json_value_free(root_value);
return result;
}
void output_fjson(FILE *out, curve_t *curve, config_t *config) {
char *s = output_sjson(curve, config);
fprintf(out, "%s", s);
json_free_serialized_string(s);
}
void output_json(curve_t *curve, config_t *config) {
output_fjson(out, curve, config);
}
void output_init(config_t *cfg) {
json_set_allocation_functions(pari_malloc, pari_free);
if (cfg->output) {
out = fopen(cfg->output, cfg->append ? "a" : "w");
if (!out) {
// fallback to stdout and output err
out = stdout;
perror("Failed to open output file.");
}
} else {
out = stdout;
}
if (cfg->debug) {
debug = fopen(cfg->debug, "w");
if (!debug) {
debug = stdout;
perror("Failed to open verbose output file.");
}
} else {
debug = stdout;
}
switch (cfg->format) {
case FORMAT_JSON:
output_s = &output_sjson;
output_f = &output_fjson;
output_o = &output_json;
break;
case FORMAT_CSV:
output_s = &output_scsv;
output_f = &output_fcsv;
output_o = &output_csv;
break;
}
}
void output_quit(void) {
if (out != NULL && out != stdout) {
fclose(out);
}
}
|