128 uint16_t s = (t-1)/160;
130 uint16_t h = t - (160*s);
132 uint16_t hbytes = (h / 8) + (h%8 != 0);
136 uint8_t * Wprime = calloc(s*20+hbytes,
sizeof(uint8_t));
141 uint8_t seed_bin[20];
144 sha1(H, seed_bin, 160);
146 memcpy(Wprime, H+(20-hbytes), hbytes);
186 sha1(H, seed_bin, 160);
187 memcpy(Wprime+i*20+hbytes, H, 20);
193 unsigned char Wprime_hex[2*(s*20+hbytes)+1];
194 bin2hex(Wprime, s*20+hbytes, Wprime_hex);
239 number_set_str(bound_h,
"ffffffffffffffffffffffffffffffffffffffff", 16);
240 number_set_str(bound_l,
"1000000000000000000000000000000000000000", 16);
266 uint16_t s = (t-1)/160;
268 uint16_t h = t - (160*s);
270 uint16_t hbytes = (h / 8) + (h%8 != 0);
273 uint8_t * Wprime = calloc(s*20+hbytes,
sizeof(uint8_t));
289 uint8_t seed_bin[20];
292 sha1(H, seed_bin, 160);
294 memcpy(Wprime, H+(20-hbytes), hbytes);
332 sha1(H, seed_bin, 160);
333 memcpy(Wprime+i*20+hbytes, H, 20);
337 unsigned char Wprime_hex[2*(s*20+hbytes)+1];
338 bin2hex(Wprime, s*20+hbytes, Wprime_hex);
382 strcpy(seed_res, seed_hex);
433 const bool is_reduced,
const uint8_t base,
field_srcptr k, uint8_t stack)
944 field_elt tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
951 MPHELL_ASSERT(
field_elt_isone(P1->
z, k),
"\nThis point P1 should have its Z coordinate equals to 1 to apply DBLU\n");
1133 field_elt tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
1227 field_elt U1, U2, S1, S2, ZZ, T, TT, M, R, F, L, LL, G, W;
1522 for(i = 1; i < strlen(n_str); i++)
1543 void setup_Kim(
fe_ptr X1,
fe_ptr X2,
fe_ptr K,
fe_ptr L,
fe_ptr A,
fe_ptr S,
fe_ptr T,
fe_srcptr x0,
fe_srcptr y0,
bool bit,
ec_curve_srcptr E, uint8_t stack)
1608 void update_Kim(
fe_ptr X1,
fe_ptr X2,
fe_ptr K,
fe_ptr L,
fe_ptr A,
fe_ptr S,
fe_ptr T,
bool prev,
bool new,
ec_curve_srcptr E, uint8_t stack)
1716 void recovery_Kim(
fe_ptr P3x,
fe_ptr P3y,
fe_ptr X1,
fe_ptr X2,
fe_ptr K,
fe_ptr L,
fe_ptr A,
fe_ptr S,
fe_ptr T,
fe_srcptr x0,
fe_srcptr y0,
field_ptr k,
bool safe, uint8_t stack)
1790 setup_Kim(X1, X2, K, L, A, S, T, P1->
x, P1->
y, n_str[1]==
'1', E, stack);
1794 for(i = 1; i < strlen(n_str)-1; i++)
1798 if(n_str[i+1]==
'0')
1800 update_Kim(X1, X2, K, L, A, S, T,
false,
false, E, stack);
1804 update_Kim(X1, X2, K, L, A, S, T,
false,
true, E, stack);
1809 if(n_str[i+1]==
'0')
1811 update_Kim(X1, X2, K, L, A, S, T,
true,
false, E, stack);
1815 update_Kim(X1, X2, K, L, A, S, T,
true,
true, E, stack);
1819 if(n_str[strlen(n_str)-1]==
'0')
1821 update_Kim(X1, X2, K, L, A, S, T,
false,
true, E, stack);
1825 update_Kim(X1, X2, K, L, A, S, T,
true,
true, E, stack);
1828 recovery_Kim(P3->
x, P3->
y, X1, X2, K, L, A, S, T, P1->
x, P1->
y, E->
k,
false, stack);
1859 if(n_str[strlen(n_str)-1] ==
'1')
1861 if(n_str[strlen(n_str)-2] ==
'0')
1872 for(i = strlen(n_str) - 3; i >=0; i--)
1887 if(n_str[strlen(n_str)-2] ==
'0')
1898 for(i = strlen(n_str) - 3; i >=0; i--)
2022 field_elt c, d, e, f, g, c2, d2, e2, f2, non_residue;
2044 MPHELL_ASSERT( E->
type ==
WEIERSTRASS,
"This curve is not a Weierstrass Curve \n");
2075 MPHELL_ASSERT(
field_elt_ispower_ui(e2, 3, k2, stack),
"This curve do not seem to have points of order 2 defined \n");
2076 MPHELL_ASSERT(
field_elt_ispower_ui(f2, 3, k2, stack),
"This curve do not seem to have points of order 2 defined \n");
2109 MPHELL_ASSERT(
field_elt_iszero(e, k),
"\nThere is no point of order 2 defined in FP \n");
2113 MPHELL_ASSERT( k->
type==
FP,
"We have to work in an a quadratic extension and it is not implemented for non FP field type \n");
2124 MPHELL_ASSERT(
field_elt_ispower_ui(e, 3, k, stack),
"This curve do not seem to have points of order 2 defined \n");
2125 MPHELL_ASSERT(
field_elt_ispower_ui(f, 3, k, stack),
"This curve do not seem to have points of order 2 defined \n");
2148 MPHELL_ASSERT(
weierstrass_belongs(Q, E, stack),
"The Point of order 2 computed does not belong to the Weierstrass curve E ! \n");
2169 field_elt c, d, e, f, g, c2, d2, e2, f2, non_residue;
2190 MPHELL_ASSERT(E->
type ==
WEIERSTRASS,
"This curve is not a Weierstrass Curve \n");
2221 MPHELL_ASSERT(
field_elt_ispower_ui(e2, 3, k2, stack),
"This curve do not seem to have points of order 2 defined \n");
2222 MPHELL_ASSERT(
field_elt_ispower_ui(f2, 3, k2, stack),
"This curve do not seem to have points of order 2 defined \n");
2262 MPHELL_ASSERT(
field_elt_iszero(e, k),
"\nThere is no point of order 2 defined in FP \n");
2266 MPHELL_ASSERT(k->
type==
FP,
"We have to work in an a quadratic extension and it is not implemented for non FP field type \n");
2277 MPHELL_ASSERT(
field_elt_ispower_ui(e, 3, k, stack),
"This curve do not seem to have points of order 2 defined \n");
2278 MPHELL_ASSERT(
field_elt_ispower_ui(f, 3, k, stack),
"This curve do not seem to have points of order 2 defined \n");
2298 MPHELL_ASSERT(
weierstrass_belongs(Q, E, stack),
"The Point of order 2 computed does not belong to the Weierstrass curve E ! \n");
2322 MPHELL_ASSERT(
weierstrass_belongs(Q, E, stack),
"The Point of order 2 computed does not belong to the Weierstrass curve E ! \n");
2325 MPHELL_ASSERT(
weierstrass_belongs(Q, E, stack),
"The Point of order 2 computed does not belong to the Weierstrass curve E ! \n");
void weierstrass_point_neg(ec_point_ptr P3, ec_point_srcptr P1, ec_curve_srcptr E)
Set P3 to -P1.
void weierstrass_point_add_ZADDC(ec_point_ptr P3, ec_point_ptr P4, ec_point_srcptr P1, ec_point_srcptr P2, ec_curve_srcptr E, uint8_t stack)
Conjugate Co-Z point addition: Set P3 to P1 + P2 and P4 to P1 - P2 such that Z3=Z4.
bool number_iszero(number_srcptr src)
Test if src is zero.
bool weierstrass_verify_random_generation(ec_curve E, const char *seed, uint8_t stack)
Test if E if generated from the seed "seed".
fp_elt * field_elt
Generic field element.
void field_elt_unity_nth_root(fe_ptr dst, const block n, field_ptr k, uint8_t stack)
Set dst to a non trivial n-th root of unity if it exists (ie n divides order(k)-1),...
void ec_point_set_neutral(ec_point_ptr dst, ec_curve_srcptr E, uint8_t stack)
Set dst to the neutral element.
static bool field_elt_iszero(fe_srcptr src, field_srcptr k)
Test if src is zero.
static void field_elt_mul(fe_ptr dst, fe_srcptr src1, fe_srcptr src2, field_srcptr k, uint8_t stack)
Set dst <- src1 * src2, if Montgomery arithmetic is used, the Montgomery multiplication will be used ...
void drbg_incr_data(uint8_t *data, const uint16_t data_len)
Increment data (Set data to data + 1 where data is an array of data_len bytes)
Declaration of sha1 functions.
static void field_elt_mul4(fe_ptr dst, fe_srcptr src, field_srcptr k)
Set dst <- 4 * src.
void weierstrass_point_set_aff_str(ec_point_ptr P, const char *str_x, const char *str_y, const bool is_reduced, const uint8_t base, field_srcptr k, uint8_t stack)
Set dest to the affine point (str_x,str_y)
static void field_elt_mul8(fe_ptr dst, fe_srcptr src, field_srcptr k)
Set dst <- 8 * src.
uint16_t number_log2(number_srcptr src)
Calculate log2(src), which is the binary size of src.
void weierstrass_Zmontgomery_Kim(ec_point_ptr P3, number_srcptr n, ec_point_srcptr P1, ec_curve_srcptr E, uint8_t stack)
Perform the Montgomery ladder, taken from Kim et al's 2017 work, compute P3=[n]P1 it has the drawback...
void weierstrass_point_add_projective_unified(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 projective coordinate.
void number_to_bit_string(char *str, number_srcptr src)
Converts src (must be positive) to a bit string.
Define an elliptic curve point.
void weierstrass_point_TPLU(ec_point_ptr P3, ec_point_ptr P4, ec_point_srcptr P1, ec_curve_srcptr E, uint8_t stack)
Co-Z point tripling with update: Set P3 to 3*P1 and P4 such that P4 = P1 and Z4 = Z3.
uint32_t hex2bin(const char *in, unsigned char *out)
Convert an hexadecimal string into a binary string.
static void field_elt_inc(fe_ptr dst, fe_srcptr src, field_srcptr k)
Set dst <- src + 1.
void weierstrass_point_random(ec_point_ptr P, ec_curve_srcptr E, uint8_t stack)
Set P to a random point on E.
uint8_t bits_to_nblock(const uint16_t nbits)
Return the number of blocks required to store a nbits number.
void field_elt_sqrt(fe_ptr dst, fe_srcptr src, field_srcptr k, uint8_t stack)
Set dst <- src^(1/2)
void weierstrass_compute_disc(ec_curve E, uint8_t stack)
Set the discriminant of E: disc = -16(4a^3 + 27b^2)
void weierstrass_point_DBLU(ec_point_ptr P3, ec_point_ptr P4, ec_point_srcptr P1, ec_curve_srcptr E, uint8_t stack)
Co-Z point doubling with update: Set P3 to 2*P1 and P4 such that P1 = P4 and Z4 = Z3 and assume that ...
field_t field[1]
Address of a field structure.
void field_elt_div(fe_ptr dst, fe_srcptr src1, fe_srcptr src2, field_srcptr k, uint8_t stack)
Set dst <- src1 / src2.
static void field_elt_mul3(fe_ptr dst, fe_srcptr src, field_srcptr k, uint8_t stack)
Set dst <- 3 * src.
void weierstrass_point_add_projective_dedicated(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 projective coordinate.
bool field_elt_isone(fe_srcptr src, field_srcptr k)
Test if src is one.
void field_elt_cube_root(fe_ptr dst, fe_srcptr src, field_srcptr k, uint8_t stack)
Set dst <- src^(1/3)
void number_random1(number_ptr dst, number_srcptr bound, uint8_t stack)
Set dst to a random number_ptr between 0 and bound, the random process is chosen at the MPHELL initia...
void weierstrass_point_add_unified(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 unified formulae (protection against SPA)
void weierstrass_point_set_neutral(ec_point_ptr dst, ec_curve_srcptr E, uint8_t stack)
Set dst to the neutral element: (0,1,0) for projective coordinates and (1,1,0) for jacobian coordinat...
static void ec_point_relax_pool_elt(ec_point_ptr P, field_ptr k, uint8_t stack)
Relax an initialised point from the pool.
bool weierstrass_belongs(ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Test if P belongs to E.
void weierstrass_point_get_x_affine(field_elt x, ec_point_ptr P, ec_curve_srcptr E, uint8_t stack)
Convert P->x to its affine representation.
void weierstrass_point_get_y_affine(field_elt y, ec_point_ptr P, ec_curve_srcptr E, uint8_t stack)
Convert P->y to its affine representation.
void field_elt_set_one(fe_ptr dst, field_srcptr k)
Set dst to one (or its Montgomery form if Montgomery arithmetic is used)
Declaration of ECC functions.
void field_elt_copy(fe_ptr dst, fe_srcptr src, field_srcptr k)
Copy src into dst, src and dst must belong to the same field.
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 weierstrass_point_set_aff(ec_point_ptr P, fe_srcptr x, fe_srcptr y, field_srcptr k)
Set dest to the affine point (x, y)
bool weierstrass_point_is_neutral(ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Test if P is the neutral element.
void field_elt_set_fp_elts(fe_ptr dst, field_srcptr k, const uint32_t n,...)
Set dst to src(s)
void field_get_size(number_ptr c, field_srcptr k)
Get the size of the field "k".
static void ec_point_get_pool_elt(ec_point_ptr P, field_ptr k, uint8_t stack)
Get an initialised point from the pool.
void weierstrass_point_of_order_2(ec_point_ptr dst, ec_curve_srcptr E, uint8_t stack)
Set dst to one of the point of order 2 of the curve E assuming that at least one exist.
bool field_elt_issquare(fe_srcptr src, field_srcptr k, uint8_t stack)
Test if src is a square using the Lengendre symbol.
bool weierstrass_point_are_equal(ec_point_srcptr P1, ec_point_srcptr P2, ec_curve_srcptr E, uint8_t stack)
Test if P1 and P2 are equal on E (BUT do not test if they belong to the curve)
const fp_elt * fe_srcptr
Pointer on a field element, the field element cannot be modified through this pointer.
void weierstrass_point_add_ZADDU(ec_point_ptr P3, ec_point_ptr P1, ec_point_srcptr P2, ec_curve_srcptr E, uint8_t stack)
Co-Z point addition with update: Set P3 = (X3,Y3,Z3) to P1 + P2 using Co-Z addition and update P1 suc...
int8_t field_elt_ispower_ui(fe_srcptr src, const block n, field_srcptr k, uint8_t stack)
Test if src is a n-power in src->k.
void field_elt_get_fp_elt(fe_ptr dst, fe_srcptr src, uint8_t pos, field_srcptr k)
Get the field_elt in position 1, 2 or 3 of the src element. Rmq: if src element is in FP,...
int8_t field_elt_cmp(fe_srcptr src1, fe_srcptr src2, field_srcptr k)
Compare src1 and src2.
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 weierstrass_point_dbl_jacobian_dedicated(ec_point_ptr P3, ec_point_srcptr P1, ec_curve_srcptr E, uint8_t stack)
Set P3 to 2 P1 using jacobian coordinate with the 2007 Bernstein/Lange method.
void number_tmp_free(number *t, const uint8_t size, uint8_t stack)
Free a temporary number.
void weierstrass_point_add_jacobian_dedicated(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 jacobian coordinate with the 2007 Bernstein/Lange method.
fp_elt * fe_ptr
Pointer on a field element.
void field_elt_inv(fe_ptr dst, fe_srcptr src, field_srcptr k, uint8_t stack)
Set dst <- src^(-1)
void weierstrass_points_of_order_2(ec_point_ptr dst1, ec_point_ptr dst2, ec_point_ptr dst3, ec_curve_srcptr E, uint8_t stack)
Set dst1, dst2, dst3 to the points of order 2 of the curve E assuming they are different otherwise th...
void ec_point_copy(ec_point_ptr P3, ec_point_srcptr P, field_srcptr k)
Copy P into P3.
static void field_elt_mul2(fe_ptr dst, fe_srcptr src, field_srcptr k)
Set dst <- 2 * src.
void number_dec(number_ptr dst, number_srcptr src)
Set dst to src - 1 if src - 1 fit in dst.
void field_elt_pow_number(fe_ptr dst, fe_srcptr src, number_srcptr n, field_srcptr k, uint8_t stack)
Set dst <- src^n.
void number_set_str(number_ptr dst, const char *str, const uint8_t base)
Set dst to str.
void number_tmp_alloc(number *t, const uint8_t size, uint8_t stack)
Allocate a temporary number.
static void field_elt_neg(fe_ptr dst, fe_srcptr src, field_srcptr k)
Set dst <- (-src)
void weierstrass_point_norm(ec_point_ptr P, ec_curve_srcptr E, uint8_t stack)
Set P in affine coordinates.
int8_t number_cmp(number_srcptr src1, number_srcptr src2)
Compare src1 and src2.
void weierstrass_point_dbl_dedicated(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 field_elt_pow_ui(fe_ptr dst, fe_srcptr src, const block n, field_srcptr k, uint8_t stack)
Set dst <- src^n.
void field_free(field_ptr k)
Free the space of the field informations structure.
void field_find_nonsquare(fe_ptr dst, field_ptr k, uint8_t stack)
Look for a random non square element in k.
Define an elliptic curve.
void weierstrass_point_dbl_projective_dedicated(ec_point_ptr P3, ec_point_srcptr P1, ec_curve_srcptr E, uint8_t stack)
Set P3 to 2 P1 using projective coordinate.
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 weierstrass_Zjoye_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 Joye’s double-add algorithm with Co-Z addition formula.The formulae are not co...
static void field_elt_sqr(fe_ptr dst, fe_srcptr src, field_srcptr k, uint8_t stack)
Set dst <- src^2.
Primary field parameters.
Declaration of the Deterministic Random Bit Generator internal functions.
void weierstrass_point_add_ZDAU(ec_point_ptr P3, ec_point_srcptr P1, ec_point_ptr P2, ec_curve_srcptr E, uint8_t stack)
Co-Z point doubling-addition with update: Set P3 to 2*P1 + P2 and update P2 such that Z2 = Z3.
void field_elt_set_ui(fe_ptr dst, const block src, const bool isreduced, field_srcptr k, uint8_t stack)
Set dst to src, if Montgomery arithmetic is used, is_reduced == false -> transform dst into its Montg...
void weierstrass_Zmontgomery_ladder(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 ladder with Co-Z addition formula.
static void field_elt_sub(fe_ptr dst, fe_srcptr src1, fe_srcptr src2, field_srcptr k)
Set dst <- src1 - src2.
static void field_elt_dec(fe_ptr dst, fe_srcptr src, field_srcptr k)
Set dst <- src - 1.
static void field_elt_relax_pool_elt(field_elt *dst, field_ptr k, uint8_t stack)
Relax an initialised field element from the pool.
static void field_elt_add(fe_ptr dst, fe_srcptr src1, fe_srcptr src2, field_srcptr k)
Set dst <- src1 + src2.
static void field_elt_get_pool_elt(field_elt *dst, field_ptr k, uint8_t stack)
Get an initialised field element from the pool.
Quadratic extension field structure.
void number_str(char **str, number_srcptr src, const uint8_t base)
Converts src to string format in base specified by base.
void weierstrass_point_add_dedicated(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 weierstrass_curve_random_generation(fe_ptr a, fe_ptr b, char *seed_res, field_srcptr k, uint8_t stack)
Generate a 160 bits seed and coefficients a and b defining a Weiestrass elliptic curve....
void field_elt_random(fe_ptr dst, field_srcptr k, uint8_t stack)
Set dst to a random element of k, the random process is chosen at the MHELL initialisation.
void field_elt_set_number(fe_ptr dst, const bool isreduced, field_srcptr k, uint8_t stack, const uint32_t n,...)
Set dst to src, if Montgomery arithmetic is used, is_reduced == false -> transform dst into its Montg...