|
| 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 [`MatPolynomialRingZq`]. |
| 10 | +
|
| 11 | +use super::MatPolynomialRingZq; |
| 12 | +use crate::{error::MathError, traits::CompareBase}; |
| 13 | + |
| 14 | +impl CompareBase for MatPolynomialRingZq { |
| 15 | + /// Compares the moduli of the two elements. |
| 16 | + /// |
| 17 | + /// Parameters: |
| 18 | + /// - `other`: The other object whose base is compared to `self` |
| 19 | + /// |
| 20 | + /// Returns true if the moduli match and false otherwise. |
| 21 | + /// |
| 22 | + /// # Example |
| 23 | + /// ``` |
| 24 | + /// use qfall_math::{ |
| 25 | + /// integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq}, |
| 26 | + /// traits::CompareBase, |
| 27 | + /// }; |
| 28 | + /// use std::str::FromStr; |
| 29 | + /// |
| 30 | + /// let modulus = ModulusPolynomialRingZq::from_str("3 1 0 1 mod 17").unwrap(); |
| 31 | + /// let one_1 = MatPolynomialRingZq::identity(10, 7, &modulus); |
| 32 | + /// let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 23").unwrap(); |
| 33 | + /// let one_2 = MatPolynomialRingZq::identity(10, 7, &modulus); |
| 34 | + /// |
| 35 | + /// assert!(!one_1.compare_base(&one_2)); |
| 36 | + /// ``` |
| 37 | + fn compare_base(&self, other: &Self) -> bool { |
| 38 | + self.get_mod() == other.get_mod() |
| 39 | + } |
| 40 | + |
| 41 | + /// Returns an error that gives small explanation how the moduli differ. |
| 42 | + /// |
| 43 | + /// Parameters: |
| 44 | + /// - `other`: The other object whose base is compared to `self` |
| 45 | + /// |
| 46 | + /// Returns a MathError of type [MathError::MismatchingModulus]. |
| 47 | + /// |
| 48 | + /// # Example |
| 49 | + /// ``` |
| 50 | + /// use qfall_math::{ |
| 51 | + /// integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq}, |
| 52 | + /// traits::CompareBase, |
| 53 | + /// }; |
| 54 | + /// use std::str::FromStr; |
| 55 | + /// |
| 56 | + /// let modulus = ModulusPolynomialRingZq::from_str("3 1 0 1 mod 17").unwrap(); |
| 57 | + /// let one_1 = MatPolynomialRingZq::identity(10, 7, &modulus); |
| 58 | + /// let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 23").unwrap(); |
| 59 | + /// let one_2 = MatPolynomialRingZq::identity(10, 7, &modulus); |
| 60 | + /// |
| 61 | + /// assert!(one_1.call_compare_base_error(&one_2).is_some()); |
| 62 | + /// ``` |
| 63 | + fn call_compare_base_error(&self, other: &Self) -> Option<MathError> { |
| 64 | + Some(MathError::MismatchingModulus(format!( |
| 65 | + "The moduli of the matrices mismatch. One of them is {} and the other is {}. |
| 66 | + The desired operation is not defined and an error is returned.", |
| 67 | + self.get_mod(), |
| 68 | + other.get_mod() |
| 69 | + ))) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// Test that the [`CompareBase`] trait uses an actual implementation. |
| 74 | +#[cfg(test)] |
| 75 | +mod test_compare_base { |
| 76 | + use crate::{ |
| 77 | + integer_mod_q::{MatPolynomialRingZq, ModulusPolynomialRingZq}, |
| 78 | + traits::CompareBase, |
| 79 | + }; |
| 80 | + use std::str::FromStr; |
| 81 | + |
| 82 | + /// Ensures that the [`CompareBase`] trait uses an actual implementation. |
| 83 | + #[test] |
| 84 | + fn different_base() { |
| 85 | + let modulus = ModulusPolynomialRingZq::from_str("3 1 0 1 mod 17").unwrap(); |
| 86 | + let one_1 = MatPolynomialRingZq::identity(10, 7, &modulus); |
| 87 | + let modulus = ModulusPolynomialRingZq::from_str("4 1 0 0 1 mod 23").unwrap(); |
| 88 | + let one_2 = MatPolynomialRingZq::identity(10, 7, &modulus); |
| 89 | + |
| 90 | + assert!(!one_1.compare_base(&one_2)); |
| 91 | + assert!(one_1.call_compare_base_error(&one_2).is_some()) |
| 92 | + } |
| 93 | + |
| 94 | + /// Ensures that the same base return `true`. |
| 95 | + #[test] |
| 96 | + fn same_base() { |
| 97 | + let modulus = ModulusPolynomialRingZq::from_str("3 1 0 1 mod 17").unwrap(); |
| 98 | + let one_1 = MatPolynomialRingZq::identity(10, 7, &modulus); |
| 99 | + let one_2 = MatPolynomialRingZq::identity(29, 3, &modulus); |
| 100 | + |
| 101 | + assert!(one_1.compare_base(&one_2)); |
| 102 | + } |
| 103 | +} |
0 commit comments