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.
 
 
 
 
 

97 lines
1.7 KiB

  1. /**
  2. * @file field.h
  3. * @brief Generic gf 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 __GF_H__
  10. #define __GF_H__
  11. #include "constant_time.h"
  12. #include "f_field.h"
  13. #include <string.h>
  14. /**
  15. * Returns 1/sqrt(+- x).
  16. *
  17. * The Legendre symbol of the result is the same as that of the
  18. * input.
  19. *
  20. * If x=0, returns 0.
  21. */
  22. void
  23. gf_isr (
  24. gf a,
  25. const gf x
  26. );
  27. /**
  28. * Square x, n times.
  29. */
  30. static __inline__ void
  31. __attribute__((unused,always_inline))
  32. gf_sqrn (
  33. gf_s *__restrict__ y,
  34. const gf x,
  35. int n
  36. ) {
  37. gf tmp;
  38. assert(n>0);
  39. if (n&1) {
  40. gf_sqr(y,x);
  41. n--;
  42. } else {
  43. gf_sqr(tmp,x);
  44. gf_sqr(y,tmp);
  45. n-=2;
  46. }
  47. for (; n; n-=2) {
  48. gf_sqr(tmp,y);
  49. gf_sqr(y,tmp);
  50. }
  51. }
  52. static __inline__ void
  53. gf_sub (
  54. gf d,
  55. const gf a,
  56. const gf b
  57. ) {
  58. gf_sub_RAW ( d, a, b );
  59. gf_bias( d, 2 );
  60. gf_weak_reduce ( d );
  61. }
  62. static __inline__ void
  63. gf_add (
  64. gf d,
  65. const gf a,
  66. const gf b
  67. ) {
  68. gf_add_RAW ( d, a, b );
  69. gf_weak_reduce ( d );
  70. }
  71. #define gf_add_nr gf_add_RAW
  72. /** Subtract mod p. Bias by 2 and don't reduce */
  73. static inline void gf_sub_nr ( gf c, const gf a, const gf b ) {
  74. // FOR_LIMB_U(i, c->limb[i] = a->limb[i] - b->limb[i] + 2*P->limb[i] );
  75. gf_sub_RAW(c,a,b);
  76. gf_bias(c, 2);
  77. if (DECAF_WORD_BITS==32) gf_weak_reduce(c); // HACK
  78. }
  79. /** Subtract mod p. Bias by amt but don't reduce. */
  80. static inline void gf_subx_nr ( gf c, const gf a, const gf b, int amt ) {
  81. gf_sub_RAW(c,a,b);
  82. gf_bias(c, amt);
  83. if (DECAF_WORD_BITS==32) gf_weak_reduce(c); // HACK
  84. }
  85. #endif // __GF_H__