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.
 
 
 
 
 

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