Skip to content

Commit f075940

Browse files
Regenerate curve25519 asm: sc_reduce and sc_muladd range fixes
- fe_x25519_asm.S/.asm, armv8-curve25519.S/.asm/_c.c, armv8-32-curve25519.S/_c.c and thumb2-curve25519.S/_c.c are regenerated from wolfSSL/scripts.
1 parent 15096bf commit f075940

9 files changed

Lines changed: 1216 additions & 98 deletions

‎wolfcrypt/src/fe_x25519_asm.S‎

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11952,6 +11952,19 @@ _sc_reduce_x64:
1195211952
adcq %rdx, %r13
1195311953
adcq %rsi, %r14
1195411954
adcq $0x00, %r15
11955+
# Add order if negative
11956+
movq %r15, %rcx
11957+
sarq $63, %rcx
11958+
movq $0x5812631a5cf5d3ed, %rax
11959+
movq $0x14def9dea2f79cd6, %rdx
11960+
movq $0x1000000000000000, %rsi
11961+
andq %rcx, %rax
11962+
andq %rcx, %rdx
11963+
andq %rcx, %rsi
11964+
addq %rax, %r12
11965+
adcq %rdx, %r13
11966+
adcq $0x00, %r14
11967+
adcq %rsi, %r15
1195511968
# Sub product of top 4 words and order
1195611969
movq $0xa7ed9ce5a30a2c13, %rcx
1195711970
movq %r12, %rax
@@ -12068,7 +12081,7 @@ _sc_reduce_x64:
1206812081
andq %rsi, %rdx
1206912082
andq %rsi, %rbx
1207012083
addq %rax, %r8
12071-
movq $0xfffffffffffffff, %rax
12084+
movq $0x1fffffffffffffff, %rax
1207212085
adcq %rdx, %r9
1207312086
adcq $0x00, %r10
1207412087
adcq %rbx, %r11
@@ -12240,6 +12253,19 @@ _sc_muladd_x64:
1224012253
adcq %rdx, %r13
1224112254
adcq %rcx, %r14
1224212255
adcq $0x00, %r15
12256+
# Add order if negative
12257+
movq %r15, %rbx
12258+
sarq $63, %rbx
12259+
movq $0x5812631a5cf5d3ed, %rax
12260+
movq $0x14def9dea2f79cd6, %rdx
12261+
movq $0x1000000000000000, %rcx
12262+
andq %rbx, %rax
12263+
andq %rbx, %rdx
12264+
andq %rbx, %rcx
12265+
addq %rax, %r12
12266+
adcq %rdx, %r13
12267+
adcq $0x00, %r14
12268+
adcq %rcx, %r15
1224312269
# Sub product of top 4 words and order
1224412270
movq $0xa7ed9ce5a30a2c13, %rbx
1224512271
movq %r12, %rax
@@ -12356,7 +12382,7 @@ _sc_muladd_x64:
1235612382
andq %rcx, %rdx
1235712383
andq %rcx, %rsi
1235812384
addq %rax, %r8
12359-
movq $0xfffffffffffffff, %rax
12385+
movq $0x1fffffffffffffff, %rax
1236012386
adcq %rdx, %r9
1236112387
adcq $0x00, %r10
1236212388
adcq %rsi, %r11
@@ -21401,6 +21427,19 @@ _sc_reduce_avx2:
2140121427
adcq %rbx, %r13
2140221428
adcq %rcx, %r14
2140321429
adcq $0x00, %r15
21430+
# Add order if negative
21431+
movq %r15, %rax
21432+
sarq $63, %rax
21433+
movq $0x5812631a5cf5d3ed, %rsi
21434+
movq $0x14def9dea2f79cd6, %rbx
21435+
movq $0x1000000000000000, %rcx
21436+
andq %rax, %rsi
21437+
andq %rax, %rbx
21438+
andq %rax, %rcx
21439+
addq %rsi, %r12
21440+
adcq %rbx, %r13
21441+
adcq $0x00, %r14
21442+
adcq %rcx, %r15
2140421443
# Sub product of top 4 words and order
2140521444
movq $0xa7ed9ce5a30a2c13, %rdx
2140621445
mulxq %r12, %rcx, %rax
@@ -21497,7 +21536,7 @@ _sc_reduce_avx2:
2149721536
andq %rcx, %rbx
2149821537
andq %rcx, %rbp
2149921538
addq %rsi, %r8
21500-
movq $0xfffffffffffffff, %rsi
21539+
movq $0x1fffffffffffffff, %rsi
2150121540
adcq %rbx, %r9
2150221541
adcq $0x00, %r10
2150321542
adcq %rbp, %r11
@@ -21647,6 +21686,19 @@ _sc_muladd_avx2:
2164721686
adcq %r8, %r15
2164821687
adcq %rcx, %rbx
2164921688
adcq $0x00, %rbp
21689+
# Add order if negative
21690+
movq %rbp, %rax
21691+
sarq $63, %rax
21692+
movq $0x5812631a5cf5d3ed, %rsi
21693+
movq $0x14def9dea2f79cd6, %r8
21694+
movq $0x1000000000000000, %rcx
21695+
andq %rax, %rsi
21696+
andq %rax, %r8
21697+
andq %rax, %rcx
21698+
addq %rsi, %r14
21699+
adcq %r8, %r15
21700+
adcq $0x00, %rbx
21701+
adcq %rcx, %rbp
2165021702
# Sub product of top 4 words and order
2165121703
movq $0xa7ed9ce5a30a2c13, %rdx
2165221704
mulxq %r14, %rcx, %rax
@@ -21743,7 +21795,7 @@ _sc_muladd_avx2:
2174321795
andq %rcx, %r8
2174421796
andq %rcx, %r9
2174521797
addq %rsi, %r10
21746-
movq $0xfffffffffffffff, %rsi
21798+
movq $0x1fffffffffffffff, %rsi
2174721799
adcq %r8, %r11
2174821800
adcq $0x00, %r12
2174921801
adcq %r9, %r13

