Skip to content

Commit

Permalink
updated samples (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheroz committed Aug 19, 2023
1 parent 3479f83 commit 40b820a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 34 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## 0.8.3 (2023-08-19)

- Updated sample for buffer encryption by parallel processing
- Moved unit tests into sample modules
- Updated documentation

## 0.8.2 (2023-08-19)

- Added sample for buffer encryption by parallel processing
Expand Down
6 changes: 6 additions & 0 deletions cipher_magma/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## 0.8.3 (2023-08-19)

- Updated sample for buffer encryption by parallel processing
- Moved unit tests into sample modules
- Updated documentation

## 0.8.2 (2023-08-19)

- Added sample for buffer encryption by parallel processing
Expand Down
9 changes: 9 additions & 0 deletions magma_samples/src/encrypt_bmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ pub fn encrypt_bmp(filename: &str, cipher_mode: CipherMode) {

println!("Completed.");
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encrypt_bmp_test() {
encrypt_bmp("ferris.bmp", cipher_magma::CipherMode::ECB);
}
}
70 changes: 50 additions & 20 deletions magma_samples/src/encrypt_buffer_parallel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/// Sample of buffer encryption by parallel processing of chunks
/// Results (MacBook Pro M1 2021):
///
/// ```text
/// Sample of buffer encryption by parallel processing in chunks
///
/// Results #1
/// MacBook Pro M1 2021
/// MacOS Ventura 13.4, Darwin Kernel Version 22.5.0
/// TLDR: parallel processing is ~8.3 times faster
///
/// Source len: 16850000
/// Encrypting by parallel processing...
/// Encryption, elapsed ticks: 19012211
Expand All @@ -12,7 +15,25 @@
/// Decrypted len: 16850000
/// Decrypted final len: 16850000
/// Completed.
/// ```
///
/// ---
///
/// Results #2
/// Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
/// Linux 6.2.0-26-generic #26~22.04.1-Ubuntu x86_64
/// TLDR: parallel processing is ~ 4.5 times faster
///
/// Source len: 16850000
/// Encrypting by parallel processing...
/// Encryption, elapsed ticks: 8276437589
/// Encrypted len: 16850000
/// Decrypting in single thread...
/// Decryption, elapsed ticks: 37064304591
/// Parallel processing speedup: 4.478292042008655
/// Decrypted len: 16850000
/// Decrypted final len: 16850000
/// Completed.
///
pub fn encrypt_buffer_parallel() {
use cipher_magma::{CipherMode, MagmaStream};
use rayon::prelude::*;
Expand All @@ -38,41 +59,41 @@ pub fn encrypt_buffer_parallel() {

println!("Encrypting by parallel processing...");
let mut encrypted = Vec::<u8>::with_capacity(source.len());
let counter_alignment = if CHUNK_SIZE % 8 == 0 { 0 } else { 1 };
let mutex = Arc::new(Mutex::new(HashMap::<usize, Vec<u8>>::new()));
let counter_size = CHUNK_SIZE / 8 + if CHUNK_SIZE % 8 == 0 { 0 } else { 1 };
let encrypt_start = tick_counter::start();
source
.par_chunks(CHUNK_SIZE)
.enumerate()
.for_each(|(index, chunk)| {
let counter = counter_alignment + (index * CHUNK_SIZE / 8) as u64;
let (ciphertext, _counter) = cipher_magma::ctr::cipher_ctr_core(&magma, chunk, counter);
let counter = index * counter_size;
let (ciphertext, _) = cipher_magma::ctr::cipher_ctr_core(&magma, chunk, counter as u64);
mutex.lock().unwrap().insert(index, ciphertext);
});

// merge encrypted chunks
// merging encrypted chunks
let mut map = mutex.lock().unwrap();
let mut map_keys = map.keys().map(|v| *v).collect::<Vec<_>>();
map_keys.sort();
map_keys
.iter()
.for_each(|index| encrypted.append(map.get_mut(index).unwrap()));

let encrypt_ticks = tick_counter::stop() - encrypt_start;
println!("Encryption, elapsed ticks: {}", encrypt_ticks);
(0..map.len()).for_each(|index| encrypted.append(map.get_mut(&index).unwrap()));

let encrypt_elapsed_ticks = tick_counter::stop() - encrypt_start;
println!("Encryption, elapsed ticks: {}", encrypt_elapsed_ticks);

println!("Encrypted len: {}", encrypted.len());

println!("Decrypting in single thread...");
let mut decrypted = Vec::<u8>::with_capacity(encrypted.len());
let encrypted_chunks = encrypted.chunks(CHUNK_SIZE);

let decrypt_start = tick_counter::start();
for chunk in encrypted_chunks {
// using the generic stream method to make sure of compatibility
let mut plaintext = magma.decrypt(&chunk);
decrypted.append(&mut plaintext);
}
let decrypt_ticks = tick_counter::stop() - decrypt_start;
let speedup = (decrypt_ticks as f64) / (encrypt_ticks as f64);
println!("Decryption, elapsed ticks: {}", decrypt_ticks);

let decrypt_elapsed_ticks = tick_counter::stop() - decrypt_start;
let speedup = (decrypt_elapsed_ticks as f64) / (encrypt_elapsed_ticks as f64);
println!("Decryption, elapsed ticks: {}", decrypt_elapsed_ticks);
println!("Parallel processing speedup: {}", speedup);

println!("Decrypted len: {}", encrypted.len());
Expand All @@ -87,3 +108,12 @@ pub fn encrypt_buffer_parallel() {

println!("Completed.");
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encrypt_buffer_parallel_test() {
encrypt_buffer_parallel();
}
}
9 changes: 9 additions & 0 deletions magma_samples/src/encrypt_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,12 @@ pub fn encrypt_file() {

println!("Completed.");
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encrypt_file_test() {
encrypt_file();
}
}
12 changes: 1 addition & 11 deletions magma_samples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
println!("\n***\n\nEncrypting buffer in chunks\n");
encrypt_buffer::encrypt_buffer();

println!("\n***\n\nEncrypting buffer by parallel processing of chunks\n");
println!("\n***\n\nEncrypting buffer by parallel processing\n");
encrypt_buffer_parallel::encrypt_buffer_parallel();

println!("\n***\n\nMessage Authentication Code (MAC) calculation\n");
Expand All @@ -36,13 +36,3 @@ fn main() {
encrypt_bmp::encrypt_bmp(bmp_filename, cipher_magma::CipherMode::CTR);
encrypt_bmp::encrypt_bmp(bmp_filename, cipher_magma::CipherMode::CFB);
}

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

#[test]
fn main_test() {
main();
}
}
3 changes: 0 additions & 3 deletions todo.txt
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
add tests for buffer ciphering in chunk
sample parallel processing of big file in chunks
bitmap samples in different modes + parallel processing + performance metrics

0 comments on commit 40b820a

Please sign in to comment.