From ba3975e7fdb58a7b8d6aabebb8d72cc4352c162c Mon Sep 17 00:00:00 2001 From: 0xalivecow Date: Fri, 18 Oct 2024 22:44:46 +0200 Subject: [PATCH] feat: block2poly working fix: for loops boundries --- .vscode/launch.json | 16 +++++++++++ src/tasks/tasks01/block2poly.rs | 51 +++++++++++++++++++++++++++++++-- src/utils/poly.rs | 4 +-- 3 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..10efcb2 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceFolder}/", + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/src/tasks/tasks01/block2poly.rs b/src/tasks/tasks01/block2poly.rs index 65682a9..df0f164 100644 --- a/src/tasks/tasks01/block2poly.rs +++ b/src/tasks/tasks01/block2poly.rs @@ -1,7 +1,52 @@ +use std::str::Bytes; + use crate::utils::poly; use base64::prelude::*; -fn block2poly(block: String) { - let num_block: u128 = BASE64_STANDARD.decode(block).unwrap().into(); - let coefficients = poly::get_bit_indices_from_byte(); +fn block2poly(block: &str) -> Vec { + // Convert JSON data in to a u128 + let decoded: Vec = BASE64_STANDARD.decode(block).unwrap(); + let mut bytes: [u8; 16] = [0u8; 16]; + bytes.copy_from_slice(&decoded); + let number: u128 = ::from_ne_bytes(bytes); + + let mut coefficients: Vec = vec![]; + + for shift in 0..128 { + //println!("{:?}", ((num >> shift) & 1)); + if (((number >> shift) & 1) == 1) { + println!("Shift success"); + coefficients.push(shift); + } + } + + //Legacy code. + // TODO: Remove + /* + let mut counter: u8 = 0; + let mut coefficients: Vec = vec![]; + for blk in decoded { + let indices: Vec = poly::get_bit_indices_from_byte(blk); + for index in indices { + coefficients.push(counter*8+index); + } + counter += 1; + } + */ + coefficients +} + +#[cfg(test)] +mod tests { + use std::str::FromStr; + + // Note this useful idiom: importing names from outer (for mod tests) scope. + use super::*; + + #[test] + fn block2poly_task01() { + let block: &str = "ARIAAAAAAAAAAAAAAAAAgA=="; + let coefficients: Vec = vec![0, 9, 12, 127]; + assert_eq!(block2poly(block), coefficients); + } } \ No newline at end of file diff --git a/src/utils/poly.rs b/src/utils/poly.rs index 9d7fa54..fc668f9 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -4,7 +4,7 @@ use base64::prelude::*; pub fn get_alpha_rep(num: u128) -> String { let mut powers: Vec = vec![]; - for shift in 0..127 { + for shift in 0..128 { //println!("{:?}", ((num >> shift) & 1)); if (((num >> shift) & 1) == 1) { println!("Shift success"); @@ -29,7 +29,7 @@ pub fn get_alpha_rep(num: u128) -> String { pub fn get_bit_indices_from_byte(byte: u8) -> Vec { let mut coefficients: Vec = vec![]; - for shift in 0..7 { + for shift in 0..8 { if ((byte >> shift) & 1) == 1 { coefficients.push(shift); }