Poly2Block; Block2Poly; SEA128 tasks working #2
3 changed files with 24 additions and 25 deletions
|
|
@ -33,7 +33,7 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
|
||||||
}
|
}
|
||||||
"block2poly" => {
|
"block2poly" => {
|
||||||
let result: Vec<u8> = block2poly(args)?;
|
let result: Vec<u8> = block2poly(args)?;
|
||||||
let json = json!({"block" : result});
|
let json = json!({"coefficients" : result});
|
||||||
Ok(json)
|
Ok(json)
|
||||||
}
|
}
|
||||||
_ => Err(anyhow!("Fatal. No compatible action found")),
|
_ => Err(anyhow!("Fatal. No compatible action found")),
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{str::Bytes, string};
|
use std::{str::Bytes, string};
|
||||||
|
|
||||||
use crate::utils::poly;
|
use crate::utils::poly::{self, get_coefficients};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
@ -16,15 +16,7 @@ pub fn block2poly(val: &Value) -> Result<Vec<u8>> {
|
||||||
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);
|
||||||
|
|
||||||
let mut coefficients: Vec<u8> = vec![];
|
let coefficients: Vec<u8> = get_coefficients(number);
|
||||||
|
|
||||||
for shift in 0..128 {
|
|
||||||
//println!("{:?}", ((num >> shift) & 1));
|
|
||||||
if ((number >> shift) & 1) == 1 {
|
|
||||||
println!("Shift success");
|
|
||||||
coefficients.push(shift);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(coefficients)
|
Ok(coefficients)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,9 @@
|
||||||
use std::{fmt::format, str::FromStr, u128, u8};
|
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
|
use std::{fmt::format, str::FromStr, u128, u8};
|
||||||
|
|
||||||
pub fn get_alpha_rep(num: u128) -> String {
|
pub fn get_alpha_rep(num: u128) -> String {
|
||||||
let mut powers: Vec<u32> = vec![];
|
let powers: Vec<u8> = get_coefficients(num);
|
||||||
|
|
||||||
for shift in 0..128 {
|
|
||||||
//println!("{:?}", ((num >> shift) & 1));
|
|
||||||
if (((num >> shift) & 1) == 1) {
|
|
||||||
println!("Shift success");
|
|
||||||
powers.push(shift);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//println!("{:?}", powers);
|
//println!("{:?}", powers);
|
||||||
|
|
||||||
let mut alpha_rep = String::new();
|
let mut alpha_rep = String::new();
|
||||||
|
|
@ -26,6 +19,18 @@ pub fn get_alpha_rep(num: u128) -> String {
|
||||||
alpha_rep
|
alpha_rep
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_coefficients(num: u128) -> Vec<u8> {
|
||||||
|
let mut powers: Vec<u8> = vec![];
|
||||||
|
for shift in 0..128 {
|
||||||
|
//println!("{:?}", ((num >> shift) & 1));
|
||||||
|
if ((num >> shift) & 1) == 1 {
|
||||||
|
println!("Shift success");
|
||||||
|
powers.push(shift);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
powers
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_bit_indices_from_byte(byte: u8) -> Vec<u8> {
|
pub fn get_bit_indices_from_byte(byte: u8) -> Vec<u8> {
|
||||||
let mut coefficients: Vec<u8> = vec![];
|
let mut coefficients: Vec<u8> = vec![];
|
||||||
|
|
||||||
|
|
@ -47,7 +52,6 @@ pub fn coefficient_to_binary(coefficients: Vec<u8>) -> u128{
|
||||||
binary_number
|
binary_number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||||
|
|
@ -79,6 +83,9 @@ mod tests {
|
||||||
let coefficients: Vec<u8> = vec![12, 127, 9, 0];
|
let coefficients: Vec<u8> = vec![12, 127, 9, 0];
|
||||||
let b64: &str = "ARIAAAAAAAAAAAAAAAAAgA==";
|
let b64: &str = "ARIAAAAAAAAAAAAAAAAAgA==";
|
||||||
let calculated_num: u128 = coefficient_to_binary(coefficients);
|
let calculated_num: u128 = coefficient_to_binary(coefficients);
|
||||||
assert_eq!(BASE64_STANDARD.encode(calculated_num.to_ne_bytes()), "ARIAAAAAAAAAAAAAAAAAgA==");
|
assert_eq!(
|
||||||
|
BASE64_STANDARD.encode(calculated_num.to_ne_bytes()),
|
||||||
|
"ARIAAAAAAAAAAAAAAAAAgA=="
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue