-
Notifications
You must be signed in to change notification settings - Fork 4
/
x25519-cm0.S
3409 lines (3128 loc) · 76.6 KB
/
x25519-cm0.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@ =======================================================================
@ This is an X25519 implementation in ARMv6-M assembly, suitable for
@ ARM Cortex M0 and M0+. It will also run on later 32-bit ARM CPUs,
@ e.g. ARM Cortex M3 and M4 (but substantially faster implementations
@ exist for these larger CPUs).
@
@ This source file uses from C preprocessor directives and macros, and
@ thus should be compiled with a preprocessor pass. With GCC and Clang,
@ this is activated by default when the file name ends with '.S' with
@ an uppercase 'S'.
@
@ X25519 is implemented as specified in RFC 7748. Notably:
@ - In the input 'u' coordinate, the last bit (most significant bit of
@ the last byte) is ignored.
@ - All possible 255-bit patterns are supported, including values in
@ the 2^255-19 to 2^255-1 range.
@ - "Clamping" is automatically applied on the scalar:
@ - bit 255 is forced to 0
@ - bit 254 is forced to 1
@ - bits 0, 1 and 2 are forced to 0
@ - All operations are strictly "constant-time". In particular, the
@ memory access pattern does not depend on secret values. There is
@ no reliance on RAM accesses to be free of timing-based side
@ channels (the cumulative cost of the constant-time "conditional
@ swap" operations is about 46080 cycles).
@
@ The implementation has a single dependency on the external memcpy()
@ function.
@
@ The x25519() function is the external API; it is callable from C code,
@ since it respects the standard ABI.
@ None of the code contained therein modifies or even reads the r9
@ register. This register may be reserved for all purposes by the ABI
@ (in some systems using this ABI, r9 may be reserved at all times and
@ not even usable for local temporary storage; this was the case, for
@ instance, in iOS up to version 2).
@
@ -----------------------------------------------------------------------
@
@ IMPLEMENTATION NOTES:
@ =====================
@
@ Field elements are internally represented as sequences of eight 32-bit
@ words, in little-endian order. All 256-bit patterns are allowed; i.e.
@ values up to and including 2^256-1 are properly handled. The
@ gf_normalize_inner() function performs full reduction and ensures the
@ output is in the 0..p-1 range.
@
@ Implementation uses the macro MQ to designate the value 19. All
@ functions can in fact be used with other moduli 2^255-t for odd values
@ of t in the 1..32767 range; the MQ macro just has to be adjusted
@ accordingly. The gf_inv_inner() and gf_legendre_inner() functions have
@ some extra requirements:
@ - Both functions expect the modulus to be a prime integer (i.e. that
@ we work in a finite field).
@ - gf_inv_inner() requires the INVT510 macro to evaluate to the correct
@ constant value of 1/2^510 modulo p. If MQ is changed, then this
@ value must be adjusted accordingly.
@ All other functions work just as well with any modulus as long as MQ
@ is odd and in the supported range.
@
@ (If we assumed that MQ is in the 1..127 range, then a few cycles could
@ be saved here and there by replacing some 2-cycle 'ldr' opcodes with
@ 1-cycle 'movs' opcodes. This might save up to 2000 cycles in total,
@ i.e. less than 0.1% of the total cost. This optimization has not been
@ implemented here.)
@
@
@ The gf_inv_inner() function implements inversion in the field using the
@ optimized binary GCD algorithm described in:
@ https://eprint.iacr.org/2020/972
@ On the ARM Cortex-M0+, this function is much faster than the
@ traditional method using Fermat's Little Theorem: the latter requires
@ 254 squarings and about 11 extra multiplications, for a total cost of
@ at least 270000 cycles, while gf_inv_inner() completes in 54793 cycles
@ only. gf_inv_inner() is fully constant-time, like the rest of the code.
@
@ gf_inv_inner() requires the modulus to be prime only because it assumes
@ that the GCD will be 1 unless the source value is zero. The reliance on
@ a prime modulus could be removed by instead performing a multiplication
@ at the end to verify that an inverse has truly been obtained; the
@ overhead would be about 1500 to 2000 cycles.
@ The constant precomputed value 1/2^510 modulo p would be removed by
@ replacing that multiplication with two Montgomery reductions. This would
@ also imply an overhead of a few thousand cycles, and need some extra code
@ for Montgomery reduction.
@
@
@ The gf_legendre_inner() function computes the Legendre symbol for a field
@ element. This is not actually needed for X25519, and is included here
@ only because it could be helpful in other operations adjacent to
@ X25519, e.g. the use of the Elligator2 map for encoding/hashing values
@ into curve points in a constant-time way. The Legendre symbol of x is:
@ 1 if x is a non-zero quadratic residue in the field
@ -1 if x is not a quadratic residue in the field
@ 0 if x is zero
@ The traditional method is again Fermat's Little Theorem: for a prime
@ p, the Legendre symbol of x is equal to x^((p-1)/2) mod p. This would
@ again require about 270000 cycles for p = 2^255-19.
@
@ The algorithm implemented here is roughly the same as the binary GCD
@ used for inversion. It internally computes the GCD of x and p with the
@ exact same steps (hence, it always converges with the same number of
@ iterations); it does not keep track of the Bezout coefficients, since
@ these are not needed for a Legendre symbol; however, it follows value
@ updates to compute the symbol. What is actually computed is the
@ Kronecker symbol (x|p), with the following properties:
@
@ (x|n) is equal to the Legendre symbol of x modulo n when n is a
@ nonnegative odd prime.
@
@ (x|n) == (y|n) if x == y mod n and either n > 0, or x and y have
@ the same sign.
@
@ If n and m are not both negative, then (n|m) == (m|n), unless
@ both n == 3 mod 4 and m == 3 mod 4, in which case (n|m) == -(m|n).
@ (This is the law of quadratic reciprocity.)
@
@ (2|n) == 1 if n == 1 or 7 mod 8, or -1 if n == 3 or 5 mod 8.
@
@ In the course of the binary GCD algorithm, we work over two values a
@ and b, such that they both converge toward 0 and 1. b is always odd.
@ Each iteration consists in three successive steps:
@
@ 1. If a and b are odd and a < b, then a and b are exchanged.
@ 2. If a is odd, then a is replaced with a-b.
@ 3. a <- a/2
@
@ When adapted to the Legendre symbol computation, we use the same steps,
@ but also maintain the expected Kronecker symbol in a variable j which
@ is initially 1, and is negated when approriate:
@
@ - Step 1 exercises the law of quadratic reciprocity; j is negated if
@ both a and b are equal to 3 modulo 4 at the time of the swap.
@
@ - Step 2 does not change the Kronecker symbol; a critical observation
@ here is that throughout the optimized binary GCD algorithm, it can
@ never happen that a and b are both negative.
@
@ - Step 3 negates j if and only if b == 3 or 5 mod 8 at that point.
@
@ These updates to j only need to look at the low bits of a and b (up to
@ three bits) and is thus largely compatible with the intermediate values
@ maintained by the optimized binary GCD in its inner loop. This implies
@ a relatively low overhead for the inner loop iterations. Combined with
@ the savings obtained by not keeping track of the Bezout coefficients,
@ we finally achieve the Legendre symbol computation in 43726 cycles, i.e.
@ even faster than inversions. This implementation is fully constant-time.
@
@ gf_legendre_inner() requires the modulus p to be prime only because it
@ assumes the GCD of x and p to be 1 as long as x != 0. The implementation
@ could be modified to support a non-prime modulus (in which case this
@ would compute the Jacobi symbol) with a slight overhead (the final
@ iterations may not be specialized out, and we would need an extra
@ comparison of b with 1 to check for a non-invertible input case).
@
@ =======================================================================
.syntax unified
.cpu cortex-m0
.file "x25519-cm0.S"
.text
@ =======================================================================
@ All 'inner' functions are local to this file and may use non-conformant
@ ABIs (about preservation and modification of registers). All global
@ functions (callable from the outside) conform to the ABI.
@ =======================================================================
@ We work in finite field 2^255-MQ, with 1 <= MQ <= 32767.
#define MQ 19
@ Constant 1/2^510 mod p, in little-endian order.
#define INVT510 0xE72E181B, 0x4A75B7AA, 0x209ED8FF, 0x9E237502, 0x2595A0F9, 0x8F3F1D13, 0x5242A8C6, 0x093805AC
@ =======================================================================
@ void gf_add_inner(gf *d, const gf *a, const gf *b)
@
@ Registers r0 to r8 and r10 to r12 are modified.
@
@ Cost: 71
@ =======================================================================
.align 1
.thumb
.thumb_func
.type gf_add_inner, %function
gf_add_inner:
@ Load and perform main addition.
ldm r1!, { r3, r4 }
ldm r2!, { r5, r6 }
adds r3, r5
adcs r4, r6
mov r8, r3
mov r10, r4
ldm r1!, { r3, r4 }
ldm r2!, { r5, r6 }
adcs r3, r5
adcs r4, r6
mov r11, r3
mov r12, r4
ldm r1!, { r3, r4 }
ldm r2!, { r5, r6 }
adcs r3, r5
adcs r4, r6
ldm r1!, { r5, r6 }
ldm r2, { r1, r2 }
adcs r5, r1
adcs r6, r2
@ Sum is now in r8:r10:r11:r12:r3:r4:r5:r6, with carry in C flag.
@ We recover the carry and combine it with the top bit of the top
@ word to get the result modulo 2^255 (in r7). We also truncate
@ the top word.
sbcs r1, r1
adds r1, #1
lsls r1, r1, #1
lsrs r7, r6, #31
adds r7, r1
lsls r6, r6, #1
lsrs r6, r6, #1
@ Using 2^255 = MQ mod p, we perform the reduction. Since we
@ cleared the top bit of the top word, this cannot create an
@ extra carry.
ldr r1, const_gf_add_mq
muls r7, r1
mov r1, r8
mov r2, r10
adds r1, r7
eors r7, r7
adcs r2, r7
stm r0!, { r1, r2 }
mov r1, r11
mov r2, r12
adcs r1, r7
adcs r2, r7
adcs r3, r7
adcs r4, r7
adcs r5, r7
adcs r6, r7
stm r0!, { r1, r2, r3, r4, r5, r6 }
bx lr
.align 2
const_gf_add_mq:
.long MQ
.size gf_add_inner, .-gf_add_inner
@ =======================================================================
@ void gf_sub_inner(gf *d, const gf *a, const gf *b)
@
@ Registers r0 r1 to r8 and r10 to r12 are modified.
@
@ Cost: 72
@ =======================================================================
.align 1
.thumb
.thumb_func
.type gf_sub_inner, %function
gf_sub_inner:
@ Load and perform main subtraction.
ldm r1!, { r3, r4 }
ldm r2!, { r5, r6 }
subs r3, r5
sbcs r4, r6
mov r8, r3
mov r10, r4
ldm r1!, { r3, r4 }
ldm r2!, { r5, r6 }
sbcs r3, r5
sbcs r4, r6
mov r11, r3
mov r12, r4
ldm r1!, { r3, r4 }
ldm r2!, { r5, r6 }
sbcs r3, r5
sbcs r4, r6
ldm r1!, { r5, r6 }
ldm r2, { r1, r2 }
sbcs r5, r1
sbcs r6, r2
@ Difference is now in r8:r10:r11:r12:r3:r4:r5:r6, with borrow in
@ C flag.
@
@ Suppose that we recover the borrow (of value -1 or 0) and combine
@ it with the top bit of the value into a value x (with -2 <= x <= 1);
@ then, we truncate the remaining value to 255 bits (the top bit
@ of the top word, currently in r6, is set to 0).
@ We then need to add x*MQ to the trunacted value (which is in
@ the 0..2^255-1 range). To get a value that fits in 256 bits, we
@ also add an extra p = 2^255 - MQ; i.e. in total we add
@ 2^255 + (x-1)*MQ. Since x-1 <= 0, the result cannot exceed 2^256-1.
@ This is equivalent to adding 2^255 and then adding (x-1)*MQ. The
@ addition of 2^255 to the truncated value is equivalent to forcing
@ its top bit to 1.
@ Get the borrow and top bit into value x. We in fact recover -(x-1)
@ into r7.
sbcs r1, r1
lsls r1, r1, #1
asrs r7, r6, #31
subs r7, r1
adds r7, #1
@ Force top bit of top word to 1.
movs r2, #1
lsls r2, r2, #31
orrs r6, r2
@ Add (x-1)*MQ, i.e. subtract -(x-1)*MQ. We have -(x-1) in r7,
@ and it is nonnegative.
ldr r1, const_gf_sub_mq
muls r7, r1
mov r1, r8
mov r2, r10
subs r1, r7
eors r7, r7
sbcs r2, r7
stm r0!, { r1, r2 }
mov r1, r11
mov r2, r12
sbcs r1, r7
sbcs r2, r7
sbcs r3, r7
sbcs r4, r7
sbcs r5, r7
sbcs r6, r7
stm r0!, { r1, r2, r3, r4, r5, r6 }
bx lr
.align 2
const_gf_sub_mq:
.long MQ
.size gf_sub_inner, .-gf_sub_inner
@ =======================================================================
@ Multiply rl (32 bits) by rb (32 bits) and store result in rl:rh.
@ rb is consumed. On output, CF = 0.
@ ru and rt are two scratch registers.
@ Cost: 17
.macro MUL32x32 rl, rh, rb, rt, ru
@ hi(a)*hi(b) -> rh
lsrs \rt, \rb, #16
lsrs \rh, \rl, #16
muls \rh, \rt
@ lo(a)*lo(b) -> rl
@ lo(a)*hi(b) -> rt
@ hi(a)*lo(b) -> ru
uxth \rb, \rb
lsrs \ru, \rl, #16
uxth \rl, \rl
muls \rt, \rl
muls \ru, \rb
muls \rl, \rb
@ Add rt and ru at the right place. We can use rb as scratch.
lsrs \rb, \rt, #16
lsls \rt, \rt, #16
adds \rl, \rt
adcs \rh, \rb
lsrs \rb, \ru, #16
lsls \ru, \ru, #16
adds \rl, \ru
adcs \rh, \rb
.endm
@ Perform 64x64->128 multiplication.
@ ra0:ra1 first operand
@ rb0:rb1 second operand
@ Result is written in ra0:rb0:ra1:rb1
@ rt1, rt2, rt3, rt4, rh5 and rh6 are scratch registers; rh5 and rh6 are
@ high registers (r8+).
@ Cost: 30 + 3*cost(MUL32x32) = 81
.macro MUL64x64 ra0, ra1, rb0, rb1, rt1, rt2, rt3, rt4, rh5, rh6
@ Save rb0 and rb1 into high registers.
mov \rh5, \rb0
mov \rh6, \rb1
@ rt1 <- |a0 - a1|, sign in rt3 (-1 for _positive_, 0 otherwise)
subs \rt1, \ra1, \ra0
sbcs \rt3, \rt3
eors \rt1, \rt3
subs \rt1, \rt3
@ rt4 <- |b0 - b1|, sign in ra0 (-1 for negative, 0 otherwise)
subs \rt4, \rb0, \rb1
sbcs \rb1, \rb1
eors \rt4, \rb1
subs \rt4, \rb1
@ rt3 <- -sign(a0-a1)*sign(b0-b1) (-1 for same sign, 0 otherwise)
eors \rt3, \rb1
@ rb0 and rb1 are now scratch registers.
@ rt1:rt2:rt3 <- xee = -sign(a0-a1)*sign(b0-b1) XOR |a0-a1|*|b0-b1|
MUL32x32 \rt1, \rt2, \rt4, \rb0, \rb1
eors \rt1, \rt3
eors \rt2, \rt3
@ a0*b0 -> ra0:rb0. Also save lo(xee) into rh5.
mov \rt4, \rh5
mov \rh5, \rt1
MUL32x32 \ra0, \rb0, \rt4, \rt1, \rb1
@ a1*b1 -> ra1:rb1. Also save hi(xee) into rh6.
mov \rt4, \rh6
mov \rh6, \rt2
MUL32x32 \ra1, \rb1, \rt4, \rt1, \rt2
@ We have a0*b0 and a1*b1 in place. We must still:
@ - add 2^16*a0*b0
@ - add 2^16*a1*b1
@ - add ee (with sign)
@ Adding ee is really adding xee - sign(ee), with:
@ xee = rh5:rh6:rt3
@ sign(ee) = rt3:rt3:rt3
@ Thus, we need to add rh5:rh6:rt3, and subtract rt3:rt3:rt3.
@ Since rt3 = 0 or -1, subtraction of rt3:rt3:rt3 really is
@ addition of 0 or 1, which we can smuggle as an extra carry
@ when adding xee.
@ Recover xee into rt1:rt2; sign is still in rt3.
mov \rt1, \rh5
mov \rt2, \rh6
@ Set rt4 to 0.
eors \rt4, \rt4
@ Add a0*b0 into rt1:rt2:rt3. We also put the extra carry there.
asrs \rt3, \rt3, #1
adcs \rt1, \ra0
adcs \rt2, \rb0
adcs \rt3, \rt4
@ Add a1*b1 into r1:rt2:rt3.
adds \rt1, \ra1
adcs \rt2, \rb1
adcs \rt3, \rt4
@ Add rt1:rt2:rt3 into the result.
adds \rb0, \rt1
adcs \ra1, \rt2
adcs \rb1, \rt3
.endm
@ Input:
@ id output buffer index (in stack)
@ alt if zero, then:
@ r1 = pointer to first integer (a)
@ r2 = pointer to second integer (b)
@ else:
@ first integer (a) is at stack index iin
@ b[0]..b[3] is in r0:r1:r2:r3
@ second integer (b) is not on the stack
@ alt must be 0 or 1
@ iin index for inputs, if alt != 0 (ignored if alt == 0)
@
@ Output: result goes to index itmp (8 words). If alt != 0, then the
@ eight output words are modified, but only the first two contain the
@ correct values; the remaining 6 words of output are returned in r1..r6.
@
@ All registers except r11 are consumed (including operands).
@ IMPORTANT: destination MUST NOT overlap with either source.
@ Cost:
@ alt = 0: 114 + 3*cost(MUL64x64) = 357
@ alt = 1: 103 + 3*cost(MUL64x64) = 346
.macro MUL128x128 id, alt, iin
@ If alt == 0, then we save input pointers in r12 and r14.
@ If alt != 0, then we save b0 on the stack and b1 (b[2]:b[3])
@ in r12:r14; in that case, stack indexes are skewed until we
@ pop b0 again.
@ a0*b0 -> dest
.if (\alt) != 0
push { r0, r1 }
mov r12, r2
mov r14, r3
movs r2, r1
add r1, sp, #(4 * ((\iin) + 2))
ldm r1, { r1, r3 }
MUL64x64 r0, r2, r1, r3, r4, r5, r6, r7, r8, r10
add r6, sp, #(4 * ((\id) + 2))
stm r6!, { r0, r1, r2, r3 }
.else
ldm r1!, { r4, r6 }
mov r12, r1
ldm r2!, { r5, r7 }
mov r14, r2
MUL64x64 r4, r6, r5, r7, r0, r1, r2, r3, r8, r10
add r0, sp, #(4 * (\id))
stm r0!, { r4, r5, r6, r7 }
.endif
@ a1*b1 -> dest + 4
.if (\alt) != 0
add r0, sp, #(4 * ((\iin) + 2 + 2))
ldm r0, { r0, r2 }
mov r1, r12
mov r3, r14
.else
mov r0, r12
ldm r0, { r0, r2 }
mov r1, r14
ldm r1, { r1, r3 }
.endif
MUL64x64 r0, r2, r1, r3, r4, r5, r6, r7, r8, r10
.if (\alt) != 0
add r4, sp, #(4 * ((\id) + 4 + 2))
.else
add r4, sp, #(4 * ((\id) + 4))
.endif
stm r4!, { r0, r1, r2, r3 }
@ |a1-a0| -> r2:r4, sign in r6
.if (\alt) != 0
add r0, sp, #(4 * ((\iin) + 2))
.else
mov r0, r12
subs r0, #8
.endif
ldm r0, { r0, r1, r2, r4 }
subs r2, r0
sbcs r4, r1
sbcs r6, r6
eors r2, r6
eors r4, r6
subs r2, r6
sbcs r4, r6
@ |b0-b1| -> r1:r3, sign in r0
.if (\alt) != 0
pop { r1, r3 } @ This removes the skew on stack indexes
mov r5, r12
mov r7, r14
.else
mov r1, r14
subs r1, #8
ldm r1, { r1, r3, r5, r7 }
.endif
subs r1, r5
sbcs r3, r7
sbcs r0, r0
eors r1, r0
eors r3, r0
subs r1, r0
sbcs r3, r0
@ -sign(a0-a1)*sign(b0-b1) -> r12 (-1 if same sign, 0 otherwise)
eors r6, r0
mov r12, r6
@ -sign(a0-a1)*sign(b0-b1) XOR |a0-a1|*|b0-b1| -> r1:r2:r4:r5
@ Also retrieve -sign(a0-a1)*sign(b0-b1) into r5.
MUL64x64 r1, r3, r2, r4, r0, r5, r6, r7, r8, r10
mov r5, r12
eors r1, r5
eors r2, r5
eors r3, r5
eors r4, r5
@ Final assembly. The destination array already contains a0*b0
@ and a1*b1. We must add into (shifted by two words):
@ - a0*b0
@ - a1*b1
@ - xee:r12
@ and also subtract r12:r12:r12:r12:r12. Since r12 = 0 or -1, this
@ can be done by adding 0 or 1, which is only an initial carry
@ in one of the additions above.
@ We will accumulate the intermediate result in r1:r2:r3:r4:r5.
@ Add a0*b0 into the accumulator; also perform subtraction of
@ r12:r12:r12:r12:r12 here.
add r0, sp, #(4 * (\id))
ldm r0!, { r6, r7 }
asrs r5, r5, #1
adcs r1, r6
adcs r2, r7
ldm r0!, { r6, r7 }
adcs r3, r6
adcs r4, r7
eors r7, r7
adcs r5, r7
@ Add a1*b1 into the accumulator.
ldm r0!, { r6, r7 }
adds r1, r6
adcs r2, r7
ldm r0!, { r6, r7 }
adcs r3, r6
adcs r4, r7
eors r7, r7
adcs r5, r7
@ Add the accumulator into the result, at the right position.
@ At that point, the destination buffer contains:
@ a0*b0 + 2^128*a1*b1
@ The mathematical result is (a0+2^64*a1)*(b0+2^64*b1); therefore,
@ the accumulator contains:
@ a0*b1 + a1*b0
@ which is necessarily positive. The sign extension of that value
@ into 6 words is then done by appending a word of value zero.
subs r0, #24
ldm r0!, { r6, r7 }
adds r1, r6
adcs r2, r7
ldm r0!, { r6, r7 }
adcs r3, r6
adcs r4, r7
ldm r0!, { r6, r7 }
adcs r5, r6
eors r6, r6
adcs r6, r7
@ We write out the final accumulator only in non-alt mode; otherwise,
@ the caller will use the registers directly.
.if (\alt) == 0
subs r0, #24
stm r0!, { r1, r2, r3, r4, r5, r6 }
.endif
.endm
@ Input:
@ id output buffer index (in stack)
@ r1 pointer to first integer (a)
@ r2 pointer to second integer (b)
@ itmp index in stack for temporary area (12 words) (counted in words)
@ IMPORTANT: destination MUST NOT overlap with either source.
@ Cost: 222 + 2*cost(MUL128x128[alt=0]) + cost(MUL128x128[alt=1]) = 1282
.macro MUL256x256 id, itmp
@ Save b pointer to stack. This means that \id and \itmp have to be
@ adjusted whenever used (since we pushed 1 extra word), until we
@ pop the value again.
push { r2 }
mov r11, r1
@ a0*b0 -> dest
MUL128x128 ((\id) + 1), 0, 0
@ a1*b1 -> dest + 8
mov r1, r11
ldr r2, [sp]
adds r1, #16
adds r2, #16
MUL128x128 ((\id) + 9), 0, 0
@ |a1-a0| -> tmp, save sign into r11.
mov r1, r11
ldm r1, { r0, r1, r2, r3, r4, r5, r6, r7 }
subs r4, r0
sbcs r5, r1
sbcs r6, r2
sbcs r7, r3
sbcs r0, r0
eors r4, r0
eors r5, r0
eors r6, r0
eors r7, r0
subs r4, r0
sbcs r5, r0
sbcs r6, r0
sbcs r7, r0
mov r11, r0
add r1, sp, #(4 * ((\itmp) + 1))
stm r1!, { r4, r5, r6, r7 }
@ |b0-b1| -> r0..r3, and combine sign with the one in stack slot.
pop { r2 } @ This removes the skew on stack indexes
ldm r2, { r0, r1, r2, r3, r4, r5, r6, r7 }
subs r0, r4
sbcs r1, r5
sbcs r2, r6
sbcs r3, r7
sbcs r7, r7
eors r0, r7
eors r1, r7
eors r2, r7
eors r3, r7
subs r0, r7
sbcs r1, r7
sbcs r2, r7
sbcs r3, r7
mov r6, r11
eors r7, r6
mov r11, r7
@ We don't store |b0-b1| on the stack, MUL128x128 can reuse it
@ from the registers themselves.
@ |a0-a1|*|b0-b1| -> tmp + 4
MUL128x128 ((\itmp) + 4), 1, (\itmp)
@ Load |a0-a1|*|b0-b1| and XOR with sign word.
@ Words 2..7 of the value are already in r1..r6.
mov r7, r11
eors r3, r7
eors r4, r7
eors r5, r7
eors r6, r7
mov r10, r3
mov r11, r4
mov r12, r5
mov r14, r6
add r4, sp, #(4 * ((\itmp) + 4))
ldm r4!, { r0, r3 }
eors r0, r7
eors r1, r7
eors r2, r7
eors r3, r7
@ Accumulator: r0:r3:r1:r2:r10:r11:r12:r14:r7
@ Add a0*b0 to accumulator. Also handle extra carry (for the
@ subtraction of the sign, which is 0 or -1).
add r4, sp, #(4 * (\id))
asrs r7, r7, #1
mov r8, r7
ldm r4!, { r5, r6, r7 }
adcs r0, r5
adcs r3, r6
adcs r1, r7
mov r5, r10
mov r6, r11
mov r7, r12
mov r10, r0
mov r11, r3
mov r12, r1
@ Accumulator: r10:r11:r12:r2:r5:r6:r7:r14:r8
ldm r4!, { r0, r1, r3 }
adcs r2, r0
adcs r5, r1
adcs r6, r3
mov r1, r14
mov r0, r8
mov r14, r2
@ Accumulator: r10:r11:r12:r14:r5:r6:r7:r1:r0
ldm r4!, { r2, r3 }
adcs r7, r2
adcs r1, r3
@ eors does not change the carry flag.
eors r2, r2
adcs r0, r2
@ Accumulator: r10:r11:r12:r14:r5:r6:r7:r1:r0
@ Reorganize to get the low accumulator words in low registers.
mov r8, r0
mov r0, r10
mov r10, r5
mov r2, r12
mov r3, r14
mov r14, r1
mov r1, r11
mov r11, r6
mov r12, r7
@ Accumulator: r0:r1:r2:r3:r10:r11:r12:r14:r8
@ Add a1*b1 to accumulator.
add r4, sp, #(4 * ((\id) + 8))
ldm r4!, { r5, r6, r7 }
adds r0, r5
adcs r1, r6
adcs r2, r7
mov r5, r10
mov r6, r11
mov r7, r12
mov r10, r0
mov r11, r1
mov r12, r2
@ Accumulator: r10:r11:r12:r3:r5:r6:r7:r14:r8
ldm r4!, { r0, r1, r2 }
adcs r3, r0
adcs r5, r1
adcs r6, r2
mov r1, r14
mov r0, r8
mov r14, r3
@ Accumulator: r10:r11:r12:r14:r5:r6:r7:r1:r0
ldm r4!, { r2, r3 }
adcs r7, r2
adcs r1, r3
eors r2, r2
adcs r0, r2
@ Accumulator: r10:r11:r12:r14:r5:r6:r7:r1:r0
@ The accumulator is necessarily nonnegative. We (virtually)
@ extend it with three zeros, and add it to the destination
@ buffer at the right place.
@ Reorganization to get the low limbs into low registers.
mov r8, r0
mov r0, r10
mov r10, r5
mov r2, r12
mov r3, r14
mov r14, r1
mov r1, r11
mov r11, r6
mov r12, r7
@ Accumulator: r0:r1:r2:r3:r10:r11:r12:r14:r8
add r4, sp, #(4 * ((\id) + 4))
ldr r5, [r4]
ldr r6, [r4, #4]
adds r0, r5
adcs r1, r6
ldr r5, [r4, #8]
ldr r6, [r4, #12]
adcs r2, r5
adcs r3, r6
stm r4!, { r0, r1, r2, r3 }
mov r0, r10
mov r1, r11
mov r2, r12
mov r3, r14
ldr r5, [r4]
ldr r6, [r4, #4]
adcs r0, r5
adcs r1, r6
ldr r5, [r4, #8]
ldr r6, [r4, #12]
adcs r2, r5
adcs r3, r6
stm r4!, { r0, r1, r2, r3 }
ldm r4!, { r0, r1, r2, r3 }
mov r7, r8
adcs r0, r7
sbcs r5, r5
adds r5, #1
eors r6, r6
adds r1, r5
adcs r2, r6
adcs r3, r6
subs r4, #16
stm r4!, { r0, r1, r2, r3 }
.endm
@ Input:
@ rx input value to square (consumed)
@ Output:
@ rl:rh square of the input
@ rx is consumed. rt is scratch.
@ Cost: 10
.macro SQR32 rl, rh, rx, rt
uxth \rl, \rx
lsrs \rx, \rx, #16
movs \rh, \rx
muls \rx, \rl
muls \rl, \rl
muls \rh, \rh
lsls \rt, \rx, #17
lsrs \rx, \rx, #15
adds \rl, \rt
adcs \rh, \rx
.endm
@ Input:
@ rx1:rx2 value to square (64-bit) (consumed)
@ Output:
@ rd0..rd3 square of the input value
@ rx1 and rx2 are consumed. rt1 and rt2 are scratch. All registers must
@ be different.
@ Cost: 14 + 3*cost(SQR32) = 44
.macro SQR64 rd0, rd1, rd2, rd3, rx1, rx2, rt1, rt2
@ |a0-a1| -> rd0
subs \rd0, \rx1, \rx2
sbcs \rd1, \rd1
eors \rd0, \rd1
subs \rd0, \rd1
@ a1^2 -> rd2:rd3
SQR32 \rd2, \rd3, \rx2, \rd1
@ (a0-a1)^2 -> rt1:rx2
SQR32 \rt1, \rx2, \rd0, \rd1
@ a0^2 -> rd0:rd1
SQR32 \rd0, \rd1, \rx1, \rt2
@ Subtract a0^2 and a1^2 from (a0-a1)^2 (in rt1:rx2:rt2)
eors \rx1, \rx1
subs \rt1, \rd0
sbcs \rx2, \rd1
sbcs \rt2, \rt2
subs \rt1, \rd2
sbcs \rx2, \rd3
sbcs \rt2, \rx1
@ rt1:rx2:rt2 now contains -2*a0*a1, which we subtract from
@ the result (with a one-word shift).
subs \rd1, \rt1
sbcs \rd2, \rx2
sbcs \rd3, \rt2
.endm
@ Input:
@ r1 pointer to value to square (128-bit)
@ Output:
@ Eight stack words, at index id
@ Cost: 89 + 3*cost(SQR64) = 221
@ Input:
@ id output buffer index (in stack)
@ alt if zero, then:
@ r1 = pointer to source integer (a)
@ else:
@ source integer (a) is in r0:r1:r2:r3
@ alt must be 0 or 1
@
@ Output: result goes to index itmp (8 words).
@ If alt is non-zero, then all output words are modified but only the
@ first two contain the correct result; the remaining 6 words are
@ returned in r2..r7.
@
@ All registers except r8 are consumed.
@ IMPORTANT: destination MUST NOT overlap with the source.
@ Cost:
@ alt = 0: 89 + 3*cost(SQR64) = 221
@ alt = 1: 76 + 3*cost(SQR64) = 208
.macro SQR128 id, alt
.if (\alt) != 0
@ Save source in high registers.
mov r10, r0
mov r11, r1
mov r12, r2
mov r14, r3
@ a0^2 -> dst
SQR64 r4, r5, r6, r7, r0, r1, r2, r3
add r0, sp, #(4 * (\id))
stm r0!, { r4, r5, r6, r7 }
@ a1^2 -> dst + 4
mov r0, r12
mov r1, r14
SQR64 r4, r5, r6, r7, r0, r1, r2, r3
add r0, sp, #(4 * ((\id) + 4))
stm r0!, { r4, r5, r6, r7 }
@ Load source into r4..r7
mov r4, r10
mov r5, r11
mov r6, r12
mov r7, r14
.else
@ a0^2 -> dst
ldm r1!, { r2, r3 }
mov r10, r1
SQR64 r4, r5, r6, r7, r2, r3, r0, r1
add r0, sp, #(4 * (\id))
stm r0!, { r4, r5, r6, r7 }
@ a1^2 -> dst + 4
mov r1, r10
ldm r1, { r0, r1 }
SQR64 r4, r5, r6, r7, r0, r1, r2, r3
add r0, sp, #(4 * ((\id) + 4))
stm r0!, { r4, r5, r6, r7 }
@ Load source into r0..r3
mov r0, r10
subs r0, #8
ldm r0!, { r4, r5, r6, r7 }
.endif
@ Compute (a0-a1)^2 -> r0..r3
subs r4, r6
sbcs r5, r7
sbcs r3, r3
eors r4, r3
eors r5, r3
subs r4, r3
sbcs r5, r3
SQR64 r0, r1, r2, r3, r4, r5, r6, r7
@ Subtract a0^2 from accumulator.
add r4, sp, #(4 * (\id))
ldm r4, { r4, r5, r6, r7 }
subs r0, r4
sbcs r1, r5
sbcs r2, r6
sbcs r3, r7
sbcs r6, r6
@ Accumulator is: r0:r1:r2:r3:r6
@ Subtract a1^2 from accumulator.
add r4, sp, #(4 * ((\id) + 4))
ldm r4!, { r5, r7 }
subs r0, r5
sbcs r1, r7
ldm r4!, { r5, r7 }
sbcs r2, r5