finitefield.h 13 KB

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