feat: Add edge case handling for poly diff

Add handling for cases in which poly is of degree 0 or 1
This commit is contained in:
Alivecow 2024-11-23 10:26:32 +01:00
parent 4b1bca8ee0
commit 8be8dc7a54

View file

@ -295,9 +295,17 @@ impl Polynomial {
pub fn diff(mut self) -> Self {
// Pop first element
// Check if the polynomial is 1 or less. In this case, output would be [] without check
// Output should be [0; 16] however
if self.polynomial.len() > 1 {
self.polynomial.remove(0);
} else {
return Polynomial::new(vec![FieldElement::new(vec![0; 16])]);
}
for (position, element) in self.polynomial.iter_mut().enumerate() {
// Set all uneven degrees to 0, as they were the even degrees before
// As we are in GF128, this means they become 0 after mul with even number
if position % 2 == 1 {
*element = FieldElement::new(vec![0; 16]);
}
@ -1206,4 +1214,16 @@ mod tests {
assert_eq!(json!(result.to_c_array()), expected);
}
#[test]
fn test_poly_diff_len1() {
let json1 = json!(["IJustWannaTellYouAAAAA==",]);
let expected = json!(["AAAAAAAAAAAAAAAAAAAAAA==",]);
let element1: Polynomial = Polynomial::from_c_array(&json1);
eprintln!("Starting poly sqrt");
let result = element1.diff();
assert_eq!(json!(result.to_c_array()), expected);
}
}