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
// 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 Montgomery form z := (x / 2^384) mod p_384, assuming x reduced
// Input x[6]; output z[6]
//
// extern void bignum_demont_p384(uint64_t z[static 6],
// const uint64_t x[static 6]);
//
// This assumes the input is < p_384 for correctness. If this is not the case,
// use the variant "bignum_deamont_p384" instead.
//
// Standard ARM ABI: X0 = z, X1 = x
// ----------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Core one-step "short" Montgomery reduction macro. Takes input in
// [d5;d4;d3;d2;d1;d0] and returns result in [d6;d5;d4;d3;d2;d1],
// adding to the existing contents of [d5;d4;d3;d2;d1]. It is fine
// for d6 to be the same register as d0.
//
// We want to add (2^384 - 2^128 - 2^96 + 2^32 - 1) * w
// where w = [d0 + (d0<<32)] mod 2^64
// ---------------------------------------------------------------------------
macro_rules! montreds {
($d6:expr, $d5:expr, $d4:expr, $d3:expr, $d2:expr, $d1:expr, $d0:expr, $t3:expr, $t2:expr, $t1:expr) => { Q!(
/* Our correction multiplier is w = [d0 + (d0<<32)] mod 2^64 */
/* Recycle d0 (which we know gets implicitly cancelled) to store it */
"lsl " $t1 ", " $d0 ", #32;\n"
"add " $d0 ", " $t1 ", " $d0 ";\n"
/* Now let [t2;t1] = 2^64 * w - w + w_hi where w_hi = floor(w/2^32) */
/* We need to subtract 2^32 * this, and we can ignore its lower 32 */
/* bits since by design it will cancel anyway; we only need the w_hi */
/* part to get the carry propagation going. */
"lsr " $t1 ", " $d0 ", #32;\n"
"subs " $t1 ", " $t1 ", " $d0 ";\n"
"sbc " $t2 ", " $d0 ", xzr;\n"
/* Now select in t1 the field to subtract from d1 */
"extr " $t1 ", " $t2 ", " $t1 ", #32;\n"
/* And now get the terms to subtract from d2 and d3 */
"lsr " $t2 ", " $t2 ", #32;\n"
"adds " $t2 ", " $t2 ", " $d0 ";\n"
"adc " $t3 ", xzr, xzr;\n"
/* Do the subtraction of that portion */
"subs " $d1 ", " $d1 ", " $t1 ";\n"
"sbcs " $d2 ", " $d2 ", " $t2 ";\n"
"sbcs " $d3 ", " $d3 ", " $t3 ";\n"
"sbcs " $d4 ", " $d4 ", xzr;\n"
"sbcs " $d5 ", " $d5 ", xzr;\n"
/* Now effectively add 2^384 * w by taking d0 as the input for last sbc */
"sbc " $d6 ", " $d0 ", xzr"
)}
}
// Input parameters
macro_rules! z {
() => {
"x0"
};
}
macro_rules! x {
() => {
"x1"
};
}
// Rotating registers for the intermediate windows
macro_rules! d0 {
() => {
"x2"
};
}
macro_rules! d1 {
() => {
"x3"
};
}
macro_rules! d2 {
() => {
"x4"
};
}
macro_rules! d3 {
() => {
"x5"
};
}
macro_rules! d4 {
() => {
"x6"
};
}
macro_rules! d5 {
() => {
"x7"
};
}
// Other temporaries
macro_rules! u {
() => {
"x8"
};
}
macro_rules! v {
() => {
"x9"
};
}
macro_rules! w {
() => {
"x10"
};
}
/// Convert from Montgomery form z := (x / 2^384) mod p_384, assuming x reduced
///
/// Input x[6]; output z[6]
///
/// This assumes the input is < p_384 for correctness. If this is not the case,
/// use the variant "bignum_deamont_p384" instead.
pub(crate) fn bignum_demont_p384(z: &mut [u64; 6], x: &[u64; 6]) {
// SAFETY: inline assembly. see [crate::low::inline_assembly_safety] for safety info.
unsafe {
core::arch::asm!(
// Set up an initial window with the input x and an extra leading zero
Q!(" ldp " d0!() ", " d1!() ", [" x!() "]"),
Q!(" ldp " d2!() ", " d3!() ", [" x!() ", #16]"),
Q!(" ldp " d4!() ", " d5!() ", [" x!() ", #32]"),
// Systematically scroll left doing 1-step reductions
montreds!(d0!(), d5!(), d4!(), d3!(), d2!(), d1!(), d0!(), u!(), v!(), w!()),
montreds!(d1!(), d0!(), d5!(), d4!(), d3!(), d2!(), d1!(), u!(), v!(), w!()),
montreds!(d2!(), d1!(), d0!(), d5!(), d4!(), d3!(), d2!(), u!(), v!(), w!()),
montreds!(d3!(), d2!(), d1!(), d0!(), d5!(), d4!(), d3!(), u!(), v!(), w!()),
montreds!(d4!(), d3!(), d2!(), d1!(), d0!(), d5!(), d4!(), u!(), v!(), w!()),
montreds!(d5!(), d4!(), d3!(), d2!(), d1!(), d0!(), d5!(), u!(), v!(), w!()),
// This is already our answer with no correction needed
Q!(" stp " d0!() ", " d1!() ", [" z!() "]"),
Q!(" stp " d2!() ", " d3!() ", [" z!() ", #16]"),
Q!(" stp " d4!() ", " d5!() ", [" z!() ", #32]"),
inout("x0") z.as_mut_ptr() => _,
inout("x1") x.as_ptr() => _,
// clobbers
out("v6") _,
out("x10") _,
out("x2") _,
out("x3") _,
out("x4") _,
out("x5") _,
out("x6") _,
out("x7") _,
out("x8") _,
out("x9") _,
)
};
}