qfall_math/integer_mod_q/polynomial_ring_zq/to_string.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//! This module contains all options to convert a modulus of type
10//! [`PolynomialRingZq`] into a [`String`].
11//!
12//! The [`Display`](std::fmt::Display) trait is omitted, since it is derived,
13//! but tests for it are included.
14
15use super::PolynomialRingZq;
16use crate::macros::for_others::implement_for_owned;
17
18impl From<&PolynomialRingZq> for String {
19 /// Converts a [`PolynomialRingZq`] 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 of element]⌴⌴[0th coefficient]⌴
25 /// [1st coefficient]⌴...⌴/⌴[#number of coefficients of polynomial modulus]⌴⌴
26 /// [0th coefficient]⌴[1st coefficient]⌴...⌴mod⌴[q]"`.
27 ///
28 /// # Examples
29 /// ```
30 /// use qfall_math::integer_mod_q::PolynomialRingZq;
31 /// use std::str::FromStr;
32 /// let poly = PolynomialRingZq::from_str("2 2 1 / 3 2 2 2 mod 3").unwrap();
33 ///
34 /// let string: String = poly.into();
35 /// ```
36 fn from(value: &PolynomialRingZq) -> Self {
37 value.to_string()
38 }
39}
40
41implement_for_owned!(PolynomialRingZq, String, From);
42
43#[cfg(test)]
44mod test_to_string {
45 use super::PolynomialRingZq;
46 use std::str::FromStr;
47
48 /// Tests whether a polynomial that is created using a string, returns the
49 /// same string, when it is converted back to a string
50 #[test]
51 fn working_keeps_same_string() {
52 let cmp_str = "2 1 1 / 3 1 2 2 mod 5";
53 let cmp = PolynomialRingZq::from_str(cmp_str).unwrap();
54
55 assert_eq!(cmp_str, cmp.to_string());
56 }
57
58 /// Tests whether a polynomial that is created using a string, returns a
59 /// string that can be used to create a polynomial
60 #[test]
61 fn working_use_result_of_to_string_as_input() {
62 let cmp_str = "2 1 1 / 3 1 2 2 mod 5";
63 let cmp = PolynomialRingZq::from_str(cmp_str).unwrap();
64
65 let cmp_str_2 = cmp.to_string();
66
67 assert!(PolynomialRingZq::from_str(&cmp_str_2).is_ok());
68 }
69
70 /// Test applied modulus if initialized with negative values
71 #[test]
72 fn initialized_neg() {
73 let cmp_str = "2 -1 1 / 3 -1 -2 -3 mod 5";
74 let cmp = PolynomialRingZq::from_str(cmp_str).unwrap();
75
76 assert_eq!("2 4 1 / 3 4 3 2 mod 5", cmp.to_string());
77 }
78
79 /// Tests that large entries and large moduli work with to_string()
80 #[test]
81 fn large_entries_modulus() {
82 let cmp_str = format!("2 1 {} / 3 1 2 {} mod 1{}", u64::MAX, u64::MAX, u64::MAX);
83 let cmp = PolynomialRingZq::from_str(&cmp_str).unwrap();
84
85 assert_eq!(cmp_str, cmp.to_string());
86 }
87
88 /// Ensures that the `Into<String>` trait works properly
89 #[test]
90 fn into_works_properly() {
91 let cmp = "2 2 1 / 3 1 1 1 mod 3";
92 let poly = PolynomialRingZq::from_str(cmp).unwrap();
93
94 let string: String = poly.clone().into();
95 let borrowed_string: String = (&poly).into();
96
97 assert_eq!(cmp, string);
98 assert_eq!(cmp, borrowed_string);
99 }
100}