WIP feat: initial gfmul algo. Not working yet.
This commit is contained in:
parent
c416547067
commit
bbae7d6f8b
5 changed files with 79 additions and 7 deletions
|
|
@ -1,6 +1,6 @@
|
|||
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 base64::prelude::*;
|
||||
use serde_json::Value;
|
||||
|
|
@ -9,11 +9,7 @@ pub fn block2poly(val: &Value) -> Result<Vec<u8>> {
|
|||
// Convert JSON data in to a u128
|
||||
// TODO: Transfer decoding into own function?
|
||||
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);
|
||||
let number: u128 = block_2_number(string)?;
|
||||
|
||||
let coefficients: Vec<u8> = get_coefficients(number);
|
||||
|
||||
|
|
|
|||
62
src/tasks/tasks01/gfmul.rs
Normal file
62
src/tasks/tasks01/gfmul.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
pub mod block2poly;
|
||||
pub mod gfmul;
|
||||
pub mod poly2block;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue