serialize-test.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. /// Unit tests of serialization implementation.
  17. /*! \file */
  18. #include <gtest/gtest.h>
  19. #include <stdint.h>
  20. #include <cstring>
  21. #include "epid/member/tiny/math/unittests/cmp-testhelper.h"
  22. extern "C" {
  23. #include "epid/common/types.h"
  24. #include "epid/member/tiny/math/mathtypes.h"
  25. #include "epid/member/tiny/math/serialize.h"
  26. }
  27. bool operator==(OctStr32 const& lhs, OctStr32 const& rhs) {
  28. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  29. }
  30. bool operator==(BigNumStr const& lhs, BigNumStr const& rhs) {
  31. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  32. }
  33. bool operator==(FpElemStr const& lhs, FpElemStr const& rhs) {
  34. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  35. }
  36. bool operator==(FqElemStr const& lhs, FqElemStr const& rhs) {
  37. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  38. }
  39. bool operator==(G1ElemStr const& lhs, G1ElemStr const& rhs) {
  40. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  41. }
  42. bool operator==(G2ElemStr const& lhs, G2ElemStr const& rhs) {
  43. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  44. }
  45. bool operator==(Fq12ElemStr const& lhs, Fq12ElemStr const& rhs) {
  46. return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
  47. }
  48. namespace {
  49. ////////////////////////////////////////////////////////////////////////
  50. // Uint32Serialize
  51. TEST(TinySerializeTest, Uint32SerializeWorks) {
  52. OctStr32 octstr = {0};
  53. const OctStr32 expected_octstr = {0x01, 0x02, 0x03, 0x04};
  54. const uint32_t num = 0x01020304;
  55. Uint32Serialize(&octstr, num);
  56. EXPECT_EQ(expected_octstr, octstr);
  57. }
  58. ////////////////////////////////////////////////////////////////////////
  59. // Uint32Deserialize
  60. TEST(TinySerializeTest, Uint32DeserializeWorks) {
  61. const OctStr32 octstr = {0x01, 0x02, 0x03, 0x04};
  62. uint32_t num = 0;
  63. const uint32_t expected_num = 0x01020304;
  64. Uint32Deserialize(&num, &octstr);
  65. EXPECT_EQ(expected_num, num);
  66. }
  67. ////////////////////////////////////////////////////////////////////////
  68. // VliSerialize
  69. TEST(TinySerializeTest, VliSerializeWorks) {
  70. const VeryLargeInt vli = {0x01020304, 0x05060708, 0x090A0B0C, 0x0D0E0F10,
  71. 0x11121314, 0x15161718, 0x191A1B1C, 0x1D1E1F20};
  72. BigNumStr serialize_vli = {0};
  73. const BigNumStr expected_serialize_vli = {
  74. 0x1D, 0x1E, 0x1F, 0x20, 0x19, 0x1A, 0x1B, 0x1C, 0x15, 0x16, 0x17,
  75. 0x18, 0x11, 0x12, 0x13, 0x14, 0x0D, 0x0E, 0x0F, 0x10, 0x09, 0x0A,
  76. 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04};
  77. VliSerialize(&serialize_vli, &vli);
  78. EXPECT_EQ(expected_serialize_vli, serialize_vli);
  79. }
  80. ////////////////////////////////////////////////////////////////////////
  81. // VliDeserialize
  82. TEST(TinySerializeTest, VliDeserializeWorks) {
  83. VeryLargeInt vli = {0};
  84. const VeryLargeInt expected_vli = {0x01020304, 0x05060708, 0x090A0B0C,
  85. 0x0D0E0F10, 0x11121314, 0x15161718,
  86. 0x191A1B1C, 0x1D1E1F20};
  87. const BigNumStr serialize_vli = {
  88. 0x1D, 0x1E, 0x1F, 0x20, 0x19, 0x1A, 0x1B, 0x1C, 0x15, 0x16, 0x17,
  89. 0x18, 0x11, 0x12, 0x13, 0x14, 0x0D, 0x0E, 0x0F, 0x10, 0x09, 0x0A,
  90. 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04};
  91. VliDeserialize(&vli, &serialize_vli);
  92. EXPECT_EQ(expected_vli, vli);
  93. }
  94. ////////////////////////////////////////////////////////////////////////
  95. // FqSerialize
  96. TEST(TinySerializeTest, FqSerializeWorks) {
  97. const FqElem fqelm = {0x01020304, 0x05060708, 0x090A0B0C, 0x0D0E0F10,
  98. 0x11121314, 0x15161718, 0x191A1B1C, 0x1D1E1F20};
  99. FqElemStr serialize_fqelm = {0};
  100. const FqElemStr expected_serialize_fqelm = {
  101. 0x1D, 0x1E, 0x1F, 0x20, 0x19, 0x1A, 0x1B, 0x1C, 0x15, 0x16, 0x17,
  102. 0x18, 0x11, 0x12, 0x13, 0x14, 0x0D, 0x0E, 0x0F, 0x10, 0x09, 0x0A,
  103. 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04};
  104. FqSerialize(&serialize_fqelm, &fqelm);
  105. EXPECT_EQ(expected_serialize_fqelm, serialize_fqelm);
  106. }
  107. ////////////////////////////////////////////////////////////////////////
  108. // FqDeserialize
  109. TEST(TinySerializeTest, FqDeserializeWorks) {
  110. const FqElemStr serialize_fqelm = {
  111. 0x1D, 0x1E, 0x1F, 0x20, 0x19, 0x1A, 0x1B, 0x1C, 0x15, 0x16, 0x17,
  112. 0x18, 0x11, 0x12, 0x13, 0x14, 0x0D, 0x0E, 0x0F, 0x10, 0x09, 0x0A,
  113. 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04};
  114. FqElem fqelm = {0};
  115. const FqElem expected_fqelm = {0x01020304, 0x05060708, 0x090A0B0C,
  116. 0x0D0E0F10, 0x11121314, 0x15161718,
  117. 0x191A1B1C, 0x1D1E1F20};
  118. FqDeserialize(&fqelm, &serialize_fqelm);
  119. EXPECT_EQ(expected_fqelm, fqelm);
  120. }
  121. ////////////////////////////////////////////////////////////////////////
  122. // Fq12Serialize
  123. TEST(TinySerializeTest, Fq12SerializeWorks) {
  124. const Fq12Elem fq12elm = {
  125. {{
  126. {0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  127. 0x00000000, 0x00000000, 0x00000000},
  128. {0x00000200, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  129. 0x00000000, 0x00000000, 0x00000000},
  130. },
  131. {
  132. {0x00030000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  133. 0x00000000, 0x00000000, 0x00000000},
  134. {0x04000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  135. 0x00000000, 0x00000000, 0x00000000},
  136. },
  137. {
  138. {0x00000000, 0x00000005, 0x00000000, 0x00000000, 0x00000000,
  139. 0x00000000, 0x00000000, 0x00000000},
  140. {0x00000000, 0x00000600, 0x00000000, 0x00000000, 0x00000000,
  141. 0x00000000, 0x00000000, 0x00000000},
  142. }},
  143. {{
  144. {0x00000000, 0x00070000, 0x00000000, 0x00000000, 0x00000000,
  145. 0x00000000, 0x00000000, 0x00000000},
  146. {0x00000000, 0x08000000, 0x00000000, 0x00000000, 0x00000000,
  147. 0x00000000, 0x00000000, 0x00000000},
  148. },
  149. {
  150. {0x00000000, 0x00000000, 0x00000009, 0x00000000, 0x00000000,
  151. 0x00000000, 0x00000000, 0x00000000},
  152. {0x00000000, 0x00000000, 0x00000A00, 0x00000000, 0x00000000,
  153. 0x00000000, 0x00000000, 0x00000000},
  154. },
  155. {
  156. {0x00000000, 0x00000000, 0x000B0000, 0x00000000, 0x00000000,
  157. 0x00000000, 0x00000000, 0x00000000},
  158. {0x00000000, 0x00000000, 0x0C000000, 0x00000000, 0x00000000,
  159. 0x00000000, 0x00000000, 0x00000000},
  160. }}};
  161. Fq12ElemStr serialized_fq12elm = {0};
  162. const Fq12ElemStr expected_serialized_fq12elm = {
  163. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  164. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  165. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  166. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  167. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  168. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
  169. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  170. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  171. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  172. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  173. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  174. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  175. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  176. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  177. 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
  178. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  179. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  180. 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
  181. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  182. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  183. 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  184. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  185. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  186. 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  187. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  188. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  189. 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  190. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  191. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A,
  192. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  193. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  194. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0B,
  195. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  196. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  197. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00,
  198. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  199. };
  200. Fq12Serialize(&serialized_fq12elm, &fq12elm);
  201. EXPECT_EQ(expected_serialized_fq12elm, serialized_fq12elm);
  202. }
  203. ////////////////////////////////////////////////////////////////////////
  204. // Fq12Deserialize
  205. TEST(TinySerializeTest, Fq12DeserializeWorks) {
  206. const Fq12ElemStr serialized_fq12elm = {
  207. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  208. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  209. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  210. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  211. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  212. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
  213. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  214. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  215. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  216. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  217. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  218. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  219. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  220. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  221. 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00,
  222. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  223. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  224. 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
  225. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  226. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  227. 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  228. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  229. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  230. 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  231. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  232. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  233. 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  234. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  235. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A,
  236. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  237. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  238. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0B,
  239. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  240. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  241. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00,
  242. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  243. };
  244. Fq12Elem fq12elm = {0};
  245. const Fq12Elem expected_fq12elm = {
  246. {{
  247. {0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  248. 0x00000000, 0x00000000, 0x00000000},
  249. {0x00000200, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  250. 0x00000000, 0x00000000, 0x00000000},
  251. },
  252. {
  253. {0x00030000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  254. 0x00000000, 0x00000000, 0x00000000},
  255. {0x04000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  256. 0x00000000, 0x00000000, 0x00000000},
  257. },
  258. {
  259. {0x00000000, 0x00000005, 0x00000000, 0x00000000, 0x00000000,
  260. 0x00000000, 0x00000000, 0x00000000},
  261. {0x00000000, 0x00000600, 0x00000000, 0x00000000, 0x00000000,
  262. 0x00000000, 0x00000000, 0x00000000},
  263. }},
  264. {{
  265. {0x00000000, 0x00070000, 0x00000000, 0x00000000, 0x00000000,
  266. 0x00000000, 0x00000000, 0x00000000},
  267. {0x00000000, 0x08000000, 0x00000000, 0x00000000, 0x00000000,
  268. 0x00000000, 0x00000000, 0x00000000},
  269. },
  270. {
  271. {0x00000000, 0x00000000, 0x00000009, 0x00000000, 0x00000000,
  272. 0x00000000, 0x00000000, 0x00000000},
  273. {0x00000000, 0x00000000, 0x00000A00, 0x00000000, 0x00000000,
  274. 0x00000000, 0x00000000, 0x00000000},
  275. },
  276. {
  277. {0x00000000, 0x00000000, 0x000B0000, 0x00000000, 0x00000000,
  278. 0x00000000, 0x00000000, 0x00000000},
  279. {0x00000000, 0x00000000, 0x0C000000, 0x00000000, 0x00000000,
  280. 0x00000000, 0x00000000, 0x00000000},
  281. }}};
  282. Fq12Deserialize(&fq12elm, &serialized_fq12elm);
  283. EXPECT_EQ(expected_fq12elm, fq12elm);
  284. }
  285. ////////////////////////////////////////////////////////////////////////
  286. // FpSerialize
  287. TEST(TinySerializeTest, FpSerializeWorks) {
  288. const FpElem fpelm = {0x01020304, 0x05060708, 0x090A0B0C, 0x0D0E0F10,
  289. 0x11121314, 0x15161718, 0x191A1B1C, 0x1D1E1F20};
  290. FpElemStr serialize_fpelm = {0};
  291. const FpElemStr expected_serialize_fpelm = {
  292. 0x1D, 0x1E, 0x1F, 0x20, 0x19, 0x1A, 0x1B, 0x1C, 0x15, 0x16, 0x17,
  293. 0x18, 0x11, 0x12, 0x13, 0x14, 0x0D, 0x0E, 0x0F, 0x10, 0x09, 0x0A,
  294. 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04};
  295. FpSerialize(&serialize_fpelm, &fpelm);
  296. EXPECT_EQ(expected_serialize_fpelm, serialize_fpelm);
  297. }
  298. ////////////////////////////////////////////////////////////////////////
  299. // FpDeserialize
  300. TEST(TinySerializeTest, FpDeserializeWorks) {
  301. const FpElemStr serialize_fpelm = {
  302. 0x1D, 0x1E, 0x1F, 0x20, 0x19, 0x1A, 0x1B, 0x1C, 0x15, 0x16, 0x17,
  303. 0x18, 0x11, 0x12, 0x13, 0x14, 0x0D, 0x0E, 0x0F, 0x10, 0x09, 0x0A,
  304. 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04};
  305. FpElem fpelm = {0};
  306. const FpElem expected_fpelm = {0x01020304, 0x05060708, 0x090A0B0C,
  307. 0x0D0E0F10, 0x11121314, 0x15161718,
  308. 0x191A1B1C, 0x1D1E1F20};
  309. FpDeserialize(&fpelm, &serialize_fpelm);
  310. EXPECT_EQ(expected_fpelm, fpelm);
  311. }
  312. ////////////////////////////////////////////////////////////////////////
  313. // EFqSerialize
  314. TEST(TinySerializeTest, EFqSerializeWorks) {
  315. const FqElem fqelm = {0x01020304, 0x05060708, 0x090A0B0C, 0x0D0E0F10,
  316. 0x11121314, 0x15161718, 0x191A1B1C, 0x1D1E1F20};
  317. const EccPointFq efq_point = {fqelm, fqelm};
  318. const FqElemStr serialize_fqelm = {
  319. 0x1D, 0x1E, 0x1F, 0x20, 0x19, 0x1A, 0x1B, 0x1C, 0x15, 0x16, 0x17,
  320. 0x18, 0x11, 0x12, 0x13, 0x14, 0x0D, 0x0E, 0x0F, 0x10, 0x09, 0x0A,
  321. 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04};
  322. G1ElemStr efq_point_serialize = {0};
  323. const G1ElemStr expected_efq_point_serialize = {serialize_fqelm,
  324. serialize_fqelm};
  325. EFqSerialize(&efq_point_serialize, &efq_point);
  326. EXPECT_EQ(expected_efq_point_serialize, efq_point_serialize);
  327. }
  328. ////////////////////////////////////////////////////////////////////////
  329. // EFqDeserialize
  330. TEST(TinySerializeTest, EFqDeserializeWorks) {
  331. const FqElemStr serialize_fqelm = {
  332. 0x1D, 0x1E, 0x1F, 0x20, 0x19, 0x1A, 0x1B, 0x1C, 0x15, 0x16, 0x17,
  333. 0x18, 0x11, 0x12, 0x13, 0x14, 0x0D, 0x0E, 0x0F, 0x10, 0x09, 0x0A,
  334. 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04};
  335. const G1ElemStr serialize_efq_point = {serialize_fqelm, serialize_fqelm};
  336. EccPointFq efq_point = {0};
  337. const FqElem fqelm = {0x01020304, 0x05060708, 0x090A0B0C, 0x0D0E0F10,
  338. 0x11121314, 0x15161718, 0x191A1B1C, 0x1D1E1F20};
  339. const EccPointFq expected_efq_point = {fqelm, fqelm};
  340. EFqDeserialize(&efq_point, &serialize_efq_point);
  341. EXPECT_EQ(expected_efq_point, efq_point);
  342. }
  343. ////////////////////////////////////////////////////////////////////////
  344. // EFq2Serialize
  345. TEST(TinySerializeTest, EFq2SerializeWorks) {
  346. const FqElem fqelm = {0x01020304, 0x05060708, 0x090A0B0C, 0x0D0E0F10,
  347. 0x11121314, 0x15161718, 0x191A1B1C, 0x1D1E1F20};
  348. const EccPointFq2 efq2_point = {{fqelm, fqelm}, {fqelm, fqelm}};
  349. const FqElemStr serialize_fqelm = {
  350. 0x1D, 0x1E, 0x1F, 0x20, 0x19, 0x1A, 0x1B, 0x1C, 0x15, 0x16, 0x17,
  351. 0x18, 0x11, 0x12, 0x13, 0x14, 0x0D, 0x0E, 0x0F, 0x10, 0x09, 0x0A,
  352. 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04};
  353. G2ElemStr efq2_point_serialize = {0};
  354. const G2ElemStr expected_efq2_point_serialize = {
  355. {serialize_fqelm, serialize_fqelm}, {serialize_fqelm, serialize_fqelm}};
  356. EFq2Serialize(&efq2_point_serialize, &efq2_point);
  357. EXPECT_EQ(expected_efq2_point_serialize, efq2_point_serialize);
  358. }
  359. ////////////////////////////////////////////////////////////////////////
  360. // EFq2Deserialize
  361. TEST(TinySerializeTest, EFq2DeserializeWorks) {
  362. const FqElemStr serialize_fqelm = {
  363. 0x1D, 0x1E, 0x1F, 0x20, 0x19, 0x1A, 0x1B, 0x1C, 0x15, 0x16, 0x17,
  364. 0x18, 0x11, 0x12, 0x13, 0x14, 0x0D, 0x0E, 0x0F, 0x10, 0x09, 0x0A,
  365. 0x0B, 0x0C, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04};
  366. const G2ElemStr serialize_efq2_point = {{serialize_fqelm, serialize_fqelm},
  367. {serialize_fqelm, serialize_fqelm}};
  368. EccPointFq2 efq2_point = {0};
  369. const FqElem fqelm = {0x01020304, 0x05060708, 0x090A0B0C, 0x0D0E0F10,
  370. 0x11121314, 0x15161718, 0x191A1B1C, 0x1D1E1F20};
  371. const EccPointFq2 expected_efq2_point = {{fqelm, fqelm}, {fqelm, fqelm}};
  372. EFq2Deserialize(&efq2_point, &serialize_efq2_point);
  373. EXPECT_EQ(expected_efq2_point, efq2_point);
  374. }
  375. } // namespace