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.
 
 
 
 
 

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