MPHELL  5.0.0
mphell_tuto_ec_random.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 "mphell/mphell.h"
27 #include <stdio.h>
28 #include <string.h>
29 
30 int main()
31 {
33 
34  int i, j;
35  bool res1;
36 
37  number p, n;
38  field k;
39  ec_curve E;
40 
41  number_init(&p, bits_to_nblock(512));
42  number_init(&n, bits_to_nblock(512));
43  field_alloc(k, FP, bits_to_nblock(512), NULL);
44 
45  number_set_str(p, "6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042159", 10);
46 #if MPHELL_USE_AMNS == 1
47  amns AMNS;
48 #if MPHELL_USE_AMNS_32 == 0
49  amns_alloc_init_str(&AMNS, "[1, 10, [-2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 56, 1949715550477596391050085143019979962187489833398677588465030396092261681514158064546239921492428732105309264987401712740590694494340370264447759306218043, [-845528290199225, 1497961916346049, 90585915898951, 820213644784137, -418821346875421, 109771977934600, 1417978576852651, -962942853221725, 143096186561835, -1324914973060058], [15983654131394969033, 16968083590411639579, 11782429094846756482, 11205845458815222214, 17093288067982085116, 9998295128409825969, 11270860305329337426, 2425357736791511401, 8703551856315982458, 8445990296066650717], [-127069546631968, -166989335710652, -2445954811583878, 49017698439948, 227449256453923, 141269326656102, 1701918410502233, -1073389722232402, 563013565593825, -217007402518282], [1899551367165673, -804402297003831, -99557494915669, 1068669298035161, -1015237632853644, 1181005277717623, -1774109424859449, -574005976420006, 529912473712413, -494747968884078]]", p);
50 #else
51  amns_alloc_init_str(&AMNS, "[0, 25, [-2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 25, 5272013709936016948593560360247807441965478824314811527282560785564390252940232029521554688907682841600720826163445829871273796278697033507587796526749811, [403175, 106687, -564204, -559311, 227005, -201089, 159568, -207716, 189216, 473331, -643646, 268885, -114862, 458206, 253944, 451925, 136939, -161289, -616347, 169765, 41552, 534117, -226111, -574653, -172568], [1143524063, 3060431233, 1471859933, 2062471496, 365278526, 1627772417, 1441512363, 4213903656, 1884167813, 1257312250, 696377799, 2267257753, 4181862705, 3615529898, 2596401023, 148574974, 2138936575, 2937846772, 3262652758, 3270552461, 2820818494, 1359648921, 546172945, 1534080154, 2030909667], [717930, 1018381, 497620, -1177464, 506923, 1417195, 1126168, -20875, -1566829, -22859, -397311, 634966, -193429, -575208, -1608771, -807302, -955671, 973637, -809281, -1397935, -794421, 349799, -101423, 22396, -643027], [-675578, -1661259, 959538, 1399909, 444505, -1696167, -427423, 2030238, -196183, 81582, -1615037, -304743, 248531, -205239, 264770, -188737, -1490392, -1705651, -1043280, 1956742, 44353, -1233696, -1377288, 62848, 1193153]]", p);
52 #endif
53  field_set_amns(k, AMNS);
54 #endif
55 
56  field_create(k, "k_512", STACK_1, 1, p);
57 
58  ec_alloc(E, k);
59  ec_init(E, k);
60 
61  char seed[41];
62 
63  char bad_seed[41]="ffffffffffffffffffffffffffffffffffffffff";
64 
65  /* Create random Weietrass curve and test them */
66  ec_random(E, "randomCurveWeiertrass", seed, k, WEIERSTRASS, PROJECTIVE, STACK_1);
67 
68  res1 = ec_verify_random_generation(E, seed, STACK_1);
69 
70  if(res1==true)
71  {
72  printf("E: \n"); ec_curve_print(E, 16, STACK_1); printf("\n");
73  printf("Weietrass: E was generated from seed:%s\n", seed);
74  }
75  else
76  {
77  printf("Problem of creation of the random weierstrass curve \n");
78  }
79 
80  number_free(&p);
81  number_free(&n);
82  field_free(k);
83 #if MPHELL_USE_AMNS == 1
84  amns_free(&AMNS);
85 #endif
86  ec_free(E);
87  free_mphell();
88 
89 }
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_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
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_alloc(ec_curve_ptr E, field_srcptr k)
Allocate a curve.
Definition: mphell-curve.c:37
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
@ WEIERSTRASS
Definition: mphell-curve.h:41
@ PROJECTIVE
Definition: mphell-curve.h:58
@ DEVURANDOM
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_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
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_AES128
Definition: mphell-random.h:37
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
Define a AMNS.
Definition: mphell-amns.h:81
Define an elliptic curve.
Definition: mphell-curve.h:141