From 4b1bca8ee023824e45ea17c1bf5f279d110c0551 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Sat, 23 Nov 2024 10:17:08 +0100 Subject: [PATCH 1/2] feat: add function for polynomial differentiation --- src/tasks/mod.rs | 10 +++++-- src/tasks/tasks01/pfmath.rs | 8 ++++++ src/utils/field.rs | 4 +++ src/utils/poly.rs | 54 +++++++++++++++++++++++-------------- test_json/sandbox.json | 15 +++++++++++ 5 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 test_json/sandbox.json 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==" + ] + } + } + } +} -- 2.49.1 From 8be8dc7a5464842dae3cbb90cbb295a0f3f40d66 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Sat, 23 Nov 2024 10:26:32 +0100 Subject: [PATCH 2/2] feat: Add edge case handling for poly diff Add handling for cases in which poly is of degree 0 or 1 --- src/utils/poly.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/utils/poly.rs b/src/utils/poly.rs index 8dc0105..8c5c51a 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -295,9 +295,17 @@ impl Polynomial { pub fn diff(mut self) -> Self { // Pop first element - self.polynomial.remove(0); + // 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]); } @@ -1206,4 +1214,16 @@ mod tests { 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); + } } -- 2.49.1