-
Notifications
You must be signed in to change notification settings - Fork 5
/
P256-cortex-m4-ecdh-sizeopt-gcc.s
1367 lines (1177 loc) · 22.7 KB
/
P256-cortex-m4-ecdh-sizeopt-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
// P-256 ECDH
// Author: Emil Lenngren
// Licensed under the BSD 2-clause license.
// Note on calling conventions: some of the local functions in this file use custom calling conventions.
// Exported symbols use the standard C calling conventions for ARM, which means that r4-r11 and sp are preserved and the other registers are clobbered.
// All integers are assumed to be in little endian
// Run time: 1108k cycles
.text
// 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)
P256_sqrmod :
mov r2,r1
// fallthrough
// If inputs are A*R mod p and B*R mod p, computes AB*R mod p
// input: *r0 = out, *r1 = in1, *r2 = in2
// output: *r8 = out
// clobbers all other registers
.type P256_mulmod, %function
P256_mulmod:
mov r3,r0
push {r2,r3,lr}
//frame push {lr}
//frame address sp,12
sub sp,#36
//frame address sp,48
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,56
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,60
//_ _ _ _ s 6 10 9| 7 | lr 12 11 _ _ _ _
ldr r2,[sp,#48]
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,56
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
strd r6,r7,[sp,#36]
str r12,[sp,#44]
pop {r0-r8}
//frame address sp,20
mov r12,#0
adds r3,r0
adcs r4,r1
adcs r5,r2
adcs r6,r0
adcs r7,r1
adcs r8,r0
adcs r9,r1
adcs r10,#0
adcs r11,#0
adcs r12,#0
adds r6,r3
adcs r7,r4 // r4 instead of 0
adcs r8,r2
adcs r9,r3
adcs r10,r2
adcs r11,r3
adcs r12,#0
subs r7,r0
sbcs r8,r1
sbcs r9,r2
sbcs r10,r3
sbcs r11,#0
sbcs r12,#0 // r12 is between 0 and 2
pop {r1-r3}
//frame address sp,8
adds r0,lr,r12
adcs r1,#0
mov r12,#0
adcs r12,#0
//adds r7,r4 (added above instead)
adcs r8,r5
adcs r9,r6
adcs r10,r4
adcs r11,r5
adcs r0,r4
adcs r1,r5
adcs r2,r12
adcs r3,#0
mov r12,#0
adcs r12,#0
adcs r10,r7
adcs r11,#0
adcs r0,r6
adcs r1,r7
adcs r2,r6
adcs r3,r7
adcs r12,#0
subs r11,r4
sbcs r0,r5
sbcs r1,r6
sbcs r2,r7
sbcs r3,#0
sbcs 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 {r8}
//frame address sp,4
stm r8,{r0-r7}
pop {pc}
.size P256_mulmod, .-P256_mulmod
// 52 cycles
// Computes A + B mod p, assumes A, B < p
// in: *r1, *r2
// out: r0-r7
// clobbers all other registers
.type P256_addmod, %function
P256_addmod:
push {r0}
ldm r2,{r2-r9}
ldm r1!,{r0,r10,r11,r12}
adds r2,r0
adcs r3,r10
adcs r4,r11
adcs r5,r12
ldm r1,{r0,r1,r11,r12}
adcs r6,r0
adcs r7,r1
adcs r8,r11
adcs r9,r12
movs r10,#0
adcs r10,r10
subs r2,#0xffffffff
sbcs r3,#0xffffffff
sbcs r4,#0xffffffff
sbcs r5,#0
sbcs r6,#0
sbcs r7,#0
sbcs r8,#1
sbcs r9,#0xffffffff
sbcs r10,#0
adds r0,r2,r10
adcs r1,r3,r10
adcs r2,r4,r10
adcs r3,r5,#0
adcs r4,r6,#0
adcs r5,r7,#0
adcs r6,r8,r10, lsr #31
adcs r7,r9,r10
pop {r8}
stm r8,{r0-r7}
bx lr
.size P256_addmod, .-P256_addmod
// 42 cycles
// Computes A - B mod p, assumes A, B < p
// in: *r1, *r2
// out: r0-r7
// clobbers all other registers
.type P256_submod, %function
P256_submod:
push {r0}
ldm r1,{r3-r10}
ldm r2!,{r0,r1,r11,r12}
subs r3,r0
sbcs r4,r1
sbcs r5,r11
sbcs r6,r12
ldm r2,{r0,r1,r11,r12}
sbcs r7,r0
sbcs r8,r1
sbcs r9,r11
sbcs r10,r12
sbcs r11,r11
adds r0,r3,r11
adcs r1,r4,r11
adcs r2,r5,r11
adcs r3,r6,#0
adcs r4,r7,#0
adcs r5,r8,#0
adcs r6,r9,r11, lsr #31
adcs r7,r10,r11
pop {r8}
stm r8,{r0-r7}
bx lr
.size P256_submod, .-P256_submod
// in: *r1
// out: *r0
.type P256_to_montgomery, %function
P256_to_montgomery:
push {r4-r11,lr}
//frame push {r4-r11,lr}
adr r2,R2_mod_p
bl P256_mulmod
pop {r4-r11,pc}
.size P256_to_montgomery, .-P256_to_montgomery
.align 2
// (2^256)^2 mod p
R2_mod_p:
.word 3
.word 0
.word 0xffffffff
.word 0xfffffffb
.word 0xfffffffe
.word 0xffffffff
.word 0xfffffffd
.word 4
// in: *r1
// out: *r0
.type P256_from_montgomery, %function
P256_from_montgomery:
push {r4-r11,lr}
//frame push {r4-r11,lr}
movs r2,#0
movs r3,#0
push {r2-r3}
//frame address sp,44
push {r2-r3}
//frame address sp,52
push {r2-r3}
//frame address sp,60
movs r2,#1
push {r2-r3}
//frame address sp,68
mov r2,sp
bl P256_mulmod
add sp,#32
//frame address sp,36
pop {r4-r11,pc}
.size P256_from_montgomery, .-P256_from_montgomery
// Elliptic curve operations on the NIST curve P256
// Checks if a point is on curve
// in: *r0 = x, *r1 = y, in Montgomery form
// out: r0 = 1 if on curve, .else 0
.type P256_point_is_on_curve, %function
P256_point_is_on_curve:
push {r0,r4-r11,lr}
//frame push {r4-r11,lr}
//frame address sp,40
// We verify y^2 - (x^3 - 3x) = b
// y^2
sub sp,#32
//frame address sp,72
mov r0,sp
bl P256_sqrmod
// x^2
ldr r1,[sp,#32]
sub sp,#32
//frame address sp,104
mov r0,sp
bl P256_sqrmod
// x^3
mov r0,sp
ldr r1,[sp,#64]
mov r2,sp
bl P256_mulmod
// x^3 - 3x
movs r0,#3
0:
push {r0}
//frame address sp,108
add r0,sp,#4
add r1,sp,#4
ldr r2,[sp,#68]
bl P256_submod
pop {r0}
//frame address sp,104
subs r0,#1
bne 0b
// y^2 - (x^3 - 3x)
mov r0,sp
add r1,sp,#32
mov r2,sp
bl P256_submod
// compare with b
mov r0,sp
adr r1,P256_b_mont
bl P256_less_than
subs r0,#1
beq 1f
adr r0,P256_b_mont
mov r1,sp
bl P256_less_than
eor r0,#1
1:
add sp,#68
//frame address sp,36
pop {r4-r11,pc}
.size P256_point_is_on_curve, .-P256_point_is_on_curve
.align 2
P256_b_mont:
.word 0x29c4bddf
.word 0xd89cdf62
.word 0x78843090
.word 0xacf005cd
.word 0xf7212ed6
.word 0xe5a220ab
.word 0x04874834
.word 0xdc30061d
// Selects one of many values
// *r0 = output, *r1 = table, r2 = index to choose [0..7]
.type P256_select, %function
P256_select:
mov r3,r2
movs r2,#3
push {r0,r2,r3,r4-r11,lr}
//frame push {r4-r11,lr}
//frame address sp,48
0:
rsbs r3,#0
sbcs r3,r3
mvns r3,r3
ldm r1!,{r4-r11}
ands r4,r3
ands r5,r3
ands r6,r3
ands r7,r3
and r8,r3
and r9,r3
and r10,r3
and r11,r3
adds r1,#64
movs r3,#1
1:
ldr r0,[sp,#8]
eors r0,r3
mrs r0,apsr
lsrs r0,#30
ldm r1!,{r2,r12,lr}
umlal r4,r3,r0,r2
umlal r5,r2,r0,r12
umlal r6,r3,r0,lr
ldm r1!,{r2,r12,lr}
umlal r7,r3,r0,r2
umlal r8,r2,r0,r12
umlal r9,r3,r0,lr
ldm r1!,{r12,lr}
umlal r10,r2,r0,r12
umlal r11,r3,r0,lr
adds r1,#64
adds r3,#1
cmp r3,#8
bne 1b
ldm sp,{r0,r12}
stm r0!,{r4-r11}
str r0,[sp] // TODO: store r0,r12 together by push
subs r1,#736
subs r12,#1
str r12,[sp,#4]
ldr r3,[sp,#8]
bne 0b
add sp,#12
//frame address sp,36
pop {r4-r11,pc}
.size P256_select, .-P256_select
// Doubles the point in Jacobian form (integers are in Montgomery form)
// *r0 = out, *r1 = in
.type P256_double_j, %function
P256_double_j:
push {r0,r1,r4-r11,lr}
//frame push {r4-r11,lr}
//frame address sp,44
// https://eprint.iacr.org/2014/130.pdf, algorithm 10
// t1 = Z1^2
sub sp,#32
//frame address sp,76
mov r0,sp
adds r1,#64
bl P256_sqrmod
// Z2 = Y1 * Z1
ldrd r0,r1,[sp,#32]
adds r0,#64
adds r1,#32
add r2,r1,#32
bl P256_mulmod
// t2 = X1 + t1
ldr r1,[sp,#36]
mov r2,sp
sub sp,#32
//frame address sp,108
mov r0,sp
bl P256_addmod
// t1 = X1 - t1
ldr r1,[sp,#68]
add r2,sp,#32
mov r0,r2
bl P256_submod
// t1 = t1 * t2
add r1,sp,#32
mov r2,sp
mov r0,r1
bl P256_mulmod
// t2 = t1 / 2
ldm r8,{r0-r7}
lsl r8,r0,#31
adds r0,r0,r8, asr #31
adcs r1,r1,r8, asr #31
adcs r2,r2,r8, asr #31
adcs r3,#0
adcs r4,#0
adcs r5,#0
adcs r6,r6,r8, lsr #31
adcs r7,r7,r8, asr #31
rrxs r7,r7
rrxs r6,r6
rrxs r5,r5
rrxs r4,r4
rrxs r3,r3
rrxs r2,r2
rrxs r1,r1
rrx r0,r0
stm sp,{r0-r7}
// t1 = t1 + t2
add r1,sp,#32
mov r2,sp
mov r0,r1
bl P256_addmod
// t2 = t1^2
mov r0,sp
add r1,sp,#32
bl P256_sqrmod
// Y2 = Y1^2
ldrd r0,r1,[sp,#64]
adds r0,#32
adds r1,#32
bl P256_sqrmod
// t3 = Y2^2
ldr r1,[sp,#64]
adds r1,#32
sub sp,#32
//frame address sp,140
mov r0,sp
bl P256_sqrmod
// Y2 = X1 * Y2
ldrd r0,r1,[sp,#96]
adds r0,#32
mov r2,r0
bl P256_mulmod
mov r1,r8
// X2 = 2 * Y2
mov r2,r8
sub r0,r2,#32
bl P256_addmod
// X2 = t2 - X2
add r1,sp,#32
mov r2,r8
mov r0,r8
bl P256_submod
// t2 = Y2 - X2
mov r2,r8
add r1,r2,#32
add r0,sp,#32
bl P256_submod
// t1 = t1 * t2
add r0,sp,#64
add r1,sp,#64
add r2,sp,#32
bl P256_mulmod
// Y2 = t1 - t3
ldr r0,[sp,#96]
adds r0,#32
add r1,sp,#64
mov r2,sp
bl P256_submod
add sp,#104
//frame address sp,36
pop {r4-r11,pc}
.size P256_double_j, .-P256_double_j
// Adds or subtracts points in Jacobian form (integers are in Montgomery form)
// The first operand is located in *r0, the second in *r1 (may not overlap)
// The result is stored at *r0
//
// Requirements:
// - no operand is the point at infinity
// - both operand must be different
// - one operand must not be the negation of the other
// If requirements are not met, the returned Z point will be 0
.type P256_add_j, %function
P256_add_j:
push {r0,r1,r4-r11,lr}
//frame push {r4-r11,lr}
//frame address sp,44
// Here a variant of
// https://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-3/addition/add-1998-cmo-2.op3
// is used, but rearranged and uses less temporaries.
// The first operand to the function is both (X3,Y3,Z3) and (X2,Y2,Z2).
// The second operand to the function is (X1,Y1,Z1)
// Z1Z1 = Z1^2
sub sp,#32
//frame address sp,76
mov r0,sp
adds r1,#64
bl P256_sqrmod
// U2 = X2*Z1Z1
ldr r1,[sp,#32]
mov r2,sp
mov r0,r1
bl P256_mulmod
// t1 = Z1*Z1Z1
ldr r1,[sp,#36]
adds r1,#64
mov r2,sp
mov r0,sp
bl P256_mulmod
// S2 = Y2*t1
ldr r1,[sp,#32]
adds r1,#32
mov r2,sp
mov r0,r1
bl P256_mulmod
// Z2Z2 = Z2^2
sub sp,#32
//frame address sp,108
mov r0,sp
add r1,r8,#32
bl P256_sqrmod
// U1 = X1*Z2Z2
ldr r1,[sp,#68]
mov r2,sp
add r0,sp,#32
bl P256_mulmod
// t2 = Z2*Z2Z2
ldr r1,[sp,#64]
adds r1,#64
mov r2,sp
mov r0,sp
bl P256_mulmod
// S1 = Y1*t2
ldr r1,[sp,#68]
adds r1,#32
mov r2,sp
mov r0,sp
bl P256_mulmod
// H = U2-U1
ldr r1,[sp,#64]
add r2,sp,#32
mov r0,r1
bl P256_submod
// HH = H^2
mov r1,r8
sub sp,#32
//frame address sp,140
mov r0,sp
bl P256_sqrmod
// Z3 = Z2*H
ldr r2,[sp,#96]
add r1,r2,#64
mov r0,r1
bl P256_mulmod
// Z3 = Z1*Z3
ldr r1,[sp,#100]
adds r1,#64
mov r2,r8
mov r0,r8
bl P256_mulmod
// HHH = H*HH
sub r1,r8,#64
mov r2,sp
mov r0,r1
bl P256_mulmod
// r = S2-S1
add r1,r8,#32
add r2,sp,#32
mov r0,r1
bl P256_submod
// V = U1*HH
add r1,sp,#64
mov r2,sp
mov r0,r1
bl P256_mulmod
// t3 = r^2
ldr r1,[sp,#96]
adds r1,#32
mov r0,sp
bl P256_sqrmod
// t2 = S1*HHH
add r1,sp,#32
ldr r2,[sp,#96]
add r0,sp,#32
bl P256_mulmod
// X3 = t3-HHH
mov r1,sp
ldr r2,[sp,#96]
mov r0,r2
bl P256_submod
// t3 = 2*V
add r1,sp,#64
add r2,sp,#64
mov r0,sp
bl P256_addmod
// X3 = X3-t3
ldr r1,[sp,#96]
mov r2,sp
mov r0,r1
bl P256_submod
// t3 = V-X3
add r1,sp,#64
mov r2,r8
mov r0,sp
bl P256_submod
// t3 = r*t3
ldr r1,[sp,#96]
adds r1,#32
mov r2,sp
mov r0,sp
bl P256_mulmod
// Y3 = t3-t2
mov r1,sp
add r2,sp,#32
ldr r0,[sp,#96]
adds r0,#32
bl P256_submod
add sp,#104
//frame address sp,36
pop {r4-r11,pc}
.size P256_add_j, .-P256_add_j
// in/out: r0-r7
.type P256_modinv, %function
P256_modinv:
push {r0-r7,lr}
//frame push {r4-r7,lr}
//frame address sp,36
sub sp,#36
//frame address sp,72
mov r0,sp
bl P256_load_1
mov r1,r0
bl P256_to_montgomery
adr r0,P256_p
ldm r0,{r0-r7}
subs r0,#2
push {r0-r7}
//frame address sp,104
movs r0,#255
0:
str r0,[sp,#64]
add r0,sp,#32
add r1,sp,#32
bl P256_sqrmod
ldr r0,[sp,#64]
lsrs r1,r0,#3
ldrb r1,[sp,r1]
and r2,r0,#7
lsrs r1,r2
tst r1,#1
beq 1f
add r0,sp,#32
add r1,sp,#32
add r2,sp,#68
bl P256_mulmod
1:
ldr r0,[sp,#64]
subs r0,#1
bpl 0b
add sp,#32
//frame address sp,72
pop {r0-r7}
//frame address sp,40
add sp,#36
//frame address sp,4
pop {pc}
.size P256_modinv, .-P256_modinv
// *r0 = output affine montgomery, *r1 = input jacobian montgomery
.type P256_jacobian_to_affine, %function
P256_jacobian_to_affine:
push {r0,r4-r11,lr}
//frame push {r4-r11,lr}
//frame address sp,40
adds r0,#64
ldm r0,{r0-r7}
bl P256_modinv
push {r0-r7}
//frame address sp,72
mov r1,sp
sub sp,#32
//frame address sp,104
mov r0,sp
bl P256_sqrmod
add r1,sp,#32
mov r2,sp
mov r0,r1
bl P256_mulmod
mov r1,sp
ldr r0,[sp,#64]
mov r2,r0
bl P256_mulmod
add r1,sp,#32
ldr r0,[sp,#64]
adds r0,#32
mov r2,r0
bl P256_mulmod
add sp,#68
//frame address sp,36
pop {r4-r11,pc}
.size P256_jacobian_to_affine, .-P256_jacobian_to_affine
// performs r0 := abs(r0)
.type P256_abs_int, %function
P256_abs_int:
rsbs r2,r0,#0
and r3,r2,r0, asr #31
and r0,r0,r2, asr #31
orrs r0,r0,r3
bx lr
.size P256_abs_int, .-P256_abs_int
// input: *r0 = output (8 words)
// output: r0 is preserved
.type P256_load_1, %function
P256_load_1:
movs r1,#1
stm r0!,{r1}
movs r1,#0
umull r2,r3,r1,r1
stm r0!,{r1-r3}
stm r0!,{r1-r3}
stm r0!,{r1}
subs r0,#32
bx lr
.size P256_load_1, .-P256_load_1
// input: *r0 = value, *r1 = limit
// output: 1 if value < limit, .else 0
.type P256_less_than, %function
P256_less_than:
push {r4-r5,lr}
//frame push {r4-r5,lr}
subs r5,r5 // set r5 to 0 and C to 1
movs r2,#8
0:
ldm r0!,{r3}
ldm r1!,{r4}
sbcs r3,r4
sub r2,#1
cbz r2,1f
b 0b
1:
adcs r5,r5
eor r0,r5,#1