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.
 
 
 
 
 

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