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.
 
 
 
 
 

117 lines
1.9 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. /*
  43. * If doMac, accum = accum + a*b mod p.
  44. * Otherwise, accum = a*b mod p.
  45. *
  46. * This function is not __restrict__; you may pass accum,
  47. * a, b, etc all from the same location.
  48. */
  49. void
  50. barrett_mul_or_mac(
  51. word_t *accum,
  52. int nwords_accum,
  53. const word_t *a,
  54. int nwords_a,
  55. const word_t *b,
  56. int nwords_b,
  57. const word_t *p_lo,
  58. int nwords_p,
  59. int nwords_lo,
  60. int p_shift,
  61. mask_t doMac
  62. );
  63. static inline void
  64. barrett_mul(
  65. word_t *out,
  66. int nwords_out,
  67. const word_t *a,
  68. int nwords_a,
  69. const word_t *b,
  70. int nwords_b,
  71. const word_t *p_lo,
  72. int nwords_p,
  73. int nwords_lo,
  74. int p_shift
  75. ) {
  76. barrett_mul_or_mac(out,nwords_out,a,nwords_a,b,nwords_b,p_lo,nwords_p,nwords_lo,p_shift,0);
  77. }
  78. static inline void
  79. barrett_mac(
  80. word_t *out,
  81. int nwords_out,
  82. const word_t *a,
  83. int nwords_a,
  84. const word_t *b,
  85. int nwords_b,
  86. const word_t *p_lo,
  87. int nwords_p,
  88. int nwords_lo,
  89. int p_shift
  90. ) {
  91. barrett_mul_or_mac(out,nwords_out,a,nwords_a,b,nwords_b,p_lo,nwords_p,nwords_lo,p_shift,-1);
  92. }
  93. #ifdef __cplusplus
  94. }; /* extern "C" */
  95. #endif
  96. #endif /* __BARRETT_FIELD_H__ */