curve25519-donna-helpers.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Public domain by Andrew M. <liquidsun@gmail.com>
  3. See: https://github.com/floodyberry/curve25519-donna
  4. Curve25519 implementation agnostic helpers
  5. */
  6. #ifdef __GNUC__
  7. #define ED_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
  8. #endif
  9. #if __GNUC__ && ED_GCC_VERSION >= 401
  10. #if ED_GCC_VERSION >= 406
  11. #pragma GCC diagnostic push
  12. #endif
  13. /* Some versions of GCC (particularly on arm) give us bogus warnings here.
  14. * Suppress the GCC warning so we can build Tor with -Wstack-protector. */
  15. #pragma GCC diagnostic ignored "-Wstack-protector"
  16. #endif
  17. /*
  18. * In: b = 2^5 - 2^0
  19. * Out: b = 2^250 - 2^0
  20. */
  21. static void
  22. curve25519_pow_two5mtwo0_two250mtwo0(bignum25519 b) {
  23. bignum25519 ALIGN(16) t0,c;
  24. /* 2^5 - 2^0 */ /* b */
  25. /* 2^10 - 2^5 */ curve25519_square_times(t0, b, 5);
  26. /* 2^10 - 2^0 */ curve25519_mul_noinline(b, t0, b);
  27. /* 2^20 - 2^10 */ curve25519_square_times(t0, b, 10);
  28. /* 2^20 - 2^0 */ curve25519_mul_noinline(c, t0, b);
  29. /* 2^40 - 2^20 */ curve25519_square_times(t0, c, 20);
  30. /* 2^40 - 2^0 */ curve25519_mul_noinline(t0, t0, c);
  31. /* 2^50 - 2^10 */ curve25519_square_times(t0, t0, 10);
  32. /* 2^50 - 2^0 */ curve25519_mul_noinline(b, t0, b);
  33. /* 2^100 - 2^50 */ curve25519_square_times(t0, b, 50);
  34. /* 2^100 - 2^0 */ curve25519_mul_noinline(c, t0, b);
  35. /* 2^200 - 2^100 */ curve25519_square_times(t0, c, 100);
  36. /* 2^200 - 2^0 */ curve25519_mul_noinline(t0, t0, c);
  37. /* 2^250 - 2^50 */ curve25519_square_times(t0, t0, 50);
  38. /* 2^250 - 2^0 */ curve25519_mul_noinline(b, t0, b);
  39. }
  40. /*
  41. * z^(p - 2) = z(2^255 - 21)
  42. */
  43. static void
  44. curve25519_recip(bignum25519 out, const bignum25519 z) {
  45. bignum25519 ALIGN(16) a,t0,b;
  46. /* 2 */ curve25519_square_times(a, z, 1); /* a = 2 */
  47. /* 8 */ curve25519_square_times(t0, a, 2);
  48. /* 9 */ curve25519_mul_noinline(b, t0, z); /* b = 9 */
  49. /* 11 */ curve25519_mul_noinline(a, b, a); /* a = 11 */
  50. /* 22 */ curve25519_square_times(t0, a, 1);
  51. /* 2^5 - 2^0 = 31 */ curve25519_mul_noinline(b, t0, b);
  52. /* 2^250 - 2^0 */ curve25519_pow_two5mtwo0_two250mtwo0(b);
  53. /* 2^255 - 2^5 */ curve25519_square_times(b, b, 5);
  54. /* 2^255 - 21 */ curve25519_mul_noinline(out, b, a);
  55. }
  56. /*
  57. * z^((p-5)/8) = z^(2^252 - 3)
  58. */
  59. static void
  60. curve25519_pow_two252m3(bignum25519 two252m3, const bignum25519 z) {
  61. bignum25519 ALIGN(16) b,c,t0;
  62. /* 2 */ curve25519_square_times(c, z, 1); /* c = 2 */
  63. /* 8 */ curve25519_square_times(t0, c, 2); /* t0 = 8 */
  64. /* 9 */ curve25519_mul_noinline(b, t0, z); /* b = 9 */
  65. /* 11 */ curve25519_mul_noinline(c, b, c); /* c = 11 */
  66. /* 22 */ curve25519_square_times(t0, c, 1);
  67. /* 2^5 - 2^0 = 31 */ curve25519_mul_noinline(b, t0, b);
  68. /* 2^250 - 2^0 */ curve25519_pow_two5mtwo0_two250mtwo0(b);
  69. /* 2^252 - 2^2 */ curve25519_square_times(b, b, 2);
  70. /* 2^252 - 3 */ curve25519_mul_noinline(two252m3, b, z);
  71. }
  72. #if __GNUC__ && ED_GCC_VERSION >= 406
  73. #pragma GCC diagnostic pop
  74. #endif