-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffstools.cpp
339 lines (282 loc) · 5.94 KB
/
ffstools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include"ffstools.h"
//Set pol to t^i
void GF2X_set_ti(GF2X& pol,long i)
{
GF2X f;
set(f);
f=LeftShift(f,i);
pol=f;
f.kill();
}
void GF2X_submul(GF2X& f,GF2X& g,GF2X& h)
{
GF2X t;
t=g*h;
f=f-t;
t.kill();
}
//GF2X to hex
void GF2X_to_hex(GF2X f)
{
ofstream outfile;
outfile.open("date.txt",ios::app);
char hex[100];
long i=1, j, temp;
long d=deg(f);
long index=0;
f=reverse(f);
ZZ decimal=ZZ(0);
while(index<=d)
{
ZZ tmp=(power2_ZZ(index)*(ZZ)rep(f[index]));
decimal=decimal+tmp;
index++;
}
while(decimal!=ZZ(0))
{
temp = decimal % long(16);
if( temp < 10)
{
temp = temp + 48;
}
else
{
temp = temp + 55;
}
hex[i++]= temp;
decimal = decimal / long(16);
}
for(j=i-1 ;j>0;j--)
{
outfile<<hex[j];
}
}
uint64_t power(long i)
{
uint64_t num=1;
for(long j=0;j<i;j++)
num=num*2;
return num;
}
//Conversion from GF2X to uint64_t
uint64_t GF2X_to_uint64_t(GF2X& f)
{
uint64_t num=0;
long d=deg(f);
long i=0;
uint64_t base=0;
GF2 ith_bit;
while(i<=d)
{
ith_bit=coeff(f,i);
if(IsOne(ith_bit))
{
base=power(i);
num=num+base;
}
i++;
}
return num;
}
uint32_t power_uint32_t(long i)
{
uint32_t num=1;
for(long j=0;j<i;j++)
num=num*2;
return num;
}
//Conversion from GF2X to uint32_t
uint32_t GF2X_to_uint32_t(GF2X& f)
{
uint32_t num=0;
long d=deg(f);
long i=0;
uint32_t base=0;
GF2 ith_bit;
while(i<=d)
{
ith_bit=coeff(f,i);
if(IsOne(ith_bit))
{
base=power_uint32_t(i);
num=num+base;
}
i++;
}
return num;
}
//Conversion of uint64_t to GF2X
GF2X uint64_t_to_GF2X(uint64_t num)
{
GF2X f;
f.SetLength(1000);
long i=0;
long ith_bit;
long num_bits=NumBits(num);
while(i<num_bits)
{
ith_bit=bit(num,i);
SetCoeff(f,i,ith_bit);
i++;
}
f.normalize();
return f;
}
//Conversion of uint32_t to GF2X
GF2X uint32_t_to_GF2X(uint32_t num)
{
GF2X f;
f.SetLength(1000);
long i=0;
long ith_bit;
long num_bits=NumBits(num);
while(i<num_bits)
{
ith_bit=bit(num,i);
SetCoeff(f,i,ith_bit);
i++;
}
f.normalize();
return f;
}
void uint64_t_swap(uint64_t *x,uint64_t *y)
{
uint64_t t;
t=*x;
*x=*y;
*y=t;
}
//Conversion of ZZ to polynomail over GF2
GF2X set_GF2X(ZZ num)
{
GF2X f;
f.SetLength(1000);
long i=0;
long ith_bit;
while(!IsZero(num))
{
ith_bit=num % long(10);
SetCoeff(f,i,ith_bit);
i++;
num=num/(long)10;
}
f.normalize();
return f;
}
//helper function
ZZ func(char c,long length,long base,long i,ZZ val)
{
ZZ temp1,temp3,temp4;
long temp2;
temp1=ZZ(c)-val;
temp2=length-i;
temp2=temp2-1;
power(temp3,base,temp2);
temp4=temp1*temp3;
return temp4;
}
//hexadecimal to binary number for big integers
ZZ hex_to_binary(char hex[])
{
ZZ num=hex_to_decimal(hex);
ZZ rem, i=ZZ(1);
ZZ binary=ZZ(0);
while (num!=0)
{
rem=num%2;
num/=2;
binary+=rem*i;
i*=10;
}
return binary;
}
//hexadecimal to decimal number for big integers
ZZ hex_to_decimal(char *hex)
{
char *hexString;
long length = 0;
long base = 16; // Base of Hexadecimal Number
ZZ decimalNumber = ZZ(0);
long i;
// Find length of Hexadecimal Number
for (hexString = hex; *hexString != '\0'; hexString++)
{
length++;
}
// Find Hexadecimal Number
hexString = hex;
for (i = 0; *hexString != '\0' && i < length; i++, hexString++)
{
// Compare *hexString with ASCII values
if (*hexString >= 48 && *hexString <= 57) // is *hexString Between 0-9
{
decimalNumber+=func(*hexString,length,base,i,ZZ(48));
}
else if ((*hexString >= 65 && *hexString <= 70)) // is *hexString Between A-F
{
decimalNumber+=func(*hexString,length,base,i,ZZ(55));
}
else if (*hexString >= 97 && *hexString <= 102) // is *hexString Between a-f
{
decimalNumber+=func(*hexString,length,base,i,ZZ(87));
}
}
return decimalNumber;
}
uint32_t power_uint32_t(int x,uint32_t i)
{
uint32_t res=1;
for(unsigned j=0;j<i;j++)
res=res*x;
return res;
}
//helper function
uint32_t func_uint32_t(char c,int length,int base,int i,int val)
{
uint32_t temp1,temp3,temp4;
int temp2;
temp1=uint32_t(c)-val;
temp2=length-i;
temp2=temp2-1;
temp3=power_uint32_t(base,temp2);
temp4=temp1*temp3;
return temp4;
}
//hexadecimal to uint32_t
uint32_t hex_to_uint32_t(char hex[])
{
char *hexString;
int length = 0;
int base = 16; // Base of Hexadecimal Number
uint32_t decimalNumber = 0;
int i;
// Find length of Hexadecimal Number
for (hexString = hex; *hexString != '\0'; hexString++)
{
length++;
}
// Find Hexadecimal Number
hexString = hex;
for (i = 0; *hexString != '\0' && i < length; i++, hexString++)
{
// Compare *hexString with ASCII values
if (*hexString >= 48 && *hexString <= 57) // is *hexString Between 0-9
{
decimalNumber+=func_uint32_t(*hexString,length,base,i,(48));
}
else if ((*hexString >= 65 && *hexString <= 70)) // is *hexString Between A-F
{
decimalNumber+=func_uint32_t(*hexString,length,base,i,55);
}
else if (*hexString >= 97 && *hexString <= 102) // is *hexString Between a-f
{
decimalNumber+=func_uint32_t(*hexString,length,base,i,87);
}
}
return decimalNumber;
}