WIP: feat: gfmul working but LSB check is broken

This commit is contained in:
0xalivecow 2024-10-26 13:04:26 +02:00
parent 1f8e2c75c8
commit 4dc6cdfef8
No known key found for this signature in database
5 changed files with 231 additions and 22 deletions

View file

@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{Ok, Result};
pub fn xor_bytes(vec1: &Vec<u8>, mut vec2: Vec<u8>) -> Result<Vec<u8>> {
for (byte1, byte2) in vec1.iter().zip(vec2.iter_mut()) {
@ -7,3 +7,105 @@ pub fn xor_bytes(vec1: &Vec<u8>, mut vec2: Vec<u8>) -> Result<Vec<u8>> {
Ok(vec2)
}
#[derive(Debug)]
pub struct ByteArray(pub Vec<u8>);
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;
}
carry
}
pub fn right_shift(&mut self) -> u8 {
let mut carry = 0u8;
for byte in self.0.iter_mut().rev() {
let new_carry = *byte & 1;
*byte = (*byte >> 1) | (carry << 7);
carry = new_carry;
}
carry
}
pub fn xor_byte_arrays(&mut self, vec2: &ByteArray) {
self.0
.iter_mut()
.zip(vec2.0.iter())
.for_each(|(x1, x2)| *x1 ^= *x2);
}
pub fn LSB_is_one(&self) -> bool {
(self.0.first().unwrap() & 1) == 1
}
pub fn is_empty(&self) -> bool {
for i in self.0.iter() {
if *i != 0 {
return false;
}
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
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();
assert_eq!(byte_array.0, shifted_array.0);
}
#[test]
fn test_byte_array_shift2() {
let mut byte_array: ByteArray = ByteArray(vec![0x00, 0xFF]);
let shifted_array: ByteArray = ByteArray(vec![0x01, 0xFE]);
byte_array.left_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());
let mut byte_array2: ByteArray = ByteArray(vec![0x00, 0x02]);
assert!(!byte_array2.LSB_is_one());
}
#[test]
fn test_byte_xor() {
let mut byte_array: ByteArray = ByteArray(vec![0x25, 0x25]);
let byte_array2: ByteArray = ByteArray(vec![0x55, 0x55]);
byte_array.xor_byte_arrays(&byte_array2);
assert_eq!(byte_array.0, vec![0x70, 0x70]);
}
#[test]
fn test_byte_xor2() {
let mut byte_array: ByteArray = ByteArray(vec![0x00, 0x00]);
let byte_array2: ByteArray = ByteArray(vec![0x55, 0x55]);
byte_array.xor_byte_arrays(&byte_array2);
assert_eq!(byte_array.0, vec![0x55, 0x55]);
}
}

View file

@ -1,7 +1,7 @@
use anyhow::Result;
use base64::prelude::*;
use std::{str::FromStr, u128, u8};
use std::{str::FromStr, u128, u8, usize};
pub fn get_alpha_rep(num: u128) -> String {
let powers: Vec<u8> = get_coefficients(num);
@ -55,6 +55,16 @@ pub fn get_bit_indices_from_byte(byte: u8) -> Vec<u8> {
coefficients
}
pub fn coefficients_to_byte_arr_xex(coeffs: Vec<u8>) -> Vec<u8> {
let mut byte_array: Vec<u8> = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for coeff in coeffs {
let block_num = coeff / 8;
byte_array[usize::from(block_num)] |= (1 << (coeff % 7));
}
byte_array
}
pub fn coefficient_to_binary(coefficients: Vec<u8>) -> u128 {
let mut binary_number: u128 = 0;
for coeff in coefficients {
@ -71,6 +81,24 @@ mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
#[test]
fn coefficients_to_byte_arr_xex_test1() {
let coefficients: Vec<u8> = vec![0];
let byte_array = vec![
01, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
];
assert_eq!(coefficients_to_byte_arr_xex(coefficients), byte_array)
}
#[test]
fn coefficients_to_byte_arr_xex_test2() {
let coefficients: Vec<u8> = vec![127, 12, 9, 0];
let byte_array = vec![
01, 12, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 80,
];
assert_eq!(coefficients_to_byte_arr_xex(coefficients), byte_array)
}
#[test]
fn byte_indices_0x01() {
let byte: u8 = 0x01;