MPHELL  4.0.0
mphell_tuto_fp2.c
Go to the documentation of this file.
1 /*
2  MPHELL-4.0
3  Author(s): The MPHELL team
4 
5  (C) Copyright 2015-2018 - 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  /* Create base prime field of characteristic p */
36 
37  field k;
38  field_alloc(k, FP, bits_to_nblock(384), NULL);
39  number p;
40  number_init(&p, bits_to_nblock(384));
41  number_set_str(p, "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff", 16);
42  field_create(k, "", STACK_1, 1, p);
43 
44  /* Create finite field GF(p^2) of characteristic p and order p^2 */
45 
46  field_elt non_residue;
47  field_elt_alloc(&non_residue, k);
48  field_elt_init(non_residue, k);
49  /* Find an irreducible polynomial P for the test: P = X^2 - non_residue */
50  field_find_nonsquare(non_residue, k, STACK_1);
51  field k2;
52  field_alloc(k2, FP2, bits_to_nblock(384), k);
53  field_create(k2, "", STACK_1, 2, k, non_residue);
54 
55  /* Allocate element of the field k2 */
56 
57  field_elt x;
58  field_elt y;
59  field_elt res;
60  field_elt_alloc(&x, k2);
61  field_elt_init(x, k2);
62  field_elt_alloc(&y, k2);
63  field_elt_init(y, k2);
64  field_elt_alloc(&res, k2);
65  field_elt_init(res, k2);
66 
67  /* Set field element from string in base 10, which are not under Montgomery form */
68 
69  field_elt_set_str(x, "25888030203341481017599806294711784662368639501836779649649722493704107869971378062561359493103614745148646490006495,30818930178847386173826179517114528511268570512817196018110767526684990909388933012679657889738193796300356614506500", 10, false, k2, STACK_1);
70  field_elt_set_str(y, "13513975993052998194679233805431829142711099768628667018298570910541613901525492266485906595155323256712960483105824,8583076017547093038452860583029085293811168757648250649837525877560730862107937316367608198520744205561250358605819", 10, false, k2, STACK_1);
71 
72  /* Addition */
73 
74  field_elt_inc(res, x, k);
75  printf("x+1 = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
76 
77  field_elt_add(res, x, y, k);
78  printf("x+y = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
79 
80  /* Substraction */
81 
82  field_elt_dec(res, x, k);
83  printf("x-1 = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
84 
85  field_elt_sub(res, x, y, k);
86  printf("x-y = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
87 
88  /* Multiplication */
89 
90  field_elt_mul(res, x, y, k, STACK_1);
91  printf("x*y = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
92 
93  field_elt_sqr(res, x, k, STACK_1);
94  printf("x^2 = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
95 
96  /* Division */
97 
98  field_elt_div(res, x, y, k, STACK_1);
99  printf("x/y = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
100 
101  /* Power */
102 
103  field_elt_pow_ui(res, x, 3, k, STACK_1);
104  printf("x^3 = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
105  printf("Res is a power of 3 : %d\n", field_elt_ispower_ui(res, 3, k, STACK_1)>0);
106 
107  number n; number_init(&n, bits_to_nblock(192)); number_set_str(n, "f14b8dbafa22b6ba35626d40d2d00001381bef07d2c3015", 16);
108  field_elt_pow_number(res, x, n, k, STACK_1);
109  printf("x^n = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
110  printf("Res is a power of n : %d\n", field_elt_ispower_number(res, n, k, STACK_1)>0);
111  number_free(&n);
112 
113  /* Inverse */
114 
115  field_elt_inv(res, x, k, STACK_1);
116  printf("x^(-1) = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
117 
118  /* Roots */
119 
120  field_elt_sqr(x, x, k, STACK_1);
121  printf("x = "); field_elt_print(x, 16, true, k, STACK_1); printf("\n");
122  printf("x is square : %d\n", field_elt_issquare(x, k, STACK_1)>0);
123  field_elt_sqrt(res, x, k, STACK_1);
124  printf("x^(1/2) = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
125 
126  field_elt_pow_ui(x, x, 3, k, STACK_1);
127  printf("x = "); field_elt_print(x, 16, true, k, STACK_1); printf("\n");
128  printf("x is a power of 3 : %d\n", field_elt_ispower_ui(x, 3, k, STACK_1)>0);
129  field_elt_cube_root(res, x, k, STACK_1);
130  printf("x^(1/3) = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
131 
132  /* Free allocated memory */
133 
134  field_elt_free(&x, k2);
135  field_elt_free(&y, k2);
136  field_elt_free(&res, k2);
137  number_free(&p);
138  field_elt_free(&non_residue, k);
139  field_free(k);
140  field_free(k2);
141 
142  free_mphell();
143 
144  return 0;
145 }
fp_elt * field_elt
Generic field element.
Definition: mphell-field.h:39
void free_mphell()
Free MPHELL memory, especially the big amount of temporary memory.
Definition: mphell-init.c:97
void field_elt_free(fe_ptr *src, field_srcptr k)
Free space used by src.
Definition: mphell-field.c:356
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:753
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
static void field_elt_inc(fe_ptr dst, fe_srcptr src, field_srcptr k)
Set dst <- src + 1.
Definition: mphell-field.h:535
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
void field_elt_sqrt(fe_ptr dst, fe_srcptr src, field_srcptr k, uint8_t stack)
Set dst <- src^(1/2)
void field_elt_alloc(fe_ptr *dst, field_srcptr k)
Allocate space for a field element.
Definition: mphell-field.c:277
field_t field[1]
Address of a field structure.
Definition: mphell-field.h:110
void field_elt_div(fe_ptr dst, fe_srcptr src1, fe_srcptr src2, field_srcptr k, uint8_t stack)
Set dst <- src1 / src2.
void field_elt_cube_root(fe_ptr dst, fe_srcptr src, field_srcptr k, uint8_t stack)
Set dst <- src^(1/3)
void number_free(number *dst)
Free a number_ptr allocated on the RAM memory (malloc)
Definition: mphell-number.c:75
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:76
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:921
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:940
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_inv(fe_ptr dst, fe_srcptr src, field_srcptr k, uint8_t stack)
Set dst <- src^(-1)
void field_elt_init(fe_ptr dst, field_srcptr k)
Initialise the field element.
Definition: mphell-field.c:299
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:902
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
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:721
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:883
void field_free(field_ptr k)
Free the space of the field informations structure.
Definition: mphell-field.c:183
void field_find_nonsquare(fe_ptr dst, field_ptr k, uint8_t stack)
Look for a random non square element in k.
Definition: mphell-field.c:982
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:505
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:913
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:642
static void field_elt_dec(fe_ptr dst, fe_srcptr src, field_srcptr k)
Set dst <- src - 1.
Definition: mphell-field.h:615
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:562
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:961