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
// 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 from (almost-)Montgomery form z := (x / 2^{64k}) mod m
// Inputs x[k], m[k]; output z[k]
//
// extern void bignum_demont(uint64_t k, uint64_t *z, const uint64_t *x,
// const uint64_t *m);
//
// Does z := (x / 2^{64k}) mod m, hence mapping out of Montgomery domain.
// In other words, this is a k-fold Montgomery reduction with same-size input.
// This can handle almost-Montgomery inputs, i.e. any k-digit bignum.
//
// Standard x86-64 ABI: RDI = k, RSI = z, RDX = x, RCX = m
// Microsoft x64 ABI: RCX = k, RDX = z, R8 = x, R9 = m
// ----------------------------------------------------------------------------
macro_rules! k {
() => {
"rdi"
};
}
macro_rules! z {
() => {
"rsi"
};
}
macro_rules! x {
() => {
"rdx"
};
}
macro_rules! m {
() => {
"rcx"
};
}
// General temp, low part of product and mul input
macro_rules! a {
() => {
"rax"
};
}
// General temp, high part of product (no longer x)
macro_rules! b {
() => {
"rdx"
};
}
// Negated modular inverse
macro_rules! w {
() => {
"r8"
};
}
// Outer loop counter
macro_rules! i {
() => {
"r9"
};
}
// Inner loop counter
macro_rules! j {
() => {
"rbx"
};
}
// Home for Montgomery multiplier
macro_rules! d {
() => {
"rbp"
};
}
macro_rules! h {
() => {
"r10"
};
}
macro_rules! e {
() => {
"r11"
};
}
macro_rules! n {
() => {
"r12"
};
}
// A temp reg in the initial word-level negmodinv, same as j
macro_rules! t {
() => {
"rbx"
};
}
macro_rules! ashort {
() => {
"eax"
};
}
macro_rules! jshort {
() => {
"ebx"
};
}
/// Convert from (almost-)Montgomery form z := (x / 2^{64k}) mod m
///
/// Inputs x[k], m[k]; output z[k]
///
/// Does z := (x / 2^{64k}) mod m, hence mapping out of Montgomery domain.
/// In other words, this is a k-fold Montgomery reduction with same-size input.
/// This can handle almost-Montgomery inputs, i.e. any k-digit bignum.
pub(crate) fn bignum_demont(z: &mut [u64], x: &[u64], m: &[u64]) {
debug_assert!(z.len() == x.len());
debug_assert!(z.len() == m.len());
// SAFETY: inline assembly. see [crate::low::inline_assembly_safety] for safety info.
unsafe {
core::arch::asm!(
Q!(" endbr64 " ),
// Save registers
Q!(" push " "rbx"),
Q!(" push " "rbp"),
Q!(" push " "r12"),
// If k = 0 the whole operation is trivial
Q!(" test " k!() ", " k!()),
Q!(" jz " Label!("bignum_demont_end", 2, After)),
// Compute word-level negated modular inverse w for m == m[0].
Q!(" mov " a!() ", [" m!() "]"),
Q!(" mov " t!() ", " a!()),
Q!(" mov " w!() ", " a!()),
Q!(" shl " t!() ", 2"),
Q!(" sub " w!() ", " t!()),
Q!(" xor " w!() ", 2"),
Q!(" mov " t!() ", " w!()),
Q!(" imul " t!() ", " a!()),
Q!(" mov " ashort!() ", 2"),
Q!(" add " a!() ", " t!()),
Q!(" add " t!() ", 1"),
Q!(" imul " w!() ", " a!()),
Q!(" imul " t!() ", " t!()),
Q!(" mov " ashort!() ", 1"),
Q!(" add " a!() ", " t!()),
Q!(" imul " w!() ", " a!()),
Q!(" imul " t!() ", " t!()),
Q!(" mov " ashort!() ", 1"),
Q!(" add " a!() ", " t!()),
Q!(" imul " w!() ", " a!()),
Q!(" imul " t!() ", " t!()),
Q!(" mov " ashort!() ", 1"),
Q!(" add " a!() ", " t!()),
Q!(" imul " w!() ", " a!()),
// Initially just copy the input to the output. It would be a little more
// efficient but somewhat fiddlier to tweak the zeroth iteration below instead.
// After this we never use x again and can safely recycle RDX for muls
Q!(" xor " j!() ", " j!()),
Q!(Label!("bignum_demont_iloop", 3) ":"),
Q!(" mov " a!() ", [" x!() "+ 8 * " j!() "]"),
Q!(" mov " "[" z!() "+ 8 * " j!() "], " a!()),
Q!(" inc " j!()),
Q!(" cmp " j!() ", " k!()),
Q!(" jc " Label!("bignum_demont_iloop", 3, Before)),
// Outer loop, just doing a standard Montgomery reduction on z
Q!(" xor " i!() ", " i!()),
Q!(Label!("bignum_demont_outerloop", 4) ":"),
Q!(" mov " e!() ", [" z!() "]"),
Q!(" mov " d!() ", " w!()),
Q!(" imul " d!() ", " e!()),
Q!(" mov " a!() ", [" m!() "]"),
Q!(" mul " d!()),
Q!(" add " a!() ", " e!()),
Q!(" mov " h!() ", rdx"),
Q!(" mov " jshort!() ", 1"),
Q!(" mov " n!() ", " k!()),
Q!(" dec " n!()),
Q!(" jz " Label!("bignum_demont_montend", 5, After)),
Q!(Label!("bignum_demont_montloop", 6) ":"),
Q!(" adc " h!() ", [" z!() "+ 8 * " j!() "]"),
Q!(" sbb " e!() ", " e!()),
Q!(" mov " a!() ", [" m!() "+ 8 * " j!() "]"),
Q!(" mul " d!()),
Q!(" sub " "rdx, " e!()),
Q!(" add " a!() ", " h!()),
Q!(" mov " "[" z!() "+ 8 * " j!() "-8], " a!()),
Q!(" mov " h!() ", rdx"),
Q!(" inc " j!()),
Q!(" dec " n!()),
Q!(" jnz " Label!("bignum_demont_montloop", 6, Before)),
Q!(Label!("bignum_demont_montend", 5) ":"),
Q!(" adc " h!() ", 0"),
Q!(" mov " "[" z!() "+ 8 * " j!() "-8], " h!()),
// End of outer loop.
Q!(" inc " i!()),
Q!(" cmp " i!() ", " k!()),
Q!(" jc " Label!("bignum_demont_outerloop", 4, Before)),
// Now do a comparison of z with m to set a final correction mask
// indicating that z >= m and so we need to subtract m.
Q!(" xor " j!() ", " j!()),
Q!(" mov " n!() ", " k!()),
Q!(Label!("bignum_demont_cmploop", 7) ":"),
Q!(" mov " a!() ", [" z!() "+ 8 * " j!() "]"),
Q!(" sbb " a!() ", [" m!() "+ 8 * " j!() "]"),
Q!(" inc " j!()),
Q!(" dec " n!()),
Q!(" jnz " Label!("bignum_demont_cmploop", 7, Before)),
Q!(" sbb " d!() ", " d!()),
Q!(" not " d!()),
// Now do a masked subtraction of m for the final reduced result.
Q!(" xor " e!() ", " e!()),
Q!(" xor " j!() ", " j!()),
Q!(Label!("bignum_demont_corrloop", 8) ":"),
Q!(" mov " a!() ", [" m!() "+ 8 * " j!() "]"),
Q!(" and " a!() ", " d!()),
Q!(" neg " e!()),
Q!(" sbb " "[" z!() "+ 8 * " j!() "], " a!()),
Q!(" sbb " e!() ", " e!()),
Q!(" inc " j!()),
Q!(" cmp " j!() ", " k!()),
Q!(" jc " Label!("bignum_demont_corrloop", 8, Before)),
Q!(Label!("bignum_demont_end", 2) ":"),
Q!(" pop " "r12"),
Q!(" pop " "rbp"),
Q!(" pop " "rbx"),
inout("rdi") m.len() => _,
inout("rsi") z.as_mut_ptr() => _,
inout("rdx") x.as_ptr() => _,
inout("rcx") m.as_ptr() => _,
// clobbers
out("r10") _,
out("r11") _,
out("r12") _,
out("r8") _,
out("r9") _,
out("rax") _,
)
};
}