fp.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Fp math
  17. /*! \file */
  18. #ifndef EPID_MEMBER_TINY_MATH_FP_H_
  19. #define EPID_MEMBER_TINY_MATH_FP_H_
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include "epid/common/bitsupplier.h"
  23. /// \cond
  24. typedef struct FpElem FpElem;
  25. typedef struct VeryLargeInt VeryLargeInt;
  26. /// \endcond
  27. /// Test if an element is in Fp
  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 FpInField(FpElem const* in);
  34. /// Add two elements of Fp
  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 FpAdd(FpElem* result, FpElem const* left, FpElem const* right);
  41. /// Multiply two elements of Fp.
  42. /*!
  43. \param[out] result of multiplying left and right.
  44. \param[in] left The first operand to be multiplied.
  45. \param[in] right The second operand to be multiplied.
  46. */
  47. void FpMul(FpElem* result, FpElem const* left, FpElem const* right);
  48. /// Subtract two elements of Fp.
  49. /*!
  50. \param[out] result of subtracting left from right.
  51. \param[in] left The operand to be subtracted from.
  52. \param[in] right The operand to subtract.
  53. */
  54. void FpSub(FpElem* result, FpElem const* left, FpElem const* right);
  55. /// Exponentiate an element of Fp 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 FpExp(FpElem* result, FpElem const* base, VeryLargeInt const* exp);
  62. /// Negate an element of Fp.
  63. /*!
  64. \param[out] result target.
  65. \param[in] in the value to negate.
  66. */
  67. void FpNeg(FpElem* result, FpElem const* in);
  68. /// Test if two elements in Fp are equal
  69. /*!
  70. \param[in] left The first operand to be tested.
  71. \param[in] right The second operand to be tested.
  72. \returns A value different from zero (i.e., true) if indeed
  73. the values are equal. Zero (i.e., false) otherwise.
  74. */
  75. int FpEq(FpElem const* left, FpElem const* right);
  76. /// Invert an element of Fp.
  77. /*!
  78. \param[out] result target.
  79. \param[in] in the value to invert.
  80. */
  81. void FpInv(FpElem* result, FpElem const* in);
  82. /// Generate a random element of Fp.
  83. /*!
  84. \param[in] result the random value.
  85. \param[in] rnd_func Random number generator.
  86. \param[in] rnd_param Pass through context data for rnd_func.
  87. \returns A value different from zero (i.e., true) if on success.
  88. Zero (i.e., false) otherwise.
  89. */
  90. int FpRand(FpElem* result, BitSupplier rnd_func, void* rnd_param);
  91. /// Generate a non-zero random element of Fp.
  92. /*!
  93. \param[in] result the random value.
  94. \param[in] rnd_func Random number generator.
  95. \param[in] rnd_param Pass through context data for rnd_func.
  96. \returns A value different from zero (i.e., true) if on success.
  97. Zero (i.e., false) otherwise.
  98. */
  99. int FpRandNonzero(FpElem* result, BitSupplier rnd_func, void* rnd_param);
  100. /// Clear an element of Fp.
  101. /*!
  102. \param[out] result value to clear.
  103. */
  104. void FpClear(FpElem* result);
  105. /// Set a element of Fp's value.
  106. /*!
  107. \param[out] result target.
  108. \param[in] in value to set.
  109. */
  110. void FpSet(FpElem* result, uint32_t in);
  111. /// Reinterpret a buffer as an element of Fp
  112. /*!
  113. \param[out] result target.
  114. \param[in] hash buffer to reinterpret.
  115. \param[in] len length of hash in bytes.
  116. */
  117. void FpFromHash(FpElem* result, unsigned char const* hash, size_t len);
  118. #endif // EPID_MEMBER_TINY_MATH_FP_H_