From 17bade8a6241163798bbd138e857f03c51197183 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Sat, 23 Nov 2024 19:07:30 +0100 Subject: [PATCH] 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) + } +}