Merge pull request #17 from 0xalivecow/dev

Add fixes for powmod and sorting of polynomials
This commit is contained in:
An0nymous 2024-11-21 17:22:14 +01:00 committed by GitHub
commit e92c8ddba8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 82 additions and 12 deletions

View file

@ -138,4 +138,58 @@ mod tests {
assert_eq!(json!(result), expected); assert_eq!(json!(result), expected);
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA=="); //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 { if exponent == 0 {
let inter = Polynomial::new(vec![FieldElement::new( let result = Polynomial::new(vec![FieldElement::new(
polynomial_2_block(vec![0], "gcm").unwrap(), polynomial_2_block(vec![0], "gcm").unwrap(),
)]); )]);
let result = inter.div(&modulus);
eprintln!("Returned value is: {:02X?}", result); eprintln!("Returned value is: {:02X?}", result);
return result.1; return result;
} }
//eprintln!("Initial result: {:?}", result); //eprintln!("Initial result: {:?}", result);
@ -324,11 +323,12 @@ impl PartialEq for Polynomial {
} }
impl PartialOrd for Polynomial { impl PartialOrd for Polynomial {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.polynomial.len() != other.polynomial.len() { match self.polynomial.len().cmp(&other.polynomial.len()) {
return Some(self.polynomial.len().cmp(&other.polynomial.len())); Ordering::Equal => {
} else { for (field_a, field_b) in
for (field_a, field_b) in self.as_ref().iter().rev().zip(other.as_ref().iter().rev()) { self.as_ref().iter().rev().zip(other.as_ref().iter().rev())
{
match field_a.cmp(field_b) { match field_a.cmp(field_b) {
std::cmp::Ordering::Equal => continue, std::cmp::Ordering::Equal => continue,
other => return Some(other.reverse()), other => return Some(other.reverse()),
@ -336,6 +336,8 @@ impl PartialOrd for Polynomial {
} }
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] #[test]
fn test_field_poly_powmod_kn_eqdeg() { fn test_field_poly_powmod_kn_eqdeg() {
let json1 = json!([ let json1 = json!([