Merge pull request #21 from 0xalivecow/dev
Merge poly diff functionality
This commit is contained in:
commit
ab755444c6
5 changed files with 89 additions and 22 deletions
|
|
@ -9,8 +9,8 @@ use tasks01::{
|
||||||
gfmul::gfmul_task,
|
gfmul::gfmul_task,
|
||||||
pad_oracle::padding_oracle,
|
pad_oracle::padding_oracle,
|
||||||
pfmath::{
|
pfmath::{
|
||||||
gfdiv, gfpoly_add, gfpoly_divmod, gfpoly_make_monic, gfpoly_mul, gfpoly_pow, gfpoly_powmod,
|
gfdiv, gfpoly_add, gfpoly_diff, gfpoly_divmod, gfpoly_make_monic, gfpoly_mul, gfpoly_pow,
|
||||||
gfpoly_sort, gfpoly_sqrt,
|
gfpoly_powmod, gfpoly_sort, gfpoly_sqrt,
|
||||||
},
|
},
|
||||||
poly2block::poly2block,
|
poly2block::poly2block,
|
||||||
sea128::sea128,
|
sea128::sea128,
|
||||||
|
|
@ -145,6 +145,12 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
|
||||||
|
|
||||||
Ok(json)
|
Ok(json)
|
||||||
}
|
}
|
||||||
|
"gfpoly_diff" => {
|
||||||
|
let result = gfpoly_diff(args)?;
|
||||||
|
let json = json!({"F'" : result.to_c_array()});
|
||||||
|
|
||||||
|
Ok(json)
|
||||||
|
}
|
||||||
|
|
||||||
_ => Err(anyhow!(
|
_ => Err(anyhow!(
|
||||||
"Fatal. No compatible action found. Json data was {:?}. Arguments were; {:?}",
|
"Fatal. No compatible action found. Json data was {:?}. Arguments were; {:?}",
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,14 @@ pub fn gfpoly_sqrt(args: &Value) -> Result<Polynomial> {
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn gfpoly_diff(args: &Value) -> Result<Polynomial> {
|
||||||
|
let poly_f = Polynomial::from_c_array(&args["F"].clone());
|
||||||
|
|
||||||
|
let result = poly_f.diff();
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,10 @@ impl FieldElement {
|
||||||
87, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 01,
|
87, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 01,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
pub fn zero(self) -> Self {
|
||||||
|
FieldElement::new(vec![0])
|
||||||
|
}
|
||||||
|
|
||||||
pub const fn new(field_element: Vec<u8>) -> Self {
|
pub const fn new(field_element: Vec<u8>) -> Self {
|
||||||
Self { field_element }
|
Self { field_element }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -292,6 +292,27 @@ impl Polynomial {
|
||||||
|
|
||||||
Polynomial::new(result)
|
Polynomial::new(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn diff(mut self) -> Self {
|
||||||
|
// Pop first element
|
||||||
|
// Check if the polynomial is 1 or less. In this case, output would be [] without check
|
||||||
|
// Output should be [0; 16] however
|
||||||
|
if self.polynomial.len() > 1 {
|
||||||
|
self.polynomial.remove(0);
|
||||||
|
} else {
|
||||||
|
return Polynomial::new(vec![FieldElement::new(vec![0; 16])]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (position, element) in self.polynomial.iter_mut().enumerate() {
|
||||||
|
// Set all uneven degrees to 0, as they were the even degrees before
|
||||||
|
// As we are in GF128, this means they become 0 after mul with even number
|
||||||
|
if position % 2 == 1 {
|
||||||
|
*element = FieldElement::new(vec![0; 16]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for Polynomial {
|
impl Clone for Polynomial {
|
||||||
|
|
@ -645,26 +666,6 @@ 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.
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
/*
|
|
||||||
* TODO: Consider removing
|
|
||||||
#[test]
|
|
||||||
fn coefficients_to_byte_arr_xex_test1() {
|
|
||||||
let coefficients: Vec<u8> = vec![0];
|
|
||||||
let byte_array = vec![
|
|
||||||
01, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
|
|
||||||
];
|
|
||||||
assert_eq!(coefficients_to_byte_arr_xex(coefficients), byte_array)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn coefficients_to_byte_arr_xex_test2() {
|
|
||||||
let coefficients: Vec<u8> = vec![127, 12, 9, 0];
|
|
||||||
let byte_array = vec![
|
|
||||||
01, 12, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 80,
|
|
||||||
];
|
|
||||||
assert_eq!(coefficients_to_byte_arr_xex(coefficients), byte_array)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
#[test]
|
#[test]
|
||||||
fn byte_indices_0x01() {
|
fn byte_indices_0x01() {
|
||||||
let byte: u8 = 0x01;
|
let byte: u8 = 0x01;
|
||||||
|
|
@ -1192,4 +1193,37 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(json!(result.to_c_array()), expected);
|
assert_eq!(json!(result.to_c_array()), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_poly_diff() {
|
||||||
|
let json1 = json!([
|
||||||
|
"IJustWannaTellYouAAAAA==",
|
||||||
|
"HowImFeelingAAAAAAAAAA==",
|
||||||
|
"GottaMakeYouAAAAAAAAAA==",
|
||||||
|
"UnderstaaaaaaaaaaaaanQ=="
|
||||||
|
]);
|
||||||
|
let expected = json!([
|
||||||
|
"HowImFeelingAAAAAAAAAA==",
|
||||||
|
"AAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"UnderstaaaaaaaaaaaaanQ=="
|
||||||
|
]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
eprintln!("Starting poly sqrt");
|
||||||
|
|
||||||
|
let result = element1.diff();
|
||||||
|
|
||||||
|
assert_eq!(json!(result.to_c_array()), expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_poly_diff_len1() {
|
||||||
|
let json1 = json!(["IJustWannaTellYouAAAAA==",]);
|
||||||
|
let expected = json!(["AAAAAAAAAAAAAAAAAAAAAA==",]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
eprintln!("Starting poly sqrt");
|
||||||
|
|
||||||
|
let result = element1.diff();
|
||||||
|
|
||||||
|
assert_eq!(json!(result.to_c_array()), expected);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
15
test_json/sandbox.json
Normal file
15
test_json/sandbox.json
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
{
|
||||||
|
"testcases": {
|
||||||
|
"sandbox": {
|
||||||
|
"action": "gfpoly_diff",
|
||||||
|
"arguments": {
|
||||||
|
"F": [
|
||||||
|
"IJustWannaTellYouAAAAA==",
|
||||||
|
"HowImFeelingAAAAAAAAAA==",
|
||||||
|
"GottaMakeYouAAAAAAAAAA==",
|
||||||
|
"UnderstaaaaaaaaaaaaanQ=="
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue