f_impl.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2014 Cryptography Research, Inc.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. *
  10. * Originally written by Mike Hamburg
  11. */
  12. #include "field.h"
  13. void gf_mul(gf_s * RESTRICT cs, const gf as, const gf bs)
  14. {
  15. const uint32_t *a = as->limb, *b = bs->limb;
  16. uint32_t *c = cs->limb;
  17. uint64_t accum0 = 0, accum1 = 0, accum2 = 0;
  18. uint32_t mask = (1 << 28) - 1;
  19. uint32_t aa[8], bb[8];
  20. int i, j;
  21. for (i = 0; i < 8; i++) {
  22. aa[i] = a[i] + a[i + 8];
  23. bb[i] = b[i] + b[i + 8];
  24. }
  25. for (j = 0; j < 8; j++) {
  26. accum2 = 0;
  27. for (i = 0; i < j + 1; i++) {
  28. accum2 += widemul(a[j - i], b[i]);
  29. accum1 += widemul(aa[j - i], bb[i]);
  30. accum0 += widemul(a[8 + j - i], b[8 + i]);
  31. }
  32. accum1 -= accum2;
  33. accum0 += accum2;
  34. accum2 = 0;
  35. for (i = j + 1; i < 8; i++) {
  36. accum0 -= widemul(a[8 + j - i], b[i]);
  37. accum2 += widemul(aa[8 + j - i], bb[i]);
  38. accum1 += widemul(a[16 + j - i], b[8 + i]);
  39. }
  40. accum1 += accum2;
  41. accum0 += accum2;
  42. c[j] = ((uint32_t)(accum0)) & mask;
  43. c[j + 8] = ((uint32_t)(accum1)) & mask;
  44. accum0 >>= 28;
  45. accum1 >>= 28;
  46. }
  47. accum0 += accum1;
  48. accum0 += c[8];
  49. accum1 += c[0];
  50. c[8] = ((uint32_t)(accum0)) & mask;
  51. c[0] = ((uint32_t)(accum1)) & mask;
  52. accum0 >>= 28;
  53. accum1 >>= 28;
  54. c[9] += ((uint32_t)(accum0));
  55. c[1] += ((uint32_t)(accum1));
  56. }
  57. void gf_mulw_unsigned(gf_s * RESTRICT cs, const gf as, uint32_t b)
  58. {
  59. const uint32_t *a = as->limb;
  60. uint32_t *c = cs->limb;
  61. uint64_t accum0 = 0, accum8 = 0;
  62. uint32_t mask = (1 << 28) - 1;
  63. int i;
  64. assert(b <= mask);
  65. for (i = 0; i < 8; i++) {
  66. accum0 += widemul(b, a[i]);
  67. accum8 += widemul(b, a[i + 8]);
  68. c[i] = accum0 & mask;
  69. accum0 >>= 28;
  70. c[i + 8] = accum8 & mask;
  71. accum8 >>= 28;
  72. }
  73. accum0 += accum8 + c[8];
  74. c[8] = ((uint32_t)accum0) & mask;
  75. c[9] += (uint32_t)(accum0 >> 28);
  76. accum8 += c[0];
  77. c[0] = ((uint32_t)accum8) & mask;
  78. c[1] += (uint32_t)(accum8 >> 28);
  79. }
  80. void gf_sqr(gf_s * RESTRICT cs, const gf as)
  81. {
  82. gf_mul(cs, as, as); /* Performs better with a dedicated square */
  83. }