finitefield.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*############################################################################
  2. # Copyright 2016 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. /*!
  17. * \file
  18. * \brief Finite field interface.
  19. */
  20. #ifndef EPID_COMMON_MATH_FINITEFIELD_H_
  21. #define EPID_COMMON_MATH_FINITEFIELD_H_
  22. #include "epid/common/stdtypes.h"
  23. #include "epid/common/bitsupplier.h"
  24. #include "epid/common/errors.h"
  25. #include "epid/common/math/bignum.h"
  26. #include "epid/common/types.h"
  27. /// Finite field operations
  28. /*!
  29. \defgroup FiniteFieldPrimitives finitefield
  30. provides APIs for working with finite fields.
  31. Finite fields allow simple mathematical operations based on a finite set of
  32. discrete values. The results of these operations are also contained in the
  33. same set.
  34. A simple example of a finite field is all integers from zero that are less than
  35. a given value.
  36. The elements (::FfElement) of a finite field can be used in a variety of
  37. simple mathematical operations that result in elements of the same field.
  38. \ingroup EpidMath
  39. @{
  40. */
  41. /// A finite field.
  42. typedef struct FiniteField FiniteField;
  43. /// An element in a finite field.
  44. typedef struct FfElement FfElement;
  45. /// Creates new finite field.
  46. /*!
  47. Allocates memory and creates a new finite field GF(prime).
  48. Use DeleteFiniteField() to free memory.
  49. \param[in] prime
  50. The order of the finite field.
  51. \param[out] ff
  52. The newly constructed finite field.
  53. \returns ::EpidStatus
  54. \see DeleteFiniteField
  55. */
  56. EpidStatus NewFiniteField(BigNumStr const* prime, FiniteField** ff);
  57. /// Creates a new finite field using binomial extension.
  58. /*!
  59. Allocates memory and creates a finite field using binomial extension.
  60. Use DeleteFiniteField() to free memory.
  61. \param[in] ground_field
  62. The ground field.
  63. \param[in] ground_element
  64. The low-order term of the extension.
  65. \param[in] degree
  66. The degree of the extension.
  67. \param[out] ff
  68. The newly constructed finite field.
  69. \returns ::EpidStatus
  70. \see DeleteFiniteField
  71. */
  72. EpidStatus NewFiniteFieldViaBinomalExtension(FiniteField const* ground_field,
  73. FfElement const* ground_element,
  74. int degree, FiniteField** ff);
  75. /// Creates a new finite field using polynomial extension.
  76. /*!
  77. Allocates memory and creates a finite field using polynomial extension.
  78. Use DeleteFiniteField() to free memory.
  79. \note Only needed for Intel(R) EPID 1.1 verification.
  80. \param[in] ground_field
  81. The ground field.
  82. \param[in] irr_polynomial
  83. Array with coefficients of the irreducible polynomial.
  84. Number of elements must be equal to the degree of the extension.
  85. \param[in] degree
  86. The degree of the extension.
  87. \param[out] ff
  88. The newly constructed finite field.
  89. \returns ::EpidStatus
  90. \see DeleteFiniteField
  91. */
  92. EpidStatus NewFiniteFieldViaPolynomialExtension(FiniteField const* ground_field,
  93. BigNumStr const* irr_polynomial,
  94. int degree, FiniteField** ff);
  95. /// Frees a previously allocated FiniteField.
  96. /*!
  97. Frees memory pointed to by finite field. Nulls the pointer.
  98. \param[in] ff
  99. The Finite field. Can be NULL.
  100. \see NewFiniteField
  101. */
  102. void DeleteFiniteField(FiniteField** ff);
  103. /// Creates a new finite field element.
  104. /*!
  105. Allocates memory and creates a new finite field
  106. element.
  107. Use DeleteFfElement() to free memory.
  108. \param[in] ff
  109. The finite field.
  110. \param[out] new_ff_elem
  111. The Newly constructed finite field element.
  112. \returns ::EpidStatus
  113. \see NewFiniteField
  114. \see DeleteFfElement
  115. */
  116. EpidStatus NewFfElement(FiniteField const* ff, FfElement** new_ff_elem);
  117. /// Frees a previously allocated FfElement.
  118. /*!
  119. Frees memory pointed to by ff_elem. Nulls the pointer.
  120. \param[in] ff_elem
  121. The finite field element. Can be NULL.
  122. \see NewFfElement
  123. */
  124. void DeleteFfElement(FfElement** ff_elem);
  125. /// Deserializes a FfElement from a string.
  126. /*!
  127. \param[in] ff
  128. The finite field.
  129. \param[in] ff_elem_str
  130. The serialized value.
  131. \param[in] strlen
  132. The size of ff_elem_str in bytes.
  133. \param[out] ff_elem
  134. The target FfElement.
  135. \returns ::EpidStatus
  136. \see NewFfElement
  137. \see WriteFfElement
  138. */
  139. EpidStatus ReadFfElement(FiniteField* ff, void const* ff_elem_str,
  140. size_t strlen, FfElement* ff_elem);
  141. /// Initializes an existing FfElement from a BigNum.
  142. /*!
  143. \param[in] ff
  144. The finite field. Must be a Prime Field.
  145. \param[in] bn
  146. The value to read.
  147. \param[out] ff_elem
  148. The target FfElement.
  149. \returns ::EpidStatus
  150. \see NewFfElement
  151. \see WriteFfElement
  152. */
  153. EpidStatus InitFfElementFromBn(FiniteField* ff, BigNum* bn, FfElement* ff_elem);
  154. /// Serializes a finite field element to a string.
  155. /*!
  156. \param[in] ff
  157. The finite field.
  158. \param[in] ff_elem
  159. The FfElement to be serialized.
  160. \param[out] ff_elem_str
  161. The target string.
  162. \param[in] strlen
  163. The size of ff_elem_str in bytes.
  164. \returns ::EpidStatus
  165. \see NewFfElement
  166. \see FpElemStr
  167. \see FqElemStr
  168. \see GtElemStr
  169. */
  170. EpidStatus WriteFfElement(FiniteField* ff, FfElement const* ff_elem,
  171. void* ff_elem_str, size_t strlen);
  172. /// Calculates the additive inverse of a finite field element.
  173. /*!
  174. \param[in] ff
  175. The finite field.
  176. \param[in] a
  177. The element.
  178. \param[out] r
  179. The inverted element.
  180. \returns ::EpidStatus
  181. \see NewFiniteField
  182. \see NewFfElement
  183. */
  184. EpidStatus FfNeg(FiniteField* ff, FfElement const* a, FfElement* r);
  185. /// Calculates the multiplicative inverse of a finite field element.
  186. /*!
  187. \param[in] ff
  188. The finite field.
  189. \param[in] a
  190. The element.
  191. \param[out] r
  192. The inverted element.
  193. \returns ::EpidStatus
  194. \see NewFiniteField
  195. \see NewFfElement
  196. */
  197. EpidStatus FfInv(FiniteField* ff, FfElement const* a, FfElement* r);
  198. /// Adds two finite field elements.
  199. /*!
  200. \param[in] ff
  201. The finite field.
  202. \param[out] a
  203. The first operand to be added.
  204. \param[out] b
  205. The second operand to be added.
  206. \param[out] r
  207. The result of adding a and b.
  208. \returns ::EpidStatus
  209. */
  210. EpidStatus FfAdd(FiniteField* ff, FfElement const* a, FfElement const* b,
  211. FfElement* r);
  212. /// Subtracts two finite field elements.
  213. /*!
  214. \note Only needed for Intel(R) EPID 1.1 verification.
  215. \param[in] ff
  216. The finite field.
  217. \param[out] a
  218. The first operand to use in subtraction.
  219. \param[out] b
  220. The second operand to use in subtraction.
  221. \param[out] r
  222. The result of subtracting a and b.
  223. \returns ::EpidStatus
  224. */
  225. EpidStatus FfSub(FiniteField* ff, FfElement const* a, FfElement const* b,
  226. FfElement* r);
  227. /// Multiplies two finite field elements.
  228. /*!
  229. \param[in] ff
  230. The finite field.
  231. \param[out] a
  232. The first operand to be multplied.
  233. \param[out] b
  234. The second operand to be multiplied. If ff is an extension field of a
  235. field F then this parameter may be an element of either ff or F.
  236. \param[out] r
  237. The result of multiplying a and b.
  238. \returns ::EpidStatus
  239. \see NewFiniteField
  240. \see NewFfElement
  241. */
  242. EpidStatus FfMul(FiniteField* ff, FfElement const* a, FfElement const* b,
  243. FfElement* r);
  244. /// Checks if given finite field element is the additive identity (zero).
  245. /*!
  246. \param[in] ff
  247. The finite field.
  248. \param[out] a
  249. The element.
  250. \param[out] is_zero
  251. The result of the check.
  252. \returns ::EpidStatus
  253. \see NewFiniteField
  254. \see NewFfElement
  255. */
  256. EpidStatus FfIsZero(FiniteField* ff, FfElement const* a, bool* is_zero);
  257. /// Raises an element of a finite field to a power.
  258. /*!
  259. \param[in] ff
  260. The finite field in which to perform the operation
  261. \param[in] a
  262. The base.
  263. \param[in] b
  264. The power.
  265. \param[out] r
  266. The result of raising a to the power b.
  267. \returns ::EpidStatus
  268. \see NewFiniteField
  269. \see NewFfElement
  270. */
  271. EpidStatus FfExp(FiniteField* ff, FfElement const* a, BigNum const* b,
  272. FfElement* r);
  273. /// Multi-exponentiates finite field elements.
  274. /*!
  275. Calculates FfExp(p[0],b[0]) * ... * FfExp(p[m-1],b[m-1]) for m > 1
  276. \param[in] ff
  277. The finite field in which to perform the operation
  278. \param[in] a
  279. The bases.
  280. \param[in] b
  281. The powers.
  282. \param[in] m
  283. Number of entries in a and b.
  284. \param[out] r
  285. The result of raising each a to the corresponding power b and multiplying
  286. the results.
  287. \returns ::EpidStatus
  288. \see NewFiniteField
  289. \see NewFfElement
  290. */
  291. EpidStatus FfMultiExp(FiniteField* ff, FfElement const** a, BigNumStr const** b,
  292. size_t m, FfElement* r);
  293. /// Multi-exponentiates finite field elements.
  294. /*!
  295. Calculates FfExp(p[0],b[0]) * ... * FfExp(p[m-1],b[m-1]) for m > 1
  296. \param[in] ff
  297. The finite field in which to perform the operation
  298. \param[in] a
  299. The bases.
  300. \param[in] b
  301. The powers.
  302. \param[in] m
  303. Number of entries in a and b.
  304. \param[out] r
  305. The result of raising each a to the corresponding power b and multiplying
  306. the results.
  307. \returns ::EpidStatus
  308. \see NewFiniteField
  309. \see NewFfElement
  310. */
  311. EpidStatus FfMultiExpBn(FiniteField* ff, FfElement const** a, BigNum const** b,
  312. size_t m, FfElement* r);
  313. /// Software side-channel mitigated implementation of FfMultiExp.
  314. /*!
  315. Calculates FfExp(p[0],b[0]) * ... * FfExp(p[m-1],b[m-1]) for m > 1
  316. \attention
  317. The reference implementation of FfSscmMultiExp calls FfMultiExp
  318. directly because the implementation of FfMultiExp is already side channel
  319. mitigated. Implementers providing their own versions of this function are
  320. responsible for ensuring that FfSscmMultiExp is side channel mitigated per
  321. section 8 of the Intel(R) EPID 2.0 spec.
  322. \param[in] ff
  323. The finite field in which to perform the operation.
  324. \param[in] a
  325. The bases.
  326. \param[in] b
  327. The powers.
  328. \param[in] m
  329. Number of entries in a and b.
  330. \param[out] r
  331. The result of raising each a to the corresponding power b and multiplying
  332. the results.
  333. \returns ::EpidStatus
  334. \see NewFiniteField
  335. \see NewFfElement
  336. */
  337. EpidStatus FfSscmMultiExp(FiniteField* ff, FfElement const** a,
  338. BigNumStr const** b, size_t m, FfElement* r);
  339. /// Checks if two finite field elements are equal.
  340. /*!
  341. \param[in] ff
  342. The finite field.
  343. \param[in] a
  344. An element to check.
  345. \param[in] b
  346. Another element to check.
  347. \param[out] is_equal
  348. The result of the check.
  349. \returns ::EpidStatus
  350. \see NewEcGroup
  351. \see NewEcPoint
  352. */
  353. EpidStatus FfIsEqual(FiniteField* ff, FfElement const* a, FfElement const* b,
  354. bool* is_equal);
  355. /// Hashes an arbitrary message to an element in a finite field.
  356. /*!
  357. \param[in] ff
  358. The finite field.
  359. \param[in] msg
  360. The message.
  361. \param[in] msg_len
  362. The size of msg in bytes.
  363. \param[in] hash_alg
  364. The hash algorithm.
  365. \param[out] r
  366. The hashed value.
  367. \returns ::EpidStatus
  368. \see NewFiniteField
  369. \see NewFfElement
  370. */
  371. EpidStatus FfHash(FiniteField* ff, void const* msg, size_t msg_len,
  372. HashAlg hash_alg, FfElement* r);
  373. /// Generate random finite field element.
  374. /*!
  375. \param[in] ff
  376. The finite field associated with the random finite field element.
  377. \param[in] low_bound
  378. Lower bound of the random finite field to be generated.
  379. \param[in] rnd_func
  380. Random number generator.
  381. \param[in] rnd_param
  382. Pass through context data for rnd_func.
  383. \param[in,out] r
  384. The random finite field element.
  385. \returns ::EpidStatus
  386. \retval ::kEpidRandMaxIterErr the function should be called again with
  387. different random data.
  388. \see NewFfElement
  389. \see BitSupplier
  390. */
  391. EpidStatus FfGetRandom(FiniteField* ff, BigNumStr const* low_bound,
  392. BitSupplier rnd_func, void* rnd_param, FfElement* r);
  393. /// Finds a square root of a finite field element.
  394. /*!
  395. This function calculates the square root by the method of false position.
  396. \param[in] ff
  397. The finite field in which to perform the operation
  398. \param[in] a
  399. The bases.
  400. \param[out] r
  401. The result of raising each a to the corresponding power b and multiplying
  402. the results.
  403. \retval kEpidMathQuadraticNonResidueError No square root could be found.
  404. \returns ::EpidStatus
  405. \see NewFiniteField
  406. \see NewFfElement
  407. */
  408. EpidStatus FfSqrt(FiniteField* ff, FfElement const* a, FfElement* r);
  409. /*!
  410. @}
  411. */
  412. #endif // EPID_COMMON_MATH_FINITEFIELD_H_