aboutsummaryrefslogtreecommitdiff
path: root/src/math/point.c
blob: 409bc4833cd129e362cd3356f5fba1cee1a0c446 (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
/*
 * ecgen, tool for generating Elliptic curve domain parameters
 * Copyright (C) 2017 J08nY
 */
#include "point.h"

point_t *point_new(void) {
	point_t *point = pari_malloc(sizeof(point_t));
	if (!point) {
		perror("Couldn't malloc.");
		exit(1);
	}
	memset(point, 0, sizeof(point_t));
	return point;
}

point_t *point_copy(const point_t *src, point_t *dest) {
	if (src->point) dest->point = gcopy(src->point);
	if (src->order) dest->order = gcopy(src->order);
	if (src->cofactor) dest->cofactor = gcopy(src->cofactor);
	return dest;
}

void point_free(point_t **point) {
	if (*point) {
		pari_free(*point);
		*point = NULL;
	}
}

point_t **points_new(size_t num) {
	point_t **points = pari_malloc(num * sizeof(point_t *));
	if (!points) {
		perror("Couldn't malloc.");
		exit(1);
	}
	memset(points, 0, num * sizeof(point_t *));
	return points;
}

point_t **points_copy(point_t **src, point_t **dest, size_t num) {
	for (size_t i = 0; i < num; ++i) {
		dest[i] = point_new();
		dest[i] = point_copy(src[i], dest[i]);
	}
	return dest;
}

void points_free(point_t ***points) {
	if (*points) {
		pari_free(*points);
		*points = NULL;
	}
}

void points_free_deep(point_t ***points, size_t npoints) {
	if (*points) {
		for (size_t i = 0; i < npoints; ++i) {
			point_free(&(*points)[i]);
		}
		points_free(points);
	}
}

int point_random(curve_t *curve, config_t *cfg, arg_t *args) {
	points_free_deep(&curve->points, curve->npoints);

	point_t *p = point_new();
	p->point = genrand(curve->curve);
	p->order = ellorder(curve->curve, p->point, NULL);

	curve->points = points_new(1);
	curve->points[0] = p;
	curve->npoints = 1;
	return 1;
}

int points_random(curve_t *curve, config_t *cfg, arg_t *args) {
	if (!args) {
		fprintf(stderr, "No args to an arged function. points_random");
		return INT_MIN;
	}
	points_free_deep(&curve->points, curve->npoints);

	size_t npoints = *(size_t *)args->args;

	curve->points = points_new(npoints);
	curve->npoints = npoints;
	for (size_t i = 0; i < npoints; ++i) {
		point_t *p = point_new();
		p->point = genrand(curve->curve);
		p->order = ellorder(curve->curve, p->point, NULL);
		curve->points[i] = p;
	}
	return 1;
}

/*
    GEN o = utoi(dprimes[i]);
    GEN mul = ellmul(curve->curve, rand, o);

    if (gequal0(mul)) {
        printf("Success! %lu\n", npoints);
        curve->points[i] = point_new();

        gerepileall(btop, 2, &rand, &o);
        curve->points[i]->point = rand;
        curve->points[i]->order = o;
        npoints++;
        break;
    }
 */

int points_trial(curve_t *curve, config_t *cfg, arg_t *args) {
	// TODO stack code!!!
	if (!args) {
		fprintf(stderr, "No args to an arged function. points_trial");
		return INT_MIN;
	}
	points_free_deep(&curve->points, curve->npoints);

	pari_ulong *primes = (pari_ulong *)args->args;
	size_t nprimes = args->nargs;

	curve->points = points_new(nprimes);
	curve->npoints = nprimes;

	size_t npoints = 0;
	while (npoints < nprimes) {
		GEN rand = genrand(curve->curve);
		GEN ord = ellorder(curve->curve, rand, NULL);

		for (long i = 0; i < nprimes; ++i) {
			if (curve->points[i] == NULL && dvdis(ord, primes[i])) {
				pari_sp ftop = avma;

				GEN p = stoi(primes[i]);
				GEN mul = divii(ord, p);
				GEN point = ellmul(curve->curve, rand, mul);

				curve->points[i] = point_new();
				gerepileall(ftop, 2, &point, &p);
				curve->points[i]->point = point;
				curve->points[i]->order = p;
				npoints++;
			}
		}
	}

	return 1;
}

int points_prime(curve_t *curve, config_t *cfg, arg_t *args) {
	// TODO stack code!!!
	points_free_deep(&curve->points, curve->npoints);

	GEN factors = Z_factor(curve->order);
	GEN primes = gel(factors, 1);
	long nprimes = glength(primes);
	curve->points = points_new((size_t)nprimes);
	curve->npoints = (size_t)nprimes;

	long npoints = 0;
	while (npoints < nprimes) {
		GEN rand = genrand(curve->curve);
		GEN ord = ellorder(curve->curve, rand, NULL);
		// ord(rand) = ord

		for (long i = 1; i <= nprimes; ++i) {
			if (curve->points[i - 1] == NULL && dvdii(ord, gel(primes, i))) {
				pari_sp ftop = avma;

				// primes[i] divides ord
				// mul = ord/primes[i]
				GEN p = gcopy(gel(primes, i));
				GEN mul = divii(ord, p);
				GEN point = ellmul(curve->curve, rand, mul);

				curve->points[i - 1] = point_new();
				gerepileall(ftop, 2, &point, &p);
				curve->points[i - 1]->point = point;
				curve->points[i - 1]->order = p;
				npoints++;
			}
		}
	}

	return 1;
}