Python-ELMO is a Python library which offers an encapsulation of the binary tool ELMO, in order to manipulate it easily in Python and SageMath script.
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.

reduce.c 1.2KB

  1. #include <stdint.h>
  2. #include "params.h"
  3. #include "reduce.h"
  4. /*************************************************
  5. * Name: montgomery_reduce
  6. *
  7. * Description: Montgomery reduction; given a 32-bit integer a, computes
  8. * 16-bit integer congruent to a * R^-1 mod q,
  9. * where R=2^16
  10. *
  11. * Arguments: - int32_t a: input integer to be reduced; has to be in {-q2^15,...,q2^15-1}
  12. *
  13. * Returns: integer in {-q+1,...,q-1} congruent to a * R^-1 modulo q.
  14. **************************************************/
  15. int16_t montgomery_reduce(int32_t a)
  16. {
  17. int32_t t;
  18. int16_t u;
  19. u = a * QINV;
  20. t = (int32_t)u * KYBER_Q;
  21. t = a - t;
  22. t >>= 16;
  23. return t;
  24. }
  25. /*************************************************
  26. * Name: barrett_reduce
  27. *
  28. * Description: Barrett reduction; given a 16-bit integer a, computes
  29. * 16-bit integer congruent to a mod q in {0,...,q}
  30. *
  31. * Arguments: - int16_t a: input integer to be reduced
  32. *
  33. * Returns: integer in {0,...,q} congruent to a modulo q.
  34. **************************************************/
  35. int16_t barrett_reduce(int16_t a) {
  36. int32_t t;
  37. const int32_t v = (1U << 26)/KYBER_Q + 1;
  38. t = v*a;
  39. t >>= 26;
  40. t *= KYBER_Q;
  41. return a - t;
  42. }