Skip to main content

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

1//! Opaque helpers for RV64 Zknh extension
2
3#[inline(always)]
4#[doc(hidden)]
5#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
6pub fn sha256sig0(x: u32) -> u32 {
7    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
8    cfg_select! {
9        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
10            // SAFETY: Compile-time checked for supported feature
11            unsafe { core::arch::riscv64::sha256sig0(x) }
12        }
13        _ => {
14            x.rotate_right(7) ^ x.rotate_right(18) ^ (x >> 3)
15        }
16    }
17}
18
19#[inline(always)]
20#[doc(hidden)]
21#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
22pub fn sha256sig1(x: u32) -> u32 {
23    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
24    cfg_select! {
25        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
26            // SAFETY: Compile-time checked for supported feature
27            unsafe { core::arch::riscv64::sha256sig1(x) }
28        }
29        _ => {
30            x.rotate_right(17) ^ x.rotate_right(19) ^ (x >> 10)
31        }
32    }
33}
34
35#[inline(always)]
36#[doc(hidden)]
37#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
38pub fn sha256sum0(x: u32) -> u32 {
39    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
40    cfg_select! {
41        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
42            // SAFETY: Compile-time checked for supported feature
43            unsafe { core::arch::riscv64::sha256sum0(x) }
44        }
45        _ => {
46            x.rotate_right(2) ^ x.rotate_right(13) ^ x.rotate_right(22)
47        }
48    }
49}
50
51#[inline(always)]
52#[doc(hidden)]
53#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
54pub fn sha256sum1(x: u32) -> u32 {
55    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
56    cfg_select! {
57        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
58            // SAFETY: Compile-time checked for supported feature
59            unsafe { core::arch::riscv64::sha256sum1(x) }
60        }
61        _ => {
62            x.rotate_right(6) ^ x.rotate_right(11) ^ x.rotate_right(25)
63        }
64    }
65}
66
67#[inline(always)]
68#[doc(hidden)]
69#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
70pub fn sha512sig0(x: u64) -> u64 {
71    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
72    cfg_select! {
73        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
74            // SAFETY: Compile-time checked for supported feature
75            unsafe { core::arch::riscv64::sha512sig0(x) }
76        }
77        _ => {
78            x.rotate_right(1) ^ x.rotate_right(8) ^ (x >> 7)
79        }
80    }
81}
82
83#[inline(always)]
84#[doc(hidden)]
85#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
86pub fn sha512sig1(x: u64) -> u64 {
87    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
88    cfg_select! {
89        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
90            // SAFETY: Compile-time checked for supported feature
91            unsafe { core::arch::riscv64::sha512sig1(x) }
92        }
93        _ => {
94            x.rotate_right(19) ^ x.rotate_right(61) ^ (x >> 6)
95        }
96    }
97}
98
99#[inline(always)]
100#[doc(hidden)]
101#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
102pub fn sha512sum0(x: u64) -> u64 {
103    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
104    cfg_select! {
105        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
106            // SAFETY: Compile-time checked for supported feature
107            unsafe { core::arch::riscv64::sha512sum0(x) }
108        }
109        _ => {
110            x.rotate_right(28) ^ x.rotate_right(34) ^ x.rotate_right(39)
111        }
112    }
113}
114
115#[inline(always)]
116#[doc(hidden)]
117#[cfg_attr(feature = "no-panic", no_panic_const::no_panic)]
118pub fn sha512sum1(x: u64) -> u64 {
119    // TODO: Miri is excluded because corresponding intrinsic is not implemented there
120    cfg_select! {
121        all(not(miri), target_arch = "riscv64", target_feature = "zknh") => {
122            // SAFETY: Compile-time checked for supported feature
123            unsafe { core::arch::riscv64::sha512sum1(x) }
124        }
125        _ => {
126            x.rotate_right(14) ^ x.rotate_right(18) ^ x.rotate_right(41)
127        }
128    }
129}