MPHELL  5.0.0
mphell_tuto_conversion_jacobi_quartic_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 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 temorary memory */
41 
42  number p;
43  field_elt a, b;
44  ec_point G, res, res_jq;
45  number n, h;
46 
47  number_init(&p, bits_to_nblock(384));
48 
49  /* Set the number p from a string in base 16 */
50 
51  number_set_str(p, "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff", 16);
52 #if MPHELL_USE_AMNS == 1
53  amns AMNS;
54 #if MPHELL_USE_AMNS_32 == 0
55  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);
56 #else
57  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);
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_jq, k);
69  ec_point_init(res_jq, 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(384));
77  number_init(&h, bits_to_nblock(384));
78 
79 
80 
81  field_elt_set_str(a, "a3aeb56058fa59051926a1559fe934bf64545ee7dee231a8d65e7211702ac9bf9801cfdae54ad7a050e20e612d4c080", 16, false, k, STACK_1);
82  field_elt_set_str(b, "1", 16, false, k, STACK_1);
83  ec_point_set_str(G, "1541e4e6fec9431b561ad9d815306961cbda834477c2884b93bb5fb3a4c865d1638d6b1e1eb5d91afea443da57ec3c95", "e55245714090febf2f55c3dee76267c5aa27edb22a95ffa0328adecc2690ffef38c5cb63423401492f8e75bd4753f0b6","1","a7c8a5c9364fb8f2142801d10e8321c885c9fe7b88a7c76eabc474f635b090ac8001e9efcfc308ff06960a29869ebf8", false, 16, k, STACK_1);
84  number_set_str(h, "15ad3c423d6df1572016199efaeee94ca5242a87a4", 16);
85  number_set_str(n, "bcf569c46c00fa8b5f4c7d41b55e14295468dac4097918b25e24499", 16);
86 
87  /* Allocate curves */
88 
89  ec_curve E, E_jq;
90  ec_alloc (E, k);
91  ec_init(E, k);
92  ec_alloc (E_jq, k);
93  ec_init(E_jq, k);
94 
95  /* Create curve */
96 
97  ec_create (E_jq, "RandP384JQ", k, a, b, G, h, n, JACOBI_QUARTIC, PROJECTIVE, STACK_1);
98 
99  printf("The Jacobi Quartic departing curve \nE_jq: \n"); ec_curve_print(E_jq, 16, STACK_1); printf("\n");
100 
101  /* Creation of a random point on the Jacobi Quartic elliptic curve */
102 
103  jacobi_quartic_point_random(res_jq, E_jq, STACK_1);
104  printf("random point, res_jq = "); ec_point_print(res_jq, 16, true, k, STACK_1); printf("\n");
105  printf("res_jq belongs to E_jq : %d\n\n", jacobi_quartic_belongs(res_jq, E_jq, STACK_1));
106 
107  /* Conversion to the Weierstrass Elliptic curve */
108 
109  jacobi_quartic_to_weierstrass(E, E_jq, STACK_1);
110 
111  printf("The Weierstrass curve isomorphic to E_jq \n");
112  printf("E: \n"); ec_curve_print(E, 16, STACK_1); printf("\n");
113 
114  /* Conversion of a point on a Weierstrass Elliptic curve*/
115 
116  jacobi_quartic_point_to_weierstrass_point (res, res_jq, E_jq, STACK_1);
117 
118  printf("Weierstrass random point, res = "); ec_point_print(res, 16, true, k, STACK_1); printf("\n");
119  printf("res belongs to E : %d\n\n", weierstrass_belongs(res, E, STACK_1));
120 
121  /* Free allocated memory */
122 
123  field_elt_free(&a, k);
124  field_elt_free(&b, k);
125  ec_point_free(G, k);
126  number_free(&n);
127  number_free(&h);
128  ec_point_free(res, k);
129  ec_point_free(res_jq, k);
130  number_free(&p);
131 #if MPHELL_USE_AMNS == 1
132  amns_free(&AMNS);
133 #endif
134  field_free(k);
135  ec_free(E_jq);
136  ec_free(E);
137 
138  free_mphell();
139 
140  return 0;
141 }
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 jacobi_quartic_point_to_weierstrass_point(ec_point_ptr dst, ec_point_srcptr P1, 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 ec_curve_print(ec_curve_srcptr E, const uint8_t base, uint8_t stack)
Print a description of E.
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.
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 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 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_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
@ JACOBI_QUARTIC
Definition: mphell-curve.h:43
@ 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 jacobi_quartic_point_random(ec_point_ptr P, ec_curve_srcptr E, uint8_t stack)
Create a random point P on the elliptic curve 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.
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