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
// 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
// ----------------------------------------------------------------------------
// Subtract modulo m, z := (x - y) mod m, assuming x and y reduced
// Inputs x[k], y[k], m[k]; output z[k]
//
// extern void bignum_modsub(uint64_t k, uint64_t *z, const uint64_t *x,
// const uint64_t *y, const uint64_t *m);
//
// Standard ARM ABI: X0 = k, X1 = z, X2 = x, X3 = y, X4 = m
// ----------------------------------------------------------------------------
macro_rules! k {
() => {
"x0"
};
}
macro_rules! z {
() => {
"x1"
};
}
macro_rules! x {
() => {
"x2"
};
}
macro_rules! y {
() => {
"x3"
};
}
macro_rules! m {
() => {
"x4"
};
}
macro_rules! i {
() => {
"x5"
};
}
macro_rules! j {
() => {
"x6"
};
}
macro_rules! a {
() => {
"x7"
};
}
macro_rules! b {
() => {
"x8"
};
}
macro_rules! c {
() => {
"x9"
};
}
/// Subtract modulo m, z := (x - y) mod m, assuming x and y reduced
///
/// Inputs x[k], y[k], m[k]; output z[k]
pub(crate) fn bignum_modsub(z: &mut [u64], x: &[u64], y: &[u64], m: &[u64]) {
debug_assert!(z.len() == x.len());
debug_assert!(z.len() == y.len());
debug_assert!(z.len() == m.len());
// SAFETY: inline assembly. see [crate::low::inline_assembly_safety] for safety info.
unsafe {
core::arch::asm!(
Q!(" adds " j!() ", " k!() ", xzr"),
Q!(" beq " Label!("bignum_modsub_end", 2, After)),
Q!(" subs " i!() ", xzr, xzr"),
// Subtract z := x - y and record a mask for the carry x - y < 0
Q!(Label!("bignum_modsub_subloop", 3) ":"),
Q!(" ldr " a!() ", [" x!() ", " i!() "]"),
Q!(" ldr " b!() ", [" y!() ", " i!() "]"),
Q!(" sbcs " a!() ", " a!() ", " b!()),
Q!(" str " a!() ", [" z!() ", " i!() "]"),
Q!(" add " i!() ", " i!() ", #8"),
Q!(" sub " j!() ", " j!() ", #1"),
Q!(" cbnz " j!() ", " Label!("bignum_modsub_subloop", 3, Before)),
Q!(" csetm " c!() ", cc"),
// Now do a masked addition z := z + [c] * m
Q!(" mov " j!() ", " k!()),
Q!(" adds " i!() ", xzr, xzr"),
Q!(Label!("bignum_modsub_addloop", 4) ":"),
Q!(" ldr " a!() ", [" z!() ", " i!() "]"),
Q!(" ldr " b!() ", [" m!() ", " i!() "]"),
Q!(" and " b!() ", " b!() ", " c!()),
Q!(" adcs " a!() ", " a!() ", " b!()),
Q!(" str " a!() ", [" z!() ", " i!() "]"),
Q!(" add " i!() ", " i!() ", #8"),
Q!(" sub " j!() ", " j!() ", #1"),
Q!(" cbnz " j!() ", " Label!("bignum_modsub_addloop", 4, Before)),
Q!(Label!("bignum_modsub_end", 2) ":"),
inout("x0") z.len() => _,
inout("x1") z.as_mut_ptr() => _,
inout("x2") x.as_ptr() => _,
inout("x3") y.as_ptr() => _,
inout("x4") m.as_ptr() => _,
// clobbers
out("x5") _,
out("x6") _,
out("x7") _,
out("x8") _,
out("x9") _,
)
};
}