-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigint.c
121 lines (101 loc) · 2.07 KB
/
bigint.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
#include "bigint.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
// allocate a big int and fill it with zero
BIGINT allocateBigInt()
{
int* ret = malloc(sizeof(int) * BIGINT_LENGTH);
int i = 0;
for(i = 0; i < BIGINT_LENGTH; i++)
{
*(ret + i ) = 0;
}
return ret;
}
// convert a c stringt to bigint (allocates)
BIGINT convertStringToBigInt(char* input, int length)
{
int* ret = allocateBigInt();
int i, j;
int numCharsInCell = log10(BIGINT_CELL);
int numCells = (length / numCharsInCell);
int chOffset = 0;
int chLoc = 50;
// convert
for(i = 0; i < numCells; i++)
{
chOffset = 0;
for(j = 0; j < numCharsInCell; j++)
{
*(ret + i) *= 10;
*(ret + i) += *(input + (chLoc - (numCharsInCell - chOffset))) - '0';
chOffset++;
}
chLoc -= numCharsInCell;
}
chOffset = 0;
while(chLoc > 0)
{
*(ret + i) *= 10;
*(ret + i) += *(input + chOffset) - '0';
chOffset++;
chLoc--;
}
return ret;
}
// multiply a BIGINT by a scalar integer value
// returns 1 if scalar is too big
int multiplyBigIntByScalar(BIGINT target, int scalar)
{
int i;
if (INT_MAX / BIGINT_CELL < scalar)
{
return 1;
}
for(i = 0; i < BIGINT_LENGTH; i++)
{
*(target + i) = *(target + i) * scalar;
}
balanceBigInt(target);
return 0;
}
void addBigInt(BIGINT op1, BIGINT op2)
{
int i = 0;
for(i = 0; i < BIGINT_LENGTH; i++)
{
*(op1 + i) = *(op1 + i) + *(op2 + i);
}
balanceBigInt(op1);
}
void printBigInt(BIGINT val)
{
int i;
for(i = BIGINT_LENGTH - 1; i >= 0; i--)
{
printf("%d", *(val + i));
}
printf("\n");
}
void balanceBigInt(BIGINT val)
{
int i;
int carry = 0;
for(i = 0; i < BIGINT_LENGTH - 1; i++)
{
if (*(val + i) > BIGINT_CELL)
{
carry = *(val + i) / BIGINT_CELL;
*(val + i) = *(val + i) % BIGINT_CELL;
*(val + i + 1) += carry;
}
}
*(val + (BIGINT_LENGTH - 1)) = *(val + (BIGINT_LENGTH - 1)) % BIGINT_CELL;
}
void addScalarToBigInt(BIGINT value, int scalar)
{
*(value) += scalar;
balanceBigInt(value);
}