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
// generated source. do not edit.
#![allow(non_upper_case_globals, unused_macros, unused_imports)]
use crate::low::macros::*;
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT-0
// ----------------------------------------------------------------------------
// Multiply-add modulo the order of the curve25519/edwards25519 basepoint
// Inputs x[4], y[4], c[4]; output z[4]
//
// extern void bignum_madd_n25519(uint64_t z[static 4], const uint64_t x[static 4],
// const uint64_t y[static 4],
// const uint64_t c[static 4]);
//
// Performs z := (x * y + c) mod n_25519, where the modulus is
// n_25519 = 2^252 + 27742317777372353535851937790883648493, the
// order of the curve25519/edwards25519 basepoint. The result z
// and the inputs x, y and c are all 4 digits (256 bits).
//
// Standard x86-64 ABI: RDI = z, RSI = x, RDX = y, RCX = c
// Microsoft x64 ABI: RCX = z, RDX = x, R8 = y, R9 = c
// ----------------------------------------------------------------------------
// Single round of modular reduction mod_n25519, mapping
// [m4;m3;m2;m1;m0] = m to [m3;m2;m1;m0] = m mod n_25519,
// *assuming* the input m < 2^64 * n_25519. This is very
// close to the loop body of the bignum_mod_n25519 function.
macro_rules! reduce {
($m4:expr, $m3:expr, $m2:expr, $m1:expr, $m0:expr) => { Q!(
"mov rbx, " $m4 ";\n"
"shld rbx, " $m3 ", 0x4;\n"
"shr " $m4 ", 0x3c;\n"
"sub rbx, " $m4 ";\n"
"shl " $m3 ", 0x4;\n"
"shrd " $m3 ", " $m4 ", 0x4;\n"
"movabs rax, 0x5812631a5cf5d3ed;\n"
"mul rbx;\n"
"mov rbp, rax;\n"
"mov rcx, rdx;\n"
"movabs rax, 0x14def9dea2f79cd6;\n"
"mul rbx;\n"
"add rcx, rax;\n"
"adc rdx, 0x0;\n"
"sub " $m0 ", rbp;\n"
"sbb " $m1 ", rcx;\n"
"sbb " $m2 ", rdx;\n"
"sbb " $m3 ", 0x0;\n"
"sbb rbx, rbx;\n"
"movabs rax, 0x5812631a5cf5d3ed;\n"
"and rax, rbx;\n"
"movabs rdx, 0x14def9dea2f79cd6;\n"
"and rdx, rbx;\n"
"movabs rbx, 0x1000000000000000;\n"
"and rbx, rax;\n"
"add " $m0 ", rax;\n"
"adc " $m1 ", rdx;\n"
"adc " $m2 ", 0x0;\n"
"adc " $m3 ", rbx"
)}
}
// Special case of "reduce" with m4 = 0. As well as not using m4,
// the quotient selection is slightly simpler, just floor(m/2^252)
// versus min (floor(m/2^252)) (2^63-1).
macro_rules! reduce0 {
($m3:expr, $m2:expr, $m1:expr, $m0:expr) => { Q!(
"mov rbx, " $m3 ";\n"
"shr rbx, 60;\n"
"shl " $m3 ", 4;\n"
"shr " $m3 ", 4;\n"
"movabs rax, 0x5812631a5cf5d3ed;\n"
"mul rbx;\n"
"mov rbp, rax;\n"
"mov rcx, rdx;\n"
"movabs rax, 0x14def9dea2f79cd6;\n"
"mul rbx;\n"
"add rcx, rax;\n"
"adc rdx, 0x0;\n"
"sub " $m0 ", rbp;\n"
"sbb " $m1 ", rcx;\n"
"sbb " $m2 ", rdx;\n"
"sbb " $m3 ", 0x0;\n"
"sbb rbx, rbx;\n"
"movabs rax, 0x5812631a5cf5d3ed;\n"
"and rax, rbx;\n"
"movabs rdx, 0x14def9dea2f79cd6;\n"
"and rdx, rbx;\n"
"movabs rbx, 0x1000000000000000;\n"
"and rbx, rax;\n"
"add " $m0 ", rax;\n"
"adc " $m1 ", rdx;\n"
"adc " $m2 ", 0x0;\n"
"adc " $m3 ", rbx"
)}
}
/// Multiply-add modulo the order of the curve25519/edwards25519 basepoint
///
/// Inputs x[4], y[4], c[4]; output z[4]
///
/// Performs z := (x * y + c) mod n_25519, where the modulus is
/// n_25519 = 2^252 + 27742317777372353535851937790883648493, the
/// order of the curve25519/edwards25519 basepoint. The result z
/// and the inputs x, y and c are all 4 digits (256 bits).
pub(crate) fn bignum_madd_n25519(z: &mut [u64; 4], x: &[u64; 4], y: &[u64; 4], c: &[u64; 4]) {
// SAFETY: inline assembly. see [crate::low::inline_assembly_safety] for safety info.
unsafe {
core::arch::asm!(
Q!(" endbr64 " ),
// Save some additional registers for use
Q!(" push " "rbx"),
Q!(" push " "rbp"),
Q!(" push " "r12"),
Q!(" push " "r13"),
Q!(" push " "r14"),
Q!(" push " "r15"),
// First compute [r15;r14;r13;r12;r11;r10;r9;r8] = x * y + c. This is
// a multiply-add variant of an ADCX/ADOX-based schoolbook multiplier,
// starting the accumulation with the c term and doing the zeroth row
// in the same uniform fashion, otherwise similar to the start of
// bignum_mul_p256k1.
Q!(" mov " "r8, [rcx]"),
Q!(" mov " "r9, [rcx + 8]"),
Q!(" mov " "r10, [rcx + 16]"),
Q!(" mov " "r11, [rcx + 24]"),
Q!(" mov " "rcx, rdx"),
Q!(" xor " "ebp, ebp"),
Q!(" mov " "rdx, [rcx]"),
Q!(" mulx " "rbx, rax, [rsi]"),
Q!(" adcx " "r8, rax"),
Q!(" adox " "r9, rbx"),
Q!(" mulx " "rbx, rax, [rsi + 0x8]"),
Q!(" adcx " "r9, rax"),
Q!(" adox " "r10, rbx"),
Q!(" mulx " "rbx, rax, [rsi + 0x10]"),
Q!(" adcx " "r10, rax"),
Q!(" adox " "r11, rbx"),
Q!(" mulx " "r12, rax, [rsi + 0x18]"),
Q!(" adcx " "r11, rax"),
Q!(" adox " "r12, rbp"),
Q!(" adcx " "r12, rbp"),
Q!(" xor " "ebp, ebp"),
Q!(" mov " "rdx, [rcx + 0x8]"),
Q!(" mulx " "rbx, rax, [rsi]"),
Q!(" adcx " "r9, rax"),
Q!(" adox " "r10, rbx"),
Q!(" mulx " "rbx, rax, [rsi + 0x8]"),
Q!(" adcx " "r10, rax"),
Q!(" adox " "r11, rbx"),
Q!(" mulx " "rbx, rax, [rsi + 0x10]"),
Q!(" adcx " "r11, rax"),
Q!(" adox " "r12, rbx"),
Q!(" mulx " "r13, rax, [rsi + 0x18]"),
Q!(" adcx " "r12, rax"),
Q!(" adox " "r13, rbp"),
Q!(" adcx " "r13, rbp"),
Q!(" xor " "ebp, ebp"),
Q!(" mov " "rdx, [rcx + 0x10]"),
Q!(" mulx " "rbx, rax, [rsi]"),
Q!(" adcx " "r10, rax"),
Q!(" adox " "r11, rbx"),
Q!(" mulx " "rbx, rax, [rsi + 0x8]"),
Q!(" adcx " "r11, rax"),
Q!(" adox " "r12, rbx"),
Q!(" mulx " "rbx, rax, [rsi + 0x10]"),
Q!(" adcx " "r12, rax"),
Q!(" adox " "r13, rbx"),
Q!(" mulx " "r14, rax, [rsi + 0x18]"),
Q!(" adcx " "r13, rax"),
Q!(" adox " "r14, rbp"),
Q!(" adcx " "r14, rbp"),
Q!(" xor " "ebp, ebp"),
Q!(" mov " "rdx, [rcx + 0x18]"),
Q!(" mulx " "rbx, rax, [rsi]"),
Q!(" adcx " "r11, rax"),
Q!(" adox " "r12, rbx"),
Q!(" mulx " "rbx, rax, [rsi + 0x8]"),
Q!(" adcx " "r12, rax"),
Q!(" adox " "r13, rbx"),
Q!(" mulx " "rbx, rax, [rsi + 0x10]"),
Q!(" adcx " "r13, rax"),
Q!(" adox " "r14, rbx"),
Q!(" mulx " "r15, rax, [rsi + 0x18]"),
Q!(" adcx " "r14, rax"),
Q!(" adox " "r15, rbp"),
Q!(" adcx " "r15, rbp"),
// Now do the modular reduction and write back
reduce0!("r15", "r14", "r13", "r12"),
reduce!("r15", "r14", "r13", "r12", "r11"),
reduce!("r14", "r13", "r12", "r11", "r10"),
reduce!("r13", "r12", "r11", "r10", "r9"),
reduce!("r12", "r11", "r10", "r9", "r8"),
Q!(" mov " "[rdi], r8"),
Q!(" mov " "[rdi + 8], r9"),
Q!(" mov " "[rdi + 16], r10"),
Q!(" mov " "[rdi + 24], r11"),
// Restore registers and return
Q!(" pop " "r15"),
Q!(" pop " "r14"),
Q!(" pop " "r13"),
Q!(" pop " "r12"),
Q!(" pop " "rbp"),
Q!(" pop " "rbx"),
inout("rdi") z.as_mut_ptr() => _,
inout("rsi") x.as_ptr() => _,
inout("rdx") y.as_ptr() => _,
inout("rcx") c.as_ptr() => _,
// clobbers
out("r10") _,
out("r11") _,
out("r12") _,
out("r13") _,
out("r14") _,
out("r15") _,
out("r8") _,
out("r9") _,
out("rax") _,
)
};
}