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.
 
 
 
 
 

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