Poly2Block; Block2Poly; SEA128 tasks working #2

Merged
0xalivecow merged 26 commits from dev into main 2024-10-23 15:21:06 +00:00
9 changed files with 70 additions and 19 deletions
Showing only changes of commit 64fe182fc3 - Show all commits

View file

@ -4,5 +4,6 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
base64 = "0.22.1"
serde = { version = "1.0.210", features = ["derive"] } serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"

View file

@ -1 +1,2 @@
mod tasks;
mod utils; mod utils;

View file

@ -0,0 +1 @@
mod tasks01;

View file

@ -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();
}

2
src/tasks/tasks01/mod.rs Normal file
View file

@ -0,0 +1,2 @@
mod poly2block;
mod block2poly;

View file

@ -0,0 +1,7 @@
use crate::utils::poly::{self, coefficient_to_binary};
use base64::prelude::*;
use serde_json::Value;
pub fn poly2block(coefficients: Vec<u8>) -> String {
BASE64_STANDARD.encode(poly::coefficient_to_binary(coefficients).to_ne_bytes())
}

View file

@ -1,2 +1,2 @@
mod parse; pub mod parse;
mod poly; pub mod poly;

View file

@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Testcases { pub struct Testcases {
@ -9,12 +10,5 @@ pub struct Testcases {
pub struct Testcase { pub struct Testcase {
uuid: String, uuid: String,
action: String, action: String,
arguments: Vec<Argument>, arguments: Value,
} }
#[derive(Serialize, Deserialize)]
pub struct Argument {
uuid: String,
action: String,
arguments: Vec<Argument>,
}

View file

@ -1,4 +1,5 @@
use std::{fmt::format, str::FromStr, u128, u8}; use std::{fmt::format, str::FromStr, u128, u8};
use base64::prelude::*;
pub fn get_alpha_rep(num: u128) -> String { pub fn get_alpha_rep(num: u128) -> String {
let mut powers: Vec<u32> = vec![]; let mut powers: Vec<u32> = vec![];
@ -25,22 +26,59 @@ pub fn get_alpha_rep(num: u128) -> String {
alpha_rep alpha_rep
} }
pub fn get_bit_indices_from_byte(byte: u8) -> Vec<u8> {
let mut coefficients: Vec<u8> = vec![];
for shift in 0..7 {
if ((byte >> shift) & 1) == 1 {
coefficients.push(shift);
}
}
coefficients
}
pub fn coefficient_to_binary(coefficients: Vec<u8>) -> u128{
let mut binary_number: u128 = 0;
for coeff in coefficients {
binary_number = binary_number | (1<<coeff);
}
binary_number
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope. // Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*; use super::*;
#[test] #[test]
fn test_num_to_alpha_rep_1() { fn byte_indices_0x01() {
let number: u128 = 0x010000000000000000000000000000000; let byte: u8 = 0x01;
let polynomial: &str = "1"; let bit_indices: Vec<u8> = vec![0];
assert_eq!(get_alpha_rep(number.reverse_bits()), polynomial); assert_eq!(get_bit_indices_from_byte(byte), bit_indices)
} }
#[test] #[test]
fn test_num_to_alpha_rep_a4a2a() { fn byte_indices_0x23() {
let number: u128 = 0x16000000000000000000000000000000; let byte: u8 = 0x23;
let polynomial: &str = "a^4a^2a"; let bit_indices: Vec<u8> = vec![0, 1, 5];
assert_eq!(get_alpha_rep(number.reverse_bits()), polynomial); assert_eq!(get_bit_indices_from_byte(byte), bit_indices)
}
#[test]
fn byte_indices_0x56() {
let byte: u8 = 0x56;
let bit_indices: Vec<u8> = vec![1, 2, 4, 6];
assert_eq!(get_bit_indices_from_byte(byte), bit_indices)
}
#[test]
fn coeff_to_binary() {
let coefficients: Vec<u8> = 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==");
} }
} }