‎wolfcrypt/src/fe_x25519_asm.asm‎

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10644,6 +10644,19 @@ sc_reduce_x64 PROC
1064410644
adc r14, rdx
1064510645
adc r15, rsi
1064610646
adc rdi, 0
10647+
; Add order if negative
10648+
mov r8, rdi
10649+
sar r8, 63
10650+
mov rax, 6346243789798364141
10651+
mov rdx, 1503914060200516822
10652+
mov rsi, 1152921504606846976
10653+
and rax, r8
10654+
and rdx, r8
10655+
and rsi, r8
10656+
add r13, rax
10657+
adc r14, rdx
10658+
adc r15, 0
10659+
adc rdi, rsi
1064710660
; Sub product of top 4 words and order
1064810661
mov r8, 12100500283911187475
1064910662
mov rax, r13
@@ -10760,7 +10773,7 @@ sc_reduce_x64 PROC
1076010773
and rdx, rsi
1076110774
and rbx, rsi
1076210775
add r9, rax
10763-
mov rax, 1152921504606846975
10776+
mov rax, 2305843009213693951
1076410777
adc r10, rdx
1076510778
adc r11, 0
1076610779
adc r12, rbx
@@ -10924,6 +10937,19 @@ sc_muladd_x64 PROC
1092410937
adc r15, rdx
1092510938
adc rdi, r9
1092610939
adc rsi, 0
10940+
; Add order if negative
10941+
mov rbx, rsi
10942+
sar rbx, 63
10943+
mov rax, 6346243789798364141
10944+
mov rdx, 1503914060200516822
10945+
mov r9, 1152921504606846976
10946+
and rax, rbx
10947+
and rdx, rbx
10948+
and r9, rbx
10949+
add r14, rax
10950+
adc r15, rdx
10951+
adc rdi, 0
10952+
adc rsi, r9
1092710953
; Sub product of top 4 words and order
1092810954
mov rbx, 12100500283911187475
1092910955
mov rax, r14
@@ -11040,7 +11066,7 @@ sc_muladd_x64 PROC
1104011066
and rdx, r9
1104111067
and r8, r9
1104211068
add r10, rax
11043-
mov rax, 1152921504606846975
11069+
mov rax, 2305843009213693951
1104411070
adc r11, rdx
1104511071
adc r12, 0
1104611072
adc r13, r8
@@ -19677,6 +19703,19 @@ sc_reduce_avx2 PROC
1967719703
adc r14, rbx
1967819704
adc r15, rcx
1967919705
adc rdi, 0
19706+
; Add order if negative
19707+
mov rax, rdi
19708+
sar rax, 63
19709+
mov rsi, 6346243789798364141
19710+
mov rbx, 1503914060200516822
19711+
mov rcx, 1152921504606846976
19712+
and rsi, rax
19713+
and rbx, rax
19714+
and rcx, rax
19715+
add r13, rsi
19716+
adc r14, rbx
19717+
adc r15, 0
19718+
adc rdi, rcx
1968019719
; Sub product of top 4 words and order
1968119720
mov rdx, 12100500283911187475
1968219721
mulx rax, rcx, r13
@@ -19773,7 +19812,7 @@ sc_reduce_avx2 PROC
1977319812
and rbx, rcx
1977419813
and rbp, rcx
1977519814
add r9, rsi
19776-
mov rsi, 1152921504606846975
19815+
mov rsi, 2305843009213693951
1977719816
adc r10, rbx
1977819817
adc r11, 0
1977919818
adc r12, rbp
@@ -19916,6 +19955,19 @@ sc_muladd_avx2 PROC
1991619955
adc rsi, r10
1991719956
adc rbx, rcx
1991819957
adc rbp, 0
19958+
; Add order if negative
19959+
mov rax, rbp
19960+
sar rax, 63
19961+
mov r9, 6346243789798364141
19962+
mov r10, 1503914060200516822
19963+
mov rcx, 1152921504606846976
19964+
and r9, rax
19965+
and r10, rax
19966+
and rcx, rax
19967+
add rdi, r9
19968+
adc rsi, r10
19969+
adc rbx, 0
19970+
adc rbp, rcx
1991919971
; Sub product of top 4 words and order
1992019972
mov rdx, 12100500283911187475
1992119973
mulx rax, rcx, rdi
@@ -20012,7 +20064,7 @@ sc_muladd_avx2 PROC
2001220064
and r10, rcx
2001320065
and r11, rcx
2001420066
add r12, r9
20015-
mov r9, 1152921504606846975
20067+
mov r9, 2305843009213693951
2001620068
adc r13, r10
2001720069
adc r14, 0
2001820070
adc r15, r11

0 commit comments

Comments
 (0)