You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

330 lines
9.5 KiB

  1. /* Copyright (c) 2015 Cryptography Research, Inc.
  2. * Released under the MIT License. See LICENSE.txt for license information.
  3. */
  4. /**
  5. * @file decaf.h
  6. * @author Mike Hamburg
  7. * @brief A group of prime order p.
  8. *
  9. * The Decaf library implements cryptographic operations on a an elliptic curve
  10. * group of prime order p. It accomplishes this by using a twisted Edwards
  11. * curve (isogenous to Ed448-Goldilocks) and wiping out the cofactor.
  12. *
  13. * The formulas are all complete and have no special cases, except that
  14. * decaf_decode can fail because not every sequence of bytes is a valid group
  15. * element.
  16. *
  17. * The formulas contain no data-dependent branches, timing or memory accesses.
  18. */
  19. #ifndef __DECAF_H__
  20. #define __DECAF_H__ 1
  21. #include <stdint.h>
  22. /* Goldilocks' build flags default to hidden and stripping executables. */
  23. #define API_VIS __attribute__((visibility("default")))
  24. #define WARN_UNUSED __attribute__((warn_unused_result))
  25. #define NONNULL1 __attribute__((nonnull(1)))
  26. #define NONNULL2 __attribute__((nonnull(1,2)))
  27. #define NONNULL3 __attribute__((nonnull(1,2,3)))
  28. typedef uint64_t decaf_word_t, decaf_bool_t;
  29. /* TODO: prefix all these operations and factor to support multiple curves. */
  30. /* TODO: perfield, so when 25519 hits this will change */
  31. #define DECAF_FIELD_BITS 448
  32. #define DECAF_LIMBS (1 + (512-1)/8/sizeof(decaf_word_t))
  33. #define DECAF_SCALAR_LIMBS (1 + (448-3)/8/sizeof(decaf_word_t))
  34. /** Number of bytes in a serialized point. One less bit than you'd think. */
  35. #define DECAF_SER_BYTES ((DECAF_FIELD_BITS+6)/8)
  36. /** Number of bytes in a serialized scalar. Two less bits than you'd think. */
  37. #define DECAF_SCALAR_BYTES ((DECAF_FIELD_BITS+5)/8)
  38. /** Twisted Edwards (-1,d-1) extended homogeneous coordinates */
  39. typedef struct decaf_point_s {
  40. decaf_word_t x[DECAF_LIMBS],y[DECAF_LIMBS],z[DECAF_LIMBS],t[DECAF_LIMBS];
  41. } decaf_point_t[1];
  42. typedef struct decaf_scalar_s {
  43. decaf_word_t limb[DECAF_SCALAR_LIMBS];
  44. } decaf_scalar_t[1];
  45. static const decaf_bool_t DECAF_TRUE = -(decaf_bool_t)1, DECAF_FALSE = 0;
  46. /** NB Success is -1, failure is 0. TODO: see if people would rather the reverse. */
  47. static const decaf_bool_t DECAF_SUCCESS = -(decaf_bool_t)1 /*DECAF_TRUE*/,
  48. DECAF_FAILURE = 0 /*DECAF_FALSE*/;
  49. /** The identity point on the curve. */
  50. const decaf_point_t decaf_identity API_VIS;
  51. /** The prime p, for debugging purposes.
  52. * FIXME: prevent this scalar from actually being used for non-debugging purposes?
  53. */
  54. const decaf_scalar_t decaf_scalar_p API_VIS;
  55. /** A scalar equal to 1. */
  56. const decaf_scalar_t decaf_scalar_one API_VIS;
  57. /** A scalar equal to 0. */
  58. const decaf_scalar_t decaf_scalar_zero API_VIS;
  59. /** An arbitrarily chosen base point on the curve. TODO: define */
  60. const decaf_point_t decaf_basepoint API_VIS;
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64. // TODO: ser, deser, inv?.
  65. // FIXME: scalar math is untested, and therefore probably wrong.
  66. /**
  67. * @brief Read a scalar from wire format or from bytes.
  68. *
  69. * Return DECAF_SUCCESS if the scalar was in reduced form. This
  70. * function is not WARN_UNUSED because eg challenges in signatures
  71. * may need to be longer.
  72. *
  73. * TODO: create a decode long function.
  74. *
  75. * @param [in] ser Serialized form of a scalar.
  76. * @param [out] out Deserialized form.
  77. */
  78. decaf_bool_t decaf_decode_scalar(
  79. decaf_scalar_t s,
  80. const unsigned char ser[DECAF_SER_BYTES]
  81. ) API_VIS NONNULL2;
  82. /**
  83. * @brief Serialize a scalar to wire format.
  84. *
  85. * @param [out] ser Serialized form of a scalar.
  86. * @param [in] s Deserialized scalar.
  87. */
  88. void decaf_encode_scalar(
  89. unsigned char ser[DECAF_SER_BYTES],
  90. const decaf_scalar_t s
  91. ) API_VIS NONNULL2;
  92. /**
  93. * @brief Add two scalars. The scalars may use the same memory.
  94. * @param [in] a One scalar.
  95. * @param [in] b Another scalar.
  96. * @param [out] out a+b.
  97. */
  98. void decaf_add_scalars (
  99. decaf_scalar_t out,
  100. const decaf_scalar_t a,
  101. const decaf_scalar_t b
  102. ) API_VIS NONNULL3;
  103. /**
  104. * @brief Compare two scalars.
  105. * @param [in] a One scalar.
  106. * @param [in] b Another scalar.
  107. * @retval DECAF_TRUE The scalars are equal.
  108. * @retval DECAF_FALSE The scalars are not equal.
  109. */
  110. decaf_bool_t decaf_eq_scalars (
  111. const decaf_scalar_t a,
  112. const decaf_scalar_t b
  113. ) API_VIS WARN_UNUSED NONNULL2;
  114. /**
  115. * @brief Subtract two scalars. The scalars may use the same memory.
  116. * @param [in] a One scalar.
  117. * @param [in] b Another scalar.
  118. * @param [out] out a-b.
  119. */
  120. void decaf_sub_scalars (
  121. decaf_scalar_t out,
  122. const decaf_scalar_t a,
  123. const decaf_scalar_t b
  124. ) API_VIS NONNULL3;
  125. /**
  126. * @brief Multiply two scalars. The scalars may use the same memory.
  127. * @param [in] a One scalar.
  128. * @param [in] b Another scalar.
  129. * @param [out] out a*b.
  130. */
  131. void decaf_mul_scalars (
  132. decaf_scalar_t out,
  133. const decaf_scalar_t a,
  134. const decaf_scalar_t b
  135. ) API_VIS NONNULL3;
  136. /**
  137. * @brief Encode a point as a sequence of bytes.
  138. *
  139. * @param [out] ser The byte representation of the point.
  140. * @param [in] pt The point to encode.
  141. */
  142. void decaf_encode (
  143. uint8_t ser[DECAF_SER_BYTES],
  144. const decaf_point_t pt
  145. ) API_VIS NONNULL2;
  146. /**
  147. * @brief Decode a point from a sequence of bytes.
  148. *
  149. * Every point has a unique encoding, so not every
  150. * sequence of bytes is a valid encoding. If an invalid
  151. * encoding is given, the output is undefined.
  152. *
  153. * @param [out] pt The decoded point.
  154. * @param [in] ser The serialized version of the point.
  155. * @retval DECAF_SUCCESS The decoding succeeded.
  156. * @retval DECAF_FAILURE The decoding didn't succeed, because
  157. * ser does not represent a point.
  158. */
  159. decaf_bool_t decaf_decode (
  160. decaf_point_t pt,
  161. const uint8_t ser[DECAF_SER_BYTES],
  162. decaf_bool_t allow_identity
  163. ) API_VIS WARN_UNUSED NONNULL2;
  164. /**
  165. * @brief Copy a point. The input and output may alias,
  166. * in which case this function does nothing.
  167. *
  168. * @param [out] a A copy of the point.
  169. * @param [in] b Any point.
  170. */
  171. void decaf_copy (
  172. decaf_point_t a,
  173. const decaf_point_t b
  174. ) API_VIS NONNULL2;
  175. /**
  176. * @brief Test whether two points are equal. If yes, return
  177. * DECAF_TRUE, else return DECAF_FALSE.
  178. *
  179. * @param [in] a A point.
  180. * @param [in] b Another point.
  181. * @retval DECAF_TRUE The points are equal.
  182. * @retval DECAF_FALSE The points are not equal.
  183. */
  184. decaf_bool_t decaf_eq (
  185. const decaf_point_t a,
  186. const decaf_point_t b
  187. ) API_VIS WARN_UNUSED NONNULL2;
  188. /**
  189. * @brief Add two points to produce a third point. The
  190. * input points and output point can be pointers to the same
  191. * memory.
  192. *
  193. * @param [out] sum The sum a+b.
  194. * @param [in] a An addend.
  195. * @param [in] b An addend.
  196. */
  197. void decaf_add (
  198. decaf_point_t sum,
  199. const decaf_point_t a,
  200. const decaf_point_t b
  201. ) API_VIS NONNULL3;
  202. /**
  203. * @brief Subtract two points to produce a third point. The
  204. * input points and output point can be pointers to the same
  205. * memory.
  206. *
  207. * @param [out] sum The difference a-b.
  208. * @param [in] a The minuend.
  209. * @param [in] b The subtrahend.
  210. */
  211. void decaf_sub (
  212. decaf_point_t diff,
  213. const decaf_point_t a,
  214. const decaf_point_t b
  215. ) API_VIS NONNULL3;
  216. /**
  217. * @brief Multiply a base point by a scalar.
  218. *
  219. * @param [out] scaled The scaled point base*scalar
  220. * @param [in] base The point to be scaled.
  221. * @param [in] scalar The scalar to multilpy by.
  222. * @param [in] scalar_words The number of words in the scalar [TODO]
  223. */
  224. void decaf_scalarmul (
  225. decaf_point_t scaled,
  226. const decaf_point_t base,
  227. const decaf_word_t *scalar,
  228. unsigned int scalar_words
  229. ) API_VIS NONNULL3;
  230. /**
  231. * @brief Test that a point is valid, for debugging purposes.
  232. *
  233. * @param [in] point The number to test.
  234. * @retval DECAF_TRUE The point is valid.
  235. * @retval DECAF_FALSE The point is invalid.
  236. */
  237. decaf_bool_t decaf_valid (
  238. const decaf_point_t toTest
  239. ) API_VIS WARN_UNUSED NONNULL1;
  240. /**
  241. * @brief Almost-Elligator-like hash to curve.
  242. *
  243. * Call this function with the output of a hash to make a hash to the curve.
  244. *
  245. * This function runs Elligator2 on the decaf Jacobi quartic model. It then
  246. * uses the isogeny to put the result in twisted Edwards form. As a result,
  247. * it is safe (cannot produce points of order 4), and would be compatible with
  248. * hypothetical other implementations of Decaf using a Montgomery or untwisted
  249. * Edwards model.
  250. *
  251. * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
  252. * A factor of 2 due to the isogeny.
  253. * A factor of 2 because we quotient out the 2-torsion.
  254. * // TODO: check that it isn't more, especially for the identity point.
  255. *
  256. * Negating the input (mod q) results in the same point. Inverting the input
  257. * (mod q) results in the negative point. This is the same as Elligator.
  258. *
  259. * This function isn't quite indifferentiable from a random oracle.
  260. * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
  261. * Furthermore, calling it twice with independent seeds and adding the results
  262. * is indifferentiable from a random oracle.
  263. *
  264. * @param [in] hashed_data Output of some hash function.
  265. * @param [out] pt The data hashed to the curve.
  266. */
  267. void decaf_nonuniform_map_to_curve (
  268. decaf_point_t pt,
  269. const unsigned char hashed_data[DECAF_SER_BYTES]
  270. ) API_VIS NONNULL2;
  271. /**
  272. * @brief Indifferentiable hash function encoding to curve.
  273. *
  274. * Equivalent to calling decaf_nonuniform_map_to_curve twice and adding.
  275. *
  276. * @param [in] hashed_data Output of some hash function.
  277. * @param [out] pt The data hashed to the curve.
  278. */
  279. void decaf_uniform_map_to_curve (
  280. decaf_point_t pt,
  281. const unsigned char hashed_data[2*DECAF_SER_BYTES]
  282. ) API_VIS NONNULL2;
  283. #undef API_VIS
  284. #undef WARN_UNUSED
  285. #undef NONNULL2
  286. #undef NONNULL3
  287. #ifdef __cplusplus
  288. }; /* extern "C" */
  289. #endif
  290. #endif /* __DECAF_H__ */