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.
 
 
 
 
 

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