use super::ModulusPolynomialRingZq;
use crate::{
integer::Z, integer_mod_q::PolyOverZq, macros::for_others::implement_trait_reverse,
traits::GetCoefficient,
};
impl PartialEq for ModulusPolynomialRingZq {
fn eq(&self, other: &Self) -> bool {
self.modulus == other.modulus
}
}
impl Eq for ModulusPolynomialRingZq {}
impl PartialEq<PolyOverZq> for ModulusPolynomialRingZq {
fn eq(&self, other: &PolyOverZq) -> bool {
if self.get_q() != other.modulus {
return false;
}
let degree = self.get_degree();
if degree != other.get_degree() {
return false;
}
for i in 0..degree + 1 {
if unsafe { GetCoefficient::<Z>::get_coeff_unchecked(self, i) }
!= unsafe { GetCoefficient::<Z>::get_coeff_unchecked(other, i) }
{
return false;
}
}
true
}
}
implement_trait_reverse!(PartialEq, eq, PolyOverZq, ModulusPolynomialRingZq, bool);
#[cfg(test)]
mod test_partial_eq {
use super::ModulusPolynomialRingZq;
use std::str::FromStr;
const LARGE_PRIME: u64 = u64::MAX - 58;
#[test]
#[allow(clippy::op_ref)]
fn equal_call_methods() {
let one_1 = ModulusPolynomialRingZq::from_str("2 42 1 mod 17").unwrap();
let one_2 = ModulusPolynomialRingZq::from_str("2 42 1 mod 17").unwrap();
assert!(one_1 == one_2);
assert!(&one_1 == &one_2);
assert!(one_1.eq(&one_2));
assert!(ModulusPolynomialRingZq::eq(&one_1, &one_2));
assert_eq!(one_1, one_2);
}
#[test]
#[allow(clippy::op_ref)]
fn not_equal_call_methods_different_num_coeffs() {
let one = ModulusPolynomialRingZq::from_str("2 42 1 mod 17").unwrap();
let two = ModulusPolynomialRingZq::from_str("3 42 -1 1 mod 17").unwrap();
assert!(one != two);
assert!(&one != &two);
assert!(one.ne(&two));
assert!(ModulusPolynomialRingZq::ne(&one, &two));
assert_ne!(one, two);
}
#[test]
fn equal_small() {
let small_1 = ModulusPolynomialRingZq::from_str("2 1 1 mod 17").unwrap();
let small_2 = ModulusPolynomialRingZq::from_str("2 1 1 mod 17").unwrap();
assert!(small_1 == small_2);
assert!(small_2 == small_1);
assert!(small_1 == small_1);
}
#[test]
fn equal_large() {
let max_str = format!("2 {} 1 mod {LARGE_PRIME}", u64::MAX);
let min_str = format!("2 {} 1 mod {LARGE_PRIME}", i64::MIN);
let max_1 = ModulusPolynomialRingZq::from_str(&max_str).unwrap();
let max_2 = ModulusPolynomialRingZq::from_str(&max_str).unwrap();
let min = ModulusPolynomialRingZq::from_str(&min_str).unwrap();
assert!(max_1 == max_2);
assert!(max_2 == max_1);
assert!(max_1 == max_1);
assert!(min == min);
assert!(!(max_1 == min));
assert!(!(min == max_1));
}
#[test]
fn not_equal_large() {
let max_str = format!("2 {} 1 mod {LARGE_PRIME}", u64::MAX);
let min_str = format!("2 {} 1 mod {LARGE_PRIME}", i64::MIN);
let max_1 = ModulusPolynomialRingZq::from_str(&max_str).unwrap();
let max_2 = ModulusPolynomialRingZq::from_str(&max_str).unwrap();
let min = ModulusPolynomialRingZq::from_str(&min_str).unwrap();
assert!(!(max_1 != max_2));
assert!(!(max_2 != max_1));
assert!(!(max_1 != max_1));
assert!(!(min != min));
assert!(max_1 != min);
assert!(min != max_1);
}
#[test]
fn equal_large_small() {
let max_str = format!("2 {} 1 mod {LARGE_PRIME}", u64::MAX);
let min_str = format!("2 {} 1 mod {LARGE_PRIME}", i64::MIN);
let max = ModulusPolynomialRingZq::from_str(&max_str).unwrap();
let min = ModulusPolynomialRingZq::from_str(&min_str).unwrap();
let small_positive = ModulusPolynomialRingZq::from_str("2 1 1 mod 17").unwrap();
let small_negative = ModulusPolynomialRingZq::from_str("2 -1 1 mod 17").unwrap();
assert!(!(max == small_negative));
assert!(!(small_negative == max));
assert!(!(max == small_positive));
assert!(!(small_positive == max));
assert!(!(min == small_negative));
assert!(!(small_negative == min));
assert!(!(min == small_positive));
assert!(!(small_positive == min));
}
#[test]
fn not_equal_large_small() {
let max_str = format!("2 {} 1 mod {LARGE_PRIME}", u64::MAX);
let min_str = format!("2 {} 1 mod {LARGE_PRIME}", i64::MIN);
let max = ModulusPolynomialRingZq::from_str(&max_str).unwrap();
let min = ModulusPolynomialRingZq::from_str(&min_str).unwrap();
let small_positive = ModulusPolynomialRingZq::from_str("2 1 1 mod 17").unwrap();
let small_negative = ModulusPolynomialRingZq::from_str("2 -1 1 mod 17").unwrap();
assert!(max != small_negative);
assert!(small_negative != max);
assert!(max != small_positive);
assert!(small_positive != max);
assert!(min != small_negative);
assert!(small_negative != min);
assert!(min != small_positive);
assert!(small_positive != min);
}
#[test]
fn different_modulus() {
let first_str = "2 1 1 mod 17";
let second_str = "2 1 1 mod 19";
let first = ModulusPolynomialRingZq::from_str(first_str).unwrap();
let second = ModulusPolynomialRingZq::from_str(second_str).unwrap();
assert_ne!(first, second);
}
}
#[cfg(test)]
mod test_partial_eq_q_other {
use crate::integer_mod_q::{ModulusPolynomialRingZq, PolyOverZq};
use std::str::FromStr;
#[test]
#[allow(clippy::op_ref)]
fn availability() {
let q = ModulusPolynomialRingZq::from_str("4 1 2 3 1 mod 17").unwrap();
let z = PolyOverZq::from_str("4 1 2 3 1 mod 17").unwrap();
assert!(q == z);
assert!(z == q);
assert!(&q == &z);
assert!(&z == &q);
}
#[test]
fn equal() {
let q = ModulusPolynomialRingZq::from_str(&format!("3 1 2 1 mod {}", u64::MAX)).unwrap();
let z_1 = PolyOverZq::from_str(&format!("3 1 2 1 mod {}", u64::MAX)).unwrap();
let z_2 = PolyOverZq::from_str(&format!("4 1 2 1 0 mod {}", u64::MAX)).unwrap();
assert!(q == z_1);
assert!(q == z_2);
}
#[test]
fn unequal() {
let q = ModulusPolynomialRingZq::from_str(&format!("3 1 2 1 mod {}", u64::MAX)).unwrap();
let z_1 = PolyOverZq::from_str(&format!("3 1 3 {} mod {}", i64::MAX, u64::MAX)).unwrap();
let z_2 = PolyOverZq::from_str(&format!("4 1 2 {} 1 mod {}", i64::MAX, u64::MAX)).unwrap();
let z_3 =
PolyOverZq::from_str(&format!("3 1 2 {} mod {}", i64::MAX, u64::MAX - 1)).unwrap();
assert!(q != z_1);
assert!(q != z_2);
assert!(q != z_3);
}
}