-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathec_pqr.c
214 lines (173 loc) · 5.24 KB
/
ec_pqr.c
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright 2016 naehrwert
* Licensed under the terms of the GNU GPL, version 2
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*/
#include <stdlib.h>
#include "ec_pqr.h"
static void _ec_pqr_add(poly_t *d, poly_t *a, poly_t *b, ec_pqr_group_t *ecg)
{
pqr_add(d, a, b, ecg->p);
}
static void _ec_pqr_sub(poly_t *d, poly_t *a, poly_t *b, ec_pqr_group_t *ecg)
{
pqr_sub(d, a, b, ecg->p);
}
static void _ec_pqr_mul(poly_t *d, poly_t *a, poly_t *b, ec_pqr_group_t *ecg)
{
pqr_mul(d, a, b, ecg->p);
}
static void _ec_pqr_square(poly_t *d, poly_t *a, ec_pqr_group_t *ecg)
{
_ec_pqr_mul(d, a, a, ecg);
}
static void _ec_pqr_inv(poly_t *d, poly_t *a, ec_pqr_group_t *ecg)
{
pqr_inv(d, a, ecg->p);
}
ec_pqr_point_t *ec_pqr_point_alloc(ec_pqr_group_t *ecg)
{
ec_pqr_point_t *res;
if ((res = (ec_pqr_point_t *)mem_alloc(sizeof(ec_pqr_point_t))) == NULL)
return NULL;
//(degree - 1) since x, y are in the polynomial quotient ring.
res->x = poly_alloc(ecg->p->degree - 1, ecg->p->N, 1);
res->y = poly_alloc(ecg->p->degree - 1, ecg->p->N, 1);
return res;
}
void ec_pqr_point_free(ec_pqr_point_t *p)
{
poly_free(p->x, 1);
poly_free(p->y, 1);
mem_free(p);
}
ec_pqr_point_t *ec_pqr_point_copy(ec_pqr_point_t *d, ec_pqr_point_t *s)
{
//TODO: maybe we don't need to force adjust here.
poly_copy(d->x, s->x, 1);
poly_copy(d->y, s->y, 1);
return d;
}
int ec_pqr_point_cmp(ec_pqr_point_t *p, ec_pqr_point_t *q)
{
if (poly_cmp(p->x, q->x) == POLY_CMP_E && poly_cmp(p->y, q->y) == POLY_CMP_E)
return POINT_CMP_E;
return POINT_CMP_NE;
}
ec_pqr_point_t *ec_pqr_point_zero(ec_pqr_point_t *p)
{
poly_zero(p->x);
poly_zero(p->y);
return p;
}
int ec_pqr_point_is_zero(ec_pqr_point_t *p)
{
return (poly_is_zero(p->x) && poly_is_zero(p->y));
}
void ec_pqr_point_to_mon(ec_pqr_point_t *p)
{
poly_to_mon(p->x);
poly_to_mon(p->y);
}
void ec_pqr_point_from_mon(ec_pqr_point_t *p)
{
poly_from_mon(p->x);
poly_from_mon(p->y);
}
ec_pqr_point_t *ec_pqr_point_neg(ec_pqr_point_t *r, ec_pqr_point_t *p, ec_pqr_group_t *ecg)
{
poly_copy(r->x, p->x, 1);
poly_zero(r->y);
pqr_sub(r->y, r->y, p->y, ecg->p);
return r;
}
ec_pqr_point_t *ec_pqr_point_double(ec_pqr_point_t *r, ec_pqr_point_t *p, ec_pqr_group_t *ecg)
{
// Handle trivial case.
if (poly_is_zero(p->y))
{
ec_pqr_point_zero(r);
return r;
}
poly_t *s = poly_alloc(ecg->p->degree - 1, ecg->p->N, 1),
*t = poly_alloc(ecg->p->degree - 1, ecg->p->N, 1);
ec_pqr_point_t *pp = ec_pqr_point_copy(ec_pqr_point_alloc(ecg), p);
poly_t *px = pp->x, *py = pp->y, *rx = r->x, *ry = r->y;
_ec_pqr_square(t, px, ecg); // t = px*px
_ec_pqr_add(s, t, t, ecg); // s = 2*px*px
_ec_pqr_add(s, s, t, ecg); // s = 3*px*px
_ec_pqr_add(s, s, ecg->a, ecg); // s = 3*px*px + a
_ec_pqr_add(t, py, py, ecg); // t = 2*py
_ec_pqr_inv(t, t, ecg); // t = 1/(2*py)
_ec_pqr_mul(s, s, t, ecg); // s = (3*px*px+a)/(2*py)
_ec_pqr_square(rx, s, ecg); // rx = s*s
_ec_pqr_add(t, px, px, ecg); // t = 2*px
_ec_pqr_sub(rx, rx, t, ecg); // rx = s*s - 2*px
_ec_pqr_sub(t, px, rx, ecg); // t = -(rx-px)
_ec_pqr_mul(ry, s, t, ecg); // ry = -s*(rx-px)
_ec_pqr_sub(ry, ry, py, ecg); // ry = -s*(rx-px) - py
poly_free(s, 1);
poly_free(t, 1);
ec_pqr_point_free(pp);
return r;
}
ec_pqr_point_t *ec_pqr_point_add(ec_pqr_point_t *r, ec_pqr_point_t *p, ec_pqr_point_t *q, ec_pqr_group_t *ecg)
{
// Handle trivial cases.
if (ec_pqr_point_is_zero(p))
{
ec_pqr_point_copy(r, q);
return r;
}
if (ec_pqr_point_is_zero(q))
{
ec_pqr_point_copy(r, p);
return r;
}
poly_t *s = poly_alloc(ecg->p->degree - 1, ecg->p->N, 1),
*t = poly_alloc(ecg->p->degree - 1, ecg->p->N, 1),
*u = poly_alloc(ecg->p->degree - 1, ecg->p->N, 1);
ec_pqr_point_t *pp = ec_pqr_point_copy(ec_pqr_point_alloc(ecg), p),
*qq = ec_pqr_point_copy(ec_pqr_point_alloc(ecg), q);
poly_t *px = pp->x, *py = pp->y, *qx = qq->x, *qy = qq->y, *rx = r->x, *ry = r->y;
// Handle limit cases.
_ec_pqr_sub(u, qx, px, ecg);
if (poly_is_zero(u))
{
_ec_pqr_sub(u, qy, py, ecg);
if (poly_is_zero(u))
ec_pqr_point_double(r, pp, ecg);
else
ec_pqr_point_zero(r);
return r;
}
_ec_pqr_inv(t, u, ecg); // t = 1/(qx-px)
_ec_pqr_sub(u, qy, py, ecg); // u = qy-py
_ec_pqr_mul(s, t, u, ecg); // s = (qy-py)/(qx-px)
_ec_pqr_square(rx, s, ecg); // rx = s*s
_ec_pqr_add(t, px, qx, ecg); // t = px+qx
_ec_pqr_sub(rx, rx, t, ecg); // rx = s*s - (px+qx)
_ec_pqr_sub(t, px, rx, ecg); // t = -(rx-px)
_ec_pqr_mul(ry, s, t, ecg); // ry = -s*(rx-px)
_ec_pqr_sub(ry, ry, py, ecg); // ry = -s*(rx-px) - py
poly_free(s, 1);
poly_free(t, 1);
poly_free(u, 1);
ec_pqr_point_free(pp);
ec_pqr_point_free(qq);
return r;
}
ec_pqr_point_t *ec_pqr_point_mul(ec_pqr_point_t *d, bn_t *a, ec_pqr_point_t *b, ec_pqr_group_t *ecg)
{
int i;
ec_pqr_point_t *bt = ec_pqr_point_copy(ec_pqr_point_alloc(ecg), b);
ec_pqr_point_zero(d);
for (i = 0; i <= bn_maxbit(a); i++)
{
if (bn_getbit(a, i))
ec_pqr_point_add(d, d, bt, ecg);
ec_pqr_point_double(bt, bt, ecg);
}
ec_pqr_point_free(bt);
return d;
}