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.
 
 
 
 
 

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