ge_operations.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* ge_operations.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* Based On Daniel J Bernstein's ed25519 Public Domain ref10 work. */
  22. #ifndef WOLF_CRYPT_GE_OPERATIONS_H
  23. #define WOLF_CRYPT_GE_OPERATIONS_H
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifdef HAVE_ED25519
  26. #include <wolfssl/wolfcrypt/fe_operations.h>
  27. /*
  28. ge means group element.
  29. Here the group is the set of pairs (x,y) of field elements (see fe.h)
  30. satisfying -x^2 + y^2 = 1 + d x^2y^2
  31. where d = -121665/121666.
  32. Representations:
  33. ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z
  34. ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT
  35. ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
  36. ge_precomp (Duif): (y+x,y-x,2dxy)
  37. */
  38. #ifdef ED25519_SMALL
  39. ALIGN16 typedef byte ge[F25519_SIZE];
  40. #elif defined(CURVED25519_ASM_64BIT)
  41. ALIGN16 typedef sword64 ge[4];
  42. #elif defined(CURVED25519_ASM_32BIT)
  43. ALIGN16 typedef sword32 ge[8];
  44. #elif defined(CURVED25519_128BIT)
  45. ALIGN16 typedef sword64 ge[5];
  46. #else
  47. ALIGN16 typedef sword32 ge[10];
  48. #endif
  49. typedef struct {
  50. ge X;
  51. ge Y;
  52. ge Z;
  53. } ge_p2;
  54. typedef struct {
  55. ge X;
  56. ge Y;
  57. ge Z;
  58. ge T;
  59. } ge_p3;
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. WOLFSSL_LOCAL int ge_compress_key(byte* out, const byte* xIn, const byte* yIn,
  64. word32 keySz);
  65. WOLFSSL_LOCAL int ge_frombytes_negate_vartime(ge_p3 *h,const unsigned char *s);
  66. WOLFSSL_LOCAL int ge_double_scalarmult_vartime(ge_p2 *r, const unsigned char *a,
  67. const ge_p3 *A, const unsigned char *b);
  68. WOLFSSL_LOCAL void ge_scalarmult_base(ge_p3 *h,const unsigned char *a);
  69. WOLFSSL_LOCAL void sc_reduce(byte* s);
  70. WOLFSSL_LOCAL void sc_muladd(byte* s, const byte* a, const byte* b,
  71. const byte* c);
  72. WOLFSSL_LOCAL void ge_tobytes(unsigned char *s,const ge_p2 *h);
  73. #ifndef GE_P3_TOBYTES_IMPL
  74. #define ge_p3_tobytes(s, h) ge_tobytes((s), (const ge_p2 *)(h))
  75. #else
  76. WOLFSSL_LOCAL void ge_p3_tobytes(unsigned char *s,const ge_p3 *h);
  77. #endif
  78. #ifndef ED25519_SMALL
  79. typedef struct {
  80. ge X;
  81. ge Y;
  82. ge Z;
  83. ge T;
  84. } ge_p1p1;
  85. typedef struct {
  86. ge yplusx;
  87. ge yminusx;
  88. ge xy2d;
  89. } ge_precomp;
  90. typedef struct {
  91. ge YplusX;
  92. ge YminusX;
  93. ge Z;
  94. ge T2d;
  95. } ge_cached;
  96. #endif /* !ED25519_SMALL */
  97. #ifdef CURVED25519_ASM
  98. void ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p);
  99. void ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p);
  100. void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p);
  101. #define ge_p3_dbl(r, p) ge_p2_dbl((ge_p1p1 *)(r), (ge_p2 *)(p))
  102. void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q);
  103. void ge_msub(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q);
  104. void ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q);
  105. void ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q);
  106. #endif
  107. #ifdef __cplusplus
  108. } /* extern "C" */
  109. #endif
  110. #endif /* HAVE_ED25519 */
  111. #endif /* WOLF_CRYPT_GE_OPERATIONS_H */