MPHELL  5.0.0
mphell-fp.h
Go to the documentation of this file.
1 /*
2  MPHELL-5.0
3  Author(s): The MPHELL team
4 
5  (C) Copyright 2015-2021 - 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 #ifndef MPHELL_FP_H
27 #define MPHELL_FP_H
28 
29 #include "mphell-number.h"
30 
31 #if MPHELL_USE_AMNS == 1
32 #include "mphell-amns.h"
33 /* AMNS_BLOCK_SIZE can be 64 or 32 bits */
34 #endif
35 
36 #if MPHELL_USE_AMNS == 1
41  typedef amns_elt * fp_elt;
42  typedef amns_elt fp_elt_t;
47  typedef amns_elt * fp_elt_ptr;
48 
53  typedef const amns_elt * fp_elt_srcptr;
54 
55 #elif (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
60  typedef number * fp_elt;
61  typedef number fp_elt_t;
66  typedef number * fp_elt_ptr;
67 
72  typedef const number * fp_elt_srcptr;
73 
74 #elif MPHELL_USE_IPP == 1
79  typedef IppsGFpElement * fp_elt;
80  typedef IppsGFpElement fp_elt_t;
85  typedef IppsGFpElement * fp_elt_ptr;
86 
91  typedef const IppsGFpElement * fp_elt_srcptr;
92 #endif
93 
98 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1 || MPHELL_USE_AMNS == 1)
99 typedef struct
100 {
101  number p;
102  number pm[7];
103  char * pm2;
104 #if MPHELL_USE_MONTGOMERY == 1
105  number R2;
106 #if BLOCK_SIZE == 64
107  uint64_t invp;
108 #else
109  uint32_t invp;
110 #endif
111 #endif
112  fp_elt one;
113  fp_elt one_mon;
114  fp_elt non_residue;
115  number p_odd;
116  fp_elt gen_2sylow;
117  uint32_t p_even;
118  uint8_t size;
119  fp_elt pool_1[POOL_SIZE_FP];
120  uint8_t i_1;
121 #if MPHELL_USE_MULTITHREADING == 1
122  fp_elt pool_2[POOL_SIZE_FP];
123  uint8_t i_2;
124 #endif
125 #if MPHELL_USE_AMNS == 1
126  amns_ptr AMNS;
127 #endif
128 }
129 fp_param_t;
130 #elif MPHELL_USE_IPP == 1
131 typedef struct
132 {
133  IppsGFpState * gf;
134  uint8_t size;
135  number p;
136  number pm[7];
137  fp_elt one;
138  fp_elt one_mon;
139  char * pm2;
140  fp_elt pool_1[POOL_SIZE_FP];
141  uint8_t i_1;
142 #if MPHELL_USE_MULTITHREADING == 1
143  fp_elt pool_2[POOL_SIZE_FP];
144  uint8_t i_2;
145 #endif
146 }
147 fp_param_t;
148 #endif
149 
155 
160 enum fp_id_e {
167 };
168 
173 typedef enum fp_id_e fp_id;
174 
175 void
176 fp_elt_print (fp_elt_srcptr src, const uint8_t base, const bool lift, const fp_param param, uint8_t stack);
177 
178 /**************************************TMP************************************/
179 
187 static inline void
188 fp_elt_get_pool_elt (fp_elt * dst, const fp_param param, uint8_t stack)
189 {
190 #if MPHELL_USE_MULTITHREADING == 0
191  MPHELL_ASSERT(stack == STACK_1, "fp_elt_get_pool_elt, unknow stack \n");
192  MPHELL_ASSERT(param->i_1 < POOL_SIZE_FP, "fp_elt_get_pool_elt, stack is too small \n");
193  *dst = (param->pool_1)[(param->i_1)++];
194 #elif MPHELL_USE_MULTITHREADING == 1
195  if(stack == STACK_1)
196  {
197  MPHELL_ASSERT(param->i_1 < POOL_SIZE_FP, "fp_elt_get_pool_elt, stack is too small \n");
198  *dst = (param->pool_1)[(param->i_1)++];
199  }
200  else if (stack == STACK_2)
201  {
202  MPHELL_ASSERT(param->i_2 < POOL_SIZE_FP, "fp_elt_get_pool_elt, stack is too small \n");
203  *dst = (param->pool_2)[(param->i_2)++];
204  }
205  else
206  {
207  *dst = 0;
208  mphell_error("fp_elt_get_pool_elt, unknow stack \n");
209  }
210 #endif
211 }
212 
220 static inline void
221 fp_elt_relax_pool_elt (fp_elt * dst, const fp_param param, uint8_t stack)
222 {
223 #if MPHELL_USE_MULTITHREADING == 0
224  MPHELL_ASSERT(stack == STACK_1, "fp_elt_relax_pool_elt, unknow stack \n");
225  (param->i_1)--;
226  MPHELL_ASSERT(param->i_1 >= 0, "param->i_1 is < 0 in pool 1\n");
227 #elif MPHELL_USE_MULTITHREADING == 1
228  if(stack == STACK_1)
229  {
230  (param->i_1)--;
231  MPHELL_ASSERT(param->i_1 >= 0, "param->i_1 is < 0 in pool 1\n");
232  }
233  else if (stack == STACK_2)
234  {
235  (param->i_2)--;
236  MPHELL_ASSERT(param->i_2 >= 0, "param->i_2 is < 0 in pool 2\n");
237  }
238  else
239  {
240  mphell_error("fp_elt_relax_pool_elt, unknow stack \n");
241  }
242 #endif
243 }
244 
245 /************************************SETTERS**********************************/
246 
253 void
254 fp_alloc (fp_param param, const uint8_t size);
255 
256 #if MPHELL_USE_AMNS == 1
263 void
264 fp_set_amns (fp_param param, amns_ptr AMNS);
265 #endif
266 
275 void
276 fp_create (fp_param param, number_srcptr p, fp_id id, uint8_t stack);
277 
284 void
285 fp_copy (fp_param param_res, const fp_param param);
286 
292 void
293 fp_free (fp_param param);
294 
301 void
302 fp_get_characteristic (number_ptr c, const fp_param param);
303 
310 void
311 fp_elt_alloc (fp_elt * dst, const fp_param param);
312 
319 void
320 fp_elt_init (fp_elt_ptr dst, const fp_param param);
321 
329 void
330 fp_elt_copy (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param);
331 
337 void
338 fp_elt_clear (fp_elt * src);
339 
345 void
346 fp_elt_free (fp_elt * src);
347 
354 void
355 fp_elt_set_one (fp_elt_ptr dst, const fp_param param);
356 
363 void
364 fp_elt_set_zero (fp_elt_ptr dst, const fp_param param);
365 
375 void
376 fp_elt_set_ui (fp_elt_ptr dst, const block src, const bool isreduced,
377  const fp_param param, uint8_t stack);
378 
388 void
389 fp_elt_set_number (fp_elt_ptr dst, number_srcptr src, const bool isreduced,
390  const fp_param param, uint8_t stack);
391 
402 void
403 fp_elt_set_str (fp_elt_ptr dst, const char *str, const uint8_t base,
404  const bool isreduced, const fp_param param, uint8_t stack);
405 
413 void
414 fp_elt_random (fp_elt_ptr dst, const fp_param param, uint8_t stack);
415 
424 void
425 fp_elt_lift (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack);
426 
435 void
436 fp_elt_get_number (number_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack);
437 
446 void
447 fp_str (char **str, const fp_param param, const uint8_t base, uint8_t stack);
448 
459 void
460 fp_elt_str (char **str, fp_elt_srcptr src, const uint8_t base,
461  const bool lift, const fp_param param, uint8_t stack);
462 
463 /*************************COMPARISON AND LOGICAL******************************/
464 
475 int8_t
476 fp_elt_cmp (fp_elt_srcptr src1, fp_elt_srcptr src2, const fp_param param);
477 
485 bool
486 fp_elt_isone (fp_elt_srcptr src, const fp_param param);
487 
495 static inline bool
496 fp_elt_iszero (fp_elt_srcptr src, const fp_param param)
497 {
498 #if MPHELL_USE_AMNS == 1
499  return amns_elt_is_zero(*src, param->AMNS);
500 #elif (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
501  return number_iszero(*src);
502 #elif MPHELL_USE_IPP == 1
503  int res;
504  ippsGFpIsZeroElement(src, &res, param->gf);
505  return (res == IPP_IS_EQ);
506 #endif
507 }
508 
509 /***************************ADDITION SUBTRACTION******************************/
510 
511 #if MPHELL_USE_MBEDTLS == 1
512 static inline void mpi_sub_hlp_mphell( size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d )
513 {
514  size_t i;
515  mbedtls_mpi_uint c, z;
516 
517  for( i = c = 0; i < n; i++, s++, d++ )
518  {
519  z = ( *d < c ); *d -= c;
520  c = ( *d < *s ) + z; *d -= *s;
521  }
522 
523  while( c != 0 )
524  {
525  z = ( *d < c ); *d -= c;
526  c = z; d++;
527  }
528 }
529 
530 static inline bool mpi_mod_add_mphell(number_ptr dst, number_srcptr mod)
531 {
532  if( mbedtls_mpi_cmp_abs(dst, mod) >= 0 )
533  {
534  mpi_sub_hlp_mphell(mod->n, mod->p, dst->p );
535  return true;
536  }
537  return false;
538 }
539 #endif
540 
549 static inline void
550 fp_elt_add (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
551  const fp_param param)
552 {
553 #if MPHELL_USE_AMNS == 1
554  amns_elt_add(*dst, *src1, *src2, param->AMNS);
555 #elif MPHELL_USE_GMP == 1
556  number_add(*dst, *src1, *src2);
557  if(number_isgreatereq(*dst, param->p))
558  {
559  number_sub(*dst, *dst, param->p);
560  }
561 #elif MPHELL_USE_MBEDTLS == 1
562  mbedtls_mpi_add_abs(*dst, *src1, *src2);
563  mpi_mod_add_mphell(*dst, param->p);
564 #elif MPHELL_USE_IPP == 1
565  ippsGFpAdd(src1, src2, dst, param->gf);
566 #endif
567 }
568 
576 static inline void
577 fp_elt_inc (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
578 {
579 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1 || MPHELL_USE_AMNS == 1)
580  fp_elt_add(dst, src, param->one_mon, param);
581 #elif MPHELL_USE_IPP == 1
582  ippsGFpAdd(src, param->one_mon, dst, param->gf);
583 #endif
584 }
585 
594 static inline void
595 fp_elt_sub (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
596  const fp_param param)
597 {
598 #if MPHELL_USE_AMNS == 1
599  amns_elt_sub(*dst, *src1, *src2, param->AMNS);
600 #elif MPHELL_USE_GMP == 1
601  number_sub(*dst, *src1, *src2);
602  if(number_islower_ui(*dst, 0))
603  {
604  number_add(*dst, *dst, param->p);
605  }
606 #elif MPHELL_USE_MBEDTLS == 1
607  if( mbedtls_mpi_cmp_abs( *src1, *src2 ) >= 0 )
608  {
609  mbedtls_mpi_sub_abs( *dst, *src1, *src2 );
610  }
611  else
612  {
613  mbedtls_mpi B;
614  mbedtls_mpi_uint p[(*src2)->n];
615  B.s = 1;
616  B.n = (*src2)->n;
617  B.p = p;
618  mbedtls_mpi_copy(&B, *src2);
619  mpi_sub_hlp_mphell((*src1)->n, (*src1)->p, B.p);
620  mbedtls_mpi_copy(*dst, param->p);
621  mpi_sub_hlp_mphell(B.n, B.p, (*dst)->p);
622  }
623 #elif MPHELL_USE_IPP == 1
624  ippsGFpSub(src1, src2, dst, param->gf);
625 #endif
626 }
627 
635 static inline void
636 fp_elt_dec (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
637 {
638 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1 || MPHELL_USE_AMNS == 1)
639  fp_elt_sub(dst, src, param->one_mon, param);
640 #elif MPHELL_USE_IPP == 1
641  ippsGFpSub(src, param->one_mon, dst, param->gf);
642 #endif
643 }
644 
652 static inline void
653 fp_elt_neg (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
654 {
655 #if MPHELL_USE_AMNS == 1
656  amns_elt_neg(*dst, *src, param->AMNS);
657 #elif MPHELL_USE_GMP == 1
658  mpz_neg(*dst, *src);
659  if(number_islower_ui(*dst, 0))
660  {
661  number_add(*dst, *dst, param->p);
662  }
663 #elif MPHELL_USE_IPP == 1
664  ippsGFpNeg(src, dst, param->gf);
665 #elif MPHELL_USE_MBEDTLS == 1
666  number_copy(*dst, *src);
667  (*dst)->s = -1;
668  number_add(*dst, *dst, param->p);
669 #endif
670 }
671 
672 
673 /*******************************MULTIPLICATION********************************/
674 
684 static inline void
685 fp_elt_mul (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
686  const fp_param param, uint8_t stack)
687 {
688 #if MPHELL_USE_AMNS == 1
689  amns_elt_mul(*dst, *src1, *src2, param->AMNS);
690 #elif (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
691 #if MPHELL_USE_MONTGOMERY == 1
692  number_mul_montgomery(*dst, *src1, *src2, param->p, param->invp, stack);
693 #else
694  number_mul_mod(*dst, *src1, *src2, param->p, stack);
695 #endif
696 #elif MPHELL_USE_IPP == 1
697  ippsGFpMul(src1, src2, dst, param->gf);
698 #endif
699 }
700 
708 static inline void
709 fp_elt_mul2 (fp_elt_ptr dst, fp_elt_srcptr src,
710  const fp_param param)
711 {
712 #if MPHELL_USE_AMNS == 1
713  fp_elt_add(dst, src, src, param);
714 #elif MPHELL_USE_GMP == 1
715  number_lshift(*dst, *src, 1);
716  if(number_isgreatereq(*dst, param->p))
717  {
718  number_sub(*dst, *dst, param->p);
719  }
720 #elif MPHELL_USE_MBEDTLS == 1
721  number_lshift(*dst, *src, 1);
722  mpi_mod_add_mphell(*dst, param->p);
723 #elif MPHELL_USE_IPP == 1
724  fp_elt_add(dst, src, src, param);
725 #endif
726 }
727 
735 static inline void
736 fp_elt_mul4 (fp_elt_ptr dst, fp_elt_srcptr src,
737  const fp_param param)
738 {
739 #if MPHELL_USE_AMNS == 1
740  fp_elt_add(dst, src, src, param);
741  fp_elt_add(dst, dst, dst, param);
742 #elif MPHELL_USE_GMP == 1
743  number_lshift(*dst, *src, 2);
744  int i;
745  for (i=2; i>=0; i--)
746  {
747  if(number_isgreatereq(*dst, param->pm[i]))
748  {
749  number_sub(*dst, *dst, param->pm[i]);
750  break;
751  }
752  }
753 #elif MPHELL_USE_MBEDTLS == 1
754  number_lshift(*dst, *src, 2);
755  int i;
756  for (i=2; i>=0; i--)
757  {
758  if(mpi_mod_add_mphell(*dst, param->pm[i]))
759  {
760  break;
761  }
762  }
763 #elif MPHELL_USE_IPP == 1
764  fp_elt_add(dst, src, src, param);
765  fp_elt_add(dst, dst, dst, param);
766 #endif
767 }
768 
776 static inline void
777 fp_elt_mul8 (fp_elt_ptr dst, fp_elt_srcptr src,
778  const fp_param param)
779 {
780 #if MPHELL_USE_AMNS == 1
781  fp_elt_add(dst, src, src, param);
782  fp_elt_add(dst, dst, dst, param);
783  fp_elt_add(dst, dst, dst, param);
784 #elif MPHELL_USE_GMP == 1
785  number_lshift(*dst, *src, 3);
786  int i;
787  for (i=6; i>=0; i--)
788  {
789  if(number_isgreatereq(*dst, param->pm[i]))
790  {
791  number_sub(*dst, *dst, param->pm[i]);
792  break;
793  }
794  }
795 #elif MPHELL_USE_MBEDTLS == 1
796  number_lshift(*dst, *src, 3);
797  int i;
798  for (i=6; i>=0; i--)
799  {
800  if(mpi_mod_add_mphell(*dst, param->pm[i]))
801  {
802  break;
803  }
804  }
805 #elif MPHELL_USE_IPP == 1
806  fp_elt_add(dst, src, src, param);
807  fp_elt_add(dst, dst, dst, param);
808  fp_elt_add(dst, dst, dst, param);
809 #endif
810 }
811 
820 static inline void
821 fp_elt_mul3 (fp_elt_ptr dst, fp_elt_srcptr src,
822  const fp_param param, uint8_t stack)
823 {
824 #if MPHELL_USE_AMNS == 1
825  fp_elt tmp;
826  fp_elt_get_pool_elt(&tmp, param, stack);
827  fp_elt_add(tmp, src, src, param);
828  fp_elt_add(dst, tmp, src, param);
829  fp_elt_relax_pool_elt(&tmp, param, stack);
830 #elif MPHELL_USE_GMP == 1
831  fp_elt tmp;
832  fp_elt_get_pool_elt(&tmp, param, stack);
833  number_lshift(*tmp, *src, 1);
834  if(number_isgreatereq(*tmp, param->p))
835  {
836  number_sub(*tmp, *tmp, param->p);
837  }
838  fp_elt_add(dst, tmp, src, param);
839  fp_elt_relax_pool_elt(&tmp, param, stack);
840 #elif MPHELL_USE_MBEDTLS == 1
841  fp_elt tmp;
842  fp_elt_get_pool_elt(&tmp, param, stack);
843  number_lshift(*tmp, *src, 1);
844  mpi_mod_add_mphell(*tmp, param->p);
845  fp_elt_add(dst, tmp, src, param);
846  fp_elt_relax_pool_elt(&tmp, param, stack);
847 #elif MPHELL_USE_IPP == 1
848  fp_elt tmp;
849  fp_elt_get_pool_elt(&tmp, param, stack);
850  fp_elt_add(tmp, src, src, param);
851  fp_elt_add(dst, tmp, src, param);
852  fp_elt_relax_pool_elt(&tmp, param, stack);
853 #endif
854 }
855 
864 static inline void
865 fp_elt_sqr (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
866 {
867 #if MPHELL_USE_AMNS == 1
868  amns_elt_sqr(*dst, *src, param->AMNS);
869 #elif (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
870  fp_elt_mul(dst, src, src, param, stack);
871 #elif MPHELL_USE_IPP == 1
872  ippsGFpSqr(src, dst, param->gf);
873 #endif
874 }
875 
884 void
885 fp_elt_inv_flt (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack);
886 
887 
896 void
897 fp_elt_inv (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack);
898 
908 void
909 fp_elt_div (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
910  const fp_param param, uint8_t stack);
911 
921 void
922 fp_elt_pow_ui (fp_elt_ptr dst, fp_elt_srcptr src, const block n,
923  const fp_param param, uint8_t stack);
924 
934 void
935 fp_elt_pow_number (fp_elt_ptr dst, fp_elt_srcptr src, number_srcptr n,
936  const fp_param param, uint8_t stack);
937 
946 bool
947 fp_elt_issquare (fp_elt_srcptr src, const fp_param param, uint8_t stack);
948 
960 int8_t
961 fp_elt_ispower_ui (fp_elt_srcptr src, const block n, const fp_param param, uint8_t stack);
962 
974 int8_t
975 fp_elt_ispower_number (fp_elt_srcptr src, number_srcptr n, const fp_param param, uint8_t stack);
976 
977 
978 /*******************************ROOTS********************************/
979 
988 void
989 fp_elt_sqrt (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack);
990 
999 void
1000 fp_elt_cube_root (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack);
1001 
1010 void
1011 fp_elt_unity_nth_root (fp_elt_ptr dst, const block n, const fp_param param, uint8_t stack);
1012 
1013 #endif
1014 
void amns_elt_sub(amns_elt_ptr dst, amns_elt_srcptr a, amns_elt_srcptr b, amns_srcptr AMNS)
Set dst to a - b.
Definition: mphell-amns.c:2507
void amns_elt_add(amns_elt_ptr dst, amns_elt_srcptr a, amns_elt_srcptr b, amns_srcptr AMNS)
Set dst to a + b.
Definition: mphell-amns.c:2354
void amns_elt_neg(amns_elt_ptr dst, amns_elt_srcptr a, amns_srcptr AMNS)
Set dst to -a.
Definition: mphell-amns.c:2427
bool amns_elt_is_zero(amns_elt_srcptr src, amns_srcptr AMNS)
Test if src is zero.
Definition: mphell-amns.c:2261
Declaration of Adapted Modular Number System (AMNS) functions.
void amns_elt_sqr(amns_elt_ptr dst, amns_elt_srcptr a, amns_srcptr AMNS)
Set dst to a^2.
void amns_elt_mul(amns_elt_ptr dst, amns_elt_srcptr a, amns_elt_srcptr b, amns_srcptr AMNS)
Set dst to a * b.
amns_block * amns_elt
An AMNS element is a polynomial, we stock the digits in a left to right representation (a_0 is the co...
Definition: mphell-amns.h:61
void mphell_error(char *expr)
Write in stderr, filename, line and expr, free mphell.
Definition: mphell-errors.c:45
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:595
void fp_elt_print(fp_elt_srcptr src, const uint8_t base, const bool lift, const fp_param param, uint8_t stack)
Print src in base "base".
Definition: mphell-fp.c:40
void fp_create(fp_param param, number_srcptr p, fp_id id, uint8_t stack)
Create a prime field of characteristic p.
Definition: mphell-fp.c:135
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:653
void fp_elt_inv_flt(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
Set dst <- src^(-1) using Fermat Little Theorem.
Definition: mphell-fp.c:743
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:441
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:518
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:385
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:485
void fp_get_characteristic(number_ptr c, const fp_param param)
Get the characteristic of the prime field "param".
Definition: mphell-fp.c:352
void fp_elt_unity_nth_root(fp_elt_ptr dst, const block n, const fp_param param, uint8_t stack)
Set dst to a non trivial n-th root of unity if it exists (ie n divides p-1), 1 otherwise.
Definition: mphell-fp.c:1318
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:821
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:581
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:577
static bool fp_elt_iszero(fp_elt_srcptr src, const fp_param param)
Test if src is zero.
Definition: mphell-fp.h:496
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:636
void fp_elt_alloc(fp_elt *dst, const fp_param param)
Allocate space for a primary field element.
Definition: mphell-fp.c:358
void fp_elt_free(fp_elt *src)
Free space used by src.
Definition: mphell-fp.c:405
void fp_elt_pow_ui(fp_elt_ptr dst, fp_elt_srcptr src, const block n, const fp_param param, uint8_t stack)
Set dst <- src^n.
Definition: mphell-fp.c:818
int8_t fp_elt_ispower_ui(fp_elt_srcptr src, const block n, const fp_param param, uint8_t stack)
Test if src is a n-power in Fp.
Definition: mphell-fp.c:927
bool fp_elt_issquare(fp_elt_srcptr src, const fp_param param, uint8_t stack)
Test if src is a square using the Lengendre symbol.
Definition: mphell-fp.c:903
int8_t fp_elt_ispower_number(fp_elt_srcptr src, number_srcptr n, const fp_param param, uint8_t stack)
Test if src is a n-power in Fp.
Definition: mphell-fp.c:971
void fp_elt_init(fp_elt_ptr dst, const fp_param param)
Initialise a primary field element.
Definition: mphell-fp.c:373
fp_param_t * fp_param
Pointer on a primary field parameters structure.
Definition: mphell-fp.h:154
void fp_alloc(fp_param param, const uint8_t size)
Allocate space for the prime field informations structure.
Definition: mphell-fp.c:58
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:675
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:550
enum fp_id_e fp_id
Identifier for known field, use by IPPCP to accelerate the field arithmetic.
Definition: mphell-fp.h:173
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:709
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:638
bool fp_elt_isone(fp_elt_srcptr src, const fp_param param)
Test if src is one.
Definition: mphell-fp.c:700
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:685
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:416
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:777
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:461
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:736
void fp_free(fp_param param)
Free the space of the prime field informations structure.
Definition: mphell-fp.c:311
void fp_elt_get_number(number_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 number (in FP...
Definition: mphell-fp.c:563
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:221
void fp_elt_set_zero(fp_elt_ptr dst, const fp_param param)
Set dst to zero.
Definition: mphell-fp.c:428
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:397
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:546
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:188
void fp_elt_div(fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2, const fp_param param, uint8_t stack)
Set dst <- src1 / src2.
Definition: mphell-fp.c:795
void fp_elt_sqrt(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
Set dst <- src^(1/2) mod p, using Tonelli–Shanks algorithm.
Definition: mphell-fp.c:1018
void fp_elt_pow_number(fp_elt_ptr dst, fp_elt_srcptr src, number_srcptr n, const fp_param param, uint8_t stack)
Set dst <- src^n.
Definition: mphell-fp.c:861
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:865
void fp_elt_cube_root(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
Set dst <- src^(1/3) mod p.
Definition: mphell-fp.c:1081
void fp_copy(fp_param param_res, const fp_param param)
Copy the prime field structure param into param_res.
Definition: mphell-fp.c:284
fp_id_e
Identifier for known field, use by IPPCP to accelerate the field arithmetic.
Definition: mphell-fp.h:160
@ P384R1
Definition: mphell-fp.h:165
@ P192R1
Definition: mphell-fp.h:162
@ ARBITRARY
Definition: mphell-fp.h:161
@ P224R1
Definition: mphell-fp.h:163
@ P521R1
Definition: mphell-fp.h:166
@ P256R1
Definition: mphell-fp.h:164
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:764
void number_copy(number_ptr dst, number_srcptr src)
Copy src into dst.
Definition: mphell-number.c:87
bool number_iszero(number_srcptr src)
Test if src is zero.
void number_lshift(number_ptr dst, number_srcptr src, const uint16_t shift)
Set dst to src << shift.
void number_mul_montgomery(number_ptr dst, number_srcptr src1, number_srcptr src2, number_srcptr p, const block invp, uint8_t stack)
Compute dst such that dst = (src1 * src2) mod(p) into the Montgomery form.
void number_sub(number_ptr dst, number_srcptr src1, number_srcptr src2)
Set dst to src1 - src2 if src1 - src2 fit in dst.
void number_add(number_ptr dst, number_srcptr src1, number_srcptr src2)
Set dst to src1 + src2 if src1 + src2 fit in dst.
void number_mul_mod(number_ptr dst, number_srcptr src1, number_srcptr src2, number_srcptr mod, uint8_t stack)
Set dst to (src1 * src2) % mod.
bool number_isgreatereq(number_srcptr src1, number_srcptr src2)
Test if src1 >= src2.
bool number_islower_ui(number_srcptr src1, const block src2)
Test if src1 < src2.
Declaration of arithmetic functions, interface to chose either GMP mpz or number as base type for ari...
Define a AMNS.
Definition: mphell-amns.h:81
Primary field parameters.