-
Notifications
You must be signed in to change notification settings - Fork 1
/
CRT.s
executable file
·556 lines (473 loc) · 11.5 KB
/
CRT.s
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
.data
EXIT_SUCCESS=0
SYSEXIT=60
value_rns:
# 123456 100 0110 01110 0001100 0001001000000 0x8ce18240
.quad 0x8ce18240
value_pos:
.quad 123456
m1: .quad 7 # 2^3 - 1 (for relative primarity)
m2: .quad 15 # 2^4 - 1
m3: .quad 31 # 2^5 - 1
m4: .quad 127 # 2^7 - 1
m5: .quad 8192 # 2^13 - 1
M: .quad 3386449920 # 7 * 15 * 31 * 127 * 8192
M1: .quad 483778560 # 3386449920 / 7
M2: .quad 225763328 # 3386449920 / 15
M3: .quad 109240320 # 3386449920 / 31
M4: .quad 26664960 # 3386449920 / 127
M5: .quad 413385 # 3386449920 / 8192
# multiplicative inversion
y1: .quad 6 # Mi * yi = 1(mod m1)
y2: .quad 2
y3: .quad 7
y4: .quad 54
y5: .quad 2937
# Convert positional system number to RNS number
# RAX(rns) = ARG(pos)
.macro rns pos_num
push %rbx
push %rcx
push %rbx
push %r9
mov \pos_num, %r9
xor %rbx, %rbx # rbx - result
mov %r9, %rax
xor %rdx, %rdx
mov $7, %rcx
div %rcx
shl $29, %rdx
mov %rdx, %rbx
mov %r9, %rax
xor %rdx, %rdx
mov $15, %rcx
div %rcx
shl $25, %rdx
or %rdx, %rbx
mov %r9, %rax
xor %rdx, %rdx
mov $31, %rcx
div %rcx
shl $20, %rdx
or %rdx, %rbx
mov %r9, %rax
xor %rdx, %rdx
mov $127, %rcx
div %rcx
shl $13, %rdx
or %rdx, %rbx
mov %r9, %rax
xor %rdx, %rdx
mov $8192, %rcx
div %rcx
or %rdx, %rbx
mov %rbx, %rax
pop %r9
pop %rbx
pop %rcx
pop %rdx
.endm
# Convert RNS number to positional system number
# RAX(pos) = ARG(rns)
.macro drns rns_num
push %r11
push %r8
push %r9
push %rbx
push %rdx
push %rcx
mov \rns_num, %r9
mov %r9, %rax
shr $29, %rax # -----------------------------XXX
and $7, %rax # 00000000000000000000000000000111 (:3)
mov %rax, %r11 # mod 7
# a1 * M1 x y1
mov M1, %rax
mul %r11
mov y1, %rbx
mul %rbx
mov %rax, %r8
mov %r9, %rax
shr $25, %rax # -------------------------000XXXX
and $15, %rax # 00000000000000000000000000001111 (:4)
mov %rax, %r11 # mod 15
# a2 * M2 x y2 + do r8
mov M2, %rax
mul %r11
mov y2, %rbx
mul %rbx
add %rax, %r8
mov %r9, %rax
shr $20, %rax # --------------------0000000XXXXX
and $31, %rax # 00000000000000000000000000011111 (:5)
mov %rax, %r11 # mod 31
# a3 * M3 x y3 + do r8
mov M3, %rax
mul %r11
mov y3, %rbx
mul %rbx
add %rax, %r8
mov %r9, %rax
shr $13, %rax # -------------000000000000XXXXXXX
and $127, %rax # 00000000000000000000000001111111 (:7)
mov %rax, %r11 # mod 127
# a4 * M4 x y4 + do r8
mov M4, %rax
mul %r11
mov y4, %rbx
mul %rbx
add %rax, %r8
mov %r9, %rax
# 0000000000000000000XXXXXXXXXXXXX
and $8191, %rax # 00000000000000000001111111111111 (:13)
mov %rax, %r11 # mod 8192
# a5 * M5 x y5 + do r8
mov M5, %rax
mul %r11
mov y5, %rbx
mul %rbx
add %rax, %r8
# suma mod M
mov M, %rbx
xor %rdx, %rdx
mov %r8, %rax
div %rbx
mov %rdx, %rax
pop %rcx
pop %rdx
pop %rbx
pop %r9
pop %r8
pop %r11
.endm
# Add two RNS numbers. One in RAX, other as ARG.
# RAX = RAX + ARG
.macro addrns rns_num
push %rbx
push %rcx
push %rdx
push %r9
push %r10
push %r11
push %r12
mov \rns_num, %r12
mov %rax, %r9
mov %r12, %rax
xor %r11, %r11
shr $29, %rax # -----------------------------XXX
and $7, %rax # 00000000000000000000000000000111 (:3)
mov %rax, %r10
mov %r9, %rax
shr $29, %rax # -----------------------------XXX
and $7, %rax # 00000000000000000000000000000111 (:3)
add %r10, %rax
mov $7, %rbx
xor %rdx, %rdx
div %rbx
shl $29, %rdx
or %rdx, %r11
mov %r12, %rax
shr $25, %rax # -------------------------000XXXX
and $15, %rax # 00000000000000000000000000001111 (:4)
mov %rax, %r10
mov %r9, %rax
shr $25, %rax # -------------------------000XXXX
and $15, %rax # 00000000000000000000000000001111 (:4)
add %r10, %rax
mov $15, %rbx
xor %rdx, %rdx
div %rbx
shl $25, %rdx
or %rdx, %r11
mov %r12, %rax
shr $20, %rax # --------------------0000000XXXXX
and $31, %rax # 00000000000000000000000000011111 (:5)
mov %rax, %r10
mov %r9, %rax
shr $20, %rax # --------------------0000000XXXXX
and $31, %rax # 00000000000000000000000000011111 (:5)
add %r10, %rax
mov $31, %rbx
xor %rdx, %rdx
div %rbx
shl $20, %rdx
or %rdx, %r11
mov %r12, %rax
shr $13, %rax # -------------000000000000XXXXXXX
and $127, %rax # 00000000000000000000000001111111 (:7)
mov %rax, %r10
mov %r9, %rax
shr $13, %rax # -------------000000000000XXXXXXX
and $127, %rax # 00000000000000000000000001111111 (:7)
add %r10, %rax
mov $127, %rbx
xor %rdx, %rdx
div %rbx
shl $13, %rdx
or %rdx, %r11
mov %r12, %rax # 0000000000000000000XXXXXXXXXXXXX
and $8191, %rax # 00000000000000000001111111111111 (:13)
mov %rax, %r10
mov %r9, %rax
and $8191, %rax # 00000000000000000001111111111111 (:13)
add %r10, %rax
mov $8192, %rbx
xor %rdx, %rdx
div %rbx
or %rdx, %r11
mov %r11, %rax
pop %r12
pop %r11
pop %r10
pop %r9
pop %rdx
pop %rcx
pop %rbx
.endm
# Multiple two RNS numbers. One in RAX, other as ARG.
# RAX = RAX * ARG
.macro mulrns rns_num
push %rbx
push %rcx
push %rdx
push %r9
push %r10
push %r11
push %r12
mov \rns_num, %r12
mov %rax, %r9
mov %r12, %rax
xor %r11, %r11
shr $29, %rax # -----------------------------XXX
and $7, %rax # 00000000000000000000000000000111 (:3)
mov %rax, %r10
mov %r9, %rax
shr $29, %rax # -----------------------------XXX
and $7, %rax # 00000000000000000000000000000111 (:3)
mul %r10
mov $7, %rbx
xor %rdx, %rdx
div %rbx
shl $29, %rdx
or %rdx, %r11
mov %r12, %rax
shr $25, %rax # -------------------------000XXXX
and $15, %rax # 00000000000000000000000000001111 (:4)
mov %rax, %r10
mov %r9, %rax
shr $25, %rax # -------------------------000XXXX
and $15, %rax # 00000000000000000000000000001111 (:4)
mul %r10
mov $15, %rbx
xor %rdx, %rdx
div %rbx
shl $25, %rdx
or %rdx, %r11
mov %r12, %rax
shr $20, %rax # --------------------0000000XXXXX
and $31, %rax # 00000000000000000000000000011111 (:5)
mov %rax, %r10
mov %r9, %rax
shr $20, %rax # --------------------0000000XXXXX
and $31, %rax # 00000000000000000000000000011111 (:5)
mul %r10
mov $31, %rbx
xor %rdx, %rdx
div %rbx
shl $20, %rdx
or %rdx, %r11
mov %r12, %rax
shr $13, %rax # -------------000000000000XXXXXXX
and $127, %rax # 00000000000000000000000001111111 (:7)
mov %rax, %r10
mov %r9, %rax
shr $13, %rax # -------------000000000000XXXXXXX
and $127, %rax # 00000000000000000000000001111111 (:7)
mul %r10
mov $127, %rbx
xor %rdx, %rdx
div %rbx
shl $13, %rdx
or %rdx, %r11
mov %r12, %rax # 0000000000000000000XXXXXXXXXXXXX
and $8191, %rax # 00000000000000000001111111111111 (:13)
mov %rax, %r10
mov %r9, %rax
and $8191, %rax # 00000000000000000001111111111111 (:13)
mul %r10
mov $8192, %rbx
xor %rdx, %rdx
div %rbx
or %rdx, %r11
mov %r11, %rax
pop %r12
pop %r11
pop %r10
pop %r9
pop %rdx
pop %rcx
pop %rbx
.endm
.macro shr_rns_step
push %rbx
push %rcx
push %rdx
push %r9
push %r10
push %r11
xor %rdx, %rdx
xor %r11, %r11
mov %rax, %r9
mov %rax, %r8
shr $29, %rax # -----------------------------XXX
and $7, %rax # 00000000000000000000000000000111 (:3)
mov %rax, %r10
shr %r10 # ------------------------------11 |1
and $1, %rax # -------------------------------1
shl $2, %rax # -----------------------------1XX
or %r10, %rax
shl $29, %rax
or %rax, %r11
mov %r9, %rax
shr $25, %rax # -------------------------000XXXX
and $15, %rax # 00000000000000000000000000001111 (:4)
mov %rax, %r10
shr %r10 # -----------------------------111 |1
and $1, %rax # -------------------------------1
shl $3, %rax # ----------------------------1XXX
or %r10, %rax
shl $25, %rax
or %rax, %r11
mov %r9, %rax
shr $20, %rax # --------------------0000000XXXXX
and $31, %rax # 00000000000000000000000000011111 (:5)
mov %rax, %r10
shr %r10 # ----------------------------1111 |1
and $1, %rax # -------------------------------1
shl $4, %rax # ----------------------------1XXX
or %r10, %rax
shl $20, %rax
or %rax, %r11
mov %r9, %rax
shr $13, %rax # -------------000000000000XXXXXXX
and $127, %rax # 00000000000000000000000001111111 (:7)
mov %rax, %r10
shr %r10 # --------------------------111111 |1
and $1, %rax # -------------------------------1
shl $6, %rax # -------------------------1XXXXXX
or %r10, %rax
shl $13, %rax
or %rax, %r11
mov %r9, %rax
# 0000000000000000000XXXXXXXXXXXXX
and $8191, %rax # 00000000000000000001111111111111 (:13)
mov %rax, %r10
shr %r10 # --------------------111111111111 |1
and $1, %rax # -------------------------------1
shl $12, %rax # -------------------1XXXXXXXXXXXX
or %r10, %rax
or %rax, %r11
mov %r11, %rax
pop %r11
pop %r10
pop %r9
pop %rdx
pop %rcx
pop %rbx
.endm
.macro shr_rns positions
push %rsi
mov \positions, %rsi
filip_tribute:
cmp $0, %rsi
jle exit_shr_rns
shr_rns_step
dec %rsi
jmp filip_tribute
exit_shr_rns:
pop %rsi
.endm
# Compare two RNS numbers. One in RAX, other as ARG.
# If RAX bigger -> RAX = 1, If RAX smaller -> RAX = -1, If equal -> RAX = 0
.macro cmprns rns_num
push %rbx
drns %rax
mov %rax, %rbx
mov \rns_num, %rax
drns %rax
cmp %rax, %rbx
jl arg_greater
je both_equal
rax_greater:
mov $1, %rax
jmp leave_cmprns
arg_greater:
mov $-1, %rax
jmp leave_cmprns
both_equal:
xor %rax, %rax
leave_cmprns:
pop %rbx
.endm
# mrc from rns
# RAX = mrc(RAX)
.macro mrs
push %rbx
push %rcx
push %rdx
push %r9
push %r10
push %r11
push %r12
mov %rax, %r12
mov %r12, %rax # 0000000000000000000XXXXXXXXXXXXX
and $8191, %rax # 00000000000000000001111111111111 (:13)
mov $m4, %r9
mul %r9
mov %rax, %r10
mov %r12, %rax
shr $13, %rax # -------------000000000000XXXXXXX
and $127, %rax # 00000000000000000000000001111111 (:7)
mov $m3, %r9
mul %r9
mul %r10
mov %rax, %r10
mov %r12, %rax
shr $20, %rax # --------------------0000000XXXXX
and $31, %rax # 00000000000000000000000000011111 (:5)
mov $m2, %r9
mul %r9
mul %r10
mov %rax, %r10
mov %r12, %rax
shr $25, %rax # -------------------------000XXXX
and $15, %rax # 00000000000000000000000000001111 (:4)
mov $m1, %r9
mul %r9
mul %r10
mov %rax, %r10
shr $29, %rax # -----------------------------XXX
and $7, %rax # 00000000000000000000000000000111 (:3)
mul %r10
pop %r12
pop %r11
pop %r10
pop %r9
pop %rdx
pop %rcx
pop %rbx
.endm
.text
# Main. Mainly Testing
# Alter to show inner workings
.global main
main:
movq %rsp, %rbp # for correct debugging
rns_check:
rns $128
shr_rns $2
drns %rax
exit:
movq $SYSEXIT, %rax
movq $EXIT_SUCCESS, %rdi
syscall