• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*############################################################################
2 # Copyright 2017 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 ############################################################################*/
16 /// Definition of de/serialize functionality.
17 /*! \file */
18 
19 #ifndef EPID_MEMBER_TINY_MATH_SERIALIZE_H_
20 #define EPID_MEMBER_TINY_MATH_SERIALIZE_H_
21 #include <stddef.h>
22 #include <stdint.h>
23 
24 /// \cond
25 typedef struct OctStr32 OctStr32;
26 
27 typedef struct VeryLargeInt VeryLargeInt;
28 typedef struct BigNumStr BigNumStr;
29 
30 typedef struct FqElem FqElem;
31 typedef struct FqElemStr FqElemStr;
32 
33 typedef struct FpElem FpElem;
34 typedef struct FpElemStr FpElemStr;
35 
36 typedef struct EccPointFq EccPointFq;
37 typedef struct G1ElemStr G1ElemStr;
38 
39 typedef struct EccPointFq2 EccPointFq2;
40 typedef struct G2ElemStr G2ElemStr;
41 
42 typedef struct Fq12Elem Fq12Elem;
43 typedef struct Fq12ElemStr Fq12ElemStr;
44 /// \endcond
45 
46 #if !defined(UNOPTIMIZED_SERIALIZATION)
47 
48 /// Serialize or deserailize a sequence of math objects
49 /*!
50 Converts layouts between native and portable or between portable and
51 native of the following types: VeryLargeInt and BigNumStr,
52 FqElem and FqElemStr, FpElem and FpElemStr, EccPointFq and G1ElemStr,
53 Fq12Elem and Fq12ElemStr.
54 
55 If input contain multiple values of supported types all will be converted.
56 
57 \note The following types are not supported: VeryLargeIntProduct.
58 
59 \note This function have the assumptions that the input structures are packed
60 such that consequent 32 byte fields would have no gap in between.
61 
62 \param[out] dest target buffer
63 \param [in] dest_size size of dest buffer
64 \param [in] src source data
65 \param [in] src_size size of src buffer
66 */
67 void SwapNativeAndPortableLayout(void* dest, size_t dest_size, void const* src,
68                                  size_t src_size);
69 #endif  // !defined(UNOPTIMIZED_SERIALIZATION)
70 
71 /// Write a uint32_t to a portable buffer
72 /*!
73 \param[out] dest target buffer
74 \param [in] src source data
75 \returns pointer to next byte after final data written to dest
76 */
77 void* Uint32Serialize(OctStr32* dest, uint32_t src);
78 
79 /// Read a uint32_t from a portable buffer
80 /*!
81 \param[out] dest target buffer
82 \param [in] src source data
83 \returns pointer to next byte after final data read from to src
84 */
85 void const* Uint32Deserialize(uint32_t* dest, OctStr32 const* src);
86 
87 /// Write a large integer to a portable buffer
88 /*!
89 \param[out] dest target buffer
90 \param [in] src source data
91 \returns pointer to next byte after final data written to dest
92 */
93 void* VliSerialize(BigNumStr* dest, VeryLargeInt const* src);
94 
95 /// Read a large integer from a portable buffer
96 /*!
97 \param[out] dest target buffer
98 \param [in] src source data
99 \returns pointer to next byte after final data read from to src
100 */
101 void const* VliDeserialize(VeryLargeInt* dest, BigNumStr const* src);
102 
103 /// Write an element of Fq to a portable buffer
104 /*!
105 \param[out] dest target buffer
106 \param [in] src source data
107 \returns pointer to next byte after final data written to dest
108 */
109 void* FqSerialize(FqElemStr* dest, FqElem const* src);
110 
111 /// Read an element of Fq from a portable buffer
112 /*!
113 \param[out] dest target buffer
114 \param [in] src source data
115 \returns pointer to next byte after final data read from to src
116 */
117 void const* FqDeserialize(FqElem* dest, FqElemStr const* src);
118 
119 /// Write an element of Fq12 to a portable buffer
120 /*!
121 \param[out] dest target buffer
122 \param [in] src source data
123 \returns pointer to next byte after final data written to dest
124 */
125 void* Fq12Serialize(Fq12ElemStr* dest, Fq12Elem const* src);
126 
127 /// Read an element of Fq12 from a portable buffer
128 /*!
129 \param[out] dest target buffer
130 \param [in] src source data
131 \returns pointer to next byte after final data read from to src
132 */
133 void const* Fq12Deserialize(Fq12Elem* dest, Fq12ElemStr const* src);
134 
135 /// Write an element of Fp to a portable buffer
136 /*!
137 \param[out] dest target buffer
138 \param [in] src source data
139 \returns pointer to next byte after final data written to dest
140 */
141 void* FpSerialize(FpElemStr* dest, FpElem const* src);
142 
143 /// Read an element of Fp from a portable buffer
144 /*!
145 \param[out] dest target buffer
146 \param [in] src source data
147 \returns pointer to next byte after final data read from to src
148 */
149 void const* FpDeserialize(FpElem* dest, FpElemStr const* src);
150 
151 /// Write a point on EFq to a portable buffer
152 /*!
153 \param[out] dest target buffer
154 \param [in] src source data
155 \returns pointer to next byte after final data written to dest
156 */
157 void* EFqSerialize(G1ElemStr* dest, EccPointFq const* src);
158 
159 /// Read a point on EFq from a portable buffer
160 /*!
161 \param[out] dest target buffer
162 \param [in] src source data
163 \returns pointer to next byte after final data read from to src
164 */
165 void const* EFqDeserialize(EccPointFq* dest, G1ElemStr const* src);
166 
167 /// Write a point on EFq2 to a portable buffer
168 /*!
169 \param[out] dest target buffer
170 \param [in] src source data
171 \returns pointer to next byte after final data written to dest
172 */
173 void* EFq2Serialize(G2ElemStr* dest, EccPointFq2 const* src);
174 
175 /// Read a point on EFq2 from a portable buffer
176 /*!
177 \param[out] dest target buffer
178 \param [in] src source data
179 \returns pointer to next byte after final data read from to src
180 */
181 void const* EFq2Deserialize(EccPointFq2* dest, G2ElemStr const* src);
182 
183 #endif  // EPID_MEMBER_TINY_MATH_SERIALIZE_H_
184