Poly2Block; Block2Poly; SEA128 tasks working #2

Merged
0xalivecow merged 26 commits from dev into main 2024-10-23 15:21:06 +00:00
5 changed files with 69 additions and 28 deletions
Showing only changes of commit 963239903e - Show all commits

View file

@ -5,14 +5,18 @@ use std::{
}; };
use crate::utils::parse::{Responses, Testcase, Testcases}; use crate::utils::parse::{Responses, Testcase, Testcases};
use tasks01::poly2block::{self, poly2block}; use tasks01::{
block2poly::block2poly,
poly2block::{self, poly2block},
};
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::{json, Value}; use serde_json::{json, Value};
mod tasks01; mod tasks01;
pub fn task_deploy(testcase: &Testcase) -> Result<Value, String> { pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
/* /*
* Function to automatially distribute task workloads * Function to automatially distribute task workloads
* TODO: Add functionality to also pass semantics * TODO: Add functionality to also pass semantics
@ -27,10 +31,12 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value, String> {
let json = json!({"block" : result}); let json = json!({"block" : result});
Ok(json) Ok(json)
} }
_ => Err(format!( "block2poly" => {
"Fatal error in task distribution. Data was: {:?}", let result: Vec<u8> = block2poly(args)?;
args let json = json!({"block" : result});
)), Ok(json)
}
_ => Err(anyhow!("Fatal. No compatible action found")),
} }
} }

View file

@ -1,11 +1,17 @@
use std::str::Bytes; use std::{str::Bytes, string};
use crate::utils::poly; use crate::utils::poly;
use anyhow::Result;
use base64::prelude::*; use base64::prelude::*;
use serde_json::Value;
fn block2poly(block: &str) -> Vec<u8> { pub fn block2poly(val: &Value) -> Result<Vec<u8>> {
// Convert JSON data in to a u128 // Convert JSON data in to a u128
let decoded: Vec<u8> = BASE64_STANDARD.decode(block).unwrap(); // TODO: Transfer decoding into own function?
eprintln!("Decoded is: {:?}", val["block"]);
let string: String = serde_json::from_value(val["block"].clone())?;
let decoded: Vec<u8> = BASE64_STANDARD.decode(string)?;
let mut bytes: [u8; 16] = [0u8; 16]; let mut bytes: [u8; 16] = [0u8; 16];
bytes.copy_from_slice(&decoded); bytes.copy_from_slice(&decoded);
let number: u128 = <u128>::from_ne_bytes(bytes); let number: u128 = <u128>::from_ne_bytes(bytes);
@ -14,39 +20,34 @@ fn block2poly(block: &str) -> Vec<u8> {
for shift in 0..128 { for shift in 0..128 {
//println!("{:?}", ((num >> shift) & 1)); //println!("{:?}", ((num >> shift) & 1));
if (((number >> shift) & 1) == 1) { if ((number >> shift) & 1) == 1 {
println!("Shift success"); println!("Shift success");
coefficients.push(shift); coefficients.push(shift);
} }
} }
//Legacy code. Ok(coefficients)
// TODO: Remove
/*
let mut counter: u8 = 0;
let mut coefficients: Vec<u8> = vec![];
for blk in decoded {
let indices: Vec<u8> = poly::get_bit_indices_from_byte(blk);
for index in indices {
coefficients.push(counter*8+index);
}
counter += 1;
}
*/
coefficients
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::json;
use std::str::FromStr; use std::str::FromStr;
// 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 block2poly_task01() { fn block2poly_task01() -> Result<()> {
let block: &str = "ARIAAAAAAAAAAAAAAAAAgA=="; let block: Value = json!({"block" : "ARIAAAAAAAAAAAAAAAAAgA=="});
let coefficients: Vec<u8> = vec![0, 9, 12, 127]; let coefficients: Vec<u8> = vec![0, 9, 12, 127];
assert_eq!(block2poly(block), coefficients); assert_eq!(
block2poly(&block)?,
coefficients,
"Coefficients were: {:?}",
block2poly(&block)?
);
Ok(())
} }
} }

View file

@ -9,5 +9,6 @@ pub fn poly2block(args: &Value) -> String {
.into_iter() .into_iter()
.map(|x| x.as_u64().unwrap() as u8) .map(|x| x.as_u64().unwrap() as u8)
.collect(); .collect();
BASE64_STANDARD.encode(poly::coefficient_to_binary(coefficients).to_ne_bytes()) BASE64_STANDARD.encode(poly::coefficient_to_binary(coefficients).to_ne_bytes())
} }

View file

@ -0,0 +1,23 @@
{
"testcases": {
"b856d760-023d-4b00-bad2-15d2b6da22fe": {
"action": "block2poly",
"arguments": {
"semantic": "xex",
"block": "ARIAAAAAAAAAAAAAAAAAgA=="
}
},
"254eaee7-05fd-4e0d-8292-9b658a852245": {
"action": "poly2block",
"arguments": {
"semantic": "xex",
"coefficients": [
12,
127,
9,
0
]
}
}
}
}

View file

@ -0,0 +1,10 @@
{
"testcases": {
"b856d760-023d-4b00-bad2-15d2b6da22fe": {
"action": "block2poly",
"arguments": {
"semantic": "xex",
"block": "ARIAAAAAAAAAAAAAAAAAgA=="
}
}
}