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
// 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
// ----------------------------------------------------------------------------
// Convert to Montgomery form z := (2^384 * x) mod p_384
// Input x[6]; output z[6]
//
// extern void bignum_tomont_p384(uint64_t z[static 6],
// const uint64_t x[static 6]);
//
// Standard x86-64 ABI: RDI = z, RSI = x
// Microsoft x64 ABI: RCX = z, RDX = x
// ----------------------------------------------------------------------------
macro_rules! z {
() => {
"rdi"
};
}
macro_rules! x {
() => {
"rsi"
};
}
// Fairly consistently used as a zero register
macro_rules! zero {
() => {
"rbp"
};
}
// Some temp registers for the last correction stage
macro_rules! d {
() => {
"rax"
};
}
macro_rules! u {
() => {
"rdx"
};
}
macro_rules! v {
() => {
"rcx"
};
}
macro_rules! w {
() => {
"rsi"
};
}
macro_rules! vshort {
() => {
"ecx"
};
}
macro_rules! wshort {
() => {
"esi"
};
}
// Add rdx * m into a register-pair (high,low)
// maintaining consistent double-carrying with adcx and adox,
// using rax and rcx as temporaries
macro_rules! mulpadd {
($high:expr, $low:expr, $m:expr) => { Q!(
"mulx rcx, rax, " $m ";\n"
"adcx " $low ", rax;\n"
"adox " $high ", rcx"
)}
}
// Core one-step Montgomery reduction macro. Takes input in
// [d7;d6;d5;d4;d3;d2;d1;d0] and returns result in [d7;d6;d5;d4;d3;d2;d1],
// adding to the existing contents, re-using d0 as a temporary internally
//
// We want to add (2^384 - 2^128 - 2^96 + 2^32 - 1) * w
// where w = [d0 + (d0<<32)] mod 2^64
//
// montredc(d7,d6,d5,d4,d3,d2,d1,d0)
//
// This particular variant, with its mix of addition and subtraction
// at the top, is not intended to maintain a coherent carry or borrow out.
// It is assumed the final result would fit in [d7;d6;d5;d4;d3;d2;d1].
// which is always the case here as the top word is even always in {0,1}
macro_rules! montredc {
($d7:expr, $d6:expr, $d5:expr, $d4:expr, $d3:expr, $d2:expr, $d1:expr, $d0:expr) => { Q!(
/* Our correction multiplier is w = [d0 + (d0<<32)] mod 2^64 */
"mov rdx, " $d0 ";\n"
"shl rdx, 32;\n"
"add rdx, " $d0 ";\n"
/* Construct [rbp;rcx;rax;-] = (2^384 - p_384) * w */
/* We know the lowest word will cancel so we can re-use d0 as a temp */
"xor ebp, ebp;\n"
"mov rax, 0xffffffff00000001;\n"
"mulx rax, rcx, rax;\n"
"mov ecx, 0x00000000ffffffff;\n"
"mulx rcx, " $d0 ", rcx;\n"
"adc rax, " $d0 ";\n"
"adc rcx, rdx;\n"
"adc ebp, ebp;\n"
/* Now subtract that and add 2^384 * w */
"sub " $d1 ", rax;\n"
"sbb " $d2 ", rcx;\n"
"sbb " $d3 ", rbp;\n"
"sbb " $d4 ", 0;\n"
"sbb " $d5 ", 0;\n"
"sbb rdx, 0;\n"
"add " $d6 ", rdx;\n"
"adc " $d7 ", 0"
)}
}
/// Convert to Montgomery form z := (2^384 * x) mod p_384
///
/// Input x[6]; output z[6]
pub(crate) fn bignum_tomont_p384(z: &mut [u64; 6], x: &[u64; 6]) {
// SAFETY: inline assembly. see [crate::low::inline_assembly_safety] for safety info.
unsafe {
core::arch::asm!(
Q!(" endbr64 " ),
// We are essentially just doing a Montgomery multiplication of x and the
// precomputed constant y = 2^768 mod p, so the code is almost the same
// modulo a few registers and the change from loading y[i] to using constants,
// plus the easy digits y[4] = 1 and y[5] = 0 being treated specially.
// Because there is no y pointer to keep, we use one register less.
Q!(" push " "rbp"),
Q!(" push " "r12"),
Q!(" push " "r13"),
Q!(" push " "r14"),
Q!(" push " "r15"),
// Do row 0 computation, which is a bit different:
// set up initial window [r14,r13,r12,r11,r10,r9,r8] = y[0] * x
// Unlike later, we only need a single carry chain
Q!(" mov " "rdx, 0xfffffffe00000001"),
Q!(" mulx " "r9, r8, [" x!() "]"),
Q!(" mulx " "r10, rcx, [" x!() "+ 8]"),
Q!(" add " "r9, rcx"),
Q!(" mulx " "r11, rcx, [" x!() "+ 16]"),
Q!(" adc " "r10, rcx"),
Q!(" mulx " "r12, rcx, [" x!() "+ 24]"),
Q!(" adc " "r11, rcx"),
Q!(" mulx " "r13, rcx, [" x!() "+ 32]"),
Q!(" adc " "r12, rcx"),
Q!(" mulx " "r14, rcx, [" x!() "+ 40]"),
Q!(" adc " "r13, rcx"),
Q!(" adc " "r14, 0"),
// Montgomery reduce the zeroth window
Q!(" xor " "r15, r15"),
montredc!("r15", "r14", "r13", "r12", "r11", "r10", "r9", "r8"),
// Add row 1
Q!(" xor " zero!() ", " zero!()),
Q!(" mov " "rdx, 0x0000000200000000"),
Q!(" xor " "r8, r8"),
mulpadd!("r10", "r9", Q!("[" x!() "]")),
mulpadd!("r11", "r10", Q!("[" x!() "+" "8" "]")),
mulpadd!("r12", "r11", Q!("[" x!() "+" "16" "]")),
mulpadd!("r13", "r12", Q!("[" x!() "+" "24" "]")),
mulpadd!("r14", "r13", Q!("[" x!() "+" "32" "]")),
mulpadd!("r15", "r14", Q!("[" x!() "+" "40" "]")),
Q!(" adcx " "r15, " zero!()),
Q!(" adox " "r8, " zero!()),
Q!(" adcx " "r8, " zero!()),
// Montgomery reduce window 1
montredc!("r8", "r15", "r14", "r13", "r12", "r11", "r10", "r9"),
// Add row 2
Q!(" xor " zero!() ", " zero!()),
Q!(" mov " "rdx, 0xfffffffe00000000"),
Q!(" xor " "r9, r9"),
mulpadd!("r11", "r10", Q!("[" x!() "]")),
mulpadd!("r12", "r11", Q!("[" x!() "+" "8" "]")),
mulpadd!("r13", "r12", Q!("[" x!() "+" "16" "]")),
mulpadd!("r14", "r13", Q!("[" x!() "+" "24" "]")),
mulpadd!("r15", "r14", Q!("[" x!() "+" "32" "]")),
mulpadd!("r8", "r15", Q!("[" x!() "+" "40" "]")),
Q!(" adcx " "r8, " zero!()),
Q!(" adox " "r9, " zero!()),
Q!(" adcx " "r9, " zero!()),
// Montgomery reduce window 2
montredc!("r9", "r8", "r15", "r14", "r13", "r12", "r11", "r10"),
// Add row 3
Q!(" xor " zero!() ", " zero!()),
Q!(" mov " "rdx, 0x0000000200000000"),
Q!(" xor " "r10, r10"),
mulpadd!("r12", "r11", Q!("[" x!() "]")),
mulpadd!("r13", "r12", Q!("[" x!() "+" "8" "]")),
mulpadd!("r14", "r13", Q!("[" x!() "+" "16" "]")),
mulpadd!("r15", "r14", Q!("[" x!() "+" "24" "]")),
mulpadd!("r8", "r15", Q!("[" x!() "+" "32" "]")),
mulpadd!("r9", "r8", Q!("[" x!() "+" "40" "]")),
Q!(" adcx " "r9, " zero!()),
Q!(" adox " "r10, " zero!()),
Q!(" adcx " "r10, " zero!()),
// Montgomery reduce window 3
montredc!("r10", "r9", "r8", "r15", "r14", "r13", "r12", "r11"),
// Add row 4. The multiplier y[4] = 1, so we just add x to the window
// while extending it with one more digit, initially this carry
Q!(" xor " "r11, r11"),
Q!(" add " "r12, [" x!() "]"),
Q!(" adc " "r13, [" x!() "+ 8]"),
Q!(" adc " "r14, [" x!() "+ 16]"),
Q!(" adc " "r15, [" x!() "+ 24]"),
Q!(" adc " "r8, [" x!() "+ 32]"),
Q!(" adc " "r9, [" x!() "+ 40]"),
Q!(" adc " "r10, 0"),
Q!(" adc " "r11, 0"),
// Montgomery reduce window 4
montredc!("r11", "r10", "r9", "r8", "r15", "r14", "r13", "r12"),
// Add row 5, The multiplier y[5] = 0, so this is trivial: all we do is
// bring down another zero digit into the window.
Q!(" xor " "r12, r12"),
// Montgomery reduce window 5
montredc!("r12", "r11", "r10", "r9", "r8", "r15", "r14", "r13"),
// We now have a pre-reduced 7-word form [r12;r11;r10;r9;r8;r15;r14]
// We know, writing B = 2^{6*64} that the full implicit result is
// B^2 c <= z + (B - 1) * p < B * p + (B - 1) * p < 2 * B * p,
// so the top half is certainly < 2 * p. If c = 1 already, we know
// subtracting p will give the reduced modulus. But now we do a
// comparison to catch cases where the residue is >= p.
// First set [0;0;0;w;v;u] = 2^384 - p_384
Q!(" mov " u!() ", 0xffffffff00000001"),
Q!(" mov " vshort!() ", 0x00000000ffffffff"),
Q!(" mov " wshort!() ", 0x0000000000000001"),
// Let dd = [r11;r10;r9;r8;r15;r14] be the topless 6-word intermediate result.
// Set CF if the addition dd + (2^384 - p_384) >= 2^384, hence iff dd >= p_384.
Q!(" mov " d!() ", r14"),
Q!(" add " d!() ", " u!()),
Q!(" mov " d!() ", r15"),
Q!(" adc " d!() ", " v!()),
Q!(" mov " d!() ", r8"),
Q!(" adc " d!() ", " w!()),
Q!(" mov " d!() ", r9"),
Q!(" adc " d!() ", 0"),
Q!(" mov " d!() ", r10"),
Q!(" adc " d!() ", 0"),
Q!(" mov " d!() ", r11"),
Q!(" adc " d!() ", 0"),
// Now just add this new carry into the existing r12. It's easy to see they
// can't both be 1 by our range assumptions, so this gives us a {0,1} flag
Q!(" adc " "r12, 0"),
// Now convert it into a bitmask
Q!(" neg " "r12"),
// Masked addition of 2^384 - p_384, hence subtraction of p_384
Q!(" and " u!() ", r12"),
Q!(" and " v!() ", r12"),
Q!(" and " w!() ", r12"),
Q!(" add " "r14, " u!()),
Q!(" adc " "r15, " v!()),
Q!(" adc " "r8, " w!()),
Q!(" adc " "r9, 0"),
Q!(" adc " "r10, 0"),
Q!(" adc " "r11, 0"),
// Write back the result
Q!(" mov " "[" z!() "], r14"),
Q!(" mov " "[" z!() "+ 8], r15"),
Q!(" mov " "[" z!() "+ 16], r8"),
Q!(" mov " "[" z!() "+ 24], r9"),
Q!(" mov " "[" z!() "+ 32], r10"),
Q!(" mov " "[" z!() "+ 40], r11"),
// Restore registers and return
Q!(" pop " "r15"),
Q!(" pop " "r14"),
Q!(" pop " "r13"),
Q!(" pop " "r12"),
Q!(" pop " "rbp"),
inout("rdi") z.as_mut_ptr() => _,
inout("rsi") x.as_ptr() => _,
// clobbers
out("r10") _,
out("r11") _,
out("r12") _,
out("r13") _,
out("r14") _,
out("r15") _,
out("r8") _,
out("r9") _,
out("rax") _,
out("rcx") _,
out("rdx") _,
)
};
}