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.
 
 
 
 
 

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