serialize.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #ifndef EPID_MEMBER_TINY_MATH_SERIALIZE_H_
  19. #define EPID_MEMBER_TINY_MATH_SERIALIZE_H_
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. /// \cond
  23. typedef struct OctStr32 OctStr32;
  24. typedef struct VeryLargeInt VeryLargeInt;
  25. typedef struct BigNumStr BigNumStr;
  26. typedef struct FqElem FqElem;
  27. typedef struct FqElemStr FqElemStr;
  28. typedef struct FpElem FpElem;
  29. typedef struct FpElemStr FpElemStr;
  30. typedef struct EccPointFq EccPointFq;
  31. typedef struct G1ElemStr G1ElemStr;
  32. typedef struct EccPointFq2 EccPointFq2;
  33. typedef struct G2ElemStr G2ElemStr;
  34. typedef struct Fq12Elem Fq12Elem;
  35. typedef struct Fq12ElemStr Fq12ElemStr;
  36. /// \endcond
  37. #if !defined(UNOPTIMIZED_SERIALIZATION)
  38. /// Serialize or deserailize a sequence of math objects
  39. /*!
  40. Converts layouts between native and portable or between portable and
  41. native of the following types: VeryLargeInt and BigNumStr,
  42. FqElem and FqElemStr, FpElem and FpElemStr, EccPointFq and G1ElemStr,
  43. Fq12Elem and Fq12ElemStr.
  44. If input contain multiple values of supported types all will be converted.
  45. \note The following types are not supported: VeryLargeIntProduct.
  46. \note This function have the assumptions that the input structures are packed
  47. such that consequent 32 byte fields would have no gap in between.
  48. \param[out] dest target buffer
  49. \param [in] dest_size size of dest buffer
  50. \param [in] src source data
  51. \param [in] src_size size of src buffer
  52. */
  53. void SwapNativeAndPortableLayout(void* dest, size_t dest_size, void const* src,
  54. size_t src_size);
  55. #endif // !defined(UNOPTIMIZED_SERIALIZATION)
  56. /// Write a uint32_t to a portable buffer
  57. /*!
  58. \param[out] dest target buffer
  59. \param [in] src source data
  60. \returns pointer to next byte after final data written to dest
  61. */
  62. void* Uint32Serialize(OctStr32* dest, uint32_t src);
  63. /// Read a uint32_t from a portable buffer
  64. /*!
  65. \param[out] dest target buffer
  66. \param [in] src source data
  67. \returns pointer to next byte after final data read from to src
  68. */
  69. void const* Uint32Deserialize(uint32_t* dest, OctStr32 const* src);
  70. /// Write a large integer to a portable buffer
  71. /*!
  72. \param[out] dest target buffer
  73. \param [in] src source data
  74. \returns pointer to next byte after final data written to dest
  75. */
  76. void* VliSerialize(BigNumStr* dest, VeryLargeInt const* src);
  77. /// Read a large integer from a portable buffer
  78. /*!
  79. \param[out] dest target buffer
  80. \param [in] src source data
  81. \returns pointer to next byte after final data read from to src
  82. */
  83. void const* VliDeserialize(VeryLargeInt* dest, BigNumStr const* src);
  84. /// Write an element of Fq to a portable buffer
  85. /*!
  86. \param[out] dest target buffer
  87. \param [in] src source data
  88. \returns pointer to next byte after final data written to dest
  89. */
  90. void* FqSerialize(FqElemStr* dest, FqElem const* src);
  91. /// Read an element of Fq from a portable buffer
  92. /*!
  93. \param[out] dest target buffer
  94. \param [in] src source data
  95. \returns pointer to next byte after final data read from to src
  96. */
  97. void const* FqDeserialize(FqElem* dest, FqElemStr const* src);
  98. /// Write an element of Fq12 to a portable buffer
  99. /*!
  100. \param[out] dest target buffer
  101. \param [in] src source data
  102. \returns pointer to next byte after final data written to dest
  103. */
  104. void* Fq12Serialize(Fq12ElemStr* dest, Fq12Elem const* src);
  105. /// Read an element of Fq12 from a portable buffer
  106. /*!
  107. \param[out] dest target buffer
  108. \param [in] src source data
  109. \returns pointer to next byte after final data read from to src
  110. */
  111. void const* Fq12Deserialize(Fq12Elem* dest, Fq12ElemStr const* src);
  112. /// Write an element of Fp to a portable buffer
  113. /*!
  114. \param[out] dest target buffer
  115. \param [in] src source data
  116. \returns pointer to next byte after final data written to dest
  117. */
  118. void* FpSerialize(FpElemStr* dest, FpElem const* src);
  119. /// Read an element of Fp from 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 read from to src
  124. */
  125. void const* FpDeserialize(FpElem* dest, FpElemStr const* src);
  126. /// Write a point on EFq to a portable buffer
  127. /*!
  128. \param[out] dest target buffer
  129. \param [in] src source data
  130. \returns pointer to next byte after final data written to dest
  131. */
  132. void* EFqSerialize(G1ElemStr* dest, EccPointFq const* src);
  133. /// Read a point on EFq from a portable buffer
  134. /*!
  135. \param[out] dest target buffer
  136. \param [in] src source data
  137. \returns pointer to next byte after final data read from to src
  138. */
  139. void const* EFqDeserialize(EccPointFq* dest, G1ElemStr const* src);
  140. /// Write a point on EFq2 to a portable buffer
  141. /*!
  142. \param[out] dest target buffer
  143. \param [in] src source data
  144. \returns pointer to next byte after final data written to dest
  145. */
  146. void* EFq2Serialize(G2ElemStr* dest, EccPointFq2 const* src);
  147. /// Read a point on EFq2 from a portable buffer
  148. /*!
  149. \param[out] dest target buffer
  150. \param [in] src source data
  151. \returns pointer to next byte after final data read from to src
  152. */
  153. void const* EFq2Deserialize(EccPointFq2* dest, G2ElemStr const* src);
  154. #endif // EPID_MEMBER_TINY_MATH_SERIALIZE_H_