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.
 
 
 
 
 

572 lines
17 KiB

  1. /**
  2. * @file decaf_255.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 Ed25519.
  10. */
  11. #ifndef __DECAF_255_H__
  12. #define __DECAF_255_H__ 1
  13. #include "decaf_common.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define DECAF_255_LIMBS (320/DECAF_WORD_BITS)
  18. #define DECAF_255_SCALAR_BITS 254 // Curve25519: 253
  19. #define DECAF_255_SCALAR_LIMBS (256/DECAF_WORD_BITS)
  20. /** Galois field element internal structure */
  21. typedef struct gf_255_s {
  22. decaf_word_t limb[DECAF_255_LIMBS];
  23. } gf_255_s, gf_255_t[1];
  24. /** @endcond */
  25. /** Number of bytes in a serialized point. */
  26. #define DECAF_255_SER_BYTES 32
  27. /** Number of bytes in a serialized scalar. */
  28. #define DECAF_255_SCALAR_BYTES 32
  29. /** Twisted Edwards (-1,d-1) extended homogeneous coordinates */
  30. typedef struct decaf_255_point_s { /**@cond internal*/gf_255_t x,y,z,t;/**@endcond*/ } decaf_255_point_t[1];
  31. /** Precomputed table based on a point. Can be trivial implementation. */
  32. struct decaf_255_precomputed_s;
  33. /** Precomputed table based on a point. Can be trivial implementation. */
  34. typedef struct decaf_255_precomputed_s decaf_255_precomputed_s;
  35. /** Size and alignment of precomputed point tables. */
  36. extern const size_t sizeof_decaf_255_precomputed_s API_VIS, alignof_decaf_255_precomputed_s API_VIS;
  37. /** Scalar is stored packed, because we don't need the speed. */
  38. typedef struct decaf_255_scalar_s {
  39. /** @cond internal */
  40. decaf_word_t limb[DECAF_255_SCALAR_LIMBS];
  41. /** @endcond */
  42. } decaf_255_scalar_t[1];
  43. /** A scalar equal to 1. */
  44. extern const decaf_255_scalar_t decaf_255_scalar_one API_VIS;
  45. /** A scalar equal to 0. */
  46. extern const decaf_255_scalar_t decaf_255_scalar_zero API_VIS;
  47. /** The identity point on the curve. */
  48. extern const decaf_255_point_t decaf_255_point_identity API_VIS;
  49. /** An arbitrarily chosen base point on the curve. */
  50. extern const decaf_255_point_t decaf_255_point_base API_VIS;
  51. /** Precomputed table for the base point on the curve. */
  52. extern const struct decaf_255_precomputed_s *decaf_255_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_255_scalar_decode (
  64. decaf_255_scalar_t out,
  65. const unsigned char ser[DECAF_255_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_255_scalar_decode_long (
  76. decaf_255_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_255_scalar_encode (
  87. unsigned char ser[DECAF_255_SCALAR_BYTES],
  88. const decaf_255_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_255_scalar_add (
  97. decaf_255_scalar_t out,
  98. const decaf_255_scalar_t a,
  99. const decaf_255_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_255_scalar_eq (
  109. const decaf_255_scalar_t a,
  110. const decaf_255_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_255_scalar_sub (
  119. decaf_255_scalar_t out,
  120. const decaf_255_scalar_t a,
  121. const decaf_255_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_255_scalar_mul (
  130. decaf_255_scalar_t out,
  131. const decaf_255_scalar_t a,
  132. const decaf_255_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_255_scalar_invert (
  141. decaf_255_scalar_t out,
  142. const decaf_255_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_255_scalar_copy (
  151. decaf_255_scalar_t out,
  152. const decaf_255_scalar_t a
  153. ) {
  154. *out = *a;
  155. }
  156. /**
  157. * @brief Set a scalar to an unsigned integer.
  158. * @param [in] a An integer.
  159. * @param [out] out Will become equal to a.
  160. */
  161. void decaf_255_scalar_set_unsigned (
  162. decaf_255_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_255_point_encode (
  172. uint8_t ser[DECAF_255_SER_BYTES],
  173. const decaf_255_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_255_point_decode (
  190. decaf_255_point_t pt,
  191. const uint8_t ser[DECAF_255_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_255_point_copy (
  202. decaf_255_point_t a,
  203. const decaf_255_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_255_point_eq (
  217. const decaf_255_point_t a,
  218. const decaf_255_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_255_point_add (
  230. decaf_255_point_t sum,
  231. const decaf_255_point_t a,
  232. const decaf_255_point_t b
  233. ) API_VIS NONNULL3;
  234. /**
  235. * @brief Double a point. Equivalent to
  236. * decaf_255_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_255_point_double (
  242. decaf_255_point_t two_a,
  243. const decaf_255_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_255_point_sub (
  255. decaf_255_point_t diff,
  256. const decaf_255_point_t a,
  257. const decaf_255_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_255_point_negate (
  267. decaf_255_point_t nega,
  268. const decaf_255_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_255_point_scalarmul (
  278. decaf_255_point_t scaled,
  279. const decaf_255_point_t base,
  280. const decaf_255_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_255_direct_scalarmul (
  300. uint8_t scaled[DECAF_255_SER_BYTES],
  301. const uint8_t base[DECAF_255_SER_BYTES],
  302. const decaf_255_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_255_precompute (
  316. decaf_255_precomputed_s *a,
  317. const decaf_255_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_255_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_255_precomputed_scalarmul (
  331. decaf_255_point_t scaled,
  332. const decaf_255_precomputed_s *base,
  333. const decaf_255_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_255_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_255_point_double_scalarmul (
  349. decaf_255_point_t combo,
  350. const decaf_255_point_t base1,
  351. const decaf_255_scalar_t scalar1,
  352. const decaf_255_point_t base2,
  353. const decaf_255_scalar_t scalar2
  354. ) API_VIS NONNULL5 NOINLINE;
  355. /**
  356. * @brief Multiply two base points by two scalars:
  357. * scaled = scalar1*decaf_255_point_base + scalar2*base2.
  358. *
  359. * Otherwise equivalent to decaf_255_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_255_base_double_scalarmul_non_secret (
  371. decaf_255_point_t combo,
  372. const decaf_255_scalar_t scalar1,
  373. const decaf_255_point_t base2,
  374. const decaf_255_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_255_point_valid (
  384. const decaf_255_point_t toTest
  385. ) API_VIS WARN_UNUSED NONNULL1 NOINLINE;
  386. /**
  387. * @brief Torque a point, for debugging purposes. The output
  388. * will be equal to the input.
  389. *
  390. * @param [out] q The point to torque.
  391. * @param [in] p The point to torque.
  392. */
  393. void decaf_255_point_debugging_torque (
  394. decaf_255_point_t q,
  395. const decaf_255_point_t p
  396. ) API_VIS NONNULL2 NOINLINE;
  397. /**
  398. * @brief Projectively scale a point, for debugging purposes.
  399. * The output will be equal to the input, and will be valid
  400. * even if the factor is zero.
  401. *
  402. * @param [out] q The point to scale.
  403. * @param [in] p The point to scale.
  404. * @param [in] factor Serialized GF factor to scale.
  405. */
  406. void decaf_255_point_debugging_pscale (
  407. decaf_255_point_t q,
  408. const decaf_255_point_t p,
  409. const unsigned char factor[DECAF_255_SER_BYTES]
  410. ) API_VIS NONNULL2 NOINLINE;
  411. /**
  412. * @brief Almost-Elligator-like hash to curve.
  413. *
  414. * Call this function with the output of a hash to make a hash to the curve.
  415. *
  416. * This function runs Elligator2 on the decaf_255 Jacobi quartic model. It then
  417. * uses the isogeny to put the result in twisted Edwards form. As a result,
  418. * it is safe (cannot produce points of order 4), and would be compatible with
  419. * hypothetical other implementations of Decaf using a Montgomery or untwisted
  420. * Edwards model.
  421. *
  422. * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
  423. * A factor of 2 due to the isogeny.
  424. * A factor of 2 because we quotient out the 2-torsion.
  425. *
  426. * This makes it about 8:1 overall.
  427. *
  428. * Negating the input (mod q) results in the same point. Inverting the input
  429. * (mod q) results in the negative point. This is the same as Elligator.
  430. *
  431. * This function isn't quite indifferentiable from a random oracle.
  432. * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
  433. * Furthermore, calling it twice with independent seeds and adding the results
  434. * is indifferentiable from a random oracle.
  435. *
  436. * @param [in] hashed_data Output of some hash function.
  437. * @param [out] pt The data hashed to the curve.
  438. */
  439. void
  440. decaf_255_point_from_hash_nonuniform (
  441. decaf_255_point_t pt,
  442. const unsigned char hashed_data[DECAF_255_SER_BYTES]
  443. ) API_VIS NONNULL2 NOINLINE;
  444. /**
  445. * @brief Inverse of elligator-like hash to curve.
  446. *
  447. * This function writes to the buffer, to make it so that
  448. * decaf_255_point_from_hash_nonuniform(buffer) = pt if possible.
  449. *
  450. * @param [out] recovered_hash Encoded data.
  451. * @param [in] pt The point to encode.
  452. * @param [in] which A "hint" that indicates which inverse to return.
  453. *
  454. * @retval DECAF_SUCCESS The inverse succeeded.
  455. * @retval DECAF_FAILURE The pt isn't the image of
  456. * decaf_255_point_from_hash_nonuniform with the given hint.
  457. */
  458. decaf_bool_t
  459. decaf_255_invert_elligator_nonuniform (
  460. unsigned char recovered_hash[DECAF_255_SER_BYTES],
  461. const decaf_255_point_t pt,
  462. uint16_t which
  463. ) API_VIS NONNULL2 NOINLINE WARN_UNUSED;
  464. /**
  465. * @brief Inverse of elligator-like hash to curve, uniform.
  466. *
  467. * This function modifies the first DECAF_255_SER_BYTES of the
  468. * buffer, to make it so that
  469. * decaf_255_point_from_hash_uniform(buffer) = pt if possible.
  470. *
  471. * @param [out] recovered_hash Encoded data.
  472. * @param [in] pt The point to encode.
  473. * @param [in] which A "hint" that indicates which inverse to return.
  474. *
  475. * @retval DECAF_SUCCESS The inverse succeeded.
  476. * @retval DECAF_FAILURE The pt isn't the image of
  477. * decaf_255_point_from_hash_uniform with the given hint.
  478. */
  479. decaf_bool_t
  480. decaf_255_invert_elligator_uniform (
  481. unsigned char recovered_hash[2*DECAF_255_SER_BYTES],
  482. const decaf_255_point_t pt,
  483. uint16_t which
  484. ) API_VIS NONNULL2 NOINLINE WARN_UNUSED;
  485. /**
  486. * @brief Indifferentiable hash function encoding to curve.
  487. *
  488. * Equivalent to calling decaf_255_point_from_hash_nonuniform twice and adding.
  489. *
  490. * @param [in] hashed_data Output of some hash function.
  491. * @param [out] pt The data hashed to the curve.
  492. */
  493. void decaf_255_point_from_hash_uniform (
  494. decaf_255_point_t pt,
  495. const unsigned char hashed_data[2*DECAF_255_SER_BYTES]
  496. ) API_VIS NONNULL2 NOINLINE;
  497. /**
  498. * @brief Overwrite scalar with zeros.
  499. */
  500. void decaf_255_scalar_destroy (
  501. decaf_255_scalar_t scalar
  502. ) NONNULL1 API_VIS;
  503. /**
  504. * @brief Overwrite point with zeros.
  505. * @todo Use this internally.
  506. */
  507. void decaf_255_point_destroy (
  508. decaf_255_point_t point
  509. ) NONNULL1 API_VIS;
  510. /**
  511. * @brief Overwrite precomputed table with zeros.
  512. */
  513. void decaf_255_precomputed_destroy (
  514. decaf_255_precomputed_s *pre
  515. ) NONNULL1 API_VIS;
  516. #ifdef __cplusplus
  517. } /* extern "C" */
  518. #endif
  519. #endif /* __DECAF_255_H__ */