Skip to content

Commit

Permalink
add functions to allow low-level access from outside the crate
Browse files Browse the repository at this point in the history
and also make the internal functions use the same conventions
  • Loading branch information
bunnie committed Mar 11, 2024
1 parent 4c58a51 commit 80fded7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
20 changes: 16 additions & 4 deletions curve25519-dalek/src/backend/serial/u32e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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]
Expand All @@ -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];

for (src, dst) in rf
Expand All @@ -98,7 +110,7 @@ pub(crate) fn copy_from_rf(register: usize, rf: &[u32], window: usize) -> [u8; 3
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

let mut ret_r: [u8; 32] = [0; 32];
Expand All @@ -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
/// and that the result is always in r31.
pub(crate) fn run_job(
pub fn run_job(
ucode_hw: &mut [u32],
rf_hw: &[u32],
mcode: &[i32],
Expand Down
36 changes: 9 additions & 27 deletions curve25519-dalek/src/montgomery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,9 @@ impl ProjectivePoint {

use crate::backend::serial::u32e::*;
ensure_engine();
let mut ucode_hw: &'static mut [u32] = unsafe {
core::slice::from_raw_parts_mut(ENGINE_MEM.unwrap().as_mut_ptr() as *mut u32, 1024)
};
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,
)
};
// safety: these were called after ensure_engine()
let mut ucode_hw = unsafe { get_ucode() };
let rf_hw = unsafe { get_rf() };

copy_to_rf(self.U.as_bytes(), 29, rf_hw, 0);
copy_to_rf(self.W.as_bytes(), 30, rf_hw, 0);
Expand Down Expand Up @@ -629,15 +623,9 @@ pub(crate) fn differential_add_and_double(
);
use crate::backend::serial::u32e::*;
ensure_engine();
let mut ucode_hw: &'static mut [u32] = unsafe {
core::slice::from_raw_parts_mut(ENGINE_MEM.unwrap().as_mut_ptr() as *mut u32, 1024)
};
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,
)
};
// safety: these were called after ensure_engine()
let mut ucode_hw = unsafe { get_ucode() };
let rf_hw = unsafe { get_rf() };

// P.U in %20
// P.W in %21
Expand Down Expand Up @@ -958,15 +946,9 @@ impl Mul<&Scalar> for &MontgomeryPoint {

let window = 0;
ensure_engine();
let mut ucode_hw: &'static mut [u32] = unsafe {
core::slice::from_raw_parts_mut(ENGINE_MEM.unwrap().as_mut_ptr() as *mut u32, 1024)
};
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,
)
};
// safety: these were called after ensure_engine()
let mut ucode_hw = unsafe { get_ucode() };
let mut rf_hw = unsafe { get_rf() };

copy_to_rf(x0.U.as_bytes(), 25, &mut rf_hw, window);
copy_to_rf(x0.W.as_bytes(), 26, &mut rf_hw, window);
Expand Down

0 comments on commit 80fded7

Please sign in to comment.