-
Notifications
You must be signed in to change notification settings - Fork 6
/
p256-cortex-m4-asm-gcc.S
3294 lines (2814 loc) · 58 KB
/
p256-cortex-m4-asm-gcc.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
.syntax unified
.thumb
// Copyright (c) 2017-2021 Emil Lenngren
// Copyright (c) 2021 Shortcut Labs AB
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// P256-Cortex-M4
#include "p256-cortex-m4-config.h"
// This is an armv7 implementation of P-256.
//
// When secret data is processed, the implementation runs in constant time,
// and no conditional branches depend on secret data.
.text
.align 2
#if (include_p256_basemult || include_p256_varmult) && has_d_cache
// Selects one of many values
// *r0 = output, *r1 = table, r2 = num coordinates, r3 = index to choose [0..7]
// 547 cycles for affine coordinates
.type P256_select_point, %function
P256_select_point:
.global P256_select_point
push {r0,r2,r3,r4-r11,lr}
//frame push {r4-r11,lr}
//frame address sp,48
subs r2,#1
lsls r2,#5
0:
rsbs r3,#0
sbcs r3,r3,r3
mvns r3,r3
ldm r1!,{r6-r12,lr}
ands r6,r3
ands r7,r3
and r8,r3
and r9,r3
and r10,r3
and r11,r3
and r12,r3
and lr,r3
adds r1,r2
movs r3,#1
1:
ldr r0,[sp,#8]
eors r0,r3
mrs r0,apsr
lsrs r0,#30
ldm r1!,{r4,r5}
umlal r6,r2,r0,r4
umlal r7,r3,r0,r5
ldm r1!,{r4,r5}
umlal r8,r2,r0,r4
umlal r9,r3,r0,r5
ldm r1!,{r4,r5}
umlal r10,r2,r0,r4
umlal r11,r3,r0,r5
ldm r1!,{r4,r5}
umlal r12,r2,r0,r4
umlal lr,r3,r0,r5
adds r1,r2
adds r3,#1
cmp r3,#8
bne 1b
ldm sp,{r0,r4}
stm r0!,{r6-r12,lr}
str r0,[sp]
sub r1,r1,r2, lsl #3
subs r1,#224
subs r4,#1
str r4,[sp,#4]
ldr r3,[sp,#8]
bne 0b
add sp,#12
//frame address sp,36
pop {r4-r11,pc}
.size P256_select_point, .-P256_select_point
#endif
#if include_p256_verify || include_p256_sign
// in: *r0 = out, *r1 = a, *r2 = b
// quite slow, so only used in code not critical for performance
.type mul288x288, %function
mul288x288:
push {r4-r11,lr}
//frame push {r4-r11,lr}
mov r4,r0
mov r5,r2
mov r6,r1
movs r1,#72
bl setzero
ldm r5,{r0-r2,r8-r12,lr}
movs r7,#9
0:
ldm r6!,{r5}
push {r6,r7}
//frame address sp,44
movs r3,#0
ldm r4,{r6,r7}
umaal r6,r3,r5,r0
umaal r7,r3,r5,r1
stm r4!,{r6,r7}
ldm r4,{r6,r7}
umaal r6,r3,r5,r2
umaal r7,r3,r5,r8
stm r4!,{r6,r7}
ldm r4,{r6,r7}
umaal r6,r3,r5,r9
umaal r7,r3,r5,r10
stm r4!,{r6,r7}
ldm r4,{r6,r7}
umaal r6,r3,r5,r11
umaal r7,r3,r5,r12
stm r4!,{r6,r7}
ldm r4,{r6}
umaal r3,r6,r5,lr
stm r4!,{r3,r6}
subs r4,r4,#36
pop {r6,r7}
//frame address sp,36
subs r7,r7,#1
bne 0b
pop {r4-r11,pc}
.size mul288x288, .-mul288x288
// in: r0 = address, r1 = num bytes (> 0, must be multiple of 8)
.type setzero, %function
setzero:
movs r2,#0
movs r3,#0
0:
stm r0!,{r2,r3}
subs r1,r1,#8
bne 0b
bx lr
.size setzero, .-setzero
#endif
// Field arithmetics for the prime field where p = 2^256 - 2^224 + 2^192 + 2^96 - 1
// Multiplication and Squaring use Montgomery Modular Multiplication where R = 2^256
// To convert a value to Montgomery class, use P256_mulmod(value, R^512 mod p)
// To convert a value from Montgomery class to standard form, use P256_mulmod(value, 1)
#if include_p256_mult || include_p256_decompress_point || include_p256_decode_point
#if use_mul_for_sqr
.type P256_sqrmod, %function
P256_sqrmod:
push {r0-r7,lr}
//frame push {lr}
//frame address sp,36
mov r1,sp
mov r2,sp
bl P256_mulmod
add sp,sp,#32
//frame address sp,4
pop {pc}
.size P256_sqrmod, .-P256_sqrmod
#endif
#if has_fpu
// If inputs are A*R mod p and B*R mod p, computes AB*R mod p
// *r1 = in1, *r2 = in2
// out: r0-r7
// clobbers all other registers
.type P256_mulmod, %function
P256_mulmod:
push {lr}
//frame push {lr}
vmov s4,r2
vldm r1,{s8-s15}
ldm r2,{r2,r3,r4,r5}
vmov r0,r10,s8,s9
umull r6,r1,r2,r0
umull r7,r12,r3,r0
umaal r7,r1,r2,r10
vmov s0,s1,r6,r7
umull r8,r6,r4,r0
umaal r8,r1,r3,r10
umull r9,r7,r5,r0
umaal r9,r1,r4,r10
umaal r1,r7,r5,r10
vmov lr,r0,s10,s11
umaal r8,r12,r2,lr
umaal r9,r12,r3,lr
umaal r1,r12,r4,lr
umaal r12,r7,r5,lr
umaal r9,r6,r2,r0
umaal r1,r6,r3,r0
umaal r12,r6,r4,r0
umaal r6,r7,r5,r0
vmov s2,s3,r8,r9
vmov r10,lr,s12,s13
mov r9,#0
umaal r1,r9,r2,r10
umaal r12,r9,r3,r10
umaal r6,r9,r4,r10
umaal r7,r9,r5,r10
mov r10,#0
umaal r12,r10,r2,lr
umaal r6,r10,r3,lr
umaal r7,r10,r4,lr
umaal r9,r10,r5,lr
vmov r8,s14
mov lr,#0
umaal lr,r6,r2,r8
umaal r7,r6,r3,r8
umaal r9,r6,r4,r8
umaal r10,r6,r5,r8
//_ _ _ _ _ 6 10 9| 7 | lr 12 1 _ _ _ _
vmov r8,s15
mov r11,#0
umaal r7,r11,r2,r8
umaal r9,r11,r3,r8
umaal r10,r11,r4,r8
umaal r6,r11,r5,r8
//_ _ _ _ 11 6 10 9| 7 | lr 12 1 _ _ _ _
vmov r2,s4
adds r2,r2,#16
ldm r2,{r2,r3,r4,r5}
vmov r8,s8
movs r0,#0
umaal r1,r0,r2,r8
vmov s4,r1
umaal r12,r0,r3,r8
umaal lr,r0,r4,r8
umaal r0,r7,r5,r8 // 7=carry for 9
//_ _ _ _ 11 6 10 9+7| 0 | lr 12 _ _ _ _ _
vmov r8,s9
movs r1,#0
umaal r12,r1,r2,r8
vmov s5,r12
umaal lr,r1,r3,r8
umaal r0,r1,r4,r8
umaal r1,r7,r5,r8 // 7=carry for 10
//_ _ _ _ 11 6 10+7 9+1| 0 | lr _ _ _ _ _ _
vmov r8,s10
mov r12,#0
umaal lr,r12,r2,r8
vmov s6,lr
umaal r0,r12,r3,r8
umaal r1,r12,r4,r8
umaal r10,r12,r5,r8 // 12=carry for 6
//_ _ _ _ 11 6+12 10+7 9+1| 0 | _ _ _ _ _ _ _
vmov r8,s11
mov lr,#0
umaal r0,lr,r2,r8
vmov s7,r0
umaal r1,lr,r3,r8
umaal r10,lr,r4,r8
umaal r6,lr,r5,r8 // lr=carry for saved
//_ _ _ _ 11+lr 6+12 10+7 9+1| _ | _ _ _ _ _ _ _
vmov r0,r8,s12,s13
umaal r1,r9,r2,r0
vmov s8,r1
umaal r9,r10,r3,r0
umaal r10,r6,r4,r0
umaal r11,r6,r5,r0 // 6=carry for next
//_ _ _ 6 11+lr 10+12 9+7 _ | _ | _ _ _ _ _ _ _
umaal r9,r7,r2,r8
umaal r10,r7,r3,r8
umaal r11,r7,r4,r8
umaal r6,r7,r5,r8
vmov r0,r8,s14,s15
umaal r10,r12,r2,r0
umaal r11,r12,r3,r0
umaal r6,r12,r4,r0
umaal r7,r12,r5,r0
umaal r11,lr,r2,r8
umaal lr,r6,r3,r8
umaal r6,r7,r4,r8
umaal r7,r12,r5,r8
// 12 7 6 lr 11 10 9 s8 s7 s6 s5 s4 s3 s2 s1 s0
//now reduce
vmov s13,s14,r6,r7
vmov s15,r12
vmov r0,r1,s0,s1
vmov r2,r3,s2,s3
vmov r4,r5,s4,s5
vmov r6,r7,s6,s7
vmov r8,s8
mov r12,#0
adds r3,r0
adcs r4,r4,r1
adcs r5,r5,r2
adcs r6,r6,r0
adcs r7,r7,r1
adcs r8,r8,r0
adcs r9,r9,r1
adcs r10,r10,#0
adcs r11,r11,#0
adcs r12,r12,#0
adds r6,r3
adcs r7,r7,r4 // r4 instead of 0
adcs r8,r8,r2
adcs r9,r9,r3
adcs r10,r10,r2
adcs r11,r11,r3
adcs r12,r12,#0
subs r7,r0
sbcs r8,r8,r1
sbcs r9,r9,r2
sbcs r10,r10,r3
sbcs r11,r11,#0
sbcs r12,r12,#0 // r12 is between 0 and 2
vmov r1,r2,s13,s14
vmov r3,s15
adds r0,lr,r12
adcs r1,r1,#0
mov r12,#0
adcs r12,r12,#0
//adds r7,r4 (added above instead)
adcs r8,r8,r5
adcs r9,r9,r6
adcs r10,r10,r4
adcs r11,r11,r5
adcs r0,r0,r4
adcs r1,r1,r5
adcs r2,r2,r12
adcs r3,r3,#0
mov r12,#0
adcs r12,r12,#0
adcs r10,r10,r7
adcs r11,r11,#0
adcs r0,r0,r6
adcs r1,r1,r7
adcs r2,r2,r6
adcs r3,r3,r7
adcs r12,r12,#0
subs r11,r4
sbcs r0,r0,r5
sbcs r1,r1,r6
sbcs r2,r2,r7
sbcs r3,r3,#0
sbcs r12,r12,#0
// now (T + mN) / R is
// 8 9 10 11 0 1 2 3 12 (lsb -> msb)
subs r8,r8,#0xffffffff
sbcs r9,r9,#0xffffffff
sbcs r10,r10,#0xffffffff
sbcs r11,r11,#0
sbcs r4,r0,#0
sbcs r5,r1,#0
sbcs r6,r2,#1
sbcs r7,r3,#0xffffffff
sbc r12,r12,#0
adds r0,r8,r12
adcs r1,r9,r12
adcs r2,r10,r12
adcs r3,r11,#0
adcs r4,r4,#0
adcs r5,r5,#0
adcs r6,r6,r12, lsr #31
adcs r7,r7,r12
pop {pc}
.size P256_mulmod, .-P256_mulmod
#if !use_mul_for_sqr
// If input is A*R mod p, computes A^2*R mod p
// in/out: r0-r7
// clobbers all other registers
.type P256_sqrmod, %function
P256_sqrmod:
push {lr}
//frame push {lr}
//mul 01, 00
umull r9,r10,r0,r0
umull r11,r12,r0,r1
adds r11,r11,r11
mov lr,#0
umaal r10,r11,lr,lr
//r9 r10 done
//r12 carry for 3rd before col
//r11+C carry for 3rd final col
vmov s0,s1,r9,r10
//mul 02, 11
mov r8,#0
umaal r8,r12,r0,r2
adcs r8,r8,r8
umaal r8,r11,r1,r1
//r8 done (3rd col)
//r12 carry for 4th before col
//r11+C carry for 4th final col
//mul 03, 12
umull r9,r10,r0,r3
umaal r9,r12,r1,r2
adcs r9,r9,r9
umaal r9,r11,lr,lr
//r9 done (4th col)
//r10+r12 carry for 5th before col
//r11+C carry for 5th final col
vmov s2,s3,r8,r9
//mul 04, 13, 22
mov r9,#0
umaal r9,r10,r0,r4
umaal r9,r12,r1,r3
adcs r9,r9,r9
umaal r9,r11,r2,r2
//r9 done (5th col)
//r10+r12 carry for 6th before col
//r11+C carry for 6th final col
vmov s4,r9
//mul 05, 14, 23
umull r9,r8,r0,r5
umaal r9,r10,r1,r4
umaal r9,r12,r2,r3
adcs r9,r9,r9
umaal r9,r11,lr,lr
//r9 done (6th col)
//r10+r12+r8 carry for 7th before col
//r11+C carry for 7th final col
vmov s5,r9
//mul 06, 15, 24, 33
mov r9,#0
umaal r9,r8,r1,r5
umaal r9,r12,r2,r4
umaal r9,r10,r0,r6
adcs r9,r9,r9
umaal r9,r11,r3,r3
//r9 done (7th col)
//r8+r10+r12 carry for 8th before col
//r11+C carry for 8th final col
vmov s6,r9
//mul 07, 16, 25, 34
umull r0,r9,r0,r7
umaal r0,r10,r1,r6
umaal r0,r12,r2,r5
umaal r0,r8,r3,r4
adcs r0,r0,r0
umaal r0,r11,lr,lr
//r0 done (8th col)
//r9+r8+r10+r12 carry for 9th before col
//r11+C carry for 9th final col
//mul 17, 26, 35, 44
umaal r9,r8,r1,r7 //r1 is now dead
umaal r9,r10,r2,r6
umaal r12,r9,r3,r5
adcs r12,r12,r12
umaal r11,r12,r4,r4
//r11 done (9th col)
//r8+r10+r9 carry for 10th before col
//r12+C carry for 10th final col
//mul 27, 36, 45
umaal r9,r8,r2,r7 //r2 is now dead
umaal r10,r9,r3,r6
movs r2,#0
umaal r10,r2,r4,r5
adcs r10,r10,r10
umaal r12,r10,lr,lr
//r12 done (10th col)
//r8+r9+r2 carry for 11th before col
//r10+C carry for 11th final col
//mul 37, 46, 55
umaal r2,r8,r3,r7 //r3 is now dead
umaal r9,r2,r4,r6
adcs r9,r9,r9
umaal r10,r9,r5,r5
//r10 done (11th col)
//r8+r2 carry for 12th before col
//r9+C carry for 12th final col
//mul 47, 56
movs r3,#0
umaal r3,r8,r4,r7 //r4 is now dead
umaal r3,r2,r5,r6
adcs r3,r3,r3
umaal r9,r3,lr,lr
//r9 done (12th col)
//r8+r2 carry for 13th before col
//r3+C carry for 13th final col
//mul 57, 66
umaal r8,r2,r5,r7 //r5 is now dead
adcs r8,r8,r8
umaal r3,r8,r6,r6
//r3 done (13th col)
//r2 carry for 14th before col
//r8+C carry for 14th final col
//mul 67
umull r4,r5,lr,lr // set 0
umaal r4,r2,r6,r7
adcs r4,r4,r4
umaal r4,r8,lr,lr
//r4 done (14th col)
//r2 carry for 15th before col
//r8+C carry for 15th final col
//mul 77
adcs r2,r2,r2
umaal r8,r2,r7,r7
adcs r2,r2,lr
//r8 done (15th col)
//r2 done (16th col)
//msb -> lsb: r2 r8 r4 r3 r9 r10 r12 r11 r0 s6 s5 s4 s3 s2 s1 s0
//lr: 0
//now do reduction
vmov s13,s14,r4,r8
vmov s15,r2 //s15
vmov r1,r2,s0,s1
vmov r8,r7,s2,s3
vmov r6,r5,s4,s5
vmov r4,s6
//lr is already 0
X0 .req r1
X1 .req r2
X2 .req r8
X3 .req r7
X4 .req r6
X5 .req r5
X6 .req r4
X7 .req r0
X8 .req r11
X9 .req r12
X10 .req r10
X11 .req r9
X12 .req r3
X13 .req r7
X14 .req r8
X15 .req r2
adcs X3,X3,X0
adcs X4,X4,X1
adcs X5,X5,X2
adcs X6,X6,X0
adcs X7,X7,X1
adcs X8,X8,X0
adcs X9,X9,X1
adcs X10,X10,#0
adcs X11,X11,#0
adcs lr,lr,#0
adds X6,X3
adcs X7,X7,X4 // X4 instead of 0
adcs X8,X8,X2
adcs X9,X9,X3
adcs X10,X10,X2
adcs X11,X11,X3
adcs lr,lr,#0
subs X7,X0
sbcs X8,X8,X1
sbcs X9,X9,X2
sbcs X10,X10,X3
sbcs X11,X11,#0
sbcs lr,lr,#0 // lr is between 0 and 2
vmov X13,X14,s13,s14
vmov X15,s15
adds X0,X12,lr
adcs X13,X13,#0
mov lr,#0
adcs lr,lr,#0
//adds X7,X4 (added above instead)
adcs X8,X8,X5
adcs X9,X9,X6
adcs X10,X10,X4
adcs X11,X11,X5
adcs X0,X0,X4
adcs X13,X13,X5
adcs X14,X14,lr
adcs X15,X15,#0
mov lr,#0
adcs lr,lr,#0
adcs X10,X10,X7
adcs X11,X11,#0
adcs X0,X0,X6
adcs X13,X13,X7
adcs X14,X14,X6
adcs X15,X15,X7
adcs lr,lr,#0
subs X11,X4
sbcs X0,X0,X5
sbcs X13,X13,X6
sbcs X14,X14,X7
sbcs X15,X15,#0
sbcs lr,lr,#0
// now (T + mN) / R is
// X8 X9 X10 X11 X0 X13 X14 X15 lr (lsb -> msb)
// r11 r12 r10 r9 r1 r7 r8 r2 lr
subs r0,r11,#0xffffffff
sbcs r12,r12,#0xffffffff
sbcs r4,r10,#0xffffffff
sbcs r9,r9,#0
sbcs r6,r1,#0
sbcs r5,r7,#0
sbcs r10,r8,#1
sbcs r8,r2,#0xffffffff
sbcs r7,lr,#0
adds r0,r0,r7
adcs r1,r12,r7
adcs r2,r4,r7
adcs r3,r9,#0
adcs r4,r6,#0
adcs r5,r5,#0
adcs r6,r10,r7, lsr #31
adcs r7,r8,r7
pop {pc}
.size P256_sqrmod, .-P256_sqrmod
#endif
#else
// If inputs are A*R mod p and B*R mod p, computes AB*R mod p
// *r1 = in1, *r2 = in2
// out: r0-r7
// clobbers all other registers
// cycles: 231
.type P256_mulmod, %function
P256_mulmod:
push {r2,lr}
//frame push {lr}
//frame address sp,8
sub sp,#28
//frame address sp,36
ldm r2,{r2,r3,r4,r5}
ldm r1!,{r0,r10,lr}
umull r6,r11,r2,r0
umull r7,r12,r3,r0
umaal r7,r11,r2,r10
push {r6,r7}
//frame address sp,44
umull r8,r6,r4,r0
umaal r8,r11,r3,r10
umull r9,r7,r5,r0
umaal r9,r11,r4,r10
umaal r11,r7,r5,r10
umaal r8,r12,r2,lr
umaal r9,r12,r3,lr
umaal r11,r12,r4,lr
umaal r12,r7,r5,lr
ldm r1!,{r0,r10,lr}
umaal r9,r6,r2,r0
umaal r11,r6,r3,r0
umaal r12,r6,r4,r0
umaal r6,r7,r5,r0
strd r8,r9,[sp,#8]
mov r9,#0
umaal r11,r9,r2,r10
umaal r12,r9,r3,r10
umaal r6,r9,r4,r10
umaal r7,r9,r5,r10
mov r10,#0
umaal r12,r10,r2,lr
umaal r6,r10,r3,lr
umaal r7,r10,r4,lr
umaal r9,r10,r5,lr
ldr r8,[r1],#4
mov lr,#0
umaal lr,r6,r2,r8
umaal r7,r6,r3,r8
umaal r9,r6,r4,r8
umaal r10,r6,r5,r8
//_ _ _ _ _ 6 10 9| 7 | lr 12 11 _ _ _ _
ldr r8,[r1],#-28
mov r0,#0
umaal r7,r0,r2,r8
umaal r9,r0,r3,r8
umaal r10,r0,r4,r8
umaal r6,r0,r5,r8
push {r0}
//frame address sp,48
//_ _ _ _ s 6 10 9| 7 | lr 12 11 _ _ _ _
ldr r2,[sp,#40]
adds r2,r2,#16
ldm r2,{r2,r3,r4,r5}
ldr r8,[r1],#4
mov r0,#0
umaal r11,r0,r2,r8
str r11,[sp,#16+4]
umaal r12,r0,r3,r8
umaal lr,r0,r4,r8
umaal r0,r7,r5,r8 // 7=carry for 9
//_ _ _ _ s 6 10 9+7| 0 | lr 12 _ _ _ _ _
ldr r8,[r1],#4
mov r11,#0
umaal r12,r11,r2,r8
str r12,[sp,#20+4]
umaal lr,r11,r3,r8
umaal r0,r11,r4,r8
umaal r11,r7,r5,r8 // 7=carry for 10
//_ _ _ _ s 6 10+7 9+11| 0 | lr _ _ _ _ _ _
ldr r8,[r1],#4
mov r12,#0
umaal lr,r12,r2,r8
str lr,[sp,#24+4]
umaal r0,r12,r3,r8
umaal r11,r12,r4,r8
umaal r10,r12,r5,r8 // 12=carry for 6
//_ _ _ _ s 6+12 10+7 9+11| 0 | _ _ _ _ _ _ _
ldr r8,[r1],#4
mov lr,#0
umaal r0,lr,r2,r8
str r0,[sp,#28+4]
umaal r11,lr,r3,r8
umaal r10,lr,r4,r8
umaal r6,lr,r5,r8 // lr=carry for saved
//_ _ _ _ s+lr 6+12 10+7 9+11| _ | _ _ _ _ _ _ _
ldm r1!,{r0,r8}
umaal r11,r9,r2,r0
str r11,[sp,#32+4]
umaal r9,r10,r3,r0
umaal r10,r6,r4,r0
pop {r11}
//frame address sp,44
umaal r11,r6,r5,r0 // 6=carry for next
//_ _ _ 6 11+lr 10+12 9+7 _ | _ | _ _ _ _ _ _ _
umaal r9,r7,r2,r8
umaal r10,r7,r3,r8
umaal r11,r7,r4,r8
umaal r6,r7,r5,r8
ldm r1!,{r0,r8}
umaal r10,r12,r2,r0
umaal r11,r12,r3,r0
umaal r6,r12,r4,r0
umaal r7,r12,r5,r0
umaal r11,lr,r2,r8
umaal lr,r6,r3,r8
umaal r6,r7,r4,r8
umaal r7,r12,r5,r8
// 12 7 6 lr 11 10 9 stack*9
push {r6,r7,r12}
//frame address sp,56
add r7,sp,#12
ldm r7,{r0-r8}
mov r12,#0
adds r3,r0
adcs r4,r4,r1
adcs r5,r5,r2
adcs r6,r6,r0
adcs r7,r7,r1
adcs r8,r8,r0
adcs r9,r9,r1
adcs r10,r10,#0
adcs r11,r11,#0
adcs r12,r12,#0
adds r6,r3
adcs r7,r7,r4 // r4 instead of 0
adcs r8,r8,r2
adcs r9,r9,r3
adcs r10,r10,r2
adcs r11,r11,r3
adcs r12,r12,#0
subs r7,r0
sbcs r8,r8,r1
sbcs r9,r9,r2
sbcs r10,r10,r3
sbcs r11,r11,#0
sbcs r12,r12,#0 // r12 is between 0 and 2
pop {r1-r3}
//frame address sp,44
adds r0,lr,r12
adcs r1,r1,#0
mov r12,#0
adcs r12,r12,#0
//adds r7,r4 (added above instead)
adcs r8,r8,r5
adcs r9,r9,r6
adcs r10,r10,r4
adcs r11,r11,r5
adcs r0,r0,r4
adcs r1,r1,r5
adcs r2,r2,r12
adcs r3,r3,#0
mov r12,#0
adcs r12,r12,#0
adcs r10,r10,r7
adcs r11,r11,#0
adcs r0,r0,r6
adcs r1,r1,r7
adcs r2,r2,r6
adcs r3,r3,r7
adcs r12,r12,#0
subs r11,r4
sbcs r0,r0,r5
sbcs r1,r1,r6
sbcs r2,r2,r7
sbcs r3,r3,#0
sbcs r12,r12,#0
// now (T + mN) / R is
// 8 9 10 11 0 1 2 3 12 (lsb -> msb)
subs r8,r8,#0xffffffff
sbcs r9,r9,#0xffffffff
sbcs r10,r10,#0xffffffff
sbcs r11,r11,#0
sbcs r4,r0,#0
sbcs r5,r1,#0
sbcs r6,r2,#1
sbcs r7,r3,#0xffffffff
sbc r12,r12,#0
adds r0,r8,r12
adcs r1,r9,r12
adcs r2,r10,r12
adcs r3,r11,#0
adcs r4,r4,#0
adcs r5,r5,#0
adcs r6,r6,r12, lsr #31
adcs r7,r7,r12
add sp,sp,#40
//frame address sp,4
pop {pc}
.size P256_mulmod, .-P256_mulmod
#if !use_mul_for_sqr
// 173 cycles
// If input is A*R mod p, computes A^2*R mod p
// in/out: r0-r7
// clobbers all other registers
.type P256_sqrmod, %function
P256_sqrmod:
push {lr}
//frame push {lr}
//mul 01, 00
umull r9,r10,r0,r0
umull r11,r12,r0,r1
adds r11,r11,r11
mov lr,#0
umaal r10,r11,lr,lr
//r10 r9 done
//r12 carry for 3rd before col
//r11+C carry for 3rd final col
push {r9,r10}
//frame address sp,12
//mul 02, 11