MPHELL  5.0.0
mphell_tuto_fp.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 128 bits of security strength for the entropy, RANDOM_AES128 as DRBG and DEVURANDOM as entropy source */
32 
34 
35  /* Allocate a field of size 3*block_SIZE = 3*64 = 192 on 64 bits architecture */
36 
37  field k;
38  field_alloc(k, FP, bits_to_nblock(192), NULL);
39 
40  /* Allocate a number of size 3*block_SIZE = 3*64 = 192 on 64 bits architecture */
41 
42  number p;
43  number_init(&p, bits_to_nblock(192));
44 
45  /* Set the number p from a string in base 16 */
46 
47  number_set_str(p, "fffffffffffffffffffffffffffffffeffffffffffffffff", 16);
48 #if MPHELL_USE_AMNS == 1
49  amns AMNS;
50 #if MPHELL_USE_AMNS_32 == 0
51  amns_alloc_init_str(&AMNS, "[16, 4, [-2, 0, 0, 0, 1], 52, 6172336866035235830030737468594796900503843124202341567269, [276558595304361, 45414025950417, -34859774231597, -43697934106541], [15163021665183677629, 10328824020132901291, 3520630562803742874, 2017188797426544734], [175018852767439, 167878561785955, 39999505818260, -16528460836324], [197873551048475, 174802838528544, 29957080636796, 208984152402038]]", p);
52 #else
53  amns_alloc_init_str(&AMNS, "[1, 9, [-2, 0, 0, 0, 0, 0, 0, 0, 0, 1], 25, 778621639070749301272595529543640079614006395911121441202, [701093, -169839, 992868, -808873, -49859, -1298542, 346565, 1453220, 10927], [3371333987, 1139504849, 3063325107, 1068215102, 2225669310, 3489025068, 823765184, 3434826107, 24083075], [2398376, 1499579, -352758, -1034120, -179340, 1441412, 1982754, 1026111, 990222], [1506274, 1189710, -669416, 2638499, 1835063, 598693, 1905562, -10756, 308592]]", p);
54 #endif
55  field_set_amns(k, AMNS);
56 #endif
57 
58  /* Create the field of characteristic p */
59 
60  field_create(k, "", STACK_1, 1, p);
61 
62  /* Allocate element of the field k */
63 
64  field_elt x;
65  field_elt y;
66  field_elt res;
67  field_elt_alloc(&x, k);
68  field_elt_init(x, k);
69  field_elt_alloc(&y, k);
70  field_elt_init(y, k);
71  field_elt_alloc(&res, k);
72  field_elt_init(res, k);
73 
74  /* Set field element from string in base 16, which are not under Montgomery form */
75 
76  field_elt_set_str(x, "f14b8dbafa22b6ba35626d40d2d00001381bef07d2c3017", 16, false, k, STACK_1);
77  field_elt_set_str(y, "85ca4bcae9e156f1eab07c7b6cc500004ebbc0e5e3b237ce", 16, false, k, STACK_1);
78 
79  printf("x = "); field_elt_print(x, 16, true, k, STACK_1); printf("\n");
80  printf("y = "); field_elt_print(y, 16, true, k, STACK_1); printf("\n");
81 
82  /* Addition */
83 
84  field_elt_inc(res, x, k);
85  printf("x+1 = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
86 
87  field_elt_add(res, x, y, k);
88  printf("x+y = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
89 
90  /* Substraction */
91 
92  field_elt_dec(res, x, k);
93  printf("x-1 = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
94 
95  field_elt_sub(res, x, y, k);
96  printf("x-y = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
97 
98  /* Multiplication */
99 
100  field_elt_mul(res, x, y, k, STACK_1);
101  printf("x*y = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
102 
103  field_elt_sqr(res, x, k, STACK_1);
104  printf("x^2 = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
105 
106  /* Division */
107 
108  field_elt_div(res, x, y, k, STACK_1);
109  printf("x/y = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
110 
111  /* Power */
112 
113  field_elt_pow_ui(res, x, 3, k, STACK_1);
114  printf("x^3 = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
115  printf("Res is a power of 3 : %d\n", field_elt_ispower_ui(res, 3, k, STACK_1)>0);
116 
117  number n; number_init(&n, bits_to_nblock(192)); number_set_str(n, "f14b8dbafa22b6ba35626d40d2d00001381bef07d2c3015", 16);
118  field_elt_pow_number(res, x, n, k, STACK_1);
119  printf("x^n = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
120  printf("Res is a power of n : %d\n", field_elt_ispower_number(res, n, k, STACK_1)>0);
121  number_free(&n);
122 
123  /* Inverse */
124 
125  field_elt_inv(res, x, k, STACK_1);
126  printf("x^(-1) = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
127 
128  /* Roots */
129 
130  field_elt_sqrt(res, x, k, STACK_1);
131  printf("x is square : %d\n", field_elt_issquare(x, k, STACK_1)>0);
132  printf("x^(1/2) = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
133 
134  field_elt_cube_root(res, x, k, STACK_1);
135  printf("x is a power of 3 : %d\n", field_elt_ispower_ui(x, 3, k, STACK_1)>0);
136  printf("x^(1/3) = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
137 
138  /* Free allocated memory */
139 
140  field_elt_free(&x, k);
141  field_elt_free(&y, k);
142  field_elt_free(&res, k);
143  number_free(&p);
144  field_free(k);
145 #if MPHELL_USE_AMNS == 1
146  amns_free(&AMNS);
147 #endif
148 
149  free_mphell();
150 
151  return 0;
152 }
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
@ DEVURANDOM
void field_elt_pow_ui(fe_ptr dst, fe_srcptr src, const block n, field_srcptr k, uint8_t stack)
Set dst <- src^n.
Definition: mphell-field.c:894
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_print(fe_srcptr src, const uint8_t base, const bool lift, field_srcptr k, uint8_t stack)
Print src in base specified by base.
Definition: mphell-field.c:732
void field_elt_sqrt(fe_ptr dst, fe_srcptr src, field_srcptr k, uint8_t stack)
Set dst <- src^(1/2)
void field_elt_pow_number(fe_ptr dst, fe_srcptr src, number_srcptr n, field_srcptr k, uint8_t stack)
Set dst <- src^n.
Definition: mphell-field.c:913
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
bool field_elt_issquare(fe_srcptr src, field_srcptr k, uint8_t stack)
Test if src is a square using the Lengendre symbol.
Definition: mphell-field.c:932
void field_elt_cube_root(fe_ptr dst, fe_srcptr src, field_srcptr k, uint8_t stack)
Set dst <- src^(1/3)
void field_elt_inv(fe_ptr dst, fe_srcptr src, field_srcptr k, uint8_t stack)
Set dst <- src^(-1)
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
int8_t field_elt_ispower_number(fe_srcptr src, number_srcptr n, field_srcptr k, uint8_t stack)
Test if src is a n-power in src->k.
Definition: mphell-field.c:972
void field_elt_div(fe_ptr dst, fe_srcptr src1, fe_srcptr src2, field_srcptr k, uint8_t stack)
Set dst <- src1 / src2.
int8_t field_elt_ispower_ui(fe_srcptr src, const block n, field_srcptr k, uint8_t stack)
Test if src is a n-power in src->k.
Definition: mphell-field.c:951
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
static void field_elt_add(fe_ptr dst, fe_srcptr src1, fe_srcptr src2, field_srcptr k)
Set dst <- src1 + src2.
Definition: mphell-field.h:558
static void field_elt_dec(fe_ptr dst, fe_srcptr src, field_srcptr k)
Set dst <- src - 1.
Definition: mphell-field.h:611
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
static void field_elt_inc(fe_ptr dst, fe_srcptr src, field_srcptr k)
Set dst <- src + 1.
Definition: mphell-field.h:531
static void field_elt_sub(fe_ptr dst, fe_srcptr src1, fe_srcptr src2, field_srcptr k)
Set dst <- src1 - src2.
Definition: mphell-field.h:638
static void field_elt_mul(fe_ptr dst, fe_srcptr src1, fe_srcptr src2, field_srcptr k, uint8_t stack)
Set dst <- src1 * src2, if Montgomery arithmetic is used, the Montgomery multiplication will be used ...
Definition: mphell-field.h:749
static void field_elt_sqr(fe_ptr dst, fe_srcptr src, field_srcptr k, uint8_t stack)
Set dst <- src^2.
Definition: mphell-field.h:909
@ 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