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
// 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, z := x + y
// Inputs x[m], y[n]; outputs function return (carry-out) and z[p]
//
// extern uint64_t bignum_add(uint64_t p, uint64_t *z, uint64_t m,
// const uint64_t *x, uint64_t n, const uint64_t *y);
//
// Does the z := x + y operation, truncating modulo p words in general and
// returning a top carry (0 or 1) in the p'th place, only adding the input
// words below p (as well as m and n respectively) to get the sum and carry.
//
// Standard x86-64 ABI: RDI = p, RSI = z, RDX = m, RCX = x, R8 = n, R9 = y, returns RAX
// Microsoft x64 ABI: RCX = p, RDX = z, R8 = m, R9 = x, [RSP+40] = n, [RSP+48] = y, returns RAX
// ----------------------------------------------------------------------------
macro_rules! p {
() => {
"rdi"
};
}
macro_rules! z {
() => {
"rsi"
};
}
macro_rules! m {
() => {
"rdx"
};
}
macro_rules! x {
() => {
"rcx"
};
}
macro_rules! n {
() => {
"r8"
};
}
macro_rules! y {
() => {
"r9"
};
}
macro_rules! i {
() => {
"r10"
};
}
macro_rules! a {
() => {
"rax"
};
}
macro_rules! ashort {
() => {
"eax"
};
}
/// Add, z := x + y
///
/// Inputs x[m], y[n]; outputs function return (carry-out) and z[p]
///
/// Does the z := x + y operation, truncating modulo p words in general and
/// returning a top carry (0 or 1) in the p'th place, only adding the input
/// words below p (as well as m and n respectively) to get the sum and carry.
pub(crate) fn bignum_add(z: &mut [u64], x: &[u64], y: &[u64]) -> u64 {
let ret: u64;
// SAFETY: inline assembly. see [crate::low::inline_assembly_safety] for safety info.
unsafe {
core::arch::asm!(
Q!(" endbr64 " ),
// Zero the main index counter for both branches
Q!(" xor " i!() ", " i!()),
// First clamp the two input sizes m := min(p,m) and n := min(p,n) since
// we'll never need words past the p'th. Can now assume m <= p and n <= p.
// Then compare the modified m and n and branch accordingly
Q!(" cmp " p!() ", " m!()),
Q!(" cmovc " m!() ", " p!()),
Q!(" cmp " p!() ", " n!()),
Q!(" cmovc " n!() ", " p!()),
Q!(" cmp " m!() ", " n!()),
Q!(" jc " Label!("bignum_add_ylonger", 2, After)),
// The case where x is longer or of the same size (p >= m >= n)
Q!(" sub " p!() ", " m!()),
Q!(" sub " m!() ", " n!()),
Q!(" inc " m!()),
Q!(" test " n!() ", " n!()),
Q!(" jz " Label!("bignum_add_xtest", 3, After)),
Q!(Label!("bignum_add_xmainloop", 4) ":"),
Q!(" mov " a!() ", [" x!() "+ 8 * " i!() "]"),
Q!(" adc " a!() ", [" y!() "+ 8 * " i!() "]"),
Q!(" mov " "[" z!() "+ 8 * " i!() "], " a!()),
Q!(" inc " i!()),
Q!(" dec " n!()),
Q!(" jnz " Label!("bignum_add_xmainloop", 4, Before)),
Q!(" jmp " Label!("bignum_add_xtest", 3, After)),
Q!(Label!("bignum_add_xtoploop", 5) ":"),
Q!(" mov " a!() ", [" x!() "+ 8 * " i!() "]"),
Q!(" adc " a!() ", 0"),
Q!(" mov " "[" z!() "+ 8 * " i!() "], " a!()),
Q!(" inc " i!()),
Q!(Label!("bignum_add_xtest", 3) ":"),
Q!(" dec " m!()),
Q!(" jnz " Label!("bignum_add_xtoploop", 5, Before)),
Q!(" mov " ashort!() ", 0"),
Q!(" adc " a!() ", 0"),
Q!(" test " p!() ", " p!()),
Q!(" jnz " Label!("bignum_add_tails", 6, After)),
// linear hoisting in -> ret after bignum_add_tail
Q!(" jmp " Label!("hoist_finish", 7, After)),
// The case where y is longer (p >= n > m)
Q!(Label!("bignum_add_ylonger", 2) ":"),
Q!(" sub " p!() ", " n!()),
Q!(" sub " n!() ", " m!()),
Q!(" test " m!() ", " m!()),
Q!(" jz " Label!("bignum_add_ytoploop", 8, After)),
Q!(Label!("bignum_add_ymainloop", 9) ":"),
Q!(" mov " a!() ", [" x!() "+ 8 * " i!() "]"),
Q!(" adc " a!() ", [" y!() "+ 8 * " i!() "]"),
Q!(" mov " "[" z!() "+ 8 * " i!() "], " a!()),
Q!(" inc " i!()),
Q!(" dec " m!()),
Q!(" jnz " Label!("bignum_add_ymainloop", 9, Before)),
Q!(Label!("bignum_add_ytoploop", 8) ":"),
Q!(" mov " a!() ", [" y!() "+ 8 * " i!() "]"),
Q!(" adc " a!() ", 0"),
Q!(" mov " "[" z!() "+ 8 * " i!() "], " a!()),
Q!(" inc " i!()),
Q!(" dec " n!()),
Q!(" jnz " Label!("bignum_add_ytoploop", 8, Before)),
Q!(" mov " ashort!() ", 0"),
Q!(" adc " a!() ", 0"),
Q!(" test " p!() ", " p!()),
Q!(" jnz " Label!("bignum_add_tails", 6, After)),
Q!(" jmp " Label!("hoist_finish", 7, After)),
// Adding a non-trivial tail, when p > max(m,n)
Q!(Label!("bignum_add_tails", 6) ":"),
Q!(" mov " "[" z!() "+ 8 * " i!() "], " a!()),
Q!(" xor " a!() ", " a!()),
Q!(" jmp " Label!("bignum_add_tail", 12, After)),
Q!(Label!("bignum_add_tailloop", 13) ":"),
Q!(" mov " "[" z!() "+ 8 * " i!() "], " a!()),
Q!(Label!("bignum_add_tail", 12) ":"),
Q!(" inc " i!()),
Q!(" dec " p!()),
Q!(" jnz " Label!("bignum_add_tailloop", 13, Before)),
Q!(Label!("hoist_finish", 7) ":"),
inout("rdi") z.len() => _,
inout("rsi") z.as_mut_ptr() => _,
inout("rdx") x.len() => _,
inout("rcx") x.as_ptr() => _,
inout("r8") y.len() => _,
inout("r9") y.as_ptr() => _,
out("rax") ret,
// clobbers
out("r10") _,
)
};
ret
}