MPHELL  5.0.0
mphell_tuto_conversion_edwards_to_weierstrass.c
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 #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 temporary memory */
41 
42  number p;
43  field_elt a, b;
44  ec_point G, res, res_ed;
45  number n, h;
46 
47  number_init(&p, bits_to_nblock(256));
48 
49  /* Set the number p from a string in base 16 */
50 
51  number_set_str(p, "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed", 16);
52 #if MPHELL_USE_AMNS == 1
53  amns AMNS;
54 #if MPHELL_USE_AMNS_32 == 0
55  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);
56 #else
57  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);
58 #endif
59  field_set_amns(k, AMNS);
60 #endif
61 
62  /* Create the field of characteristic p */
63 
64  field_create(k, "", STACK_1, 1, p);
65 
66  ec_point_alloc(res, k);
67  ec_point_init(res, k);
68  ec_point_alloc(res_ed, k);
69  ec_point_init(res_ed, k);
70  field_elt_alloc(&a, k);
71  field_elt_init(a, k);
72  field_elt_alloc(&b, k);
73  field_elt_init(b, k);
74  ec_point_alloc(G, k);
75  ec_point_init(G, k);
76  number_init(&n, bits_to_nblock(256));
77  number_init(&h, bits_to_nblock(256));
78 
79  field_elt_set_str(a, "178bccfe3009fb7a6adc25a5981cc87fcb86f5d89097a446249d137d8676d379", 16, false, k, STACK_1);
80  field_elt_set_str(b, "55f4c3f0ed6ed8e9501feca7866d4e8d70d9960f148207bf7d618f19d7fe4169", 16, false, k, STACK_1);
81  ec_point_set_aff_str(G, "460ed8dffbd2fc64813be575cc7034ba059bfb51e03357653b07c4f6b00f8859", "361e0dc66944371de8e1860980b5c14414d5bf32c315a5a5374e38f87126c0c1", false, 16, WEIERSTRASS, k, STACK_1);
82  number_set_str(h, "1", 16);
83  number_set_str(n, "1", 16);
84 
85  /* Allocate curves */
86 
87  ec_curve E, E_ed;
88  ec_alloc(E, k);
89  ec_init(E, k);
90  ec_alloc(E_ed, k);
91  ec_init(E_ed, k);
92 
93  /* Create curve */
94 
95  ec_create (E_ed, "RandP25519_rank2_Edwards", k, a, b, G, h, n, EDWARDS, EXTENDED_EDWARDS, STACK_1);
96 
97  printf("The edwards departing curve \nE_ed: \n"); ec_curve_print(E_ed, 16, STACK_1); printf("\n");
98 
99  /* Creation of a random point on the elliptic curve */
100 
101  edwards_point_random(res_ed, E_ed, STACK_1);
102  printf("random point, res_ed = "); ec_point_print(res_ed, 16, true, k, STACK_1); printf("\n");
103  printf("res_ed belongs to E_ed : %d\n\n", edwards_belongs(res_ed, E_ed, STACK_1));
104 
105  /* Conversion to the isomorphic weierstrass curve */
106 
107  edwards_to_weierstrass(E, E_ed, STACK_1); /* Here there is only one isomorphic weierstrass curve */
108 
109  printf("The Weierstrass curve isomorphic to E_ed calculated \n");
110  printf("E: \n"); ec_curve_print(E, 16, STACK_1); printf("\n");
111 
112  /* Conversion of a point on a Weierstrass Elliptic curve */
113 
114  edwards_point_to_weierstrass_point (res, res_ed, E_ed, STACK_1);
115  printf("Weierstrass random point, res = "); ec_point_print(res, 16, true, k, STACK_1); printf("\n");
116  printf("res belongs to E : %d\n\n", weierstrass_belongs(res, E, STACK_1));
117 
118  /* Free allocated memory */
119 
120  field_elt_free(&a, k);
121  field_elt_free(&b, k);
122  ec_point_free(G, k);
123  number_free(&n);
124  number_free(&h);
125  ec_point_free(res, k);
126  ec_point_free(res_ed, k);
127  number_free(&p);
128  field_free(k);
129 #if MPHELL_USE_AMNS == 1
130  amns_free(&AMNS);
131 #endif
132  ec_free(E_ed);
133  ec_free(E);
134 
135  free_mphell();
136 
137  return 0;
138 }
void amns_free(amns_ptr *AMNS)
Free the amns system.
Definition: mphell-amns.c:444
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...
Definition: mphell-amns.c:1242
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)
Definition: mphell-curve.c:65
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_free(ec_curve_ptr E)
Free the elliptic curve E.
Definition: mphell-curve.c:809
void ec_init(ec_curve_ptr E, field_srcptr k)
Initialise a curve.
Definition: mphell-curve.c:55
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_free(ec_point_ptr P, field_srcptr k)
Free the point P.
Definition: mphell-curve.c:858
void ec_point_alloc(ec_point_ptr P, field_srcptr k)
Allocate an elliptic curve point.
Definition: mphell-curve.c:834
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...
@ EDWARDS
Definition: mphell-curve.h:42
@ WEIERSTRASS
Definition: mphell-curve.h:41
@ EXTENDED_EDWARDS
Definition: mphell-curve.h:61
void edwards_point_random(ec_point_ptr P, ec_curve_srcptr E, uint8_t stack)
Set P to a random point on E.
bool edwards_belongs(ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Test if P belongs to E.
@ DEVURANDOM
void field_elt_free(fe_ptr *src, field_srcptr k)
Free space used by src.
Definition: mphell-field.c:348
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 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:516
void field_elt_init(fe_ptr dst, field_srcptr k)
Initialise the field element.
Definition: mphell-field.c:291
void field_elt_alloc(fe_ptr *dst, field_srcptr k)
Allocate space for a field element.
Definition: mphell-field.c:269
void field_free(field_ptr k)
Free the space of the field informations structure.
Definition: mphell-field.c:194
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:87
field_t field[1]
Address of a field structure.
Definition: mphell-field.h:86
fp_elt * field_elt
Generic field element.
Definition: mphell-field.h:37
@ FP
Definition: mphell-field.h:57
void free_mphell()
Free MPHELL memory, especially the big amount of temporary memory.
Definition: mphell-init.c:97
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
void number_free(number *dst)
Free a number_ptr allocated on the RAM memory (malloc)
Definition: mphell-number.c:75
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
@ RANDOM_AES256
Definition: mphell-random.h:39
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
bool weierstrass_belongs(ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Test if P belongs to E.
Define a AMNS.
Definition: mphell-amns.h:81
Define an elliptic curve.
Definition: mphell-curve.h:141
Define an elliptic curve point.
Definition: mphell-curve.h:105