forked from sysprog21/fibdrv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bn.c
282 lines (251 loc) · 6.53 KB
/
bn.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
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/types.h>
#include "apm.h"
#include "bn.h"
#define BN_INIT_DIGITS ((8 + DIGIT_SIZE - 1) / DIGIT_SIZE)
static void bn_min_alloc(bn *n, uint32_t s)
{
if (n->alloc < s) {
n->alloc = ((s + 3) & ~3U);
n->digits = resize(n->digits, n->alloc);
}
}
static void bn_size(bn *n, uint32_t size)
{
n->size = size;
if (n->alloc < n->size) {
n->alloc = ((n->size + 3) & ~3U);
n->digits = resize(n->digits, n->alloc);
}
}
void bn_init(bn *n)
{
n->alloc = BN_INIT_DIGITS;
n->digits = new0(BN_INIT_DIGITS);
n->size = 0;
n->sign = 0;
}
void bn_init_u32(bn *n, uint32_t ui)
{
bn_init(n);
bn_set_u32(n, ui);
}
void bn_free(bn *n)
{
FREE(n->digits);
}
static void bn_set(bn *p, const bn *q)
{
if (p == q)
return;
if (q->size == 0) {
bn_zero(p);
} else {
bn_size(p, q->size);
copy(q->digits, q->size, p->digits);
p->sign = q->sign;
}
}
void bn_zero(bn *n)
{
n->sign = 0;
n->size = 0;
}
void bn_set_u32(bn *n, uint32_t m)
{
n->sign = 0;
if (m == 0) {
n->size = 0;
return;
}
bn_size(n, 1);
n->digits[0] = (uint64_t) m;
}
void bn_swap(bn *a, bn *b)
{
bn tmp = *a;
*a = *b;
*b = tmp;
}
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
void bn_add(const bn *a, const bn *b, bn *c)
{
if (a->size == 0) {
if (b->size == 0)
c->size = 0; // 0 + 0
else
bn_set(c, b); // 0 + b
return;
} else if (b->size == 0) {
bn_set(c, a); // a + 0
return;
}
// a + a
if (a == b) {
uint64_t cy;
if (a == c) {
// c = a + a
// c = 2 * a, a == c
// c <<= 1
cy = lshifti(c->digits, c->size, 1);
} else {
// c = a + a
// c = 2 * a
// c = a << 1
bn_size(c, a->size);
cy = lshift(a->digits, a->size, 1, c->digits);
}
if (cy) {
bn_min_alloc(c, c->size + 1);
c->digits[c->size++] = cy;
}
return;
}
/* Note: it should work for A == C or B == C */
uint32_t size;
if (a->sign == b->sign) { /* Both positive or negative. */
size = MAX(a->size, b->size);
bn_min_alloc(c, size + 1);
uint64_t cy = add(a->digits, a->size, b->digits, b->size, c->digits);
if (cy)
c->digits[size++] = cy;
else
APM_NORMALIZE(c->digits, size);
c->sign = a->sign;
} else { /* Differing signs. */
if (a->sign)
SWAP(a, b);
int cmpv = cmp(a->digits, a->size, b->digits, b->size);
if (cmpv > 0) { /* |A| > |B| */
/* If B < 0 and |A| > |B|, then C = A - |B| */
bn_min_alloc(c, a->size);
c->sign = 0;
size = rsize(c->digits, a->size);
} else if (cmpv < 0) { /* |A| < |B| */
/* If B < 0 and |A| < |B|, then C = -(|B| - |A|) */
bn_min_alloc(c, b->size);
c->sign = 1;
size = rsize(c->digits, b->size);
} else { /* |A| = |B| */
c->sign = 0;
size = 0;
}
}
c->size = size;
}
void bn_mul(const bn *a, const bn *b, bn *c)
{
if (a->size == 0 || b->size == 0) {
bn_zero(c);
return;
}
if (a == b) {
bn_sqr(a, c);
return;
}
uint32_t csize = a->size + b->size;
if (a == c || b == c) {
uint64_t *prod = APM_TMP_ALLOC(csize);
mul(a->digits, a->size, b->digits, b->size, prod);
csize -= (prod[csize - 1] == 0);
bn_size(c, csize);
copy(prod, csize, c->digits);
APM_TMP_FREE(prod);
} else {
bn_min_alloc(c, csize);
mul(a->digits, a->size, b->digits, b->size, c->digits);
c->size = csize - (c->digits[csize - 1] == 0);
}
c->sign = a->sign ^ b->sign;
}
void bn_sqr(const bn *a, bn *b)
{
if (a->size == 0) {
bn_zero(b);
return;
}
uint32_t bsize = a->size * 2;
if (a == b) {
uint64_t *prod = APM_TMP_ALLOC(bsize);
sqr(a->digits, a->size, prod);
bsize -= (prod[bsize - 1] == 0);
bn_size(b, bsize);
copy(prod, bsize, b->digits);
APM_TMP_FREE(prod);
} else {
bn_min_alloc(b, bsize);
sqr(a->digits, a->size, b->digits);
b->size = bsize - (b->digits[bsize - 1] == 0);
}
b->sign = 0;
}
void bn_lshift(const bn *p, unsigned int bits, bn *q)
{
if (bits == 0 || bn_is_zero(p)) {
if (bits == 0)
bn_set(q, p);
else
bn_zero(q);
return;
}
const unsigned int digits = bits / DIGIT_BITS;
bits %= DIGIT_BITS;
uint64_t cy;
if (p == q) {
cy = lshifti(q->digits, q->size, bits);
if (digits != 0) {
bn_min_alloc(q, q->size + digits);
for (int j = q->size - 1; j >= 0; j--)
q->digits[j + digits] = q->digits[j];
q->size += digits;
}
} else {
bn_size(q, p->size + digits);
cy = lshift(p->digits, p->size, bits, q->digits + digits);
}
zero(q->digits, digits);
if (cy) {
bn_size(q, q->size + 1);
q->digits[q->size - 1] = cy;
}
}
// char *bn_to_dec_str(const bn *n)
// {
// size_t str_size = (size_t) ((n->size) * DIGIT_BITS) / 3 + 2;
// if (n->size == 0) {
// char *str = kmalloc(2, GFP_KERNEL);
// str[0] = '0';
// str[1] = '\0';
// return str;
// } else {
// char *s = kmalloc(str_size, GFP_KERNEL);
// char *p = s;
// memset(s, '0', str_size - 1);
// s[str_size - 1] = '\0';
// /* n.digits[0] contains least significant bits */
// for (int i = n->size - 1; i >= 0; i--) {
// /* walk through every bit of bn */
// for (unsigned long long d = 1ULL << 63; d; d >>= 1) {
// /* binary -> decimal string */
// int carry = !!(d & n->digits[i]);
// for (int j = str_size - 2; j >= 0; j--) {
// s[j] += s[j] - '0' + carry;
// carry = (s[j] > '9');
// if (carry)
// s[j] -= 10;
// }
// }
// }
// // skip leading zero
// while (p[0] == '0' && p[1] != '\0') {
// p++;
// }
// if (n->sign)
// *(--p) = '-';
// memmove(s, p, strlen(p) + 1);
// return s;
// }
// }