pub struct Modulus { /* private fields */ }Expand description
Modulus is a type of a positive integer larger than 1 that is used
to do modulus operations.
Attributes:
modulus: holds the value of the modulus
§Examples
use qfall_math::integer_mod_q::Modulus;
use std::str::FromStr;
use qfall_math::integer::Z;
let value = Z::from(10);
// instantiations
let _ = Modulus::from(10);
let a = Modulus::from_str("42").unwrap();
let b: Modulus = (&value).try_into().unwrap();
// clone
let clone = a.clone();
// properties
let is_prime: bool = a.is_prime();
// to_string incl. (de-)serialization
assert_eq!("42", &a.to_string());
assert_eq!(
"{\"modulus\":\"42\"}",
serde_json::to_string(&a).unwrap()
);
// comparison
assert_eq!(a, clone);Implementations§
Source§impl Modulus
impl Modulus
Sourcepub unsafe fn get_fmpz_mod_ctx(&mut self) -> &mut fmpz_mod_ctx
pub unsafe fn get_fmpz_mod_ctx(&mut self) -> &mut fmpz_mod_ctx
Returns a mutable reference to the field modulus of type fmpz_mod_ctx.
WARNING: The returned struct is part of flint_sys.
Any changes to this object are unsafe and may introduce memory leaks.
Please be aware that most moduli are shared across multiple instances and all
modifications of this struct will affect any other instance with a reference to this object.
This function is a passthrough to enable users of this library to use flint_sys
and with that FLINT functions that might not be covered in our library yet.
If this is the case, please consider contributing to this open-source project
by opening a Pull Request at qfall_math
to provide this feature in the future.
§Safety
Any flint_sys struct and function is part of a FFI to the C-library FLINT.
As FLINT is a C-library, it does not provide all memory safety features
that Rust and our Wrapper provide.
Thus, using functions of flint_sys can introduce memory leaks.
Source§impl Modulus
impl Modulus
Sourcepub unsafe fn set_fmpz_mod_ctx(&mut self, flint_struct: fmpz_mod_ctx)
pub unsafe fn set_fmpz_mod_ctx(&mut self, flint_struct: fmpz_mod_ctx)
Sets a mutable reference to the field modulus of type Modulus to a given fmpz_mod_ctx.
Parameters:
flint_struct: value to set the attribute to
WARNING: The set struct is part of flint_sys.
Any changes to this object are unsafe and may introduce memory leaks.
Please be aware that most moduli are shared across multiple instances and all
modifications of this struct will affect any other instance with a reference to this object.
This function is a passthrough to enable users of this library to use flint_sys
and with that FLINT functions that might not be covered in our library yet.
If this is the case, please consider contributing to this open-source project
by opening a Pull Request at qfall_math
to provide this feature in the future.
§Safety
Ensure that the old modulus does not share any memory with any moduli
that might be used in the future. The memory of the old modulus is freed
using this function.
Any flint_sys struct and function is part of a FFI to the C-library FLINT.
As FLINT is a C-library, it does not provide all memory safety features
that Rust and our Wrapper provide.
Thus, using functions of flint_sys can introduce memory leaks.
Trait Implementations§
Source§impl Clone for Modulus
impl Clone for Modulus
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones the given element and returns another cloned reference
to the fmpz_mod_ctx element.
§Examples
use qfall_math::integer_mod_q::Modulus;
use std::str::FromStr;
let a = Modulus::from(3);
let b = a.clone();1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'de> Deserialize<'de> for Modulus
impl<'de> Deserialize<'de> for Modulus
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Implements the deserialize option. This allows to create a Modulus from a given Json-object.
Source§impl Display for Modulus
impl Display for Modulus
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Allows to convert a modulus of type Modulus into a String.
§Examples
use qfall_math::integer_mod_q::Modulus;
use std::str::FromStr;
use core::fmt;
let modulus = Modulus::from(42);
println!("{modulus}");use qfall_math::integer_mod_q::Modulus;
use std::str::FromStr;
let modulus = Modulus::from(42);
let modulus_string = modulus.to_string();Source§impl<Integer: Into<Z>> Distance<Integer> for Modulus
impl<Integer: Into<Z>> Distance<Integer> for Modulus
Source§fn distance(&self, other: Integer) -> Self::Output
fn distance(&self, other: Integer) -> Self::Output
Computes the absolute distance between a Modulus and a value that
implements Into<Z>.
Parameters:
other: specifies theZvalue whose distance is calculated toself
Returns the absolute difference, i.e. distance between the Modulus instance
and the value that implements Into<Z> as a new Z instance.
§Examples
use qfall_math::integer::Z;
use qfall_math::integer_mod_q::Modulus;
use qfall_math::traits::*;
let a = Modulus::from(2);
let distance_0 = a.distance(5);
let distance_1 = a.distance(10);
type Output = Z
Source§impl Drop for Modulus
impl Drop for Modulus
Source§fn drop(&mut self)
fn drop(&mut self)
Drops the given reference to the fmpz_mod_ctx element
and frees the allocated memory if no references are left.
§Examples
use qfall_math::integer_mod_q::Modulus;
use std::str::FromStr;
{
let a = Modulus::from(3);
} // as a's scope ends here, it get's droppeduse qfall_math::integer_mod_q::Modulus;
use std::str::FromStr;
let a = Modulus::from(3);
drop(a); // explicitly drops a's valueSource§impl From<&Modulus> for Modulus
impl From<&Modulus> for Modulus
Source§fn from(value: &Modulus) -> Self
fn from(value: &Modulus) -> Self
Alias for Modulus::clone.
Source§impl From<&Modulus> for String
impl From<&Modulus> for String
Source§fn from(value: &Modulus) -> Self
fn from(value: &Modulus) -> Self
Converts a Modulus into its String representation.
Parameters:
value: specifies the modulus that will be represented as aString
Returns a String.
§Examples
use qfall_math::integer_mod_q::Modulus;
use std::str::FromStr;
let modulus = Modulus::from_str("6").unwrap();
let string: String = modulus.into();Source§impl<Integer: AsInteger + IntoModulus> From<Integer> for Modulus
impl<Integer: AsInteger + IntoModulus> From<Integer> for Modulus
Source§fn from(value: Integer) -> Self
fn from(value: Integer) -> Self
Creates a Modulus from a positive integer.
Parameters:
value: the initial value the modulus should have. It must be larger than one.
Returns a Modulus or an panics, if the
provided value is smaller than 2.
§Examples
use qfall_math::integer_mod_q::Modulus;
use qfall_math::integer::Z;
let _ = Modulus::from(10);
let _ = Modulus::from(u64::MAX);
let _ = Modulus::from(Z::from(42));§Panics …
- if the provided value is smaller than
2.
Source§impl FromStr for Modulus
impl FromStr for Modulus
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Creates a Modulus from a String.
Parameters:
s: the modulus of form:"[0-9]+"and not all zeros.
Returns a Modulus or an MathError, if the provided string is not
a valid modulus.
§Examples
use qfall_math::integer_mod_q::Modulus;
use std::str::FromStr;
let modulus = Modulus::from_str("42").unwrap();§Errors and Failures
- Returns a
MathErrorof typeStringConversionErrorif the provided string was not formatted correctly, e.g. not a correctly formattedZ. - Returns a
MathErrorof typeInvalidModulusif the provided value is smaller than2.
Source§impl Ord for Modulus
Enables the usage of max, min, and clamp.
impl Ord for Modulus
Enables the usage of max, min, and clamp.
§Examples
use qfall_math::integer_mod_q::Modulus;
use std::cmp::{max, min};
let a: Modulus = Modulus::from(10);
let b: Modulus = Modulus::from(42);
assert_eq!(b, max(a.clone(), b.clone()));
assert_eq!(a, min(a.clone(), b.clone()));
assert_eq!(a, Modulus::from(2).clamp(a.clone(), b.clone()));Source§fn cmp(&self, other: &Self) -> Ordering
fn cmp(&self, other: &Self) -> Ordering
Compares two Modulus values. Used by the <, <=, >, and >= operators.
Parameters:
other: the other value that is used to compare the elements
Returns the Ordering of the elements.
§Examples
use qfall_math::integer_mod_q::Modulus;
let a: Modulus = Modulus::from(10);
let b: Modulus = Modulus::from(42);
assert!(a < b);
assert!(a <= b);
assert!(b > a);
assert!(b >= a);1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq<Modulus> for Q
impl PartialEq<Modulus> for Q
Source§fn eq(&self, other: &Modulus) -> bool
fn eq(&self, other: &Modulus) -> bool
Checks if an integer and a rational are equal. Used by the == and != operators.
PartialEq is also implemented for Modulus using Q.
Parameters:
other: the other value that is used to compare the elements
Returns true if the elements are equal, otherwise false.
§Examples
use qfall_math::integer_mod_q::Modulus;
use qfall_math::rational::Q;
let a: Q = Q::from(42);
let b: Modulus = Modulus::from(42);
// These are all equivalent and return true.
let compared: bool = (a == b);
let compared: bool = (b == a);
let compared: bool = (&a == &b);
let compared: bool = (&b == &a);
let compared: bool = (a.eq(&b));
let compared: bool = (b.eq(&a));
let compared: bool = (Q::eq(&a, &b));
let compared: bool = (Modulus::eq(&b, &a));Source§impl PartialEq<Z> for Modulus
impl PartialEq<Z> for Modulus
Source§fn eq(&self, other: &Z) -> bool
fn eq(&self, other: &Z) -> bool
Checks if an integer and a modulus are equal. Used by the == and != operators.
PartialEq is also implemented for Z using Modulus.
Parameters:
other: the other value that is used to compare the elements
Returns true if the elements are equal, otherwise false.
§Examples
use qfall_math::integer::Z;
use qfall_math::integer_mod_q::Modulus;
let a: Modulus = Modulus::from(42);
let b: Z = Z::from(42);
// These are all equivalent and return true.
let compared: bool = (a == b);
let compared: bool = (b == a);
let compared: bool = (&a == &b);
let compared: bool = (&b == &a);
let compared: bool = (a.eq(&b));
let compared: bool = (b.eq(&a));
let compared: bool = (Z::eq(&b, &a));
let compared: bool = (Modulus::eq(&a, &b));Source§impl PartialEq for Modulus
impl PartialEq for Modulus
Source§fn eq(&self, other: &Self) -> bool
fn eq(&self, other: &Self) -> bool
Compares the two fmpz structs hiding behind the
given Modulus instances to check whether the given Modulus instances
have the same value.
Parameters:
other: holds anotherModulusobject whichselfis compared to
§Examples
use qfall_math::integer_mod_q::Modulus;
use std::str::FromStr;
let a = Modulus::from(3);
let b = Modulus::from(3);
let c = Modulus::from(4);
assert_eq!(a, b);
assert_ne!(a, c);Source§impl PartialOrd<Modulus> for Z
impl PartialOrd<Modulus> for Z
Source§fn partial_cmp(&self, other: &Modulus) -> Option<Ordering>
fn partial_cmp(&self, other: &Modulus) -> Option<Ordering>
Compares a Z value with a Modulus. Used by the <, <=, >, and >= operators.
PartialOrd is also implemented for Z using Modulus.
Parameters:
other: the other value that is used to compare the elements
Returns the Ordering of the elements.
§Examples
use qfall_math::integer::Z;
use qfall_math::integer_mod_q::Modulus;
let a: Modulus = Modulus::from(10);
let b: Z = Z::from(42);
assert!(a < b);
assert!(a <= b);
assert!(b > a);
assert!(b >= a);Source§impl PartialOrd<Z> for Modulus
impl PartialOrd<Z> for Modulus
Source§fn partial_cmp(&self, other: &Z) -> Option<Ordering>
fn partial_cmp(&self, other: &Z) -> Option<Ordering>
Compares a Z value with a Modulus. Used by the <, <=, >, and >= operators.
PartialOrd is also implemented for Z using Modulus.
Parameters:
other: the other value that is used to compare the elements
Returns the Ordering of the elements.
§Examples
use qfall_math::integer::Z;
use qfall_math::integer_mod_q::Modulus;
let a: Modulus = Modulus::from(10);
let b: Z = Z::from(42);
assert!(a < b);
assert!(a <= b);
assert!(b > a);
assert!(b >= a);Source§impl PartialOrd for Modulus
impl PartialOrd for Modulus
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
Compares two Modulus values. Used by the <, <=, >, and >= operators.
Parameters:
other: the other value that is used to compare the elements
Returns the Ordering of the elements.
§Examples
use qfall_math::integer_mod_q::Modulus;
let a: Modulus = Modulus::from(10);
let b: Modulus = Modulus::from(42);
assert!(a < b);
assert!(a <= b);
assert!(b > a);
assert!(b >= a);Source§impl Rem<&Modulus> for &MatPolyOverZ
impl Rem<&Modulus> for &MatPolyOverZ
Source§fn rem(self, modulus: &Modulus) -> Self::Output
fn rem(self, modulus: &Modulus) -> Self::Output
Computes self mod modulus as long as modulus is greater than 1.
For negative entries in self, the smallest positive representative is returned.
Parameters:
modulus: specifies a non-zero integer over which the positive remainders are computed
Returns self mod modulus as a MatPolyOverZ instance.
§Examples
use qfall_math::integer::MatPolyOverZ;
use qfall_math::integer_mod_q::Modulus;
use std::str::FromStr;
let a: MatPolyOverZ = MatPolyOverZ::from_str("[[2 1 -2],[1 42]]").unwrap();
let b = Modulus::from(24);
let c: MatPolyOverZ = &a % &b;Source§type Output = MatPolyOverZ
type Output = MatPolyOverZ
% operator.Source§impl Rem<&Modulus> for &MatZ
impl Rem<&Modulus> for &MatZ
Source§fn rem(self, modulus: &Modulus) -> Self::Output
fn rem(self, modulus: &Modulus) -> Self::Output
Computes self mod modulus.
For negative entries in self, the smallest positive representative is returned.
Parameters:
modulus: specifies a non-zero integer over which the positive remainders are computed
Returns self mod modulus as a MatZ instance.
§Examples
use qfall_math::integer::MatZ;
use qfall_math::integer_mod_q::Modulus;
use std::str::FromStr;
let a: MatZ = MatZ::from_str("[[-2],[42]]").unwrap();
let b = Modulus::from(24);
let c: MatZ = &a % &b;Source§impl Rem<&Modulus> for &PolyOverZ
impl Rem<&Modulus> for &PolyOverZ
Source§fn rem(self, modulus: &Modulus) -> Self::Output
fn rem(self, modulus: &Modulus) -> Self::Output
Computes self mod modulus as long as modulus is greater than 1.
For negative values of self, the smallest positive representative is returned.
Parameters:
modulus: specifies a non-zero integer over which the positive remainder is computed
Returns self mod modulus as a PolyOverZ instance.
§Examples
use qfall_math::integer_mod_q::Modulus;
use qfall_math::integer::{PolyOverZ, Z};
use std::str::FromStr;
let a: PolyOverZ = PolyOverZ::from_str("2 -2 42").unwrap();
let b = Modulus::from(24);
let c: PolyOverZ = a % b;Source§impl Rem<&Modulus> for &Z
impl Rem<&Modulus> for &Z
Source§fn rem(self, modulus: &Modulus) -> Self::Output
fn rem(self, modulus: &Modulus) -> Self::Output
Computes self mod modulus as long as modulus is greater than 1.
For negative values of self, the smallest positive representative is returned.
Parameters:
modulus: specifies a non-zero integer over which the positive remainder is computed
Returns self mod modulus as a Z instance.
§Examples
use qfall_math::integer::Z;
use qfall_math::integer_mod_q::Modulus;
let a: Z = Z::from(42);
let b = Modulus::from(24);
let c: Z = a % &b;