MPHELL  4.0.0
mphell-fp.h
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 #ifndef MPHELL_FP_H
27 #define MPHELL_FP_H
28 
29 #include "mphell-number.h"
30 
31 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
32 
36  typedef number * fp_elt;
37  typedef number fp_elt_t;
42  typedef number * fp_elt_ptr;
43 
48  typedef const number * fp_elt_srcptr;
49 #elif MPHELL_USE_IPP == 1
50 
54  typedef IppsGFpElement * fp_elt;
55  typedef IppsGFpElement fp_elt_t;
60  typedef IppsGFpElement * fp_elt_ptr;
61 
66  typedef const IppsGFpElement * fp_elt_srcptr;
67 #endif
68 
73 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
74 typedef struct
75 {
76  number p;
77  number pm[7];
78  /*char * pm2; !< Binary representation of p-2 (Fermat little theorem) */
79 #if MPHELL_USE_MONTGOMERY == 1
80  number R2;
81 #if BLOCK_SIZE == 64
82  uint64_t invp;
83 #else
84  uint32_t invp;
85 #endif
86 #endif
87  fp_elt one;
88  fp_elt non_residue;
89  number p_odd;
90  fp_elt gen_2sylow;
91  uint32_t p_even;
92  uint8_t size;
93  fp_elt pool_1[POOL_SIZE_FP];
94  uint8_t i_1;
95 #if MPHELL_USE_MULTITHREADING == 1
96  fp_elt pool_2[POOL_SIZE_FP];
97  uint8_t i_2;
98 #endif
99 }
100 fp_param_t;
101 #elif MPHELL_USE_IPP == 1
102 typedef struct
103 {
104  IppsGFpState * gf;
105  uint8_t size;
106  number p;
107  number pm[7];
108  /*char * pm2; !< Binary representation of p-2 (Fermat little theorem) */
109  fp_elt one;
110  fp_elt pool_1[POOL_SIZE_FP];
111  uint8_t i_1;
112 #if MPHELL_USE_MULTITHREADING == 1
113  fp_elt pool_2[POOL_SIZE_FP];
114  uint8_t i_2;
115 #endif
116 }
117 fp_param_t;
118 #endif
119 
125 
130 enum fp_id_e {
137 };
138 
143 typedef enum fp_id_e fp_id;
144 
145 void
146 fp_elt_print (fp_elt_srcptr src, const uint8_t base, const bool lift, const fp_param param, uint8_t stack);
147 
148 /**************************************TMP************************************/
149 
157 static inline void
158 fp_elt_get_pool_elt (fp_elt * dst, const fp_param param, uint8_t stack)
159 {
160 #if MPHELL_USE_MULTITHREADING == 0
161  MPHELL_ASSERT(stack == STACK_1, "fp_elt_get_pool_elt, unknow stack \n");
162  MPHELL_ASSERT(param->i_1 < POOL_SIZE_FP, "fp_elt_get_pool_elt, stack is too small \n");
163  *dst = (param->pool_1)[(param->i_1)++];
164 #elif MPHELL_USE_MULTITHREADING == 1
165  if(stack == STACK_1)
166  {
167  MPHELL_ASSERT(param->i_1 < POOL_SIZE_FP, "fp_elt_get_pool_elt, stack is too small \n");
168  *dst = (param->pool_1)[(param->i_1)++];
169 #if MPHELL_USE_MBEDTLS == 1
170 #endif
171  }
172  else if (stack == STACK_2)
173  {
174  MPHELL_ASSERT(param->i_2 < POOL_SIZE_FP, "fp_elt_get_pool_elt, stack is too small \n");
175  *dst = (param->pool_2)[(param->i_2)++];
176 #if MPHELL_USE_MBEDTLS == 1
177 #endif
178  }
179  else
180  {
181  *dst = 0;
182  mphell_error("fp_elt_get_pool_elt, unknow stack \n");
183  }
184 #endif
185 }
186 
194 static inline void
195 fp_elt_relax_pool_elt (fp_elt * dst, const fp_param param, uint8_t stack)
196 {
197 #if MPHELL_USE_MULTITHREADING == 0
198  MPHELL_ASSERT(stack == STACK_1, "fp_elt_relax_pool_elt, unknow stack \n");
199  (param->i_1)--;
200  MPHELL_ASSERT(param->i_1 >= 0, "param->i_1 is < 0 in pool 1\n");
201 #elif MPHELL_USE_MULTITHREADING == 1
202  if(stack == STACK_1)
203  {
204  (param->i_1)--;
205  MPHELL_ASSERT(param->i_1 >= 0, "param->i_1 is < 0 in pool 1\n");
206  }
207  else if (stack == STACK_2)
208  {
209  (param->i_2)--;
210  MPHELL_ASSERT(param->i_2 >= 0, "param->i_2 is < 0 in pool 2\n");
211  }
212  else
213  {
214  mphell_error("fp_elt_relax_pool_elt, unknow stack \n");
215  }
216 #endif
217 }
218 
219 /************************************SETTERS**********************************/
220 
227 void
228 fp_alloc (fp_param param, const uint8_t size);
229 
238 void
239 fp_create (fp_param param, number_srcptr p, fp_id id, uint8_t stack);
240 
247 void
248 fp_copy (fp_param param_res, const fp_param param);
249 
255 void
256 fp_free (fp_param param);
257 
264 void
265 fp_get_characteristic (number_ptr c, const fp_param param);
266 
267 /*void*/
268 /*fp_get_pm2 (char * str, const fp_param param);*/
269 
276 void
277 fp_elt_alloc (fp_elt * dst, const fp_param param);
278 
285 void
286 fp_elt_init (fp_elt_ptr dst, const fp_param param);
287 
295 void
296 fp_elt_copy (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param);
297 
303 void
304 fp_elt_clear (fp_elt * src);
305 
311 void
312 fp_elt_free (fp_elt * src);
313 
320 void
321 fp_elt_set_one (fp_elt_ptr dst, const fp_param param);
322 
329 void
330 fp_elt_set_zero (fp_elt_ptr dst, const fp_param param);
331 
341 void
342 fp_elt_set_ui (fp_elt_ptr dst, const block src, const bool isreduced,
343  const fp_param param, uint8_t stack);
344 
354 void
355 fp_elt_set_number (fp_elt_ptr dst, number_srcptr src, const bool isreduced,
356  const fp_param param, uint8_t stack);
357 
368 void
369 fp_elt_set_str (fp_elt_ptr dst, const char *str, const uint8_t base,
370  const bool isreduced, const fp_param param, uint8_t stack);
371 
379 void
380 fp_elt_random (fp_elt_ptr dst, const fp_param param, uint8_t stack);
381 
390 void
391 fp_elt_lift (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack);
392 
401 void
402 fp_elt_get_number (number_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack);
403 
412 void
413 fp_str (char **str, const fp_param param, const uint8_t base, uint8_t stack);
414 
425 void
426 fp_elt_str (char **str, fp_elt_srcptr src, const uint8_t base,
427  const bool lift, const fp_param param, uint8_t stack);
428 
429 /*************************COMPARISON AND LOGICAL******************************/
430 
441 int8_t
442 fp_elt_cmp (fp_elt_srcptr src1, fp_elt_srcptr src2, const fp_param param);
443 
451 bool
452 fp_elt_isone (fp_elt_srcptr src, const fp_param param);
453 
461 static inline bool
462 fp_elt_iszero (fp_elt_srcptr src, const fp_param param)
463 {
464 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
465  return number_iszero(*src);
466 #elif MPHELL_USE_IPP == 1
467  int res;
468  ippsGFpIsZeroElement(src, &res, param->gf);
469  return (res == IPP_IS_EQ);
470 #endif
471 }
472 
473 /***************************ADDITION SUBTRACTION******************************/
474 
475 #if MPHELL_USE_MBEDTLS == 1
476 static inline void mpi_sub_hlp_mphell( size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d )
477 {
478  size_t i;
479  mbedtls_mpi_uint c, z;
480 
481  for( i = c = 0; i < n; i++, s++, d++ )
482  {
483  z = ( *d < c ); *d -= c;
484  c = ( *d < *s ) + z; *d -= *s;
485  }
486 
487  while( c != 0 )
488  {
489  z = ( *d < c ); *d -= c;
490  c = z; d++;
491  }
492 }
493 
494 static inline bool mpi_mod_add_mphell(number_ptr dst, number_srcptr mod)
495 {
496  if( mbedtls_mpi_cmp_abs(dst, mod) >= 0 )
497  {
498  mpi_sub_hlp_mphell(mod->n, mod->p, dst->p );
499  return true;
500  }
501  return false;
502 }
503 #endif
504 
513 static inline void
514 fp_elt_add (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
515  const fp_param param)
516 {
517 #if MPHELL_USE_GMP == 1
518  number_add(*dst, *src1, *src2);
519  if(number_isgreatereq(*dst, param->p))
520  {
521  number_sub(*dst, *dst, param->p);
522  }
523 #elif MPHELL_USE_MBEDTLS == 1
524  mbedtls_mpi_add_abs(*dst, *src1, *src2);
525  mpi_mod_add_mphell(*dst, param->p);
526 #elif MPHELL_USE_IPP == 1
527  ippsGFpAdd(src1, src2, dst, param->gf);
528 #endif
529 }
530 
538 static inline void
539 fp_elt_inc (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
540 {
541 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
542  fp_elt_add(dst, src, param->one, param);
543 #elif MPHELL_USE_IPP == 1
544  ippsGFpAdd(src, param->one, dst, param->gf);
545 #endif
546 }
547 
556 static inline void
557 fp_elt_sub (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
558  const fp_param param)
559 {
560 #if MPHELL_USE_GMP == 1
561  number_sub(*dst, *src1, *src2);
562  if(number_islower_ui(*dst, 0))
563  {
564  number_add(*dst, *dst, param->p);
565  }
566 #elif MPHELL_USE_MBEDTLS == 1
567  if( mbedtls_mpi_cmp_abs( *src1, *src2 ) >= 0 )
568  {
569  mbedtls_mpi_sub_abs( *dst, *src1, *src2 );
570  }
571  else
572  {
573  mbedtls_mpi B;
574  mbedtls_mpi_uint p[(*src2)->n];
575  B.s = 1;
576  B.n = (*src2)->n;
577  B.p = p;
578  mbedtls_mpi_copy(&B, *src2);
579  mpi_sub_hlp_mphell((*src1)->n, (*src1)->p, B.p);
580  mbedtls_mpi_copy(*dst, param->p);
581  mpi_sub_hlp_mphell(B.n, B.p, (*dst)->p);
582  }
583 #elif MPHELL_USE_IPP == 1
584  ippsGFpSub(src1, src2, dst, param->gf);
585 #endif
586 }
587 
595 static inline void
596 fp_elt_dec (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
597 {
598 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
599  fp_elt_sub(dst, src, param->one, param);
600 #elif MPHELL_USE_IPP == 1
601  ippsGFpSub(src, param->one, dst, param->gf);
602 #endif
603 }
604 
612 static inline void
613 fp_elt_neg (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
614 {
615 #if MPHELL_USE_GMP == 1
616  mpz_neg(*dst, *src);
617  if(number_islower_ui(*dst, 0))
618  {
619  number_add(*dst, *dst, param->p);
620  }
621 #elif MPHELL_USE_IPP == 1
622  ippsGFpNeg(src, dst, param->gf);
623 #elif MPHELL_USE_MBEDTLS == 1
624  number_copy(*dst, *src);
625  (*dst)->s = -1;
626  number_add(*dst, *dst, param->p);
627 #endif
628 }
629 
630 
631 /*******************************MULTIPLICATION********************************/
632 
642 static inline void
643 fp_elt_mul (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
644  const fp_param param, uint8_t stack)
645 {
646 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
647 #if MPHELL_USE_MONTGOMERY == 1
648  number_mul_montgomery(*dst, *src1, *src2, param->p, param->invp, stack);
649 #else
650  number_mul_mod(*dst, *src1, *src2, param->p, stack);
651 #endif
652 #elif MPHELL_USE_IPP == 1
653  ippsGFpMul(src1, src2, dst, param->gf);
654 #endif
655 }
656 
664 static inline void
665 fp_elt_mul2 (fp_elt_ptr dst, fp_elt_srcptr src,
666  const fp_param param)
667 {
668 #if MPHELL_USE_GMP == 1
669  number_lshift(*dst, *src, 1);
670  if(number_isgreatereq(*dst, param->p))
671  {
672  number_sub(*dst, *dst, param->p);
673  }
674 #elif MPHELL_USE_MBEDTLS == 1
675  number_lshift(*dst, *src, 1);
676  mpi_mod_add_mphell(*dst, param->p);
677 #elif MPHELL_USE_IPP == 1
678  fp_elt_add(dst, src, src, param);
679 #endif
680 }
681 
689 static inline void
690 fp_elt_mul4 (fp_elt_ptr dst, fp_elt_srcptr src,
691  const fp_param param)
692 {
693 #if MPHELL_USE_GMP == 1
694  number_lshift(*dst, *src, 2);
695  int i;
696  for (i=2; i>=0; i--)
697  {
698  if(number_isgreatereq(*dst, param->pm[i]))
699  {
700  number_sub(*dst, *dst, param->pm[i]);
701  break;
702  }
703  }
704 #elif MPHELL_USE_MBEDTLS == 1
705  number_lshift(*dst, *src, 2);
706  int i;
707  for (i=2; i>=0; i--)
708  {
709  if(mpi_mod_add_mphell(*dst, param->pm[i]))
710  {
711  break;
712  }
713  }
714 #elif MPHELL_USE_IPP == 1
715  fp_elt_add(dst, src, src, param);
716  fp_elt_add(dst, dst, dst, param);
717 #endif
718 }
719 
727 static inline void
728 fp_elt_mul8 (fp_elt_ptr dst, fp_elt_srcptr src,
729  const fp_param param)
730 {
731 #if MPHELL_USE_GMP == 1
732  number_lshift(*dst, *src, 3);
733  int i;
734  for (i=6; i>=0; i--)
735  {
736  if(number_isgreatereq(*dst, param->pm[i]))
737  {
738  number_sub(*dst, *dst, param->pm[i]);
739  break;
740  }
741  }
742 #elif MPHELL_USE_MBEDTLS == 1
743  number_lshift(*dst, *src, 3);
744  int i;
745  for (i=6; i>=0; i--)
746  {
747  if(mpi_mod_add_mphell(*dst, param->pm[i]))
748  {
749  break;
750  }
751  }
752 #elif MPHELL_USE_IPP == 1
753  fp_elt_add(dst, src, src, param);
754  fp_elt_add(dst, dst, dst, param);
755  fp_elt_add(dst, dst, dst, param);
756 #endif
757 }
758 
767 static inline void
768 fp_elt_mul3 (fp_elt_ptr dst, fp_elt_srcptr src,
769  const fp_param param, uint8_t stack)
770 {
771 #if MPHELL_USE_GMP == 1
772  fp_elt tmp;
773  fp_elt_get_pool_elt(&tmp, param, stack);
774  number_lshift(*tmp, *src, 1);
775  if(number_isgreatereq(*tmp, param->p))
776  {
777  number_sub(*tmp, *tmp, param->p);
778  }
779  fp_elt_add(dst, tmp, src, param);
780  fp_elt_relax_pool_elt(&tmp, param, stack);
781 #elif MPHELL_USE_MBEDTLS == 1
782  fp_elt tmp;
783  fp_elt_get_pool_elt(&tmp, param, stack);
784  number_lshift(*tmp, *src, 1);
785  mpi_mod_add_mphell(*tmp, param->p);
786  fp_elt_add(dst, tmp, src, param);
787  fp_elt_relax_pool_elt(&tmp, param, stack);
788 #elif MPHELL_USE_IPP == 1
789  fp_elt tmp;
790  fp_elt_get_pool_elt(&tmp, param, stack);
791  fp_elt_add(tmp, src, src, param);
792  fp_elt_add(dst, tmp, src, param);
793  fp_elt_relax_pool_elt(&tmp, param, stack);
794 #endif
795 }
796 
805 static inline void
806 fp_elt_sqr (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
807 {
808 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
809  fp_elt_mul(dst, src, src, param, stack);
810 #elif MPHELL_USE_IPP == 1
811  ippsGFpSqr(src, dst, param->gf);
812 #endif
813 }
814 
823 void
824 fp_elt_inv_flt (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack);
825 
826 
835 void
836 fp_elt_inv (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack);
837 
847 void
848 fp_elt_div (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
849  const fp_param param, uint8_t stack);
850 
860 void
861 fp_elt_pow_ui (fp_elt_ptr dst, fp_elt_srcptr src, const block n,
862  const fp_param param, uint8_t stack);
863 
873 void
874 fp_elt_pow_number (fp_elt_ptr dst, fp_elt_srcptr src, number_srcptr n,
875  const fp_param param, uint8_t stack);
876 
885 bool
886 fp_elt_issquare (fp_elt_srcptr src, const fp_param param, uint8_t stack);
887 
899 int8_t
900 fp_elt_ispower_ui (fp_elt_srcptr src, const block n, const fp_param param, uint8_t stack);
901 
913 int8_t
914 fp_elt_ispower_number (fp_elt_srcptr src, number_srcptr n, const fp_param param, uint8_t stack);
915 
916 
917 /*******************************ROOTS********************************/
918 
927 void
928 fp_elt_sqrt (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack);
929 
938 void
939 fp_elt_cube_root (fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack);
940 
949 void
950 fp_elt_unity_nth_root (fp_elt_ptr dst, const block n, const fp_param param, uint8_t stack);
951 
952 #endif
953 
bool number_iszero(number_srcptr src)
Test if src is zero.
void fp_elt_alloc(fp_elt *dst, const fp_param param)
Allocate space for a primary field element.
Definition: mphell-fp.c:344
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 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:806
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:1257
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 mphell_error(char *expr)
Write in stderr, filename, line and expr, free mphell.
Definition: mphell-errors.c:45
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:740
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
enum fp_id_e fp_id
Identifier for known field, use by IPPCP to accelerate the field arithmetic.
Definition: mphell-fp.h:143
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.
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:909
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 fp_elt_set_zero(fp_elt_ptr dst, const fp_param param)
Set dst to zero.
Definition: mphell-fp.c:404
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.
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 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:1019
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 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:848
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:763
fp_id_e
Identifier for known field, use by IPPCP to accelerate the field arithmetic.
Definition: mphell-fp.h:130
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
void number_lshift(number_ptr dst, number_srcptr src, const uint16_t shift)
Set dst to src << shift.
void fp_free(fp_param param)
Free the space of the prime field informations structure.
Definition: mphell-fp.c:292
void fp_elt_init(fp_elt_ptr dst, const fp_param param)
Initialise a primary field element.
Definition: mphell-fp.c:357
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
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
static bool fp_elt_iszero(fp_elt_srcptr src, const fp_param param)
Test if src is zero.
Definition: mphell-fp.h:462
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
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
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_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
bool fp_elt_isone(fp_elt_srcptr src, const fp_param param)
Test if src is one.
Definition: mphell-fp.c:654
void number_sub(number_ptr dst, number_srcptr src1, number_srcptr src2)
Set dst to src1 - src2 if src1 - src2 fit in dst.
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
Declaration of arithmetic functions, interface to chose either GMP mpz or number as base type for ari...
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 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
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
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 number_mul_mod(number_ptr dst, number_srcptr src1, number_srcptr src2, number_srcptr mod, uint8_t stack)
Set dst to (src1 * src2) % mod.
void fp_elt_free(fp_elt *src)
Free space used by src.
Definition: mphell-fp.c:385
void number_add(number_ptr dst, number_srcptr src1, number_srcptr src2)
Set dst to src1 + src2 if src1 + src2 fit in dst.
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:956
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 fp_copy(fp_param param_res, const fp_param param)
Copy the prime field structure param into param_res.
Definition: mphell-fp.c:266
bool number_isgreatereq(number_srcptr src1, number_srcptr src2)
Test if src1 >= src2.
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:522
bool number_islower_ui(number_srcptr src1, const block src2)
Test if src1 < src2.
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
Primary field parameters.
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
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 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:126
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:865
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
fp_param_t * fp_param
Pointer on a primary field parameters structure.
Definition: mphell-fp.h:124
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
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_alloc(fp_param param, const uint8_t size)
Allocate space for the prime field informations structure.
Definition: mphell-fp.c:58