Debian Codegraph @

others/mbedtls/tf-psa-crypto/drivers/pqcp/mldsa-native/dev/aarch64_opt/src/pointwise_montgomery.S

raw
1 /* Copyright (c) The mldsa-native project authors
2 * SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3 */
4
5 #include "../../../common.h"
6 #if defined(MLD_ARITH_BACKEND_AARCH64) && !defined(MLD_CONFIG_MULTILEVEL_NO_SHARED)
7 /* simpasm: header-end */
8
9 .macro montgomery_reduce_long res, inl, inh
10 uzp1 \res\().4s, \inl\().4s, \inh\().4s
11 mul \res\().4s, \res\().4s, modulus_twisted.4s
12 smlsl \inl\().2d, \res\().2s, modulus.2s
13 smlsl2 \inh\().2d, \res\().4s, modulus.4s
14 uzp2 \res\().4s, \inl\().4s, \inh\().4s
15 .endm
16
17 .macro pmull dl, dh, a, b
18 smull \dl\().2d, \a\().2s, \b\().2s
19 smull2 \dh\().2d, \a\().4s, \b\().4s
20 .endm
21
22 out_ptr .req x0
23 a_ptr .req x1
24 b_ptr .req x2
25
26 count .req x3
27 wtmp .req w3
28
29 modulus .req v0
30 modulus_twisted .req v1
31
32 a_0 .req v16
33 a_1 .req v17
34 a_2 .req v18
35 a_3 .req v19
36
37 b_0 .req v20
38 b_1 .req v21
39 b_2 .req v22
40 b_3 .req v23
41
42 c_0_lo .req v24
43 c_0_hi .req v25
44 c_1_lo .req v26
45 c_1_hi .req v27
46 c_2_lo .req v28
47 c_2_hi .req v29
48 c_3_lo .req v30
49 c_3_hi .req v31
50
51 c_0 .req v16
52 c_1 .req v17
53 c_2 .req v18
54 c_3 .req v19
55
56 q_a_0 .req q16
57 q_a_1 .req q17
58 q_a_2 .req q18
59 q_a_3 .req q19
60
61 q_b_0 .req q20
62 q_b_1 .req q21
63 q_b_2 .req q22
64 q_b_3 .req q23
65
66 q_c_0 .req q16
67 q_c_1 .req q17
68 q_c_2 .req q18
69 q_c_3 .req q19
70
71 .text
72 .global MLD_ASM_NAMESPACE(poly_pointwise_montgomery_asm)
73 .balign 4
74 MLD_ASM_FN_SYMBOL(poly_pointwise_montgomery_asm)
75 // Load MLDSA_Q = 8380417
76 movz wtmp, #0xe001
77 movk wtmp, #0x7f, lsl #16
78 dup modulus.4s, wtmp
79
80 /* check-magic: 58728449 == unsigned_mod(pow(MLDSA_Q, -1, 2**32), 2**32) */
81 // Load MLDSA_Q^-1 = 58728449
82 movz wtmp, #0x2001
83 movk wtmp, #0x380, lsl #16
84 dup modulus_twisted.4s, wtmp
85
86 mov count, #(MLDSA_N / 4)
87
88 poly_pointwise_montgomery_loop_start:
89 ldr q_a_1, [a_ptr, #1*16]
90 ldr q_a_2, [a_ptr, #2*16]
91 ldr q_a_3, [a_ptr, #3*16]
92 ldr q_a_0, [a_ptr], #4*16
93
94 ldr q_b_1, [b_ptr, #1*16]
95 ldr q_b_2, [b_ptr, #2*16]
96 ldr q_b_3, [b_ptr, #3*16]
97 ldr q_b_0, [b_ptr], #4*16
98 // Bounds: |a_{i}|, |b_{i}| < 9q
99
100 pmull c_0_lo, c_0_hi, a_0, b_0
101 pmull c_1_lo, c_1_hi, a_1, b_1
102 pmull c_2_lo, c_2_hi, a_2, b_2
103 pmull c_3_lo, c_3_hi, a_3, b_3
104 // Bounds: |c_{i}_lo|, |c_{i}_hi| < 81q^2 < qR/2
105
106 montgomery_reduce_long c_0, c_0_lo, c_0_hi
107 montgomery_reduce_long c_1, c_1_lo, c_1_hi
108 montgomery_reduce_long c_2, c_2_lo, c_2_hi
109 montgomery_reduce_long c_3, c_3_lo, c_3_hi
110 // All coefficients are Montgomery-reduced, resulting in
111 //
112 // Bounds: |c_{i}| < q
113 //
114 // See description of mld_montgomery_reduce() in mldsa/src/reduce.h.
115
116 str q_c_1, [out_ptr, #1*16]
117 str q_c_2, [out_ptr, #2*16]
118 str q_c_3, [out_ptr, #3*16]
119 str q_c_0, [out_ptr], #4*16
120
121 subs count, count, #4
122 cbnz count, poly_pointwise_montgomery_loop_start
123
124 ret
125
126 .unreq out_ptr
127 .unreq a_ptr
128 .unreq b_ptr
129 .unreq count
130 .unreq wtmp
131 .unreq modulus
132 .unreq modulus_twisted
133 .unreq a_0
134 .unreq a_1
135 .unreq a_2
136 .unreq a_3
137 .unreq b_0
138 .unreq b_1
139 .unreq b_2
140 .unreq b_3
141 .unreq c_0_lo
142 .unreq c_0_hi
143 .unreq c_1_lo
144 .unreq c_1_hi
145 .unreq c_2_lo
146 .unreq c_2_hi
147 .unreq c_3_lo
148 .unreq c_3_hi
149 .unreq c_0
150 .unreq c_1
151 .unreq c_2
152 .unreq c_3
153 .unreq q_a_0
154 .unreq q_a_1
155 .unreq q_a_2
156 .unreq q_a_3
157 .unreq q_b_0
158 .unreq q_b_1
159 .unreq q_b_2
160 .unreq q_b_3
161 .unreq q_c_0
162 .unreq q_c_1
163 .unreq q_c_2
164 .unreq q_c_3
165
166 /* simpasm: footer-start */
167 #endif /* MLD_ARITH_BACKEND_AARCH64 && !MLD_CONFIG_MULTILEVEL_NO_SHARED */