diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index 2cc97b7..e72b502 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -9,8 +9,8 @@ use tasks01::{ gfmul::gfmul_task, pad_oracle::padding_oracle, pfmath::{ - gfdiv, gfpoly_add, gfpoly_divmod, gfpoly_make_monic, gfpoly_mul, gfpoly_pow, gfpoly_powmod, - gfpoly_sort, gfpoly_sqrt, + gfdiv, gfpoly_add, gfpoly_diff, gfpoly_divmod, gfpoly_make_monic, gfpoly_mul, gfpoly_pow, + gfpoly_powmod, gfpoly_sort, gfpoly_sqrt, }, poly2block::poly2block, sea128::sea128, @@ -145,6 +145,12 @@ pub fn task_deploy(testcase: &Testcase) -> Result { Ok(json) } + "gfpoly_diff" => { + let result = gfpoly_diff(args)?; + let json = json!({"F'" : result.to_c_array()}); + + Ok(json) + } _ => Err(anyhow!( "Fatal. No compatible action found. Json data was {:?}. Arguments were; {:?}", diff --git a/src/tasks/tasks01/pfmath.rs b/src/tasks/tasks01/pfmath.rs index b1e7516..3da23fc 100644 --- a/src/tasks/tasks01/pfmath.rs +++ b/src/tasks/tasks01/pfmath.rs @@ -97,6 +97,14 @@ pub fn gfpoly_sqrt(args: &Value) -> Result { Ok(result) } +pub fn gfpoly_diff(args: &Value) -> Result { + let poly_f = Polynomial::from_c_array(&args["F"].clone()); + + let result = poly_f.diff(); + + Ok(result) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/utils/field.rs b/src/utils/field.rs index 28f7bb5..ce0b8a4 100644 --- a/src/utils/field.rs +++ b/src/utils/field.rs @@ -24,6 +24,10 @@ impl FieldElement { 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) -> Self { Self { field_element } } diff --git a/src/utils/poly.rs b/src/utils/poly.rs index f56abf4..8dc0105 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -292,6 +292,19 @@ impl Polynomial { Polynomial::new(result) } + + pub fn diff(mut self) -> Self { + // Pop first element + self.polynomial.remove(0); + + for (position, element) in self.polynomial.iter_mut().enumerate() { + if position % 2 == 1 { + *element = FieldElement::new(vec![0; 16]); + } + } + + self + } } impl Clone for Polynomial { @@ -645,26 +658,6 @@ mod tests { // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; - /* - * TODO: Consider removing - #[test] - fn coefficients_to_byte_arr_xex_test1() { - let coefficients: Vec = 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 = 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] fn byte_indices_0x01() { let byte: u8 = 0x01; @@ -1192,4 +1185,25 @@ mod tests { 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); + } } diff --git a/test_json/sandbox.json b/test_json/sandbox.json new file mode 100644 index 0000000..8a670d3 --- /dev/null +++ b/test_json/sandbox.json @@ -0,0 +1,15 @@ +{ + "testcases": { + "sandbox": { + "action": "gfpoly_diff", + "arguments": { + "F": [ + "IJustWannaTellYouAAAAA==", + "HowImFeelingAAAAAAAAAA==", + "GottaMakeYouAAAAAAAAAA==", + "UnderstaaaaaaaaaaaaanQ==" + ] + } + } + } +}