feat: add gcm semantic to b2p and p2b

This commit is contained in:
0xalivecow 2024-11-01 21:20:46 +01:00
parent 28a8753d55
commit 8db0bbaa63
No known key found for this signature in database
9 changed files with 299 additions and 206 deletions

View file

@ -1,15 +1,17 @@
use crate::utils::poly::{b64_2_num, get_coefficients};
use crate::utils::poly::{b64_2_num, block_2_polynomial, get_coefficients};
use anyhow::Result;
use base64::prelude::*;
use serde_json::Value;
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 block = BASE64_STANDARD.decode(string)?;
let number = b64_2_num(&string)?;
let semantic: String = serde_json::from_value(val["semantic"].clone())?;
let coefficients: Vec<u8> = get_coefficients(number);
let coefficients: Vec<u8> = block_2_polynomial(block, &semantic)?; //get_coefficients(number);
Ok(coefficients)
}
@ -24,7 +26,7 @@ mod tests {
#[test]
fn block2poly_task01() -> Result<()> {
let block: Value = json!({"block" : "ARIAAAAAAAAAAAAAAAAAgA=="});
let block: Value = json!({"block" : "ARIAAAAAAAAAAAAAAAAAgA==", "semantic" : "xex"});
let coefficients: Vec<u8> = vec![0, 9, 12, 127];
assert_eq!(
block2poly(&block)?,
@ -35,4 +37,18 @@ mod tests {
Ok(())
}
#[test]
fn block2poly_task02() -> Result<()> {
let block: Value = json!({"block" : "ARIAAAAAAAAAAAAAAAAAgA==", "semantic" : "gcm"});
let coefficients: Vec<u8> = vec![7, 11, 14, 120];
assert_eq!(
block2poly(&block)?,
coefficients,
"Coefficients were: {:?}",
block2poly(&block)?
);
Ok(())
}
}

View file

@ -1,5 +1,5 @@
use crate::utils::{
math::ByteArray,
field::ByteArray,
poly::{b64_2_num, coefficient_to_binary, gfmul},
};

View file

@ -1,8 +1,9 @@
use crate::utils::poly::{self};
use crate::utils::poly::{self, polynomial_2_block};
use anyhow::{Ok, Result};
use base64::prelude::*;
use serde_json::Value;
pub fn poly2block(args: &Value) -> String {
pub fn poly2block(args: &Value) -> Result<Vec<u8>> {
let coefficients: Vec<u8> = args["coefficients"]
.as_array()
.unwrap()
@ -10,5 +11,9 @@ pub fn poly2block(args: &Value) -> String {
.map(|x| x.as_u64().unwrap() as u8)
.collect();
BASE64_STANDARD.encode(poly::coefficient_to_binary(coefficients).to_ne_bytes())
let semantic: String = serde_json::from_value(args["semantic"].clone())?;
let result = polynomial_2_block(coefficients, &semantic).unwrap();
Ok(result)
}