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.
 
 
 
 
 

383 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 Copy a scalar. The scalars may use the same memory, in which
  145. * case this function does nothing.
  146. * @param [in] a A scalar.
  147. * @param [out] out Will become a copy of a.
  148. */
  149. void decaf_scalar_copy (
  150. decaf_scalar_t out,
  151. const decaf_scalar_t a
  152. ) API_VIS NONNULL2;
  153. /**
  154. * @brief Encode a point as a sequence of bytes.
  155. *
  156. * @param [out] ser The byte representation of the point.
  157. * @param [in] pt The point to encode.
  158. */
  159. void decaf_point_encode (
  160. uint8_t ser[DECAF_SER_BYTES],
  161. const decaf_point_t pt
  162. ) API_VIS NONNULL2;
  163. /**
  164. * @brief Decode a point from a sequence of bytes.
  165. *
  166. * Every point has a unique encoding, so not every
  167. * sequence of bytes is a valid encoding. If an invalid
  168. * encoding is given, the output is undefined.
  169. *
  170. * @param [out] pt The decoded point.
  171. * @param [in] ser The serialized version of the point.
  172. * @retval DECAF_SUCCESS The decoding succeeded.
  173. * @retval DECAF_FAILURE The decoding didn't succeed, because
  174. * ser does not represent a point.
  175. */
  176. decaf_bool_t decaf_point_decode (
  177. decaf_point_t pt,
  178. const uint8_t ser[DECAF_SER_BYTES],
  179. decaf_bool_t allow_identity
  180. ) API_VIS WARN_UNUSED NONNULL2;
  181. /**
  182. * @brief Copy a point. The input and output may alias,
  183. * in which case this function does nothing.
  184. *
  185. * @param [out] a A copy of the point.
  186. * @param [in] b Any point.
  187. */
  188. void decaf_point_copy (
  189. decaf_point_t a,
  190. const decaf_point_t b
  191. ) API_VIS NONNULL2;
  192. /**
  193. * @brief Test whether two points are equal. If yes, return
  194. * DECAF_TRUE, else return DECAF_FALSE.
  195. *
  196. * @param [in] a A point.
  197. * @param [in] b Another point.
  198. * @retval DECAF_TRUE The points are equal.
  199. * @retval DECAF_FALSE The points are not equal.
  200. */
  201. decaf_bool_t decaf_point_eq (
  202. const decaf_point_t a,
  203. const decaf_point_t b
  204. ) API_VIS WARN_UNUSED NONNULL2;
  205. /**
  206. * @brief Add two points to produce a third point. The
  207. * input points and output point can be pointers to the same
  208. * memory.
  209. *
  210. * @param [out] sum The sum a+b.
  211. * @param [in] a An addend.
  212. * @param [in] b An addend.
  213. */
  214. void decaf_point_add (
  215. decaf_point_t sum,
  216. const decaf_point_t a,
  217. const decaf_point_t b
  218. ) API_VIS NONNULL3;
  219. /**
  220. * @brief Double a point. Equivalent to
  221. * decaf_point_add(two_a,a,a), but potentially faster.
  222. *
  223. * @param [out] sum The sum a+a.
  224. * @param [in] a A point.
  225. */
  226. void decaf_point_double (
  227. decaf_point_t two_a,
  228. const decaf_point_t a
  229. ) API_VIS NONNULL2;
  230. /**
  231. * @brief Subtract two points to produce a third point. The
  232. * input points and output point can be pointers to the same
  233. * memory.
  234. *
  235. * @param [out] sum The difference a-b.
  236. * @param [in] a The minuend.
  237. * @param [in] b The subtrahend.
  238. */
  239. void decaf_point_sub (
  240. decaf_point_t diff,
  241. const decaf_point_t a,
  242. const decaf_point_t b
  243. ) API_VIS NONNULL3;
  244. /**
  245. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  246. *
  247. * @param [out] scaled The scaled point base*scalar
  248. * @param [in] base The point to be scaled.
  249. * @param [in] scalar The scalar to multilpy by.
  250. */
  251. void decaf_point_scalarmul (
  252. decaf_point_t scaled,
  253. const decaf_point_t base,
  254. const decaf_scalar_t scalar
  255. ) API_VIS NONNULL3;
  256. /**
  257. * @brief Multiply two base points by two scalars:
  258. * scaled = scalar1*base1 + scalar2*base2.
  259. *
  260. * Equivalent to two calls to decaf_point_scalarmul, but may be
  261. * faster.
  262. *
  263. * @param [out] scaled The scaled point base*scalar
  264. * @param [in] base1 A first point to be scaled.
  265. * @param [in] scalar1 A first scalar to multilpy by.
  266. * @param [in] base2 A second point to be scaled.
  267. * @param [in] scalar2 A second scalar to multilpy by.
  268. * @TODO: test
  269. */
  270. void decaf_point_double_scalarmul (
  271. decaf_point_t combo,
  272. const decaf_point_t base1,
  273. const decaf_scalar_t scalar1,
  274. const decaf_point_t base2,
  275. const decaf_scalar_t scalar2
  276. ) API_VIS NONNULL5;
  277. /**
  278. * @brief Test that a point is valid, for debugging purposes.
  279. *
  280. * @param [in] point The number to test.
  281. * @retval DECAF_TRUE The point is valid.
  282. * @retval DECAF_FALSE The point is invalid.
  283. */
  284. decaf_bool_t decaf_point_valid (
  285. const decaf_point_t toTest
  286. ) API_VIS WARN_UNUSED NONNULL1;
  287. /**
  288. * @brief Almost-Elligator-like hash to curve.
  289. *
  290. * Call this function with the output of a hash to make a hash to the curve.
  291. *
  292. * This function runs Elligator2 on the decaf Jacobi quartic model. It then
  293. * uses the isogeny to put the result in twisted Edwards form. As a result,
  294. * it is safe (cannot produce points of order 4), and would be compatible with
  295. * hypothetical other implementations of Decaf using a Montgomery or untwisted
  296. * Edwards model.
  297. *
  298. * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
  299. * A factor of 2 due to the isogeny.
  300. * A factor of 2 because we quotient out the 2-torsion.
  301. * // TODO: check that it isn't more, especially for the identity point.
  302. *
  303. * Negating the input (mod q) results in the same point. Inverting the input
  304. * (mod q) results in the negative point. This is the same as Elligator.
  305. *
  306. * This function isn't quite indifferentiable from a random oracle.
  307. * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
  308. * Furthermore, calling it twice with independent seeds and adding the results
  309. * is indifferentiable from a random oracle.
  310. *
  311. * @param [in] hashed_data Output of some hash function.
  312. * @param [out] pt The data hashed to the curve.
  313. */
  314. void decaf_point_from_hash_nonuniform (
  315. decaf_point_t pt,
  316. const unsigned char hashed_data[DECAF_SER_BYTES]
  317. ) API_VIS NONNULL2;
  318. /**
  319. * @brief Indifferentiable hash function encoding to curve.
  320. *
  321. * Equivalent to calling decaf_point_from_hash_nonuniform twice and adding.
  322. *
  323. * @param [in] hashed_data Output of some hash function.
  324. * @param [out] pt The data hashed to the curve.
  325. */
  326. void decaf_point_from_hash_uniform (
  327. decaf_point_t pt,
  328. const unsigned char hashed_data[2*DECAF_SER_BYTES]
  329. ) API_VIS NONNULL2;
  330. /* TODO: functions to invert point_from_hash?? */
  331. #undef API_VIS
  332. #undef WARN_UNUSED
  333. #undef NONNULL1
  334. #undef NONNULL2
  335. #undef NONNULL3
  336. #undef NONNULL5
  337. #ifdef __cplusplus
  338. }; /* extern "C" */
  339. #endif
  340. #endif /* __DECAF_H__ */