From ccf0b03ec0a7a040f18a6aacc6f6b59a33485e5c Mon Sep 17 00:00:00 2001 From: 0xalivecow Date: Tue, 29 Oct 2024 14:50:55 +0100 Subject: [PATCH] feat: add more shifting capabilities for gfmul --- src/utils/math.rs | 64 ++++++++++++++++++++++++++++++++++++++--------- src/utils/poly.rs | 10 ++++---- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/utils/math.rs b/src/utils/math.rs index 5fb6a35..f0308ba 100644 --- a/src/utils/math.rs +++ b/src/utils/math.rs @@ -15,14 +15,28 @@ pub fn xor_bytes(vec1: &Vec, mut vec2: Vec) -> Result> { pub struct ByteArray(pub Vec); impl ByteArray { - pub fn left_shift(&mut self) -> u8 { - let mut carry = 0u8; - for byte in self.0.iter_mut() { - let new_carry = *byte >> 7; - *byte = (*byte << 1) | carry; - carry = new_carry; + pub fn left_shift(&mut self, semantic: &str) -> Result { + match semantic { + "xex" => { + let mut carry = 0u8; + for byte in self.0.iter_mut() { + let new_carry = *byte >> 7; + *byte = (*byte << 1) | carry; + carry = new_carry; + } + Ok(carry) + } + "gcm" => { + let mut carry = 0u8; + for byte in self.0.iter_mut() { + let new_carry = *byte & 1; + *byte = (*byte >> 1) | (carry << 7); + carry = new_carry; + } + Ok(carry) + } + _ => Err(anyhow!("Failure in lsh. No compatible action found")), } - carry } pub fn left_shift_reduce(&mut self, semantic: &str) { @@ -97,7 +111,7 @@ mod tests { fn test_byte_array_shift1() { let mut byte_array: ByteArray = ByteArray(vec![0x00, 0x01]); let shifted_array: ByteArray = ByteArray(vec![0x00, 0x02]); - byte_array.left_shift(); + byte_array.left_shift("xex"); assert_eq!(byte_array.0, shifted_array.0); } @@ -106,7 +120,7 @@ mod tests { fn test_byte_array_shift2() { let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]); let shifted_array: ByteArray = ByteArray(vec![0xFE, 0x01]); - byte_array.left_shift(); + byte_array.left_shift("xex"); assert_eq!( byte_array.0, shifted_array.0, @@ -115,6 +129,32 @@ mod tests { ); } + #[test] + fn test_byte_array_shift1_gcm() { + let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]); + let shifted_array: ByteArray = ByteArray(vec![0x7F, 0x80]); + byte_array.left_shift("gcm"); + + assert_eq!( + byte_array.0, shifted_array.0, + "Failure: Shifted array was: {:02X?}", + byte_array.0 + ); + } + + #[test] + fn test_byte_array_shift1_right_gcm() { + let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]); + let shifted_array: ByteArray = ByteArray(vec![0xFE, 0x00]); + byte_array.right_shift("gcm"); + + assert_eq!( + byte_array.0, shifted_array.0, + "Failure: Shifted array was: {:02X?}", + byte_array.0 + ); + } + #[test] fn test_byte_array_shift_right() { let mut byte_array: ByteArray = ByteArray(vec![0x02]); @@ -130,13 +170,13 @@ mod tests { #[test] fn test_lsb_one() { - let mut byte_array: ByteArray = ByteArray(vec![0x00, 0xFF]); + let byte_array: ByteArray = ByteArray(vec![0x00, 0xFF]); assert!(!byte_array.LSB_is_one()); - let mut byte_array2: ByteArray = ByteArray(vec![0x02, 0xFF]); + let byte_array2: ByteArray = ByteArray(vec![0x02, 0xFF]); assert!(!byte_array2.LSB_is_one()); - let mut byte_array3: ByteArray = ByteArray(vec![0xFF, 0x00]); + let byte_array3: ByteArray = ByteArray(vec![0xFF, 0x00]); assert!(byte_array3.LSB_is_one()); } diff --git a/src/utils/poly.rs b/src/utils/poly.rs index 74233ed..5b6fb9a 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -19,21 +19,21 @@ pub fn gfmul(poly_a: Vec, poly_b: Vec, semantic: &str) -> Result if poly2.LSB_is_one() { result.xor_byte_arrays(&poly1); - poly2.right_shift(semantic); + poly2.right_shift(semantic)?; } else { - poly2.right_shift(semantic); + poly2.right_shift(semantic)?; } while !poly2.is_empty() { if poly2.LSB_is_one() { - poly1.left_shift(); + poly1.left_shift(semantic)?; poly1.xor_byte_arrays(&red_poly_bytes); result.xor_byte_arrays(&poly1); } else { - poly1.left_shift(); + poly1.left_shift(semantic)?; poly1.xor_byte_arrays(&red_poly_bytes); } - poly2.right_shift(semantic); + poly2.right_shift(semantic)?; } result.0.remove(16);