qfall_math/integer_mod_q/modulus.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//! [`Modulus`] is a type of a positive integer larger than `1` that is used in
10//! order to do modulus operations. The modulus type itself is also used for
11//! optimizations.
12//! A [`Modulus`] of `1` is not allowed, since operations would always just
13//! return `0`. This way, checks later in the code can be avoided.
14//!
15//! This implementation uses the [FLINT](https://flintlib.org/) library.
16
17use flint_sys::fmpz_mod::fmpz_mod_ctx;
18use std::{fmt, rc::Rc};
19
20mod cmp;
21mod distance;
22mod fmpz_helpers;
23mod from;
24mod get;
25mod ownership;
26mod properties;
27mod serialize;
28mod to_string;
29mod unsafe_functions;
30
31/// [`Modulus`] is a type of a positive integer larger than `1` that is used
32/// to do modulus operations.
33///
34/// Attributes:
35/// - `modulus`: holds the value of the modulus
36///
37/// # Examples
38/// Create [`Modulus`] from [`str`]:
39/// ```
40/// use qfall_math::integer_mod_q::Modulus;
41/// use std::str::FromStr;
42/// use qfall_math::integer::Z;
43/// let value = Z::from(10);
44///
45/// // instantiations
46/// let _ = Modulus::from(10);
47/// let a = Modulus::from_str("42").unwrap();
48/// let b: Modulus = (&value).try_into().unwrap();
49///
50/// // clone
51/// let clone = a.clone();
52///
53/// // properties
54/// let is_prime: bool = a.is_prime();
55///
56/// // to_string incl. (de-)serialization
57/// assert_eq!("42", &a.to_string());
58/// assert_eq!(
59/// "{\"modulus\":\"42\"}",
60/// serde_json::to_string(&a).unwrap()
61/// );
62///
63/// // comparison
64/// assert_eq!(a, clone);
65/// ```
66pub struct Modulus {
67 pub(crate) modulus: Rc<fmpz_mod_ctx>,
68}
69
70impl fmt::Debug for Modulus {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 write!(
73 f,
74 "Modulus {{ modulus: {}, storage: {{modulus: {:?}}}}}",
75 self, self.modulus
76 )
77 }
78}