31 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1) 36 typedef number * fp_elt;
37 typedef number fp_elt_t;
42 typedef number * fp_elt_ptr;
48 typedef const number * fp_elt_srcptr;
49 #elif MPHELL_USE_IPP == 1 54 typedef IppsGFpElement * fp_elt;
55 typedef IppsGFpElement fp_elt_t;
60 typedef IppsGFpElement * fp_elt_ptr;
66 typedef const IppsGFpElement * fp_elt_srcptr;
73 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1) 79 #if MPHELL_USE_MONTGOMERY == 1 93 fp_elt pool_1[POOL_SIZE_FP];
95 #if MPHELL_USE_MULTITHREADING == 1 96 fp_elt pool_2[POOL_SIZE_FP];
101 #elif MPHELL_USE_IPP == 1 110 fp_elt pool_1[POOL_SIZE_FP];
112 #if MPHELL_USE_MULTITHREADING == 1 113 fp_elt pool_2[POOL_SIZE_FP];
146 fp_elt_print (fp_elt_srcptr src,
const uint8_t base,
const bool lift,
const fp_param param, uint8_t stack);
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 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 172 else if (stack == STACK_2)
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 197 #if MPHELL_USE_MULTITHREADING == 0 198 MPHELL_ASSERT(stack == STACK_1,
"fp_elt_relax_pool_elt, unknow stack \n");
200 MPHELL_ASSERT(param->i_1 >= 0,
"param->i_1 is < 0 in pool 1\n");
201 #elif MPHELL_USE_MULTITHREADING == 1 205 MPHELL_ASSERT(param->i_1 >= 0,
"param->i_1 is < 0 in pool 1\n");
207 else if (stack == STACK_2)
210 MPHELL_ASSERT(param->i_2 >= 0,
"param->i_2 is < 0 in pool 2\n");
342 fp_elt_set_ui (fp_elt_ptr dst,
const block src,
const bool isreduced,
343 const fp_param param, uint8_t stack);
356 const fp_param param, uint8_t stack);
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);
413 fp_str (
char **str,
const fp_param param,
const uint8_t base, uint8_t stack);
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);
464 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1) 466 #elif MPHELL_USE_IPP == 1 468 ippsGFpIsZeroElement(src, &res, param->gf);
469 return (res == IPP_IS_EQ);
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 )
479 mbedtls_mpi_uint c, z;
481 for( i = c = 0; i < n; i++, s++, d++ )
483 z = ( *d < c ); *d -= c;
484 c = ( *d < *s ) + z; *d -= *s;
489 z = ( *d < c ); *d -= c;
494 static inline bool mpi_mod_add_mphell(number_ptr dst, number_srcptr mod)
496 if( mbedtls_mpi_cmp_abs(dst, mod) >= 0 )
498 mpi_sub_hlp_mphell(mod->n, mod->p, dst->p );
514 fp_elt_add (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
517 #if MPHELL_USE_GMP == 1 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);
541 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1) 543 #elif MPHELL_USE_IPP == 1 544 ippsGFpAdd(src, param->one, dst, param->gf);
557 fp_elt_sub (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
560 #if MPHELL_USE_GMP == 1 566 #elif MPHELL_USE_MBEDTLS == 1 567 if( mbedtls_mpi_cmp_abs( *src1, *src2 ) >= 0 )
569 mbedtls_mpi_sub_abs( *dst, *src1, *src2 );
574 mbedtls_mpi_uint p[(*src2)->n];
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);
583 #elif MPHELL_USE_IPP == 1 584 ippsGFpSub(src1, src2, dst, param->gf);
598 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1) 600 #elif MPHELL_USE_IPP == 1 601 ippsGFpSub(src, param->one, dst, param->gf);
615 #if MPHELL_USE_GMP == 1 621 #elif MPHELL_USE_IPP == 1 622 ippsGFpNeg(src, dst, param->gf);
623 #elif MPHELL_USE_MBEDTLS == 1 643 fp_elt_mul (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
644 const fp_param param, uint8_t stack)
646 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1) 647 #if MPHELL_USE_MONTGOMERY == 1 652 #elif MPHELL_USE_IPP == 1 653 ippsGFpMul(src1, src2, dst, param->gf);
668 #if MPHELL_USE_GMP == 1 674 #elif MPHELL_USE_MBEDTLS == 1 676 mpi_mod_add_mphell(*dst, param->p);
677 #elif MPHELL_USE_IPP == 1 693 #if MPHELL_USE_GMP == 1 704 #elif MPHELL_USE_MBEDTLS == 1 709 if(mpi_mod_add_mphell(*dst, param->pm[i]))
714 #elif MPHELL_USE_IPP == 1 731 #if MPHELL_USE_GMP == 1 742 #elif MPHELL_USE_MBEDTLS == 1 747 if(mpi_mod_add_mphell(*dst, param->pm[i]))
752 #elif MPHELL_USE_IPP == 1 769 const fp_param param, uint8_t stack)
771 #if MPHELL_USE_GMP == 1 781 #elif MPHELL_USE_MBEDTLS == 1 785 mpi_mod_add_mphell(*tmp, param->p);
788 #elif MPHELL_USE_IPP == 1 808 #if (MPHELL_USE_GMP == 1 || MPHELL_USE_MBEDTLS == 1) 810 #elif MPHELL_USE_IPP == 1 811 ippsGFpSqr(src, dst, param->gf);
848 fp_elt_div (fp_elt_ptr dst, fp_elt_srcptr src1, fp_elt_srcptr src2,
849 const fp_param param, uint8_t stack);
861 fp_elt_pow_ui (fp_elt_ptr dst, fp_elt_srcptr src,
const block n,
862 const fp_param param, uint8_t stack);
875 const fp_param param, uint8_t stack);
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.
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.
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.
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.
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 mphell_error(char *expr)
Write in stderr, filename, line and expr, free mphell.
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.
static void fp_elt_mul8(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- 8 * src.
enum fp_id_e fp_id
Identifier for known field, use by IPPCP to accelerate the field arithmetic.
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.
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_set_zero(fp_elt_ptr dst, const fp_param param)
Set dst to zero.
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.
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_get_characteristic(number_ptr c, const fp_param param)
Get the characteristic of the prime field "param".
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.
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.
fp_id_e
Identifier for known field, use by IPPCP to accelerate the field arithmetic.
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 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.
void fp_elt_init(fp_elt_ptr dst, const fp_param param)
Initialise a primary field element.
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 ...
static void fp_elt_inc(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- src + 1.
void fp_elt_inv(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
Set dst <- src^(-1)
static bool fp_elt_iszero(fp_elt_srcptr src, const fp_param param)
Test if src is zero.
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_get_pool_elt(fp_elt *dst, const fp_param param, uint8_t stack)
Get an initialised field element from the pool.
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)
static void fp_elt_mul3(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
Set dst <- 3 * src.
static void fp_elt_mul2(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- 2 * src.
void number_copy(number_ptr dst, number_srcptr src)
Copy src into dst.
bool fp_elt_isone(fp_elt_srcptr src, const fp_param param)
Test if src is one.
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.
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.
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_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_mul4(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- 4 * src.
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.
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.
static void fp_elt_dec(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- src - 1.
void fp_copy(fp_param param_res, const fp_param param)
Copy the prime field structure param into param_res.
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...
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...
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.
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_create(fp_param param, number_srcptr p, fp_id id, uint8_t stack)
Create a prime field of characteristic p.
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.
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.
fp_param_t * fp_param
Pointer on a primary field parameters structure.
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.
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.
void fp_alloc(fp_param param, const uint8_t size)
Allocate space for the prime field informations structure.