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.
 
 
 
 
 

144 lines
2.9 KiB

  1. /**
  2. * @file field.h
  3. * @brief Generic field header.
  4. * @copyright
  5. * Copyright (c) 2014 Cryptography Research, Inc. \n
  6. * Released under the MIT License. See LICENSE.txt for license information.
  7. * @author Mike Hamburg
  8. */
  9. #ifndef __FIELD_H__
  10. #define __FIELD_H__
  11. #include "constant_time.h"
  12. #include "f_field.h"
  13. #include <string.h>
  14. /** @brief Bytes in a field element */
  15. #define FIELD_BYTES (1+(FIELD_BITS-1)/8)
  16. /** @brief Words in a field element */
  17. #define FIELD_WORDS (1+(FIELD_BITS-1)/sizeof(word_t))
  18. /* TODO: standardize notation */
  19. /** @brief The number of words in the Goldilocks field. */
  20. #define GOLDI_FIELD_WORDS DIV_CEIL(FIELD_BITS,WORD_BITS)
  21. /** @brief The number of bits in the Goldilocks curve's cofactor (cofactor=4). */
  22. #define COFACTOR_BITS 2
  23. /** @brief The number of bits in a Goldilocks scalar. */
  24. #define SCALAR_BITS (FIELD_BITS - COFACTOR_BITS)
  25. /** @brief The number of bytes in a Goldilocks scalar. */
  26. #define SCALAR_BYTES (1+(SCALAR_BITS)/8)
  27. /** @brief The number of words in the Goldilocks field. */
  28. #define SCALAR_WORDS WORDS_FOR_BITS(SCALAR_BITS)
  29. /**
  30. * @brief For GMP tests: little-endian representation of the field modulus.
  31. */
  32. extern const uint8_t FIELD_MODULUS[FIELD_BYTES];
  33. /**
  34. * Copy one field element to another.
  35. */
  36. static inline void
  37. __attribute__((unused,always_inline))
  38. field_copy (
  39. struct field_t *__restrict__ a,
  40. const struct field_t *__restrict__ b
  41. ) {
  42. memcpy(a,b,sizeof(*a));
  43. }
  44. /**
  45. * Negate a in place if doNegate.
  46. */
  47. static inline void
  48. __attribute__((unused,always_inline))
  49. field_cond_neg(
  50. field_t *a,
  51. mask_t doNegate
  52. ) {
  53. struct field_t negated;
  54. field_neg(&negated, a);
  55. field_bias(&negated, 2);
  56. constant_time_select(a, &negated, a, sizeof(negated), doNegate);
  57. }
  58. /**
  59. * Returns 1/sqrt(+- x).
  60. *
  61. * The Legendre symbol of the result is the same as that of the
  62. * input.
  63. *
  64. * If x=0, returns 0.
  65. */
  66. void
  67. field_isr (
  68. struct field_t* a,
  69. const struct field_t* x
  70. );
  71. /**
  72. * Batch inverts out[i] = 1/in[i]
  73. *
  74. * If any input is zero, all the outputs will be zero.
  75. */
  76. void
  77. field_simultaneous_invert (
  78. struct field_t *__restrict__ out,
  79. const struct field_t *in,
  80. unsigned int n
  81. );
  82. /**
  83. * Returns 1/x.
  84. *
  85. * If x=0, returns 0.
  86. */
  87. void
  88. field_inverse (
  89. struct field_t* a,
  90. const struct field_t* x
  91. );
  92. /**
  93. * Returns -1 if a==b, 0 otherwise.
  94. */
  95. mask_t
  96. field_eq (
  97. const struct field_t *a,
  98. const struct field_t *b
  99. );
  100. /**
  101. * Square x, n times.
  102. */
  103. static __inline__ void
  104. __attribute__((unused,always_inline))
  105. field_sqrn (
  106. field_t *__restrict__ y,
  107. const field_t *x,
  108. int n
  109. ) {
  110. field_t tmp;
  111. assert(n>0);
  112. if (n&1) {
  113. field_sqr(y,x);
  114. n--;
  115. } else {
  116. field_sqr(&tmp,x);
  117. field_sqr(y,&tmp);
  118. n-=2;
  119. }
  120. for (; n; n-=2) {
  121. field_sqr(&tmp,y);
  122. field_sqr(y,&tmp);
  123. }
  124. }
  125. #endif // __FIELD_H__