qfall_math/integer_mod_q/modulus/
serialize.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//! This module contains implementations of functions
10//! important for serialization such as the [`Serialize`] and [`Deserialize`] trait.
11//!
12//! The explicit functions contain the documentation.
13
14use super::Modulus;
15use crate::macros::serialize::{deserialize, serialize};
16use core::fmt;
17use serde::{
18    Deserialize, Serialize,
19    de::{Error, MapAccess, Unexpected, Visitor},
20    ser::SerializeStruct,
21};
22use std::str::FromStr;
23
24serialize!("modulus", Modulus);
25deserialize!("modulus", Modulus, Modulus);
26
27#[cfg(test)]
28mod test_serialize {
29    use crate::integer_mod_q::Modulus;
30    use std::str::FromStr;
31
32    /// Tests whether the serialization of a positive [`Modulus`] works.
33    #[test]
34    fn serialize_output_positive() {
35        let z = Modulus::from(17);
36        let cmp_str = "{\"modulus\":\"17\"}";
37
38        assert_eq!(cmp_str, serde_json::to_string(&z).unwrap());
39    }
40
41    /// Tests whether the serialization of a positive large [`Modulus`] works.
42    #[test]
43    fn serialize_output_positive_large() {
44        let val_str = u64::MAX.to_string();
45        let z = Modulus::from_str(&val_str).unwrap();
46        let cmp_str = format!("{{\"modulus\":\"{val_str}\"}}");
47
48        assert_eq!(cmp_str, serde_json::to_string(&z).unwrap());
49    }
50}
51
52#[cfg(test)]
53mod test_deserialize {
54    use crate::integer_mod_q::Modulus;
55    use std::str::FromStr;
56
57    /// Tests whether the deserialization of a positive [`Modulus`] works.
58    #[test]
59    fn deserialize_positive() {
60        let z_string = "{\"modulus\":\"17\"}";
61        assert_eq!(
62            Modulus::from(17),
63            serde_json::from_str::<Modulus>(z_string).unwrap()
64        );
65    }
66
67    /// Tests whether the deserialization of a negative [`Modulus`] fails.
68    #[test]
69    fn deserialize_negative() {
70        let z_string = "{\"modulus\":\"-17\"}";
71
72        let a: Result<Modulus, serde_json::Error> = serde_json::from_str(z_string);
73        assert!(a.is_err());
74    }
75
76    /// Tests whether the deserialization of a positive [`Modulus`] works.
77    #[test]
78    fn deserialize_positive_large() {
79        let val_str = u64::MAX.to_string();
80        let z_string = format!("{{\"modulus\":\"{val_str}\"}}");
81
82        assert_eq!(
83            Modulus::from_str(&val_str).unwrap(),
84            serde_json::from_str::<Modulus>(&z_string).unwrap()
85        )
86    }
87
88    /// Tests whether the deserialization of a large negative [`Modulus`] fails.
89    #[test]
90    fn deserialize_negative_large() {
91        let val_str = format!("-{}", u64::MAX);
92        let z_string = format!("{{\"modulus\":\"{val_str}\"}}");
93
94        let a: Result<Modulus, serde_json::Error> = serde_json::from_str(&z_string);
95        assert!(a.is_err());
96    }
97
98    /// Tests whether no fields `modulus` provided yield an error
99    #[test]
100    fn no_field_value() {
101        let a: Result<Modulus, serde_json::Error> = serde_json::from_str("{{\"tree\":\"{17}\"}}");
102        assert!(a.is_err());
103
104        let b: Result<Modulus, serde_json::Error> = serde_json::from_str("{{}}");
105        assert!(b.is_err());
106    }
107
108    /// Tests whether too many fields yield an error
109    #[test]
110    fn too_many_fields() {
111        let a: Result<Modulus, serde_json::Error> =
112            serde_json::from_str("{{\"tree\":\"{17}\", \"modulus\":\"{17}\"}}");
113        assert!(a.is_err());
114
115        let b: Result<Modulus, serde_json::Error> =
116            serde_json::from_str("{{\"modulus\":\"{}\", \"modulus\":\"{17}\"}}");
117        assert!(b.is_err());
118    }
119}