69 param->
size = base->size;
141 param->
size = (uint8_t)0;
236 const bool isreduced,
const fp2_param param, uint8_t stack)
251 const bool isreduced,
const fp2_param param, uint8_t stack)
253 char *s = (
char*) malloc(
sizeof(
char)*strlen(str)+10);
257 char *str1, *str2 = NULL;
258 str1 = strsep(&s,
",");
259 str2 = strsep(&s,
",");
260 MPHELL_ASSERT_ALWAYS((str1!= NULL) && (str2 != NULL),
261 "fp2_elt_set_str : invalid string");
284 char *base_str, *non_residue_str, *q_str;
290 *str = (
char*)malloc(100 + strlen(base_str) + strlen(q_str) +
291 strlen(non_residue_str));
293 sprintf(s,
"base parameter \n%s\nnon residue quadratic = %s\norder = %s",
294 base_str, non_residue_str, q_str);
298 free(non_residue_str);
303 const bool lift,
const fp2_param param, uint8_t stack)
308 *str = malloc(strlen(str1) + strlen(str2) + 4);
309 sprintf(*str,
"%s,%s", str1, str2);
572 while (m != (block)0)
591 #if MPHELL_USE_GMP == 1
593 #elif MPHELL_USE_IPP == 1
595 ippsRef_BN(NULL, &size, NULL, n);
597 #elif MPHELL_USE_MBEDTLS == 1
614 #if MPHELL_USE_GMP == 1
616 #elif MPHELL_USE_IPP == 1
618 #elif MPHELL_USE_MBEDTLS == 1
707 #if MPHELL_USE_GMP == 1
709 #elif MPHELL_USE_IPP == 1
711 ippsRef_BN(NULL, &size, NULL, n);
713 #elif MPHELL_USE_MBEDTLS == 1
724 #if MPHELL_USE_GMP == 1
726 #elif MPHELL_USE_IPP == 1
728 #elif MPHELL_USE_MBEDTLS == 1
738 #if MPHELL_USE_GMP == 1
740 #elif MPHELL_USE_IPP == 1
742 #elif MPHELL_USE_MBEDTLS == 1
763 #if MPHELL_USE_GMP == 1
765 #elif MPHELL_USE_IPP == 1
767 ippsRef_BN(NULL, &size, NULL, param->
p_odd);
769 #elif MPHELL_USE_MBEDTLS == 1
788 #if MPHELL_USE_GMP == 1
790 #elif MPHELL_USE_IPP == 1
792 #elif MPHELL_USE_MBEDTLS == 1
802 MPHELL_ASSERT_ALWAYS(m <= param->p_even,
"fp_elt_sqrt : \
806 while (m < param->p_even - 1)
816 #if MPHELL_USE_GMP == 1
818 #elif MPHELL_USE_IPP == 1
820 #elif MPHELL_USE_MBEDTLS == 1
835 block pow = (block)1;
839 fp2_elt b, c1, c2, h, r, d, tmp1;
894 for(i=1; i<= (s-1); i++)
975 block prime[20]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71};
1004 while(test ==
false);
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.
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.
void fp2_elt_init(fp2_elt_ptr dst, const fp2_param param)
Initialise a quadratic extension field element.
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)
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.
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.
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.
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.
void fp2_elt_sub(fp2_elt_ptr dst, fp2_elt_srcptr src1, fp2_elt_srcptr src2, const fp2_param param)
Set dst <- src1 - src2.
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....
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 ...
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...
void fp2_elt_inc(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Set dst <- src + 1.
void fp2_get_characteristic(number_ptr c, const fp2_param param)
Get the characteristic of the quadratic extension field "param".
void fp2_elt_dec(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Set dst <- src - 1.
void fp2_free(fp2_param param)
Free the space of the quadratic extension field informations structure.
void fp2_elt_add(fp2_elt_ptr dst, fp2_elt_srcptr src1, fp2_elt_srcptr src2, const fp2_param param)
Set dst <- src1 + src2.
void fp2_elt_mul8(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Set dst <- 8 * src.
void fp2_elt_inv(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
Set dst <- src^(-1)
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)
void fp2_elt_mul2(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Set dst <- 2 * src.
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".
void fp2_elt_mul3(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
Set dst <- 3 * src.
bool fp2_elt_iszero(fp2_elt_srcptr src, const fp2_param param)
Test if src is zero.
int8_t fp2_elt_cmp(fp2_elt_srcptr src1, fp2_elt_srcptr src2, const fp2_param param)
Compare src1 and src2 in FP2.
void fp2_elt_sqr(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param, uint8_t stack)
Set dst <- src^2.
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.
void fp2_elt_free(fp2_elt *src)
Free space used by src.
void fp2_elt_set_fp_elt(fp2_elt_ptr dst, fp_elt_srcptr src, const fp2_param param)
Set dst to src.
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.
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.
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...
void fp2_alloc(fp2_param param, const fp_param base)
Allocate space for the quadratic extension field informations structure.
void fp2_elt_neg_fp_elt(fp2_elt_ptr dst, fp_elt_srcptr src, const fp2_param param)
Set dst <- (-src)
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...
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.
void fp2_elt_neg(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Set dst <- (-src)
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.
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...
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...
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.
bool fp2_elt_isone(fp2_elt_srcptr src, const fp2_param param)
Test if src is one.
void fp2_copy(fp2_param param_res, const fp2_param param)
Copy the quadratic extension field structure param into param_res.
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...
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)
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.
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...
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.
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.
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)
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.
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.
void fp2_elt_set_zero(fp2_elt_ptr dst, const fp2_param param)
Set dst to zero.
void fp2_get_size(number_ptr c, const fp2_param param)
Get the size of the quadratic extension field "param".
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.
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.
void fp2_elt_mul4(fp2_elt_ptr dst, fp2_elt_srcptr src, const fp2_param param)
Set dst <- 4 * src.
void fp2_elt_alloc(fp2_elt *dst, const fp2_param param)
Allocate space for a quadratic extension field element.
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.
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 ...
Declaration of binary field (finite field constructed with an irreducible polynomial of degree 2) fun...
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_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_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_init(fp_elt_ptr dst, const fp_param param)
Initialise a primary field element.
int8_t fp_elt_cmp(fp_elt_srcptr src1, fp_elt_srcptr src2, const fp_param param)
Compare src1 and src2 in Fp.
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.
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)
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...
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.
void fp_elt_inv(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param, uint8_t stack)
Set dst <- src^(-1)
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.
static void fp_elt_neg(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- (-src) mod p.
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_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.
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.
static void fp_elt_mul2(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- 2 * src.
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_mul8(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- 8 * src.
static void fp_elt_mul4(fp_elt_ptr dst, fp_elt_srcptr src, const fp_param param)
Set dst <- 4 * src.
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.
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.
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 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_set_ui(number_ptr dst, const block src)
Set dst to src.
void number_free(number *dst)
Free a number_ptr allocated on the RAM memory (malloc)
void number_str(char **str, number_srcptr src, const uint8_t base)
Converts src to string format in base specified by base.
bool number_isdiff_ui(number_srcptr src1, const block src2)
Test if src1 != 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.
void number_tmp_free(number *t, const uint8_t size, uint8_t stack)
Free a temporary number.
void number_mod_ui(block *dst, number_srcptr src1, const block src2)
Compute dst such that src1 = q * src2 + dst ; dst < src2.
void number_gcd_ui(block *dst, number_srcptr src1, const block src2)
Set dst to GCD(src1, src2)
bool number_iseven(number_srcptr src)
Test if src is even.
block number_and_ui(number_srcptr src1, const block src2, uint8_t stack)
Apply logical bitwise AND operator between src1 and src2.
void number_rshift(number_ptr dst, number_srcptr src, const uint16_t shift)
Set dst to src >> shift.
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.
void number_sqr(number_ptr dst, number_srcptr src)
Set dst to src1^2.
void number_tmp_alloc(number *t, const uint8_t size, uint8_t stack)
Allocate a temporary number.
int8_t number_cmp_ui(number_srcptr src1, const block src2)
Compare src1 and 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 number_dec(number_ptr dst, number_srcptr src)
Set dst to src - 1 if src - 1 fit in dst.
void number_inc(number_ptr dst, number_srcptr src)
Set dst to src + 1 if src + 1 fit in dst.
void number_init(number *dst, const uint8_t n)
Allocate a number_ptr on the RAM memory (malloc)
Quadratic extension field element structure.
Quadratic extension field structure.
Primary field parameters.