fix: Fix dff algorithm attempt

This commit is contained in:
Alivecow 2024-11-27 10:17:29 +01:00
parent b54753fe7e
commit d599292d3a
3 changed files with 9 additions and 19 deletions

View file

@ -1,6 +1,6 @@
use std::usize; use std::usize;
use num::{pow::Pow, traits::ToBytes, BigUint, FromPrimitive}; use num::{cast::AsPrimitive, pow::Pow, traits::ToBytes, BigUint, FromPrimitive};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::poly::{gcd, Polynomial}; use super::poly::{gcd, Polynomial};
@ -13,25 +13,30 @@ pub struct Factors {
pub fn ddf(f: Polynomial) -> Vec<(Polynomial, u128)> { pub fn ddf(f: Polynomial) -> Vec<(Polynomial, u128)> {
let q = BigUint::pow(&BigUint::from_u8(2).unwrap(), 128); let q = BigUint::pow(&BigUint::from_u8(2).unwrap(), 128);
eprintln!("q: {:?}", q);
let mut z: Vec<(Polynomial, u128)> = vec![]; let mut z: Vec<(Polynomial, u128)> = vec![];
let mut d: u128 = 1; let mut d: u128 = 1;
let mut f_star = f.clone(); let mut f_star = f.clone();
let one_cmp = Polynomial::one(); let one_cmp = Polynomial::one();
while f_star.degree() >= (2 * d) as usize { while f_star.degree() as u128 >= (d) {
let h = Polynomial::x().bpow_mod(q.clone().pow(d), f_star.clone()) + Polynomial::x(); let h = Polynomial::x().bpow_mod(q.clone().pow(d), f_star.clone()) + Polynomial::x();
let g = gcd(&h, &f_star); let g = gcd(&h, &f_star);
if g != one_cmp { if g != one_cmp {
eprintln!("d is: {}", d);
eprintln!("g is: {:?}", &g.clone().to_c_array());
z.push((g.clone(), d)); z.push((g.clone(), d));
f_star = f_star.div(&g).0; f_star = f_star.div(&g).0;
} }
eprintln!("d outer is: {}", d);
eprintln!("F star degree is {:?}", &f_star.degree());
d += 1; d += 1;
} }
if f_star != one_cmp { if f_star != one_cmp {
eprintln!("fstar not one");
z.push((f_star.clone(), f_star.degree() as u128)); z.push((f_star.clone(), f_star.degree() as u128));
} else if z.len() == 0 { } else if z.len() == 0 {
z.push((f.clone(), 1)); z.push((f.clone(), 1));

View file

@ -180,10 +180,7 @@ impl BitXor for FieldElement {
impl Div for FieldElement { impl Div for FieldElement {
type Output = Self; type Output = Self;
fn div(self, rhs: Self) -> Self::Output { fn div(self, rhs: Self) -> Self::Output {
eprintln!("RHS in div{:02X?}", &rhs);
let inverse = rhs.inv(); let inverse = rhs.inv();
eprintln!("Inverse in div{:02X?}", inverse);
self.clone() * inverse self.clone() * inverse
} }
} }
@ -202,15 +199,11 @@ impl Div for &FieldElement {
impl PartialOrd for FieldElement { impl PartialOrd for FieldElement {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
for (byte_a, byte_b) in self.as_ref().iter().rev().zip(other.as_ref().iter().rev()) { for (byte_a, byte_b) in self.as_ref().iter().rev().zip(other.as_ref().iter().rev()) {
eprintln!("Field Partial Ord Bytes: {:02X} {:02X}", byte_a, byte_b);
if byte_a > byte_b { if byte_a > byte_b {
eprintln!("Bytes were greater");
return Some(Ordering::Greater); return Some(Ordering::Greater);
} else if byte_a < byte_b { } else if byte_a < byte_b {
eprintln!("Bytes were less");
return Some(Ordering::Less); return Some(Ordering::Less);
} else { } else {
eprintln!("Bytes were equal");
continue; continue;
} }
} }
@ -231,15 +224,11 @@ impl Eq for FieldElement {
impl Ord for FieldElement { impl Ord for FieldElement {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
for (byte_a, byte_b) in self.as_ref().iter().rev().zip(other.as_ref().iter().rev()) { for (byte_a, byte_b) in self.as_ref().iter().rev().zip(other.as_ref().iter().rev()) {
eprintln!("Field Ord Bytes: {:02X} {:02X}", byte_a, byte_b);
if byte_a > byte_b { if byte_a > byte_b {
eprintln!("Bytes were greater");
return Ordering::Greater; return Ordering::Greater;
} else if byte_a < byte_b { } else if byte_a < byte_b {
eprintln!("Bytes were less");
return Ordering::Less; return Ordering::Less;
} else { } else {
eprintln!("Bytes were equal");
continue; continue;
} }
} }

View file

@ -177,8 +177,6 @@ impl Polynomial {
exponent >>= 1; exponent >>= 1;
} }
eprintln!("result in powmod before reduction: {:02X?}", result);
while !result.polynomial.is_empty() while !result.polynomial.is_empty()
&& result && result
.polynomial .polynomial
@ -191,8 +189,6 @@ impl Polynomial {
result.polynomial.pop(); result.polynomial.pop();
} }
eprintln!("result in powmod after reduction: {:02X?}", result);
if result.is_empty() { if result.is_empty() {
result = Polynomial::new(vec![FieldElement::new(vec![0; 16])]); result = Polynomial::new(vec![FieldElement::new(vec![0; 16])]);
} }
@ -262,7 +258,7 @@ impl Polynomial {
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 // Div by zero check ommitted since data is guaranteed to be non 0
eprintln!("{:?}, {:?}", self.polynomial.len(), rhs.polynomial.len()); //eprintln!("{:?}, {:?}", self.polynomial.len(), rhs.polynomial.len());
if self.polynomial.len() < rhs.polynomial.len() { if self.polynomial.len() < rhs.polynomial.len() {
return ( return (