MPHELL  4.0.0
mphell-curve.h
Go to the documentation of this file.
1 /*
2  MPHELL-4.0
3  Author(s): The MPHELL team
4 
5  (C) Copyright 2015-2018 - Institut Fourier / Univ. Grenoble Alpes (France)
6 
7  This file is part of the MPHELL Library.
8  MPHELL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation, version 3 of the License.
11 
12  MPHELL is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with MPHELL. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
26 #ifndef MPHELL_CURVE_H
27 #define MPHELL_CURVE_H
28 
29 #include "mphell-field.h"
30 
31 
32 /**************************************DATA STRUCTURE**********************************/
33 
34 
40 {
44 };
45 
50 typedef enum ec_type_enum ec_type;
51 
57 {
62 };
63 
68 typedef enum ec_algo_enum ec_formula;
69 
75 {
91 };
92 
98 
103 struct ec_point
104 {
109 };
110 
115 typedef struct ec_point ec_point_t;
116 
121 typedef ec_point_t ec_point[1];
122 
128 
133 typedef const ec_point_t * ec_point_srcptr;
134 
139 struct ec_curve
140 {
141  char *id_curve;
143  number q;
148  number n;
149  number h;
152  bool ec_spec1;
153 };
154 
159 typedef struct ec_curve ec_curve_t;
160 
165 typedef ec_curve_t ec_curve[1];
166 
172 
177 typedef const ec_curve_t * ec_curve_srcptr;
178 
179 
180 /**********************************WEIESTRASS*****************************/
181 
182 #include "mphell-weierstrass.h"
183 
184 /********************************JACOBI QUARTIC***************************/
185 
186 #include "mphell-jacobi.h"
187 
188 /***********************************EDWARDS*******************************/
189 
190 #include "mphell-edwards.h"
191 
192 /*************************************TMP*********************************/
193 
201 static inline void
203 {
204  field_elt_get_pool_elt(&(P->x), k, stack);
205  field_elt_get_pool_elt(&(P->y), k, stack);
206  field_elt_get_pool_elt(&(P->z), k, stack);
207  field_elt_get_pool_elt(&(P->t), k, stack);
208 }
209 
217 static inline void
219 {
220  field_elt_relax_pool_elt(&(P->x), k, stack);
221  field_elt_relax_pool_elt(&(P->y), k, stack);
222  field_elt_relax_pool_elt(&(P->z), k, stack);
223  field_elt_relax_pool_elt(&(P->t), k, stack);
224 }
225 
226 /************************************SETTERS**********************************/
227 
228 
235 void
237 
244 void
246 
262 void
264  fe_srcptr b, ec_point_srcptr G, number_srcptr h, number_srcptr n,
265  const ec_type type, const ec_formula f, uint8_t stack);
266 
276 void
277 ec_use_curve (ec_curve_ptr E, field_ptr k, const ec_known_curve id_curve, const ec_formula f, uint8_t stack);
278 
289 void
290 ec_random(ec_curve_ptr E, const char *id_curve, char * seed_res, field_srcptr k, const ec_type type, const ec_formula f, uint8_t stack);
291 
301 bool
302 ec_verify_random_generation(ec_curve_ptr E, const char * seed, uint8_t stack);
303 
309 void
311 
317 void
319 
326 void
327 ec_compute_disc (ec_curve_ptr E, uint8_t stack);
328 
335 void
336 ec_test_spec (ec_curve_ptr E, uint8_t stack);
337 
344 void
346 
352 void
354 
360 void
362 
369 void
371 
378 void
380 
387 void
389 
396 void
398 
406 void
408 
419 void
422 
436 void
437 ec_point_set_str (ec_point_ptr P, const char *str_x, const char *str_y,
438  const char *str_z, const char *str_t, const bool is_reduced,
439  const uint8_t base, field_srcptr k, uint8_t stack);
440 
450 void
452  const ec_type type, field_srcptr k, uint8_t stack);
453 
466 void
467 ec_point_set_aff_str (ec_point_ptr P, const char *str_x, const char *str_y,
468  const bool is_reduced, const uint8_t base, const ec_type type,
469  field_srcptr k, uint8_t stack);
470 
478 void
479 ec_point_set_neutral (ec_point_ptr dst, ec_curve_srcptr E, uint8_t stack);
480 
488 void
489 ec_point_norm (ec_point_ptr P, ec_curve_srcptr E, uint8_t stack);
490 
499 void
501 
510 void
512 
520 void
521 ec_point_lift (ec_point_ptr P, field_srcptr k, uint8_t stack);
522 
531 bool
532 ec_belongs (ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack);
533 
541 void
542 ec_point_random (ec_point_ptr P, ec_curve_srcptr E, uint8_t stack);
543 
552 void
553 ec_curve_str (char **str, ec_curve_srcptr E, const uint8_t base, uint8_t stack);
554 
562 void
563 ec_curve_print (ec_curve_srcptr E, const uint8_t base, uint8_t stack);
564 
575 void
576 ec_point_str (char **str, ec_point_srcptr P, const uint8_t base,
577  const bool lift, field_srcptr k, uint8_t stack);
578 
588 void
589 ec_point_print (ec_point_srcptr P, const uint8_t base, const bool lift, field_srcptr k, uint8_t stack);
590 
591 
592 /*******************************COMPARISON************************************/
593 
594 
603 bool
605 
615 bool
616 ec_point_are_equal (ec_point_srcptr P1, ec_point_srcptr P2, const ec_curve_srcptr E, uint8_t stack);
617 
618 
619 /*******************************OPERATIONS************************************/
620 
628 void
630 
631 /* UNIFIED => Resistant to SPA */
632 
642 void
644  ec_curve_srcptr E, uint8_t stack);
645 
654 void
656 
666 void
668  ec_curve_srcptr E, uint8_t stack);
669 
679 void
680 ec_point_mul_unified (ec_point_ptr P3, number_srcptr n, ec_point_srcptr P1,
681  ec_curve_srcptr E, uint8_t stack);
682 
683 
684 /* DEDICATED => Not resistant to SPA, but faster, execpt Montgomery and joye which are anyhow faster and unified */
685 
695 void
697  ec_curve_srcptr E, uint8_t stack);
698 
707 void
708 ec_point_dbl (ec_point_ptr P3, ec_point_srcptr P1, ec_curve_srcptr E, uint8_t stack);
709 
719 void
721  ec_curve_srcptr E, uint8_t stack);
722 
732 void
733 ec_point_mul (ec_point_ptr P3, number_srcptr n, ec_point_srcptr P1,
734  ec_curve_srcptr E, uint8_t stack);
735 
746 void
747 ec_point_mul_with_precomp (ec_point_ptr P3, number_srcptr n, ec_point * tab_P1,
748  int16_t window_size, ec_curve_srcptr E, uint8_t stack);
749 
762 void
763 ec_point_2mul_with_precomp (ec_point_ptr P3, number_srcptr n1, ec_point * tab_P1,
764  number_srcptr n2, ec_point * tab_P2,
765  int16_t win_size, ec_curve_srcptr E, uint8_t stack);
766 
767 /***********************************CONVERSION********************************/
768 
776 void
778 
787 void
789  ec_curve_srcptr E, uint8_t stack);
790 
800 void
801 weierstrass_to_jacobi_quartic (ec_curve_ptr E_res, ec_curve_srcptr E, const bool determined, fe_ptr teta, uint8_t stack);
802 
811 void
813  ec_curve_srcptr E, uint8_t stack);
814 
822 void
823 edwards_to_weierstrass (ec_curve_ptr E_res, ec_curve_srcptr E, uint8_t stack);
824 
833 void
835  ec_curve_srcptr E, uint8_t stack);
836 
847 void
848 weierstrass_to_edwards (ec_curve_ptr E_res, ec_curve_srcptr E, const uint8_t n,
849  fe_ptr alpha, fe_ptr beta, uint8_t stack);
850 
862 void
864  ec_curve_srcptr E, const uint8_t n, fe_ptr alpha, fe_ptr beta, uint8_t stack);
865 
866 
867 #endif
868 
fp_elt * field_elt
Generic field element.
Definition: mphell-field.h:39
field_elt y
Definition: mphell-curve.h:106
Declaration of Jacobi ECC functions.
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...
const ec_point_t * ec_point_srcptr
Define ec_point_srcptr, use in all functions where the parameter is const.
Definition: mphell-curve.h:133
Declaration of field functions.
ec_type type
Definition: mphell-curve.h:145
number q
Definition: mphell-curve.h:143
void ec_point_set_aff(ec_point_ptr P, fe_srcptr x, fe_srcptr y, const ec_type type, field_srcptr k, uint8_t stack)
Set a point from its affine coordinates.
Definition: mphell-curve.c:739
Define a field.
Definition: mphell-field.h:90
Define an elliptic curve point.
Definition: mphell-curve.h:103
ec_known_curve_enum
Define the hardcoded curves.
Definition: mphell-curve.h:74
void ec_set_fast_unified_coordinates(ec_curve_ptr E)
Set the fastest unified coordinates system.
Definition: mphell-curve.c:544
void ec_clear(ec_curve_ptr E)
Clear the elliptic curve E (remove the action of ec_init, but let the one of ec_alloc)
Definition: mphell-curve.c:643
bool ec_point_is_neutral(ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Test if P is the neutral element.
void ec_point_set(ec_point_ptr P, fe_srcptr x, fe_srcptr y, fe_srcptr z, fe_srcptr t, field_srcptr k)
Set a point from its coordinates.
Definition: mphell-curve.c:718
bool ec_spec1
Definition: mphell-curve.h:152
number h
Definition: mphell-curve.h:149
void ec_point_sub_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)
field_elt t
Definition: mphell-curve.h:108
void ec_point_alloc(ec_point_ptr P, field_srcptr k)
Allocate an elliptic curve point.
Definition: mphell-curve.c:673
ec_algo_enum
Define the type of coordinate.
Definition: mphell-curve.h:56
ec_formula f
Definition: mphell-curve.h:146
void ec_point_set_str(ec_point_ptr P, const char *str_x, const char *str_y, const char *str_z, const char *str_t, const bool is_reduced, const uint8_t base, field_srcptr k, uint8_t stack)
Set a point from its coordinates under string format.
Definition: mphell-curve.c:728
bool ec_point_are_equal(ec_point_srcptr P1, ec_point_srcptr P2, const ec_curve_srcptr E, uint8_t stack)
Test if P1 and P2 are equal on E.
field_elt b
Definition: mphell-curve.h:144
void ec_point_set_neutral(ec_point_ptr dst, ec_curve_srcptr E, uint8_t stack)
Set dst to the neutral element.
Definition: mphell-curve.c:780
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.
Definition: mphell-curve.c:759
void jacobi_quartic_point_to_weierstrass_point(ec_point_ptr dst, ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Convert a point on a jacobi quartic elliptic curve to a point on a weierstrass elliptic curve.
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.
bool ec_verify_random_generation(ec_curve_ptr E, const char *seed, uint8_t stack)
Test if E if generated from the seed "seed", using algorithm A.3.4.2 from ANS X9.62-1998....
Definition: mphell-curve.c:523
field_elt disc
Definition: mphell-curve.h:150
void ec_random(ec_curve_ptr E, const char *id_curve, char *seed_res, field_srcptr k, const ec_type type, const ec_formula f, uint8_t stack)
Create a random elliptic curve E, not cryptographically secure (not tested to be)....
Definition: mphell-curve.c:495
static void ec_point_relax_pool_elt(ec_point_ptr P, field_ptr k, uint8_t stack)
Relax an initialised point from the pool.
Definition: mphell-curve.h:218
void ec_point_2mul_with_precomp(ec_point_ptr P3, number_srcptr n1, ec_point *tab_P1, number_srcptr n2, ec_point *tab_P2, int16_t win_size, ec_curve_srcptr E, uint8_t stack)
Set P3 to n1 * P1 + n2 * P2 using 2 precomputated array.
void ec_curve_print(ec_curve_srcptr E, const uint8_t base, uint8_t stack)
Print a description of E.
void weierstrass_point_to_jacobi_quartic_point(ec_point_ptr dst, ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Convert a point on a weierstrass elliptic curve to a point on a jacobi quartic elliptic curve.
void ec_point_init(ec_point_ptr P, field_srcptr k)
Initialise an elliptic curve point.
Definition: mphell-curve.c:682
void weierstrass_to_jacobi_quartic(ec_curve_ptr E_res, ec_curve_srcptr E, const bool determined, fe_ptr teta, uint8_t stack)
Convert a Weierstrass elliptic curve into a jacobi quartic elliptic 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)
static void ec_point_get_pool_elt(ec_point_ptr P, field_ptr k, uint8_t stack)
Get an initialised point from the pool.
Definition: mphell-curve.h:202
void ec_point_clear(ec_point_ptr P, field_srcptr k)
Clear the point P (remove the action of ec_point_init, but let the one of ec_point_alloc)
Definition: mphell-curve.c:691
void weierstrass_to_edwards(ec_curve_ptr E_res, ec_curve_srcptr E, const uint8_t n, fe_ptr alpha, fe_ptr beta, uint8_t stack)
Convert the Weierstrass elliptic curve E to the one of the corresponding Edwards elliptic curve accor...
char * id_curve
Definition: mphell-curve.h:141
void ec_point_dbl_unified(ec_point_ptr P3, ec_point_srcptr P1, ec_curve_srcptr E, uint8_t stack)
Set P3 to 2*P1, using unified formulae (protection against SPA)
const fp_elt * fe_srcptr
Pointer on a field element, the field element cannot be modified through this pointer.
Definition: mphell-field.h:51
void ec_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.
Definition: mphell-curve.c:818
field_elt z
Definition: mphell-curve.h:107
void ec_point_lift(ec_point_ptr P, field_srcptr k, uint8_t stack)
Lift the coordinates from Montgomery basis to classical arithmetic.
Definition: mphell-curve.c:856
number n
Definition: mphell-curve.h:148
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)
Definition: mphell-curve.c:62
void ec_point_copy(ec_point_ptr P3, ec_point_srcptr P, field_srcptr k)
Copy P into P3.
Definition: mphell-curve.c:709
void ec_point_norm(ec_point_ptr P, ec_curve_srcptr E, uint8_t stack)
Convert a point in projective or jacobian coordinate to an affine point (x,y)
Definition: mphell-curve.c:799
enum ec_algo_enum ec_formula
Define the type of coordinate.
Definition: mphell-curve.h:68
fp_elt * fe_ptr
Pointer on a field element.
Definition: mphell-field.h:45
void ec_point_str(char **str, ec_point_srcptr P, const uint8_t base, const bool lift, field_srcptr k, uint8_t stack)
Allocate *str and write in it the description of P.
field_elt x
Definition: mphell-curve.h:105
ec_type_enum
Define the type of curve.
Definition: mphell-curve.h:39
void ec_copy(ec_curve_ptr E_res, ec_curve_srcptr E)
Copy E into E_res which has been previously allocated, beware: the same field is used,...
Definition: mphell-curve.c:618
ec_curve_t * ec_curve_ptr
Define ec_curve_ptr, use in all functions.
Definition: mphell-curve.h:171
void ec_free(ec_curve_ptr E)
Free the elliptic curve E.
Definition: mphell-curve.c:655
ec_point G
Definition: mphell-curve.h:147
void ec_point_mul_with_precomp(ec_point_ptr P3, number_srcptr n, ec_point *tab_P1, int16_t window_size, ec_curve_srcptr E, uint8_t stack)
Set P3 to n * P1 using naive double and add method, Montgomery and Joye multiplication are also avail...
void ec_test_spec(ec_curve_ptr E, uint8_t stack)
Set the E->ec_spec1 to true if E->a = -3 mod p, false otherwise.
Definition: mphell-curve.c:599
void weierstrass_point_to_edwards_point(ec_point_ptr dst, ec_point_srcptr P, ec_curve_srcptr E, const uint8_t n, fe_ptr alpha, fe_ptr beta, uint8_t stack)
Convert the point of the Weierstrass elliptic curve E to the elliptic curve of one of the correspondi...
void ec_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 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)
enum ec_type_enum ec_type
Define the type of curve.
Definition: mphell-curve.h:50
field_ptr k
Definition: mphell-curve.h:142
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 ec_point_neg(ec_point_ptr P3, ec_point_srcptr P1, ec_curve_srcptr E)
Set P3 to -P1.
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.
Definition: mphell-curve.c:885
ec_point_t * ec_point_ptr
Define ec_point_ptr, use in all functions.
Definition: mphell-curve.h:127
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_curve_str(char **str, ec_curve_srcptr E, const uint8_t base, uint8_t stack)
Allocate *str and write in it the description of E.
Definition: mphell-curve.c:932
Define an elliptic curve.
Definition: mphell-curve.h:139
void ec_point_free(ec_point_ptr P, field_srcptr k)
Free the point P.
Definition: mphell-curve.c:700
void ec_alloc(ec_curve_ptr E, field_srcptr k)
Allocate a curve.
Definition: mphell-curve.c:37
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.
Declaration of Weierstrass ECC function.
const ec_curve_t * ec_curve_srcptr
Define ec_curve_srcptr, use in all functions where the parameter is const.
Definition: mphell-curve.h:177
enum ec_known_curve_enum ec_known_curve
Define the hardcoded curves.
Definition: mphell-curve.h:97
void ec_compute_disc(ec_curve_ptr E, uint8_t stack)
Set the discriminant of E.
Definition: mphell-curve.c:580
bool ec_belongs(ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Test if P belongs to E.
Definition: mphell-curve.c:865
void ec_set_fast_dedicated_coordinates(ec_curve_ptr E)
Set the fastest dedicated coordinates system.
Definition: mphell-curve.c:562
void jacobi_quartic_to_weierstrass(ec_curve_ptr E_res, ec_curve_srcptr E, uint8_t stack)
Convert a jacobi quartic elliptic curve into a Weierstrass elliptic curve.
Declaration of Edward ECC functions.
void ec_init(ec_curve_ptr E, field_srcptr k)
Initialise a curve.
Definition: mphell-curve.c:52
void ec_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.
Definition: mphell-curve.c:837
void ec_use_curve(ec_curve_ptr E, field_ptr k, const ec_known_curve id_curve, const ec_formula f, uint8_t stack)
Create the elliptic curve (and the associated base field) id_curve, the curve and the field must me a...
Definition: mphell-curve.c:114
static void field_elt_relax_pool_elt(field_elt *dst, field_ptr k, uint8_t stack)
Relax an initialised field element from the pool.
Definition: mphell-field.h:167
void ec_point_mul_unified(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...
static void field_elt_get_pool_elt(field_elt *dst, field_ptr k, uint8_t stack)
Get an initialised field element from the pool.
Definition: mphell-field.h:134
field_elt D
Definition: mphell-curve.h:151