qfall_math/integer/
z.rs

1// Copyright © 2023 Marcel Luca Schmidt
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//! `Z` is a type for integers with arbitrary length.
10//! This implementation uses the [FLINT](https://flintlib.org/) library.
11
12use flint_sys::fmpz::fmpz;
13use std::fmt;
14
15mod arithmetic;
16mod cmp;
17mod default;
18mod distance;
19pub(crate) mod fmpz_helpers;
20mod from;
21mod gcd;
22mod hash;
23mod lcm;
24mod logic;
25mod ownership;
26mod properties;
27mod sample;
28mod serialize;
29mod to_string;
30mod unsafe_functions;
31
32/// [`Z`] is an arbitrary integer value.
33///
34/// Attributes:
35/// - `value`: holds [FLINT](https://flintlib.org/)'s [struct](fmpz)
36///   for an integer value
37///
38/// # Implicit Typecasting
39/// Most of our functions take as input values of type [`Into<Z>`].
40/// These capture all types that can be turned into a [`Z`] value.
41/// The types are [`Z`],[`Modulus`](crate::integer_mod_q::Modulus), [`i8`],
42/// [`i16`],[`i32`],[`i64`],[`u8`],[`u16`],[`u32`],[`u64`] and the
43/// references of all of these types. These types are then implicitly casted to a [`Z`]
44/// before the desired action is performed.
45///
46/// # Examples
47/// ```
48/// use qfall_math::integer::Z;
49/// use std::str::FromStr;
50///
51/// // instantiations
52/// let a = Z::from_str("-876543")?;
53/// let b = Z::from(i64::MIN);
54/// let zero = Z::default();
55///
56/// // arithmetics
57/// let _ = &a * b.clone();
58/// let _ = &b - zero;
59///
60/// // comparison
61/// assert_ne!(b, a);
62///
63/// // to_string incl. (de-)serialization
64/// assert_eq!("-876543", &a.to_string());
65/// assert_eq!(
66///     "{\"value\":\"-876543\"}",
67///     serde_json::to_string(&a).unwrap()
68/// );
69/// # Ok::<(), qfall_math::error::MathError>(())
70/// ```
71pub struct Z {
72    pub(crate) value: fmpz,
73}
74
75impl fmt::Debug for Z {
76    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        write!(
78            f,
79            "Z {{value: {}, storage: {{value: {:?}}}}}",
80            self, self.value
81        )
82    }
83}