-
Notifications
You must be signed in to change notification settings - Fork 5
/
fp2.c
464 lines (405 loc) · 9.17 KB
/
fp2.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* F_p^2 routines
* Ben Lynn
*/
/*
Copyright (C) 2001 Benjamin Lynn ([email protected])
See LICENSE for license
*/
#include <stdlib.h>
#include <string.h>
#include "fp2.h"
#include "mm.h"
enum {
//constants for sliding window algorithms
windowsize = 5,
windowsizepower = 15 //this is 2^(windowsize - 1) - 1
};
//we represent elements of F_p^2 with
//a + sqrt(-1)b where a, b lie in F_p
//these are inefficient?
inline void zp_mul(mpz_t x, mpz_t a, mpz_t b, mpz_t p)
//x = a * b mod p
{
mpz_mul(x, a, b);
mpz_mod(x, x, p);
}
inline void zp_add(mpz_t x, mpz_t a, mpz_t b, mpz_t p)
//x = a + b mod p
{
mpz_add(x, a, b);
if (mpz_cmp(x, p) >= 0) mpz_sub(x, x, p);
/*
mpz_add(x, a, b);
mpz_mod(x, x, p);
*/
}
inline void zp_neg(mpz_t x, mpz_t b, mpz_t p)
//x = -b mod p
{
if (mpz_sgn(b)) {
mpz_sub(x, p, b);
}
/*
mpz_sub(x, a, b);
mpz_mod(x, x, p);
*/
}
inline void zp_sub(mpz_t x, mpz_t a, mpz_t b, mpz_t p)
//x = a - b mod p
{
if (mpz_cmp(a, b) >= 0) {
mpz_sub(x, a, b);
} else {
mpz_sub(x, a, b);
mpz_add(x, x, p);
}
/*
mpz_sub(x, a, b);
mpz_mod(x, x, p);
*/
}
void fp2_init(fp2_ptr x)
//call before using x
//allocates space for x
{
mm_tally("fp2", 1, "init");
mpz_init(x->a);
mpz_init(x->b);
}
void fp2_clear(fp2_ptr x)
//deallocates space for x
{
mm_tally("fp2", -1, "clear");
mpz_clear(x->a);
mpz_clear(x->b);
}
void fp2_set(fp2_ptr x, fp2_ptr y)
//assignment
//x = y
{
mpz_set(x->a, y->a);
mpz_set(x->b, y->b);
}
void fp2_set_0(fp2_ptr x)
//x = 0
{
mpz_set_ui(x->a, 0);
mpz_set_ui(x->b, 0);
}
void fp2_set_1(fp2_ptr x)
//x = 1
{
mpz_set_ui(x->a, 1);
mpz_set_ui(x->b, 0);
}
void fp2_set_cbrt_unity(fp2_ptr x, mpz_t p)
{
//Warning: depends on representation of F_p^2
//Z_p[sqrt(-1)] version:
mpz_t p1on4;
mpz_set(x->a, p);
mpz_sub_ui(x->a, x->a, 1);
mpz_div_ui(x->a, x->a, 2);
mpz_init_set(p1on4, p);
mpz_add_ui(p1on4, p1on4, 1);
mpz_div_ui(p1on4, p1on4, 4);
mpz_set_ui(x->b, 3);
mpz_powm(x->b, x->b, p1on4, p);
//inefficient way of halving
mpz_mul(x->b, x->b, x->a);
mpz_mod(x->b, x->b, p);
mpz_clear(p1on4);
//Z_p[sqrt(-3)] version:
//mpz_set(x->a, p);
//mpz_sub_ui(x->a, x->a, 1);
//mpz_div_ui(x->a, x->a, 2);
//mpz_set(x->b, x->a);
}
void fp2_init_set(fp2_ptr x, fp2_ptr y)
//for convenience
//combines fp2_init, fp2_set
{
fp2_init(x);
fp2_set(x, y);
}
void fp2_neg(fp2_ptr x, fp2_ptr a, mpz_t p)
//x = -a
{
mpz_sub(x->a, p, a->a);
mpz_sub(x->b, p, a->b);
}
void fp2_add(fp2_ptr x, fp2_ptr a, fp2_ptr b, mpz_t p)
//x = a + b
{
zp_add(x->a, a->a, b->a, p);
zp_add(x->b, a->b, b->b, p);
}
void fp2_sub(fp2_ptr x, fp2_ptr a, fp2_ptr b, mpz_t p)
//x = a - b
{
zp_sub(x->a, a->a, b->a, p);
zp_sub(x->b, a->b, b->b, p);
}
void fp2_sub_ui(fp2_ptr x, fp2_ptr a, unsigned long b, mpz_t p)
//x = a - b
{
mpz_sub_ui(x->a, a->a, b);
mpz_mod(x->a, x->a, p);
}
void fp2_sqr(fp2_ptr x, fp2_ptr a, mpz_t p)
//x = a * a
{
mpz_t t0, t1; //temp variables
mpz_init(t0); mpz_init(t1);
//sqr_mod(t0, a->a);
//sqr_mod(t1, a->b);
//zp_sub(t0, t0, t1, p);
/*
zp_sub(t0, a->a, a->b, p);
zp_add(t1, a->a, a->b, p);
zp_mul(t0, t0, t1, p);
zp_mul(t1, a->a, a->b, p);
zp_add(x->b, t1, t1, p);
mpz_set(x->a, t0);
*/
mpz_sub(t0, a->a, a->b);
mpz_add(t1, a->a, a->b);
mpz_mul(t0, t0, t1);
mpz_mul(t1, a->a, a->b);
mpz_mul_2exp(x->b, t1, 1);
mpz_mod(x->b, x->b, p);
mpz_mod(x->a, t0, p);
mpz_clear(t0); mpz_clear(t1);
}
void fp2_mul_mpz(fp2_ptr x, fp2_ptr a, mpz_ptr b, mpz_t p)
//x = a * b
{
zp_mul(x->a, a->a, b, p);
zp_mul(x->b, a->b, b, p);
}
void fp2_mul(fp2_ptr x, fp2_ptr a, fp2_ptr b, mpz_t p)
//x = a * b
{
mpz_t t0, t1, t2; //temp variables
mpz_init(t0); mpz_init(t1); mpz_init(t2);
/* This is slower
zp_mul(t0, a->a, b->a);
zp_mul(t1, a->b, b->b);
zp_sub(t0, t0, t1);
zp_mul(t1, a->a, b->b);
zp_mul(t2, a->b, b->a);
zp_add(x->b, t1, t2);
mpz_set(x->a, t0);
*/
mpz_mul(t0, a->a, b->a);
mpz_mul(t1, a->b, b->b);
mpz_sub(t0, t0, t1);
mpz_mul(t1, a->a, b->b);
mpz_mul(t2, a->b, b->a);
mpz_add(x->b, t1, t2);
mpz_mod(x->b, x->b, p);
mpz_mod(x->a, t0, p);
mpz_clear(t0); mpz_clear(t1); mpz_clear(t2);
}
void fp2_inv(fp2_ptr x, fp2_ptr b, mpz_t p)
//x = 1 / b
{
mpz_t t0, t1; //temp variables
mpz_t t2;
mpz_init(t2);
mpz_init(t0); mpz_init(t1);
/*
zp_mul(t0, b->a, b->a);
zp_mul(t1, b->b, b->b);
zp_add(t0, t0, t1);
inv_mod(t1, t0);
zp_mul(t0, t1, b->b);
zp_neg(x->b, t0);
zp_mul(x->a, t1, b->a);
*/
mpz_mul(t0, b->a, b->a);
mpz_mul(t1, b->b, b->b);
mpz_add(t0, t0, t1);
mpz_invert(t1, t0, p);
mpz_mul(t0, t1, b->b);
mpz_neg(x->b, t0);
mpz_mod(x->b, x->b, p);
mpz_mul(x->a, t1, b->a);
mpz_mod(x->a, x->a, p);
mpz_clear(t0); mpz_clear(t1);
mpz_clear(t2);
}
void fp2_div(fp2_ptr x, fp2_ptr a, fp2_ptr b, mpz_t p)
//x = a / b
{
mpz_t t0, t1, t2, t3; //temp variables
mpz_init(t0); mpz_init(t1); mpz_init(t2); mpz_init(t3);
/*
zp_mul(t0, b->a, b->a);
zp_mul(t1, b->b, b->b);
zp_add(t0, t0, t1);
inv_mod(t1, t0);
zp_mul(t0, a->a, b->a);
zp_mul(t2, a->b, b->b);
zp_add(t0, t0, t2);
zp_mul(t2, a->b, b->a);
zp_mul(t3, a->a, b->b);
zp_sub(t2, t2, t3);
zp_mul(x->b, t1, t2);
zp_mul(x->a, t1, t0);
*/
mpz_mul(t0, b->a, b->a);
mpz_mul(t1, b->b, b->b);
mpz_add(t0, t0, t1);
mpz_invert(t1, t0, p);
mpz_mul(t0, a->a, b->a);
mpz_mul(t2, a->b, b->b);
mpz_add(t0, t0, t2);
mpz_mul(t2, a->b, b->a);
mpz_mul(t3, a->a, b->b);
mpz_sub(t2, t2, t3);
mpz_mul(x->b, t1, t2);
mpz_mod(x->b, x->b, p);
mpz_mul(x->a, t1, t0);
mpz_mod(x->a, x->a, p);
mpz_clear(t0); mpz_clear(t1); mpz_clear(t2); mpz_clear(t3);
}
void fp2_pow(fp2_ptr res, fp2_ptr x, mpz_t n, mpz_t p)
//exponentiation
//res = x^n
{
int i, j, k, l, m;
mpz_t t0, t1; //temp variables
//use sliding-window method
fp2_t g[2*windowsizepower+2];
fp2_init_set(g[1], x);
fp2_init(g[2]);
fp2_mul(g[2], x, x, p);
mpz_init(t0); mpz_init(t1);
for (i=1; i<=windowsizepower; i++) {
j = 2 * i + 1;
fp2_init(g[j]);
fp2_mul(g[j], g[j - 2], g[2], p);
}
m = mpz_sizeinbase(n, 2) - 1;
fp2_set_1(res);
while(m>=0) {
if (!mpz_tstbit(n, m)) {
{ //fp2_sqr(res,res);
mpz_sub(t0, res->a, res->b);
mpz_add(t1, res->a, res->b);
mpz_mul(t0, t0, t1);
mpz_mul(t1, res->a, res->b);
mpz_mul_2exp(res->b, t1, 1);
mpz_mod(res->b, res->b, p);
mpz_mod(res->a, t0, p);
}
m--;
} else {
l = m - windowsize + 1;
if (l < 0) l = 0;
l = mpz_scan1(n, l);
j = 1;
{ //fp2_sqr(res,res);
mpz_sub(t0, res->a, res->b);
mpz_add(t1, res->a, res->b);
mpz_mul(t0, t0, t1);
mpz_mul(t1, res->a, res->b);
mpz_mul_2exp(res->b, t1, 1);
mpz_mod(res->b, res->b, p);
mpz_mod(res->a, t0, p);
}
for (k = m - 1; k>=l; k--) {
j = j << 1;
if (mpz_tstbit(n, k)) j++;
{ //fp2_sqr(res,res);
mpz_sub(t0, res->a, res->b);
mpz_add(t1, res->a, res->b);
mpz_mul(t0, t0, t1);
mpz_mul(t1, res->a, res->b);
mpz_mul_2exp(res->b, t1, 1);
mpz_mod(res->b, res->b, p);
mpz_mod(res->a, t0, p);
}
}
fp2_mul(res, res, g[j], p);
m = l-1;
}
}
fp2_clear(g[2]);
for (i=0; i<=windowsizepower; i++) {
j = 2 * i + 1;
fp2_clear(g[j]);
}
mpz_clear(t0); mpz_clear(t1);
/* simple method:
int m = mpz_sizeinbase(n, 2) - 1;
fp2_t x1;
fp2_init_set(x1, x);
for(;;) {
if (mpz_tstbit(n, m)) {
fp2_mul(res, res, x1);
}
if (m == 0) break;
fp2_mul(res, res, res);
m--;
}
*/
}
int fp2_is_0(fp2_ptr x)
// x == 0?
{
return (!(mpz_size(x->a) || mpz_size(x->b)));
}
int fp2_equal(fp2_ptr x, fp2_ptr y)
// x == y?
{
return (!(mpz_cmp(x->a, y->a) || mpz_cmp(x->b, y->b)));
}
size_t fp2_out_str(FILE *stream, int base, fp2_ptr x)
//output x on stream in given base
{
FILE *fp;
size_t s, status;
if (!stream) fp = stdout;
else fp = stream;
status = fprintf(fp, "[");
if (status < 0) return status;
s = status;
status = mpz_out_str(fp, base, x->a);
if (status < 0) return status;
s += status;
status = fprintf(fp, " ");
if (status < 0) return status;
s += status;
status = mpz_out_str(fp, base, x->b);
if (status < 0) return status;
s += status;
status = fprintf(fp, "]");
if (status < 0) return status;
s += status;
return s;
}
int fp2_set_str(fp2_t x, char *s, int base)
//set value of x from s, a NULL-terminated C string, in base `base'
//returns -1 on error
{
int len;
char *sp;
char *buf;
len = strlen(s);
fp2_set_0(x);
if (!len) return -1;
if (s[0] != '[') return -1;
if (s[len - 1] != ']') return -1;
buf = (char *) alloca(len - 1);
memcpy(buf, &s[1], len - 2);
buf[len - 2] = 0;
sp = strchr(buf, ' ');
if (!sp) return -1;
*sp = 0;
sp++;
if (mpz_set_str(x->a, buf, base)) return -1;
if (mpz_set_str(x->b, sp, base)) return -1;
return 0;
}