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.
 
 
 
 
 

389 lines
11 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. /** @cond internal */
  24. #if defined(DOXYGEN) && !defined(__attribute__)
  25. #define __attribute__((x))
  26. #endif
  27. #define API_VIS __attribute__((visibility("default")))
  28. #define WARN_UNUSED __attribute__((warn_unused_result))
  29. #define NONNULL1 __attribute__((nonnull(1)))
  30. #define NONNULL2 __attribute__((nonnull(1,2)))
  31. #define NONNULL3 __attribute__((nonnull(1,2,3)))
  32. #define NONNULL5 __attribute__((nonnull(1,2,3,4,5)))
  33. /** @endcond */
  34. /** Types of internal words. TODO: ARCH: make 32-bit clean */
  35. typedef uint64_t decaf_word_t, decaf_bool_t;
  36. /* TODO: prefix all these operations and factor to support multiple curves. */
  37. /* TODO: perfield, so when 25519 hits this will change */
  38. #define DECAF_FIELD_BITS 448
  39. #define DECAF_LIMBS 8
  40. #define DECAF_SCALAR_BITS 446
  41. #define DECAF_SCALAR_LIMBS (1 + (DECAF_SCALAR_BITS-1)/8/sizeof(decaf_word_t))
  42. /** Number of bytes in a serialized point. One less bit than you'd think. */
  43. #define DECAF_SER_BYTES ((DECAF_FIELD_BITS+6)/8)
  44. /** Number of bytes in a serialized scalar. Two less bits than you'd think. */
  45. #define DECAF_SCALAR_BYTES ((DECAF_FIELD_BITS+5)/8)
  46. /** Twisted Edwards (-1,d-1) extended homogeneous coordinates */
  47. typedef struct decaf_point_s {
  48. decaf_word_t x[DECAF_LIMBS],y[DECAF_LIMBS],z[DECAF_LIMBS],t[DECAF_LIMBS];
  49. } decaf_point_t[1];
  50. /** Scalar is stored packed, because we don't need the speed. */
  51. typedef struct decaf_scalar_s {
  52. decaf_word_t limb[DECAF_SCALAR_LIMBS];
  53. } decaf_scalar_t[1];
  54. /** DECAF_TRUE = -1 so that DECAF_TRUE & x = x */
  55. static const decaf_bool_t DECAF_TRUE = -(decaf_bool_t)1, DECAF_FALSE = 0;
  56. /** NB Success is -1, failure is 0. TODO: see if people would rather the reverse. */
  57. static const decaf_bool_t DECAF_SUCCESS = -(decaf_bool_t)1 /*DECAF_TRUE*/,
  58. DECAF_FAILURE = 0 /*DECAF_FALSE*/;
  59. /** The prime p, for debugging purposes.
  60. * TODO: prevent this scalar from actually being used for non-debugging purposes?
  61. */
  62. const decaf_scalar_t decaf_scalar_p API_VIS;
  63. /** A scalar equal to 1. */
  64. const decaf_scalar_t decaf_scalar_one API_VIS;
  65. /** A scalar equal to 0. */
  66. const decaf_scalar_t decaf_scalar_zero API_VIS;
  67. /** The identity point on the curve. */
  68. const decaf_point_t decaf_point_identity API_VIS;
  69. /**
  70. * An arbitrarily chosen base point on the curve.
  71. * Equal to Ed448-Goldilocks base point defined by DJB, except of course that
  72. * it's on the twist in this case. TODO: choose a base point with nice encoding?
  73. */
  74. const decaf_point_t decaf_point_base API_VIS;
  75. #ifdef __cplusplus
  76. extern "C" {
  77. #endif
  78. /* TODO: scalar invert? */
  79. /**
  80. * @brief Read a scalar from wire format or from bytes.
  81. *
  82. * Return DECAF_SUCCESS if the scalar was in reduced form. This
  83. * function is not WARN_UNUSED because eg challenges in signatures
  84. * may need to be longer.
  85. *
  86. * TODO: create a decode long function, and make this WARN_UNUSED.
  87. *
  88. * @param [in] ser Serialized form of a scalar.
  89. * @param [out] out Deserialized form.
  90. */
  91. decaf_bool_t decaf_scalar_decode (
  92. decaf_scalar_t s,
  93. const unsigned char ser[DECAF_SCALAR_BYTES]
  94. ) API_VIS NONNULL2;
  95. /**
  96. * @brief Serialize a scalar to wire format.
  97. *
  98. * @param [out] ser Serialized form of a scalar.
  99. * @param [in] s Deserialized scalar.
  100. */
  101. void decaf_scalar_encode (
  102. unsigned char ser[DECAF_SCALAR_BYTES],
  103. const decaf_scalar_t s
  104. ) API_VIS NONNULL2;
  105. /**
  106. * @brief Add two scalars. The scalars may use the same memory.
  107. * @param [in] a One scalar.
  108. * @param [in] b Another scalar.
  109. * @param [out] out a+b.
  110. */
  111. void decaf_scalar_add (
  112. decaf_scalar_t out,
  113. const decaf_scalar_t a,
  114. const decaf_scalar_t b
  115. ) API_VIS NONNULL3;
  116. /**
  117. * @brief Compare two scalars.
  118. * @param [in] a One scalar.
  119. * @param [in] b Another scalar.
  120. * @retval DECAF_TRUE The scalars are equal.
  121. * @retval DECAF_FALSE The scalars are not equal.
  122. */
  123. decaf_bool_t decaf_scalar_eq (
  124. const decaf_scalar_t a,
  125. const decaf_scalar_t b
  126. ) API_VIS WARN_UNUSED NONNULL2;
  127. /**
  128. * @brief Subtract two scalars. The scalars may use the same memory.
  129. * @param [in] a One scalar.
  130. * @param [in] b Another scalar.
  131. * @param [out] out a-b.
  132. */
  133. void decaf_scalar_sub (
  134. decaf_scalar_t out,
  135. const decaf_scalar_t a,
  136. const decaf_scalar_t b
  137. ) API_VIS NONNULL3;
  138. /**
  139. * @brief Multiply two scalars. The scalars may use the same memory.
  140. * @param [in] a One scalar.
  141. * @param [in] b Another scalar.
  142. * @param [out] out a*b.
  143. */
  144. void decaf_scalar_mul (
  145. decaf_scalar_t out,
  146. const decaf_scalar_t a,
  147. const decaf_scalar_t b
  148. ) API_VIS NONNULL3;
  149. /**
  150. * @brief Copy a scalar. The scalars may use the same memory, in which
  151. * case this function does nothing.
  152. * @param [in] a A scalar.
  153. * @param [out] out Will become a copy of a.
  154. */
  155. void decaf_scalar_copy (
  156. decaf_scalar_t out,
  157. const decaf_scalar_t a
  158. ) API_VIS NONNULL2;
  159. /**
  160. * @brief Encode a point as a sequence of bytes.
  161. *
  162. * @param [out] ser The byte representation of the point.
  163. * @param [in] pt The point to encode.
  164. */
  165. void decaf_point_encode (
  166. uint8_t ser[DECAF_SER_BYTES],
  167. const decaf_point_t pt
  168. ) API_VIS NONNULL2;
  169. /**
  170. * @brief Decode a point from a sequence of bytes.
  171. *
  172. * Every point has a unique encoding, so not every
  173. * sequence of bytes is a valid encoding. If an invalid
  174. * encoding is given, the output is undefined.
  175. *
  176. * @param [out] pt The decoded point.
  177. * @param [in] ser The serialized version of the point.
  178. * @retval DECAF_SUCCESS The decoding succeeded.
  179. * @retval DECAF_FAILURE The decoding didn't succeed, because
  180. * ser does not represent a point.
  181. */
  182. decaf_bool_t decaf_point_decode (
  183. decaf_point_t pt,
  184. const uint8_t ser[DECAF_SER_BYTES],
  185. decaf_bool_t allow_identity
  186. ) API_VIS WARN_UNUSED NONNULL2;
  187. /**
  188. * @brief Copy a point. The input and output may alias,
  189. * in which case this function does nothing.
  190. *
  191. * @param [out] a A copy of the point.
  192. * @param [in] b Any point.
  193. */
  194. void decaf_point_copy (
  195. decaf_point_t a,
  196. const decaf_point_t b
  197. ) API_VIS NONNULL2;
  198. /**
  199. * @brief Test whether two points are equal. If yes, return
  200. * DECAF_TRUE, else return DECAF_FALSE.
  201. *
  202. * @param [in] a A point.
  203. * @param [in] b Another point.
  204. * @retval DECAF_TRUE The points are equal.
  205. * @retval DECAF_FALSE The points are not equal.
  206. */
  207. decaf_bool_t decaf_point_eq (
  208. const decaf_point_t a,
  209. const decaf_point_t b
  210. ) API_VIS WARN_UNUSED NONNULL2;
  211. /**
  212. * @brief Add two points to produce a third point. The
  213. * input points and output point can be pointers to the same
  214. * memory.
  215. *
  216. * @param [out] sum The sum a+b.
  217. * @param [in] a An addend.
  218. * @param [in] b An addend.
  219. */
  220. void decaf_point_add (
  221. decaf_point_t sum,
  222. const decaf_point_t a,
  223. const decaf_point_t b
  224. ) API_VIS NONNULL3;
  225. /**
  226. * @brief Double a point. Equivalent to
  227. * decaf_point_add(two_a,a,a), but potentially faster.
  228. *
  229. * @param [out] sum The sum a+a.
  230. * @param [in] a A point.
  231. */
  232. void decaf_point_double (
  233. decaf_point_t two_a,
  234. const decaf_point_t a
  235. ) API_VIS NONNULL2;
  236. /**
  237. * @brief Subtract two points to produce a third point. The
  238. * input points and output point can be pointers to the same
  239. * memory.
  240. *
  241. * @param [out] sum The difference a-b.
  242. * @param [in] a The minuend.
  243. * @param [in] b The subtrahend.
  244. */
  245. void decaf_point_sub (
  246. decaf_point_t diff,
  247. const decaf_point_t a,
  248. const decaf_point_t b
  249. ) API_VIS NONNULL3;
  250. /**
  251. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  252. *
  253. * @param [out] scaled The scaled point base*scalar
  254. * @param [in] base The point to be scaled.
  255. * @param [in] scalar The scalar to multiply by.
  256. */
  257. void decaf_point_scalarmul (
  258. decaf_point_t scaled,
  259. const decaf_point_t base,
  260. const decaf_scalar_t scalar
  261. ) API_VIS NONNULL3;
  262. /**
  263. * @brief Multiply two base points by two scalars:
  264. * scaled = scalar1*base1 + scalar2*base2.
  265. *
  266. * Equivalent to two calls to decaf_point_scalarmul, but may be
  267. * faster.
  268. *
  269. * @param [out] scaled The scaled point base*scalar
  270. * @param [in] base1 A first point to be scaled.
  271. * @param [in] scalar1 A first scalar to multiply by.
  272. * @param [in] base2 A second point to be scaled.
  273. * @param [in] scalar2 A second scalar to multiply by.
  274. * @TODO: test
  275. */
  276. void decaf_point_double_scalarmul (
  277. decaf_point_t combo,
  278. const decaf_point_t base1,
  279. const decaf_scalar_t scalar1,
  280. const decaf_point_t base2,
  281. const decaf_scalar_t scalar2
  282. ) API_VIS NONNULL5;
  283. /**
  284. * @brief Test that a point is valid, for debugging purposes.
  285. *
  286. * @param [in] point The number to test.
  287. * @retval DECAF_TRUE The point is valid.
  288. * @retval DECAF_FALSE The point is invalid.
  289. */
  290. decaf_bool_t decaf_point_valid (
  291. const decaf_point_t toTest
  292. ) API_VIS WARN_UNUSED NONNULL1;
  293. /**
  294. * @brief Almost-Elligator-like hash to curve.
  295. *
  296. * Call this function with the output of a hash to make a hash to the curve.
  297. *
  298. * This function runs Elligator2 on the decaf Jacobi quartic model. It then
  299. * uses the isogeny to put the result in twisted Edwards form. As a result,
  300. * it is safe (cannot produce points of order 4), and would be compatible with
  301. * hypothetical other implementations of Decaf using a Montgomery or untwisted
  302. * Edwards model.
  303. *
  304. * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
  305. * A factor of 2 due to the isogeny.
  306. * A factor of 2 because we quotient out the 2-torsion.
  307. * // TODO: check that it isn't more, especially for the identity point.
  308. *
  309. * Negating the input (mod q) results in the same point. Inverting the input
  310. * (mod q) results in the negative point. This is the same as Elligator.
  311. *
  312. * This function isn't quite indifferentiable from a random oracle.
  313. * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
  314. * Furthermore, calling it twice with independent seeds and adding the results
  315. * is indifferentiable from a random oracle.
  316. *
  317. * @param [in] hashed_data Output of some hash function.
  318. * @param [out] pt The data hashed to the curve.
  319. */
  320. void decaf_point_from_hash_nonuniform (
  321. decaf_point_t pt,
  322. const unsigned char hashed_data[DECAF_SER_BYTES]
  323. ) API_VIS NONNULL2;
  324. /**
  325. * @brief Indifferentiable hash function encoding to curve.
  326. *
  327. * Equivalent to calling decaf_point_from_hash_nonuniform twice and adding.
  328. *
  329. * @param [in] hashed_data Output of some hash function.
  330. * @param [out] pt The data hashed to the curve.
  331. */
  332. void decaf_point_from_hash_uniform (
  333. decaf_point_t pt,
  334. const unsigned char hashed_data[2*DECAF_SER_BYTES]
  335. ) API_VIS NONNULL2;
  336. /* TODO: functions to invert point_from_hash?? */
  337. #undef API_VIS
  338. #undef WARN_UNUSED
  339. #undef NONNULL1
  340. #undef NONNULL2
  341. #undef NONNULL3
  342. #undef NONNULL5
  343. #ifdef __cplusplus
  344. }; /* extern "C" */
  345. #endif
  346. #endif /* __DECAF_H__ */