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.
 
 
 
 
 

564 lines
17 KiB

  1. /**
  2. * @file decaf_448.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, based on Ed448-Goldilocks.
  10. */
  11. #ifndef __DECAF_448_H__
  12. #define __DECAF_448_H__ 1
  13. #include "decaf_common.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define DECAF_448_LIMBS (512/DECAF_WORD_BITS)
  18. #define DECAF_448_SCALAR_BITS 446
  19. #define DECAF_448_SCALAR_LIMBS (448/DECAF_WORD_BITS)
  20. /** Galois field element internal structure */
  21. typedef struct gf_s {
  22. decaf_word_t limb[DECAF_448_LIMBS];
  23. } __attribute__((aligned(32))) gf_s, gf[1];
  24. /** @endcond */
  25. /** Number of bytes in a serialized point. */
  26. #define DECAF_448_SER_BYTES 56
  27. /** Number of bytes in a serialized scalar. */
  28. #define DECAF_448_SCALAR_BYTES 56
  29. /** Twisted Edwards (-1,d-1) extended homogeneous coordinates */
  30. typedef struct decaf_448_point_s { /**@cond internal*/gf x,y,z,t;/**@endcond*/ } decaf_448_point_t[1];
  31. /** Precomputed table based on a point. Can be trivial implementation. */
  32. struct decaf_448_precomputed_s;
  33. /** Precomputed table based on a point. Can be trivial implementation. */
  34. typedef struct decaf_448_precomputed_s decaf_448_precomputed_s;
  35. /** Size and alignment of precomputed point tables. */
  36. extern const size_t sizeof_decaf_448_precomputed_s API_VIS, alignof_decaf_448_precomputed_s API_VIS;
  37. /** Scalar is stored packed, because we don't need the speed. */
  38. typedef struct decaf_448_scalar_s {
  39. /** @cond internal */
  40. decaf_word_t limb[DECAF_448_SCALAR_LIMBS];
  41. /** @endcond */
  42. } decaf_448_scalar_t[1];
  43. /** A scalar equal to 1. */
  44. extern const decaf_448_scalar_t decaf_448_scalar_one API_VIS;
  45. /** A scalar equal to 0. */
  46. extern const decaf_448_scalar_t decaf_448_scalar_zero API_VIS;
  47. /** The identity point on the curve. */
  48. extern const decaf_448_point_t decaf_448_point_identity API_VIS;
  49. /** An arbitrarily chosen base point on the curve. */
  50. extern const decaf_448_point_t decaf_448_point_base API_VIS;
  51. /** Precomputed table for the base point on the curve. */
  52. extern const struct decaf_448_precomputed_s *decaf_448_precomputed_base API_VIS;
  53. /**
  54. * @brief Read a scalar from wire format or from bytes.
  55. *
  56. * @param [in] ser Serialized form of a scalar.
  57. * @param [out] out Deserialized form.
  58. *
  59. * @retval DECAF_SUCCESS The scalar was correctly encoded.
  60. * @retval DECAF_FAILURE The scalar was greater than the modulus,
  61. * and has been reduced modulo that modulus.
  62. */
  63. decaf_bool_t decaf_448_scalar_decode (
  64. decaf_448_scalar_t out,
  65. const unsigned char ser[DECAF_448_SCALAR_BYTES]
  66. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  67. /**
  68. * @brief Read a scalar from wire format or from bytes. Reduces mod
  69. * scalar prime.
  70. *
  71. * @param [in] ser Serialized form of a scalar.
  72. * @param [in] ser_len Length of serialized form.
  73. * @param [out] out Deserialized form.
  74. */
  75. void decaf_448_scalar_decode_long (
  76. decaf_448_scalar_t out,
  77. const unsigned char *ser,
  78. size_t ser_len
  79. ) API_VIS NONNULL2 NOINLINE;
  80. /**
  81. * @brief Serialize a scalar to wire format.
  82. *
  83. * @param [out] ser Serialized form of a scalar.
  84. * @param [in] s Deserialized scalar.
  85. */
  86. void decaf_448_scalar_encode (
  87. unsigned char ser[DECAF_448_SCALAR_BYTES],
  88. const decaf_448_scalar_t s
  89. ) API_VIS NONNULL2 NOINLINE NOINLINE;
  90. /**
  91. * @brief Add two scalars. The scalars may use the same memory.
  92. * @param [in] a One scalar.
  93. * @param [in] b Another scalar.
  94. * @param [out] out a+b.
  95. */
  96. void decaf_448_scalar_add (
  97. decaf_448_scalar_t out,
  98. const decaf_448_scalar_t a,
  99. const decaf_448_scalar_t b
  100. ) API_VIS NONNULL3 NOINLINE;
  101. /**
  102. * @brief Compare two scalars.
  103. * @param [in] a One scalar.
  104. * @param [in] b Another scalar.
  105. * @retval DECAF_TRUE The scalars are equal.
  106. * @retval DECAF_FALSE The scalars are not equal.
  107. */
  108. decaf_bool_t decaf_448_scalar_eq (
  109. const decaf_448_scalar_t a,
  110. const decaf_448_scalar_t b
  111. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  112. /**
  113. * @brief Subtract two scalars. The scalars may use the same memory.
  114. * @param [in] a One scalar.
  115. * @param [in] b Another scalar.
  116. * @param [out] out a-b.
  117. */
  118. void decaf_448_scalar_sub (
  119. decaf_448_scalar_t out,
  120. const decaf_448_scalar_t a,
  121. const decaf_448_scalar_t b
  122. ) API_VIS NONNULL3 NOINLINE;
  123. /**
  124. * @brief Multiply two scalars. The scalars may use the same memory.
  125. * @param [in] a One scalar.
  126. * @param [in] b Another scalar.
  127. * @param [out] out a*b.
  128. */
  129. void decaf_448_scalar_mul (
  130. decaf_448_scalar_t out,
  131. const decaf_448_scalar_t a,
  132. const decaf_448_scalar_t b
  133. ) API_VIS NONNULL3 NOINLINE;
  134. /**
  135. * @brief Invert a scalar. When passed zero, return 0. The input and output may alias.
  136. * @param [in] a A scalar.
  137. * @param [out] out 1/a.
  138. * @return DECAF_TRUE The input is nonzero.
  139. */
  140. decaf_bool_t decaf_448_scalar_invert (
  141. decaf_448_scalar_t out,
  142. const decaf_448_scalar_t a
  143. ) API_VIS NONNULL2 NOINLINE;
  144. /**
  145. * @brief Copy a scalar. The scalars may use the same memory, in which
  146. * case this function does nothing.
  147. * @param [in] a A scalar.
  148. * @param [out] out Will become a copy of a.
  149. */
  150. static inline void NONNULL2 decaf_448_scalar_copy (
  151. decaf_448_scalar_t out,
  152. const decaf_448_scalar_t a
  153. ) {
  154. *out = *a;
  155. }
  156. /**
  157. * @brief Set a scalar to an integer.
  158. * @param [in] a An integer.
  159. * @param [out] out Will become equal to a.
  160. */
  161. void decaf_448_scalar_set_unsigned (
  162. decaf_448_scalar_t out,
  163. decaf_word_t a
  164. ) API_VIS NONNULL1;
  165. /**
  166. * @brief Encode a point as a sequence of bytes.
  167. *
  168. * @param [out] ser The byte representation of the point.
  169. * @param [in] pt The point to encode.
  170. */
  171. void decaf_448_point_encode (
  172. uint8_t ser[DECAF_448_SER_BYTES],
  173. const decaf_448_point_t pt
  174. ) API_VIS NONNULL2 NOINLINE;
  175. /**
  176. * @brief Decode a point from a sequence of bytes.
  177. *
  178. * Every point has a unique encoding, so not every
  179. * sequence of bytes is a valid encoding. If an invalid
  180. * encoding is given, the output is undefined.
  181. *
  182. * @param [out] pt The decoded point.
  183. * @param [in] ser The serialized version of the point.
  184. * @param [in] allow_identity DECAF_TRUE if the identity is a legal input.
  185. * @retval DECAF_SUCCESS The decoding succeeded.
  186. * @retval DECAF_FAILURE The decoding didn't succeed, because
  187. * ser does not represent a point.
  188. */
  189. decaf_bool_t decaf_448_point_decode (
  190. decaf_448_point_t pt,
  191. const uint8_t ser[DECAF_448_SER_BYTES],
  192. decaf_bool_t allow_identity
  193. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  194. /**
  195. * @brief Copy a point. The input and output may alias,
  196. * in which case this function does nothing.
  197. *
  198. * @param [out] a A copy of the point.
  199. * @param [in] b Any point.
  200. */
  201. static inline void NONNULL2 decaf_448_point_copy (
  202. decaf_448_point_t a,
  203. const decaf_448_point_t b
  204. ) {
  205. *a=*b;
  206. }
  207. /**
  208. * @brief Test whether two points are equal. If yes, return
  209. * DECAF_TRUE, else return DECAF_FALSE.
  210. *
  211. * @param [in] a A point.
  212. * @param [in] b Another point.
  213. * @retval DECAF_TRUE The points are equal.
  214. * @retval DECAF_FALSE The points are not equal.
  215. */
  216. decaf_bool_t decaf_448_point_eq (
  217. const decaf_448_point_t a,
  218. const decaf_448_point_t b
  219. ) API_VIS WARN_UNUSED NONNULL2 NOINLINE;
  220. /**
  221. * @brief Add two points to produce a third point. The
  222. * input points and output point can be pointers to the same
  223. * memory.
  224. *
  225. * @param [out] sum The sum a+b.
  226. * @param [in] a An addend.
  227. * @param [in] b An addend.
  228. */
  229. void decaf_448_point_add (
  230. decaf_448_point_t sum,
  231. const decaf_448_point_t a,
  232. const decaf_448_point_t b
  233. ) API_VIS NONNULL3;
  234. /**
  235. * @brief Double a point. Equivalent to
  236. * decaf_448_point_add(two_a,a,a), but potentially faster.
  237. *
  238. * @param [out] two_a The sum a+a.
  239. * @param [in] a A point.
  240. */
  241. void decaf_448_point_double (
  242. decaf_448_point_t two_a,
  243. const decaf_448_point_t a
  244. ) API_VIS NONNULL2;
  245. /**
  246. * @brief Subtract two points to produce a third point. The
  247. * input points and output point can be pointers to the same
  248. * memory.
  249. *
  250. * @param [out] diff The difference a-b.
  251. * @param [in] a The minuend.
  252. * @param [in] b The subtrahend.
  253. */
  254. void decaf_448_point_sub (
  255. decaf_448_point_t diff,
  256. const decaf_448_point_t a,
  257. const decaf_448_point_t b
  258. ) API_VIS NONNULL3;
  259. /**
  260. * @brief Negate a point to produce another point. The input
  261. * and output points can use the same memory.
  262. *
  263. * @param [out] nega The negated input point
  264. * @param [in] a The input point.
  265. */
  266. void decaf_448_point_negate (
  267. decaf_448_point_t nega,
  268. const decaf_448_point_t a
  269. ) API_VIS NONNULL2;
  270. /**
  271. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  272. *
  273. * @param [out] scaled The scaled point base*scalar
  274. * @param [in] base The point to be scaled.
  275. * @param [in] scalar The scalar to multiply by.
  276. */
  277. void decaf_448_point_scalarmul (
  278. decaf_448_point_t scaled,
  279. const decaf_448_point_t base,
  280. const decaf_448_scalar_t scalar
  281. ) API_VIS NONNULL3 NOINLINE;
  282. /**
  283. * @brief Multiply a base point by a scalar: scaled = scalar*base.
  284. * This function operates directly on serialized forms.
  285. *
  286. * @warning This function is experimental. It may not be supported
  287. * long-term.
  288. *
  289. * @param [out] scaled The scaled point base*scalar
  290. * @param [in] base The point to be scaled.
  291. * @param [in] scalar The scalar to multiply by.
  292. * @param [in] allow_identity Allow the input to be the identity.
  293. * @param [in] short_circuit Allow a fast return if the input is illegal.
  294. *
  295. * @retval DECAF_SUCCESS The scalarmul succeeded.
  296. * @retval DECAF_FAILURE The scalarmul didn't succeed, because
  297. * base does not represent a point.
  298. */
  299. decaf_bool_t decaf_448_direct_scalarmul (
  300. uint8_t scaled[DECAF_448_SER_BYTES],
  301. const uint8_t base[DECAF_448_SER_BYTES],
  302. const decaf_448_scalar_t scalar,
  303. decaf_bool_t allow_identity,
  304. decaf_bool_t short_circuit
  305. ) API_VIS NONNULL3 WARN_UNUSED NOINLINE;
  306. /**
  307. * @brief Precompute a table for fast scalar multiplication.
  308. * Some implementations do not include precomputed points; for
  309. * those implementations, this implementation simply copies the
  310. * point.
  311. *
  312. * @param [out] a A precomputed table of multiples of the point.
  313. * @param [in] b Any point.
  314. */
  315. void decaf_448_precompute (
  316. decaf_448_precomputed_s *a,
  317. const decaf_448_point_t b
  318. ) API_VIS NONNULL2 NOINLINE;
  319. /**
  320. * @brief Multiply a precomputed base point by a scalar:
  321. * scaled = scalar*base.
  322. * Some implementations do not include precomputed points; for
  323. * those implementations, this function is the same as
  324. * decaf_448_point_scalarmul
  325. *
  326. * @param [out] scaled The scaled point base*scalar
  327. * @param [in] base The point to be scaled.
  328. * @param [in] scalar The scalar to multiply by.
  329. */
  330. void decaf_448_precomputed_scalarmul (
  331. decaf_448_point_t scaled,
  332. const decaf_448_precomputed_s *base,
  333. const decaf_448_scalar_t scalar
  334. ) API_VIS NONNULL3 NOINLINE;
  335. /**
  336. * @brief Multiply two base points by two scalars:
  337. * scaled = scalar1*base1 + scalar2*base2.
  338. *
  339. * Equivalent to two calls to decaf_448_point_scalarmul, but may be
  340. * faster.
  341. *
  342. * @param [out] combo The linear combination scalar1*base1 + scalar2*base2.
  343. * @param [in] base1 A first point to be scaled.
  344. * @param [in] scalar1 A first scalar to multiply by.
  345. * @param [in] base2 A second point to be scaled.
  346. * @param [in] scalar2 A second scalar to multiply by.
  347. */
  348. void decaf_448_point_double_scalarmul (
  349. decaf_448_point_t combo,
  350. const decaf_448_point_t base1,
  351. const decaf_448_scalar_t scalar1,
  352. const decaf_448_point_t base2,
  353. const decaf_448_scalar_t scalar2
  354. ) API_VIS NONNULL5 NOINLINE;
  355. /**
  356. * @brief Multiply two base points by two scalars:
  357. * scaled = scalar1*decaf_448_point_base + scalar2*base2.
  358. *
  359. * Otherwise equivalent to decaf_448_point_double_scalarmul, but may be
  360. * faster at the expense of being variable time.
  361. *
  362. * @param [out] combo The linear combination scalar1*base + scalar2*base2.
  363. * @param [in] scalar1 A first scalar to multiply by.
  364. * @param [in] base2 A second point to be scaled.
  365. * @param [in] scalar2 A second scalar to multiply by.
  366. *
  367. * @warning: This function takes variable time, and may leak the scalars
  368. * used. It is designed for signature verification.
  369. */
  370. void decaf_448_base_double_scalarmul_non_secret (
  371. decaf_448_point_t combo,
  372. const decaf_448_scalar_t scalar1,
  373. const decaf_448_point_t base2,
  374. const decaf_448_scalar_t scalar2
  375. ) API_VIS NONNULL4 NOINLINE;
  376. /**
  377. * @brief Test that a point is valid, for debugging purposes.
  378. *
  379. * @param [in] toTest The point to test.
  380. * @retval DECAF_TRUE The point is valid.
  381. * @retval DECAF_FALSE The point is invalid.
  382. */
  383. decaf_bool_t decaf_448_point_valid (
  384. const decaf_448_point_t toTest
  385. ) API_VIS WARN_UNUSED NONNULL1 NOINLINE;
  386. /**
  387. * @brief 2-torque a point, for debugging purposes.
  388. *
  389. * @param [out] q The point to torque.
  390. * @param [in] p The point to torque.
  391. */
  392. void decaf_448_point_debugging_torque (
  393. decaf_448_point_t q,
  394. const decaf_448_point_t p
  395. ) API_VIS NONNULL2 NOINLINE;
  396. /**
  397. * @brief Almost-Elligator-like hash to curve.
  398. *
  399. * Call this function with the output of a hash to make a hash to the curve.
  400. *
  401. * This function runs Elligator2 on the decaf_448 Jacobi quartic model. It then
  402. * uses the isogeny to put the result in twisted Edwards form. As a result,
  403. * it is safe (cannot produce points of order 4), and would be compatible with
  404. * hypothetical other implementations of Decaf using a Montgomery or untwisted
  405. * Edwards model.
  406. *
  407. * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
  408. * A factor of 2 due to the isogeny.
  409. * A factor of 2 because we quotient out the 2-torsion.
  410. *
  411. * This makes it about 8:1 overall.
  412. *
  413. * Negating the input (mod q) results in the same point. Inverting the input
  414. * (mod q) results in the negative point. This is the same as Elligator.
  415. *
  416. * This function isn't quite indifferentiable from a random oracle.
  417. * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
  418. * Furthermore, calling it twice with independent seeds and adding the results
  419. * is indifferentiable from a random oracle.
  420. *
  421. * @param [in] hashed_data Output of some hash function.
  422. * @param [out] pt The data hashed to the curve.
  423. * @return A "hint" value which can be used to help invert the encoding.
  424. */
  425. unsigned char
  426. 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 Inverse of elligator-like hash to curve.
  432. *
  433. * This function writes to the buffer, to make it so that
  434. * decaf_448_point_from_hash_nonuniform(buffer) = pt,hint
  435. * if possible.
  436. *
  437. * @param [out] recovered_hash Encoded data.
  438. * @param [in] pt The point to encode.
  439. * @param [in] hint The hint value returned from
  440. * decaf_448_point_from_hash_nonuniform.
  441. *
  442. * @retval DECAF_SUCCESS The inverse succeeded.
  443. * @retval DECAF_FAILURE The pt isn't the image of
  444. * decaf_448_point_from_hash_nonuniform with the given hint.
  445. */
  446. decaf_bool_t
  447. decaf_448_invert_elligator_nonuniform (
  448. unsigned char recovered_hash[DECAF_448_SER_BYTES],
  449. const decaf_448_point_t pt,
  450. unsigned char hint
  451. ) API_VIS NONNULL2 NOINLINE WARN_UNUSED;
  452. /**
  453. * @brief Inverse of elligator-like hash to curve, uniform.
  454. *
  455. * This function modifies the first DECAF_448_SER_BYTES of the
  456. * buffer, to make it so that
  457. * decaf_448_point_from_hash_uniform(buffer) = pt,hint
  458. * if possible.
  459. *
  460. * @param [out] recovered_hash Encoded data.
  461. * @param [in] pt The point to encode.
  462. * @param [in] hint The hint value returned from
  463. * decaf_448_point_from_hash_nonuniform.
  464. *
  465. * @retval DECAF_SUCCESS The inverse succeeded.
  466. * @retval DECAF_FAILURE The pt isn't the image of
  467. * decaf_448_point_from_hash_uniform with the given hint.
  468. *
  469. * @warning The hinting system is subject to change, especially in corner cases.
  470. */
  471. decaf_bool_t
  472. decaf_448_invert_elligator_uniform (
  473. unsigned char recovered_hash[2*DECAF_448_SER_BYTES],
  474. const decaf_448_point_t pt,
  475. unsigned char hint
  476. ) API_VIS NONNULL2 NOINLINE WARN_UNUSED;
  477. /**
  478. * @brief Indifferentiable hash function encoding to curve.
  479. *
  480. * Equivalent to calling decaf_448_point_from_hash_nonuniform twice and adding.
  481. *
  482. * @param [in] hashed_data Output of some hash function.
  483. * @param [out] pt The data hashed to the curve.
  484. * @return A "hint" value which can be used to help invert the encoding.
  485. */
  486. unsigned char decaf_448_point_from_hash_uniform (
  487. decaf_448_point_t pt,
  488. const unsigned char hashed_data[2*DECAF_448_SER_BYTES]
  489. ) API_VIS NONNULL2 NOINLINE;
  490. /**
  491. * @brief Overwrite scalar with zeros.
  492. */
  493. void decaf_448_scalar_destroy (
  494. decaf_448_scalar_t scalar
  495. ) NONNULL1 API_VIS;
  496. /**
  497. * @brief Overwrite point with zeros.
  498. * @todo Use this internally.
  499. */
  500. void decaf_448_point_destroy (
  501. decaf_448_point_t point
  502. ) NONNULL1 API_VIS;
  503. /**
  504. * @brief Overwrite precomputed table with zeros.
  505. */
  506. void decaf_448_precomputed_destroy (
  507. decaf_448_precomputed_s *pre
  508. ) NONNULL1 API_VIS;
  509. #ifdef __cplusplus
  510. } /* extern "C" */
  511. #endif
  512. #endif /* __DECAF_448_H__ */