517 lines
16 KiB

  1. /**
  2. * @file decaf.h
  3. * @author Mike Hamburg
  4. *
  5. * @copyright
  6. * Copyright (c) 2015 Cryptography Research, Inc. \n
  7. * Released under the MIT License. See LICENSE.txt for license information.
  8. *
  9. * @brief A group of prime order p.
  10. *
  11. * The Decaf library implements cryptographic operations on a an elliptic curve
  12. * group of prime order p. It accomplishes this by using a twisted Edwards
  13. * curve (isogenous to Ed448-Goldilocks) and wiping out the cofactor.
  14. *
  15. * The formulas are all complete and have no special cases, except that
  16. * decaf_448_decode can fail because not every sequence of bytes is a valid group
  17. * element.
  18. *
  19. * The formulas contain no data-dependent branches, timing or memory accesses.
  20. *
  21. * This library may support multiple curves eventually. The Ed448-Goldilocks
  22. * specific identifiers are prefixed with DECAF_448 or decaf_448.
  23. */
  24. #ifndef __DECAF_448_H__
  25. #define __DECAF_448_H__ 1
  26. #include <stdint.h>
  27. #include <sys/types.h>
  28. /* Goldilocks' build flags default to hidden and stripping executables. */
  29. /** @cond internal */
  30. #if defined(DOXYGEN) && !defined(__attribute__)
  31. #define __attribute__((x))
  32. #endif
  33. #define API_VIS __attribute__((visibility("default")))
  34. #define NOINLINE __attribute__((noinline))
  35. #define WARN_UNUSED __attribute__((warn_unused_result))
  36. #define NONNULL1 __attribute__((nonnull(1)))
  37. #define NONNULL2 __attribute__((nonnull(1,2)))
  38. #define NONNULL3 __attribute__((nonnull(1,2,3)))
  39. #define NONNULL5 __attribute__((nonnull(1,2,3,4,5)))
  40. /* Internal word types */
  41. #if (defined(__ILP64__) || defined(__amd64__) || defined(__x86_64__) || (((__UINT_FAST32_MAX__)>>30)>>30)) \
  42. && !defined(DECAF_FORCE_32_BIT)
  43. #define DECAF_WORD_BITS 64
  44. typedef uint64_t decaf_word_t, decaf_bool_t;
  45. #else
  46. #define DECAF_WORD_BITS 32
  47. typedef uint32_t decaf_word_t, decaf_bool_t;
  48. #endif
  49. /** @endcond */
  50. #define DECAF_448_LIMBS (512/DECAF_WORD_BITS)
  51. #define DECAF_448_SCALAR_BITS 446
  52. #define DECAF_448_SCALAR_LIMBS (448/DECAF_WORD_BITS)
  53. /** Number of bytes in a serialized point. */
  54. #define DECAF_448_SER_BYTES 56
  55. /** Number of bytes in a serialized scalar. */
  56. #define DECAF_448_SCALAR_BYTES 56
  57. /** Galois field element internal structure */
  58. typedef struct gf_s {
  59. decaf_word_t limb[DECAF_448_LIMBS];
  60. } __attribute__((aligned(32))) gf_s, gf[1];
  61. /** Twisted Edwards (-1,d-1) extended homogeneous coordinates */
  62. typedef struct decaf_448_point_s { gf x,y,z,t; } decaf_448_point_t[1];
  63. /** Precomputed table based on a point. Can be trivial implementation. */
  64. struct decaf_448_precomputed_s;
  65. typedef struct decaf_448_precomputed_s decaf_448_precomputed_s;
  66. /** Size and alignment of precomputed point tables. */
  67. extern const size_t sizeof_decaf_448_precomputed_s, alignof_decaf_448_precomputed_s;
  68. /** Scalar is stored packed, because we don't need the speed. */
  69. typedef struct decaf_448_scalar_s {
  70. decaf_word_t limb[DECAF_448_SCALAR_LIMBS];
  71. } decaf_448_scalar_t[1];
  72. /** DECAF_TRUE = -1 so that DECAF_TRUE & x = x */
  73. static const decaf_bool_t DECAF_TRUE = -(decaf_bool_t)1, DECAF_FALSE = 0;
  74. /** NB Success is -1, failure is 0. TODO: see if people would rather the reverse. */
  75. static const decaf_bool_t DECAF_SUCCESS = -(decaf_bool_t)1 /*DECAF_TRUE*/,
  76. DECAF_FAILURE = 0 /*DECAF_FALSE*/;
  77. /** The prime p, for debugging purposes.
  78. * TODO: prevent this scalar from actually being used for non-debugging purposes?
  79. */
  80. extern const decaf_448_scalar_t decaf_448_scalar_p API_VIS;
  81. /** A scalar equal to 1. */
  82. extern const decaf_448_scalar_t decaf_448_scalar_one API_VIS;
  83. /** A scalar equal to 0. */
  84. extern const decaf_448_scalar_t decaf_448_scalar_zero API_VIS;
  85. /** The identity point on the curve. */
  86. extern const decaf_448_point_t decaf_448_point_identity API_VIS;
  87. /**
  88. * An arbitrarily chosen base point on the curve.
  89. * Equal to Ed448-Goldilocks base point defined by DJB, except of course that
  90. * it's on the twist in this case. TODO: choose a base point with nice encoding?
  91. */
  92. extern const decaf_448_point_t decaf_448_point_base API_VIS;
  93. /** Precomputed table for the base point on the curve. */
  94. extern const struct decaf_448_precomputed_s *decaf_448_precomputed_base API_VIS;
  95. #ifdef __cplusplus
  96. extern "C" {
  97. #endif
  98. /* TODO: scalar invert? */
  99. /**
  100. * @brief Read a scalar from wire format or from bytes.
  101. *
  102. * @param [in] ser Serialized form of a scalar.
  103. * @param [out] out Deserialized form.
  104. *
  105. * @retval DECAF_SUCCESS The scalar was correctly encoded.
  106. * @retval DECAF_FAILURE The scalar was greater than the modulus,
  107. * and has been reduced modulo that modulus.
  108. */
  109. decaf_bool_t decaf_448_scalar_decode (
  110. decaf_448_scalar_t s,
  111. const unsigned char ser[DECAF_448_SCALAR_BYTES]
  112. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  113. /**
  114. * @brief Read a scalar from wire format or from bytes. Reduces mod
  115. * scalar prime.
  116. *
  117. * @param [in] ser Serialized form of a scalar.
  118. * @param [in] ser_len Length of serialized form.
  119. * @param [out] out Deserialized form.
  120. */
  121. void decaf_448_scalar_decode_long (
  122. decaf_448_scalar_t s,
  123. const unsigned char *ser,
  124. size_t ser_len
  125. ) API_VIS NONNULL2 NOINLINE;
  126. /**
  127. * @brief Serialize a scalar to wire format.
  128. *
  129. * @param [out] ser Serialized form of a scalar.
  130. * @param [in] s Deserialized scalar.
  131. */
  132. void decaf_448_scalar_encode (
  133. unsigned char ser[DECAF_448_SCALAR_BYTES],
  134. const decaf_448_scalar_t s
  135. ) API_VIS NONNULL2 NOINLINE NOINLINE;
  136. /**
  137. * @brief Add two scalars. The scalars may use the same memory.
  138. * @param [in] a One scalar.
  139. * @param [in] b Another scalar.
  140. * @param [out] out a+b.
  141. */
  142. void decaf_448_scalar_add (
  143. decaf_448_scalar_t out,
  144. const decaf_448_scalar_t a,
  145. const decaf_448_scalar_t b
  146. ) API_VIS NONNULL3 NOINLINE;
  147. /**
  148. * @brief Compare two scalars.
  149. * @param [in] a One scalar.
  150. * @param [in] b Another scalar.
  151. * @retval DECAF_TRUE The scalars are equal.
  152. * @retval DECAF_FALSE The scalars are not equal.
  153. */
  154. decaf_bool_t decaf_448_scalar_eq (
  155. const decaf_448_scalar_t a,
  156. const decaf_448_scalar_t b
  157. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  158. /**
  159. * @brief Subtract two scalars. The scalars may use the same memory.
  160. * @param [in] a One scalar.
  161. * @param [in] b Another scalar.
  162. * @param [out] out a-b.
  163. */
  164. void decaf_448_scalar_sub (
  165. decaf_448_scalar_t out,
  166. const decaf_448_scalar_t a,
  167. const decaf_448_scalar_t b
  168. ) API_VIS NONNULL3 NOINLINE;
  169. /**
  170. * @brief Multiply two scalars. The scalars may use the same memory.
  171. * @param [in] a One scalar.
  172. * @param [in] b Another scalar.
  173. * @param [out] out a*b.
  174. */
  175. void decaf_448_scalar_mul (
  176. decaf_448_scalar_t out,
  177. const decaf_448_scalar_t a,
  178. const decaf_448_scalar_t b
  179. ) API_VIS NONNULL3 NOINLINE;
  180. /**
  181. * @brief Copy a scalar. The scalars may use the same memory, in which
  182. * case this function does nothing.
  183. * @param [in] a A scalar.
  184. * @param [out] out Will become a copy of a.
  185. */
  186. void decaf_448_scalar_copy (
  187. decaf_448_scalar_t out,
  188. const decaf_448_scalar_t a
  189. ) API_VIS NONNULL2;
  190. /**
  191. * @brief Encode a point as a sequence of bytes.
  192. *
  193. * @param [out] ser The byte representation of the point.
  194. * @param [in] pt The point to encode.
  195. */
  196. void decaf_448_point_encode (
  197. uint8_t ser[DECAF_448_SER_BYTES],
  198. const decaf_448_point_t pt
  199. ) API_VIS NONNULL2 NOINLINE;
  200. /**
  201. * @brief Decode a point from a sequence of bytes.
  202. *
  203. * Every point has a unique encoding, so not every
  204. * sequence of bytes is a valid encoding. If an invalid
  205. * encoding is given, the output is undefined.
  206. *
  207. * @param [out] pt The decoded point.
  208. * @param [in] ser The serialized version of the point.
  209. * @retval DECAF_SUCCESS The decoding succeeded.
  210. * @retval DECAF_FAILURE The decoding didn't succeed, because
  211. * ser does not represent a point.
  212. */
  213. decaf_bool_t decaf_448_point_decode (
  214. decaf_448_point_t pt,
  215. const uint8_t ser[DECAF_448_SER_BYTES],
  216. decaf_bool_t allow_identity
  217. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  218. /**
  219. * @brief Copy a point. The input and output may alias,
  220. * in which case this function does nothing.
  221. *
  222. * @param [out] a A copy of the point.
  223. * @param [in] b Any point.
  224. */
  225. void decaf_448_point_copy (
  226. decaf_448_point_t a,
  227. const decaf_448_point_t b
  228. ) API_VIS NONNULL2;
  229. /**
  230. * @brief Test whether two points are equal. If yes, return
  231. * DECAF_TRUE, else return DECAF_FALSE.
  232. *
  233. * @param [in] a A point.
  234. * @param [in] b Another point.
  235. * @retval DECAF_TRUE The points are equal.
  236. * @retval DECAF_FALSE The points are not equal.
  237. */
  238. decaf_bool_t decaf_448_point_eq (
  239. const decaf_448_point_t a,
  240. const decaf_448_point_t b
  241. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  242. /**
  243. * @brief Add two points to produce a third point. The
  244. * input points and output point can be pointers to the same
  245. * memory.
  246. *
  247. * @param [out] sum The sum a+b.
  248. * @param [in] a An addend.
  249. * @param [in] b An addend.
  250. */
  251. void decaf_448_point_add (
  252. decaf_448_point_t sum,
  253. const decaf_448_point_t a,
  254. const decaf_448_point_t b
  255. ) API_VIS NONNULL3; // TODO: NOINLINE?
  256. /**
  257. * @brief Double a point. Equivalent to
  258. * decaf_448_point_add(two_a,a,a), but potentially faster.
  259. *
  260. * @param [out] sum The sum a+a.
  261. * @param [in] a A point.
  262. */
  263. void decaf_448_point_double (
  264. decaf_448_point_t two_a,
  265. const decaf_448_point_t a
  266. ) API_VIS NONNULL2; // TODO: NOINLINE?
  267. /**
  268. * @brief Subtract two points to produce a third point. The
  269. * input points and output point can be pointers to the same
  270. * memory.
  271. *
  272. * @param [out] sum The difference a-b.
  273. * @param [in] a The minuend.
  274. * @param [in] b The subtrahend.
  275. */
  276. void decaf_448_point_sub (
  277. decaf_448_point_t diff,
  278. const decaf_448_point_t a,
  279. const decaf_448_point_t b
  280. ) API_VIS NONNULL3; // TODO: NOINLINE?
  281. /**
  282. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  283. *
  284. * @param [out] scaled The scaled point base*scalar
  285. * @param [in] base The point to be scaled.
  286. * @param [in] scalar The scalar to multiply by.
  287. */
  288. void decaf_448_point_scalarmul (
  289. decaf_448_point_t scaled,
  290. const decaf_448_point_t base,
  291. const decaf_448_scalar_t scalar
  292. ) API_VIS NONNULL3 NOINLINE;
  293. /**
  294. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  295. * This function operates directly on serialized forms.
  296. *
  297. * @warning This function is experimental. It may not be supported
  298. * long-term.
  299. *
  300. * @param [out] scaled The scaled point base*scalar
  301. * @param [in] base The point to be scaled.
  302. * @param [in] scalar The scalar to multiply by.
  303. * @param [in] allow_identity Allow the input to be the identity.
  304. * @param [in] short_circuit Allow a fast return if the input is illegal.
  305. *
  306. * @retval DECAF_SUCCESS The scalarmul succeeded.
  307. * @retval DECAF_FAILURE The scalarmul didn't succeed, because
  308. * base does not represent a point.
  309. */
  310. decaf_bool_t decaf_448_direct_scalarmul (
  311. uint8_t scaled[DECAF_448_SER_BYTES],
  312. const uint8_t base[DECAF_448_SER_BYTES],
  313. const decaf_448_scalar_t scalar,
  314. decaf_bool_t allow_identity,
  315. decaf_bool_t short_circuit
  316. ) API_VIS NONNULL3 WARN_UNUSED NOINLINE;
  317. /**
  318. * @brief Precompute a table for fast scalar multiplication.
  319. * Some implementations do not include precomputed points; for
  320. * those implementations, this implementation simply copies the
  321. * point.
  322. *
  323. * @param [out] a A precomputed table of multiples of the point.
  324. * @param [in] b Any point.
  325. */
  326. void decaf_448_precompute (
  327. decaf_448_precomputed_s *a,
  328. const decaf_448_point_t b
  329. ) API_VIS NONNULL2 NOINLINE;
  330. /**
  331. * @brief Multiply a precomputed base point by a scalar:
  332. * scaled = scalar*base.
  333. * Some implementations do not include precomputed points; for
  334. * those implementations, this function is the same as
  335. * decaf_448_point_scalarmul
  336. *
  337. * @param [out] scaled The scaled point base*scalar
  338. * @param [in] base The point to be scaled.
  339. * @param [in] scalar The scalar to multiply by.
  340. */
  341. void decaf_448_precomputed_scalarmul (
  342. decaf_448_point_t scaled,
  343. const decaf_448_precomputed_s *base,
  344. const decaf_448_scalar_t scalar
  345. ) API_VIS NONNULL3 NOINLINE;
  346. /**
  347. * @brief Multiply two base points by two scalars:
  348. * scaled = scalar1*base1 + scalar2*base2.
  349. *
  350. * Equivalent to two calls to decaf_448_point_scalarmul, but may be
  351. * faster.
  352. *
  353. * @param [out] scaled The scaled point base*scalar
  354. * @param [in] base1 A first point to be scaled.
  355. * @param [in] scalar1 A first scalar to multiply by.
  356. * @param [in] base2 A second point to be scaled.
  357. * @param [in] scalar2 A second scalar to multiply by.
  358. */
  359. void decaf_448_point_double_scalarmul (
  360. decaf_448_point_t combo,
  361. const decaf_448_point_t base1,
  362. const decaf_448_scalar_t scalar1,
  363. const decaf_448_point_t base2,
  364. const decaf_448_scalar_t scalar2
  365. ) API_VIS NONNULL5 NOINLINE;
  366. /**
  367. * @brief Multiply two base points by two scalars:
  368. * scaled = scalar1*base1 + scalar2*base2.
  369. *
  370. * Otherwise quivalent to two calls to decaf_448_point_scalarmul, but may be
  371. * faster.
  372. *
  373. * @param [out] scaled The scaled point base*scalar
  374. * @param [in] base1 A precomputed first point to be scaled.
  375. * @param [in] scalar1 A first scalar to multiply by.
  376. * @param [in] base2 A second point to be scaled.
  377. * @param [in] scalar2 A second scalar to multiply by.
  378. *
  379. * @warning: This function takes variable time, and may leak the scalars
  380. * used. It is designed for signature verification.
  381. */
  382. void decaf_448_precomputed_double_scalarmul_non_secret (
  383. decaf_448_point_t combo,
  384. const decaf_448_precomputed_s *base1,
  385. const decaf_448_scalar_t scalar1,
  386. const decaf_448_point_t base2,
  387. const decaf_448_scalar_t scalar2
  388. ) API_VIS NONNULL5 NOINLINE;
  389. /**
  390. * @brief Test that a point is valid, for debugging purposes.
  391. *
  392. * @param [in] point The number to test.
  393. * @retval DECAF_TRUE The point is valid.
  394. * @retval DECAF_FALSE The point is invalid.
  395. */
  396. decaf_bool_t decaf_448_point_valid (
  397. const decaf_448_point_t toTest
  398. ) API_VIS WARN_UNUSED NONNULL1 NOINLINE;
  399. /**
  400. * @brief Almost-Elligator-like hash to curve.
  401. *
  402. * Call this function with the output of a hash to make a hash to the curve.
  403. *
  404. * This function runs Elligator2 on the decaf_448 Jacobi quartic model. It then
  405. * uses the isogeny to put the result in twisted Edwards form. As a result,
  406. * it is safe (cannot produce points of order 4), and would be compatible with
  407. * hypothetical other implementations of Decaf using a Montgomery or untwisted
  408. * Edwards model.
  409. *
  410. * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
  411. * A factor of 2 due to the isogeny.
  412. * A factor of 2 because we quotient out the 2-torsion.
  413. * // TODO: check that it isn't more, especially for the identity point.
  414. *
  415. * Negating the input (mod q) results in the same point. Inverting the input
  416. * (mod q) results in the negative point. This is the same as Elligator.
  417. *
  418. * This function isn't quite indifferentiable from a random oracle.
  419. * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
  420. * Furthermore, calling it twice with independent seeds and adding the results
  421. * is indifferentiable from a random oracle.
  422. *
  423. * @param [in] hashed_data Output of some hash function.
  424. * @param [out] pt The data hashed to the curve.
  425. */
  426. void decaf_448_point_from_hash_nonuniform (
  427. decaf_448_point_t pt,
  428. const unsigned char hashed_data[DECAF_448_SER_BYTES]
  429. ) API_VIS NONNULL2 NOINLINE;
  430. /**
  431. * @brief Indifferentiable hash function encoding to curve.
  432. *
  433. * Equivalent to calling decaf_448_point_from_hash_nonuniform twice and adding.
  434. *
  435. * @param [in] hashed_data Output of some hash function.
  436. * @param [out] pt The data hashed to the curve.
  437. */
  438. void decaf_448_point_from_hash_uniform (
  439. decaf_448_point_t pt,
  440. const unsigned char hashed_data[2*DECAF_448_SER_BYTES]
  441. ) API_VIS NONNULL2 NOINLINE;
  442. /**
  443. * @brief Overwrite data with zeros. Use memset_s if available.
  444. */
  445. void decaf_bzero (
  446. void *data,
  447. size_t size
  448. ) NONNULL1 API_VIS NOINLINE;
  449. /**
  450. * @brief Overwrite scalar with zeros.
  451. */
  452. void decaf_448_scalar_destroy (
  453. decaf_448_scalar_t scalar
  454. ) NONNULL1 API_VIS;
  455. /* TODO: functions to invert point_from_hash?? */
  456. #undef API_VIS
  457. #undef WARN_UNUSED
  458. #undef NONNULL1
  459. #undef NONNULL2
  460. #undef NONNULL3
  461. #undef NONNULL5
  462. #ifdef __cplusplus
  463. }; /* extern "C" */
  464. #endif
  465. #endif /* __DECAF_448_H__ */