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.
 
 
 
 
 

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