MPHELL  5.0.0
mphell-curve.h
Go to the documentation of this file.
1 /*
2  MPHELL-5.0
3  Author(s): The MPHELL team
4 
5  (C) Copyright 2015-2021 - 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  ED_448
92 };
93 
99 
104 struct ec_point
105 {
110 };
111 
116 typedef struct ec_point ec_point_t;
117 
122 typedef ec_point_t ec_point[1];
123 
129 
134 typedef const ec_point_t * ec_point_srcptr;
135 
140 struct ec_curve
141 {
142  char *id_curve;
144  number q;
149  number n;
150  number h;
153  bool ec_spec1;
154 #if MPHELL_USE_AMNS == 1
155  amns * AMNS;
156 #endif
157 };
158 
163 typedef struct ec_curve ec_curve_t;
164 
169 typedef ec_curve_t ec_curve[1];
170 
176 
181 typedef const ec_curve_t * ec_curve_srcptr;
182 
183 
184 /**********************************WEIESTRASS*****************************/
185 
186 #include "mphell-weierstrass.h"
187 
188 /********************************JACOBI QUARTIC***************************/
189 
190 #include "mphell-jacobi.h"
191 
192 /***********************************EDWARDS*******************************/
193 
194 #include "mphell-edwards.h"
195 
196 /*************************************TMP*********************************/
197 
205 static inline void
207 {
208  field_elt_get_pool_elt(&(P->x), k, stack);
209  field_elt_get_pool_elt(&(P->y), k, stack);
210  field_elt_get_pool_elt(&(P->z), k, stack);
211  field_elt_get_pool_elt(&(P->t), k, stack);
212 }
213 
221 static inline void
223 {
224  field_elt_relax_pool_elt(&(P->x), k, stack);
225  field_elt_relax_pool_elt(&(P->y), k, stack);
226  field_elt_relax_pool_elt(&(P->z), k, stack);
227  field_elt_relax_pool_elt(&(P->t), k, stack);
228 }
229 
230 /************************************SETTERS**********************************/
231 
232 
239 void
241 
248 void
250 
266 void
268  fe_srcptr b, ec_point_srcptr G, number_srcptr h, number_srcptr n,
269  const ec_type type, const ec_formula f, uint8_t stack);
270 
280 void
281 ec_use_curve (ec_curve_ptr E, field_ptr k, const ec_known_curve id_curve, const ec_formula f, uint8_t stack);
282 
293 void
294 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);
295 
305 bool
306 ec_verify_random_generation(ec_curve_ptr E, const char * seed, uint8_t stack);
307 
313 void
315 
321 void
323 
330 void
331 ec_compute_disc (ec_curve_ptr E, uint8_t stack);
332 
339 void
340 ec_test_spec (ec_curve_ptr E, uint8_t stack);
341 
348 void
350 
356 void
358 
364 void
366 
373 void
375 
382 void
384 
391 void
393 
400 void
402 
410 void
412 
423 void
426 
440 void
441 ec_point_set_str (ec_point_ptr P, const char *str_x, const char *str_y,
442  const char *str_z, const char *str_t, const bool is_reduced,
443  const uint8_t base, field_srcptr k, uint8_t stack);
444 
454 void
456  const ec_type type, field_srcptr k, uint8_t stack);
457 
470 void
471 ec_point_set_aff_str (ec_point_ptr P, const char *str_x, const char *str_y,
472  const bool is_reduced, const uint8_t base, const ec_type type,
473  field_srcptr k, uint8_t stack);
474 
482 void
483 ec_point_set_neutral (ec_point_ptr dst, ec_curve_srcptr E, uint8_t stack);
484 
492 void
493 ec_point_norm (ec_point_ptr P, ec_curve_srcptr E, uint8_t stack);
494 
503 void
505 
514 void
516 
524 void
525 ec_point_lift (ec_point_ptr P, field_srcptr k, uint8_t stack);
526 
535 bool
536 ec_belongs (ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack);
537 
545 void
546 ec_point_random (ec_point_ptr P, ec_curve_srcptr E, uint8_t stack);
547 
556 void
557 ec_curve_str (char **str, ec_curve_srcptr E, const uint8_t base, uint8_t stack);
558 
566 void
567 ec_curve_print (ec_curve_srcptr E, const uint8_t base, uint8_t stack);
568 
579 void
580 ec_point_str (char **str, ec_point_srcptr P, const uint8_t base,
581  const bool lift, field_srcptr k, uint8_t stack);
582 
592 void
593 ec_point_print (ec_point_srcptr P, const uint8_t base, const bool lift, field_srcptr k, uint8_t stack);
594 
595 
596 /*******************************COMPARISON************************************/
597 
598 
607 bool
609 
619 bool
620 ec_point_are_equal (ec_point_srcptr P1, ec_point_srcptr P2, const ec_curve_srcptr E, uint8_t stack);
621 
622 
623 /*******************************OPERATIONS************************************/
624 
632 void
634 
635 /* UNIFIED => Resistant to SPA */
636 
646 void
648  ec_curve_srcptr E, uint8_t stack);
649 
658 void
660 
670 void
672  ec_curve_srcptr E, uint8_t stack);
673 
684 void
685 ec_point_mul_unified (ec_point_ptr P3, number_srcptr n, ec_point_srcptr P1,
686  ec_curve_srcptr E, uint8_t stack);
687 
688 
689 /* DEDICATED => Not resistant to SPA, but faster */
690 
700 void
702  ec_curve_srcptr E, uint8_t stack);
703 
712 void
713 ec_point_dbl (ec_point_ptr P3, ec_point_srcptr P1, ec_curve_srcptr E, uint8_t stack);
714 
724 void
726  ec_curve_srcptr E, uint8_t stack);
727 
737 void
738 ec_point_mul (ec_point_ptr P3, number_srcptr n, ec_point_srcptr P1,
739  ec_curve_srcptr E, uint8_t stack);
740 
751 void
752 ec_point_mul_with_precomp (ec_point_ptr P3, number_srcptr n, ec_point * tab_P1,
753  int16_t window_size, ec_curve_srcptr E, uint8_t stack);
754 
767 void
768 ec_point_2mul_with_precomp (ec_point_ptr P3, number_srcptr n1, ec_point * tab_P1,
769  number_srcptr n2, ec_point * tab_P2,
770  int16_t win_size, ec_curve_srcptr E, uint8_t stack);
771 
772 /***********************************CONVERSION********************************/
773 
781 void
783 
792 void
794  ec_curve_srcptr E, uint8_t stack);
795 
805 void
806 weierstrass_to_jacobi_quartic (ec_curve_ptr E_res, ec_curve_srcptr E, const bool determined, fe_ptr teta, uint8_t stack);
807 
816 void
818  ec_curve_srcptr E, uint8_t stack);
819 
827 void
828 edwards_to_weierstrass (ec_curve_ptr E_res, ec_curve_srcptr E, uint8_t stack);
829 
838 void
840  ec_curve_srcptr E, uint8_t stack);
841 
852 void
853 weierstrass_to_edwards (ec_curve_ptr E_res, ec_curve_srcptr E, const uint8_t n,
854  fe_ptr alpha, fe_ptr beta, uint8_t stack);
855 
867 void
869  ec_curve_srcptr E, const uint8_t n, fe_ptr alpha, fe_ptr beta, uint8_t stack);
870 
871 
872 #endif
873 
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_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:972
enum ec_algo_enum ec_formula
Define the type of coordinate.
Definition: mphell-curve.h:68
void ec_curve_print(ec_curve_srcptr E, const uint8_t base, uint8_t stack)
Print a description of E.
void ec_set_fast_unified_coordinates(ec_curve_ptr E)
Set the fastest unified coordinates system.
Definition: mphell-curve.c:703
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.
ec_known_curve_enum
Define the hardcoded curves.
Definition: mphell-curve.h:75
@ NIST_192
Definition: mphell-curve.h:76
@ BRAINPOOL_256
Definition: mphell-curve.h:84
@ NIST_384
Definition: mphell-curve.h:79
@ BRAINPOOL_160
Definition: mphell-curve.h:81
@ FR_256
Definition: mphell-curve.h:88
@ BRAINPOOL_512
Definition: mphell-curve.h:87
@ JQ_256_3
Definition: mphell-curve.h:89
@ NIST_521
Definition: mphell-curve.h:80
@ ED_448
Definition: mphell-curve.h:91
@ BRAINPOOL_320
Definition: mphell-curve.h:85
@ BRAINPOOL_224
Definition: mphell-curve.h:83
@ NIST_256
Definition: mphell-curve.h:78
@ NIST_224
Definition: mphell-curve.h:77
@ ED_25519
Definition: mphell-curve.h:90
@ BRAINPOOL_384
Definition: mphell-curve.h:86
@ BRAINPOOL_192
Definition: mphell-curve.h:82
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.
void ec_point_neg(ec_point_ptr P3, ec_point_srcptr P1, ec_curve_srcptr E)
Set P3 to -P1.
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:206
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:893
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_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:798
void ec_compute_disc(ec_curve_ptr E, uint8_t stack)
Set the discriminant of E.
Definition: mphell-curve.c:738
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:655
ec_point_t * ec_point_ptr
Define ec_point_ptr, use in all functions.
Definition: mphell-curve.h:128
ec_curve_t * ec_curve_ptr
Define ec_curve_ptr, use in all functions.
Definition: mphell-curve.h:175
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:65
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_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:882
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.
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 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 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_set_neutral(ec_point_ptr dst, ec_curve_srcptr E, uint8_t stack)
Set dst to the neutral element.
Definition: mphell-curve.c:934
void ec_set_fast_dedicated_coordinates(ec_curve_ptr E)
Set the fastest dedicated coordinates system.
Definition: mphell-curve.c:721
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_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)
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.
Definition: mphell-curve.c:809
void ec_point_copy(ec_point_ptr P3, ec_point_srcptr P, field_srcptr k)
Copy P into P3.
Definition: mphell-curve.c:866
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:874
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:991
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...
void ec_init(ec_curve_ptr E, field_srcptr k)
Initialise a curve.
Definition: mphell-curve.c:55
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_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...
const ec_curve_t * ec_curve_srcptr
Define ec_curve_srcptr, use in all functions where the parameter is const.
Definition: mphell-curve.h:181
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 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_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.
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:913
void ec_point_init(ec_point_ptr P, field_srcptr k)
Initialise an elliptic curve point.
Definition: mphell-curve.c:842
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:850
void ec_point_lift(ec_point_ptr P, field_srcptr k, uint8_t stack)
Lift the coordinates from Montgomery basis to classical arithmetic.
ec_type_enum
Define the type of curve.
Definition: mphell-curve.h:40
@ EDWARDS
Definition: mphell-curve.h:42
@ WEIERSTRASS
Definition: mphell-curve.h:41
@ JACOBI_QUARTIC
Definition: mphell-curve.h:43
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:683
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)
void ec_point_free(ec_point_ptr P, field_srcptr k)
Free the point P.
Definition: mphell-curve.c:858
enum ec_type_enum ec_type
Define the type of curve.
Definition: mphell-curve.h:50
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.
const ec_point_t * ec_point_srcptr
Define ec_point_srcptr, use in all functions where the parameter is const.
Definition: mphell-curve.h:134
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_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:953
void ec_point_alloc(ec_point_ptr P, field_srcptr k)
Allocate an elliptic curve point.
Definition: mphell-curve.c:834
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:117
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:756
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:222
void ec_alloc(ec_curve_ptr E, field_srcptr k)
Allocate a curve.
Definition: mphell-curve.c:37
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 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:774
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)
ec_algo_enum
Define the type of coordinate.
Definition: mphell-curve.h:57
@ EXTENDED_HOMOGENEOUS_PROJECTIVE
Definition: mphell-curve.h:60
@ PROJECTIVE
Definition: mphell-curve.h:58
@ JACOBIAN
Definition: mphell-curve.h:59
@ EXTENDED_EDWARDS
Definition: mphell-curve.h:61
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...
enum ec_known_curve_enum ec_known_curve
Define the hardcoded curves.
Definition: mphell-curve.h:98
Declaration of Edward ECC functions.
Declaration of field functions.
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:143
const fp_elt * fe_srcptr
Pointer on a field element, the field element cannot be modified through this pointer.
Definition: mphell-field.h:49
fp_elt * field_elt
Generic field element.
Definition: mphell-field.h:37
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:110
fp_elt * fe_ptr
Pointer on a field element.
Definition: mphell-field.h:43
Declaration of Jacobi ECC functions.
Declaration of Weierstrass ECC function.
Define a AMNS.
Definition: mphell-amns.h:81
Define an elliptic curve.
Definition: mphell-curve.h:141
field_ptr k
Definition: mphell-curve.h:143
bool ec_spec1
Definition: mphell-curve.h:153
ec_point G
Definition: mphell-curve.h:148
field_elt D
Definition: mphell-curve.h:152
field_elt b
Definition: mphell-curve.h:145
field_elt disc
Definition: mphell-curve.h:151
char * id_curve
Definition: mphell-curve.h:142
number q
Definition: mphell-curve.h:144
number n
Definition: mphell-curve.h:149
ec_formula f
Definition: mphell-curve.h:147
ec_type type
Definition: mphell-curve.h:146
number h
Definition: mphell-curve.h:150
Define an elliptic curve point.
Definition: mphell-curve.h:105
field_elt z
Definition: mphell-curve.h:108
field_elt t
Definition: mphell-curve.h:109
field_elt x
Definition: mphell-curve.h:106
field_elt y
Definition: mphell-curve.h:107
Define a field.
Definition: mphell-field.h:67