vli.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 Large Integer math
  17. /*! \file */
  18. #ifndef EPID_MEMBER_TINY_MATH_VLI_H_
  19. #define EPID_MEMBER_TINY_MATH_VLI_H_
  20. #include <stdint.h>
  21. #include "epid/common/bitsupplier.h"
  22. /// \cond
  23. typedef struct VeryLargeInt VeryLargeInt;
  24. typedef struct VeryLargeIntProduct VeryLargeIntProduct;
  25. /// \endcond
  26. /// Add two large integers.
  27. /*!
  28. \param[out] result target.
  29. \param[in] left The first operand to be added.
  30. \param[in] right The second operand to be added.
  31. \returns the carry portion of the addition.
  32. */
  33. uint32_t VliAdd(VeryLargeInt* result, VeryLargeInt const* left,
  34. VeryLargeInt const* right);
  35. /// Multiply two large integers.
  36. /*!
  37. \param[out] result of multiplying left and right.
  38. \param[in] left The first operand to be multiplied.
  39. \param[in] right The second operand to be multiplied.
  40. */
  41. void VliMul(VeryLargeIntProduct* result, VeryLargeInt const* left,
  42. VeryLargeInt const* right);
  43. /// Right shift a large integers.
  44. /*!
  45. \param[out] result target.
  46. \param[in] in The value to be shifted.
  47. \param[in] shift The number of bits to shift.
  48. */
  49. void VliRShift(VeryLargeInt* result, VeryLargeInt const* in, uint32_t shift);
  50. /// Subtract two large integers.
  51. /*!
  52. \param[out] result target.
  53. \param[in] left The operand to be subtracted from.
  54. \param[in] right The operand to subtract.
  55. \returns 1 on success, 0 on failure
  56. */
  57. uint32_t VliSub(VeryLargeInt* result, VeryLargeInt const* left,
  58. VeryLargeInt const* right);
  59. /// Set a large integer's value.
  60. /*!
  61. \param[out] result target.
  62. \param[in] in value to set.
  63. */
  64. void VliSet(VeryLargeInt* result, VeryLargeInt const* in);
  65. /// Clear a large integer's value.
  66. /*!
  67. \param[out] result value to clear.
  68. */
  69. void VliClear(VeryLargeInt* result);
  70. /// Test if a large integer is zero.
  71. /*!
  72. \param[in] in the value to test.
  73. \returns A value different from zero (i.e., true) if indeed
  74. the value is zero. Zero (i.e., false) otherwise.
  75. */
  76. int VliIsZero(VeryLargeInt const* in);
  77. /// Conditionally Set a large inter's value to one of two values.
  78. /*!
  79. \param[out] result target.
  80. \param[in] true_val value to set if condition is true.
  81. \param[in] false_val value to set if condition is false.
  82. \param[in] truth_val value of condition.
  83. */
  84. void VliCondSet(VeryLargeInt* result, VeryLargeInt const* true_val,
  85. VeryLargeInt const* false_val, int truth_val);
  86. /// Test the value of a bit in a large integer.
  87. /*!
  88. \param[in] in the value to test.
  89. \param[in] bit the bit index.
  90. \returns value of the bit (1 or 0).
  91. */
  92. uint32_t VliTestBit(VeryLargeInt const* in, uint32_t bit);
  93. /// Generate a random large integer.
  94. /*!
  95. \param[in] result the random value.
  96. \param[in] rnd_func Random number generator.
  97. \param[in] rnd_param Pass through context data for rnd_func.
  98. \returns A value different from zero (i.e., true) if on success.
  99. Zero (i.e., false) otherwise.
  100. */
  101. int VliRand(VeryLargeInt* result, BitSupplier rnd_func, void* rnd_param);
  102. /// compare two large integers.
  103. /*!
  104. \param[in] left the left hand value.
  105. \param[in] right the right hand value.
  106. \returns the sign of left - right
  107. */
  108. int VliCmp(VeryLargeInt const* left, VeryLargeInt const* right);
  109. /// Add two large integers modulo a value.
  110. /*!
  111. \param[out] result target.
  112. \param[in] left The first operand to be added.
  113. \param[in] right The second operand to be added.
  114. \param[in] mod The modulo.
  115. */
  116. void VliModAdd(VeryLargeInt* result, VeryLargeInt const* left,
  117. VeryLargeInt const* right, VeryLargeInt const* mod);
  118. /// Subtract two large integers modulo a value.
  119. /*!
  120. \param[out] result target.
  121. \param[in] left The operand to be subtracted from.
  122. \param[in] right The operand to subtract.
  123. \param[in] mod The modulo.
  124. */
  125. void VliModSub(VeryLargeInt* result, VeryLargeInt const* left,
  126. VeryLargeInt const* right, VeryLargeInt const* mod);
  127. /// Multiply two large integers modulo a value.
  128. /*!
  129. \param[out] result target.
  130. \param[in] left The first operand to be multiplied.
  131. \param[in] right The second operand to be multiplied.
  132. \param[in] mod The modulo.
  133. */
  134. void VliModMul(VeryLargeInt* result, VeryLargeInt const* left,
  135. VeryLargeInt const* right, VeryLargeInt const* mod);
  136. /// Exponentiate a large integer modulo a value.
  137. /*!
  138. \param[out] result target.
  139. \param[in] base the base.
  140. \param[in] exp the exponent.
  141. \param[in] mod The modulo.
  142. */
  143. void VliModExp(VeryLargeInt* result, VeryLargeInt const* base,
  144. VeryLargeInt const* exp, VeryLargeInt const* mod);
  145. /// Invert a large integer modulo a value.
  146. /*!
  147. \param[out] result target.
  148. \param[in] input the value to invert.
  149. \param[in] mod The modulo.
  150. */
  151. void VliModInv(VeryLargeInt* result, VeryLargeInt const* input,
  152. VeryLargeInt const* mod);
  153. /// Square a large integer modulo a value.
  154. /*!
  155. \param[out] result target.
  156. \param[in] input the base.
  157. \param[in] mod The modulo.
  158. */
  159. void VliModSquare(VeryLargeInt* result, VeryLargeInt const* input,
  160. VeryLargeInt const* mod);
  161. /// Reduce a value to a modulo.
  162. /*!
  163. \param[out] result target.
  164. \param[in] input the base.
  165. \param[in] mod The modulo.
  166. \warning This function makes significant assumptions about
  167. the range of values input
  168. */
  169. void VliModBarrett(VeryLargeInt* result, VeryLargeIntProduct const* input,
  170. VeryLargeInt const* mod);
  171. #endif // EPID_MEMBER_TINY_MATH_VLI_H_