qfall_math/integer_mod_q/modulus_polynomial_ring_zq/to_string.rs
1// Copyright © 2023 Marcel Luca Schmidt, 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//! This module contains all options to convert a polynomial of type
10//! [`ModulusPolynomialRingZq`] into a [`String`].
11//!
12//! This includes the [`Display`] trait.
13
14use super::ModulusPolynomialRingZq;
15use crate::macros::for_others::implement_for_owned;
16use std::fmt::Display;
17
18impl From<&ModulusPolynomialRingZq> for String {
19 /// Converts a [`ModulusPolynomialRingZq`] into its [`String`] representation.
20 ///
21 /// Parameters:
22 /// - `value`: specifies the polynomial that will be represented as a [`String`]
23 ///
24 /// Returns a [`String`] of the form `"[#number of coefficients]⌴⌴[0th coefficient]⌴[1st coefficient]⌴...⌴mod⌴[q]"`.
25 ///
26 /// # Examples
27 /// ```
28 /// use qfall_math::integer_mod_q::ModulusPolynomialRingZq;
29 /// use std::str::FromStr;
30 /// let modulus = ModulusPolynomialRingZq::from_str("2 2 1 mod 3").unwrap();
31 ///
32 /// let string: String = modulus.into();
33 /// ```
34 fn from(value: &ModulusPolynomialRingZq) -> Self {
35 value.to_string()
36 }
37}
38
39implement_for_owned!(ModulusPolynomialRingZq, String, From);
40
41impl Display for ModulusPolynomialRingZq {
42 /// Allows to convert a [`ModulusPolynomialRingZq`] into a [`String`].
43 ///
44 /// # Examples
45 /// ```
46 /// use qfall_math::integer_mod_q::ModulusPolynomialRingZq;
47 /// use std::str::FromStr;
48 ///
49 /// let poly = ModulusPolynomialRingZq::from_str("3 1 0 1 mod 17").unwrap();
50 /// println!("{poly}");
51 /// ```
52 ///
53 /// ```
54 /// use qfall_math::integer_mod_q::ModulusPolynomialRingZq;
55 /// use std::str::FromStr;
56 ///
57 /// let poly = ModulusPolynomialRingZq::from_str("3 1 0 1 mod 17").unwrap();
58 /// let poly_string = poly.to_string();
59 /// ```
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 // get the value of the modulus
62 let modulus = self.get_q_as_modulus();
63
64 // get the value of the polynomial
65 let poly = self.modulus.get_representative_least_nonnegative_residue();
66
67 write!(f, "{poly} mod {modulus}")
68 }
69}
70
71/// most tests with specific values are covered in [`PolyOverZq`](crate::integer_mod_q::PolyOverZq)
72/// since the format is reused, we omit some tests
73#[cfg(test)]
74mod test_to_string {
75 use crate::integer_mod_q::ModulusPolynomialRingZq;
76 use std::str::FromStr;
77
78 /// Test whether a roundtrip works
79 #[test]
80 fn working_keeps_same_string() {
81 let cmp_str = "3 1 2 1 mod 5";
82 let cmp = ModulusPolynomialRingZq::from_str(cmp_str).unwrap();
83
84 assert_eq!(cmp_str, cmp.to_string());
85 }
86
87 /// Test whether a string returned from to_string can be used to construct a [`ModulusPolynomialRingZq`]
88 #[test]
89 fn working_use_result_of_to_string() {
90 let cmp_str = "3 1 2 1 mod 5";
91 let cmp = ModulusPolynomialRingZq::from_str(cmp_str).unwrap();
92 let str_1 = cmp.to_string();
93
94 assert!(ModulusPolynomialRingZq::from_str(&str_1).is_ok());
95 }
96
97 /// Ensures that the `Into<String>` trait works properly
98 #[test]
99 fn into_works_properly() {
100 let cmp = "2 2 1 mod 3";
101 let modulus = ModulusPolynomialRingZq::from_str(cmp).unwrap();
102
103 let string: String = modulus.clone().into();
104 let borrowed_string: String = (&modulus).into();
105
106 assert_eq!(cmp, string);
107 assert_eq!(cmp, borrowed_string);
108 }
109}