Dev merge gfmul and XEX tasks #3

Merged
0xalivecow merged 21 commits from dev into main 2024-10-28 17:45:47 +00:00
5 changed files with 79 additions and 7 deletions
Showing only changes of commit bbae7d6f8b - Show all commits

View file

@ -33,6 +33,7 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
} }
"block2poly" => { "block2poly" => {
let result: Vec<u8> = block2poly(args)?; let result: Vec<u8> = block2poly(args)?;
//TODO: Sort Coefficients
let json = json!({"coefficients" : result}); let json = json!({"coefficients" : result});
Ok(json) Ok(json)
} }

View file

@ -1,6 +1,6 @@
use std::{str::Bytes, string}; use std::{str::Bytes, string};
use crate::utils::poly::{self, get_coefficients}; use crate::utils::poly::{self, block_2_number, get_coefficients};
use anyhow::Result; use anyhow::Result;
use base64::prelude::*; use base64::prelude::*;
use serde_json::Value; use serde_json::Value;
@ -9,11 +9,7 @@ pub fn block2poly(val: &Value) -> Result<Vec<u8>> {
// Convert JSON data in to a u128 // Convert JSON data in to a u128
// TODO: Transfer decoding into own function? // TODO: Transfer decoding into own function?
let string: String = serde_json::from_value(val["block"].clone())?; let string: String = serde_json::from_value(val["block"].clone())?;
let decoded: Vec<u8> = BASE64_STANDARD.decode(string)?; let number: u128 = block_2_number(string)?;
let mut bytes: [u8; 16] = [0u8; 16];
bytes.copy_from_slice(&decoded);
let number: u128 = <u128>::from_ne_bytes(bytes);
let coefficients: Vec<u8> = get_coefficients(number); let coefficients: Vec<u8> = get_coefficients(number);

View file

@ -0,0 +1,62 @@
use anyhow::Result;
use base64::prelude::*;
use serde_json::Value;
use crate::utils::poly::{block_2_number, coefficient_to_binary};
pub fn gfmul(args: &Value) -> Result<String> {
eprintln!("{args}");
// Generate reduction polynomial
let reduction_polynomial_coeffs: Vec<u8> = vec![127, 126, 125, 121];
let red_poly_num: u128 = coefficient_to_binary(reduction_polynomial_coeffs);
//eprintln!("{:?}", serde_json::from_value(args["a"].clone())?);
let mut poly1: u128 = block_2_number(serde_json::from_value(args["a"].clone())?)?;
let poly2: u128 = block_2_number(serde_json::from_value(args["b"].clone())?)?;
eprintln!("poly1 is: {}", poly1);
eprintln!("poly2 is: {}", poly2);
/* Begin of magic algorithm
* poly1 = a = X = V ???
* poly2 = b
* result = Z
*/
let mut result: u128 = 0;
for i in 0..128 {
// If poly2 at pos i is 1 then...
if ((poly2 >> i) & 1) == 1 {
result ^= poly1;
}
// If poly1 at pos 127 is 0 then...
if ((poly1 >> 127) & 0) == 0 {
poly1 = poly1 >> 1;
} else {
poly1 = (poly1 >> 1) ^ red_poly_num;
}
}
Ok(BASE64_STANDARD.encode(result.to_ne_bytes()))
}
#[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 gfmul_task01() -> Result<()> {
let args: Value = json!({"a": "ARIAAAAAAAAAAAAAAAAAgA==", "b": "AgAAAAAAAAAAAAAAAAAAAA=="});
let result = gfmul(&args)?;
assert_eq!(
result, "hSQAAAAAAAAAAAAAAAAAAA==",
"Failure. Calulated result was: {}",
result
);
Ok(())
}
}

View file

@ -1,3 +1,3 @@
pub mod block2poly; pub mod block2poly;
pub mod gfmul;
pub mod poly2block; pub mod poly2block;

View file

@ -1,4 +1,6 @@
use anyhow::Result;
use base64::prelude::*; use base64::prelude::*;
use serde_json::Value;
use std::{fmt::format, str::FromStr, u128, u8}; use std::{fmt::format, str::FromStr, u128, u8};
pub fn get_alpha_rep(num: u128) -> String { pub fn get_alpha_rep(num: u128) -> String {
@ -19,6 +21,17 @@ pub fn get_alpha_rep(num: u128) -> String {
alpha_rep alpha_rep
} }
pub fn block_2_number(string: String) -> Result<u128> {
//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);
Ok(number)
}
pub fn get_coefficients(num: u128) -> Vec<u8> { pub fn get_coefficients(num: u128) -> Vec<u8> {
let mut powers: Vec<u8> = vec![]; let mut powers: Vec<u8> = vec![];
for shift in 0..128 { for shift in 0..128 {