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.
 
 
 
 
 

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