efq.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 EFq math
  17. /*! \file */
  18. #ifndef EPID_MEMBER_TINY_MATH_EFQ_H_
  19. #define EPID_MEMBER_TINY_MATH_EFQ_H_
  20. #include <stddef.h>
  21. #include "epid/common/bitsupplier.h"
  22. #include "epid/common/types.h"
  23. /// \cond
  24. typedef struct EccPointFq EccPointFq;
  25. typedef struct EccPointJacobiFq EccPointJacobiFq;
  26. typedef struct FpElem FpElem;
  27. typedef struct FqElem FqElem;
  28. /// \endcond
  29. /// Multiply two points in EFq.
  30. /*!
  31. This function is mitigated against software side-channel
  32. attacks.
  33. \param[out] result of multiplying left and right.
  34. \param[in] base The first operand to be multiplied.
  35. \param[in] exp The second operand to be multiplied.
  36. */
  37. void EFqMulSSCM(EccPointJacobiFq* result, EccPointJacobiFq const* base,
  38. FpElem const* exp);
  39. /// Exponentiate a point in EFq by an element of Fp.
  40. /*!
  41. \param[out] result target.
  42. \param[in] base the base.
  43. \param[in] exp the exponent.
  44. \returns A value different from zero (i.e., true) if on success.
  45. Zero (i.e., false) otherwise.
  46. \returns 1 on success, 0 on failure
  47. */
  48. int EFqAffineExp(EccPointFq* result, EccPointFq const* base, FpElem const* exp);
  49. /// Sum the results of exponentiating two points in EFq by elements of Fp.
  50. /*!
  51. \param[out] result target.
  52. \param[in] base0 the first base.
  53. \param[in] exp0 the first exponent.
  54. \param[in] base1 the second base.
  55. \param[in] exp1 the second exponent.
  56. \returns A value different from zero (i.e., true) if on success.
  57. Zero (i.e., false) otherwise.
  58. */
  59. int EFqAffineMultiExp(EccPointFq* result, EccPointFq const* base0,
  60. FpElem const* exp0, EccPointFq const* base1,
  61. FpElem const* exp1);
  62. /// Sum the results of exponentiating two points in EFq by elements of Fp.
  63. /*!
  64. \param[out] result target.
  65. \param[in] base0 the first base.
  66. \param[in] exp0 the first exponent.
  67. \param[in] base1 the second base.
  68. \param[in] exp1 the second exponent.
  69. \returns 1 on success, 0 on failure
  70. */
  71. void EFqMultiExp(EccPointJacobiFq* result, EccPointJacobiFq const* base0,
  72. FpElem const* exp0, EccPointJacobiFq const* base1,
  73. FpElem const* exp1);
  74. /// Add two points in EFq.
  75. /*!
  76. \param[out] result of adding left and right.
  77. \param[in] left The first operand to be added.
  78. \param[in] right The second operand to be added.
  79. \returns A value different from zero (i.e., true) if on success.
  80. Zero (i.e., false) otherwise.
  81. */
  82. int EFqAffineAdd(EccPointFq* result, EccPointFq const* left,
  83. EccPointFq const* right);
  84. /// Double a point in EFq.
  85. /*!
  86. \param[out] result target.
  87. \param[in] in the value to double.
  88. \returns A value different from zero (i.e., true) if on success.
  89. Zero (i.e., false) otherwise.
  90. */
  91. int EFqAffineDbl(EccPointFq* result, EccPointFq const* in);
  92. /// Double a point in EFq.
  93. /*!
  94. \param[out] result target.
  95. \param[in] in the value to double.
  96. */
  97. void EFqDbl(EccPointJacobiFq* result, EccPointJacobiFq const* in);
  98. /// Add two points in EFq.
  99. /*!
  100. \param[out] result of adding left and right.
  101. \param[in] left The first operand to be added.
  102. \param[in] right The second operand to be added.
  103. */
  104. void EFqAdd(EccPointJacobiFq* result, EccPointJacobiFq const* left,
  105. EccPointJacobiFq const* right);
  106. /// Generate a random point in EFq.
  107. /*!
  108. \param[in] result the random value.
  109. \param[in] rnd_func Random number generator.
  110. \param[in] rnd_param Pass through context data for rnd_func.
  111. \returns A value different from zero (i.e., true) if on success.
  112. Zero (i.e., false) otherwise.
  113. */
  114. int EFqRand(EccPointFq* result, BitSupplier rnd_func, void* rnd_param);
  115. /// Set a point's value.
  116. /*!
  117. \param[out] result target.
  118. \param[in] x value to set.
  119. \param[in] y value to set.
  120. */
  121. void EFqSet(EccPointJacobiFq* result, FqElem const* x, FqElem const* y);
  122. /// Test if a point is infinity.
  123. /*!
  124. \param[in] in the point to test.
  125. \returns A value different from zero (i.e., true) indeed
  126. the value is infinity. Zero (i.e., false) otherwise.
  127. */
  128. int EFqIsInf(EccPointJacobiFq const* in);
  129. /// Convert a point from Affine to Jacobi representation.
  130. /*!
  131. \param[out] result target.
  132. \param[in] in value to set.
  133. */
  134. void EFqFromAffine(EccPointJacobiFq* result, EccPointFq const* in);
  135. /// Convert a point from Jacobi to Affine representation.
  136. /*!
  137. \param[out] result target.
  138. \param[in] in value to set.
  139. \returns A value different from zero (i.e., true) if on success.
  140. Zero (i.e., false) otherwise.
  141. */
  142. int EFqToAffine(EccPointFq* result, EccPointJacobiFq const* in);
  143. /// Negate a point on EFq.
  144. /*!
  145. \param[out] result the negative of the element.
  146. \param[in] in the element to negate.
  147. */
  148. void EFqNeg(EccPointJacobiFq* result, EccPointJacobiFq const* in);
  149. /// Test if two points on EFq are equal
  150. /*!
  151. \param[in] left The first operand to be tested.
  152. \param[in] right The second operand to be tested.
  153. \returns A value different from zero (i.e., true) if indeed
  154. the values are equal. Zero (i.e., false) otherwise.
  155. */
  156. int EFqEq(EccPointJacobiFq const* left, EccPointJacobiFq const* right);
  157. /// Hashes an arbitrary message to a point on EFq.
  158. /*!
  159. \param[out] result target.
  160. \param[in] msg buffer to reinterpret.
  161. \param[in] len length of msg in bytes.
  162. \param[in] hashalg hash algorithm to use.
  163. \returns A value different from zero (i.e., true) if on success.
  164. Zero (i.e., false) otherwise.
  165. */
  166. int EFqHash(EccPointFq* result, unsigned char const* msg, size_t len,
  167. HashAlg hashalg);
  168. /// Copy a point's value
  169. /*!
  170. \param[out] result copy target.
  171. \param[in] in copy source.
  172. */
  173. void EFqCp(EccPointFq* result, EccPointFq const* in);
  174. /// Test if two points on EFq are equal
  175. /*!
  176. \param[in] left The first operand to be tested.
  177. \param[in] right The second operand to be tested.
  178. \returns A value different from zero (i.e., true) if indeed
  179. the values are equal. Zero (i.e., false) otherwise.
  180. */
  181. int EFqEqAffine(EccPointFq const* left, EccPointFq const* right);
  182. /// Conditionally Set a point's value to one of two values.
  183. /*!
  184. \param[out] result target.
  185. \param[in] true_val value to set if condition is true.
  186. \param[in] false_val value to set if condition is false.
  187. \param[in] truth_val value of condition.
  188. */
  189. void EFqCondSet(EccPointJacobiFq* result, EccPointJacobiFq const* true_val,
  190. EccPointJacobiFq const* false_val, int truth_val);
  191. /// Copy a point's value
  192. /*!
  193. \param[out] result copy target.
  194. \param[in] in copy source.
  195. */
  196. void EFqJCp(EccPointJacobiFq* result, EccPointJacobiFq const* in);
  197. /// Set an element's value to infinity.
  198. /*!
  199. \param[out] result element to set.
  200. */
  201. void EFqInf(EccPointJacobiFq* result);
  202. /// Test if a point is on EFq.
  203. /*!
  204. \param[in] in the point to test.
  205. \returns A value different from zero (i.e., true) indeed
  206. the point is on the curve. Zero (i.e., false) otherwise.
  207. */
  208. int EFqOnCurve(EccPointFq const* in);
  209. /// Test if a point is on EFq.
  210. /*!
  211. \param[in] in the point to test.
  212. \returns A value different from zero (i.e., true) indeed
  213. the point is on the curve. Zero (i.e., false) otherwise.
  214. */
  215. int EFqJOnCurve(EccPointJacobiFq const* in);
  216. /// Generate a random point in EFq.
  217. /*!
  218. \param[in] result the random value.
  219. \param[in] rnd_func Random number generator.
  220. \param[in] rnd_param Pass through context data for rnd_func.
  221. \returns A value different from zero (i.e., true) if on success.
  222. Zero (i.e., false) otherwise.
  223. */
  224. int EFqJRand(EccPointJacobiFq* result, BitSupplier rnd_func, void* rnd_param);
  225. #endif // EPID_MEMBER_TINY_MATH_EFQ_H_