Merge fixes and initial monic function #18
2 changed files with 67 additions and 54 deletions
|
|
@ -143,43 +143,38 @@ mod tests {
|
||||||
fn test_poly_sorting_02() {
|
fn test_poly_sorting_02() {
|
||||||
let json1 = json!(
|
let json1 = json!(
|
||||||
{"polys": [
|
{"polys": [
|
||||||
[
|
[
|
||||||
"AQAAAAAAAAAAAAAAAAAAAA==", // 0x01
|
"AQAAAAAAAAAAAAAAAAAAAA==", // 0x01
|
||||||
"AgAAAAAAAAAAAAAAAAAAAA==", // 0x02
|
"AgAAAAAAAAAAAAAAAAAAAA==", // 0x02
|
||||||
"AwAAAAAAAAAAAAAAAAAAAA==" // 0x03
|
"AwAAAAAAAAAAAAAAAAAAAA==" // 0x03
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"AQAAAAAAAAAAAAAAAAAAAA==", // 0x01
|
"AQAAAAAAAAAAAAAAAAAAAA==", // 0x01
|
||||||
"AgAAAAAAAAAAAAAAAAAAAA==", // 0x02
|
"AgAAAAAAAAAAAAAAAAAAAA==", // 0x02
|
||||||
"BAAAAAAAAAAAAAAAAAAAAA==" // 0x04
|
"BAAAAAAAAAAAAAAAAAAAAA==" // 0x04
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"AQAAAAAAAAAAAAAAAAAAAA==", // 0x01
|
"AQAAAAAAAAAAAAAAAAAAAA==", // 0x01
|
||||||
"AgAAAAAAAAAAAAAAAAAAAA==" // 0x02
|
"AgAAAAAAAAAAAAAAAAAAAA==" // 0x02
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"AQAAAAAAAAAAAAAAAAAAAA==", // 0x01
|
"AQAAAAAAAAAAAAAAAAAAAA==", // 0x01
|
||||||
"AwAAAAAAAAAAAAAAAAAAAA==" // 0x03
|
"AwAAAAAAAAAAAAAAAAAAAA==" // 0x03
|
||||||
]
|
]
|
||||||
],});
|
],});
|
||||||
|
|
||||||
let expected = json!([
|
let expected = json!([
|
||||||
|
["AQAAAAAAAAAAAAAAAAAAAA==", "AgAAAAAAAAAAAAAAAAAAAA=="],
|
||||||
|
["AQAAAAAAAAAAAAAAAAAAAA==", "AwAAAAAAAAAAAAAAAAAAAA=="],
|
||||||
[
|
[
|
||||||
"WereNoStrangersToLoveA==",
|
"AQAAAAAAAAAAAAAAAAAAAA==",
|
||||||
"YouKnowTheRulesAAAAAAA==",
|
"AgAAAAAAAAAAAAAAAAAAAA==",
|
||||||
"AndSoDoIAAAAAAAAAAAAAA=="
|
"BAAAAAAAAAAAAAAAAAAAAA=="
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
"NeverGonnaMakeYouCryAA==",
|
"AQAAAAAAAAAAAAAAAAAAAA==",
|
||||||
"NeverGonnaSayGoodbyeAA==",
|
"AgAAAAAAAAAAAAAAAAAAAA==",
|
||||||
"NeverGonnaTellALieAAAA==",
|
"AwAAAAAAAAAAAAAAAAAAAA=="
|
||||||
"AndHurtYouAAAAAAAAAAAA=="
|
|
||||||
],
|
|
||||||
[
|
|
||||||
"NeverGonnaGiveYouUpAAA==",
|
|
||||||
"NeverGonnaLetYouDownAA==",
|
|
||||||
"NeverGonnaRunAroundAAA==",
|
|
||||||
"AndDesertYouAAAAAAAAAA=="
|
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
cmp::Ordering,
|
cmp::Ordering,
|
||||||
|
mem::discriminant,
|
||||||
ops::{Add, BitXor, Div, Mul, Sub},
|
ops::{Add, BitXor, Div, Mul, Sub},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -9,7 +10,10 @@ use serde_json::Value;
|
||||||
|
|
||||||
use crate::utils::poly::polynomial_2_block;
|
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)]
|
#[derive(Debug, serde::Serialize)]
|
||||||
pub struct Polynomial {
|
pub struct Polynomial {
|
||||||
|
|
@ -210,6 +214,16 @@ impl Polynomial {
|
||||||
}
|
}
|
||||||
true
|
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 {
|
impl Clone for Polynomial {
|
||||||
|
|
@ -324,19 +338,21 @@ impl PartialEq for Polynomial {
|
||||||
|
|
||||||
impl PartialOrd for Polynomial {
|
impl PartialOrd for Polynomial {
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
match self.polynomial.len().cmp(&other.polynomial.len()) {
|
match other.polynomial.len().cmp(&self.polynomial.len()) {
|
||||||
Ordering::Equal => {
|
Ordering::Equal => {
|
||||||
for (field_a, field_b) in
|
for (field_a, field_b) in self.as_ref().iter().zip(other.as_ref().iter()) {
|
||||||
self.as_ref().iter().rev().zip(other.as_ref().iter().rev())
|
match field_a
|
||||||
{
|
.reverse_bits()
|
||||||
match field_a.cmp(field_b) {
|
.partial_cmp(&field_b.reverse_bits())
|
||||||
std::cmp::Ordering::Equal => continue,
|
.unwrap()
|
||||||
other => return Some(other.reverse()),
|
{
|
||||||
|
Ordering::Equal => continue,
|
||||||
|
other => return Some(other),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(Ordering::Equal)
|
Some(Ordering::Equal)
|
||||||
}
|
}
|
||||||
other => Some(other),
|
other => Some(other.reverse()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -345,19 +361,17 @@ impl Eq for Polynomial {}
|
||||||
|
|
||||||
impl Ord for Polynomial {
|
impl Ord for Polynomial {
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
match self.polynomial.len().cmp(&other.polynomial.len()) {
|
match other.polynomial.len().cmp(&self.polynomial.len()) {
|
||||||
Ordering::Equal => {
|
Ordering::Equal => {
|
||||||
for (field_a, field_b) in
|
for (field_a, field_b) in self.as_ref().iter().zip(other.as_ref().iter()) {
|
||||||
self.as_ref().iter().rev().zip(other.as_ref().iter().rev())
|
match field_a.reverse_bits().cmp(&field_b.reverse_bits()) {
|
||||||
{
|
Ordering::Equal => continue,
|
||||||
match field_a.cmp(field_b) {
|
other => return other,
|
||||||
std::cmp::Ordering::Equal => continue,
|
|
||||||
other => return other.reverse(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ordering::Equal
|
Ordering::Equal
|
||||||
}
|
}
|
||||||
other => other,
|
other => other.reverse(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -432,6 +446,10 @@ impl FieldElement {
|
||||||
fn is_zero(&self) -> bool {
|
fn is_zero(&self) -> bool {
|
||||||
self.field_element.iter().all(|&x| x == 0x00)
|
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 {
|
impl Mul for FieldElement {
|
||||||
|
|
@ -525,10 +543,10 @@ 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().zip(other.as_ref().iter()) {
|
||||||
match byte_a.reverse_bits().cmp(&byte_b.reverse_bits()) {
|
match byte_a.partial_cmp(&byte_b).unwrap() {
|
||||||
std::cmp::Ordering::Equal => continue,
|
std::cmp::Ordering::Equal => continue,
|
||||||
other => return Some(other),
|
other => return Some(other.reverse()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(Ordering::Equal)
|
Some(Ordering::Equal)
|
||||||
|
|
@ -548,9 +566,9 @@ 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().zip(other.as_ref().iter()) {
|
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,
|
std::cmp::Ordering::Equal => continue,
|
||||||
other => return other,
|
other => return other.reverse(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ordering::Equal
|
Ordering::Equal
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue