diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index 05b3956..65b06d2 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -33,7 +33,7 @@ pub fn task_deploy(testcase: &Testcase) -> Result { } "block2poly" => { let result: Vec = block2poly(args)?; - let json = json!({"block" : result}); + let json = json!({"coefficients" : result}); Ok(json) } _ => Err(anyhow!("Fatal. No compatible action found")), diff --git a/src/tasks/tasks01/block2poly.rs b/src/tasks/tasks01/block2poly.rs index d44fcb4..c9a8bc1 100644 --- a/src/tasks/tasks01/block2poly.rs +++ b/src/tasks/tasks01/block2poly.rs @@ -1,6 +1,6 @@ use std::{str::Bytes, string}; -use crate::utils::poly; +use crate::utils::poly::{self, get_coefficients}; use anyhow::Result; use base64::prelude::*; use serde_json::Value; @@ -16,15 +16,7 @@ pub fn block2poly(val: &Value) -> Result> { 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); - } - } + let coefficients: Vec = get_coefficients(number); Ok(coefficients) } diff --git a/src/utils/poly.rs b/src/utils/poly.rs index fc668f9..2aaba7d 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -1,16 +1,9 @@ -use std::{fmt::format, str::FromStr, u128, u8}; use base64::prelude::*; +use std::{fmt::format, str::FromStr, u128, u8}; pub fn get_alpha_rep(num: u128) -> String { - let mut powers: Vec = vec![]; + let powers: Vec = get_coefficients(num); - for shift in 0..128 { - //println!("{:?}", ((num >> shift) & 1)); - if (((num >> shift) & 1) == 1) { - println!("Shift success"); - powers.push(shift); - } - } //println!("{:?}", powers); let mut alpha_rep = String::new(); @@ -26,9 +19,21 @@ pub fn get_alpha_rep(num: u128) -> String { alpha_rep } +pub fn get_coefficients(num: u128) -> Vec { + let mut powers: Vec = vec![]; + for shift in 0..128 { + //println!("{:?}", ((num >> shift) & 1)); + if ((num >> shift) & 1) == 1 { + println!("Shift success"); + powers.push(shift); + } + } + powers +} + pub fn get_bit_indices_from_byte(byte: u8) -> Vec { let mut coefficients: Vec = vec![]; - + for shift in 0..8 { if ((byte >> shift) & 1) == 1 { coefficients.push(shift); @@ -38,16 +43,15 @@ pub fn get_bit_indices_from_byte(byte: u8) -> Vec { coefficients } -pub fn coefficient_to_binary(coefficients: Vec) -> u128{ +pub fn coefficient_to_binary(coefficients: Vec) -> u128 { let mut binary_number: u128 = 0; for coeff in coefficients { - binary_number = binary_number | (1< = 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=="); + assert_eq!( + BASE64_STANDARD.encode(calculated_num.to_ne_bytes()), + "ARIAAAAAAAAAAAAAAAAAgA==" + ); } }