MPHELL  4.0.0
mphell-fp2.c
Go to the documentation of this file.
1 /*
2  MPHELL-4.0
3  Author(s): The MPHELL team
4 
5  (C) Copyright 2015-2018 - Institut Fourier / Univ. Grenoble Alpes (France)
6 
7  This file is part of the MPHELL Library.
8  MPHELL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation, version 3 of the License.
11 
12  MPHELL is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with MPHELL. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
26 #include "mphell-fp2.h"
27 
28 /*************************************DEBUG***********************************/
29 
39 void
40 fp2_elt_print (fp2_elt_srcptr src, const uint8_t base, const bool lift, const fp2_param param, uint8_t stack)
41 {
42  char *str;
43  fp2_elt_str(&str, src, base, lift, param, stack);
44  printf("%s", str);
45  free(str);
46 }
47 
48 /**************************************TMP************************************/
49 
50 void
51 fp2_elt_get_pool_elt (fp2_elt * dst, const fp2_param param, uint8_t stack)
52 {
53  fp_elt_get_pool_elt(&(dst->v0), param->base, stack);
54  fp_elt_get_pool_elt(&(dst->v1), param->base, stack);
55 }
56 
57 void
58 fp2_elt_relax_pool_elt (fp2_elt * dst, const fp2_param param, uint8_t stack)
59 {
60  fp_elt_relax_pool_elt(&(dst->v0), param->base, stack);
61  fp_elt_relax_pool_elt(&(dst->v1), param->base, stack);
62 }
63 
64 /************************************SETTERS**********************************/
65 
66 void
67 fp2_alloc (fp2_param param, const fp_param base)
68 {
69  param->size = base->size;
70  param->base = base;
71  number_init(&(param->q), 2 * param->size);
72  fp_elt_alloc(&(param->non_residue), base);
73  fp_elt_init(param->non_residue, base);
74  fp2_elt_alloc(&(param->non_res), param);
75  fp2_elt_init(&(param->non_res), param);
76  fp2_elt_alloc(&(param->gen_sylow), param);
77  fp2_elt_init(&(param->gen_sylow), param);
78  number_init(&(param->p_odd), 2 * param->size);
79 }
80 
87 void
88 fp2_prepare_sqrt(fp2_param param, uint8_t stack)
89 {
90  do
91  {
92  fp2_elt_random(&(param->non_res), param, stack);
93  } while (fp2_elt_issquare(&(param->non_res), param, stack));
94 
95  number_dec(param->p_odd, param->q);
96  param->p_even = 0;
97  while (number_iseven(param->p_odd))
98  {
99  ++(param->p_even);
100  number_rshift(param->p_odd, param->p_odd, 1);
101  }
102  fp2_elt_pow_number(&(param->gen_sylow), &(param->non_res), param->p_odd, param, stack);
103 }
104 
105 void
106 fp2_create (fp2_param param, const fp_param base, fp_elt_srcptr non_residue, uint8_t stack)
107 {
108  param->base = base;
109  number_sqr(param->q, base->p);
110  fp_elt_copy(param->non_residue, non_residue, base);
111  fp2_prepare_sqrt(param, stack);
112 
113  /* For FLT */
114 /* number pm2;*/
115 /* number_tmp_alloc(&pm2, 2*number_block_size(base->p), stack);*/
116 /* number_dec(pm2, base->p);*/
117 /* number_mul(pm2, pm2, base->p);*/
118 /* number_dec(pm2, pm2);*/
119 /* number_str(&(param->pm2), pm2, 2);*/
120 /* number_tmp_free(&pm2, 2*number_block_size(base->p), stack);*/
121 
122 }
123 
124 void
125 fp2_copy (fp2_param param_res, const fp2_param param)
126 {
127  param_res->base = param->base;
128  number_copy(param_res->q, param->q);
129  fp_elt_copy(param_res->non_residue, param->non_residue, param->base);
130 }
131 
132 void
134 {
135  number_free(&(param->q));
136  fp_elt_free(&(param->non_residue));
137  fp2_elt_free(&(param->non_res));
138  fp2_elt_free(&(param->gen_sylow));
139  number_free(&(param->p_odd));
140  param->size = (uint8_t)0;
141 }
142 
143 void
144 fp2_get_characteristic (number_ptr c, const fp2_param param)
145 {
146  fp_get_characteristic (c, param->base);
147 }
148 
149 void
150 fp2_get_size (number_ptr c, const fp2_param param)
151 {
152  number_copy(c, param->q);
153 }
154 
155 /*void*/
156 /*fp2_get_pm2 (char * str, const fp2_param param)*/
157 /*{*/
158 /* strcpy(str, param->pm2);*/
159 /*}*/
160 
161 void
162 fp2_elt_alloc (fp2_elt * dst, const fp2_param param)
163 {
164  fp_elt_alloc(&(dst->v0), param->base);
165  fp_elt_alloc(&(dst->v1), param->base);
166 }
167 
168 void
170 {
171  fp_elt_init(dst->v0, param->base);
172  fp_elt_init(dst->v1, param->base);
173 }
174 
175 void
177 {
178  fp_elt_copy(dst->v0, src->v0, param->base);
179  fp_elt_copy(dst->v1, src->v1, param->base);
180 }
181 
182 void
184 {
185  fp_elt_clear(&(src->v0));
186  fp_elt_clear(&(src->v1));
187 }
188 
189 void
191 {
192  fp_elt_free(&(src->v0));
193  fp_elt_free(&(src->v1));
194 }
195 
196 void
198 {
199  fp_elt_set_one(dst->v0, param->base);
200  fp_elt_set_zero(dst->v1, param->base);
201 }
202 
203 void
204 fp2_elt_set_ui (fp2_elt_ptr dst, const uint64_t src, const bool isreduced,
205  const fp2_param param, uint8_t stack)
206 {
207  fp_elt_set_ui(dst->v0, src, isreduced, param->base, stack);
208  fp_elt_set_zero(dst->v1, param->base);
209 }
210 
211 void
212 fp2_elt_set_number (fp2_elt_ptr dst, number_srcptr src, const bool isreduced,
213  const fp2_param param, uint8_t stack)
214 {
215  fp_elt_set_number(dst->v0, src, isreduced, param->base, stack);
216  fp_elt_set_zero(dst->v1, param->base);
217 }
218 
219 void
220 fp2_elt_set_fp_elt (fp2_elt_ptr dst, fp_elt_srcptr src, const fp2_param param)
221 {
222  fp_elt_copy(dst->v0, src, param->base);
223  fp_elt_set_zero(dst->v1, param->base);
224 }
225 
226 void
227 fp2_elt_set (fp2_elt_ptr dst, number_srcptr src1, number_srcptr src2,
228  const bool isreduced, const fp2_param param, uint8_t stack)
229 {
230  fp_elt_set_number(dst->v0, src1, isreduced, param->base, stack);
231  fp_elt_set_number(dst->v1, src2, isreduced, param->base, stack);
232 }
233 
234 void
235 fp2_elt_set_fp_elts (fp2_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2, const fp2_param param)
236 {
237  fp_elt_copy(dst->v0, src1, param->base);
238  fp_elt_copy(dst->v1, src2, param->base);
239 }
240 
241 void
242 fp2_elt_set_str (fp2_elt_ptr dst, const char *str, const uint8_t base,
243  const bool isreduced, const fp2_param param, uint8_t stack)
244 {
245  char *s = (char*) malloc(sizeof(char)*strlen(str)+10);
246  char *mem =s;
247  strcpy(s, str);
248 
249  char *str1, *str2 = NULL;
250  str1 = strsep(&s, ",");
251  str2 = strsep(&s, ",");
252  MPHELL_ASSERT_ALWAYS((str1!= NULL) && (str2 != NULL),
253  "fp2_elt_set_str : invalid string");
254  fp_elt_set_str(dst->v0, str1, base, isreduced, param->base, stack);
255  fp_elt_set_str(dst->v1, str2, base, isreduced, param->base, stack);
256  free(mem);
257 }
258 
259 void
260 fp2_elt_random (fp2_elt_ptr dst, const fp2_param param, uint8_t stack)
261 {
262  fp_elt_random(dst->v0, param->base, stack);
263  fp_elt_random(dst->v1, param->base, stack);
264 }
265 
266 void
267 fp2_elt_lift (fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
268 {
269  fp_elt_lift(dst->v0, src->v0, param->base, stack);
270  fp_elt_lift(dst->v1, src->v1, param->base, stack);
271 }
272 
273 void
274 fp2_str (char **str, const fp2_param param, const uint8_t base, uint8_t stack)
275 {
276  char *base_str, *non_residue_str, *q_str;
277 
278  fp_str(&base_str, param->base, base, stack);
279  number_str(&q_str, param->q, base);
280  fp_elt_str(&non_residue_str, param->non_residue, base, true, param->base, stack);
281 
282  *str = (char*)malloc(100 + strlen(base_str) + strlen(q_str) +
283  strlen(non_residue_str));
284  char *s = *str;
285  sprintf(s, "base parameter \n%s\nnon residue quadratic = %s\norder = %s",
286  base_str, non_residue_str, q_str);
287 
288  free(base_str);
289  free(q_str);
290  free(non_residue_str);
291 }
292 
293 void
294 fp2_elt_str (char **str, fp2_elt_srcptr src, const uint8_t base,
295  const bool lift, const fp2_param param, uint8_t stack)
296 {
297  char *str1, *str2;
298  fp_elt_str(&str1, src->v0, base, lift, param->base, stack);
299  fp_elt_str(&str2, src->v1, base, lift, param->base, stack);
300  *str = malloc(strlen(str1) + strlen(str2) + 4);
301  sprintf(*str, "%s,%s", str1, str2);
302  free(str1);
303  free(str2);
304 }
305 
306 
307 
308 /*************************COMPARISON AND LOGICAL******************************/
309 
310 int8_t
311 fp2_elt_cmp_fp_elt (fp2_elt_srcptr src1, fp_elt_srcptr src2, const fp2_param param)
312 {
313  if(!fp_elt_iszero(src1->v1, param->base))
314  {
315  return 1;
316  }
317  return fp_elt_cmp(src1->v0, src2, param->base);
318 }
319 
320 int8_t
322 {
323  int8_t res = fp_elt_cmp(src1->v1, src2->v1, param->base);
324  if(res == 0)
325  {
326  return fp_elt_cmp(src1->v0, src2->v0, param->base);
327  }
328  else
329  {
330  return res;
331  }
332 }
333 
334 bool
336 {
337  return (fp_elt_isone(src->v0, param->base) && fp_elt_iszero(src->v1, param->base));
338 }
339 
340 bool
342 {
343  return (fp_elt_iszero(src->v0, param->base) && fp_elt_iszero(src->v1, param->base));
344 }
345 
346 /***************************ADDITION SUBTRACTION******************************/
347 
348 void
350 {
351  fp_elt_inc(dst->v0, src->v0, param->base);
352  fp_elt_copy(dst->v1, src->v1, param->base);
353 }
354 
355 void
356 fp2_elt_add_fp_elt (fp2_elt_ptr dst, fp2_elt_srcptr src1, fp_elt_srcptr src2,
357  const fp2_param param)
358 {
359  fp_elt_add(dst->v0, src1->v0, src2, param->base);
360  fp_elt_copy(dst->v1, src1->v1, param->base);
361 }
362 
363 void
365  const fp2_param param)
366 {
367  fp_elt_add(dst->v0, src1->v0, src2->v0, param->base);
368  fp_elt_add(dst->v1, src1->v1, src2->v1, param->base);
369 }
370 
371 void
373 {
374  fp_elt_dec(dst->v0, src->v0, param->base);
375  fp_elt_copy(dst->v1, src->v1, param->base);
376 }
377 
378 void
379 fp2_elt_sub_fp_elt (fp2_elt_ptr dst, fp2_elt_srcptr src1, fp_elt_srcptr src2,
380  const fp2_param param)
381 {
382  fp_elt_sub(dst->v0, src1->v0, src2, param->base);
383  fp_elt_copy(dst->v1, src1->v1, param->base);
384 }
385 
386 void
388  const fp2_param param)
389 {
390  fp_elt_sub(dst->v0, src1->v0, src2->v0, param->base);
391  fp_elt_sub(dst->v1, src1->v1, src2->v1, param->base);
392 }
393 
394 void
395 fp2_elt_neg_fp_elt (fp2_elt_ptr dst, fp_elt_srcptr src, const fp2_param param)
396 {
397  fp_elt_neg(dst->v0, src, param->base);
398  fp_elt_set_zero(dst->v1, param->base);
399 }
400 
401 void
403 {
404  fp_elt_neg(dst->v0, src->v0, param->base);
405  fp_elt_neg(dst->v1, src->v1, param->base);
406 }
407 
408 /*******************************MULTIPLICATION********************************/
409 
410 void
411 fp2_elt_mul_fp_elt (fp2_elt_ptr dst, fp2_elt_srcptr src1, fp_elt_srcptr src2,
412  const fp2_param param, uint8_t stack)
413 {
414  fp_elt_mul(dst->v0, src1->v0, src2, param->base, stack);
415  fp_elt_mul(dst->v1, src1->v1, src2, param->base, stack);
416 }
417 
418 void
420  const fp2_param param)
421 {
422  fp_elt_mul2(dst->v0, src->v0, param->base);
423  fp_elt_mul2(dst->v1, src->v1, param->base);
424 }
425 
426 void
428  const fp2_param param)
429 {
430  fp_elt_mul4(dst->v0, src->v0, param->base);
431  fp_elt_mul4(dst->v1, src->v1, param->base);
432 }
433 
434 void
436  const fp2_param param)
437 {
438  fp_elt_mul8(dst->v0, src->v0, param->base);
439  fp_elt_mul8(dst->v1, src->v1, param->base);
440 }
441 
442 void
444  const fp2_param param, uint8_t stack)
445 {
446  fp_elt_mul3(dst->v0, src->v0, param->base, stack);
447  fp_elt_mul3(dst->v1, src->v1, param->base, stack);
448 }
449 
450 void
452  const fp2_param param, uint8_t stack)
453 {
454  /* src1 = [v0, v1] */
455  /* src2 = [y0, y1] */
456  fp_param_t *base_param = (fp_param_t*)(param->base);
457 
458  fp_elt u0, u1, v, w;
459  fp_elt_get_pool_elt(&u0, base_param, stack);
460  fp_elt_get_pool_elt(&u1, base_param, stack);
461  fp_elt_get_pool_elt(&v, base_param, stack);
462  fp_elt_get_pool_elt(&w, base_param, stack);
463 
464  fp_elt_add(u0, src1->v0, src1->v1, base_param); /* u0 = v0 + v1 */
465  fp_elt_add(u1, src2->v0, src2->v1, base_param); /* u1 = y0 + y1 */
466  fp_elt_mul(w, u0, u1, base_param, stack); /* w = (v0+v1)(y0+y1) */
467 
468  fp_elt_mul(u0, src1->v0, src2->v0, base_param, stack); /* u0 = v0 * y0 */
469  fp_elt_mul(u1, src1->v1, src2->v1, base_param, stack); /* u1 = v1 * y1 */
470  fp_elt_sub(dst->v1, w, u0, base_param); /* dst->v1 = w - u0 */
471  fp_elt_sub(dst->v1, dst->v1, u1, base_param); /* dst->v1 = v0y1 + y0v1 */
472 
473  fp_elt_mul(w, u1, param->non_residue, base_param, stack); /* w = u1 * non_square_elt */
474  fp_elt_add(dst->v0, u0, w, base_param); /* dst->v0 = v0y0 + non_square_elt*v1y1 */
475 
476  fp_elt_relax_pool_elt(&u0, base_param, stack);
477  fp_elt_relax_pool_elt(&u1, base_param, stack);
478  fp_elt_relax_pool_elt(&v, base_param, stack);
479  fp_elt_relax_pool_elt(&w, base_param, stack);
480 }
481 
482 void
483 fp2_elt_sqr_fp_elt (fp2_elt_ptr dst, fp_elt_srcptr src, const fp2_param param, uint8_t stack)
484 {
485  fp_elt_sqr(dst->v0, src, param->base, stack);
486  fp_elt_set_zero(dst->v1, param->base);
487 }
488 
489 /*
490  * Set dst to src^2 mod (fp->p).
491  * This function requires that src is defined on fp and that dst has
492  * been allocated.
493  */
494 void
495 fp2_elt_sqr (fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
496 {
497  fp2_elt_mul(dst, src, src, param, stack);
498 }
499 
500 void
501 fp2_elt_inv_fp_elt (fp2_elt_ptr dst, fp_elt_srcptr src, const fp2_param param, uint8_t stack)
502 {
503  fp_elt_inv(dst->v0, src, param->base, stack);
504  fp_elt_set_zero(dst->v1, param->base);
505 }
506 
507 void
508 fp2_elt_inv (fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
509 {
510  /* src = [v0, v1] */
511  /* d = non_residue (quadratic) */
512  fp_param_t *base_param = (fp_param_t*)(param->base);
513  fp_elt tmp, tmp1;
514  fp_elt_get_pool_elt(&tmp, base_param, stack);
515  fp_elt_get_pool_elt(&tmp1, base_param, stack);
516 
517  fp_elt_sqr(tmp, src->v1, base_param, stack); /* tmp <- v1^2 */
518  fp_elt_mul(tmp, tmp, param->non_residue, base_param, stack); /* tmp <- v1^2 * d */
519  fp_elt_sqr(tmp1, src->v0, base_param, stack); /* tmp1 <- v0^2 */
520  fp_elt_sub(tmp1, tmp1, tmp, base_param); /* tmp1 <- v0^2 - (v1^2 * d) */
521  fp_elt_inv(tmp, tmp1, base_param, stack); /* tmp = tmp1^(-1) */
522 
523  fp_elt_mul(dst->v0, src->v0, tmp, base_param, stack); /* v0 <- v0 * tmp */
524  fp_elt_mul(dst->v1, src->v1, tmp, base_param, stack); /* v1 <- v1 * tmp */
525  fp_elt_neg(dst->v1, dst->v1, base_param); /* v1 <- -v1 * tmp */
526 
527  fp_elt_relax_pool_elt(&tmp, base_param, stack);
528  fp_elt_relax_pool_elt(&tmp1, base_param, stack);
529 }
530 
531 void
532 fp2_elt_div_fp_elt (fp2_elt_ptr dst, fp2_elt_srcptr src1, fp_elt_srcptr src2,
533  const fp2_param param, uint8_t stack)
534 {
535  fp2_elt tmp;
536  fp2_elt_get_pool_elt(&tmp, param, stack);
537  fp2_elt_inv_fp_elt(&tmp, src2, param, stack);
538  fp2_elt_mul(dst, src1, &tmp, param, stack);
539  fp2_elt_relax_pool_elt(&tmp, param, stack);
540 }
541 
542 void
544  const fp2_param param, uint8_t stack)
545 {
546  fp2_elt tmp;
547  fp2_elt_get_pool_elt(&tmp, param, stack);
548  fp2_elt_inv(&tmp, src2, param, stack);
549  fp2_elt_mul(dst, src1, &tmp, param, stack);
550  fp2_elt_relax_pool_elt(&tmp, param, stack);
551 }
552 
553 void
554 fp2_elt_pow_ui (fp2_elt_ptr dst, fp2_elt_srcptr src, const block n,
555  const fp2_param param, uint8_t stack)
556 {
557  fp2_elt y;
558  fp2_elt_get_pool_elt(&y, param, stack);
559  block m = n;
560 
561  fp2_elt_copy(&y, src, param);
562  fp2_elt_set_one(dst, param);
563 
564  while (m != (block)0)
565  {
566  if (m & (block)1)
567  {
568  fp2_elt_mul(dst, dst, &y, param, stack);
569  }
570  fp2_elt_sqr(&y, &y, param, stack);
571  m >>= 1;
572  }
573  fp2_elt_relax_pool_elt(&y, param, stack);
574 }
575 
576 void
577 fp2_elt_pow_number (fp2_elt_ptr dst, fp2_elt_srcptr src, number_srcptr n,
578  const fp2_param param, uint8_t stack)
579 {
580  number m;
581  fp2_elt y;
582  fp2_elt_get_pool_elt(&y, param, stack);
583 #if MPHELL_USE_GMP == 1
584  number_tmp_alloc(&m, SIZ(n), stack);
585 #elif MPHELL_USE_IPP == 1
586  int size;
587  ippsRef_BN(NULL, &size, NULL, n);
588  number_tmp_alloc(&m, size, stack);
589 #elif MPHELL_USE_MBEDTLS == 1
590  number_tmp_alloc(&m, n->n, stack);
591 #endif
592  number_copy(m, n);
593  fp2_elt_copy(&y, src, param);
594  fp2_elt_set_one(dst, param);
595 
596  while (number_isdiff_ui(m, 0))
597  {
598  if (number_and_ui(m, (block)1, stack))
599  {
600  fp2_elt_mul(dst, dst, &y, param, stack);
601  }
602  fp2_elt_sqr(&y, &y, param, stack);
603  number_rshift(m, m, 1);
604  }
605  fp2_elt_relax_pool_elt(&y, param, stack);
606 #if MPHELL_USE_GMP == 1
607  number_tmp_free(&m, SIZ(n), stack);
608 #elif MPHELL_USE_IPP == 1
609  number_tmp_free(&m, size, stack);
610 #elif MPHELL_USE_MBEDTLS == 1
611  number_tmp_free(&m, n->n, stack);
612 #endif
613 }
614 
615 bool
616 fp2_elt_issquare (fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
617 {
618  fp2_elt y;
619  number m;
620  fp2_elt_get_pool_elt(&y, param, stack);
621  number_tmp_alloc(&m, (param->size)*2, stack);
622 
623  number_dec(m, param->q);
624  number_div_ui(m, m, (block)2);
625 
626  fp2_elt_pow_number(&y, src, m, param, stack);
627  bool res = fp2_elt_isone(&y, param);
628  fp2_elt_relax_pool_elt(&y, param, stack);
629  number_tmp_free(&m, (param->size)*2, stack);
630  return res;
631 }
632 
633 int8_t
634 fp2_elt_ispower_ui (fp2_elt_srcptr src, const block n, const fp2_param param, uint8_t stack)
635 {
636  if(n == 0)
637  {
638  return (fp2_elt_isone(src, param));
639  }
640 
641  if(n == 1)
642  {
643  return true;
644  }
645 
646  if(n == 2)
647  {
648  return (fp2_elt_issquare(src, param, stack));
649  }
650 
651  fp2_elt y;
652  number m;
653  block r;
654  fp2_elt_get_pool_elt(&y, param, stack);
655  number_tmp_alloc(&m, (param->size)*2, stack);
656 
657  number_dec(m, param->q);
658  number_divmod_ui(m, &r, m, n);
659 
660  if (r != (block)0)
661  {
662  fp2_elt_relax_pool_elt(&y, param, stack);
663  number_tmp_free(&m, (param->size)*2, stack);
664  return 2;
665  }
666 
667  fp2_elt_pow_number(&y, src, m, param, stack);
668  bool res = fp2_elt_isone(&y, param);
669  fp2_elt_relax_pool_elt(&y, param, stack);
670  number_tmp_free(&m, (param->size)*2, stack);
671  return res;
672 }
673 
674 int8_t
676  const fp2_param param, uint8_t stack)
677 {
678  if(number_iszero(n))
679  {
680  return (fp2_elt_isone(src, param));
681  }
682 
683  if(number_cmp_ui(n , 1) == 0)
684  {
685  return true;
686  }
687 
688  if(number_cmp_ui(n , 2) == 0)
689  {
690  return (fp2_elt_issquare(src, param, stack));
691  }
692 
693  fp2_elt y;
694  number m, r;
695 
696  fp2_elt_get_pool_elt(&y, param, stack);
697  number_tmp_alloc(&m, (param->size)*2, stack);
698 
699 #if MPHELL_USE_GMP == 1
700  number_tmp_alloc(&r, SIZ(n), stack);
701 #elif MPHELL_USE_IPP == 1
702  int size;
703  ippsRef_BN(NULL, &size, NULL, n);
704  number_tmp_alloc(&r, size, stack);
705 #elif MPHELL_USE_MBEDTLS == 1
706  number_tmp_alloc(&r, n->n, stack);
707 #endif
708 
709  number_dec(m, param->q);
710  number_divmod(m, r, m, n);
711 
712  if (!number_iszero(r))
713  {
714  fp2_elt_relax_pool_elt(&y, param, stack);
715  number_tmp_free(&m, (param->size)*2, stack);
716 #if MPHELL_USE_GMP == 1
717  number_tmp_free(&r, SIZ(n), stack);
718 #elif MPHELL_USE_IPP == 1
719  number_tmp_free(&r, size, stack);
720 #elif MPHELL_USE_MBEDTLS == 1
721  number_tmp_free(&r, n->n, stack);
722 #endif
723  return 2;
724  }
725 
726  fp2_elt_pow_number(&y, src, m, param, stack);
727  bool res = fp2_elt_isone(&y, param);
728  fp2_elt_relax_pool_elt(&y, param, stack);
729  number_tmp_free(&m, (param->size)*2, stack);
730 #if MPHELL_USE_GMP == 1
731  number_tmp_free(&r, SIZ(n), stack);
732 #elif MPHELL_USE_IPP == 1
733  number_tmp_free(&r, size, stack);
734 #elif MPHELL_USE_MBEDTLS == 1
735  number_tmp_free(&r, n->n, stack);
736 #endif
737  return res;
738 
739 }
740 
741 void
742 fp2_elt_sqrt (fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
743 {
744  fp2_elt r, b, x ;
745  if(fp2_elt_iszero(src, param))
746  {
747  fp2_elt_set_ui(dst, 0, true, param, stack);
748  }
749 
750  fp2_elt_get_pool_elt(&r, param, stack);
751  fp2_elt_get_pool_elt(&b, param, stack);
752  fp2_elt_get_pool_elt(&x, param, stack);
753 
754  number q;
755 #if MPHELL_USE_GMP == 1
756  number_tmp_alloc(&q, SIZ(param->p_odd)+1, stack);
757 #elif MPHELL_USE_IPP == 1
758  int size;
759  ippsRef_BN(NULL, &size, NULL, param->p_odd);
760  number_tmp_alloc(&q, size+1, stack);
761 #elif MPHELL_USE_MBEDTLS == 1
762  number_tmp_alloc(&q, (param->p_odd->n)+1, stack);
763 #endif
764 
765  number_inc(q, param->p_odd);
766  number_rshift(q, q, 1);
767 
768  fp2_elt_pow_number(&x, src, q, param, stack);
769 
770  while (1)
771  {
772  fp2_elt_sqr(&r, &x, param, stack);
773  fp2_elt_div(&b, &r, src, param, stack);
774 
775  if (fp2_elt_isone(&b, param))
776  {
777  fp2_elt_neg(dst, &x, param);
778  fp2_elt_relax_pool_elt(&r, param, stack);
779  fp2_elt_relax_pool_elt(&b, param, stack);
780 #if MPHELL_USE_GMP == 1
781  number_tmp_free(&q, SIZ(param->p_odd)+1, stack);
782 #elif MPHELL_USE_IPP == 1
783  number_tmp_free(&q, size+1, stack);
784 #elif MPHELL_USE_MBEDTLS == 1
785  number_tmp_free(&q, (param->p_odd->n)+1, stack);
786 #endif
787  fp2_elt_relax_pool_elt(&x, param, stack);
788  return;
789  }
790  uint32_t m = 0;
791  do {
792  fp2_elt_sqr(&b, &b, param, stack);
793  ++m;
794  MPHELL_ASSERT_ALWAYS(m <= param->p_even, "fp_elt_sqrt : \
795  m > p_even");
796  } while (!fp2_elt_isone(&b, param));
797  fp2_elt_copy(&r, &(param->gen_sylow), param);
798  while (m < param->p_even - 1)
799  {
800  fp2_elt_sqr(&r, &r, param, stack);
801  ++m;
802  }
803  fp2_elt_mul(&x, &x, &r, param, stack);
804  }
805 
806  fp2_elt_relax_pool_elt(&r, param, stack);
807  fp2_elt_relax_pool_elt(&b, param, stack);
808 #if MPHELL_USE_GMP == 1
809  number_tmp_free(&q, SIZ(param->p_odd)+1, stack);
810 #elif MPHELL_USE_IPP == 1
811  number_tmp_free(&q, size+1, stack);
812 #elif MPHELL_USE_MBEDTLS == 1
813  number_tmp_free(&q, (param->p_odd->n)+1, stack);
814 #endif
815  fp2_elt_relax_pool_elt(&x, param, stack);
816 }
817 
818 void
819 fp2_elt_cube_root (fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
820 {
821  number t;
822  number l;
823  uint32_t s = 0;
824  uint32_t s1 = 0;
825  uint32_t i;
826  uint8_t k;
827  block pow = (block)1;
828  block r0;
829  int8_t r1;
830  block gcd;
831  fp2_elt b, c1, c2, h, r, d, tmp1;
832  number_tmp_alloc(&t, param->size*2, stack);
833  number_tmp_alloc(&l, param->size*2, stack);
834  fp2_elt_get_pool_elt(&b, param, stack);
835  fp2_elt_get_pool_elt(&c1, param, stack);
836  fp2_elt_get_pool_elt(&c2, param, stack);
837  fp2_elt_get_pool_elt(&h, param, stack);
838  fp2_elt_get_pool_elt(&r, param, stack);
839  fp2_elt_get_pool_elt(&d, param, stack);
840  fp2_elt_get_pool_elt(&tmp1, param, stack);
841 
842  number_dec(t, param->q); /* t <- q-1 */
843  number_gcd_ui(&gcd, t, (block)3);
844 
845  /* Step 1: We write q-1 as t.3^s, with t = 3l +- 1 */
846  number_mod_ui(&r0, t, (block)3);
847  while(r0==0)
848  {
849  s++;
850  number_divmod_ui(t, &r0, t, (block)3);
851  number_mod_ui(&r0, t, (block)3);
852  }
853  number_divmod_ui(l, &r0, t, (block)3);
854 
855  if(r0 == 2)
856  {
857  number_inc(l, l);
858  r1=-1;
859  }
860  else
861  {
862  r1 = 1;
863  }
864 
865  /* Step 2: Find a non cubic residue */
866  do
867  {
868  fp2_elt_random(&b, param, stack);
869  }
870  while(fp2_elt_ispower_ui(&b, (block)3, param, stack)!=0);
871 
872  /* Compute c1 and c2 */
873  fp2_elt_pow_number(&c1, &b, t, param, stack); /* c1 <- b^t */
874  s1 = s - 1;
875  while(s1 != 0)
876  {
877  pow*=3;
878  s1--;
879  }
880  fp2_elt_pow_ui(&c2, &c1, pow, param, stack); /* c2 <- c1^(3^(s-1)) */
881 
882  /* Step 3: Computation of the cube root of (a^t)^(-1) */
883  fp2_elt_set_one(&h, param); /* h <- 1 */
884  fp2_elt_pow_number(&r, src, t, param, stack); /* r = a^t = src^t */
885 
886  for(i=1; i<= (s-1); i++)
887  {
888  /* d <- r^(3^(s-i-1)) */
889  s1 = s-i-1;
890  pow = (block)1;
891  while(s1 != 0)
892  {
893  pow*=3;
894  s1--;
895  }
896  fp2_elt_pow_ui(&d, &r, pow, param, stack);
897  if(fp2_elt_isone(&d, param))
898  {
899  k = 0;
900  }
901  else if(fp2_elt_cmp(&d, &c2, param)==0)
902  {
903  k = 2;
904  }
905  else
906  {
907  k = 1;
908  }
909  fp2_elt_pow_ui(&tmp1, &c1, (block)k, param, stack); /* tmp1 <- c1^k */
910  fp2_elt_mul(&h, &h, &tmp1, param, stack); /* h <- h.c1^k */
911  fp2_elt_pow_ui(&tmp1, &c1, (block)(k*3), param, stack); /* tmp1 <- c1^3k */
912  fp2_elt_mul(&r, &r, &tmp1, param, stack); /* r <- r.(c1^3)^k */
913  fp2_elt_pow_ui(&c1, &c1, (block)3, param, stack); /* c1 <- c1^3 */
914  }
915 
916  /* Step 4: */
917 
918  fp2_elt_pow_number(&tmp1, src, l, param, stack); /* tmp1 <- a^l */
919  fp2_elt_mul(&r, &tmp1, &h, param, stack); /* r <- (a^l).h */
920 
921  if(r1 == 1)
922  {
923  fp2_elt_inv(dst, &r, param, stack);
924  }
925  else
926  {
927  fp2_elt_copy(dst, &r, param);
928  }
929 
930  number_tmp_free(&t, param->size*2, stack);
931  number_tmp_free(&l, param->size*2, stack);
932  fp2_elt_relax_pool_elt(&b, param, stack);
933  fp2_elt_relax_pool_elt(&c1, param, stack);
934  fp2_elt_relax_pool_elt(&c2, param, stack);
935  fp2_elt_relax_pool_elt(&h, param, stack);
936  fp2_elt_relax_pool_elt(&r, param, stack);
937  fp2_elt_relax_pool_elt(&d, param, stack);
938  fp2_elt_relax_pool_elt(&tmp1, param, stack);
939 }
940 
948 void
949 fp2_elt_primitive_elt (fp2_elt_ptr dst, const fp2_param param, uint8_t stack)
950 {
951  /* Find a possible primitive element */
952  fp2_elt a;
953  fp2_elt res;
954  fp2_elt_get_pool_elt(&a, param, stack);
955  fp2_elt_get_pool_elt(&res, param, stack);
956  number m;
957  number q;
958  number_tmp_alloc(&m, 2*(param->size), stack);
959  number_tmp_alloc(&q, 2*(param->size), stack);
960  bool test = true;
961  block r;
962  int8_t i = 0;
963 
964  number_dec(m, param->q); /* m <- q-1 */
965 
966  /* First 20 prime */
967  block prime[20]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71};
968 
969  do
970  {
971  /* Select a random element */
972  fp2_elt_random(&a, param, stack);
973 
974  /* Suppose that the element is primitive */
975  test = true;
976  i = 0;
977  while(i<20)
978  {
979  number_mod_ui(&r, m, prime[i]);
980  number_divmod_ui(q, &r, m, prime[i]);
981  if(r == 0)
982  {
983  /* prime[i] divides p-1 */
984  /* q = (q-1) / prime[i] */
985  fp2_elt_pow_number(&res, &a, q, param, stack);
986  if(fp2_elt_isone(&res, param))
987  {
988  /* The element is not primitive */
989  test = false;
990  break;
991  }
992  }
993  i++;
994  }
995  }
996  while(test == false);
997 
998  fp2_elt_copy(dst, &a, param);
999 
1000  fp2_elt_relax_pool_elt(&a, param, stack);
1001  fp2_elt_relax_pool_elt(&res, param, stack);
1002  number_tmp_free(&m, 2*(param->size), stack);
1003  number_tmp_free(&q, 2*(param->size), stack);
1004 }
1005 
1006 void
1007 fp2_elt_unity_nth_root (fp2_elt_ptr dst, const block n, const fp2_param param, uint8_t stack)
1008 {
1009  /* Find a possible primitive element */
1010  fp2_elt a;
1011  fp2_elt b;
1012  fp2_elt_get_pool_elt(&a, param, stack);
1013  fp2_elt_get_pool_elt(&b, param, stack);
1014  number m;
1015  number q;
1016  block r;
1017  number_tmp_alloc(&m, 2*(param->size), stack);
1018  number_tmp_alloc(&q, 2*(param->size), stack);
1019  number_set_ui(q, (block)0);
1020  number_set_ui(m, (block)0);
1021  number_dec(m, param->q); /* m <- q-1 */
1022  number_divmod_ui(q, &r, m, n); /* q <- (q-1)/n */
1023  if(r != 0)
1024  {
1025  /* There is no non-trivial nth root of unity */
1026  fp2_elt_set_one(dst, param);
1027  }
1028  else
1029  {
1030  /* There is a non-trivial nth root of unity */
1031  do
1032  {
1033  /* Set a to a strongly possible primitive element */
1034  fp2_elt_primitive_elt (&a, param, stack);
1035  /* Set b = a^((q-1)/n) */
1036  fp2_elt_pow_number(&b, &a, q, param, stack);
1037  }
1038  while(fp2_elt_isone(&b, param));
1039 
1040  fp2_elt_copy(dst, &b, param);
1041  }
1042 
1043  fp2_elt_relax_pool_elt(&a, param, stack);
1044  fp2_elt_relax_pool_elt(&b, param, stack);
1045  number_tmp_free(&m, 2*(param->size), stack);
1046  number_tmp_free(&q, 2*(param->size), stack);
1047 }
bool number_isdiff_ui(number_srcptr src1, const block src2)
Test if src1 != src2.
bool number_iszero(number_srcptr src)
Test if src is zero.
void fp2_elt_set_one(fp2_elt_ptr dst, const fp2_param param)
Set dst to one (or its Montgomery form if Montgomery arithmetic is used)
Definition: mphell-fp2.c:197
void fp2_elt_alloc(fp2_elt *dst, const fp2_param param)
Allocate space for a quadratic extension field element.
Definition: mphell-fp2.c:162
int8_t fp_elt_cmp(fp_elt_srcptr src1, fp_elt_srcptr src2, const fp_param param)
Compare src1 and src2 in Fp.
Definition: mphell-fp.c:631
static void fp_elt_add(fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2, const fp_param param)
Set dst <- src1 + src2.
Definition: mphell-fp.h:514
void fp2_elt_primitive_elt(fp2_elt_ptr dst, const fp2_param param, uint8_t stack)
Find a possible primitive element in the field defined by param.
Definition: mphell-fp2.c:949
void fp2_alloc(fp2_param param, const fp_param base)
Allocate space for the quadratic extension field informations structure.
Definition: mphell-fp2.c:67
void fp2_elt_sqrt(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
Set dst <- src^(1/2), using Tonelli–Shanks algorithm.
Definition: mphell-fp2.c:742
void fp2_elt_clear(fp2_elt *src)
Clear space used by src (remove the action of fp2_elt_init but less the one of fp2_elt_alloc)
Definition: mphell-fp2.c:183
void fp2_elt_copy(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Copy src into dst, src and dst must belong to the same FP2.
Definition: mphell-fp2.c:176
void fp2_elt_mul(fp2_elt_ptr dst, fp2_elt_srcptr src1, fp2_elt_srcptr src2, const fp2_param param, uint8_t stack)
Set dst <- src1 * src2, if Montgomery arithmetic is used, the Montgomery multiplication will be used ...
Definition: mphell-fp2.c:451
void fp2_elt_mul2(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Set dst <- 2 * src.
Definition: mphell-fp2.c:419
void number_mod_ui(block *dst, number_srcptr src1, const block src2)
Compute dst such that src1 = q * src2 + dst ; dst < src2.
void number_div_ui(number_ptr dst, number_srcptr src1, const block src2)
Compute dst such that src1 = dst * src2 + r ; r < src2.
void fp2_elt_lift(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
If Montgomery arithmetic is used, lift src (which is into Montgomery form) to classical FP2.
Definition: mphell-fp2.c:267
void fp_elt_set_ui(fp_elt_ptr dst, const block src, const bool isreduced, const fp_param param, uint8_t stack)
Set dst to src, if Montgomery arithmetic is used, is_reduced == false -> transform dst into its Montg...
Definition: mphell-fp.c:415
fp_param base
Definition: mphell-fp2.h:65
void fp2_elt_unity_nth_root(fp2_elt_ptr dst, const block n, const fp2_param param, uint8_t stack)
Set dst to a non trivial n-th root of unity if it exists (ie n divides q-1), 1 otherwise.
Definition: mphell-fp2.c:1007
void fp_elt_free(fp_elt *src)
Free space used by src.
Definition: mphell-fp.c:385
void fp2_elt_pow_ui(fp2_elt_ptr dst, fp2_elt_srcptr src, const block n, const fp2_param param, uint8_t stack)
Set dst <- src^n.
Definition: mphell-fp2.c:554
void fp2_create(fp2_param param, const fp_param base, fp_elt_srcptr non_residue, uint8_t stack)
Create a quadratic extension of the field base.
Definition: mphell-fp2.c:106
void fp2_free(fp2_param param)
Free the space of the quadratic extension field informations structure.
Definition: mphell-fp2.c:133
void fp_elt_clear(fp_elt *src)
Clear space used by src (remove the action of fp_elt_init but let the one of fp_elt_alloc)
Definition: mphell-fp.c:377
static void fp_elt_mul8(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- 8 * src.
Definition: mphell-fp.h:728
void number_set_ui(number_ptr dst, const block src)
Set dst to src.
bool number_iseven(number_srcptr src)
Test if src is even.
void fp2_elt_sqr(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
Set dst <- src^2.
Definition: mphell-fp2.c:495
void fp2_elt_mul3(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
Set dst <- 3 * src.
Definition: mphell-fp2.c:443
void fp2_elt_set_ui(fp2_elt_ptr dst, const uint64_t src, const bool isreduced, const fp2_param param, uint8_t stack)
Set dst to src, if Montgomery arithmetic is used, is_reduced == false -> transform dst into its Montg...
Definition: mphell-fp2.c:204
void fp2_elt_mul8(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Set dst <- 8 * src.
Definition: mphell-fp2.c:435
static void fp_elt_sqr(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
Set dst <- src^2.
Definition: mphell-fp.h:806
void fp2_elt_set(fp2_elt_ptr dst, number_srcptr src1, number_srcptr src2, const bool isreduced, const fp2_param param, uint8_t stack)
Set dst to src1 + src2*x, if Montgomery arithmetic is used, is_reduced == false -> transform dst into...
Definition: mphell-fp2.c:227
void fp2_elt_neg(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Set dst <- (-src)
Definition: mphell-fp2.c:402
static void fp_elt_relax_pool_elt(fp_elt *dst, const fp_param param, uint8_t stack)
Relax an initialised field element from the pool.
Definition: mphell-fp.h:195
void number_free(number *dst)
Free a number_ptr allocated on the RAM memory (malloc)
Definition: mphell-number.c:75
void fp2_elt_str(char **str, fp2_elt_srcptr src, const uint8_t base, const bool lift, const fp2_param param, uint8_t stack)
Converts src to string format in base specified by base.
Definition: mphell-fp2.c:294
void fp2_elt_sub_fp_elt(fp2_elt_ptr dst, fp2_elt_srcptr src1, fp_elt_srcptr src2, const fp2_param param)
Set dst <- src1 - src2.
Definition: mphell-fp2.c:379
void fp2_elt_inc(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Set dst <- src + 1.
Definition: mphell-fp2.c:349
void number_divmod_ui(number_ptr q, block *r, number_srcptr src1, const block src2)
Compute (q, r) such that src1 = q * src2 + r ; r < src2.
int8_t fp2_elt_ispower_ui(fp2_elt_srcptr src, const block n, const fp2_param param, uint8_t stack)
Test if src is a n-power in FP2, using A NOTE ON POWERS IN FINITE FIELDS from ANDREAS AABRANDT AND VA...
Definition: mphell-fp2.c:634
void fp2_elt_mul4(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Set dst <- 4 * src.
Definition: mphell-fp2.c:427
void fp2_copy(fp2_param param_res, const fp2_param param)
Copy the quadratic extension field structure param into param_res.
Definition: mphell-fp2.c:125
void fp2_elt_random(fp2_elt_ptr dst, const fp2_param param, uint8_t stack)
Set dst to a random element of FP2, the random process is chosen at the MHELL initialisation.
Definition: mphell-fp2.c:260
void fp2_elt_cube_root(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
Set dst <- src^(1/3)
Definition: mphell-fp2.c:819
number q
Definition: mphell-fp2.h:67
void fp_elt_copy(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Copy src into dst, src and dst must belong to the same Fp.
Definition: mphell-fp.c:367
fp2_elt gen_sylow
Definition: mphell-fp2.h:70
void fp_get_characteristic(number_ptr c, const fp_param param)
Get the characteristic of the prime field "param".
Definition: mphell-fp.c:332
bool fp2_elt_isone(fp2_elt_srcptr src, const fp2_param param)
Test if src is one.
Definition: mphell-fp2.c:335
void fp2_elt_inv(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
Set dst <- src^(-1)
Definition: mphell-fp2.c:508
void fp_str(char **str, const fp_param param, const uint8_t base, uint8_t stack)
Converts fp_param param to string format in base specified by base.
Definition: mphell-fp.c:543
void fp_elt_set_number(fp_elt_ptr dst, number_srcptr src, const bool isreduced, const fp_param param, uint8_t stack)
Set dst to src, if Montgomery arithmetic is used, is_reduced == false -> transform dst into its Montg...
Definition: mphell-fp.c:433
bool fp_elt_isone(fp_elt_srcptr src, const fp_param param)
Test if src is one.
Definition: mphell-fp.c:654
void number_gcd_ui(block *dst, number_srcptr src1, const block src2)
Set dst to GCD(src1, src2)
static void fp_elt_mul(fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2, const fp_param param, uint8_t stack)
Set dst <- src1 * src2, if Montgomery arithmetic is used, the Montgomery multiplication will be used ...
Definition: mphell-fp.h:643
static void fp_elt_inc(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- src + 1.
Definition: mphell-fp.h:539
static bool fp_elt_iszero(fp_elt_srcptr src, const fp_param param)
Test if src is zero.
Definition: mphell-fp.h:462
block number_and_ui(number_srcptr src1, const block src2, uint8_t stack)
Apply logical bitwise AND operator between src1 and src2.
static void fp_elt_get_pool_elt(fp_elt *dst, const fp_param param, uint8_t stack)
Get an initialised field element from the pool.
Definition: mphell-fp.h:158
static void fp_elt_mul3(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
Set dst <- 3 * src.
Definition: mphell-fp.h:768
static void fp_elt_mul2(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- 2 * src.
Definition: mphell-fp.h:665
void number_copy(number_ptr dst, number_srcptr src)
Copy src into dst.
Definition: mphell-number.c:87
Declaration of binary field (finite field constructed with an irreducible polynomial of degree 2) fun...
void fp2_elt_div(fp2_elt_ptr dst, fp2_elt_srcptr src1, fp2_elt_srcptr src2, const fp2_param param, uint8_t stack)
Set dst <- src1 / src2.
Definition: mphell-fp2.c:543
void number_sqr(number_ptr dst, number_srcptr src)
Set dst to src1^2.
bool fp2_elt_issquare(fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
Test if src is a square, using A NOTE ON POWERS IN FINITE FIELDS from ANDREAS AABRANDT AND VAGN LUNDS...
Definition: mphell-fp2.c:616
void fp_elt_alloc(fp_elt *dst, const fp_param param)
Allocate space for a primary field element.
Definition: mphell-fp.c:344
void fp_elt_inv(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
Set dst <- src^(-1)
Definition: mphell-fp.c:716
void fp2_str(char **str, const fp2_param param, const uint8_t base, uint8_t stack)
Converts fp2_param param to string format in base specified by base.
Definition: mphell-fp2.c:274
static void fp_elt_neg(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- (-src) mod p.
Definition: mphell-fp.h:613
void fp2_prepare_sqrt(fp2_param param, uint8_t stack)
Find a non square residue in FP2, factor out q-1 by powers of 2, find Q and S such that q − 1 = Q....
Definition: mphell-fp2.c:88
void fp2_elt_print(fp2_elt_srcptr src, const uint8_t base, const bool lift, const fp2_param param, uint8_t stack)
Print src in base "base".
Definition: mphell-fp2.c:40
void fp2_elt_relax_pool_elt(fp2_elt *dst, const fp2_param param, uint8_t stack)
Free space of a temporary quadratic extension field element.
Definition: mphell-fp2.c:58
void fp_elt_set_zero(fp_elt_ptr dst, const fp_param param)
Set dst to zero.
Definition: mphell-fp.c:404
void fp2_elt_sqr_fp_elt(fp2_elt_ptr dst, fp_elt_srcptr src, const fp2_param param, uint8_t stack)
Set dst <- src^2.
Definition: mphell-fp2.c:483
static void fp_elt_mul4(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- 4 * src.
Definition: mphell-fp.h:690
void fp2_elt_div_fp_elt(fp2_elt_ptr dst, fp2_elt_srcptr src1, fp_elt_srcptr src2, const fp2_param param, uint8_t stack)
Set dst <- src1 / src2.
Definition: mphell-fp2.c:532
void fp2_elt_mul_fp_elt(fp2_elt_ptr dst, fp2_elt_srcptr src1, fp_elt_srcptr src2, const fp2_param param, uint8_t stack)
Set dst <- src1 * src2, if Montgomery arithmetic is used, the Montgomery multiplication will be used ...
Definition: mphell-fp2.c:411
void number_tmp_free(number *t, const uint8_t size, uint8_t stack)
Free a temporary number.
Definition: mphell-number.c:45
void fp2_elt_init(fp2_elt_ptr dst, const fp2_param param)
Initialise a quadratic extension field element.
Definition: mphell-fp2.c:169
void number_dec(number_ptr dst, number_srcptr src)
Set dst to src - 1 if src - 1 fit in dst.
uint8_t size
Definition: mphell-fp2.h:73
void number_inc(number_ptr dst, number_srcptr src)
Set dst to src + 1 if src + 1 fit in dst.
Quadratic extension field element structure.
Definition: mphell-fp2.h:35
int8_t fp2_elt_cmp_fp_elt(fp2_elt_srcptr src1, fp_elt_srcptr src2, const fp2_param param)
Compare src1 and src2 in FP2.
Definition: mphell-fp2.c:311
void fp_elt_set_str(fp_elt_ptr dst, const char *str, const uint8_t base, const bool isreduced, const fp_param param, uint8_t stack)
Set dst to str, if Montgomery arithmetic is used, is_reduced == false -> transform dst into its Montg...
Definition: mphell-fp.c:455
uint32_t p_even
Definition: mphell-fp2.h:72
fp_elt non_residue
Definition: mphell-fp2.h:66
void fp2_elt_set_number(fp2_elt_ptr dst, number_srcptr src, const bool isreduced, const fp2_param param, uint8_t stack)
Set dst to src, if Montgomery arithmetic is used, is_reduced == false -> transform dst into its Montg...
Definition: mphell-fp2.c:212
void number_tmp_alloc(number *t, const uint8_t size, uint8_t stack)
Allocate a temporary number.
Definition: mphell-number.c:31
void number_init(number *dst, const uint8_t n)
Allocate a number_ptr on the RAM memory (malloc)
Definition: mphell-number.c:59
void fp2_elt_pow_number(fp2_elt_ptr dst, fp2_elt_srcptr src, number_srcptr n, const fp2_param param, uint8_t stack)
Set dst <- src^n.
Definition: mphell-fp2.c:577
void fp2_elt_add_fp_elt(fp2_elt_ptr dst, fp2_elt_srcptr src1, fp_elt_srcptr src2, const fp2_param param)
Set dst <- src1 + src2.
Definition: mphell-fp2.c:356
static void fp_elt_dec(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- src - 1.
Definition: mphell-fp.h:596
void fp2_elt_sub(fp2_elt_ptr dst, fp2_elt_srcptr src1, fp2_elt_srcptr src2, const fp2_param param)
Set dst <- src1 - src2.
Definition: mphell-fp2.c:387
int8_t fp2_elt_ispower_number(fp2_elt_srcptr src, number_srcptr n, const fp2_param param, uint8_t stack)
Test if src is a n-power in FP2, using A NOTE ON POWERS IN FINITE FIELDS from ANDREAS AABRANDT AND VA...
Definition: mphell-fp2.c:675
void fp2_elt_inv_fp_elt(fp2_elt_ptr dst, fp_elt_srcptr src, const fp2_param param, uint8_t stack)
Set dst <- src^(-1)
Definition: mphell-fp2.c:501
void fp_elt_lift(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
If Montgomery arithmetic is used, lift src (which is into Montgomery form) to classical fp.
Definition: mphell-fp.c:503
void fp2_elt_neg_fp_elt(fp2_elt_ptr dst, fp_elt_srcptr src, const fp2_param param)
Set dst <- (-src)
Definition: mphell-fp2.c:395
fp2_elt non_res
Definition: mphell-fp2.h:69
void number_rshift(number_ptr dst, number_srcptr src, const uint16_t shift)
Set dst to src >> shift.
Primary field parameters.
static void fp_elt_sub(fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2, const fp_param param)
Set dst <- src1 - src2.
Definition: mphell-fp.h:557
void fp2_elt_dec(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Set dst <- src - 1.
Definition: mphell-fp2.c:372
void fp2_elt_set_str(fp2_elt_ptr dst, const char *str, const uint8_t base, const bool isreduced, const fp2_param param, uint8_t stack)
Set dst to str, if Montgomery arithmetic is used, is_reduced == false -> transform dst into its Montg...
Definition: mphell-fp2.c:242
bool fp2_elt_iszero(fp2_elt_srcptr src, const fp2_param param)
Test if src is zero.
Definition: mphell-fp2.c:341
number p_odd
Definition: mphell-fp2.h:71
void fp2_elt_set_fp_elts(fp2_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2, const fp2_param param)
Set dst to src1 + src2*x.
Definition: mphell-fp2.c:235
void fp_elt_random(fp_elt_ptr dst, const fp_param param, uint8_t stack)
Set dst to a random element of Fp, the random process is chosen at the MHELL initialisation.
Definition: mphell-fp.c:481
int8_t number_cmp_ui(number_srcptr src1, const block src2)
Compare src1 and src2.
void number_divmod(number_ptr q, number_ptr r, number_srcptr src1, number_srcptr src2)
Compute (q, r) such that src1 = q * src2 + r ; r < src2.
fp_elt v1
Definition: mphell-fp2.h:38
void fp2_elt_add(fp2_elt_ptr dst, fp2_elt_srcptr src1, fp2_elt_srcptr src2, const fp2_param param)
Set dst <- src1 + src2.
Definition: mphell-fp2.c:364
void fp2_get_characteristic(number_ptr c, const fp2_param param)
Get the characteristic of the quadratic extension field "param".
Definition: mphell-fp2.c:144
int8_t fp2_elt_cmp(fp2_elt_srcptr src1, fp2_elt_srcptr src2, const fp2_param param)
Compare src1 and src2 in FP2.
Definition: mphell-fp2.c:321
void fp_elt_set_one(fp_elt_ptr dst, const fp_param param)
Set dst to one (or its Montgomery form if Montgomery arithmetic is used)
Definition: mphell-fp.c:394
void fp2_get_size(number_ptr c, const fp2_param param)
Get the size of the quadratic extension field "param".
Definition: mphell-fp2.c:150
void fp2_elt_get_pool_elt(fp2_elt *dst, const fp2_param param, uint8_t stack)
Allocate and initialise space for a temporary quadratic extension field element.
Definition: mphell-fp2.c:51
void fp_elt_str(char **str, fp_elt_srcptr src, const uint8_t base, const bool lift, const fp_param param, uint8_t stack)
Converts src to string format in base specified by base.
Definition: mphell-fp.c:600
Quadratic extension field structure.
Definition: mphell-fp2.h:63
void number_str(char **str, number_srcptr src, const uint8_t base)
Converts src to string format in base specified by base.
fp_elt v0
Definition: mphell-fp2.h:37
void fp2_elt_set_fp_elt(fp2_elt_ptr dst, fp_elt_srcptr src, const fp2_param param)
Set dst to src.
Definition: mphell-fp2.c:220
void fp_elt_init(fp_elt_ptr dst, const fp_param param)
Initialise a primary field element.
Definition: mphell-fp.c:357
void fp2_elt_free(fp2_elt *src)
Free space used by src.
Definition: mphell-fp2.c:190