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.
 
 
 
 
 

230 lines
6.8 KiB

  1. /* Copyright (c) 2015 Cryptography Research, Inc.
  2. * Released under the MIT License. See LICENSE.txt for license information.
  3. */
  4. /**
  5. * @file decaf.h
  6. * @author Mike Hamburg
  7. * @brief A group of prime order p.
  8. *
  9. * The Decaf library implements cryptographic operations on a an elliptic curve
  10. * group of prime order p. It accomplishes this by using a twisted Edwards
  11. * curve (isogenous to Ed448-Goldilocks) and wiping out the cofactor.
  12. *
  13. * The formulas are all complete and have no special cases, except that
  14. * decaf_decode can fail because not every sequence of bytes is a valid group
  15. * element.
  16. *
  17. * The formulas contain no data-dependent branches, timing or memory accesses.
  18. */
  19. #ifndef __DECAF_H__
  20. #define __DECAF_H__ 1
  21. #include <stdint.h>
  22. typedef uint64_t decaf_word_t, decaf_bool_t;
  23. /* TODO: prefix all these operations and factor to support multiple curves. */
  24. /* TODO: perfield, so when 25519 hits this will change */
  25. #define DECAF_FIELD_BITS 448
  26. #define DECAF_LIMBS (512/8/sizeof(decaf_word_t))
  27. /** Number of bytes in a serialized point. One less bit than you'd think. */
  28. #define DECAF_SER_BYTES ((DECAF_FIELD_BITS+6)/8)
  29. /** Twisted Edwards (-1,d-1) extended homogeneous coordinates */
  30. typedef struct decaf_point_s {
  31. decaf_word_t x[DECAF_LIMBS],y[DECAF_LIMBS],z[DECAF_LIMBS],t[DECAF_LIMBS];
  32. } decaf_point_t[1];
  33. static const decaf_bool_t DECAF_TRUE = -(decaf_bool_t)1, DECAF_FALSE = 0;
  34. /** NB Success is -1, failure is 0. TODO: see if people would rather the reverse. */
  35. static const decaf_bool_t DECAF_SUCCESS = DECAF_TRUE, DECAF_FAILURE = DECAF_FALSE;
  36. /** The identity point on the curve. */
  37. const decaf_point_t decaf_identity;
  38. /** An arbitrarily chosen base point on the curve. TODO: define */
  39. const decaf_point_t decaf_basepoint;
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* Goldilocks' build flags default to hidden and stripping executables. */
  44. #define API_VIS __attribute__((visibility("default")))
  45. #define WARN_UNUSED __attribute__((warn_unused_result))
  46. #define NONNULL1 __attribute__((nonnull(1)))
  47. #define NONNULL2 __attribute__((nonnull(1,2)))
  48. #define NONNULL3 __attribute__((nonnull(1,2,3)))
  49. /**
  50. * @brief Encode a point as a sequence of bytes.
  51. *
  52. * @param [out] ser The byte representation of the point.
  53. * @param [in] pt The point to encode.
  54. */
  55. void decaf_encode (
  56. uint8_t ser[DECAF_SER_BYTES],
  57. const decaf_point_t pt
  58. ) API_VIS NONNULL2;
  59. /**
  60. * @brief Decode a point from a sequence of bytes.
  61. *
  62. * Every point has a unique encoding, so not every
  63. * sequence of bytes is a valid encoding. If an invalid
  64. * encoding is given, the output is undefined.
  65. *
  66. * @param [out] pt The decoded point.
  67. * @param [in] ser The serialized version of the point.
  68. * @retval DECAF_SUCCESS The decoding succeeded.
  69. * @retval DECAF_FAILURE The decoding didn't succeed, because
  70. * ser does not represent a point.
  71. */
  72. decaf_bool_t decaf_decode (
  73. decaf_point_t pt,
  74. const uint8_t ser[DECAF_SER_BYTES],
  75. decaf_bool_t allow_identity
  76. ) API_VIS WARN_UNUSED NONNULL2;
  77. /**
  78. * @brief Copy a point. The input and output may alias,
  79. * in which case this function does nothing.
  80. *
  81. * @param [out] a A copy of the point.
  82. * @param [in] b Any point.
  83. */
  84. void decaf_copy (
  85. decaf_point_t a,
  86. const decaf_point_t b
  87. ) API_VIS NONNULL2;
  88. /**
  89. * @brief Test whether two points are equal. If yes, return
  90. * DECAF_TRUE, else return DECAF_FALSE.
  91. *
  92. * @param [in] a A point.
  93. * @param [in] b Another point.
  94. * @retval DECAF_TRUE The points are equal.
  95. * @retval DECAF_FALSE The points are not equal.
  96. */
  97. decaf_bool_t decaf_eq (
  98. const decaf_point_t a,
  99. const decaf_point_t b
  100. ) API_VIS WARN_UNUSED NONNULL2;
  101. /**
  102. * @brief Add two points to produce a third point. The
  103. * input points and output point can be pointers to the same
  104. * memory.
  105. *
  106. * @param [out] sum The sum a+b.
  107. * @param [in] a An addend.
  108. * @param [in] b An addend.
  109. */
  110. void decaf_add (
  111. decaf_point_t sum,
  112. const decaf_point_t a,
  113. const decaf_point_t b
  114. ) API_VIS NONNULL3;
  115. /**
  116. * @brief Subtract two points to produce a third point. The
  117. * input points and output point can be pointers to the same
  118. * memory.
  119. *
  120. * @param [out] sum The difference a-b.
  121. * @param [in] a The minuend.
  122. * @param [in] b The subtrahend.
  123. */
  124. void decaf_sub (
  125. decaf_point_t diff,
  126. const decaf_point_t a,
  127. const decaf_point_t b
  128. ) API_VIS NONNULL3;
  129. /**
  130. * @brief Multiply a base point by a scalar.
  131. *
  132. * @param [out] scaled The scaled point base*scalar
  133. * @param [in] base The point to be scaled.
  134. * @param [in] scalar The scalar to multilpy by.
  135. * @param [in] scalar_words The number of words in the scalar [TODO]
  136. */
  137. void decaf_scalarmul (
  138. decaf_point_t scaled,
  139. const decaf_point_t base,
  140. const decaf_word_t *scalar,
  141. unsigned int scalar_words
  142. ) API_VIS NONNULL3;
  143. /**
  144. * @brief Test that a point is valid, for debugging purposes.
  145. *
  146. * @param [in] point The number to test.
  147. * @retval DECAF_TRUE The point is valid.
  148. * @retval DECAF_FALSE The point is invalid.
  149. */
  150. decaf_bool_t decaf_valid (
  151. const decaf_point_t toTest
  152. ) API_VIS WARN_UNUSED NONNULL1;
  153. /**
  154. * @brief Almost-Elligator-like hash to curve.
  155. *
  156. * Call this function with the output of a hash to make a hash to the curve.
  157. *
  158. * This function runs Elligator2 on the decaf Jacobi quartic model. It then
  159. * uses the isogeny to put the result in twisted Edwards form. As a result,
  160. * it is safe (cannot produce points of order 4), and would be compatible with
  161. * hypothetical other implementations of Decaf using a Montgomery or untwisted
  162. * Edwards model.
  163. *
  164. * Unlike Elligator, this function may be up to 4:1 on [0,(p-1)/2]:
  165. * A factor of 2 due to the isogeny.
  166. * A factor of 2 because we quotient out the 2-torsion.
  167. * // TODO: check that it isn't more, especially for the identity point.
  168. *
  169. * Negating the input (mod q) results in the same point. Inverting the input
  170. * (mod q) results in the negative point. This is the same as Elligator.
  171. *
  172. * This function isn't quite indifferentiable from a random oracle.
  173. * However, it is suitable for many protocols, including SPEKE and SPAKE2 EE.
  174. * Furthermore, calling it twice with independent seeds and adding the results
  175. * is indifferentiable from a random oracle.
  176. *
  177. * @param [in] hashed_data Output of some hash function.
  178. * @param [out] pt The data hashed to the curve.
  179. */
  180. void decaf_nonuniform_map_to_curve (
  181. decaf_point_t pt,
  182. const unsigned char hashed_data[DECAF_SER_BYTES]
  183. ) API_VIS NONNULL2;
  184. /**
  185. * @brief Indifferentiable hash function encoding to curve.
  186. *
  187. * Equivalent to calling decaf_nonuniform_map_to_curve twice and adding.
  188. *
  189. * @param [in] hashed_data Output of some hash function.
  190. * @param [out] pt The data hashed to the curve.
  191. */
  192. void decaf_uniform_map_to_curve (
  193. decaf_point_t pt,
  194. const unsigned char hashed_data[2*DECAF_SER_BYTES]
  195. ) API_VIS NONNULL2;
  196. #undef API_VIS
  197. #undef WARN_UNUSED
  198. #undef NONNULL2
  199. #undef NONNULL3
  200. #ifdef __cplusplus
  201. }; /* extern "C" */
  202. #endif
  203. #endif /* __DECAF_H__ */