From 64fe182fc3e1917fbb7866bd5ab343802641cd7b Mon Sep 17 00:00:00 2001 From: 0xalivecow Date: Fri, 18 Oct 2024 20:28:30 +0200 Subject: [PATCH] feat: implement first task as file --- Cargo.toml | 1 + src/lib.rs | 1 + src/tasks/mod.rs | 1 + src/tasks/tasks01/block2poly.rs | 7 +++++ src/tasks/tasks01/mod.rs | 2 ++ src/tasks/tasks01/poly2block.rs | 7 +++++ src/utils/mod.rs | 4 +-- src/utils/parse.rs | 12 ++------ src/utils/poly.rs | 54 ++++++++++++++++++++++++++++----- 9 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 src/tasks/tasks01/block2poly.rs create mode 100644 src/tasks/tasks01/mod.rs create mode 100644 src/tasks/tasks01/poly2block.rs diff --git a/Cargo.toml b/Cargo.toml index f7cf89d..1ac9f59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] +base64 = "0.22.1" serde = { version = "1.0.210", features = ["derive"] } serde_json = "1.0" diff --git a/src/lib.rs b/src/lib.rs index 95adf51..ee4ebc7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ +mod tasks; mod utils; diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index e69de29..564d44f 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -0,0 +1 @@ +mod tasks01; diff --git a/src/tasks/tasks01/block2poly.rs b/src/tasks/tasks01/block2poly.rs new file mode 100644 index 0000000..65682a9 --- /dev/null +++ b/src/tasks/tasks01/block2poly.rs @@ -0,0 +1,7 @@ +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(); +} \ No newline at end of file diff --git a/src/tasks/tasks01/mod.rs b/src/tasks/tasks01/mod.rs new file mode 100644 index 0000000..59f15bb --- /dev/null +++ b/src/tasks/tasks01/mod.rs @@ -0,0 +1,2 @@ +mod poly2block; +mod block2poly; \ No newline at end of file diff --git a/src/tasks/tasks01/poly2block.rs b/src/tasks/tasks01/poly2block.rs new file mode 100644 index 0000000..ce9dc09 --- /dev/null +++ b/src/tasks/tasks01/poly2block.rs @@ -0,0 +1,7 @@ +use crate::utils::poly::{self, coefficient_to_binary}; +use base64::prelude::*; +use serde_json::Value; + +pub fn poly2block(coefficients: Vec) -> String { + BASE64_STANDARD.encode(poly::coefficient_to_binary(coefficients).to_ne_bytes()) +} \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 884f6fb..b8568f9 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,2 +1,2 @@ -mod parse; -mod poly; +pub mod parse; +pub mod poly; diff --git a/src/utils/parse.rs b/src/utils/parse.rs index 8149bde..aa3afd5 100644 --- a/src/utils/parse.rs +++ b/src/utils/parse.rs @@ -1,4 +1,5 @@ use serde::{Deserialize, Serialize}; +use serde_json::Value; #[derive(Serialize, Deserialize)] pub struct Testcases { @@ -9,12 +10,5 @@ pub struct Testcases { pub struct Testcase { uuid: String, action: String, - arguments: Vec, -} - -#[derive(Serialize, Deserialize)] -pub struct Argument { - uuid: String, - action: String, - arguments: Vec, -} + arguments: Value, +} \ No newline at end of file diff --git a/src/utils/poly.rs b/src/utils/poly.rs index ec08c36..9d7fa54 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -1,4 +1,5 @@ use std::{fmt::format, str::FromStr, u128, u8}; +use base64::prelude::*; pub fn get_alpha_rep(num: u128) -> String { let mut powers: Vec = vec![]; @@ -25,22 +26,59 @@ pub fn get_alpha_rep(num: u128) -> String { alpha_rep } +pub fn get_bit_indices_from_byte(byte: u8) -> Vec { + let mut coefficients: Vec = vec![]; + + for shift in 0..7 { + if ((byte >> shift) & 1) == 1 { + coefficients.push(shift); + } + } + + coefficients +} + +pub fn coefficient_to_binary(coefficients: Vec) -> u128{ + let mut binary_number: u128 = 0; + for coeff in coefficients { + binary_number = binary_number | (1< = vec![0]; + assert_eq!(get_bit_indices_from_byte(byte), bit_indices) } #[test] - fn test_num_to_alpha_rep_a4a2a() { - let number: u128 = 0x16000000000000000000000000000000; - let polynomial: &str = "a^4a^2a"; - assert_eq!(get_alpha_rep(number.reverse_bits()), polynomial); + fn byte_indices_0x23() { + let byte: u8 = 0x23; + let bit_indices: Vec = vec![0, 1, 5]; + assert_eq!(get_bit_indices_from_byte(byte), bit_indices) + } + + #[test] + fn byte_indices_0x56() { + let byte: u8 = 0x56; + let bit_indices: Vec = vec![1, 2, 4, 6]; + assert_eq!(get_bit_indices_from_byte(byte), bit_indices) + } + + #[test] + fn coeff_to_binary() { + let coefficients: Vec = vec![12, 127, 9, 0]; + let b64: &str = "ARIAAAAAAAAAAAAAAAAAgA=="; + let calculated_num: u128 = coefficient_to_binary(coefficients); + assert_eq!(BASE64_STANDARD.encode(calculated_num.to_ne_bytes()), "ARIAAAAAAAAAAAAAAAAAgA=="); } }