diff --git a/src/tasks/tasks01/pfmath.rs b/src/tasks/tasks01/pfmath.rs index 71d6991..1df7e96 100644 --- a/src/tasks/tasks01/pfmath.rs +++ b/src/tasks/tasks01/pfmath.rs @@ -143,43 +143,38 @@ mod tests { fn test_poly_sorting_02() { let json1 = json!( {"polys": [ - [ - "AQAAAAAAAAAAAAAAAAAAAA==", // 0x01 - "AgAAAAAAAAAAAAAAAAAAAA==", // 0x02 - "AwAAAAAAAAAAAAAAAAAAAA==" // 0x03 - ], - [ - "AQAAAAAAAAAAAAAAAAAAAA==", // 0x01 - "AgAAAAAAAAAAAAAAAAAAAA==", // 0x02 - "BAAAAAAAAAAAAAAAAAAAAA==" // 0x04 - ], - [ - "AQAAAAAAAAAAAAAAAAAAAA==", // 0x01 - "AgAAAAAAAAAAAAAAAAAAAA==" // 0x02 - ], - [ - "AQAAAAAAAAAAAAAAAAAAAA==", // 0x01 - "AwAAAAAAAAAAAAAAAAAAAA==" // 0x03 - ] - ],}); + [ + "AQAAAAAAAAAAAAAAAAAAAA==", // 0x01 + "AgAAAAAAAAAAAAAAAAAAAA==", // 0x02 + "AwAAAAAAAAAAAAAAAAAAAA==" // 0x03 + ], + [ + "AQAAAAAAAAAAAAAAAAAAAA==", // 0x01 + "AgAAAAAAAAAAAAAAAAAAAA==", // 0x02 + "BAAAAAAAAAAAAAAAAAAAAA==" // 0x04 + ], + [ + "AQAAAAAAAAAAAAAAAAAAAA==", // 0x01 + "AgAAAAAAAAAAAAAAAAAAAA==" // 0x02 + ], + [ + "AQAAAAAAAAAAAAAAAAAAAA==", // 0x01 + "AwAAAAAAAAAAAAAAAAAAAA==" // 0x03 + ] + ],}); let expected = json!([ + ["AQAAAAAAAAAAAAAAAAAAAA==", "AgAAAAAAAAAAAAAAAAAAAA=="], + ["AQAAAAAAAAAAAAAAAAAAAA==", "AwAAAAAAAAAAAAAAAAAAAA=="], [ - "WereNoStrangersToLoveA==", - "YouKnowTheRulesAAAAAAA==", - "AndSoDoIAAAAAAAAAAAAAA==" + "AQAAAAAAAAAAAAAAAAAAAA==", + "AgAAAAAAAAAAAAAAAAAAAA==", + "BAAAAAAAAAAAAAAAAAAAAA==" ], [ - "NeverGonnaMakeYouCryAA==", - "NeverGonnaSayGoodbyeAA==", - "NeverGonnaTellALieAAAA==", - "AndHurtYouAAAAAAAAAAAA==" - ], - [ - "NeverGonnaGiveYouUpAAA==", - "NeverGonnaLetYouDownAA==", - "NeverGonnaRunAroundAAA==", - "AndDesertYouAAAAAAAAAA==" + "AQAAAAAAAAAAAAAAAAAAAA==", + "AgAAAAAAAAAAAAAAAAAAAA==", + "AwAAAAAAAAAAAAAAAAAAAA==" ] ]); diff --git a/src/utils/field.rs b/src/utils/field.rs index 3c7c683..86f545d 100644 --- a/src/utils/field.rs +++ b/src/utils/field.rs @@ -1,5 +1,6 @@ use std::{ cmp::Ordering, + mem::discriminant, ops::{Add, BitXor, Div, Mul, Sub}, }; @@ -9,7 +10,10 @@ use serde_json::Value; use crate::utils::poly::polynomial_2_block; -use super::{math::xor_bytes, poly::gfmul}; +use super::{ + math::{reverse_bits_in_bytevec, xor_bytes}, + poly::gfmul, +}; #[derive(Debug, serde::Serialize)] pub struct Polynomial { @@ -210,6 +214,16 @@ impl Polynomial { } true } + + fn monic(mut self) -> Self { + let divident = self.polynomial.last().unwrap().clone(); + + for fieldelement in &mut self.polynomial.iter_mut() { + *fieldelement = fieldelement.clone() / divident.clone(); + } + + todo!(); + } } impl Clone for Polynomial { @@ -324,19 +338,21 @@ impl PartialEq for Polynomial { impl PartialOrd for Polynomial { fn partial_cmp(&self, other: &Self) -> Option { - match self.polynomial.len().cmp(&other.polynomial.len()) { + match other.polynomial.len().cmp(&self.polynomial.len()) { Ordering::Equal => { - for (field_a, field_b) in - self.as_ref().iter().rev().zip(other.as_ref().iter().rev()) - { - match field_a.cmp(field_b) { - std::cmp::Ordering::Equal => continue, - other => return Some(other.reverse()), + for (field_a, field_b) in self.as_ref().iter().zip(other.as_ref().iter()) { + match field_a + .reverse_bits() + .partial_cmp(&field_b.reverse_bits()) + .unwrap() + { + Ordering::Equal => continue, + other => return Some(other), } } Some(Ordering::Equal) } - other => Some(other), + other => Some(other.reverse()), } } } @@ -345,19 +361,17 @@ impl Eq for Polynomial {} impl Ord for Polynomial { fn cmp(&self, other: &Self) -> Ordering { - match self.polynomial.len().cmp(&other.polynomial.len()) { + match other.polynomial.len().cmp(&self.polynomial.len()) { Ordering::Equal => { - for (field_a, field_b) in - self.as_ref().iter().rev().zip(other.as_ref().iter().rev()) - { - match field_a.cmp(field_b) { - std::cmp::Ordering::Equal => continue, - other => return other.reverse(), + for (field_a, field_b) in self.as_ref().iter().zip(other.as_ref().iter()) { + match field_a.reverse_bits().cmp(&field_b.reverse_bits()) { + Ordering::Equal => continue, + other => return other, } } Ordering::Equal } - other => other, + other => other.reverse(), } } } @@ -432,6 +446,10 @@ impl FieldElement { fn is_zero(&self) -> bool { self.field_element.iter().all(|&x| x == 0x00) } + + fn reverse_bits(&self) -> Self { + FieldElement::new(reverse_bits_in_bytevec(self.field_element.clone())) + } } impl Mul for FieldElement { @@ -525,10 +543,10 @@ impl Div for &FieldElement { impl PartialOrd for FieldElement { fn partial_cmp(&self, other: &Self) -> Option { - for (byte_a, byte_b) in self.as_ref().iter().rev().zip(other.as_ref().iter().rev()) { - match byte_a.reverse_bits().cmp(&byte_b.reverse_bits()) { + for (byte_a, byte_b) in self.as_ref().iter().zip(other.as_ref().iter()) { + match byte_a.partial_cmp(&byte_b).unwrap() { std::cmp::Ordering::Equal => continue, - other => return Some(other), + other => return Some(other.reverse()), } } Some(Ordering::Equal) @@ -548,9 +566,9 @@ impl Eq for FieldElement { impl Ord for FieldElement { fn cmp(&self, other: &Self) -> Ordering { for (byte_a, byte_b) in self.as_ref().iter().zip(other.as_ref().iter()) { - match byte_a.reverse_bits().cmp(&byte_b.reverse_bits()) { + match byte_a.cmp(&byte_b) { std::cmp::Ordering::Equal => continue, - other => return other, + other => return other.reverse(), } } Ordering::Equal