31 #if MPHELL_USE_AMNS == 1
36 #if MPHELL_USE_AMNS == 1
53 typedef const amns_elt * fp_elt_srcptr;
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;
72 typedef const number * fp_elt_srcptr;
74 #elif MPHELL_USE_IPP == 1
79 typedef IppsGFpElement * fp_elt;
80 typedef IppsGFpElement fp_elt_t;
85 typedef IppsGFpElement * fp_elt_ptr;
91 typedef const IppsGFpElement * fp_elt_srcptr;
98 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1 || MPHELL_USE_AMNS == 1)
104 #if MPHELL_USE_MONTGOMERY == 1
119 fp_elt pool_1[POOL_SIZE_FP];
121 #if MPHELL_USE_MULTITHREADING == 1
122 fp_elt pool_2[POOL_SIZE_FP];
125 #if MPHELL_USE_AMNS == 1
130 #elif MPHELL_USE_IPP == 1
140 fp_elt pool_1[POOL_SIZE_FP];
142 #if MPHELL_USE_MULTITHREADING == 1
143 fp_elt pool_2[POOL_SIZE_FP];
176 fp_elt_print (fp_elt_srcptr src,
const uint8_t base,
const bool lift,
const fp_param param, uint8_t stack);
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
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)++];
200 else if (stack == STACK_2)
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)++];
223 #if MPHELL_USE_MULTITHREADING == 0
224 MPHELL_ASSERT(stack == STACK_1,
"fp_elt_relax_pool_elt, unknow stack \n");
226 MPHELL_ASSERT(param->i_1 >= 0,
"param->i_1 is < 0 in pool 1\n");
227 #elif MPHELL_USE_MULTITHREADING == 1
231 MPHELL_ASSERT(param->i_1 >= 0,
"param->i_1 is < 0 in pool 1\n");
233 else if (stack == STACK_2)
236 MPHELL_ASSERT(param->i_2 >= 0,
"param->i_2 is < 0 in pool 2\n");
256 #if MPHELL_USE_AMNS == 1
376 fp_elt_set_ui (fp_elt_ptr dst,
const block src,
const bool isreduced,
377 const fp_param param, uint8_t stack);
390 const fp_param param, uint8_t stack);
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);
447 fp_str (
char **str,
const fp_param param,
const uint8_t base, uint8_t stack);
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);
498 #if MPHELL_USE_AMNS == 1
500 #elif (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
502 #elif MPHELL_USE_IPP == 1
504 ippsGFpIsZeroElement(src, &res, param->gf);
505 return (res == IPP_IS_EQ);
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 )
515 mbedtls_mpi_uint c, z;
517 for( i = c = 0; i < n; i++, s++, d++ )
519 z = ( *d < c ); *d -= c;
520 c = ( *d < *s ) + z; *d -= *s;
525 z = ( *d < c ); *d -= c;
530 static inline bool mpi_mod_add_mphell(number_ptr dst, number_srcptr mod)
532 if( mbedtls_mpi_cmp_abs(dst, mod) >= 0 )
534 mpi_sub_hlp_mphell(mod->n, mod->p, dst->p );
550 fp_elt_add (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
553 #if MPHELL_USE_AMNS == 1
555 #elif MPHELL_USE_GMP == 1
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);
579 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1 || MPHELL_USE_AMNS == 1)
581 #elif MPHELL_USE_IPP == 1
582 ippsGFpAdd(src, param->one_mon, dst, param->gf);
595 fp_elt_sub (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
598 #if MPHELL_USE_AMNS == 1
600 #elif MPHELL_USE_GMP == 1
606 #elif MPHELL_USE_MBEDTLS == 1
607 if( mbedtls_mpi_cmp_abs( *src1, *src2 ) >= 0 )
609 mbedtls_mpi_sub_abs( *dst, *src1, *src2 );
614 mbedtls_mpi_uint p[(*src2)->n];
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);
623 #elif MPHELL_USE_IPP == 1
624 ippsGFpSub(src1, src2, dst, param->gf);
638 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1 || MPHELL_USE_AMNS == 1)
640 #elif MPHELL_USE_IPP == 1
641 ippsGFpSub(src, param->one_mon, dst, param->gf);
655 #if MPHELL_USE_AMNS == 1
657 #elif MPHELL_USE_GMP == 1
663 #elif MPHELL_USE_IPP == 1
664 ippsGFpNeg(src, dst, param->gf);
665 #elif MPHELL_USE_MBEDTLS == 1
685 fp_elt_mul (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
686 const fp_param param, uint8_t stack)
688 #if MPHELL_USE_AMNS == 1
690 #elif (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
691 #if MPHELL_USE_MONTGOMERY == 1
696 #elif MPHELL_USE_IPP == 1
697 ippsGFpMul(src1, src2, dst, param->gf);
712 #if MPHELL_USE_AMNS == 1
714 #elif MPHELL_USE_GMP == 1
720 #elif MPHELL_USE_MBEDTLS == 1
722 mpi_mod_add_mphell(*dst, param->p);
723 #elif MPHELL_USE_IPP == 1
739 #if MPHELL_USE_AMNS == 1
742 #elif MPHELL_USE_GMP == 1
753 #elif MPHELL_USE_MBEDTLS == 1
758 if(mpi_mod_add_mphell(*dst, param->pm[i]))
763 #elif MPHELL_USE_IPP == 1
780 #if MPHELL_USE_AMNS == 1
784 #elif MPHELL_USE_GMP == 1
795 #elif MPHELL_USE_MBEDTLS == 1
800 if(mpi_mod_add_mphell(*dst, param->pm[i]))
805 #elif MPHELL_USE_IPP == 1
822 const fp_param param, uint8_t stack)
824 #if MPHELL_USE_AMNS == 1
830 #elif MPHELL_USE_GMP == 1
840 #elif MPHELL_USE_MBEDTLS == 1
844 mpi_mod_add_mphell(*tmp, param->p);
847 #elif MPHELL_USE_IPP == 1
867 #if MPHELL_USE_AMNS == 1
869 #elif (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1)
871 #elif MPHELL_USE_IPP == 1
872 ippsGFpSqr(src, dst, param->gf);
909 fp_elt_div (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
910 const fp_param param, uint8_t stack);
922 fp_elt_pow_ui (fp_elt_ptr dst, fp_elt_srcptr src,
const block n,
923 const fp_param param, uint8_t stack);
936 const fp_param param, uint8_t stack);
void amns_elt_sub(amns_elt_ptr dst, amns_elt_srcptr a, amns_elt_srcptr b, amns_srcptr AMNS)
Set dst to a - b.
void amns_elt_add(amns_elt_ptr dst, amns_elt_srcptr a, amns_elt_srcptr b, amns_srcptr AMNS)
Set dst to a + b.
void amns_elt_neg(amns_elt_ptr dst, amns_elt_srcptr a, amns_srcptr AMNS)
Set dst to -a.
bool amns_elt_is_zero(amns_elt_srcptr src, amns_srcptr AMNS)
Test if src is zero.
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...
void mphell_error(char *expr)
Write in stderr, filename, line and expr, free mphell.
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.
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".
void fp_create(fp_param param, number_srcptr p, fp_id id, uint8_t stack)
Create a prime field of characteristic p.
static void fp_elt_neg(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- (-src) mod p.
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.
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...
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.
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.
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...
void fp_get_characteristic(number_ptr c, const fp_param param)
Get the characteristic of the prime field "param".
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.
static void fp_elt_mul3(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
Set dst <- 3 * src.
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.
static void fp_elt_inc(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- src + 1.
static bool fp_elt_iszero(fp_elt_srcptr src, const fp_param param)
Test if src is zero.
static void fp_elt_dec(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- src - 1.
void fp_elt_alloc(fp_elt *dst, const fp_param param)
Allocate space for a primary field element.
void fp_elt_free(fp_elt *src)
Free space used by src.
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.
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.
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.
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.
void fp_elt_init(fp_elt_ptr dst, const fp_param param)
Initialise a primary field element.
fp_param_t * fp_param
Pointer on a primary field parameters structure.
void fp_alloc(fp_param param, const uint8_t size)
Allocate space for the prime field informations structure.
int8_t fp_elt_cmp(fp_elt_srcptr src1, fp_elt_srcptr src2, const fp_param param)
Compare src1 and src2 in Fp.
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.
enum fp_id_e fp_id
Identifier for known field, use by IPPCP to accelerate the field arithmetic.
static void fp_elt_mul2(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- 2 * src.
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.
bool fp_elt_isone(fp_elt_srcptr src, const fp_param param)
Test if src is one.
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 ...
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)
static void fp_elt_mul8(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- 8 * src.
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...
static void fp_elt_mul4(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- 4 * src.
void fp_free(fp_param param)
Free the space of the prime field informations structure.
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...
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.
void fp_elt_set_zero(fp_elt_ptr dst, const fp_param param)
Set dst to zero.
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)
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.
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.
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.
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.
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.
static void fp_elt_sqr(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
Set dst <- src^2.
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.
void fp_copy(fp_param param_res, const fp_param param)
Copy the prime field structure param into param_res.
fp_id_e
Identifier for known field, use by IPPCP to accelerate the field arithmetic.
void fp_elt_inv(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
Set dst <- src^(-1)
void number_copy(number_ptr dst, number_srcptr src)
Copy src into dst.
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...
Primary field parameters.