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.
 
 
 
 
 

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