add functions to allow low-level access from outside the crate

and also make the internal functions use the same conventions
This commit is contained in:
bunnie 2024-03-11 18:44:17 +08:00
parent 4c58a5166f
commit 80fded7589
2 changed files with 25 additions and 31 deletions

View file

@ -72,7 +72,19 @@ pub fn ensure_engine() {
} }
} }
pub(crate) fn copy_to_rf(bytes: [u8; 32], register: usize, rf: &mut [u32], window: usize) { /// Safety: must be called after ensure_engine()
pub unsafe fn get_ucode() -> &'static mut [u32] {
core::slice::from_raw_parts_mut(ENGINE_MEM.unwrap().as_mut_ptr() as *mut u32, 1024)
}
/// Safety: must be called after ensure_engine()
pub unsafe fn get_rf() -> &'static mut [u32] {
core::slice::from_raw_parts_mut(
(ENGINE_MEM.unwrap().as_mut_ptr() as usize + RF_U8_BASE) as *mut u32,
TOTAL_RF_SIZE_IN_U32,
)
}
pub fn copy_to_rf(bytes: [u8; 32], register: usize, rf: &mut [u32], window: usize) {
use core::convert::TryInto; use core::convert::TryInto;
for (byte, rf_dst) in bytes.chunks_exact(4).zip( for (byte, rf_dst) in bytes.chunks_exact(4).zip(
rf[window * RF_SIZE_IN_U32 + register * 8..window * RF_SIZE_IN_U32 + (register + 1) * 8] rf[window * RF_SIZE_IN_U32 + register * 8..window * RF_SIZE_IN_U32 + (register + 1) * 8]
@ -82,7 +94,7 @@ pub(crate) fn copy_to_rf(bytes: [u8; 32], register: usize, rf: &mut [u32], windo
} }
} }
pub(crate) fn copy_from_rf(register: usize, rf: &[u32], window: usize) -> [u8; 32] { pub fn copy_from_rf(register: usize, rf: &[u32], window: usize) -> [u8; 32] {
let mut ret: [u8; 32] = [0; 32]; let mut ret: [u8; 32] = [0; 32];
for (src, dst) in rf for (src, dst) in rf
@ -98,7 +110,7 @@ pub(crate) fn copy_from_rf(register: usize, rf: &[u32], window: usize) -> [u8; 3
ret ret
} }
pub(crate) fn get_single_result(rf_hw: &[u32], window: usize, r: usize) -> [u8; 32] { pub fn get_single_result(rf_hw: &[u32], window: usize, r: usize) -> [u8; 32] {
// TODO: put handlers for illegal opcodes, suspend/resume catch // TODO: put handlers for illegal opcodes, suspend/resume catch
let mut ret_r: [u8; 32] = [0; 32]; let mut ret_r: [u8; 32] = [0; 32];
@ -115,7 +127,7 @@ pub(crate) fn get_single_result(rf_hw: &[u32], window: usize, r: usize) -> [u8;
/// This assumes that arguments have been loaded in appropriate locations for the microcode /// This assumes that arguments have been loaded in appropriate locations for the microcode
/// and that the result is always in r31. /// and that the result is always in r31.
pub(crate) fn run_job( pub fn run_job(
ucode_hw: &mut [u32], ucode_hw: &mut [u32],
rf_hw: &[u32], rf_hw: &[u32],
mcode: &[i32], mcode: &[i32],

View file

@ -466,15 +466,9 @@ impl ProjectivePoint {
use crate::backend::serial::u32e::*; use crate::backend::serial::u32e::*;
ensure_engine(); ensure_engine();
let mut ucode_hw: &'static mut [u32] = unsafe { // safety: these were called after ensure_engine()
core::slice::from_raw_parts_mut(ENGINE_MEM.unwrap().as_mut_ptr() as *mut u32, 1024) let mut ucode_hw = unsafe { get_ucode() };
}; let rf_hw = unsafe { get_rf() };
let rf_hw: &mut [u32] = unsafe {
core::slice::from_raw_parts_mut(
(ENGINE_MEM.unwrap().as_mut_ptr() as usize + RF_U8_BASE) as *mut u32,
TOTAL_RF_SIZE_IN_U32,
)
};
copy_to_rf(self.U.as_bytes(), 29, rf_hw, 0); copy_to_rf(self.U.as_bytes(), 29, rf_hw, 0);
copy_to_rf(self.W.as_bytes(), 30, rf_hw, 0); copy_to_rf(self.W.as_bytes(), 30, rf_hw, 0);
@ -629,15 +623,9 @@ pub(crate) fn differential_add_and_double(
); );
use crate::backend::serial::u32e::*; use crate::backend::serial::u32e::*;
ensure_engine(); ensure_engine();
let mut ucode_hw: &'static mut [u32] = unsafe { // safety: these were called after ensure_engine()
core::slice::from_raw_parts_mut(ENGINE_MEM.unwrap().as_mut_ptr() as *mut u32, 1024) let mut ucode_hw = unsafe { get_ucode() };
}; let rf_hw = unsafe { get_rf() };
let rf_hw: &mut [u32] = unsafe {
core::slice::from_raw_parts_mut(
(ENGINE_MEM.unwrap().as_mut_ptr() as usize + RF_U8_BASE) as *mut u32,
TOTAL_RF_SIZE_IN_U32,
)
};
// P.U in %20 // P.U in %20
// P.W in %21 // P.W in %21
@ -958,15 +946,9 @@ impl Mul<&Scalar> for &MontgomeryPoint {
let window = 0; let window = 0;
ensure_engine(); ensure_engine();
let mut ucode_hw: &'static mut [u32] = unsafe { // safety: these were called after ensure_engine()
core::slice::from_raw_parts_mut(ENGINE_MEM.unwrap().as_mut_ptr() as *mut u32, 1024) let mut ucode_hw = unsafe { get_ucode() };
}; let mut rf_hw = unsafe { get_rf() };
let mut rf_hw: &mut [u32] = unsafe {
core::slice::from_raw_parts_mut(
(ENGINE_MEM.unwrap().as_mut_ptr() as usize + RF_U8_BASE) as *mut u32,
TOTAL_RF_SIZE_IN_U32,
)
};
copy_to_rf(x0.U.as_bytes(), 25, &mut rf_hw, window); copy_to_rf(x0.U.as_bytes(), 25, &mut rf_hw, window);
copy_to_rf(x0.W.as_bytes(), 26, &mut rf_hw, window); copy_to_rf(x0.W.as_bytes(), 26, &mut rf_hw, window);