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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/*
* Example of a singular curve, similar to 19.c
* but the Tate pairing degenerates
*
* Consider the curve E: y^2 = x^3 + x^2 over F_19:
* E_ns(F_19) is a cyclic group of order 18.
*/
#include "pbc.h"
#include "pbc_singular.h"
#include "pbc_fp.h"
static void miller(element_t res, element_t P, element_t Q, element_t R, int n)
{
//collate divisions
int m;
element_t v, vd;
element_t Z;
element_t a, b, c;
element_t e0, e1;
mpz_t q;
element_ptr Zx, Zy;
const element_ptr Px = curve_x_coord(P);
const element_ptr Py = curve_y_coord(P);
const element_ptr numx = curve_x_coord(Q);
const element_ptr numy = curve_y_coord(Q);
const element_ptr denomx = curve_x_coord(R);
const element_ptr denomy = curve_y_coord(R);
void do_vertical(element_t e, element_t edenom)
{
element_sub(e0, numx, Zx);
element_mul(e, e, e0);
element_sub(e0, denomx, Zx);
element_mul(edenom, edenom, e0);
}
void do_tangent(element_t e, element_t edenom)
{
//a = -slope_tangent(A.x, A.y);
//b = 1;
//c = -(A.y + a * A.x);
//but we multiply by 2*A.y to avoid division
//a = -Ax * (Ax + Ax + Ax + twicea_2) - a_4;
//This curve is special:
//a = -(3 Ax^2 + 2Ax)
//b = 2 * Ay
//c = -(2 Ay^2 + a Ax);
if (element_is0(Zy)) {
do_vertical(e, edenom);
return;
}
element_square(a, Zx);
element_mul_si(a, a, 3);
element_add(a, a, Zx);
element_add(a, a, Zx);
element_neg(a, a);
element_add(b, Zy, Zy);
element_mul(e0, b, Zy);
element_mul(c, a, Zx);
element_add(c, c, e0);
element_neg(c, c);
element_mul(e0, a, numx);
element_mul(e1, b, numy);
element_add(e0, e0, e1);
element_add(e0, e0, c);
element_mul(e, e, e0);
element_mul(e0, a, denomx);
element_mul(e1, b, denomy);
element_add(e0, e0, e1);
element_add(e0, e0, c);
element_mul(edenom, edenom, e0);
}
void do_line(element_ptr e, element_ptr edenom)
{
if (!element_cmp(Zx, Px)) {
if (!element_cmp(Zy, Py)) {
do_tangent(e, edenom);
} else {
do_vertical(e, edenom);
}
return;
}
element_sub(b, Px, Zx);
element_sub(a, Zy, Py);
element_mul(c, Zx, Py);
element_mul(e0, Zy, Px);
element_sub(c, c, e0);
element_mul(e0, a, numx);
element_mul(e1, b, numy);
element_add(e0, e0, e1);
element_add(e0, e0, c);
element_mul(e, e, e0);
element_mul(e0, a, denomx);
element_mul(e1, b, denomy);
element_add(e0, e0, e1);
element_add(e0, e0, c);
element_mul(edenom, edenom, e0);
}
element_init(a, res->field);
element_init(b, res->field);
element_init(c, res->field);
element_init(e0, res->field);
element_init(e1, res->field);
element_init(v, res->field);
element_init(vd, res->field);
element_init(Z, P->field);
element_set(Z, P);
Zx = curve_x_coord(Z);
Zy = curve_y_coord(Z);
element_set1(v);
element_set1(vd);
mpz_init(q);
mpz_set_ui(q, n);
m = mpz_sizeinbase(q, 2) - 2;
while(m >= 0) {
element_square(v, v);
element_square(vd, vd);
do_tangent(v, vd);
element_double(Z, Z);
do_vertical(vd, v);
if (mpz_tstbit(q, m)) {
do_line(v, vd);
element_add(Z, Z, P);
if (m) {
do_vertical(vd, v);
}
}
m--;
}
mpz_clear(q);
element_invert(vd, vd);
element_mul(res, v, vd);
element_clear(v);
element_clear(vd);
element_clear(Z);
element_clear(a);
element_clear(b);
element_clear(c);
element_clear(e0);
element_clear(e1);
}
static void tate_3(element_ptr out, element_ptr P, element_ptr Q, element_ptr R)
{
mpz_t six;
mpz_init(six);
mpz_set_ui(six, 6);
element_t QR;
element_t e0;
element_init(QR, P->field);
element_init(e0, out->field);
element_add(QR, Q, R);
//for subgroup size 3, -2P = P, hence
//the tangent line at P has divisor 3(P) - 3(O)
miller(out, P, QR, R, 3);
element_pow_mpz(out, out, six);
element_clear(QR);
element_clear(e0);
mpz_clear(six);
}
static void tate_9(element_ptr out, element_ptr P, element_ptr Q, element_ptr R)
{
element_t QR;
element_init(QR, P->field);
element_add(QR, Q, R);
miller(out, P, QR, R, 9);
element_square(out, out);
element_clear(QR);
}
int main(void)
{
field_t c;
field_t Z19;
element_t P, Q, R;
mpz_t q, z;
element_t a;
int i;
mpz_init(q);
mpz_init(z);
mpz_set_ui(q, 19);
field_init_fp(Z19, q);
element_init(a, Z19);
field_init_curve_singular_with_node(c, Z19);
element_init(P, c);
element_init(Q, c);
element_init(R, c);
//(3,+/-6) is a generator
//we have an isomorphism from E_ns to F_19^*
// (3,6) --> 3
//(generally (x,y) --> (y+x)/(y-x)
curve_set_si(R, 3, 6);
for (i=1; i<=18; i++) {
mpz_set_si(z, i);
element_mul_mpz(Q, R, z);
element_printf("%dR = %B\n", i, Q);
}
mpz_set_ui(z, 6);
element_mul_mpz(P, R, z);
//P has order 3
element_printf("P = %B\n", P);
for (i=1; i<=3; i++) {
mpz_set_si(z, i);
element_mul_mpz(Q, R, z);
tate_3(a, P, Q, R);
element_printf("e_3(P,%dP) = %B\n", i, a);
}
element_double(P, R);
//P has order 9
element_printf("P = %B\n", P);
for (i=1; i<=9; i++) {
mpz_set_si(z, i);
element_mul_mpz(Q, P, z);
tate_9(a, P, Q, R);
element_printf("e_9(P,%dP) = %B\n", i, a);
}
return 0;
}
|