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.
 
 
 
 
 

62 lines
1.4 KiB

  1. /* Copyright (c) 2014 Cryptography Research, Inc.
  2. * Released under the MIT License. See LICENSE.txt for license information.
  3. */
  4. #include "f_field.h"
  5. void gf_mul (gf_s *__restrict__ cs, const gf as, const gf bs) {
  6. const uint64_t *a = as->limb, *b = bs->limb, mask = ((1ull<<51)-1);
  7. uint64_t bh[4];
  8. int i,j;
  9. for (i=0; i<4; i++) bh[i] = b[i+1] * 19;
  10. uint64_t *c = cs->limb;
  11. __uint128_t accum = 0;
  12. for (i=0; i<5; i++) {
  13. for (j=0; j<=i; j++) {
  14. accum += widemul(b[i-j], a[j]);
  15. }
  16. for (; j<5; j++) {
  17. accum += widemul(bh[i-j+4], a[j]);
  18. }
  19. c[i] = accum & mask;
  20. accum >>= 51;
  21. }
  22. accum *= 19;
  23. accum += c[0];
  24. c[0] = accum & mask;
  25. accum >>= 51;
  26. assert(accum < mask);
  27. c[1] += accum;
  28. }
  29. void gf_mulw_unsigned (gf_s *__restrict__ cs, const gf as, uint32_t b) {
  30. const uint64_t *a = as->limb, mask = ((1ull<<51)-1);
  31. int i;
  32. uint64_t *c = cs->limb;
  33. __uint128_t accum = 0;
  34. for (i=0; i<5; i++) {
  35. accum += widemul(b, a[i]);
  36. c[i] = accum & mask;
  37. accum >>= 51;
  38. }
  39. accum *= 19;
  40. accum += c[0];
  41. c[0] = accum & mask;
  42. accum >>= 51;
  43. assert(accum < mask);
  44. c[1] += accum;
  45. }
  46. void gf_sqr (gf_s *__restrict__ cs, const gf as) {
  47. gf_mul(cs,as,as); /* Performs better with dedicated square */
  48. }