-
Notifications
You must be signed in to change notification settings - Fork 45
/
bignum1.c
204 lines (174 loc) · 5.87 KB
/
bignum1.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
/* BIGNUM1.C
**
** Routines to do Big Number Arithmetic. These are multi-precision, unsigned
** natural numbers (0, 1, 2, ...). For the storage method, see the BigNum
** typedef in file BIGNUM.H
**
** Released to the public domain by Clifford Rhodes, June 15, 1995, with
** no guarantees of any sort as to accuracy or fitness of use.
*/
#include <stdlib.h>
#include "bignum.h" /* Typedef for BigNum type */
BigNum * BigNumAdd(const BigNum * a, const BigNum * b)
{
/* Big Numbers a and b are added. If successful a pointer to the sum is
** returned. If unsuccessful, a NULL is returned.
** Neither a nor b is changed by the call.
*/
UShort carry = 0;
int size, la, lb;
long tsum;
BigNum * sum;
/* Allocate memory for Big Number sum to be returned... */
size = max(a->nlen, b->nlen) + 1;
if ((sum = BigNumAlloc(size)) == NULL)
return NULL;
/* Get indexes to last digits in each number (Least significant) */
la = a->nlen - 1;
lb = b->nlen - 1;
size--;
while (la >= 0 && lb >= 0) /* While both a and b have data */
{
tsum = carry + (long) *(a->n + la) + (long) *(b->n + lb);
*(sum->n + size) = (UShort) (tsum % (long) MODULUS);
carry = (tsum / (long) MODULUS) ? 1 : 0;
la--;
lb--;
size--;
}
if (lb < 0) /* Must see if a still has data */
{
while (carry && la >= 0)
{
tsum = carry + (long) *(a->n + la);
*(sum->n + size) = (UShort) (tsum % (long) MODULUS);
carry = (tsum / (long) MODULUS) ? 1 : 0;
la--;
size--;
}
while (la >= 0)
{
*(sum->n + size) = *(a->n + la);
la--;
size--;
}
}
else /* See if b still has data */
{
while (carry && lb >= 0)
{
tsum = carry + (long) *(b->n + lb);
*(sum->n + size) = (UShort) (tsum % (long) MODULUS);
carry = (tsum / (long) MODULUS) ? 1 : 0;
lb--;
size--;
}
while (lb >= 0)
{
*(sum->n + size) = *(b->n + lb);
lb--;
size--;
}
}
*(sum->n + size) = carry;
return sum;
}
BigNum * BigNumSub(const BigNum * a, const BigNum * b)
{
/* Big Numbers a and b are subtracted. a must be >= to b. A pointer to
** the difference (a - b) is returned if successful. If unsuccessful,
** a NULL is returned.
** Neither a nor b is changed by the call.
*/
int size, la, lb, borrow = 0;
long tdiff;
BigNum * diff;
/* Allocate memory for Big Number difference to be returned... */
if ((diff = BigNumAlloc(a->nlen)) == NULL)
return NULL;
la = a->nlen - 1;
size = la;
lb = b->nlen - 1;
while (lb >= 0)
{
tdiff = (long) *(a->n + la) - (long) *(b->n + lb) - borrow;
if (tdiff < 0)
{
tdiff += (long) (MODULUS - 1);
borrow = 1;
}
else borrow = 0;
*(diff->n + size) = (UShort) tdiff + borrow;
la--;
lb--;
size--;
}
while (la >= 0) /* Must get rest of a->n */
{
tdiff = (long) *(a->n + la) - borrow;
if (tdiff < 0)
{
tdiff += (long) (MODULUS - 1);
borrow = 1;
}
else borrow = 0;
*(diff->n + size) = (UShort) tdiff + borrow;
la--;
size--;
}
if (borrow) /* We've got an underflow here! */
{
BigNumFree(diff);
return NULL;
}
return diff;
}
BigNum * BigNumMul(const BigNum * a, const BigNum * b)
{
/* Big Numbers a and b are multiplied. A pointer to the product
** is returned if successful. If unsuccessful, a NULL is returned.
** Neither a nor b is changed by the call.
*/
int size, la, lb, apos, bpos, ppos;
UShort carry;
BigNum * product;
/* Allocate memory for Big Number product to be returned... */
size = a->nlen + b->nlen;
if ((product = BigNumAlloc(size)) == NULL)
return NULL;
la = a->nlen - 1;
lb = b->nlen - 1;
size--;
bpos = lb;
while (bpos >= 0) /* We'll multiply by each digit in b */
{
ppos = size + (bpos - lb); /* Position in product */
if (*(b->n + bpos) == 0) /* If zero multiplier, skip this pass */
ppos = ppos - la - 1;
else /* Multiply a by next b digit */
{
apos = la;
carry = 0;
while (apos >= 0) /* Go a digit at a time through a */
{
ULong temp;
temp = (ULong) *(a->n + apos) *
(ULong) *(b->n + bpos) + carry;
/*
** We must add any previous product term in
** this 'column' too
*/
temp += (ULong) *(product->n + ppos);
/* Now update product term, remembering any carry */
*(product->n + ppos) =
(UShort) (temp % (ULong) MODULUS);
carry = (UShort) (temp / (ULong) MODULUS);
apos--;
ppos--;
}
*(product->n + ppos) = carry;
}
bpos--;
}
return product;
}