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
// 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
// ----------------------------------------------------------------------------
// Add modulo p_521, z := (x + y) mod p_521, assuming x and y reduced
// Inputs x[9], y[9]; output z[9]
//
// extern void bignum_add_p521(uint64_t z[static 9], const uint64_t x[static 9],
// const uint64_t y[static 9]);
//
// Standard x86-64 ABI: RDI = z, RSI = x, RDX = y
// Microsoft x64 ABI: RCX = z, RDX = x, R8 = y
// ----------------------------------------------------------------------------
macro_rules! z {
() => {
"rdi"
};
}
macro_rules! x {
() => {
"rsi"
};
}
macro_rules! y {
() => {
"rdx"
};
}
macro_rules! d0 {
() => {
"rax"
};
}
macro_rules! d1 {
() => {
"rcx"
};
}
macro_rules! d2 {
() => {
"r8"
};
}
macro_rules! d3 {
() => {
"r9"
};
}
macro_rules! d4 {
() => {
"r10"
};
}
macro_rules! d5 {
() => {
"r11"
};
}
macro_rules! d6 {
() => {
"r12"
};
}
macro_rules! d7 {
() => {
"rbx"
};
}
// Re-use the input pointers as other variables once safe to do so
macro_rules! d8 {
() => {
"rsi"
};
}
macro_rules! m {
() => {
"rdx"
};
}
/// Add modulo p_521, z := (x + y) mod p_521, assuming x and y reduced
///
/// Inputs x[9], y[9]; output z[9]
pub(crate) fn bignum_add_p521(z: &mut [u64; 9], x: &[u64; 9], y: &[u64; 9]) {
// SAFETY: inline assembly. see [crate::low::inline_assembly_safety] for safety info.
unsafe {
core::arch::asm!(
Q!(" endbr64 " ),
// Save more registers to play with
Q!(" push " "rbx"),
Q!(" push " "r12"),
// Force carry-in to get s = [d8;d7;d6;d5;d4;d3;d2;d1;d0] = x + y + 1.
// We ignore the carry-out, assuming inputs are reduced so there is none.
Q!(" stc " ),
Q!(" mov " d0!() ", [" x!() "]"),
Q!(" adc " d0!() ", [" y!() "]"),
Q!(" mov " d1!() ", [" x!() "+ 8]"),
Q!(" adc " d1!() ", [" y!() "+ 8]"),
Q!(" mov " d2!() ", [" x!() "+ 16]"),
Q!(" adc " d2!() ", [" y!() "+ 16]"),
Q!(" mov " d3!() ", [" x!() "+ 24]"),
Q!(" adc " d3!() ", [" y!() "+ 24]"),
Q!(" mov " d4!() ", [" x!() "+ 32]"),
Q!(" adc " d4!() ", [" y!() "+ 32]"),
Q!(" mov " d5!() ", [" x!() "+ 40]"),
Q!(" adc " d5!() ", [" y!() "+ 40]"),
Q!(" mov " d6!() ", [" x!() "+ 48]"),
Q!(" adc " d6!() ", [" y!() "+ 48]"),
Q!(" mov " d7!() ", [" x!() "+ 56]"),
Q!(" adc " d7!() ", [" y!() "+ 56]"),
Q!(" mov " d8!() ", [" x!() "+ 64]"),
Q!(" adc " d8!() ", [" y!() "+ 64]"),
// Now x + y >= p_521 <=> s = x + y + 1 >= 2^521
// Make m = 512 * [x + y >= p_521]
Q!(" mov " m!() ", 512"),
Q!(" and " m!() ", " d8!()),
// Now if x + y >= p_521, we want (x + y) - p_521 = s - 2^521
// while otherwise we want x + y = s - 1
// We use the mask m both as an operand and to generate the dual carry
// Write back the results as generated
Q!(" cmp " m!() ", 512"),
Q!(" sbb " d0!() ", 0"),
Q!(" mov " "[" z!() "], " d0!()),
Q!(" sbb " d1!() ", 0"),
Q!(" mov " "[" z!() "+ 8], " d1!()),
Q!(" sbb " d2!() ", 0"),
Q!(" mov " "[" z!() "+ 16], " d2!()),
Q!(" sbb " d3!() ", 0"),
Q!(" mov " "[" z!() "+ 24], " d3!()),
Q!(" sbb " d4!() ", 0"),
Q!(" mov " "[" z!() "+ 32], " d4!()),
Q!(" sbb " d5!() ", 0"),
Q!(" mov " "[" z!() "+ 40], " d5!()),
Q!(" sbb " d6!() ", 0"),
Q!(" mov " "[" z!() "+ 48], " d6!()),
Q!(" sbb " d7!() ", 0"),
Q!(" mov " "[" z!() "+ 56], " d7!()),
Q!(" sbb " d8!() ", " m!()),
Q!(" mov " "[" z!() "+ 64], " d8!()),
// Restore registers and return
Q!(" pop " "r12"),
Q!(" pop " "rbx"),
inout("rdi") z.as_mut_ptr() => _,
inout("rsi") x.as_ptr() => _,
inout("rdx") y.as_ptr() => _,
// clobbers
out("r10") _,
out("r11") _,
out("r12") _,
out("r8") _,
out("r9") _,
out("rax") _,
out("rcx") _,
)
};
}