qfall_math/integer_mod_q/modulus/
properties.rs

1// Copyright © 2023 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//! Implementations to check certain properties of [`Modulus`]
10//! This includes checks such as primeness.
11
12use super::Modulus;
13use flint_sys::fmpz::fmpz_is_prime;
14
15impl Modulus {
16    /// Checks if a [`Modulus`] is prime.
17    ///
18    /// Returns `true` if the modulus is prime.
19    ///
20    /// ```
21    /// use std::str::FromStr;
22    /// use qfall_math::integer_mod_q::Modulus;
23    ///
24    /// let modulus = Modulus::from(17);
25    /// assert!(modulus.is_prime());
26    /// ```
27    pub fn is_prime(&self) -> bool {
28        1 == unsafe { fmpz_is_prime(&self.get_fmpz_mod_ctx_struct().n[0]) }
29    }
30}
31
32#[cfg(test)]
33mod test_is_prime {
34    use crate::integer_mod_q::Modulus;
35
36    /// Ensure that if a [`Modulus`] is instantiated with a prime, `true` is returned
37    #[test]
38    fn modulus_is_prime() {
39        let modulus = Modulus::from(2_i32.pow(16) + 1);
40        assert!(modulus.is_prime());
41    }
42
43    /// Ensure that if a [`Modulus`] is instantiated with a non-prime, `false` is returned
44    #[test]
45    fn modulus_is_not_prime() {
46        let modulus = Modulus::from(2_i32.pow(16));
47        assert!(!modulus.is_prime());
48    }
49}