Skip to content

Commit

Permalink
fix opaque load
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Aug 26, 2024
1 parent 5ad4f58 commit 87c8b87
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
11 changes: 11 additions & 0 deletions sha2/src/sha256/riscv_zknh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ fn maj(x: u32, y: u32, z: u32) -> u32 {
pub(super) fn opaque_load<const R: usize>(k: &[u32]) -> u32 {
assert!(R < k.len());
let dst;
#[cfg(target_arch = "riscv64")]
unsafe {
core::arch::asm!(
"lwu {dst}, 4*{R}({k})",
R = const R,
k = in(reg) k.as_ptr(),
dst = out(reg) dst,
options(pure, readonly, nostack, preserves_flags),
);
}
#[cfg(target_arch = "riscv32")]
unsafe {
core::arch::asm!(
"lw {dst}, 4*{R}({k})",
Expand Down
27 changes: 22 additions & 5 deletions sha2/src/sha512/riscv_zknh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,35 @@ fn maj(x: u64, y: u64, z: u64) -> u64 {

/// This function returns `k[R]`, but prevents compiler from inlining the indexed value
pub(super) fn opaque_load<const R: usize>(k: &[u64]) -> u64 {
use core::arch::asm;
assert!(R < k.len());
let dst;
#[cfg(target_arch = "riscv64")]
unsafe {
core::arch::asm!(
"ld {dst}, 8*{R}({k})",
R = const R,
let dst;
asm!(
"ld {dst}, {N}({k})",
N = const 8 * R,
k = in(reg) k.as_ptr(),
dst = out(reg) dst,
options(pure, readonly, nostack, preserves_flags),
);
dst
}
#[cfg(target_arch = "riscv32")]
unsafe {
let [hi, lo]: [u32; 2];
asm!(
"lwu {lo}, {N1}({k})",
"lwu {hi}, {N2}({k})",
N1 = const 8 * R,
N2 = const 8 * R + 4,
k = in(reg) k.as_ptr(),
lo = out(reg) lo,
hi = out(reg) hi,
options(pure, readonly, nostack, preserves_flags),
);
((hi as u64) << 32) | (lo as u64)
}
dst
}

fn round<const R: usize>(state: &mut [u64; 8], block: &[u64; 16], k: &[u64]) {
Expand Down

0 comments on commit 87c8b87

Please sign in to comment.