-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.c
276 lines (203 loc) · 4.79 KB
/
lib.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
#include <8051.h>
#include "lib.h"
void multiply1024(__xdata unsigned char *a, __xdata unsigned char *b, __xdata unsigned char *z) {
int i;
int j;
unsigned int cl;
unsigned char carry = 0;
int m;
for(cl = 0; cl < 2*SIZE; cl++) {
z[cl] = 0;
}
for(i = SIZE-1; i >= 0; i--) {
for(j = SIZE-1; j >= 0; j--) {
m = a[i]*b[j] + z[i+j+1] + carry;
z[i+j+1] = m;
carry = m >> 8;
}
z[i] = carry;
carry = 0;
}
}
char larger_or_equal(__xdata unsigned char *z, __xdata unsigned char *p, unsigned char size)
{
unsigned char i;
for(i = 0; i < size-1; i++) {
if(z[i] > p[i]) return 1;
if(z[i] < p[i]) return 0;
}
return z[size-1] >= p[size-1];
}
char add1024(__xdata unsigned char *a, __xdata unsigned char *b, __xdata unsigned char *z) {
a;
b;
z;
__asm
mov A, DPL
add A, #0x7F
mov DPL, A
mov R2, DPL
mov R3, DPH
mov DPL, _add1024_PARM_2
mov DPH, (_add1024_PARM_2+1)
mov A, DPL
add A, #0x7F
mov DPL, A
mov R4, DPL
mov R5, DPH
mov DPL, _add1024_PARM_3
mov DPH, (_add1024_PARM_3+1)
mov A, DPL
add A, #0x7F
mov DPL, A
mov R6, DPL
mov R7, DPH
mov R0, #128
loop_add: mov DPL, R2
mov DPH, R3
movx A, @DPTR
mov R1, A
mov DPL, R4
mov DPH, R5
movx A, @DPTR
addc A, R1
mov DPL, R6
mov DPH, R7
movx @DPTR, A
dec R2
dec R4
dec R6
djnz R0, loop_add
mov A, #0
addc A, #0
mov DPL, A
__endasm;
}
char subtract1024(__xdata unsigned char *a, __xdata unsigned char *b, __xdata unsigned char *z) {
a;
b;
z;
__asm
mov A, DPL
add A, #0x7F
mov DPL, A
mov R2, DPL
mov R3, DPH
mov DPL, _subtract1024_PARM_2
mov DPH, (_subtract1024_PARM_2+1)
mov A, DPL
add A, #0x7F
mov DPL, A
mov R4, DPL
mov R5, DPH
mov DPL, _subtract1024_PARM_3
mov DPH, (_subtract1024_PARM_3+1)
mov A, DPL
add A, #0x7F
mov DPL, A
mov R6, DPL
mov R7, DPH
mov R0, #128
loop_sub: mov DPL, R4
mov DPH, R5
movx A, @DPTR
mov R1, A
mov DPL, R2
mov DPH, R3
movx A, @DPTR
subb A, R1
mov DPL, R6
mov DPH, R7
movx @DPTR, A
dec R2
dec R4
dec R6
djnz R0, loop_sub
mov A, #0
addc A, #0
mov DPL, A
__endasm;
}
void copy(__xdata unsigned char *dest, __xdata unsigned char *src) {
int i;
for(i = 0; i < SIZE; i++) {
*dest++ = *src++;
}
}
void coproc_execute() {
volatile char __xdata __at(0x800) state[1];
P1 = 0x01;
P1 = 0x02;
/* Wait for the coprocessor to signal done */
while(state[0] == 0) {
}
P1 = 0x03;
}
void display_result() {
volatile char __xdata __at(0x780) coproc_up[SIZE];
coproc_up[0x00] = 0xFF;
coproc_up[0x01] = 0x00;
coproc_execute();
}
void montpro(__xdata unsigned char *a, __xdata unsigned char *b, __xdata unsigned char *m, __xdata unsigned char *n) {
volatile char __xdata __at(0x600) coproc_a[SIZE];
volatile char __xdata __at(0x680) coproc_b[SIZE];
volatile char __xdata __at(0x700) coproc_n[SIZE];
volatile char __xdata __at(0x780) coproc_up[SIZE];
copy(coproc_a, a);
copy(coproc_b, b);
copy(coproc_n, n);
coproc_up[0x00] = 0x03;
coproc_up[0x01] = 0x40;
coproc_up[0x02] = 0x45;
coproc_up[0x03] = 0x01;
coproc_up[0x04] = 0x50;
coproc_up[0x05] = 0x00;
coproc_execute();
copy(m, coproc_a);
}
void montinv(__xdata unsigned char *a, __xdata unsigned char *p, __xdata unsigned char *r) {
volatile char __xdata __at(0x600) coproc_a[SIZE];
volatile char __xdata __at(0x700) coproc_n[SIZE];
volatile char __xdata __at(0x780) coproc_up[SIZE];
coproc_up[0x00] = 0x03;
coproc_up[0x01] = 0x99;
coproc_up[0x02] = 0x50;
coproc_up[0x03] = 0x00;
coproc_execute();
copy(r, coproc_a);
}
void modexp(__xdata unsigned char *xt, __xdata unsigned char *exp, __xdata unsigned char *yt, __xdata unsigned char *R) {
volatile char __xdata __at(0x600) coproc_a[SIZE];
volatile char __xdata __at(0x680) coproc_b[SIZE];
volatile char __xdata __at(0x780) coproc_up[SIZE];
int qpos = 0;
int i, first;
unsigned char e;
copy(coproc_a, R);
copy(coproc_b, xt);
for(first = 0; exp[first] == 0 && first < SIZE; first++);
e = exp[first];
coproc_up[qpos++] = 0x40;
coproc_up[qpos++] = 0x45;
for(i = first; i < SIZE; i++) {
e = exp[i];
coproc_up[qpos++] = 0x02; if(e & 0x80) coproc_up[qpos++] = 0x01;
coproc_up[qpos++] = 0x02; if(e & 0x40) coproc_up[qpos++] = 0x01;
coproc_up[qpos++] = 0x02; if(e & 0x20) coproc_up[qpos++] = 0x01;
coproc_up[qpos++] = 0x02; if(e & 0x10) coproc_up[qpos++] = 0x01;
coproc_up[qpos++] = 0x02; if(e & 0x08) coproc_up[qpos++] = 0x01;
coproc_up[qpos++] = 0x02; if(e & 0x04) coproc_up[qpos++] = 0x01;
coproc_up[qpos++] = 0x02; if(e & 0x02) coproc_up[qpos++] = 0x01;
coproc_up[qpos++] = 0x02; if(e & 0x01) coproc_up[qpos++] = 0x01;
if(qpos > 100) {
coproc_up[qpos] = 0x00;
coproc_execute();
qpos = 0x00;
}
}
coproc_up[qpos++] = 0x50;
coproc_up[qpos] = 0x00;
coproc_execute();
copy(yt, coproc_a);
}