Add fixes for powmod and sorting of polynomials #17

Merged
0xalivecow merged 2 commits from dev into main 2024-11-21 16:22:15 +00:00
2 changed files with 82 additions and 12 deletions

View file

@ -138,4 +138,58 @@ mod tests {
assert_eq!(json!(result), expected);
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
}
#[test]
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
]
],});
let expected = json!([
[
"WereNoStrangersToLoveA==",
"YouKnowTheRulesAAAAAAA==",
"AndSoDoIAAAAAAAAAAAAAA=="
],
[
"NeverGonnaMakeYouCryAA==",
"NeverGonnaSayGoodbyeAA==",
"NeverGonnaTellALieAAAA==",
"AndHurtYouAAAAAAAAAAAA=="
],
[
"NeverGonnaGiveYouUpAAA==",
"NeverGonnaLetYouDownAA==",
"NeverGonnaRunAroundAAA==",
"AndDesertYouAAAAAAAAAA=="
]
]);
let sorted_array = gfpoly_sort(&json1).unwrap();
let mut result: Vec<Vec<String>> = vec![];
for poly in sorted_array {
result.push(poly.to_c_array());
}
assert_eq!(json!(result), expected);
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
}
}

View file

@ -85,13 +85,12 @@ impl Polynomial {
}
if exponent == 0 {
let inter = Polynomial::new(vec![FieldElement::new(
let result = Polynomial::new(vec![FieldElement::new(
polynomial_2_block(vec![0], "gcm").unwrap(),
)]);
let result = inter.div(&modulus);
eprintln!("Returned value is: {:02X?}", result);
return result.1;
return result;
}
//eprintln!("Initial result: {:?}", result);
@ -324,17 +323,20 @@ impl PartialEq for Polynomial {
}
impl PartialOrd for Polynomial {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
if self.polynomial.len() != other.polynomial.len() {
return Some(self.polynomial.len().cmp(&other.polynomial.len()));
} else {
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()),
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match self.polynomial.len().cmp(&other.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()),
}
}
Some(Ordering::Equal)
}
Some(Ordering::Equal)
other => Some(other),
}
}
}
@ -1172,6 +1174,20 @@ mod tests {
);
}
#[test]
fn test_field_poly_powmod_k0_special() {
let json1 = json!(["NeverGonnaGiveYouUpAAA=="]);
let json2 = json!(["NeverGonnaGiveYouUpAAA=="]);
let element1: Polynomial = Polynomial::from_c_array(&json1);
let modulus: Polynomial = Polynomial::from_c_array(&json2);
let result = element1.pow_mod(0, modulus);
eprintln!("Result is: {:02X?}", result);
assert_eq!(result.to_c_array(), vec!["gAAAAAAAAAAAAAAAAAAAAA=="]);
}
#[test]
fn test_field_poly_powmod_kn_eqdeg() {
let json1 = json!([