diff --git a/libcrux-iot-testutil/src/lib.rs b/libcrux-iot-testutil/src/lib.rs index 16da016..143e40d 100644 --- a/libcrux-iot-testutil/src/lib.rs +++ b/libcrux-iot-testutil/src/lib.rs @@ -29,6 +29,8 @@ mod events; mod logger; +pub mod platform; + pub use events::*; pub use logger::defmt_logger::DefmtInfoLogger; pub use logger::vec_logger::VecLogger; @@ -65,7 +67,7 @@ where E: Display, { /// Returns whether the test configuration is set to skip the test case. - pub fn is_skipped(&self, config: &TestConfig) -> bool { + pub fn is_skipped(&self, config: &TestConfig

) -> bool { let all_tests_should_run = config.only_names.is_empty(); let test_is_selected = config.only_names.contains(&self.name); @@ -97,15 +99,15 @@ where /// If the configuration permits, runs the test case as a benchmark. Logs the /// appropriate messages, and returns the error returned by the test. - pub fn benchmark(&self, logger: &mut L, run_id: u32, state: &S) -> Result { + pub fn benchmark(&self, config: &TestConfig

, logger: &mut L, run_id: u32, state: &S) -> Result { logger.log_event(TestUtilEvent::Benchmark(BenchmarkEvent::Run { name: String::from(self.name), run_id, })); - let cycles_start = cortex_m::peripheral::DWT::cycle_count(); + let cycles_start = config.platform.cycle_count(); let result = core::hint::black_box((self.test_fn)(logger, state)); - let cycles_end = cortex_m::peripheral::DWT::cycle_count(); + let cycles_end = config.platform.cycle_count(); let cycles = cycles_end - cycles_start; match &result { @@ -146,10 +148,10 @@ impl<'a, L: EventLogger, E: Display, S> TestSuite<'a, L, E, S> { /// returns the first error, wrapped in [`ErrorReport::EarlyAbort`]. /// Otherwise it runs all tests and, if errors were encountered, returns the /// vector of all errors wrapped in [`ErrorReport::Combined`]. - pub fn run( + pub fn run( &self, logger: &mut L, - config: &TestConfig<'a>, + config: &TestConfig<'a, P>, state: &S, ) -> Result<(), ErrorReport> where @@ -191,15 +193,16 @@ impl<'a, L: EventLogger, E: Display, S> TestSuite<'a, L, E, S> { /// set, it returns the first error, wrapped in [`ErrorReport::EarlyAbort`]. /// Otherwise it runs all tests and, if errors were encountered, returns the /// vector of all errors wrapped in [`ErrorReport::Combined`]. - pub fn benchmark( + pub fn benchmark( &self, logger: &mut L, - config: &TestConfig<'a>, + config: &TestConfig<'a, P>, state: &S, ) -> Result, ErrorReport> where E: Display + Debug, { + config.platform.init(); logger.log_event(TestUtilEvent::Launch(LaunchEvent { core_freq: config.core_freq, name: self.name.to_string(), @@ -218,7 +221,7 @@ impl<'a, L: EventLogger, E: Display, S> TestSuite<'a, L, E, S> { } for run_id in 0..config.benchmark_runs { - let result = test_case.benchmark(logger, run_id, state); + let result = test_case.benchmark(config,logger, run_id, state); if config.early_abort { cycle_infos.push(( @@ -246,7 +249,8 @@ impl<'a, L: EventLogger, E: Display, S> TestSuite<'a, L, E, S> { #[derive(Debug)] /// The test config contains information on the system and settings that control /// how the tests are run. -pub struct TestConfig<'a> { +pub struct TestConfig<'a, P: platform::Platform> { + pub platform: P, /// The core frequency used when benchmarking, so we can reconstruct the actual time passed /// of running the benchmark. pub core_freq: u32, @@ -283,9 +287,9 @@ impl Display for ErrorReport { } } -impl<'a> TestConfig<'a> { +impl<'a, P: platform::Platform> TestConfig<'a, P> { /// Parses a config string. - pub fn parse_config(core_freq: u32, config: &'a str) -> Result { + pub fn parse_config(platform: P, core_freq: u32, config: &'a str) -> Result { let mut early_abort = false; let mut only_names = vec![]; let mut benchmark_runs = 5; @@ -320,6 +324,7 @@ impl<'a> TestConfig<'a> { } Ok(Self { + platform, core_freq, only_names, early_abort, diff --git a/libcrux-iot-testutil/src/platform.rs b/libcrux-iot-testutil/src/platform.rs new file mode 100644 index 0000000..065743f --- /dev/null +++ b/libcrux-iot-testutil/src/platform.rs @@ -0,0 +1,25 @@ +/// A trait capturing platform specific details +pub trait Platform { + /// Initialize cycle counters and other peripherals. + fn init(&self); + + /// Return a cycle count, assuming the cycle counter has been initialized. + fn cycle_count(&self) -> u32; +} + +/// The Cortex-M specific implementation details. +pub struct CortexM; + +impl Platform for CortexM { + + fn init(&self) { + use cortex_m::peripheral::Peripherals; + let mut peripherals = Peripherals::take().unwrap(); + peripherals.DCB.enable_trace(); + peripherals.DWT.enable_cycle_counter(); + } + + fn cycle_count(&self) -> u32 { + cortex_m::peripheral::DWT::cycle_count() + } +} diff --git a/libcrux-iot-testutil/tests/run_tests.rs b/libcrux-iot-testutil/tests/run_tests.rs index ad030e1..823ed79 100644 --- a/libcrux-iot-testutil/tests/run_tests.rs +++ b/libcrux-iot-testutil/tests/run_tests.rs @@ -41,6 +41,7 @@ fn run_tests_no_early_abort() { let test_suite = TestSuite::new("suite for testing", &cases); let test_config = TestConfig { + platform: platform::CortexM, core_freq: 25_000_000, only_names: vec!["test_pass", "test_log", "test_error"], early_abort: false, diff --git a/libcrux-nrf52810/src/bin/mlkem.rs b/libcrux-nrf52810/src/bin/mlkem.rs index 57a36e4..f230b4e 100644 --- a/libcrux-nrf52810/src/bin/mlkem.rs +++ b/libcrux-nrf52810/src/bin/mlkem.rs @@ -1,7 +1,6 @@ #![no_main] #![no_std] -use cortex_m::peripheral::Peripherals; use libcrux_nrf52810 as board; // global logger + panicking-behavior + memory layout extern crate alloc; @@ -21,14 +20,11 @@ fn main() -> ! { const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) } - - let mut peripherals = Peripherals::take().unwrap(); - peripherals.DCB.enable_trace(); - peripherals.DWT.enable_cycle_counter(); } // set up the test config let test_config = TestConfig { + platform: platform::CortexM, core_freq: board::COREFREQ, only_names: alloc::vec![], early_abort: false, diff --git a/libcrux-nrf52832/src/bin/mldsa.rs b/libcrux-nrf52832/src/bin/mldsa.rs index a2534f6..4658afd 100644 --- a/libcrux-nrf52832/src/bin/mldsa.rs +++ b/libcrux-nrf52832/src/bin/mldsa.rs @@ -1,7 +1,6 @@ #![no_main] #![no_std] -use cortex_m::peripheral::Peripherals; use libcrux_nrf52832 as board; // global logger + panicking-behavior + memory layout extern crate alloc; @@ -22,14 +21,11 @@ fn main() -> ! { const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) } - - let mut peripherals = Peripherals::take().unwrap(); - peripherals.DCB.enable_trace(); - peripherals.DWT.enable_cycle_counter(); } // set up the test config let test_config = TestConfig { + platform: platform::CortexM, core_freq: board::COREFREQ, only_names: alloc::vec![], early_abort: false, diff --git a/libcrux-nrf52832/src/bin/mlkem.rs b/libcrux-nrf52832/src/bin/mlkem.rs index 159bca6..3eeab1d 100644 --- a/libcrux-nrf52832/src/bin/mlkem.rs +++ b/libcrux-nrf52832/src/bin/mlkem.rs @@ -1,7 +1,6 @@ #![no_main] #![no_std] -use cortex_m::peripheral::Peripherals; use libcrux_nrf52832 as board; // global logger + panicking-behavior + memory layout extern crate alloc; @@ -21,14 +20,11 @@ fn main() -> ! { const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) } - - let mut peripherals = Peripherals::take().unwrap(); - peripherals.DCB.enable_trace(); - peripherals.DWT.enable_cycle_counter(); } // set up the test config let test_config = TestConfig { + platform: platform::CortexM, core_freq: board::COREFREQ, only_names: alloc::vec![], early_abort: false, diff --git a/libcrux-nrf52840/src/bin/mldsa.rs b/libcrux-nrf52840/src/bin/mldsa.rs index b9d9633..1fc310d 100644 --- a/libcrux-nrf52840/src/bin/mldsa.rs +++ b/libcrux-nrf52840/src/bin/mldsa.rs @@ -1,7 +1,6 @@ #![no_main] #![no_std] -use cortex_m::peripheral::Peripherals; use libcrux_nrf52840 as board; // global logger + panicking-behavior + memory layout extern crate alloc; @@ -15,21 +14,18 @@ static HEAP: Heap = Heap::empty(); #[cortex_m_rt::entry] fn main() -> ! { use libcrux_iot_testutil::*; - + // Initialize the allocator BEFORE you use it { use core::mem::MaybeUninit; const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) } - - let mut peripherals = Peripherals::take().unwrap(); - peripherals.DCB.enable_trace(); - peripherals.DWT.enable_cycle_counter(); } - + // set up the test config let test_config = TestConfig { + platform: platform::CortexM, core_freq: board::COREFREQ, only_names: alloc::vec![], early_abort: false, diff --git a/libcrux-nrf52840/src/bin/mlkem.rs b/libcrux-nrf52840/src/bin/mlkem.rs index cefd045..4ae7228 100644 --- a/libcrux-nrf52840/src/bin/mlkem.rs +++ b/libcrux-nrf52840/src/bin/mlkem.rs @@ -1,7 +1,6 @@ #![no_main] #![no_std] -use cortex_m::peripheral::Peripherals; use libcrux_nrf52840 as board; // global logger + panicking-behavior + memory layout extern crate alloc; @@ -21,14 +20,11 @@ fn main() -> ! { const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) } - - let mut peripherals = Peripherals::take().unwrap(); - peripherals.DCB.enable_trace(); - peripherals.DWT.enable_cycle_counter(); } // set up the test config let test_config = TestConfig { + platform: platform::CortexM, core_freq: board::COREFREQ, only_names: alloc::vec![], early_abort: false, diff --git a/libcrux-nrf5340/src/bin/mldsa.rs b/libcrux-nrf5340/src/bin/mldsa.rs index a348af4..21be9d1 100644 --- a/libcrux-nrf5340/src/bin/mldsa.rs +++ b/libcrux-nrf5340/src/bin/mldsa.rs @@ -1,7 +1,6 @@ #![no_main] #![no_std] -use cortex_m::peripheral::Peripherals; use libcrux_nrf5340 as board; // global logger + panicking-behavior + memory layout extern crate alloc; @@ -22,14 +21,11 @@ fn main() -> ! { const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) } - - let mut peripherals = Peripherals::take().unwrap(); - peripherals.DCB.enable_trace(); - peripherals.DWT.enable_cycle_counter(); } // set up the test config let test_config = TestConfig { + platform: platform::CortexM, core_freq: board::COREFREQ, only_names: alloc::vec![], early_abort: false, diff --git a/libcrux-nrf5340/src/bin/mlkem.rs b/libcrux-nrf5340/src/bin/mlkem.rs index 2b739c6..3d38c73 100644 --- a/libcrux-nrf5340/src/bin/mlkem.rs +++ b/libcrux-nrf5340/src/bin/mlkem.rs @@ -1,7 +1,7 @@ #![no_main] #![no_std] -use cortex_m::peripheral::Peripherals; + use libcrux_nrf5340 as board; // global logger + panicking-behavior + memory layout extern crate alloc; @@ -21,14 +21,11 @@ fn main() -> ! { const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) } - - let mut peripherals = Peripherals::take().unwrap(); - peripherals.DCB.enable_trace(); - peripherals.DWT.enable_cycle_counter(); } // set up the test config let test_config = TestConfig { + platform: platform::CortexM, core_freq: board::COREFREQ, only_names: alloc::vec![], early_abort: false, diff --git a/libcrux-nucleo-l4r5zi/src/bin/mldsa.rs b/libcrux-nucleo-l4r5zi/src/bin/mldsa.rs index bb32d73..6b3a58b 100644 --- a/libcrux-nucleo-l4r5zi/src/bin/mldsa.rs +++ b/libcrux-nucleo-l4r5zi/src/bin/mldsa.rs @@ -21,13 +21,10 @@ fn main() -> ! { const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) } - - let mut peripherals = Peripherals::take().unwrap(); - peripherals.DCB.enable_trace(); - peripherals.DWT.enable_cycle_counter(); } // set up the test config let test_config = TestConfig { + platform: platform::CortexM, core_freq: board::COREFREQ, only_names: alloc::vec![], early_abort: false, diff --git a/libcrux-nucleo-l4r5zi/src/bin/mlkem.rs b/libcrux-nucleo-l4r5zi/src/bin/mlkem.rs index c9ffe93..f4b8af0 100644 --- a/libcrux-nucleo-l4r5zi/src/bin/mlkem.rs +++ b/libcrux-nucleo-l4r5zi/src/bin/mlkem.rs @@ -21,14 +21,11 @@ fn main() -> ! { const HEAP_SIZE: usize = 1024; static mut HEAP_MEM: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; unsafe { HEAP.init(addr_of_mut!(HEAP_MEM) as usize, HEAP_SIZE) } - - let mut peripherals = Peripherals::take().unwrap(); - peripherals.DCB.enable_trace(); - peripherals.DWT.enable_cycle_counter(); } // set up the test config let test_config = TestConfig { + platform: platform::CortexM, core_freq: board::COREFREQ, only_names: alloc::vec![], early_abort: false, diff --git a/libcrux-testbench/src/mldsa.rs b/libcrux-testbench/src/mldsa.rs index d94bd80..be77154 100644 --- a/libcrux-testbench/src/mldsa.rs +++ b/libcrux-testbench/src/mldsa.rs @@ -58,7 +58,7 @@ fn bench_verify(_l: &mut L, state: &MLDSABenchState) -> Result<( Ok(()) } -pub fn run_benchmarks(test_config: TestConfig) { +pub fn run_benchmarks(test_config: TestConfig

) { // set up the test suite let test_cases = [ TestCase::new("bench_keygen", bench_keygen), diff --git a/libcrux-testbench/src/mlkem.rs b/libcrux-testbench/src/mlkem.rs index e86fb85..2fc23ab 100644 --- a/libcrux-testbench/src/mlkem.rs +++ b/libcrux-testbench/src/mlkem.rs @@ -54,7 +54,7 @@ fn bench_decaps(_l: &mut L, state: &MlKemBenchState) -> Result<( Ok(()) } -pub fn run_benchmarks(test_config: TestConfig) { +pub fn run_benchmarks(test_config: TestConfig

) { // set up the test suite let test_cases = [ TestCase::new("bench_keygen", bench_keygen),