diff --git a/src/tasks/tasks01/gfmul.rs b/src/tasks/tasks01/gfmul.rs index 9104114..dac7970 100644 --- a/src/tasks/tasks01/gfmul.rs +++ b/src/tasks/tasks01/gfmul.rs @@ -44,10 +44,13 @@ pub fn gfmul(args: &Value) -> Result { if poly2.LSB_is_one() { result.xor_byte_arrays(&poly1); + poly2.right_shift(); + } else { + poly2.right_shift(); } while !poly2.is_empty() { - if !poly2.LSB_is_one() { + if poly2.LSB_is_one() { poly1.left_shift(); poly1.xor_byte_arrays(&red_poly_bytes); eprintln!("Poly1 after reduction: {:01X?}", poly1); diff --git a/src/utils/math.rs b/src/utils/math.rs index 4392b86..5fff14a 100644 --- a/src/utils/math.rs +++ b/src/utils/math.rs @@ -69,8 +69,8 @@ mod tests { #[test] fn test_byte_array_shift2() { - let mut byte_array: ByteArray = ByteArray(vec![0x00, 0xFF]); - let shifted_array: ByteArray = ByteArray(vec![0x01, 0xFE]); + let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]); + let shifted_array: ByteArray = ByteArray(vec![0xFE, 0x01]); byte_array.left_shift(); assert_eq!( @@ -80,13 +80,29 @@ mod tests { ); } + #[test] + fn test_byte_array_shift_right() { + let mut byte_array: ByteArray = ByteArray(vec![0x02]); + let shifted_array: ByteArray = ByteArray(vec![0x01]); + byte_array.right_shift(); + + assert_eq!( + byte_array.0, shifted_array.0, + "Failure: Shifted array was: {:?}", + byte_array.0 + ); + } + #[test] fn test_lsb_one() { let mut byte_array: ByteArray = ByteArray(vec![0x00, 0xFF]); - assert!(byte_array.LSB_is_one()); + assert!(!byte_array.LSB_is_one()); - let mut byte_array2: ByteArray = ByteArray(vec![0x00, 0x02]); + let mut byte_array2: ByteArray = ByteArray(vec![0x02, 0xFF]); assert!(!byte_array2.LSB_is_one()); + + let mut byte_array3: ByteArray = ByteArray(vec![0xFF, 0x00]); + assert!(byte_array3.LSB_is_one()); } #[test]