internal.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (c) 2020, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #ifndef OPENSSL_HEADER_CURVE25519_INTERNAL_H
  15. #define OPENSSL_HEADER_CURVE25519_INTERNAL_H
  16. #include <GFp/base.h>
  17. #include "../internal.h"
  18. #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_APPLE)
  19. #define BORINGSSL_X25519_NEON
  20. // x25519_NEON is defined in asm/x25519-arm.S.
  21. void GFp_x25519_NEON(uint8_t out[32], const uint8_t scalar[32],
  22. const uint8_t point[32]);
  23. #endif
  24. #if defined(BORINGSSL_HAS_UINT128)
  25. #define BORINGSSL_CURVE25519_64BIT
  26. #endif
  27. #if defined(BORINGSSL_CURVE25519_64BIT)
  28. // An element t,
  29. // entries t[0]...t[4], represents the integer t[0]+2^51 t[1]+2^102 t[2]+2^153
  30. // t[3]+2^204 t[4].
  31. // fe limbs are bounded by 1.125*2^51.
  32. // fe_loose limbs are bounded by 3.375*2^51.
  33. typedef uint64_t fe_limb_t;
  34. #define FE_NUM_LIMBS 5
  35. #else
  36. // An element t,
  37. // entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
  38. // t[3]+2^102 t[4]+...+2^230 t[9].
  39. // fe limbs are bounded by 1.125*2^26,1.125*2^25,1.125*2^26,1.125*2^25,etc.
  40. // fe_loose limbs are bounded by 3.375*2^26,3.375*2^25,3.375*2^26,3.375*2^25,etc.
  41. typedef uint32_t fe_limb_t;
  42. #define FE_NUM_LIMBS 10
  43. #endif
  44. // fe means field element. Here the field is \Z/(2^255-19).
  45. // Multiplication and carrying produce fe from fe_loose.
  46. // Keep in sync with `Elem` and `ELEM_LIMBS` in curve25519/ops.rs.
  47. typedef struct fe { fe_limb_t v[FE_NUM_LIMBS]; } fe;
  48. // Addition and subtraction produce fe_loose from (fe, fe).
  49. // Keep in sync with `Elem` and `ELEM_LIMBS` in curve25519/ops.rs.
  50. typedef struct fe_loose { fe_limb_t v[FE_NUM_LIMBS]; } fe_loose;
  51. static inline void fe_limbs_copy(fe_limb_t r[], const fe_limb_t a[]) {
  52. for (size_t i = 0; i < FE_NUM_LIMBS; ++i) {
  53. r[i] = a[i];
  54. }
  55. }
  56. // ge means group element.
  57. //
  58. // Here the group is the set of pairs (x,y) of field elements (see fe.h)
  59. // satisfying -x^2 + y^2 = 1 + d x^2y^2
  60. // where d = -121665/121666.
  61. //
  62. // Representations:
  63. // ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z
  64. // ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT
  65. // ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
  66. // ge_precomp (Duif): (y+x,y-x,2dxy)
  67. // Keep in sync with `Point` in curve25519/ops.rs.
  68. typedef struct {
  69. fe X;
  70. fe Y;
  71. fe Z;
  72. } ge_p2;
  73. // Keep in sync with `ExtPoint` in curve25519/ops.rs.
  74. typedef struct {
  75. fe X;
  76. fe Y;
  77. fe Z;
  78. fe T;
  79. } ge_p3;
  80. typedef struct {
  81. fe_loose X;
  82. fe_loose Y;
  83. fe_loose Z;
  84. fe_loose T;
  85. } ge_p1p1;
  86. typedef struct {
  87. fe_loose yplusx;
  88. fe_loose yminusx;
  89. fe_loose xy2d;
  90. } ge_precomp;
  91. typedef struct {
  92. fe_loose YplusX;
  93. fe_loose YminusX;
  94. fe_loose Z;
  95. fe_loose T2d;
  96. } ge_cached;
  97. #endif // OPENSSL_HEADER_CURVE25519_INTERNAL_H