fq.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 Fq math
  17. /*! \file */
  18. #ifndef EPID_MEMBER_TINY_MATH_FQ_H_
  19. #define EPID_MEMBER_TINY_MATH_FQ_H_
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include "epid/common/bitsupplier.h"
  23. /// \cond
  24. typedef struct FqElem FqElem;
  25. typedef struct VeryLargeInt VeryLargeInt;
  26. /// \endcond
  27. /// Test if an element is in Fq.
  28. /*!
  29. \param[in] in the element to test.
  30. \returns A value different from zero (i.e., true) indeed
  31. the value is in the field. Zero (i.e., false) otherwise.
  32. */
  33. int FqInField(FqElem const* in);
  34. /// Add two elements of Fq.
  35. /*!
  36. \param[out] result of adding left and right.
  37. \param[in] left The first operand to be added.
  38. \param[in] right The second operand to be added.
  39. */
  40. void FqAdd(FqElem* result, FqElem const* left, FqElem const* right);
  41. /// Subtract two elements of Fq.
  42. /*!
  43. \param[out] result of subtracting left from right.
  44. \param[in] left The operand to be subtracted from.
  45. \param[in] right The operand to subtract.
  46. */
  47. void FqSub(FqElem* result, FqElem const* left, FqElem const* right);
  48. /// Multiply two elements of Fq.
  49. /*!
  50. \param[out] result of multiplying left and right.
  51. \param[in] left The first operand to be multiplied.
  52. \param[in] right The second operand to be multiplied.
  53. */
  54. void FqMul(FqElem* result, FqElem const* left, FqElem const* right);
  55. /// Exponentiate an element of Fq by a large integer.
  56. /*!
  57. \param[out] result target.
  58. \param[in] base the base.
  59. \param[in] exp the exponent.
  60. */
  61. void FqExp(FqElem* result, FqElem const* base, VeryLargeInt const* exp);
  62. /// Copy an element's value
  63. /*!
  64. \param[out] result copy target.
  65. \param[in] in copy source.
  66. */
  67. void FqCp(FqElem* result, FqElem const* in);
  68. /// Test if an element is zero.
  69. /*!
  70. \param[in] value the element to test.
  71. \returns A value different from zero (i.e., true) if indeed
  72. the value is zero. Zero (i.e., false) otherwise.
  73. */
  74. int FqIsZero(FqElem const* value);
  75. /// Invert an element of Fq.
  76. /*!
  77. \param[out] result the inverse of the element.
  78. \param[in] in the element to invert.
  79. */
  80. void FqInv(FqElem* result, FqElem const* in);
  81. /// Negate an element of Fq.
  82. /*!
  83. This function was formerly called as FqConst.
  84. \param[out] result the negative of the element.
  85. \param[in] in the element to negate.
  86. */
  87. void FqNeg(FqElem* result, FqElem const* in);
  88. /// Square an element of Fq.
  89. /*!
  90. \param[out] result the square of the element.
  91. \param[in] in the element to square.
  92. */
  93. void FqSquare(FqElem* result, FqElem const* in);
  94. /// Clear an element's value.
  95. /*!
  96. \param[out] result element to clear.
  97. */
  98. void FqClear(FqElem* result);
  99. /// Set an element's value.
  100. /*!
  101. \param[out] result target.
  102. \param[in] in value to set.
  103. */
  104. void FqSet(FqElem* result, uint32_t in);
  105. /// Test if two elements in Fq are equal
  106. /*!
  107. \param[in] left The first operand to be tested.
  108. \param[in] right The second operand to be tested.
  109. \returns A value different from zero (i.e., true) if indeed
  110. the values are equal. Zero (i.e., false) otherwise.
  111. */
  112. int FqEq(FqElem const* left, FqElem const* right);
  113. /// Conditionally Set an element's value to one of two values.
  114. /*!
  115. \param[out] result target.
  116. \param[in] true_val value to set if condition is true.
  117. \param[in] false_val value to set if condition is false.
  118. \param[in] truth_val value of condition.
  119. */
  120. void FqCondSet(FqElem* result, FqElem const* true_val, FqElem const* false_val,
  121. int truth_val);
  122. /// Compute the Square root of an element of Fq.
  123. /*!
  124. \param[out] result the square root of the element.
  125. \param[in] in the element to find the square root of.
  126. \returns A value different from zero (i.e., true) if the square root
  127. exists. Zero (i.e., false) otherwise.
  128. */
  129. int FqSqrt(FqElem* result, FqElem const* in);
  130. /// Generate a random element of Fq.
  131. /*!
  132. \param[in] result the random value.
  133. \param[in] rnd_func Random number generator.
  134. \param[in] rnd_param Pass through context data for rnd_func.
  135. \returns A value different from zero (i.e., true) if on success.
  136. Zero (i.e., false) otherwise.
  137. */
  138. int FqRand(FqElem* result, BitSupplier rnd_func, void* rnd_param);
  139. /// Reinterpret a buffer as an element of Fq
  140. /*!
  141. \param[out] result target.
  142. \param[in] hash buffer to reinterpret.
  143. \param[in] len length of hash in bytes.
  144. */
  145. void FqFromHash(FqElem* result, unsigned char const* hash, size_t len);
  146. #endif // EPID_MEMBER_TINY_MATH_FQ_H_