27 #include "mphell/mphell.h"
47 number_set_str(p,
"7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed", 16);
48 #if MPHELL_USE_AMNS == 1
50 #if MPHELL_USE_AMNS_32 == 0
51 amns_alloc_init_str(&AMNS,
"[7, 5, [-2, 0, 0, 0, 0, 1], 54, 18452995865838783329900129877370266585014740033535441780974987498391596057242, [961252216531765, -808644316442835, 1045474214679914, 876400205157459, 80702276750246], [215521628593942615, 6036461283567564605, 7224436840646086003, 10819948880990321094, 2770174224844310327], [1261219955914665, 2245748633013857, 1942034862299073, 1713436964429893, 1484033610923007], [982299117090493, 2384806805124374, 473263249613373, 1302309306467128, 767899215173126]]", p);
53 amns_alloc_init_str(&AMNS,
"[1, 13, [-2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 24, 27348853913383126840958695328985844583331474485808319900533972989426705703935, [295765, -240077, -384002, 86822, 427979, -178061, 355024, 35335, -47509, 293623, -162279, 239276, -54823], [304002351, 603274027, 987322065, 611192953, 899268748, 456421020, 1804479788, 151565031, 3376928416, 3404037305, 3106617823, 3555398377, 1245081753], [420571, 1005149, 1316639, 803101, 880079, 1425571, 78727, 673803, 292695, 429454, 603140, -81310, 484327], [-78425, 649592, 1210799, 537480, 931731, 330032, 587956, 604351, 932082, 256513, 633201, 50270, 916590]]", p);
55 field_set_amns(k, AMNS);
84 field_elt_set_str(b,
"2dfc9311d490018c7338bf8688861767ff8ff5b2bebe27548a14b235eca6874a", 16,
false, k, STACK_1);
86 ec_point_set_aff_str(G,
"159a6849e44c3c7f061b3d570fc4ed5b5d14c8ba4253df49cc7edf80f533ad9b",
"6666666666666666666666666666666666666666666666666666666666666658",
false, 16,
EDWARDS, k, STACK_1);
87 number_set_str(h,
"0000000000000000000000000000000000000000000000000000000000000008", 16);
88 number_set_str(n,
"1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed", 16);
90 ec_create (E,
"Edwards_test", k, a, b, G, h, n,
EDWARDS,
EXTENDED_EDWARDS, STACK_1);
114 ec_point_set_aff_str(x,
"67e82d6ecfec167788bc76ffc0165be5b3b5ac785f5bd1d2c427fa5c4ef02548",
"5f6b282c46a137a1d612df50487cfa27a4b4df2c77bf6f12e56ec667ee37b181",
false, 16,
EDWARDS, k, STACK_1);
115 ec_point_set_aff_str(y,
"1ec6d017b0d11bd1d1b5cfefebfccbc8c8079e0f1030008a2e75207b77a987e6",
"20bbd47f960781e9e13ea7cff81a4f5bec6106453cbae87abd3018d7803539be",
false, 16,
EDWARDS, k, STACK_1);
120 printf(
"x+y = ");
ec_point_print(res, 16,
true, k, STACK_1); printf(
"\n");
121 printf(
"x+y belongs to E : %d\n\n",
ec_belongs(res, E, STACK_1));
126 printf(
"x-y = ");
ec_point_print(res, 16,
true, k, STACK_1); printf(
"\n");
127 printf(
"x-y belongs to E : %d\n\n",
ec_belongs(res, E, STACK_1));
132 printf(
"x*2 = ");
ec_point_print(res, 16,
true, k, STACK_1); printf(
"\n");
133 printf(
"x*2 belongs to E : %d\n\n",
ec_belongs(res, E, STACK_1));
138 printf(
"-x = ");
ec_point_print(res, 16,
true, k, STACK_1); printf(
"\n");
139 printf(
"-x belongs to E : %d\n\n",
ec_belongs(res, E, STACK_1));
145 number_set_str(n1,
"67e82d6ecfec167788bc76ffc0165be5b3b5ac785f5bd1d2c427fa5c4ef02551", 16);
147 printf(
"x*67e82d6ecfec167788bc76ffc0165be5b3b5ac785f5bd1d2c427fa5c4ef02551 = ");
ec_point_print(res, 16,
true, k, STACK_1); printf(
"\n");
148 printf(
"x*67e82d6ecfec167788bc76ffc0165be5b3b5ac785f5bd1d2c427fa5c4ef02551 belongs to E : %d\n\n",
ec_belongs(res, E, STACK_1));
154 printf(
"random point, res = ");
ec_point_print(res, 16,
true, k, STACK_1); printf(
"\n");
155 printf(
"res belongs to E : %d\n\n",
ec_belongs(res, E, STACK_1));
166 printf(
"the image point on a weierstrass curve, convP=");
ec_point_print(convP, 16,
true, k, STACK_1); printf(
"\n");
177 printf(
"convC: \n");
ec_curve_print(convC, 16, STACK_1); printf(
"\n");
187 #if MPHELL_USE_AMNS == 1
void amns_free(amns_ptr *AMNS)
Free the amns system.
void amns_alloc_init_str(amns_ptr *AMNS, char *str, number p)
Allocate and initialise the amns system from the string generated by the Sage AMNS generator from htt...
void ec_curve_print(ec_curve_srcptr E, const uint8_t base, uint8_t stack)
Print a description of E.
void ec_point_print(ec_point_srcptr P, const uint8_t base, const bool lift, field_srcptr k, uint8_t stack)
Print a description of P.
void ec_create(ec_curve_ptr E, const char *id_curve, field_srcptr k, fe_srcptr a, fe_srcptr b, ec_point_srcptr G, number_srcptr h, number_srcptr n, const ec_type type, const ec_formula f, uint8_t stack)
Create an elliptic curve E, the curve must be allocated and initialised (ec_alloc & ec_init)
bool ec_belongs(ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Test if P belongs to E.
void ec_point_mul(ec_point_ptr P3, number_srcptr n, ec_point_srcptr P1, ec_curve_srcptr E, uint8_t stack)
Set P3 to n * P1 using Montgomery for Weierstrass elliptic curve, and naive method for other elliptic...
void edwards_to_weierstrass(ec_curve_ptr E_res, ec_curve_srcptr E, uint8_t stack)
Convert the Edwards elliptic curve E to the corresponding Weierstrass elliptic curve E_res.
void ec_point_random(ec_point_ptr P, ec_curve_srcptr E, uint8_t stack)
Create a random point P on the elliptic curve E.
void ec_point_add(ec_point_ptr P3, ec_point_srcptr P1, ec_point_srcptr P2, ec_curve_srcptr E, uint8_t stack)
Set P3 to P1 + P2, using dedicated formulae (not protected against SPA, but faster)
void ec_free(ec_curve_ptr E)
Free the elliptic curve E.
void ec_init(ec_curve_ptr E, field_srcptr k)
Initialise a curve.
void ec_point_dbl(ec_point_ptr P3, ec_point_srcptr P1, ec_curve_srcptr E, uint8_t stack)
Set P3 to 2*P1, using dedicated formulae (not protected against SPA, but faster)
void ec_point_set_aff_str(ec_point_ptr P, const char *str_x, const char *str_y, const bool is_reduced, const uint8_t base, const ec_type type, field_srcptr k, uint8_t stack)
Set a point from its affine coordinates under string format.
void ec_point_neg(ec_point_ptr P3, ec_point_srcptr P1, ec_curve_srcptr E)
Set P3 to -P1.
void ec_point_init(ec_point_ptr P, field_srcptr k)
Initialise an elliptic curve point.
void ec_point_free(ec_point_ptr P, field_srcptr k)
Free the point P.
void ec_point_alloc(ec_point_ptr P, field_srcptr k)
Allocate an elliptic curve point.
void ec_alloc(ec_curve_ptr E, field_srcptr k)
Allocate a curve.
void ec_point_sub(ec_point_ptr P3, ec_point_srcptr P1, ec_point_srcptr P2, ec_curve_srcptr E, uint8_t stack)
Set P3 to P1 - P2, using dedicated formulae (not protected against SPA, but faster)
void edwards_point_to_weierstrass_point(ec_point_ptr dst, ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Convert the Edwards point P of the elliptic curve E to the corresponding Weierstrass elliptic curve p...
void field_elt_free(fe_ptr *src, field_srcptr k)
Free space used by src.
void field_alloc(field_ptr k, const field_type type, const uint8_t size, field_ptr base)
Allocates space for the different fields of the structure pointed by k.
void field_elt_set_str(fe_ptr dst, const char *str, const uint8_t base, const bool isreduced, field_srcptr k, uint8_t stack)
Set dst to str, if Montgomery arithmetic is used, is_reduced == false -> transform dst into its Montg...
void field_elt_init(fe_ptr dst, field_srcptr k)
Initialise the field element.
void field_elt_alloc(fe_ptr *dst, field_srcptr k)
Allocate space for a field element.
void field_free(field_ptr k)
Free the space of the field informations structure.
void field_create(field_ptr k, const char *id, uint8_t stack, const uint32_t n,...)
Initialize the different fields of the structure pointed by k.
void field_elt_set_one(fe_ptr dst, field_srcptr k)
Set dst to one (or its Montgomery form if Montgomery arithmetic is used)
field_t field[1]
Address of a field structure.
fp_elt * field_elt
Generic field element.
void free_mphell()
Free MPHELL memory, especially the big amount of temporary memory.
void init_mphell(const uint16_t security_strength, const random_type type, const entropy_type entropy)
Initialise MPHELL with security_strength bits of security (for random number only).
void number_free(number *dst)
Free a number_ptr allocated on the RAM memory (malloc)
void number_set_str(number_ptr dst, const char *str, const uint8_t base)
Set dst to str.
void number_init(number *dst, const uint8_t n)
Allocate a number_ptr on the RAM memory (malloc)
uint8_t bits_to_nblock(const uint16_t nbits)
Return the number of blocks required to store a nbits number.
Define an elliptic curve.
Define an elliptic curve point.