MPHELL  5.0.0
mphell_tuto_conversion_weierstrass_to_jacobi_quartic.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 6*block_SIZE = 6*64 = 384 on 64 bits architecture */
36 
37  field k;
38  field_alloc(k, FP, bits_to_nblock(384), NULL);
39 
40  /* Allocate temporary memory */
41 
42  number p;
43  field_elt a, b;
44  ec_point G, res, res_jq;
45  number n, h;
46  field_elt teta;
47 
48  number_init(&p, bits_to_nblock(384));
49 
50  /* Set the number p from a string in base 16 */
51 
52  number_set_str(p, "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff", 16);
53 #if MPHELL_USE_AMNS == 1
54  amns AMNS;
55 #if MPHELL_USE_AMNS_32 == 0
56  amns_alloc_init_str(&AMNS, "[0, 7, [-2, 0, 0, 0, 0, 0, 0, 1], 59, 8010427042960414838884725536003025378504357914088932113335334539926383793809451548018236227209625700052722361381087, [-8576931003528147, 12963241998501462, -11685779024318585, 12198217901242760, 5469056521909520, -16952339206744690, -7801587214757611], [936600828956320511, 17864700281010200544, 7457034104920941375, 4906978974859566336, 1521310033682434027, 11977106235113612084, 6613199331606154624], [-48661600919137196, -16406260014740521, -9043881017757559, -15840327204754192, -4503903233538141, -10931797952018020, 3933578076642458], [-10700965224081570, -31747207631521220, -21134995866173053, -3985617456248275, -932744058138743, -25427794319760237, -2489368063126092]]", p);
57 #else
58  amns_alloc_init_str(&AMNS, "[0, 18, [-2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 26, 28816340648335589686749390875939188706868735452517709312097740458242732054770156839198296353641579295263032233716200, [-51353, 1463368, 1158979, 544183, -726450, 976213, 144497, -886193, -264198, 688818, 1070182, -170086, 864472, -144241, -360825, -510521, 1017098, -59072], [3993315613, 3730700630, 867896355, 1312786307, 471958917, 4205419201, 2925107995, 1257333470, 1009072108, 3882493844, 334530385, 923966745, 2062755457, 1054721524, 1062123069, 2491481070, 813468425, 1351925765], [1499105, 1805368, 3513276, 2380622, 4551927, 3187369, 3810913, 2129326, 3493684, 1909307, 1981401, 2013148, 1633911, 2996996, 2975679, 3661385, 2455278, 1909836], [5854047, 6627253, 2246346, 3861327, 2251173, 2334257, 1163302, 3551220, 4498564, 4312125, 4221299, 3184071, 2952719, 307544, 1633107, 2468475, 2132615, 2940733]]", p);
59 #endif
60  field_set_amns(k, AMNS);
61 #endif
62 
63  /* Create the field of characteristic p */
64 
65  field_create(k, "", STACK_1, 1, p);
66 
67  ec_point_alloc(res, k);
68  ec_point_init(res, k);
69  ec_point_alloc(res_jq, k);
70  ec_point_init(res_jq, k);
71 
72  field_elt_alloc(&a, k);
73  field_elt_init(a, k);
74  field_elt_alloc(&b, k);
75  field_elt_init(b, k);
76  ec_point_alloc(G, k);
77  ec_point_init(G, k);
78  number_init(&n, bits_to_nblock(384));
79  number_init(&h, bits_to_nblock(384));
80  field_elt_alloc(&teta, k);
81  field_elt_init(teta, k);
82 
83  field_elt_set_str(a, "e6d121fc8cfc85448e51524d40b0a3f6146006a7e2001270385be91a918ba0c2066c8ae2615a9bf927b73c9756bce6b2", 16, false, k, STACK_1);
84  field_elt_set_str(b, "a36e0555338282573d94a77351044dfad63d30aac6505bcd470ad021e3a4b057d81fc21d6c92ef0190798b386c38f9ba", 16, false, k, STACK_1);
85  ec_point_set_aff_str(G, "a3f0d2b9cbda17b9201391877f744cbc69f7efe29f56e2552439582e70238edbdb70193133bae1d9a8b659d2d7692931", "c467049b488e283fdf78669237ca9387b347f63d17ce4fac5e6bbe737b4d8e306dfcb80eee28477ab6a0e06f262e47d2", false, 16, WEIERSTRASS, k, STACK_1);
86  number_set_str(h, "15ad3c423d6df1572016199efaeee94ca5242a87a4", 16);
87  number_set_str(n, "bcf569c46c00fa8b5f4c7d41b55e14295468dac4097918b25e24499", 16);
88 
89  /* Allocate curves */
90 
91  ec_curve E, E_jq;
92  ec_alloc (E, k);
93  ec_init(E, k);
94  ec_alloc (E_jq, k);
95  ec_init(E_jq, k);
96 
97  /* Create curve */
98 
99  ec_create(E, "RandP384", k, a, b, G, h, n, WEIERSTRASS, PROJECTIVE, STACK_1);
100 
101  printf("The weierstrass departing curve \nE: \n"); ec_curve_print(E, 16, STACK_1); printf("\n");
102 
103  /* Creation of a random point on the weierstras elliptic curve */
104 
105  weierstrass_point_random(res, E, STACK_1);
106  printf("random point, res = "); ec_point_print(res, 16, true, k, STACK_1); printf("\n");
107  printf("res belongs to E : %d\n\n", weierstrass_belongs(res, E, STACK_1));
108 
109  /* Conversion to one (specific) of the possible Jacobi_Quartic curve */
110 
111  weierstrass_to_jacobi_quartic(E_jq, E, false, teta, STACK_1);
112  /* Here false signifies that teta is not specified and that the function have to find the isomorphism for which b_j=1 */
113 
114  printf("The Jacobi Quartic curve isomorphic to E calculated with teta not specified \n");
115  printf("E_jq: \n"); ec_curve_print(E_jq, 16, STACK_1); printf("\n");
116 
117  weierstrass_to_jacobi_quartic(E_jq, E, true, teta, STACK_1);
118  /* Here true signifies that teta is specified */
119 
120  printf("The Jacobi Quartic curve calculated with teta specified \n");
121  printf("E_jq: \n"); ec_curve_print(E_jq, 16, STACK_1); printf("\n");
122 
123  /* Conversion of a point on an Jacobi Quartic Elliptic curve*/
124 
125  weierstrass_point_to_jacobi_quartic_point(res_jq, res, E_jq, STACK_1);
126  printf("Jacobi Quartic random point, res_jq = "); ec_point_print(res_jq, 16, true, k, STACK_1); printf("\n");
127  printf("res_jq belongs to E_jq : %d\n\n", jacobi_quartic_belongs(res_jq, E_jq, STACK_1));
128 
129  ec_free(E_jq);
130 
131  /* Free allocated memory */
132 
133  field_elt_free(&teta, k);
134  field_elt_free(&a, k);
135  field_elt_free(&b, k);
136  ec_point_free(G, k);
137  number_free(&n);
138  number_free(&h);
139  ec_point_free(res, k);
140  ec_point_free(res_jq, k);
141  number_free(&p);
142  field_free(k);
143 #if MPHELL_USE_AMNS == 1
144  amns_free(&AMNS);
145 #endif
146  ec_free(E);
147 
148  free_mphell();
149 
150  return 0;
151 }
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 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_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 weierstrass_point_to_jacobi_quartic_point(ec_point_ptr dst, ec_point_srcptr P1, 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_alloc(ec_curve_ptr E, field_srcptr k)
Allocate a curve.
Definition: mphell-curve.c:37
@ WEIERSTRASS
Definition: mphell-curve.h:41
@ PROJECTIVE
Definition: mphell-curve.h:58
@ 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
bool jacobi_quartic_belongs(ec_point_srcptr P, ec_curve_srcptr E, uint8_t stack)
Test if P belongs to E.
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.
void weierstrass_point_random(ec_point_ptr P, ec_curve_srcptr E, uint8_t stack)
Set P to a random point on 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