From 17bade8a6241163798bbd138e857f03c51197183 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Sat, 23 Nov 2024 19:07:30 +0100 Subject: [PATCH 1/4] WIP: feat: Initial implementation of ssf. Sort missinf --- src/tasks/tasks01/pfmath.rs | 2 +- src/utils/dff.rs | 0 src/utils/edf.rs | 0 src/utils/field.rs | 2 +- src/utils/mod.rs | 3 ++ src/utils/poly.rs | 20 ++++---- src/utils/sff.rs | 91 +++++++++++++++++++++++++++++++++++++ 7 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 src/utils/dff.rs create mode 100644 src/utils/edf.rs create mode 100644 src/utils/sff.rs diff --git a/src/tasks/tasks01/pfmath.rs b/src/tasks/tasks01/pfmath.rs index 4fc686e..182e17d 100644 --- a/src/tasks/tasks01/pfmath.rs +++ b/src/tasks/tasks01/pfmath.rs @@ -112,7 +112,7 @@ pub fn gfpoly_gcd(args: &Value) -> Result { let poly_a = Polynomial::from_c_array(&args["A"].clone()); let poly_b = Polynomial::from_c_array(&args["B"].clone()); - let result = gcd(poly_a.monic(), poly_b.monic()); + let result = gcd(&poly_a.monic(), &poly_b.monic()); Ok(result) } diff --git a/src/utils/dff.rs b/src/utils/dff.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/edf.rs b/src/utils/edf.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/field.rs b/src/utils/field.rs index ce0b8a4..626d40d 100644 --- a/src/utils/field.rs +++ b/src/utils/field.rs @@ -14,7 +14,7 @@ use super::{ poly::gfmul, }; -#[derive(Debug, serde::Serialize)] +#[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct FieldElement { field_element: Vec, } diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 298415b..35fb781 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,6 +1,9 @@ pub mod ciphers; +pub mod dff; +pub mod edf; pub mod field; pub mod math; pub mod net; pub mod parse; pub mod poly; +pub mod sff; diff --git a/src/utils/poly.rs b/src/utils/poly.rs index b820e01..b4a1060 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -12,7 +12,7 @@ use serde_json::Value; use super::field::FieldElement; -#[derive(Debug, serde::Serialize)] +#[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Polynomial { polynomial: Vec, } @@ -178,13 +178,16 @@ impl Polynomial { } // Returns (quotient, remainder) - pub fn div(self, rhs: &Self) -> (Self, Self) { + pub fn div(&self, rhs: &Self) -> (Self, Self) { // Div by zero check ommitted since data is guaranteed to be non 0 eprintln!("{:?}, {:?}", self.polynomial.len(), rhs.polynomial.len()); if self.polynomial.len() < rhs.polynomial.len() { - return (Polynomial::new(vec![FieldElement::new(vec![0; 16])]), self); + return ( + Polynomial::new(vec![FieldElement::new(vec![0; 16])]), + self.clone(), + ); } let mut remainder = self.clone(); @@ -483,12 +486,13 @@ impl Ord for Polynomial { } } -pub fn gcd(a: Polynomial, b: Polynomial) -> Polynomial { +pub fn gcd(a: &Polynomial, b: &Polynomial) -> Polynomial { if a.is_zero() { - return b; + return b.clone(); } - return gcd(b.div(&a).1.monic(), a); + let monic_b = b.div(&a).1.monic(); + return gcd(&monic_b, a); } pub fn sort_polynomial_array(mut polys: Vec) -> Result> { @@ -1300,7 +1304,7 @@ mod tests { let a: Polynomial = Polynomial::from_c_array(&a); let b: Polynomial = Polynomial::from_c_array(&b); - let result = gcd(a.monic(), b.monic()); + let result = gcd(&a.monic(), &b.monic()); assert_eq!(json!(result.to_c_array()), expected); } @@ -1314,7 +1318,7 @@ mod tests { let a: Polynomial = Polynomial::from_c_array(&a); let b: Polynomial = Polynomial::from_c_array(&b); - let result = gcd(a.monic(), b.monic()); + let result = gcd(&a.monic(), &b.monic()); assert_eq!(json!(result.to_c_array()), expected); } diff --git a/src/utils/sff.rs b/src/utils/sff.rs new file mode 100644 index 0000000..fc0705a --- /dev/null +++ b/src/utils/sff.rs @@ -0,0 +1,91 @@ +use serde::{Deserialize, Serialize}; + +use crate::utils::{ + field::FieldElement, + poly::{gcd, polynomial_2_block}, +}; + +use super::poly::Polynomial; + +#[derive(Debug, Serialize, Deserialize)] +struct Factors { + factor: Vec, + exponent: u32, +} + +pub fn sff(mut f: Polynomial) -> Vec<(Polynomial, u32)> { + let mut c = gcd(&f, &f.clone().diff()); + f = f.div(&c).0; + let mut z: Vec<(Polynomial, u32)> = vec![]; + let mut e: u32 = 1; + + let one_element = Polynomial::new(vec![FieldElement::new( + polynomial_2_block(vec![0], "gcm").unwrap(), + )]); + + while f != one_element { + let y = gcd(&f, &c); + if f != y { + z.push(((f.div(&y).0), e)); + } + + f = y.clone(); + c = c.div(&y).0; + e += 1; + } + + if c != one_element { + let r = sff(c.sqrt()); + for (f_star, e_star) in r { + z.push((f_star, 2 * e_star)); + } + } + + z +} + +#[cfg(test)] +mod tests { + + use serde_json::json; + // Note this useful idiom: importing names from outer (for mod tests) scope. + use super::*; + + #[test] + fn byte_indices_0x01() { + let json_f = json!([ + "vL77UwAAAAAAAAAAAAAAAA==", + "mEHchYAAAAAAAAAAAAAAAA==", + "9WJa0MAAAAAAAAAAAAAAAA==", + "akHfwWAAAAAAAAAAAAAAAA==", + "E12o/QAAAAAAAAAAAAAAAA==", + "vKJ/FgAAAAAAAAAAAAAAAA==", + "yctWwAAAAAAAAAAAAAAAAA==", + "c1BXYAAAAAAAAAAAAAAAAA==", + "o0AtAAAAAAAAAAAAAAAAAA==", + "AbP2AAAAAAAAAAAAAAAAAA==", + "k2YAAAAAAAAAAAAAAAAAAA==", + "vBYAAAAAAAAAAAAAAAAAAA==", + "dSAAAAAAAAAAAAAAAAAAAA==", + "69gAAAAAAAAAAAAAAAAAAA==", + "VkAAAAAAAAAAAAAAAAAAAA==", + "a4AAAAAAAAAAAAAAAAAAAA==", + "gAAAAAAAAAAAAAAAAAAAAA==" + ]); + let poly_f = Polynomial::from_c_array(&json_f); + + let factors = sff(poly_f); + let mut result: Vec = vec![]; + + for (factor, exponent) in factors { + result.push(Factors { + factor: factor.to_c_array(), + exponent, + }); + } + + println!("{:?}", result.sort()); + let bit_indices: Vec = vec![0]; + assert!(false) + } +} From 2d4f7a111033c23d6007144f928422d9db186256 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Sat, 23 Nov 2024 19:20:25 +0100 Subject: [PATCH 2/4] feat: sff working in testcase --- src/utils/sff.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/utils/sff.rs b/src/utils/sff.rs index fc0705a..04eb439 100644 --- a/src/utils/sff.rs +++ b/src/utils/sff.rs @@ -1,3 +1,5 @@ +use std::usize; + use serde::{Deserialize, Serialize}; use crate::utils::{ @@ -10,13 +12,13 @@ use super::poly::Polynomial; #[derive(Debug, Serialize, Deserialize)] struct Factors { factor: Vec, - exponent: u32, + exponent: usize, } -pub fn sff(mut f: Polynomial) -> Vec<(Polynomial, u32)> { +pub fn sff(mut f: Polynomial) -> Vec { let mut c = gcd(&f, &f.clone().diff()); f = f.div(&c).0; - let mut z: Vec<(Polynomial, u32)> = vec![]; + let mut z: Vec = vec![]; let mut e: u32 = 1; let one_element = Polynomial::new(vec![FieldElement::new( @@ -26,7 +28,7 @@ pub fn sff(mut f: Polynomial) -> Vec<(Polynomial, u32)> { while f != one_element { let y = gcd(&f, &c); if f != y { - z.push(((f.div(&y).0), e)); + z.push(f.div(&y).0); } f = y.clone(); @@ -36,8 +38,8 @@ pub fn sff(mut f: Polynomial) -> Vec<(Polynomial, u32)> { if c != one_element { let r = sff(c.sqrt()); - for (f_star, e_star) in r { - z.push((f_star, 2 * e_star)); + for f_star in r { + z.push(f_star); } } @@ -74,17 +76,19 @@ mod tests { ]); let poly_f = Polynomial::from_c_array(&json_f); - let factors = sff(poly_f); + let mut factors = sff(poly_f); + factors.sort(); + let mut result: Vec = vec![]; - for (factor, exponent) in factors { + for (exponent, factor) in factors.iter().enumerate() { result.push(Factors { - factor: factor.to_c_array(), - exponent, + factor: factor.clone().to_c_array(), + exponent: exponent + 1, }); } - println!("{:?}", result.sort()); + println!("{:?}", result); let bit_indices: Vec = vec![0]; assert!(false) } From 1c9948ac6210142416ef22800cbe43642efbcc46 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Sun, 24 Nov 2024 14:07:37 +0100 Subject: [PATCH 3/4] fix: Change sff to use the exponent as a tuple again --- src/utils/sff.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/utils/sff.rs b/src/utils/sff.rs index 04eb439..f2478b5 100644 --- a/src/utils/sff.rs +++ b/src/utils/sff.rs @@ -1,5 +1,3 @@ -use std::usize; - use serde::{Deserialize, Serialize}; use crate::utils::{ @@ -12,13 +10,13 @@ use super::poly::Polynomial; #[derive(Debug, Serialize, Deserialize)] struct Factors { factor: Vec, - exponent: usize, + exponent: u32, } -pub fn sff(mut f: Polynomial) -> Vec { +pub fn sff(mut f: Polynomial) -> Vec<(Polynomial, u32)> { let mut c = gcd(&f, &f.clone().diff()); f = f.div(&c).0; - let mut z: Vec = vec![]; + let mut z: Vec<(Polynomial, u32)> = vec![]; let mut e: u32 = 1; let one_element = Polynomial::new(vec![FieldElement::new( @@ -28,7 +26,7 @@ pub fn sff(mut f: Polynomial) -> Vec { while f != one_element { let y = gcd(&f, &c); if f != y { - z.push(f.div(&y).0); + z.push(((f.div(&y).0), e)); } f = y.clone(); @@ -38,8 +36,8 @@ pub fn sff(mut f: Polynomial) -> Vec { if c != one_element { let r = sff(c.sqrt()); - for f_star in r { - z.push(f_star); + for (f_star, e_star) in r { + z.push((f_star, 2 * e_star)); } } @@ -78,13 +76,12 @@ mod tests { let mut factors = sff(poly_f); factors.sort(); - let mut result: Vec = vec![]; - for (exponent, factor) in factors.iter().enumerate() { + for (factor, exponent) in factors { result.push(Factors { - factor: factor.clone().to_c_array(), - exponent: exponent + 1, + factor: factor.to_c_array(), + exponent, }); } From 6856420ff90e66a866d29e5019906f22bf335582 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Mon, 25 Nov 2024 14:19:41 +0100 Subject: [PATCH 4/4] feat: Add task runner for the sff task --- src/tasks/mod.rs | 10 ++++++++-- src/tasks/tasks01/pfmath.rs | 18 ++++++++++++++++++ src/utils/dff.rs | 10 ++++++++++ src/utils/poly.rs | 4 ++++ src/utils/sff.rs | 6 +++--- test_json/sandbox.json | 38 ++++++++++++++++++------------------- 6 files changed, 62 insertions(+), 24 deletions(-) diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index 0613d93..2c3a428 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_diff, gfpoly_divmod, gfpoly_gcd, gfpoly_make_monic, gfpoly_mul, - gfpoly_pow, gfpoly_powmod, gfpoly_sort, gfpoly_sqrt, + gfdiv, gfpoly_add, gfpoly_diff, gfpoly_divmod, gfpoly_factor_sff, gfpoly_gcd, + gfpoly_make_monic, gfpoly_mul, gfpoly_pow, gfpoly_powmod, gfpoly_sort, gfpoly_sqrt, }, poly2block::poly2block, sea128::sea128, @@ -157,6 +157,12 @@ pub fn task_deploy(testcase: &Testcase) -> Result { Ok(json) } + "gfpoly_factor_sff" => { + let result = gfpoly_factor_sff(args)?; + let json = json!({"factors" : result}); + + 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 182e17d..b9067cc 100644 --- a/src/tasks/tasks01/pfmath.rs +++ b/src/tasks/tasks01/pfmath.rs @@ -5,6 +5,7 @@ use serde_json::Value; use crate::utils::{ field::FieldElement, poly::{gcd, Polynomial}, + sff::{sff, Factors}, }; pub fn gfpoly_add(args: &Value) -> Result { @@ -117,6 +118,23 @@ pub fn gfpoly_gcd(args: &Value) -> Result { Ok(result) } +pub fn gfpoly_factor_sff(arsg: &Value) -> Result> { + let poly_f = Polynomial::from_c_array(&arsg["F"].clone()); + + let mut factors = sff(poly_f); + factors.sort(); + let mut result: Vec = vec![]; + + for (factor, exponent) in factors { + result.push(Factors { + factor: factor.to_c_array(), + exponent, + }); + } + + Ok(result) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/utils/dff.rs b/src/utils/dff.rs index e69de29..575d9de 100644 --- a/src/utils/dff.rs +++ b/src/utils/dff.rs @@ -0,0 +1,10 @@ +use super::poly::Polynomial; + +pub fn dff(f: Polynomial) { + let q = 2u128.pow(128); + let z: Vec<(Polynomial, u32)> = vec![]; + let d = 1; + let f_start = f.clone(); + + while f_start.degree() >= 2 * d {} +} diff --git a/src/utils/poly.rs b/src/utils/poly.rs index b4a1060..786c67a 100644 --- a/src/utils/poly.rs +++ b/src/utils/poly.rs @@ -22,6 +22,10 @@ impl Polynomial { Self { polynomial } } + pub fn degree(&self) -> usize { + self.polynomial.len() + } + pub fn from_c_array(array: &Value) -> Self { let mut polynomial: Vec = vec![]; let c_array: Vec = array diff --git a/src/utils/sff.rs b/src/utils/sff.rs index f2478b5..f8f1358 100644 --- a/src/utils/sff.rs +++ b/src/utils/sff.rs @@ -8,9 +8,9 @@ use crate::utils::{ use super::poly::Polynomial; #[derive(Debug, Serialize, Deserialize)] -struct Factors { - factor: Vec, - exponent: u32, +pub struct Factors { + pub factor: Vec, + pub exponent: u32, } pub fn sff(mut f: Polynomial) -> Vec<(Polynomial, u32)> { diff --git a/test_json/sandbox.json b/test_json/sandbox.json index 48d59ed..007d487 100644 --- a/test_json/sandbox.json +++ b/test_json/sandbox.json @@ -1,26 +1,26 @@ { "testcases": { "sandbox": { - "action": "gfpoly_gcd", + "action": "gfpoly_factor_sff", "arguments": { - "A": [ - "DNWpXnnY24XecPa7a8vrEA==", - "I8uYpCbsiPaVvUznuv1IcA==", - "wsbiU432ARWuO93He3vbvA==", - "zp0g3o8iNz7Y+8oUxw1vJw==", - "J0GekE3uendpN6WUAuJ4AA==", - "wACd0e6u1ii4AAAAAAAAAA==", - "ACAAAAAAAAAAAAAAAAAAAA==" - ], - "B": [ - "I20VjJmlSnRSe88gaDiLRQ==", - "0Cw5HxJm/pfybJoQDf7/4w==", - "8ByrMMf+vVj5r3YXUNCJ1g==", - "rEU/f2UZRXqmZ6V7EPKfBA==", - "LfdALhvCrdhhGZWl9l9DSg==", - "KSUKhN0n6/DZmHPozd1prw==", - "DQrRkuA9Zx279wAAAAAAAA==", - "AhCEAAAAAAAAAAAAAAAAAA==" + "F": [ + "vL77UwAAAAAAAAAAAAAAAA==", + "mEHchYAAAAAAAAAAAAAAAA==", + "9WJa0MAAAAAAAAAAAAAAAA==", + "akHfwWAAAAAAAAAAAAAAAA==", + "E12o/QAAAAAAAAAAAAAAAA==", + "vKJ/FgAAAAAAAAAAAAAAAA==", + "yctWwAAAAAAAAAAAAAAAAA==", + "c1BXYAAAAAAAAAAAAAAAAA==", + "o0AtAAAAAAAAAAAAAAAAAA==", + "AbP2AAAAAAAAAAAAAAAAAA==", + "k2YAAAAAAAAAAAAAAAAAAA==", + "vBYAAAAAAAAAAAAAAAAAAA==", + "dSAAAAAAAAAAAAAAAAAAAA==", + "69gAAAAAAAAAAAAAAAAAAA==", + "VkAAAAAAAAAAAAAAAAAAAA==", + "a4AAAAAAAAAAAAAAAAAAAA==", + "gAAAAAAAAAAAAAAAAAAAAA==" ] } }