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.
 
 
 
 
 

170 lines
3.0 KiB

  1. /* Copyright (c) 2014 Cryptography Research, Inc.
  2. * Released under the MIT License. See LICENSE.txt for license information.
  3. */
  4. #ifndef __P25519_H__
  5. #define __P25519_H__ 1
  6. #include <stdint.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include "word.h"
  10. #ifndef __DECAF_255_H__ // HACK FIXME
  11. #define DECAF_WORD_BITS 64
  12. typedef struct gf_25519_s {
  13. uint64_t limb[5];
  14. } gf_25519_s, gf_25519_t[1];
  15. #endif
  16. #define LBITS 51
  17. #define FIELD_LITERAL(a,b,c,d,e) {{ a,b,c,d,e }}
  18. /*
  19. #define FIELD_LITERAL(a,b,c,d) {{ \
  20. (a##ull) & LMASK, \
  21. ((a##ull)>>51 | (b##ull)<<13) & LMASK, \
  22. ((b##ull)>>38 | (c##ull)<<26) & LMASK, \
  23. ((c##ull)>>25 | (d##ull)<<39) & LMASK, \
  24. (d##ull)>>12 \
  25. }}
  26. */
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. static __inline__ void
  31. gf_25519_add_RAW (
  32. gf_25519_t out,
  33. const gf_25519_t a,
  34. const gf_25519_t b
  35. ) __attribute__((unused));
  36. static __inline__ void
  37. gf_25519_sub_RAW (
  38. gf_25519_t out,
  39. const gf_25519_t a,
  40. const gf_25519_t b
  41. ) __attribute__((unused));
  42. static __inline__ void
  43. gf_25519_copy (
  44. gf_25519_t out,
  45. const gf_25519_t a
  46. ) __attribute__((unused));
  47. static __inline__ void
  48. gf_25519_weak_reduce (
  49. gf_25519_t inout
  50. ) __attribute__((unused));
  51. void
  52. gf_25519_strong_reduce (
  53. gf_25519_t inout
  54. );
  55. static __inline__ void
  56. gf_25519_bias (
  57. gf_25519_t inout,
  58. int amount
  59. ) __attribute__((unused));
  60. void
  61. gf_25519_mul (
  62. gf_25519_s *__restrict__ out,
  63. const gf_25519_t a,
  64. const gf_25519_t b
  65. );
  66. void
  67. gf_25519_mulw (
  68. gf_25519_s *__restrict__ out,
  69. const gf_25519_t a,
  70. uint64_t b
  71. );
  72. void
  73. gf_25519_sqr (
  74. gf_25519_s *__restrict__ out,
  75. const gf_25519_t a
  76. );
  77. void
  78. gf_25519_serialize (
  79. uint8_t serial[32],
  80. const gf_25519_t x
  81. );
  82. mask_t
  83. gf_25519_deserialize (
  84. gf_25519_t x,
  85. const uint8_t serial[32]
  86. );
  87. /* -------------- Inline functions begin here -------------- */
  88. void
  89. gf_25519_add_RAW (
  90. gf_25519_t out,
  91. const gf_25519_t a,
  92. const gf_25519_t b
  93. ) {
  94. unsigned int i;
  95. for (i=0; i<5; i++) {
  96. out->limb[i] = a->limb[i] + b->limb[i];
  97. }
  98. }
  99. void
  100. gf_25519_sub_RAW (
  101. gf_25519_t out,
  102. const gf_25519_t a,
  103. const gf_25519_t b
  104. ) {
  105. unsigned int i;
  106. uint64_t co1 = ((1ull<<51)-1)*2, co2 = co1-36;
  107. for (i=0; i<5; i++) {
  108. out->limb[i] = a->limb[i] - b->limb[i] + ((i==0) ? co2 : co1);
  109. }
  110. }
  111. void
  112. gf_25519_copy (
  113. gf_25519_t out,
  114. const gf_25519_t a
  115. ) {
  116. memcpy(out,a,sizeof(*a));
  117. }
  118. void
  119. gf_25519_bias (
  120. gf_25519_t a,
  121. int amt
  122. ) {
  123. a->limb[0] += ((uint64_t)(amt)<<52) - 38*amt;
  124. int i;
  125. for (i=1; i<5; i++) {
  126. a->limb[i] += ((uint64_t)(amt)<<52)-2*amt;
  127. }
  128. }
  129. void
  130. gf_25519_weak_reduce (
  131. gf_25519_t a
  132. ) {
  133. uint64_t mask = (1ull<<51) - 1;
  134. uint64_t tmp = a->limb[4] >> 51;
  135. int i;
  136. for (i=4; i>0; i--) {
  137. a->limb[i] = (a->limb[i] & mask) + (a->limb[i-1]>>51);
  138. }
  139. a->limb[0] = (a->limb[0] & mask) + tmp*19;
  140. }
  141. #ifdef __cplusplus
  142. }; /* extern "C" */
  143. #endif
  144. #endif /* __P25519_H__ */