Skip to content

Commit

Permalink
test(kyberlib): ✅ updating tests cases and naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Nov 21, 2023
1 parent 33f6694 commit d51443a
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 83 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ default-features = false
features = ["getrandom"]
optional = true

benchmarking = ["criterion"]

[build-dependencies]
cc = {version = "1.0.73", optional = true }
nasm-rs = {version = "0.2.4", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ align="right"

# kyberlib

A Robust Rust Library for CRYSTALS-Kyber Post-Quantum Cryptography (CRYSTALS-Kyber is a finalist in the NIST Post-Quantum Cryptography Standardization Process).
A Robust Rust Library for CRYSTALS-Kyber Post-Quantum Cryptography.

<!-- markdownlint-disable MD033 MD041 -->
<center>
Expand Down
2 changes: 1 addition & 1 deletion benches/api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extern crate criterion;
#![cfg(feature = "benchmarking")]
use criterion::{criterion_group, criterion_main, Criterion};
use kyberlib::{*, kem::{generate_key_pair, encrypt_message}};

Expand Down
56 changes: 0 additions & 56 deletions src/loggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,59 +162,3 @@ impl<'a> Default for Log<'a> {
}
}

#[cfg(test)]
mod tests {
use super::*;

// Assuming a maximum log message size, adjust as needed
const MAX_LOG_SIZE: usize = 1024;

struct CustomFile {
data: [u8; MAX_LOG_SIZE],
len: usize,
}

impl fmt::Write for CustomFile {
fn write_str(&mut self, s: &str) -> fmt::Result {
let bytes = s.as_bytes();
let bytes_len = bytes.len();
if self.len + bytes_len > MAX_LOG_SIZE {
return Err(fmt::Error); // Buffer overflow
}

self.data[self.len..self.len + bytes_len].copy_from_slice(bytes);
self.len += bytes_len;

Ok(())
}
}

impl CustomWrite for CustomFile {
fn custom_flush(&mut self) -> CoreResult<(), CustomError> {
Ok(())
}
}

#[test]
fn test_log_info() {
let mut custom_file = CustomFile { data: [0; MAX_LOG_SIZE], len: 0 };
let log_entry = Log::new(
"session123",
"2023-11-20T12:34:56",
LogLevel::INFO,
"component_name",
"This is a log message",
LogFormat::CLF,
);

assert!(log_entry.log(&mut custom_file).is_ok());

// Convert the written bytes to a string slice for checking
let logged_data = core::str::from_utf8(&custom_file.data[..custom_file.len])
.expect("Failed to convert to string");

// Here you can assert the contents of `logged_data`
// For example, checking if it contains certain substrings
assert!(logged_data.contains("This is a log message"));
}
}
38 changes: 19 additions & 19 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ macro_rules! kyberlib_max {
/// Example
///
/// ```rust
/// use kyberlib::loggers::LogFormat;
/// use kyberlib::loggers::LogFormat;
/// use kyberlib::kyberlib_info;
/// use kyberlib::loggers::Log;
/// use kyberlib::loggers::LogLevel;
///
/// let log = kyberlib_info!(
///
/// let log = kyberlib_info!(
/// "session123",
/// "2023-01-04T21:00:00",
/// "app",
/// "Message logged",
/// "Message logged",
/// LogFormat::CLF
/// );
/// ```
/// ```
#[macro_export]
macro_rules! kyberlib_info {
($session_id:expr, $time:expr, $component:expr, $desc:expr, $format:expr) => {
Expand All @@ -96,16 +96,16 @@ macro_rules! kyberlib_info {

/// Shorthand macros to create `Log` instances with different log levels.
///
/// Example
///
/// Example
///
/// ```rust
/// use kyberlib::loggers::LogFormat;
/// use kyberlib::loggers::LogFormat;
/// use kyberlib::kyberlib_error;
/// use kyberlib::loggers::Log;
/// use kyberlib::loggers::LogLevel;
///
///
/// let error_log = kyberlib_error!(
/// "session123",
/// "session123",
/// "2023-01-04T21:00:00",
/// "app",
/// "Connection failed",
Expand Down Expand Up @@ -138,7 +138,7 @@ macro_rules! kyberlib_error {
///
/// let log = kyberlib_debug!(
/// "session123",
/// "2023-01-04T21:00:00",
/// "2023-01-04T21:00:00",
/// "app",
/// "Message logged",
/// LogFormat::CLF
Expand All @@ -160,21 +160,21 @@ macro_rules! kyberlib_debug {

/// Shorthand macro to create a `Log` with the given log level.
///
/// Example
/// Example
///
/// ```rust
/// ```rust
/// use kyberlib::loggers::{LogLevel, LogFormat};
/// use kyberlib::kyberlib_log;
///
/// let log = kyberlib_log!(
/// "session123",
///
/// let log = kyberlib_log!(
/// "session123",
/// "2023-01-04T21:00:00",
/// "app",
/// "Message logged",
/// LogFormat::CLF
/// "Message logged",
/// LogFormat::CLF
/// );
/// ```
#[macro_export]
#[macro_export]
macro_rules! kyberlib_log {
($session_id:expr, $time:expr, $component:expr, $description:expr, $format:expr) => {{
use kyberlib::loggers::{Log, LogFormat, LogLevel};
Expand Down
18 changes: 17 additions & 1 deletion tests/KAT/readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
# Known Answer Tests
<!-- markdownlint-disable MD033 MD041 -->

<img
src="https://kura.pro/kyberlib/images/logos/kyberlib.webp"
alt="kyberlib's logo"
height="261"
width="261"
align="right"
/>

<!-- markdownlint-enable MD033 MD041 -->

# kyberlib

A Robust Rust Library for CRYSTALS-Kyber Post-Quantum Cryptography.

## Known Answer Tests

The test vectors need to be generated locally. Running [build_kats.sh](./build_kats.sh) will automate the process, otherwise follow the instructions below to clone the C reference repo, compile the test binaries, then generate and rename the files.

Expand Down
18 changes: 17 additions & 1 deletion tests/rand_bufs/readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
# Randbuf Generation
<!-- markdownlint-disable MD033 MD041 -->

<img
src="https://kura.pro/kyberlib/images/logos/kyberlib.webp"
alt="kyberlib's logo"
height="261"
width="261"
align="right"
/>

<!-- markdownlint-enable MD033 MD041 -->

# kyberlib

A Robust Rust Library for CRYSTALS-Kyber Post-Quantum Cryptography.

## Randbuf Generation

This program generates the deterministic rng output used in the intermediate stages of keypair generation and encoding from KAT seed values.

Expand Down
26 changes: 22 additions & 4 deletions tests/readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
# Testing
<!-- markdownlint-disable MD033 MD041 -->

<img
src="https://kura.pro/kyberlib/images/logos/kyberlib.webp"
alt="kyberlib's logo"
height="261"
width="261"
align="right"
/>

<!-- markdownlint-enable MD033 MD041 -->

# kyberlib

A Robust Rust Library for CRYSTALS-Kyber Post-Quantum Cryptography.

## Testing

Without any feature flags `cargo test` will run through the key exchange functions and some doctests for the selected security level and mode. Running the Known Answer Tests require deterministic rng buffers from the test vector files. These files are quite large, you will need to generate them yourself. Instructions for building the KAT files are [here](./KAT/readme.md). Otherwise you can run:

Expand All @@ -10,13 +26,15 @@ cd KAT
Which will clone the C reference repo, generate the KAT files, then rename and put them in the correct folder for testing.

To run the known answer tests you will need to enable `KYBER_SECURITY_PARAMETERat` in `RUSTFLAGS`. To check different Kyber levels or 90's mode you will need to include those flags also. eg:

```bash
RUSTFLAGS=' --cfg KYBER_SECURITY_PARAMETERat' cargo test --features "kyber1024 90s"
```

For applicible x86 architectures you must export the avx2 RUSTFLAGS if you don't want to test on the reference codebase.
For applicable x86 architectures you must export the avx2 RUSTFLAGS if you don't want to test on the reference codebase.

To run a matrix of all possible features use the helper script from this folder:

```shell
./run_all_tests.sh
```
Expand All @@ -26,12 +44,12 @@ its behaviour

* KAT: Runs the known answer tests
* AVX2: Runs avx2 code on x86 platforms with compiled GAS files
* NASM: Runs avx2 code with both GAS and NASM files seperately, requires a NASM compiler installed
* NASM: Runs avx2 code with both GAS and NASM files separately, requires a NASM compiler installed

To activate, instantiate the variables, for example:

```shell
KAT=1 AVX2=1 NASM=1 ./run_all_tests.sh
KAT=1 AVX2=1 NASM=1 ./run_all_tests.sh
```

Test files:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
61 changes: 61 additions & 0 deletions tests/test_loggers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright © 2023 kyberlib. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

#[cfg(test)]
mod tests {
use kyberlib::loggers::*;
use core::fmt;
use core::result::Result as CoreResult;

// Assuming a maximum log message size, adjust as needed
const MAX_LOG_SIZE: usize = 1024;

struct CustomFile {
data: [u8; MAX_LOG_SIZE],
len: usize,
}

impl fmt::Write for CustomFile {
fn write_str(&mut self, s: &str) -> fmt::Result {
let bytes = s.as_bytes();
let bytes_len = bytes.len();
if self.len + bytes_len > MAX_LOG_SIZE {
return Err(fmt::Error); // Buffer overflow
}

self.data[self.len..self.len + bytes_len].copy_from_slice(bytes);
self.len += bytes_len;

Ok(())
}
}

impl CustomWrite for CustomFile {
fn custom_flush(&mut self) -> CoreResult<(), CustomError> {
Ok(())
}
}

#[test]
fn test_log_info() {
let mut custom_file = CustomFile { data: [0; MAX_LOG_SIZE], len: 0 };
let log_entry = Log::new(
"session123",
"2023-11-20T12:34:56",
LogLevel::INFO,
"component_name",
"This is a log message",
LogFormat::CLF,
);

assert!(log_entry.log(&mut custom_file).is_ok());

// Convert the written bytes to a string slice for checking
let logged_data = core::str::from_utf8(&custom_file.data[..custom_file.len])
.expect("Failed to convert to string");

// Here you can assert the contents of `logged_data`
// For example, checking if it contains certain substrings
assert!(logged_data.contains("This is a log message"));
}
}

0 comments on commit d51443a

Please sign in to comment.