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.
 
 
 
 
 

65 lines
1.7 KiB

  1. /* Copyright (c) 2014-2016 Cryptography Research, Inc.
  2. * Released under the MIT License. See LICENSE.txt for license information.
  3. */
  4. #define LIMB_PLACE_VALUE(i) 60
  5. void gf_add_RAW (gf out, const gf a, const gf b) {
  6. for (unsigned int i=0; i<sizeof(*out)/sizeof(uint64xn_t); i++) {
  7. ((uint64xn_t*)out)[i] = ((const uint64xn_t*)a)[i] + ((const uint64xn_t*)b)[i];
  8. }
  9. /*
  10. unsigned int i;
  11. for (i=0; i<sizeof(*out)/sizeof(out->limb[0]); i++) {
  12. out->limb[i] = a->limb[i] + b->limb[i];
  13. }
  14. */
  15. }
  16. void gf_sub_RAW (gf out, const gf a, const gf b) {
  17. for (unsigned int i=0; i<sizeof(*out)/sizeof(uint64xn_t); i++) {
  18. ((uint64xn_t*)out)[i] = ((const uint64xn_t*)a)[i] - ((const uint64xn_t*)b)[i];
  19. }
  20. /*
  21. unsigned int i;
  22. for (i=0; i<sizeof(*out)/sizeof(out->limb[0]); i++) {
  23. out->limb[i] = a->limb[i] - b->limb[i];
  24. }
  25. */
  26. }
  27. void gf_bias (
  28. gf a, int amt
  29. ) {
  30. uint64_t co1 = ((1ull<<60)-1)*amt, co2 = co1-amt;
  31. #if __AVX2__
  32. uint64x4_t lo = {co1,co1,co1,co1}, hi = {co2,co1,co1,co1};
  33. uint64x4_t *aa = (uint64x4_t*) a;
  34. aa[0] += lo;
  35. aa[1] += hi;
  36. #elif __SSE2__
  37. uint64x2_t lo = {co1,co1}, hi = {co2,co1};
  38. uint64x2_t *aa = (uint64x2_t*) a;
  39. aa[0] += lo;
  40. aa[1] += lo;
  41. aa[2] += hi;
  42. aa[3] += lo;
  43. #else
  44. for (unsigned int i=0; i<sizeof(*a)/sizeof(uint64_t); i++) {
  45. a->limb[i] += (i==4) ? co2 : co1;
  46. }
  47. #endif
  48. }
  49. void gf_weak_reduce (gf a) {
  50. /* PERF: use pshufb/palignr if anyone cares about speed of this */
  51. uint64_t mask = (1ull<<60) - 1;
  52. uint64_t tmp = a->limb[7] >> 60;
  53. a->limb[4] += tmp;
  54. for (unsigned int i=7; i>0; i--) {
  55. a->limb[i] = (a->limb[i] & mask) + (a->limb[i-1]>>60);
  56. }
  57. a->limb[0] = (a->limb[0] & mask) + tmp;
  58. }