This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BN.c
256 lines (176 loc) · 3.95 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
#include "BN.h"
static char bnSet(BN* bn, uint8_t val){
bn->nbytes = 0;
bn->data = _bnMalloc(1);
if(!bn->data) return 0;
bn->data[0] = val;
return 1;
}
static void bnFree(BN* bn){
_bnFree(bn->data);
}
static char bnIsNonzero(const BN* bn){
uint8_t i = bn->nbytes;
do{
if(bn->data[i]) return 1;
}while(i--);
return 0;
}
static void bnNormalize(BN* bn){
while(bn->nbytes && !bn->data[bn->nbytes]) bn->nbytes--;
}
static char bnIsGE(const BN* a, const BN* b){ //is a >= b ?
uint8_t an = a->nbytes, bn = b->nbytes;
while(an > bn) if(a->data[an--]) return 1;
while(an < bn) if(b->data[bn--]) return 0;
//now an == bn
do{
if(a->data[an] > b->data[an]) return 1;
else if(a->data[an] < b->data[an]) return 0;
}while(an--);
//equal
return 1;
}
void bnSub(BN* a, const BN* b){ //a -= b. precondition: a >= b [this is assumed and not checked]
uint8_t i, o, c = 0;
i = 0;
do{
o = a->data[i];
a->data[i] -= c;
c = 0;
if(a->data[i] > o) c = 1;
o = a->data[i];
a->data[i] -= b->data[i];
if(a->data[i] > o) c = 1;
}while(i++ != a->nbytes);
}
char bnAdd(BN* d, const BN* a, const BN* b){
uint8_t i, c = 0, o;
if(a->nbytes < b->nbytes){ //make sure A is longer than B
const BN* t;
t = a;
a = b;
b = t;
}
d->nbytes = a->nbytes;
if(a->data[a->nbytes] == 0xFF) d->nbytes++;
d->data = _bnMalloc((uint16_t)d->nbytes + 1);
if(!d->data) return 0;
i = 0;
do{
o = a->data[i];
a->data[i] -= c;
c = 0;
if(a->data[i] > o) c = 1;
o = a->data[i];
a->data[i] -= b->data[i];
if(a->data[i] > o) c = 1;
}while(i++ != b->nbytes);
do{
o = a->data[i];
a->data[i] -= c;
c = 0;
if(a->data[i] > o) c = 1;
}while(i++ != a->nbytes);
if(c) a->data[i]++;
return 1;
}
static void bnLsr1(BN* bn){
uint8_t i = bn->nbytes;
uint8_t t, top = 0;
do{
if(bn->data[i] & 1) t = 0x80;
else t = 0;
bn->data[i] = (bn->data[i] >> 1) | top;
top = t;
}while(i--);
}
static char bnLslMul8(BN* dst, uint8_t byteShift){
uint8_t* newData;
uint8_t i;
newData = _bnRealloc(dst->data, dst->nbytes + 1 + byteShift);
if(!newData) return 0;
dst->data = newData;
i = 0;
do{
newData[dst->nbytes + byteShift - i] = newData[dst->nbytes - i];
}while(i++ != dst->nbytes);
dst->nbytes += byteShift;
while(byteShift--) *newData++ = 0;
return 1;
}
static char bnMod(BN* top, BN* bot){
if(bot->nbytes <= top->nbytes){
uint16_t lsh = top->nbytes - bot->nbytes + 1;
if(!bnLslMul8(bot, lsh)) return 0;
lsh <<= 3;
while(lsh--){
bnLsr1(bot);
if(bnIsGE(top, bot)) bnSub(top, bot);
}
bnNormalize(top); //normalize the result
bnNormalize(bot); //fix any damade we did to "bot->nbytes"
}
}
char bnMul(BN* d, BN* a, BN* b){ //numbers may be normalized but will otherwse be unmodified
uint8_t i, j, c, o;
uint16_t v16;
uint8_t *buf, *dst;
i = d->nbytes = a->nbytes + b->nbytes + 1;
d->data = _bnMalloc((uint16_t)i + 1);
if(!d->data) return 0;
buf = _bnMalloc((uint16_t)b->nbytes + 2);
if(!buf){
_bnFree(d->data);
return 0;
}
do{
d->data[i] = 0;
}while(i--);
dst = d->data;
j = 0;
do{
i = 0;
buf[0] = 0;
do{
v16 = ((uint16_t)a->data[j] * (uint16_t)b->data[i]) + (uint16_t)buf[i];
buf[i + 0] = v16;
buf[i + 1] = v16 >> 8;
}while(i++ != b->nbytes);
c = 0;
i = 0;
do{
o = dst[i];
dst[i] += c;
c = 0;
if(o > dst[i]) c = 1;
o = dst[i];
dst[i] += buf[i];
if(o > dst[i]) c = 1;
}while(i++ != b->nbytes + 1);
dst++;
}while(j++ != a->nbytes);
bnNormalize(d);
return 1;
}
char bnExpMod(BN* dst, BN* a, BN* b, BN* n){ //a^b mod n a and b are destroyed, n stays
BN ret, t;
bnMod(b, n);
bnMod(a, n);
if(!bnSet(&ret, 1)) return 0;
while(bnIsNonzero(b)){
if(b->data[0] & 1){
if(!bnMul(&t, &ret, a)) return 0;
bnFree(&ret);
bnMod(&t, n);
ret = t;
}
if(!bnMul(&t, a, a)) return 0;
bnFree(a);
bnMod(&t, n);
*a = t;
bnLsr1(b);
}
*dst = ret;
return 1;
}