he_ring/ciphertext_ring/
serialization.rs1use feanor_math::integer::BigIntRing;
2use feanor_math::matrix::AsPointerToSlice;
3use feanor_math::matrix::Submatrix;
4use feanor_math::matrix::SubmatrixMut;
5use feanor_math::rings::zn::zn_rns;
6use feanor_math::rings::zn::zn_64::*;
7use feanor_math::seq::VectorView;
8use feanor_math::serialization::DeserializeWithRing;
9use feanor_math::serialization::SerializeWithRing;
10use serde::de;
11use serde::de::DeserializeSeed;
12use serde::de::Visitor;
13use serde::ser::SerializeTuple;
14use serde::{Serialize, Serializer};
15
16pub fn serialize_rns_data<'a, V>(rns_base: &'a zn_rns::Zn<Zn, BigIntRing>, data: Submatrix<'a, V, ZnEl>) -> impl use<'a, V> + Serialize
17 where V: AsPointerToSlice<ZnEl>
18{
19 assert_eq!(rns_base.len(), data.row_count());
20
21 struct SerializeWrapper<'a, V>
22 where V: AsPointerToSlice<ZnEl>
23 {
24 rns_base: &'a zn_rns::Zn<Zn, BigIntRing>,
25 data: Submatrix<'a, V, ZnEl>
26 }
27
28 impl<'a, V> Serialize for SerializeWrapper<'a, V>
29 where V: AsPointerToSlice<ZnEl>
30 {
31 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
32 where S: Serializer
33 {
34 let mut tuple = serializer.serialize_tuple(self.data.row_count() * self.data.col_count())?;
35 for i in 0..self.data.row_count() {
36 for j in 0..self.data.col_count() {
37 tuple.serialize_element(&SerializeWithRing::new(self.data.at(i, j), self.rns_base.at(i)))?;
38 }
39 }
40 return tuple.end();
41 }
42 }
43
44 return SerializeWrapper { rns_base, data };
45}
46
47pub fn deserialize_rns_data<'a, V>(rns_base: &'a zn_rns::Zn<Zn, BigIntRing>, result: SubmatrixMut<'a, V, ZnEl>) -> impl use<'a, V> + for<'de> DeserializeSeed<'de, Value = SubmatrixMut<'a, V, ZnEl>>
48 where V: AsPointerToSlice<ZnEl>
49{
50 struct ResultVisitor<'a, V>
51 where V: AsPointerToSlice<ZnEl>
52 {
53 rns_base: &'a zn_rns::Zn<Zn, BigIntRing>,
54 result: SubmatrixMut<'a, V, ZnEl>
55 }
56
57 impl<'a, 'de, V> Visitor<'de> for ResultVisitor<'a, V>
58 where V: AsPointerToSlice<ZnEl>
59 {
60 type Value = SubmatrixMut<'a, V, ZnEl>;
61
62 fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
63 write!(f, "a sequence of {} RNS coefficients", self.result.row_count() * self.result.col_count())
64 }
65
66 fn visit_seq<S>(mut self, mut seq: S) -> Result<Self::Value, S::Error>
67 where S: de::SeqAccess<'de>
68 {
69 for i in 0..self.result.row_count() {
70 for j in 0..self.result.col_count() {
71 if let Some(c) = seq.next_element_seed(DeserializeWithRing::new(self.rns_base.at(i)))? {
72 *self.result.at_mut(i, j) = c;
73 } else {
74 return Err(de::Error::invalid_length(i * self.result.col_count() + j, &format!("expected a sequence of {} RNS coefficients", self.result.row_count() * self.result.col_count()).as_str()));
75 }
76 }
77 }
78 return Ok(self.result);
79 }
80 }
81
82 struct DeserializeResult<'a, V>
83 where V: AsPointerToSlice<ZnEl>
84 {
85 rns_base: &'a zn_rns::Zn<Zn, BigIntRing>,
86 result: SubmatrixMut<'a, V, ZnEl>
87 }
88 impl<'a, 'de, V> DeserializeSeed<'de> for DeserializeResult<'a, V>
89 where V: AsPointerToSlice<ZnEl>
90 {
91 type Value = SubmatrixMut<'a, V, ZnEl>;
92
93 fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
94 where D: de::Deserializer<'de>
95 {
96 deserializer.deserialize_tuple(self.result.col_count() * self.result.row_count(), ResultVisitor { rns_base: self.rns_base, result: self.result })
97 }
98 }
99
100 return DeserializeResult { rns_base, result };
101}