core/stdarch/crates/core_arch/src/riscv64/zk.rs
1#[cfg(test)]
2use stdarch_test::assert_instr;
3
4unsafe extern "unadjusted" {
5 #[link_name = "llvm.riscv.aes64es"]
6 fn _aes64es(rs1: i64, rs2: i64) -> i64;
7
8 #[link_name = "llvm.riscv.aes64esm"]
9 fn _aes64esm(rs1: i64, rs2: i64) -> i64;
10
11 #[link_name = "llvm.riscv.aes64ds"]
12 fn _aes64ds(rs1: i64, rs2: i64) -> i64;
13
14 #[link_name = "llvm.riscv.aes64dsm"]
15 fn _aes64dsm(rs1: i64, rs2: i64) -> i64;
16
17 #[link_name = "llvm.riscv.aes64ks1i"]
18 fn _aes64ks1i(rs1: i64, rnum: i32) -> i64;
19
20 #[link_name = "llvm.riscv.aes64ks2"]
21 fn _aes64ks2(rs1: i64, rs2: i64) -> i64;
22
23 #[link_name = "llvm.riscv.aes64im"]
24 fn _aes64im(rs1: i64) -> i64;
25
26 #[link_name = "llvm.riscv.sha512sig0"]
27 fn _sha512sig0(rs1: i64) -> i64;
28
29 #[link_name = "llvm.riscv.sha512sig1"]
30 fn _sha512sig1(rs1: i64) -> i64;
31
32 #[link_name = "llvm.riscv.sha512sum0"]
33 fn _sha512sum0(rs1: i64) -> i64;
34
35 #[link_name = "llvm.riscv.sha512sum1"]
36 fn _sha512sum1(rs1: i64) -> i64;
37}
38
39/// AES final round encryption instruction for RV64.
40///
41/// Uses the two 64-bit source registers to represent the entire AES state, and produces half
42/// of the next round output, applying the ShiftRows and SubBytes steps. This instruction must
43/// always be implemented such that its execution latency does not depend on the data being
44/// operated on.
45///
46/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions
47///
48/// Version: v1.0.1
49///
50/// Section: 3.7
51#[target_feature(enable = "zkne")]
52#[cfg_attr(test, assert_instr(aes64es))]
53#[inline]
54#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
55pub fn aes64es(rs1: u64, rs2: u64) -> u64 {
56 unsafe { _aes64es(rs1 as i64, rs2 as i64) as u64 }
57}
58
59/// AES middle round encryption instruction for RV64.
60///
61/// Uses the two 64-bit source registers to represent the entire AES state, and produces half
62/// of the next round output, applying the ShiftRows, SubBytes and MixColumns steps. This
63/// instruction must always be implemented such that its execution latency does not depend on
64/// the data being operated on.
65///
66/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions
67///
68/// Version: v1.0.1
69///
70/// Section: 3.8
71#[target_feature(enable = "zkne")]
72#[cfg_attr(test, assert_instr(aes64esm))]
73#[inline]
74#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
75pub fn aes64esm(rs1: u64, rs2: u64) -> u64 {
76 unsafe { _aes64esm(rs1 as i64, rs2 as i64) as u64 }
77}
78
79/// AES final round decryption instruction for RV64.
80///
81/// Uses the two 64-bit source registers to represent the entire AES state, and produces half
82/// of the next round output, applying the Inverse ShiftRows and SubBytes steps. This
83/// instruction must always be implemented such that its execution latency does not depend on
84/// the data being operated on.
85///
86/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions
87///
88/// Version: v1.0.1
89///
90/// Section: 3.5
91#[target_feature(enable = "zknd")]
92#[cfg_attr(test, assert_instr(aes64ds))]
93#[inline]
94#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
95pub fn aes64ds(rs1: u64, rs2: u64) -> u64 {
96 unsafe { _aes64ds(rs1 as i64, rs2 as i64) as u64 }
97}
98
99/// AES middle round decryption instruction for RV64.
100///
101/// Uses the two 64-bit source registers to represent the entire AES state, and produces half
102/// of the next round output, applying the Inverse ShiftRows, SubBytes and MixColumns steps.
103/// This instruction must always be implemented such that its execution latency does not depend
104/// on the data being operated on.
105///
106/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions
107///
108/// Version: v1.0.1
109///
110/// Section: 3.6
111#[target_feature(enable = "zknd")]
112#[cfg_attr(test, assert_instr(aes64dsm))]
113#[inline]
114#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
115pub fn aes64dsm(rs1: u64, rs2: u64) -> u64 {
116 unsafe { _aes64dsm(rs1 as i64, rs2 as i64) as u64 }
117}
118
119/// This instruction implements part of the KeySchedule operation for the AES Block cipher
120/// involving the SBox operation.
121///
122/// This instruction implements the rotation, SubBytes and Round Constant addition steps of the
123/// AES block cipher Key Schedule. This instruction must always be implemented such that its
124/// execution latency does not depend on the data being operated on. Note that rnum must be in
125/// the range 0x0..0xA. The values 0xB..0xF are reserved.
126///
127/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions
128///
129/// Version: v1.0.1
130///
131/// Section: 3.10
132///
133/// # Note
134///
135/// The `RNUM` parameter is expected to be a constant value inside the range of `0..=10`.
136#[target_feature(enable = "zkne", enable = "zknd")]
137#[rustc_legacy_const_generics(1)]
138#[cfg_attr(test, assert_instr(aes64ks1i, RNUM = 0))]
139#[inline]
140#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
141pub fn aes64ks1i<const RNUM: u8>(rs1: u64) -> u64 {
142 static_assert!(RNUM <= 10);
143
144 unsafe { _aes64ks1i(rs1 as i64, RNUM as i32) as u64 }
145}
146
147/// This instruction implements part of the KeySchedule operation for the AES Block cipher.
148///
149/// This instruction implements the additional XOR’ing of key words as part of the AES block
150/// cipher Key Schedule. This instruction must always be implemented such that its execution
151/// latency does not depend on the data being operated on.
152///
153/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions
154///
155/// Version: v1.0.1
156///
157/// Section: 3.11
158#[target_feature(enable = "zkne", enable = "zknd")]
159#[cfg_attr(test, assert_instr(aes64ks2))]
160#[inline]
161#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
162pub fn aes64ks2(rs1: u64, rs2: u64) -> u64 {
163 unsafe { _aes64ks2(rs1 as i64, rs2 as i64) as u64 }
164}
165
166/// This instruction accelerates the inverse MixColumns step of the AES Block Cipher, and is used to aid creation of
167/// the decryption KeySchedule.
168///
169/// The instruction applies the inverse MixColumns transformation to two columns of the state array, packed
170/// into a single 64-bit register. It is used to create the inverse cipher KeySchedule, according to the equivalent
171/// inverse cipher construction in (Page 23, Section 5.3.5). This instruction must always be implemented
172/// such that its execution latency does not depend on the data being operated on.
173///
174/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions
175///
176/// Version: v1.0.1
177///
178/// Section: 3.9
179#[target_feature(enable = "zkne", enable = "zknd")]
180#[cfg_attr(test, assert_instr(aes64im))]
181#[inline]
182#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
183pub fn aes64im(rs1: u64) -> u64 {
184 unsafe { _aes64im(rs1 as i64) as u64 }
185}
186
187/// Implements the Sigma0 transformation function as used in the SHA2-512 hash function \[49\]
188/// (Section 4.1.3).
189///
190/// This instruction is supported for the RV64 base architecture. It implements the Sigma0
191/// transform of the SHA2-512 hash function. \[49\]. This instruction must always be
192/// implemented such that its execution latency does not depend on the data being operated on.
193///
194/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions
195///
196/// Version: v1.0.1
197///
198/// Section: 3.37
199#[target_feature(enable = "zknh")]
200#[cfg_attr(test, assert_instr(sha512sig0))]
201#[inline]
202#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
203pub fn sha512sig0(rs1: u64) -> u64 {
204 unsafe { _sha512sig0(rs1 as i64) as u64 }
205}
206
207/// Implements the Sigma1 transformation function as used in the SHA2-512 hash function \[49\]
208/// (Section 4.1.3).
209///
210/// This instruction is supported for the RV64 base architecture. It implements the Sigma1
211/// transform of the SHA2-512 hash function. \[49\]. This instruction must always be
212/// implemented such that its execution latency does not depend on the data being operated on.
213///
214/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions
215///
216/// Version: v1.0.1
217///
218/// Section: 3.38
219#[target_feature(enable = "zknh")]
220#[cfg_attr(test, assert_instr(sha512sig1))]
221#[inline]
222#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
223pub fn sha512sig1(rs1: u64) -> u64 {
224 unsafe { _sha512sig1(rs1 as i64) as u64 }
225}
226
227/// Implements the Sum0 transformation function as used in the SHA2-512 hash function \[49\]
228/// (Section 4.1.3).
229///
230/// This instruction is supported for the RV64 base architecture. It implements the Sum0
231/// transform of the SHA2-512 hash function. \[49\]. This instruction must always be
232/// implemented such that its execution latency does not depend on the data being operated on.
233///
234/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions
235///
236/// Version: v1.0.1
237///
238/// Section: 3.39
239#[target_feature(enable = "zknh")]
240#[cfg_attr(test, assert_instr(sha512sum0))]
241#[inline]
242#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
243pub fn sha512sum0(rs1: u64) -> u64 {
244 unsafe { _sha512sum0(rs1 as i64) as u64 }
245}
246
247/// Implements the Sum1 transformation function as used in the SHA2-512 hash function \[49\]
248/// (Section 4.1.3).
249///
250/// This instruction is supported for the RV64 base architecture. It implements the Sum1
251/// transform of the SHA2-512 hash function. \[49\]. This instruction must always be
252/// implemented such that its execution latency does not depend on the data being operated on.
253///
254/// Source: RISC-V Cryptography Extensions Volume I: Scalar & Entropy Source Instructions
255///
256/// Version: v1.0.1
257///
258/// Section: 3.40
259#[target_feature(enable = "zknh")]
260#[cfg_attr(test, assert_instr(sha512sum1))]
261#[inline]
262#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
263pub fn sha512sum1(rs1: u64) -> u64 {
264 unsafe { _sha512sum1(rs1 as i64) as u64 }
265}