MPHELL  5.0.0
mphell_tuto_fp3.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  /* 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, "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB71123ACD3A729901D1A71874700133107EC53", 16);
42 #if MPHELL_USE_AMNS == 1
43  amns AMNS;
44 #if MPHELL_USE_AMNS_32 == 0
45  amns_alloc_init_str(&AMNS, "[0, 7, [-2, 0, 0, 0, 0, 0, 0, 1], 59, 11122730497867082426703774898586389214554972136239004234162613722782469803511232111483936084892743587563706779788925, [7572289551659433, 15029753698333892, 12945988611366992, -17184105187674967, 9595663517139077, -11302621560098544, -12294469334304406], [10169066017504681843, 3575077594573780678, 3538935777114687514, 16507089961979300095, 7926629081983887831, 690069536102011560, 17699199433799716401], [13128632887084164, 13283202763506961, -37474906482124134, -5757219201359127, -520497147653064, -3514330879039906, -12308865400691268], [17553533491086361, 656897356505228, -11894435724207870, 9591856635824350, -9034393502820346, -15955756988989613, -11492482547612475]]", p);
46 #else
47  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, 15833037756866545055998898291141368998274581393034077040975259164919709950904103228613975659981455240604797564049604, [932701, 433354, 867148, 1376016, -820303, -127687, 1155700, 1112779, 307627, 503910, 1088277, 41202, -266900, 206613, 227662, -577070, -62320, -607037], [2442378721, 774711420, 4166835354, 1313207720, 2634336855, 1889308819, 3146940992, 2759526369, 1546604778, 1846306862, 164979678, 1925121308, 2536898293, 3015550174, 1088438470, 2878856050, 78410923, 4145047564], [-5386846, -3868366, -5501843, -2198805, -732487, -1478087, -1222878, 2358562, 3373754, 2302470, 5171783, 6870841, 4415644, 5731905, 6871422, 4159301, 4308858, 5069638], [-4109309, -1222785, 359256, -975605, -1639851, 50057, 767582, 929099, 4170484, 3642339, 3392628, 3999507, 4783865, 2419694, 3913793, 3635972, 2217139, 1649766]]", p);
48 #endif
49  field_set_amns(k, AMNS);
50 #endif
51  field_create(k, "", STACK_1, 1, p);
52 
53  /* Create finite field GF(p^3) of characteristic p and order p^3 */
54 
55  field_elt non_residue;
56  field_elt_alloc(&non_residue, k);
57  field_elt_init(non_residue, k);
58  /* Find an irreducible polynomial P for the test: P = X^3 - non_residue */
59  printf("Find a non cube element in k : %d\n", field_find_nonpower_ui(non_residue, 3, k, STACK_1));
60  field k3;
61  field_alloc(k3, FP3, bits_to_nblock(384), k);
62  field_create(k3, "", STACK_1, 2, k, non_residue);
63 
64  /* Allocate element of the field k2 */
65 
66  field_elt x;
67  field_elt y;
68  field_elt res;
69  field_elt_alloc(&x, k3);
70  field_elt_init(x, k3);
71  field_elt_alloc(&y, k3);
72  field_elt_init(y, k3);
73  field_elt_alloc(&res, k3);
74  field_elt_init(res, k3);
75 
76  /* Set field element from string in base 10, which are not under Montgomery form */
77 
78  field_elt_set_str(x, "17533160620960747720658538821194093754122675622398475560762133052267072585559185687559064016240090086860997857609409,6500634679458907167017356094867727707826954836406037804481994709882642877127237360869164454175728327118666667333650,11742847707788052444522195706157812780258053127409735266675287415078850342578399252850159043780127493098080513256739", 10, false, k3, STACK_1);
79  field_elt_set_str(y, "4126110149158568452410698021138511225673440764619173039319485451554017348466776134677497966604444001579710560363922,15158636090660409006051880747464877271969161550611610795599623793938447056898724461367397528668805761322041750639681,9916423062331263728547041136174792199538063259607913333406331088742239591447562569386402939064406595342627904716592", 10, false, k3, STACK_1);
80 
81  /* Addition */
82 
83  field_elt_inc(res, x, k);
84  printf("x+1 = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
85 
86  field_elt_add(res, x, y, k);
87  printf("x+y = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
88 
89  /* Substraction */
90 
91  field_elt_dec(res, x, k);
92  printf("x-1 = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
93 
94  field_elt_sub(res, x, y, k);
95  printf("x-y = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
96 
97  /* Multiplication */
98 
99  field_elt_mul(res, x, y, k, STACK_1);
100  printf("x*y = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
101 
102  field_elt_sqr(res, x, k, STACK_1);
103  printf("x^2 = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
104  printf("res is square : %d\n", field_elt_issquare(res, k, STACK_1)>0);
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_sqr(x, x, k, STACK_1);
131  printf("x = "); field_elt_print(x, 16, true, k, STACK_1); printf("\n");
132  printf("x is square : %d\n", field_elt_issquare(x, k, STACK_1)>0);
133  field_elt_sqrt(res, x, k, STACK_1);
134  printf("x^(1/2) = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
135 
136  field_elt_pow_ui(x, x, 3, k, STACK_1);
137  printf("x = "); field_elt_print(x, 16, true, k, STACK_1); printf("\n");
138  printf("x is a power of 3 : %d\n", field_elt_ispower_ui(x, 3, k, STACK_1)>0);
139  field_elt_cube_root(res, x, k, STACK_1);
140  printf("x^(1/3) = "); field_elt_print(res, 16, true, k, STACK_1); printf("\n");
141 
142  /* Free allocated memory */
143 
144  field_elt_free(&x, k3);
145  field_elt_free(&y, k3);
146  field_elt_free(&res, k3);
147  number_free(&p);
148  field_elt_free(&non_residue, k);
149  field_free(k);
150 #if MPHELL_USE_AMNS == 1
151  amns_free(&AMNS);
152 #endif
153  field_free(k3);
154 
155  free_mphell();
156 
157  return 0;
158 }
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
bool field_find_nonpower_ui(fe_ptr dst, const block n, field_ptr k, uint8_t stack)
Look for a random non n-power element in k.
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
@ FP3
Definition: mphell-field.h:59
@ 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