2
0

f_generic.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2015-2016 Cryptography Research, Inc.
  4. *
  5. * Licensed under the Apache License 2.0 (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. static const gf MODULUS = {
  14. FIELD_LITERAL(0xffffffffffffffULL, 0xffffffffffffffULL, 0xffffffffffffffULL,
  15. 0xffffffffffffffULL, 0xfffffffffffffeULL, 0xffffffffffffffULL,
  16. 0xffffffffffffffULL, 0xffffffffffffffULL)
  17. };
  18. /* Serialize to wire format. */
  19. void gf_serialize(uint8_t *serial, const gf x, int with_hibit)
  20. {
  21. unsigned int j = 0, fill = 0;
  22. dword_t buffer = 0;
  23. int i;
  24. gf red;
  25. gf_copy(red, x);
  26. gf_strong_reduce(red);
  27. if (!with_hibit)
  28. assert(gf_hibit(red) == 0);
  29. for (i = 0; i < (with_hibit ? X_SER_BYTES : SER_BYTES); i++) {
  30. if (fill < 8 && j < NLIMBS) {
  31. buffer |= ((dword_t) red->limb[LIMBPERM(j)]) << fill;
  32. fill += LIMB_PLACE_VALUE(LIMBPERM(j));
  33. j++;
  34. }
  35. serial[i] = (uint8_t)buffer;
  36. fill -= 8;
  37. buffer >>= 8;
  38. }
  39. }
  40. /* Return high bit of x = low bit of 2x mod p */
  41. mask_t gf_hibit(const gf x)
  42. {
  43. gf y;
  44. gf_add(y, x, x);
  45. gf_strong_reduce(y);
  46. return 0 - (y->limb[0] & 1);
  47. }
  48. /* Return high bit of x = low bit of 2x mod p */
  49. mask_t gf_lobit(const gf x)
  50. {
  51. gf y;
  52. gf_copy(y, x);
  53. gf_strong_reduce(y);
  54. return 0 - (y->limb[0] & 1);
  55. }
  56. /* Deserialize from wire format; return -1 on success and 0 on failure. */
  57. mask_t gf_deserialize(gf x, const uint8_t serial[SER_BYTES], int with_hibit,
  58. uint8_t hi_nmask)
  59. {
  60. unsigned int j = 0, fill = 0;
  61. dword_t buffer = 0;
  62. dsword_t scarry = 0;
  63. const unsigned nbytes = with_hibit ? X_SER_BYTES : SER_BYTES;
  64. unsigned int i;
  65. mask_t succ;
  66. for (i = 0; i < NLIMBS; i++) {
  67. while (fill < LIMB_PLACE_VALUE(LIMBPERM(i)) && j < nbytes) {
  68. uint8_t sj;
  69. sj = serial[j];
  70. if (j == nbytes - 1)
  71. sj &= ~hi_nmask;
  72. buffer |= ((dword_t) sj) << fill;
  73. fill += 8;
  74. j++;
  75. }
  76. x->limb[LIMBPERM(i)] = (word_t)
  77. ((i < NLIMBS - 1) ? buffer & LIMB_MASK(LIMBPERM(i)) : buffer);
  78. fill -= LIMB_PLACE_VALUE(LIMBPERM(i));
  79. buffer >>= LIMB_PLACE_VALUE(LIMBPERM(i));
  80. scarry =
  81. (scarry + x->limb[LIMBPERM(i)] -
  82. MODULUS->limb[LIMBPERM(i)]) >> (8 * sizeof(word_t));
  83. }
  84. succ = with_hibit ? 0 - (mask_t) 1 : ~gf_hibit(x);
  85. return succ & word_is_zero((word_t)buffer) & ~word_is_zero((word_t)scarry);
  86. }
  87. /* Reduce to canonical form. */
  88. void gf_strong_reduce(gf a)
  89. {
  90. dsword_t scarry;
  91. word_t scarry_0;
  92. dword_t carry = 0;
  93. unsigned int i;
  94. /* first, clear high */
  95. gf_weak_reduce(a); /* Determined to have negligible perf impact. */
  96. /* now the total is less than 2p */
  97. /* compute total_value - p. No need to reduce mod p. */
  98. scarry = 0;
  99. for (i = 0; i < NLIMBS; i++) {
  100. scarry = scarry + a->limb[LIMBPERM(i)] - MODULUS->limb[LIMBPERM(i)];
  101. a->limb[LIMBPERM(i)] = scarry & LIMB_MASK(LIMBPERM(i));
  102. scarry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
  103. }
  104. /*
  105. * uncommon case: it was >= p, so now scarry = 0 and this = x common case:
  106. * it was < p, so now scarry = -1 and this = x - p + 2^255 so let's add
  107. * back in p. will carry back off the top for 2^255.
  108. */
  109. assert(scarry == 0 || scarry == -1);
  110. scarry_0 = (word_t)scarry;
  111. /* add it back */
  112. for (i = 0; i < NLIMBS; i++) {
  113. carry =
  114. carry + a->limb[LIMBPERM(i)] +
  115. (scarry_0 & MODULUS->limb[LIMBPERM(i)]);
  116. a->limb[LIMBPERM(i)] = carry & LIMB_MASK(LIMBPERM(i));
  117. carry >>= LIMB_PLACE_VALUE(LIMBPERM(i));
  118. }
  119. assert(carry < 2 && ((word_t)carry + scarry_0) == 0);
  120. }
  121. /* Subtract two gf elements d=a-b */
  122. void gf_sub(gf d, const gf a, const gf b)
  123. {
  124. gf_sub_RAW(d, a, b);
  125. gf_bias(d, 2);
  126. gf_weak_reduce(d);
  127. }
  128. /* Add two field elements d = a+b */
  129. void gf_add(gf d, const gf a, const gf b)
  130. {
  131. gf_add_RAW(d, a, b);
  132. gf_weak_reduce(d);
  133. }
  134. /* Compare a==b */
  135. mask_t gf_eq(const gf a, const gf b)
  136. {
  137. gf c;
  138. mask_t ret = 0;
  139. unsigned int i;
  140. gf_sub(c, a, b);
  141. gf_strong_reduce(c);
  142. for (i = 0; i < NLIMBS; i++)
  143. ret |= c->limb[LIMBPERM(i)];
  144. return word_is_zero(ret);
  145. }
  146. mask_t gf_isr(gf a, const gf x)
  147. {
  148. gf L0, L1, L2;
  149. gf_sqr(L1, x);
  150. gf_mul(L2, x, L1);
  151. gf_sqr(L1, L2);
  152. gf_mul(L2, x, L1);
  153. gf_sqrn(L1, L2, 3);
  154. gf_mul(L0, L2, L1);
  155. gf_sqrn(L1, L0, 3);
  156. gf_mul(L0, L2, L1);
  157. gf_sqrn(L2, L0, 9);
  158. gf_mul(L1, L0, L2);
  159. gf_sqr(L0, L1);
  160. gf_mul(L2, x, L0);
  161. gf_sqrn(L0, L2, 18);
  162. gf_mul(L2, L1, L0);
  163. gf_sqrn(L0, L2, 37);
  164. gf_mul(L1, L2, L0);
  165. gf_sqrn(L0, L1, 37);
  166. gf_mul(L1, L2, L0);
  167. gf_sqrn(L0, L1, 111);
  168. gf_mul(L2, L1, L0);
  169. gf_sqr(L0, L2);
  170. gf_mul(L1, x, L0);
  171. gf_sqrn(L0, L1, 223);
  172. gf_mul(L1, L2, L0);
  173. gf_sqr(L2, L1);
  174. gf_mul(L0, L2, x);
  175. gf_copy(a, L1);
  176. return gf_eq(L0, ONE);
  177. }