qfall_math/integer_mod_q/polynomial_ring_zq/
cmp.rs

1// Copyright © 2025 Marvin Beckmann
2//
3// This file is part of qFALL-math.
4//
5// qFALL-math is free software: you can redistribute it and/or modify it under
6// the terms of the Mozilla Public License Version 2.0 as published by the
7// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
8
9//! This module contains implementations for comparison of [`PolynomialRingZq`].
10
11use super::PolynomialRingZq;
12use crate::{
13    error::MathError,
14    integer::{PolyOverZ, Z},
15    integer_mod_q::{PolyOverZq, Zq},
16    macros::compare_base::{
17        compare_base_default, compare_base_get_mod, compare_base_get_mod_get_q, compare_base_impl,
18    },
19    traits::CompareBase,
20};
21
22compare_base_default!(PolynomialRingZq for PolyOverZ);
23compare_base_get_mod!(PolynomialRingZq for PolynomialRingZq);
24compare_base_get_mod_get_q!(PolynomialRingZq for Zq PolyOverZq);
25impl<Integer: Into<Z>> CompareBase<Integer> for PolynomialRingZq {}
26
27/// Test that the [`CompareBase`] trait uses an actual implementation.
28#[cfg(test)]
29mod test_compare_base {
30    use crate::{
31        integer::{PolyOverZ, Z},
32        integer_mod_q::{ModulusPolynomialRingZq, PolyOverZq, PolynomialRingZq, Zq},
33        traits::CompareBase,
34    };
35    use std::str::FromStr;
36
37    /// Ensures that the [`CompareBase`] is available for all types it would be checked against
38    /// where no comparison is needed
39    #[test]
40    fn availability_without_comparisons() {
41        let modulus = ModulusPolynomialRingZq::from_str("3  1 0 1 mod 17").unwrap();
42        let one_1 = PolynomialRingZq::from(&modulus);
43
44        assert!(one_1.compare_base(&Z::ONE));
45        assert!(one_1.compare_base(&PolyOverZ::from_str("1  3").unwrap()));
46        assert!(one_1.compare_base(&0_i8));
47        assert!(one_1.compare_base(&0_i16));
48        assert!(one_1.compare_base(&0_i32));
49        assert!(one_1.compare_base(&0_i64));
50        assert!(one_1.compare_base(&0_u8));
51        assert!(one_1.compare_base(&0_u16));
52        assert!(one_1.compare_base(&0_u32));
53        assert!(one_1.compare_base(&0_u64));
54
55        assert!(one_1.call_compare_base_error(&Z::ONE).is_none());
56        assert!(
57            one_1
58                .call_compare_base_error(&PolyOverZ::from_str("1  3").unwrap())
59                .is_none()
60        );
61        assert!(one_1.call_compare_base_error(&0_i8).is_none());
62        assert!(one_1.call_compare_base_error(&0_i16).is_none());
63        assert!(one_1.call_compare_base_error(&0_i32).is_none());
64        assert!(one_1.call_compare_base_error(&0_i64).is_none());
65        assert!(one_1.call_compare_base_error(&0_u8).is_none());
66        assert!(one_1.call_compare_base_error(&0_u16).is_none());
67        assert!(one_1.call_compare_base_error(&0_u32).is_none());
68        assert!(one_1.call_compare_base_error(&0_u64).is_none());
69    }
70
71    /// Ensures that the [`CompareBase`] is available for all types it would be checked against
72    /// where comparison is needed
73    #[test]
74    fn availability_with_comparisons() {
75        let modulus = ModulusPolynomialRingZq::from_str("3  1 0 1 mod 17").unwrap();
76        let modulus_other = ModulusPolynomialRingZq::from_str("3  1 0 1 mod 18").unwrap();
77        let one_1 = PolynomialRingZq::from(&modulus);
78
79        assert!(one_1.compare_base(&one_1));
80        assert!(one_1.compare_base(&Zq::from((3, 17))));
81        assert!(!one_1.compare_base(&Zq::from((3, 18))));
82        assert!(one_1.compare_base(&PolyOverZq::from_str("1  3 mod 17").unwrap()));
83        assert!(!one_1.compare_base(&PolyOverZq::from_str("1  3 mod 18").unwrap()));
84        assert!(one_1.compare_base(&PolynomialRingZq::from(&modulus)));
85        assert!(!one_1.compare_base(&PolynomialRingZq::from(&modulus_other)));
86
87        assert!(one_1.call_compare_base_error(&Zq::from((3, 18))).is_some());
88        assert!(
89            one_1
90                .call_compare_base_error(&PolyOverZq::from_str("1  3 mod 18").unwrap())
91                .is_some()
92        );
93        assert!(
94            one_1
95                .call_compare_base_error(&PolynomialRingZq::from(&modulus_other))
96                .is_some()
97        );
98    }
99}