Skip to content

Commit

Permalink
Move xor tests to submodule & fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mitiko committed Oct 6, 2023
1 parent 897b59b commit 1bcd2ea
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 37 deletions.
37 changes: 1 addition & 36 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,4 @@
use crate::{xor::*, ecb::*, cbc::*};

#[test]
fn hamming_distance_test() {
let start = b"this is a test";
let end = b"wokka wokka!!!";
assert_eq!(hamming_distance(start, end), 37);
}

#[test]
fn xor_test() {
let a = [0x0f, 0xf0, 0x55, 0x13];
let b = [0x0f, 0x0f, 0xaa, 0xa7];
assert_eq!(xor(&a, &b), [0x00, 0xff, 0xff, 0xb4]);
}

#[test]
fn xor_uneven_test() {
let a = [0x0f, 0xf0, 0x55, 0x13, 0xde, 0xad, 0xbe, 0xef];
let b = [0x0f, 0x0f, 0xaa, 0xa7];
assert_eq!(xor(&a, &b), [0x00, 0xff, 0xff, 0xb4]);
}

#[test]
fn xor_rep_test() {
let data = [0x0f, 0xf0, 0x55, 0x13, 0xde, 0xad, 0xbe, 0xef];
let key = [0xbb, 0x71, 0xa2]; // chosen randomly
assert_eq!(xor_rep(&data, &key), [0xb4, 0x81, 0xf7, 0xa8, 0xaf, 0xf, 0x5, 0x9e]);
}

#[test]
fn xor_rep_long_key() {
let data = [0x0f, 0xf0, 0x55];
let key = [0xbb, 0x71, 0xa2, 0x25];
assert_eq!(xor_rep(&data, &key), [0xb4, 0x81, 0xf7]);
}
use crate::{ecb::*, cbc::*};

#[test]
fn aes128_ecb_encrypt_decrypt() {
Expand Down
53 changes: 52 additions & 1 deletion src/xor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,56 @@ pub fn xor_rep(a: &[u8], b: &[u8]) -> Vec<u8> {
}

pub fn hamming_distance(a: &[u8], b: &[u8]) -> u32 {
a.iter().zip(b.iter()).map(|(m, n)| m ^ n).map(|x| x.count_ones()).sum()
a.iter()
.zip(b.iter())
.map(|(m, n)| m ^ n)
.map(|x| x.count_ones())
.sum()
}

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

#[test]
fn hamming_distance_test() {
let start = b"this is a test";
let end = b"wokka wokka!!!";
assert_eq!(hamming_distance(start, end), 37);
}

#[test]
fn xor_test() {
let a = [0x0f, 0xf0, 0x55, 0x13];
let b = [0x0f, 0x0f, 0xaa, 0xa7];
assert_eq!(xor(&a, &b), [0x00, 0xff, 0xff, 0xb4]);
}

#[test]
fn xor_uneven_test() {
let a = [0x0f, 0xf0, 0x55, 0x13, 0xde, 0xad, 0xbe, 0xef];
let b = [0x0f, 0x0f, 0xaa, 0xa7];
assert_eq!(xor(&a, &b), [0x00, 0xff, 0xff, 0xb4]);
// TODO: Do the other direction
}

// TODO: xor_round_trip xor(xor(a, b), b) = a
// TODO: xor_round_trip xor(xor(a, b), a) = b

#[test]
fn xor_rep_test() {
let data = [0x0f, 0xf0, 0x55, 0x13, 0xde, 0xad, 0xbe, 0xef];
let key = [0xbb, 0x71, 0xa2]; // chosen randomly
assert_eq!(
xor_rep(&data, &key),
[0xb4, 0x81, 0xf7, 0xa8, 0xaf, 0xf, 0x5, 0x9e]
);
}

#[test]
fn xor_rep_long_key() {
let data = [0x0f, 0xf0, 0x55];
let key = [0xbb, 0x71, 0xa2, 0x25];
assert_eq!(xor_rep(&data, &key), [0xb4, 0x81, 0xf7]);
}
}

0 comments on commit 1bcd2ea

Please sign in to comment.