-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBigInteger.c
377 lines (290 loc) · 8.54 KB
/
BigInteger.c
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "BigInteger.h"
uint32_t cnt = 0;
void BIGINT_from_String(uint32_t *destination, int length, char* num)
{
//str1 is the raw data in hex without 0x.
uint8_t counter = 0;
uint32_t val;
int i = 0;
char hex[8];
char *ptr;
for (i = length * 2 - 1; i >= 0; i = i - 8)
{
hex[0] = num[i - 7];
hex[1] = num[i - 6];
hex[2] = num[i - 5];
hex[3] = num[i - 4];
hex[4] = num[i - 3];
hex[5] = num[i - 2];
hex[6] = num[i - 1];
hex[7] = num[i];
val = (uint32_t)strtoll(hex, &ptr, 16);
destination[counter] = val;
counter++;
}
}
void Display_BIGD(BIGINT Data)
{
uint8_t i = 0;
printf("%d - ", Data.Length);
printf("0x");
for (i = 0; i < Data.Length; i++)
{
printf("%08x", Data.Data[Data.Length - i - 1]);
}
}
uint8_t Add(BIGINT Output, BIGINT Input1, BIGINT Input2)
{
uint8_t i;
uint8_t carry = 0;
for (i = 0; i < Input1.Length; i++)
{
Input1.Data[i] += Input2.Data[i];
Input1.Data[i] += carry;
carry = (Input1.Data[i] < Input2.Data[i]);
}
return carry;
}
uint8_t Subtract(BIGINT Output, BIGINT Input1, BIGINT Input2)
{
uint8_t i;
uint32_t tempResult = 0;
for (i = 0; i < Input1.Length * 2; i++)
{
tempResult += *(((uint16_t*)Input1.Data) + i) - *(((uint16_t*)Input2.Data) + i);
*(((uint16_t*)Output.Data) + i) = tempResult;
if ((tempResult & 0x80000000) == 0)
tempResult >>= 16;
else
tempResult = 0xFFFFFFFF;
}
return ((uint8_t)tempResult == 0xFF);
}
uint8_t Subtract2(BIGINT Output, BIGINT Input1, BIGINT Input2)
{
uint8_t i;
uint8_t carry = 0;
for (i = 0; i < Input1.Length; i++)
{
Input1.Data[i] -= carry;
if ((int32_t)Input1.Data[i] < (int32_t)Input2.Data[i])
carry = 1;
else
carry = 0;
Input1.Data[i] -= Input2.Data[i];
}
return carry;
}
void Multiply(BIGINT Output, BIGINT Input1, BIGINT Input2)
{
uint8_t k, l;
uint32_t tmp;
for (k = 0; k < Input1.Length + Input2.Length; k++)
{
Output.Data[k] = 0;
}
for (k = 0; k < Input1.Length * 2; k++)
{
tmp = 0;
for (l = 0; l < Input2.Length * 2; l++)
{
tmp = *(((uint16_t*)Input1.Data) + k) * *(((uint16_t*)Input2.Data) + l) + *(((uint16_t*)Output.Data) + k + l) + tmp;
*(((uint16_t*)Output.Data) + k + l) = tmp;
tmp >>= 16;
}
*(((uint16_t*)Output.Data) + k + l) += tmp;
}
}
void Divide(BIGINT Quotient, BIGINT Divident, BIGINT Divisor)
{
uint8_t j, i, l;
uint32_t tmp = 0;
uint16_t temp1[3] = { 0, };
BIGINT temp2;
uint32_t temp2data[MAX_ARRAY_SIZE] = { 0, };
temp2.Data = temp2data;
uint8_t n = Divident.Length * 2 - 1;
uint8_t t = Divisor.Length * 2 - 1;
/*
BIGINT Quotient;
uint32_t Qdata[MAX_ARRAY_SIZE] = { 0, };
Quotient.Length = Quotient2.Length;
Quotient.Data = Qdata;
*/
// STEP 1.
// For j from 0 to (n-t) do: Quotient[j] = 0
for (j = 0; j < n - t; j++)
*((uint16_t*)Quotient.Data + j) = 0;
// STEP 2.
// While Divident > Divisor * b^(n-t) :
// Quotient[n-t] = Quotient[n-t] + 1
// Divident = Divident - Divisor * b^(n-t)
//
// Note: Multiplying Divisor with b^(n-t) means, aligning MSB of Divisor and Divident.
while (isAlignedGreater(Divident, Divisor))
{
*((uint16_t*)Quotient.Data + n - t) = *((uint16_t*)Quotient.Data + n - t) + 1;
SubtractAligned(Divident, Divident, Divisor);
}
// STEP 3.
// For i from n down to (t+1):
//
// STEP 3.1
// if Divident[i] == Divisor[t] then:
// Quotient[i-t-1] = b - 1
// else
// Quotient[i-t-1] = ( Divident[i] || Divident[i-1] ) / Divisor[t]
//
// STEP 3.2
// While Quotient[i-t-1] * ( Divisor[t] || Divisor[t-1]) > ( Divident[i] || Divident[i-1] || Divident[i-2] ) :
// Quotient[i-t-1] = Quotient[i-t-1] - 1;
//
// STEP 3.3
// Divident = Divident - Quotient[i-t-1] * Divisor * b^(i-t-1)
//
// STEP 3.4
// if Divident < 0 then:
// Divident = Divident + Divisor * b^(i-t-1)
// Quotient[i-t-1] = Quotient[i-t-1] - 1
for (i = n; i > t; i--)
{
// STEP 3.1
if (*((uint16_t*)Divident.Data + i) == *((uint16_t*)Divisor.Data + i))
*((uint16_t*)Quotient.Data + i - t - 1) = 0xFFFF;
else
*((uint16_t*)Quotient.Data + i - t - 1) = ((uint32_t)(*((uint16_t*)Divident.Data + i) << 16) | (uint32_t)*((uint16_t*)Divident.Data + i - 1)) / *((uint16_t*)Divisor.Data + t);
// STEP 3.2
tmp = ((uint32_t)(*((uint16_t*)Divident.Data + i) << 16) | (uint32_t)*((uint16_t*)Divident.Data + i - 1)) / *((uint16_t*)Divisor.Data + t);
// Calculate Quotient[i-t-1] * ( Divisor[t] || Divisor[t-1]) into temp1[]
tmp = *((uint16_t*)Divisor.Data + t - 1) * *((uint16_t*)Quotient.Data + i - t - 1);
temp1[0] = tmp;
tmp >>= 16;
tmp = *((uint16_t*)Divisor.Data + t) * *((uint16_t*)Quotient.Data + i - t - 1) + tmp;
temp1[1] = tmp;
temp1[2] = tmp >> 16;
while (isGreater16(temp1, (uint16_t*)Divident.Data + i - 2, 3))
{
*((uint16_t*)Quotient.Data + i - t - 1) = *((uint16_t*)Quotient.Data + i - t - 1) - 1;
// Calculate Quotient[i-t-1] * ( Divisor[t] || Divisor[t-1]) again for next while loop
tmp = *((uint16_t*)Divisor.Data + t - 1) * *((uint16_t*)Quotient.Data + i - t - 1);
temp1[0] = tmp;
tmp >>= 16;
tmp = *((uint16_t*)Divisor.Data + t) * *((uint16_t*)Quotient.Data + i - t - 1) + tmp;
temp1[1] = tmp;
temp1[2] = tmp >> 16;
}
// STEP 3.3
// Calculate Quotient[i-t-1] * Divisor * b^(i-t-1) into temp[2]
tmp = 0;
for (l = 0; l < t + (i - t - 1) + 1; l++)
{
if (l<(i - t - 1))
tmp = *((uint16_t*)temp2.Data + l) + tmp;
else
tmp = *((uint16_t*)Divisor.Data + l - (i - t - 1)) * *((uint16_t*)Quotient.Data + i - t - 1) + tmp;
*((uint16_t*)temp2.Data + l) = tmp;
tmp >>= 16;
}
*((uint16_t*)temp2.Data + l) = tmp;
*((uint16_t*)temp2.Data + l + 1) = 0;
// Now perform the subtraction:
// Divident = Divident - Quotient[i-t-1] * Divisor * b^(i-t-1)
// Also goto next step;
// STEP 3.4
if (Subtract(Divident, Divident, temp2))
{
// Calculate Divisor * b^(i-t-1) into temp2[]
for (l = 0; l < t + (i - t - 1) + 1; l++)
if (l < (i - t - 1))
*((uint16_t*)temp2.Data + l) = 0;
else
*((uint16_t*)temp2.Data + l) = *((uint16_t*)Divisor.Data + l - (i - t - 1));
*((uint16_t*)temp2.Data + l) = 0;
Add(Divident, Divident, temp2);
// Quotient[i-t-1] = Quotient[i-t-1] - 1
*((uint16_t*)Quotient.Data + i - t - 1) = *((uint16_t*)Quotient.Data + i - t - 1) - 1;
}
tmp = 0;
}
// STEP 4.
// Remainder = Divident
}
uint8_t isGreaterThanZero(BIGINT Input)
{
uint8_t i;
//if ((Input.Data[Input.Length - 1] & 0x80000000) != 0) return 0;
for (i = 0; i < Input.Length; i++)
{
if (Input.Data[i] > 0) return 1;
else return 0;
}
return 0;
}
void ShiftLeft(BIGINT Input)
{
uint8_t i, oldMSB = 0, newMSB;
for (i = 0; i < Input.Length; i++)
{
newMSB = (Input.Data[i] & 0x80000000) != 0;
Input.Data[i] = Input.Data[i] << 1 | oldMSB;
oldMSB = newMSB;
}
}
void ShiftRight(BIGINT Input)
{
uint8_t oldLSB = 0, newLSB;
int i;
for (i = Input.Length-1; i >= 0; i--)
{
newLSB = (Input.Data[i] & 0x00000001) != 0;
if (oldLSB)
Input.Data[i] = 0x80000000 | Input.Data[i] >> 1;
else
Input.Data[i] = Input.Data[i] >> 1;
oldLSB = newLSB;
}
}
uint8_t isAlignedGreater(BIGINT Input1, BIGINT Input2)
{
uint8_t i;
for (i = 0; i < Input2.Length; i++)
{
if (Input1.Data[Input1.Length - i - 1] > Input2.Data[Input2.Length - i - 1]) return 1;
else if (Input1.Data[Input1.Length - i - 1] < Input2.Data[Input2.Length - i - 1]) return 0;
}
while (i < Input1.Length)
{
if (Input1.Data[Input1.Length - i - 1] > 0)
return 1;
i++;
}
return 0;
}
uint8_t SubtractAligned(BIGINT Output, BIGINT Input1, BIGINT Input2)
{
uint8_t i;
uint32_t tempResult = 0;
for (i = 0; i < Input1.Length * 2; i++)
{
if (i < (Input1.Length - Input2.Length) * 2)
tempResult += *((uint16_t*)Input1.Data + i);
else
tempResult += *((uint16_t*)Input1.Data + i) - *((uint16_t*)Input2.Data + i - (Input1.Length - Input2.Length) * 2);
*((uint16_t*)Output.Data + i) = tempResult;
if ((tempResult & 0x80000000) == 0)
tempResult >>= 16;
else
tempResult = 0xFFFFFFFF;
}
return ((uint8_t)tempResult == 0xFF);
}
uint8_t isGreater16(uint16_t *Input1, uint16_t *Input2, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i++)
{
if (Input1[Length - i - 1] > Input2[Length - i - 1]) return 1;
else if (Input1[Length - i - 1] < Input2[Length - i - 1]) return 0;
}
return 0;
}