feat: add task runner for block2poly

This commit is contained in:
0xalivecow 2024-10-20 20:11:00 +02:00
parent 52dca1bcaf
commit 963239903e
No known key found for this signature in database
5 changed files with 69 additions and 28 deletions

View file

@ -5,14 +5,18 @@ use std::{
};
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_json::{json, Value};
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
* 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});
Ok(json)
}
_ => Err(format!(
"Fatal error in task distribution. Data was: {:?}",
args
)),
"block2poly" => {
let result: Vec<u8> = block2poly(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 anyhow::Result;
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
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];
bytes.copy_from_slice(&decoded);
let number: u128 = <u128>::from_ne_bytes(bytes);
@ -14,39 +20,34 @@ fn block2poly(block: &str) -> Vec<u8> {
for shift in 0..128 {
//println!("{:?}", ((num >> shift) & 1));
if (((number >> shift) & 1) == 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<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
Ok(coefficients)
}
#[cfg(test)]
mod tests {
use serde_json::json;
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==";
fn block2poly_task01() -> Result<()> {
let block: Value = json!({"block" : "ARIAAAAAAAAAAAAAAAAAgA=="});
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()
.map(|x| x.as_u64().unwrap() as u8)
.collect();
BASE64_STANDARD.encode(poly::coefficient_to_binary(coefficients).to_ne_bytes())
}