Skip to main content

ab_riscv_interpreter/rv64/zk/zkn/zknh/
rv64_zknh_helpers.rs

1//! Opaque helpers for RV64 Zknh extension
2
3use const_fn_specialization::const_fn_specialization;
4
5#[const_fn_specialization]
6#[inline(always)]
7#[doc(hidden)]
8#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
9pub fn sha256sig0(x: u32) -> u32 {
10    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
11    cfg_select! {
12        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
13            // SAFETY: Compile-time checked for supported feature
14            unsafe { core::arch::riscv64::sha256sig0(x) }
15        }
16        _ => x.rotate_right(7) ^ x.rotate_right(18) ^ (x >> 3),
17    }
18}
19
20#[const_fn_specialization]
21#[inline(always)]
22#[doc(hidden)]
23#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
24pub const fn sha256sig0(x: u32) -> u32 {
25    x.rotate_right(7) ^ x.rotate_right(18) ^ (x >> 3)
26}
27
28#[const_fn_specialization]
29#[inline(always)]
30#[doc(hidden)]
31#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
32pub fn sha256sig1(x: u32) -> u32 {
33    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
34    cfg_select! {
35        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
36            // SAFETY: Compile-time checked for supported feature
37            unsafe { core::arch::riscv64::sha256sig1(x) }
38        }
39        _ => x.rotate_right(17) ^ x.rotate_right(19) ^ (x >> 10),
40    }
41}
42
43#[const_fn_specialization]
44#[inline(always)]
45#[doc(hidden)]
46#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
47pub const fn sha256sig1(x: u32) -> u32 {
48    x.rotate_right(17) ^ x.rotate_right(19) ^ (x >> 10)
49}
50
51#[const_fn_specialization]
52#[inline(always)]
53#[doc(hidden)]
54#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
55pub fn sha256sum0(x: u32) -> u32 {
56    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
57    cfg_select! {
58        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
59            // SAFETY: Compile-time checked for supported feature
60            unsafe { core::arch::riscv64::sha256sum0(x) }
61        }
62        _ => x.rotate_right(2) ^ x.rotate_right(13) ^ x.rotate_right(22),
63    }
64}
65
66#[const_fn_specialization]
67#[inline(always)]
68#[doc(hidden)]
69#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
70pub const fn sha256sum0(x: u32) -> u32 {
71    x.rotate_right(2) ^ x.rotate_right(13) ^ x.rotate_right(22)
72}
73
74#[const_fn_specialization]
75#[inline(always)]
76#[doc(hidden)]
77#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
78pub fn sha256sum1(x: u32) -> u32 {
79    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
80    cfg_select! {
81        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
82            // SAFETY: Compile-time checked for supported feature
83            unsafe { core::arch::riscv64::sha256sum1(x) }
84        }
85        _ => x.rotate_right(6) ^ x.rotate_right(11) ^ x.rotate_right(25),
86    }
87}
88
89#[const_fn_specialization]
90#[inline(always)]
91#[doc(hidden)]
92#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
93pub const fn sha256sum1(x: u32) -> u32 {
94    x.rotate_right(6) ^ x.rotate_right(11) ^ x.rotate_right(25)
95}
96
97#[const_fn_specialization]
98#[inline(always)]
99#[doc(hidden)]
100#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
101pub fn sha512sig0(x: u64) -> u64 {
102    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
103    cfg_select! {
104        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
105            // SAFETY: Compile-time checked for supported feature
106            unsafe { core::arch::riscv64::sha512sig0(x) }
107        }
108        _ => x.rotate_right(1) ^ x.rotate_right(8) ^ (x >> 7),
109    }
110}
111
112#[const_fn_specialization]
113#[inline(always)]
114#[doc(hidden)]
115#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
116pub const fn sha512sig0(x: u64) -> u64 {
117    x.rotate_right(1) ^ x.rotate_right(8) ^ (x >> 7)
118}
119
120#[const_fn_specialization]
121#[inline(always)]
122#[doc(hidden)]
123#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
124pub fn sha512sig1(x: u64) -> u64 {
125    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
126    cfg_select! {
127        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
128            // SAFETY: Compile-time checked for supported feature
129            unsafe { core::arch::riscv64::sha512sig1(x) }
130        }
131        _ => x.rotate_right(19) ^ x.rotate_right(61) ^ (x >> 6),
132    }
133}
134
135#[const_fn_specialization]
136#[inline(always)]
137#[doc(hidden)]
138#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
139pub const fn sha512sig1(x: u64) -> u64 {
140    x.rotate_right(19) ^ x.rotate_right(61) ^ (x >> 6)
141}
142
143#[const_fn_specialization]
144#[inline(always)]
145#[doc(hidden)]
146#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
147pub fn sha512sum0(x: u64) -> u64 {
148    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
149    cfg_select! {
150        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
151            // SAFETY: Compile-time checked for supported feature
152            unsafe { core::arch::riscv64::sha512sum0(x) }
153        }
154        _ => x.rotate_right(28) ^ x.rotate_right(34) ^ x.rotate_right(39),
155    }
156}
157
158#[const_fn_specialization]
159#[inline(always)]
160#[doc(hidden)]
161#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
162pub const fn sha512sum0(x: u64) -> u64 {
163    x.rotate_right(28) ^ x.rotate_right(34) ^ x.rotate_right(39)
164}
165
166#[const_fn_specialization]
167#[inline(always)]
168#[doc(hidden)]
169#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
170pub fn sha512sum1(x: u64) -> u64 {
171    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
172    cfg_select! {
173        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
174            // SAFETY: Compile-time checked for supported feature
175            unsafe { core::arch::riscv64::sha512sum1(x) }
176        }
177        _ => x.rotate_right(14) ^ x.rotate_right(18) ^ x.rotate_right(41),
178    }
179}
180
181#[const_fn_specialization]
182#[inline(always)]
183#[doc(hidden)]
184#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
185pub const fn sha512sum1(x: u64) -> u64 {
186    x.rotate_right(14) ^ x.rotate_right(18) ^ x.rotate_right(41)
187}