qfall-math 0.1.1

Mathematical foundations for rapid prototyping of lattice-based cryptography
Documentation
// Copyright © 2023 Marvin Beckmann
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.

//! Implementations to check certain properties of [`Modulus`]
//! This includes checks such as primeness.

use super::Modulus;
use flint_sys::fmpz::fmpz_is_prime;

impl Modulus {
    /// Checks if a [`Modulus`] is prime.
    ///
    /// Returns `true` if the modulus is prime.
    ///
    /// ```
    /// use std::str::FromStr;
    /// use qfall_math::integer_mod_q::Modulus;
    ///
    /// let modulus = Modulus::from(17);
    /// assert!(modulus.is_prime());
    /// ```
    pub fn is_prime(&self) -> bool {
        1 == unsafe { fmpz_is_prime(&self.get_fmpz_mod_ctx_struct().n[0]) }
    }
}

#[cfg(test)]
mod test_is_prime {
    use crate::integer_mod_q::Modulus;

    /// Ensure that if a [`Modulus`] is instantiated with a prime, `true` is returned
    #[test]
    fn modulus_is_prime() {
        let modulus = Modulus::from(2_i32.pow(16) + 1);
        assert!(modulus.is_prime());
    }

    /// Ensure that if a [`Modulus`] is instantiated with a non-prime, `false` is returned
    #[test]
    fn modulus_is_not_prime() {
        let modulus = Modulus::from(2_i32.pow(16));
        assert!(!modulus.is_prime());
    }
}