WIP: feat: Initial implementation of ssf. Sort missinf

This commit is contained in:
Alivecow 2024-11-23 19:07:30 +01:00
parent 69a2026c84
commit 17bade8a62
7 changed files with 108 additions and 10 deletions

0
src/utils/dff.rs Normal file
View file

0
src/utils/edf.rs Normal file
View file

View file

@ -14,7 +14,7 @@ use super::{
poly::gfmul,
};
#[derive(Debug, serde::Serialize)]
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct FieldElement {
field_element: Vec<u8>,
}

View file

@ -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;

View file

@ -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<FieldElement>,
}
@ -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<Polynomial>) -> Result<Vec<Polynomial>> {
@ -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);
}

91
src/utils/sff.rs Normal file
View file

@ -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<String>,
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<Factors> = vec![];
for (factor, exponent) in factors {
result.push(Factors {
factor: factor.to_c_array(),
exponent,
});
}
println!("{:?}", result.sort());
let bit_indices: Vec<u8> = vec![0];
assert!(false)
}
}