MPHELL  4.0.0
mphell_tuto_edwards.c
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 #include <stdio.h>
27 #include "mphell/mphell.h"
28 
29 int main()
30 {
31  /* Initialise MPHELL with 256 bits of security strength for the entropy, RANDOM_AES256 as DRBG and DEVURANDOM as entropy source */
32 
34 
35  /* Allocate a field of size 4*block_SIZE = 4*64 = 256 on 64 bits architecture */
36 
37  field k;
38  field_alloc(k, FP, bits_to_nblock(256), NULL);
39 
40  /* Allocate a number of size 4*block_SIZE = 4*64 = 256 on 64 bits architecture */
41 
42  number p;
43  number_init(&p, bits_to_nblock(256));
44 
45  /* Set the number p from a string in base 16 */
46 
47  number_set_str(p, "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed", 16);
48 
49  /* Create the field of characteristic p */
50 
51  field_create(k, "", STACK_1, 1, p);
52 
53  /* Allocate curve */
54 
55  ec_curve E;
56  ec_alloc (E, k);
57  ec_init(E, k);
58 
59  /* Create curve */
60 
61  field_elt a, b;
62  ec_point G;
63  number n, h;
64 
65  field_elt_alloc(&a, k);
66  field_elt_init(a, k);
67  field_elt_alloc(&b, k);
68  field_elt_init(b, k);
69  ec_point_alloc(G, k);
70  ec_point_init(G, k);
71  number_init(&n, bits_to_nblock(256));
72  number_init(&h, bits_to_nblock(256));
73 
74  field_elt_set_one(a, k);
75  field_elt_set_str(b, "2dfc9311d490018c7338bf8688861767ff8ff5b2bebe27548a14b235eca6874a", 16, false, k, STACK_1);
76 
77  ec_point_set_aff_str(G, "159a6849e44c3c7f061b3d570fc4ed5b5d14c8ba4253df49cc7edf80f533ad9b", "6666666666666666666666666666666666666666666666666666666666666658", false, 16, EDWARDS, k, STACK_1);
78  number_set_str(h, "0000000000000000000000000000000000000000000000000000000000000008", 16);
79  number_set_str(n, "1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed", 16);
80 
81  ec_create (E, "Edwards_test", k, a, b, G, h, n, EDWARDS, EXTENDED_EDWARDS, STACK_1);
82 
83  printf("E: \n"); ec_curve_print(E, 16, STACK_1); printf("\n");
84 
85  field_elt_free(&a, k);
86  field_elt_free(&b, k);
87  ec_point_free(G, k);
88  number_free(&n);
89  number_free(&h);
90 
91  /* Allocate element of the field k */
92 
93  ec_point x;
94  ec_point y;
95  ec_point res;
96  ec_point_alloc(x, k);
97  ec_point_init(x, k);
98  ec_point_alloc(y, k);
99  ec_point_init(y, k);
100  ec_point_alloc(res, k);
101  ec_point_init(res, k);
102 
103  /* Set field element from string in base 16, which are not under Montgomery form */
104 
105  ec_point_set_aff_str(x, "67e82d6ecfec167788bc76ffc0165be5b3b5ac785f5bd1d2c427fa5c4ef02548", "5f6b282c46a137a1d612df50487cfa27a4b4df2c77bf6f12e56ec667ee37b181", false, 16, EDWARDS, k, STACK_1);
106  ec_point_set_aff_str(y, "1ec6d017b0d11bd1d1b5cfefebfccbc8c8079e0f1030008a2e75207b77a987e6", "20bbd47f960781e9e13ea7cff81a4f5bec6106453cbae87abd3018d7803539be", false, 16, EDWARDS, k, STACK_1);
107 
108  /* Addition */
109 
110  ec_point_add(res, x, y, E, STACK_1);
111  printf("x+y = "); ec_point_print(res, 16, true, k, STACK_1); printf("\n");
112  printf("x+y belongs to E : %d\n\n", ec_belongs(res, E, STACK_1));
113 
114  /* Substraction */
115 
116  ec_point_sub(res, x, y, E, STACK_1);
117  printf("x-y = "); ec_point_print(res, 16, true, k, STACK_1); printf("\n");
118  printf("x-y belongs to E : %d\n\n", ec_belongs(res, E, STACK_1));
119 
120  /* Doubling */
121 
122  ec_point_dbl(res, x, E, STACK_1);
123  printf("x*2 = "); ec_point_print(res, 16, true, k, STACK_1); printf("\n");
124  printf("x*2 belongs to E : %d\n\n", ec_belongs(res, E, STACK_1));
125 
126  /* Negation */
127 
128  ec_point_neg(res, x, E);
129  printf("-x = "); ec_point_print(res, 16, true, k, STACK_1); printf("\n");
130  printf("-x belongs to E : %d\n\n", ec_belongs(res, E, STACK_1));
131 
132  /* Scalar Multiplication */
133 
134  number n1;
135  number_init(&n1, 4);
136  number_set_str(n1, "67e82d6ecfec167788bc76ffc0165be5b3b5ac785f5bd1d2c427fa5c4ef02551", 16);
137  ec_point_mul(res, n1, x, E, STACK_1);
138  printf("x*67e82d6ecfec167788bc76ffc0165be5b3b5ac785f5bd1d2c427fa5c4ef02551 = "); ec_point_print(res, 16, true, k, STACK_1); printf("\n");
139  printf("x*67e82d6ecfec167788bc76ffc0165be5b3b5ac785f5bd1d2c427fa5c4ef02551 belongs to E : %d\n\n", ec_belongs(res, E, STACK_1));
140  number_free(&n1);
141 
142  /* Random point */
143 
144  ec_point_random(res, E, STACK_1);
145  printf("random point, res = "); ec_point_print(res, 16, true, k, STACK_1); printf("\n");
146  printf("res belongs to E : %d\n\n", ec_belongs(res, E, STACK_1));
147 
148  /* Conversion to a point on a Weierstrass curve */
149 
150  /* Allocation of memory for the destination point */
151 
152  ec_point convP;
153  ec_point_alloc(convP, k);
154  ec_point_init(convP, k);
155 
156  edwards_point_to_weierstrass_point (convP, res, E, STACK_1);
157  printf("the image point on a weierstrass curve, convP="); ec_point_print(convP, 16, true, k, STACK_1); printf("\n");
158 
159  /* Conversion to a Weierstrass curve */
160 
161  /* Allocation of memory for the destination point */
162 
163  ec_curve convC;
164  ec_alloc (convC, k);
165  ec_init (convC, k);
166 
167  edwards_to_weierstrass(convC, E, STACK_1);
168  printf("convC: \n"); ec_curve_print(convC, 16, STACK_1); printf("\n");
169 
170  /* Free allocated memory */
171 
172  ec_point_free(convP, k);
173  ec_point_free(x, k);
174  ec_point_free(y, k);
175  ec_point_free(res, k);
176  number_free(&p);
177  field_free(k);
178  ec_free(E);
179  ec_free(convC);
180 
181  free_mphell();
182 
183  return 0;
184 }
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
fp_elt * field_elt
Generic field element.
Definition: mphell-field.h:39
void free_mphell()
Free MPHELL memory, especially the big amount of temporary memory.
Definition: mphell-init.c:97
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 field_elt_free(fe_ptr *src, field_srcptr k)
Free space used by src.
Definition: mphell-field.c:356
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).
Definition: mphell-init.c:35
Define an elliptic curve point.
Definition: mphell-curve.h:103
void ec_point_alloc(ec_point_ptr P, field_srcptr k)
Allocate an elliptic curve point.
Definition: mphell-curve.c:673
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_neg(ec_point_ptr P3, ec_point_srcptr P1, ec_curve_srcptr E)
Set P3 to -P1.
void ec_init(ec_curve_ptr E, field_srcptr k)
Initialise a curve.
Definition: mphell-curve.c:52
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.
uint8_t bits_to_nblock(const uint16_t nbits)
Return the number of blocks required to store a nbits number.
Definition: mphell-util.c:29
void field_elt_alloc(fe_ptr *dst, field_srcptr k)
Allocate space for a field element.
Definition: mphell-field.c:277
void ec_point_init(ec_point_ptr P, field_srcptr k)
Initialise an elliptic curve point.
Definition: mphell-curve.c:682
field_t field[1]
Address of a field structure.
Definition: mphell-field.h:110
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 number_free(number *dst)
Free a number_ptr allocated on the RAM memory (malloc)
Definition: mphell-number.c:75
void ec_point_free(ec_point_ptr P, field_srcptr k)
Free the point P.
Definition: mphell-curve.c:700
void ec_free(ec_curve_ptr E)
Free the elliptic curve E.
Definition: mphell-curve.c:655
void field_elt_set_one(fe_ptr dst, field_srcptr k)
Set dst to one (or its Montgomery form if Montgomery arithmetic is used)
Definition: mphell-field.c:376
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.
Definition: mphell-field.c:76
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 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_alloc(ec_curve_ptr E, field_srcptr k)
Allocate a curve.
Definition: mphell-curve.c:37
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_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.
Definition: mphell-field.c:37
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 field_elt_init(fe_ptr dst, field_srcptr k)
Initialise the field element.
Definition: mphell-field.c:299
void ec_curve_print(ec_curve_srcptr E, const uint8_t base, uint8_t stack)
Print a description of E.
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)
Definition: mphell-number.c:59
void field_free(field_ptr k)
Free the space of the field informations structure.
Definition: mphell-field.c:183
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
Define an elliptic curve.
Definition: mphell-curve.h:139
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...
Definition: mphell-field.c:505
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)