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.
 
 
 
 
 

127 lines
2.0 KiB

  1. /* Copyright (c) 2014 Cryptography Research, Inc.
  2. * Released under the MIT License. See LICENSE.txt for license information.
  3. */
  4. #ifndef __BARRETT_FIELD_H__
  5. #define __BARRETT_FIELD_H__ 1
  6. #include "word.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. void
  11. barrett_reduce(
  12. word_t *a,
  13. int nwords_a,
  14. word_t a_carry,
  15. const word_t *p_lo,
  16. int nwords_p,
  17. int nwords_lo,
  18. int p_shift
  19. );
  20. /*
  21. * out = a+(c&mask), with carry returned.
  22. * #out must equal #a (HACK?)
  23. */
  24. word_t
  25. add_nr_ext_packed(
  26. word_t *out,
  27. const word_t *a,
  28. int nwords_a,
  29. const word_t *c,
  30. int nwords_c,
  31. word_t mask
  32. );
  33. word_t
  34. sub_nr_ext_packed(
  35. word_t *out,
  36. const word_t *a,
  37. int nwords_a,
  38. const word_t *c,
  39. int nwords_c,
  40. word_t mask
  41. );
  42. void
  43. barrett_negate (
  44. word_t *a,
  45. int nwords_a,
  46. const word_t *p_lo,
  47. int nwords_p,
  48. int nwords_lo,
  49. int p_shift
  50. );
  51. /*
  52. * If doMac, accum = accum + a*b mod p.
  53. * Otherwise, accum = a*b mod p.
  54. *
  55. * This function is not __restrict__; you may pass accum,
  56. * a, b, etc all from the same location.
  57. */
  58. void
  59. barrett_mul_or_mac(
  60. word_t *accum,
  61. int nwords_accum,
  62. const word_t *a,
  63. int nwords_a,
  64. const word_t *b,
  65. int nwords_b,
  66. const word_t *p_lo,
  67. int nwords_p,
  68. int nwords_lo,
  69. int p_shift,
  70. mask_t doMac
  71. );
  72. static inline void
  73. barrett_mul(
  74. word_t *out,
  75. int nwords_out,
  76. const word_t *a,
  77. int nwords_a,
  78. const word_t *b,
  79. int nwords_b,
  80. const word_t *p_lo,
  81. int nwords_p,
  82. int nwords_lo,
  83. int p_shift
  84. ) {
  85. barrett_mul_or_mac(out,nwords_out,a,nwords_a,b,nwords_b,p_lo,nwords_p,nwords_lo,p_shift,0);
  86. }
  87. static inline void
  88. barrett_mac(
  89. word_t *out,
  90. int nwords_out,
  91. const word_t *a,
  92. int nwords_a,
  93. const word_t *b,
  94. int nwords_b,
  95. const word_t *p_lo,
  96. int nwords_p,
  97. int nwords_lo,
  98. int p_shift
  99. ) {
  100. barrett_mul_or_mac(out,nwords_out,a,nwords_a,b,nwords_b,p_lo,nwords_p,nwords_lo,p_shift,-1);
  101. }
  102. #ifdef __cplusplus
  103. }; /* extern "C" */
  104. #endif
  105. #endif /* __BARRETT_FIELD_H__ */