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.
 
 
 
 
 

171 lines
4.7 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 NONNULL2 __attribute__((nonnull(1,2)))
  46. #define NONNULL3 __attribute__((nonnull(1,2,3)))
  47. /**
  48. * @brief Encode a point as a sequence of bytes.
  49. *
  50. * @param [out] ser The byte representation of the point.
  51. * @param [in] pt The point to encode.
  52. */
  53. void decaf_encode (
  54. uint8_t ser[DECAF_SER_BYTES],
  55. const decaf_point_t pt
  56. ) API_VIS NONNULL2;
  57. /**
  58. * @brief Decode a point from a sequence of bytes.
  59. *
  60. * Every point has a unique encoding, so not every
  61. * sequence of bytes is a valid encoding. If an invalid
  62. * encoding is given, the output is undefined.
  63. *
  64. * @param [out] pt The decoded point.
  65. * @param [in] ser The serialized version of the point.
  66. * @retval DECAF_SUCCESS The decoding succeeded.
  67. * @retval DECAF_FAILURE The decoding didn't succeed, because
  68. * ser does not represent a point.
  69. */
  70. decaf_bool_t decaf_decode (
  71. decaf_point_t pt,
  72. const uint8_t ser[DECAF_SER_BYTES],
  73. decaf_bool_t allow_identity
  74. ) API_VIS WARN_UNUSED NONNULL2;
  75. /**
  76. * @brief Copy a point. The input and output may alias,
  77. * in which case this function does nothing.
  78. *
  79. * @param [out] a A copy of the point.
  80. * @param [in] b Any point.
  81. */
  82. void decaf_copy (
  83. decaf_point_t a,
  84. const decaf_point_t b
  85. ) API_VIS NONNULL2;
  86. /**
  87. * @brief Test whether two points are equal. If yes, return
  88. * DECAF_TRUE, else return DECAF_FALSE.
  89. *
  90. * @param [in] a A point.
  91. * @param [in] b Another point.
  92. * @retval DECAF_TRUE The points are equal.
  93. * @retval DECAF_FALSE The points are not equal.
  94. */
  95. decaf_bool_t decaf_eq (
  96. const decaf_point_t a,
  97. const decaf_point_t b
  98. ) API_VIS WARN_UNUSED NONNULL2;
  99. /**
  100. * @brief Add two points to produce a third point. The
  101. * input points and output point can be pointers to the same
  102. * memory.
  103. *
  104. * @param [out] sum The sum a+b.
  105. * @param [in] a An addend.
  106. * @param [in] b An addend.
  107. */
  108. void decaf_add (
  109. decaf_point_t sum,
  110. const decaf_point_t a,
  111. const decaf_point_t b
  112. ) API_VIS NONNULL3;
  113. /**
  114. * @brief Subtract two points to produce a third point. The
  115. * input points and output point can be pointers to the same
  116. * memory.
  117. *
  118. * @param [out] sum The difference a-b.
  119. * @param [in] a The minuend.
  120. * @param [in] b The subtrahend.
  121. */
  122. void decaf_sub (
  123. decaf_point_t diff,
  124. const decaf_point_t a,
  125. const decaf_point_t b
  126. ) API_VIS NONNULL3;
  127. /**
  128. * @brief Multiply a base point by a scalar.
  129. *
  130. * @param [out] scaled The scaled point base*scalar
  131. * @param [in] base The point to be scaled.
  132. * @param [in] scalar The scalar to multilpy by.
  133. * @param [in] scalar_words The number of words in the scalar [TODO]
  134. */
  135. void decaf_scalarmul (
  136. decaf_point_t scaled,
  137. const decaf_point_t base,
  138. const decaf_word_t *scalar,
  139. unsigned int scalar_words
  140. ) API_VIS NONNULL3;
  141. #undef API_VIS
  142. #undef WARN_UNUSED
  143. #undef NONNULL2
  144. #undef NONNULL3
  145. #ifdef __cplusplus
  146. }; /* extern "C" */
  147. #endif
  148. #endif /* __DECAF_H__ */