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.

field_5x52.h 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #ifndef _SECP256K1_FIELD_REPR_IMPL_H_
  2. #define _SECP256K1_FIELD_REPR_IMPL_H_
  3. #include <assert.h>
  4. #include <string.h>
  5. #include "../num.h"
  6. #include "../field.h"
  7. #if defined(USE_FIELD_5X52_ASM)
  8. #include "field_5x52_asm.h"
  9. #elif defined(USE_FIELD_5X52_INT128)
  10. #include "field_5x52_int128.h"
  11. #else
  12. #error "Please select field_5x52 implementation"
  13. #endif
  14. /** Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F,
  15. * represented as 5 uint64_t's in base 2^52. The values are allowed to contain >52 each. In particular,
  16. * each FieldElem has a 'magnitude' associated with it. Internally, a magnitude M means each element
  17. * is at most M*(2^53-1), except the most significant one, which is limited to M*(2^49-1). All operations
  18. * accept any input with magnitude at most M, and have different rules for propagating magnitude to their
  19. * output.
  20. */
  21. void static secp256k1_fe_inner_start(void) {}
  22. void static secp256k1_fe_inner_stop(void) {}
  23. void static secp256k1_fe_normalize(secp256k1_fe_t *r) {
  24. uint64_t c;
  25. c = r->n[0];
  26. uint64_t t0 = c & 0xFFFFFFFFFFFFFULL;
  27. c = (c >> 52) + r->n[1];
  28. uint64_t t1 = c & 0xFFFFFFFFFFFFFULL;
  29. c = (c >> 52) + r->n[2];
  30. uint64_t t2 = c & 0xFFFFFFFFFFFFFULL;
  31. c = (c >> 52) + r->n[3];
  32. uint64_t t3 = c & 0xFFFFFFFFFFFFFULL;
  33. c = (c >> 52) + r->n[4];
  34. uint64_t t4 = c & 0x0FFFFFFFFFFFFULL;
  35. c >>= 48;
  36. // The following code will not modify the t's if c is initially 0.
  37. c = c * 0x1000003D1ULL + t0;
  38. t0 = c & 0xFFFFFFFFFFFFFULL;
  39. c = (c >> 52) + t1;
  40. t1 = c & 0xFFFFFFFFFFFFFULL;
  41. c = (c >> 52) + t2;
  42. t2 = c & 0xFFFFFFFFFFFFFULL;
  43. c = (c >> 52) + t3;
  44. t3 = c & 0xFFFFFFFFFFFFFULL;
  45. c = (c >> 52) + t4;
  46. t4 = c & 0x0FFFFFFFFFFFFULL;
  47. assert((c >> 48) == 0);
  48. // Subtract p if result >= p
  49. uint64_t mask = -(int64_t)((t4 < 0xFFFFFFFFFFFFULL) | (t3 < 0xFFFFFFFFFFFFFULL) | (t2 < 0xFFFFFFFFFFFFFULL) | (t1 < 0xFFFFFFFFFFFFFULL) | (t0 < 0xFFFFEFFFFFC2FULL));
  50. t4 &= mask;
  51. t3 &= mask;
  52. t2 &= mask;
  53. t1 &= mask;
  54. t0 -= (~mask & 0xFFFFEFFFFFC2FULL);
  55. // push internal variables back
  56. r->n[0] = t0; r->n[1] = t1; r->n[2] = t2; r->n[3] = t3; r->n[4] = t4;
  57. #ifdef VERIFY
  58. r->magnitude = 1;
  59. r->normalized = 1;
  60. #endif
  61. }
  62. void static inline secp256k1_fe_set_int(secp256k1_fe_t *r, int a) {
  63. r->n[0] = a;
  64. r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
  65. #ifdef VERIFY
  66. r->magnitude = 1;
  67. r->normalized = 1;
  68. #endif
  69. }
  70. // TODO: not constant time!
  71. int static inline secp256k1_fe_is_zero(const secp256k1_fe_t *a) {
  72. #ifdef VERIFY
  73. assert(a->normalized);
  74. #endif
  75. return (a->n[0] == 0 && a->n[1] == 0 && a->n[2] == 0 && a->n[3] == 0 && a->n[4] == 0);
  76. }
  77. int static inline secp256k1_fe_is_odd(const secp256k1_fe_t *a) {
  78. #ifdef VERIFY
  79. assert(a->normalized);
  80. #endif
  81. return a->n[0] & 1;
  82. }
  83. // TODO: not constant time!
  84. int static inline secp256k1_fe_equal(const secp256k1_fe_t *a, const secp256k1_fe_t *b) {
  85. #ifdef VERIFY
  86. assert(a->normalized);
  87. assert(b->normalized);
  88. #endif
  89. return (a->n[0] == b->n[0] && a->n[1] == b->n[1] && a->n[2] == b->n[2] && a->n[3] == b->n[3] && a->n[4] == b->n[4]);
  90. }
  91. void static secp256k1_fe_set_b32(secp256k1_fe_t *r, const unsigned char *a) {
  92. r->n[0] = r->n[1] = r->n[2] = r->n[3] = r->n[4] = 0;
  93. for (int i=0; i<32; i++) {
  94. for (int j=0; j<2; j++) {
  95. int limb = (8*i+4*j)/52;
  96. int shift = (8*i+4*j)%52;
  97. r->n[limb] |= (uint64_t)((a[31-i] >> (4*j)) & 0xF) << shift;
  98. }
  99. }
  100. #ifdef VERIFY
  101. r->magnitude = 1;
  102. r->normalized = 1;
  103. #endif
  104. }
  105. /** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */
  106. void static secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe_t *a) {
  107. #ifdef VERIFY
  108. assert(a->normalized);
  109. #endif
  110. for (int i=0; i<32; i++) {
  111. int c = 0;
  112. for (int j=0; j<2; j++) {
  113. int limb = (8*i+4*j)/52;
  114. int shift = (8*i+4*j)%52;
  115. c |= ((a->n[limb] >> shift) & 0xF) << (4 * j);
  116. }
  117. r[31-i] = c;
  118. }
  119. }
  120. void static inline secp256k1_fe_negate(secp256k1_fe_t *r, const secp256k1_fe_t *a, int m) {
  121. #ifdef VERIFY
  122. assert(a->magnitude <= m);
  123. r->magnitude = m + 1;
  124. r->normalized = 0;
  125. #endif
  126. r->n[0] = 0xFFFFEFFFFFC2FULL * (m + 1) - a->n[0];
  127. r->n[1] = 0xFFFFFFFFFFFFFULL * (m + 1) - a->n[1];
  128. r->n[2] = 0xFFFFFFFFFFFFFULL * (m + 1) - a->n[2];
  129. r->n[3] = 0xFFFFFFFFFFFFFULL * (m + 1) - a->n[3];
  130. r->n[4] = 0x0FFFFFFFFFFFFULL * (m + 1) - a->n[4];
  131. }
  132. void static inline secp256k1_fe_mul_int(secp256k1_fe_t *r, int a) {
  133. #ifdef VERIFY
  134. r->magnitude *= a;
  135. r->normalized = 0;
  136. #endif
  137. r->n[0] *= a;
  138. r->n[1] *= a;
  139. r->n[2] *= a;
  140. r->n[3] *= a;
  141. r->n[4] *= a;
  142. }
  143. void static inline secp256k1_fe_add(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
  144. #ifdef VERIFY
  145. r->magnitude += a->magnitude;
  146. r->normalized = 0;
  147. #endif
  148. r->n[0] += a->n[0];
  149. r->n[1] += a->n[1];
  150. r->n[2] += a->n[2];
  151. r->n[3] += a->n[3];
  152. r->n[4] += a->n[4];
  153. }
  154. void static secp256k1_fe_mul(secp256k1_fe_t *r, const secp256k1_fe_t *a, const secp256k1_fe_t *b) {
  155. #ifdef VERIFY
  156. assert(a->magnitude <= 8);
  157. assert(b->magnitude <= 8);
  158. r->magnitude = 1;
  159. r->normalized = 0;
  160. #endif
  161. secp256k1_fe_mul_inner(a->n, b->n, r->n);
  162. }
  163. void static secp256k1_fe_sqr(secp256k1_fe_t *r, const secp256k1_fe_t *a) {
  164. #ifdef VERIFY
  165. assert(a->magnitude <= 8);
  166. r->magnitude = 1;
  167. r->normalized = 0;
  168. #endif
  169. secp256k1_fe_sqr_inner(a->n, r->n);
  170. }
  171. #endif