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.
 
 
 

262 line
7.6 KiB

  1. /*
  2. * Support functions for signatures (hash-to-point, norm).
  3. *
  4. * ==========================(LICENSE BEGIN)============================
  5. *
  6. * Copyright (c) 2017-2019 Falcon Project
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the
  10. * "Software"), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sublicense, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject to
  14. * the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. * ===========================(LICENSE END)=============================
  28. *
  29. * @author Thomas Pornin <thomas.pornin@nccgroup.com>
  30. */
  31. #include "inner.h"
  32. /* see inner.h */
  33. void
  34. PQCLEAN_FALCON512_CLEAN_hash_to_point(
  35. shake256_context *sc,
  36. uint16_t *x, unsigned logn, uint8_t *tmp) {
  37. /*
  38. * Each 16-bit sample is a value in 0..65535. The value is
  39. * kept if it falls in 0..61444 (because 61445 = 5*12289)
  40. * and rejected otherwise; thus, each sample has probability
  41. * about 0.93758 of being selected.
  42. *
  43. * We want to oversample enough to be sure that we will
  44. * have enough values with probability at least 1 - 2^(-256).
  45. * Depending on degree N, this leads to the following
  46. * required oversampling:
  47. *
  48. * logn n oversampling
  49. * 1 2 65
  50. * 2 4 67
  51. * 3 8 71
  52. * 4 16 77
  53. * 5 32 86
  54. * 6 64 100
  55. * 7 128 122
  56. * 8 256 154
  57. * 9 512 205
  58. * 10 1024 287
  59. *
  60. * If logn >= 7, then the provided temporary buffer is large
  61. * enough. Otherwise, we use a stack buffer of 63 entries
  62. * (i.e. 126 bytes) for the values that do not fit in tmp[].
  63. */
  64. static const uint16_t overtab[] = {
  65. 0, /* unused */
  66. 65,
  67. 67,
  68. 71,
  69. 77,
  70. 86,
  71. 100,
  72. 122,
  73. 154,
  74. 205,
  75. 287
  76. };
  77. unsigned n, n2, u, m, p, over;
  78. uint16_t *tt1, tt2[63];
  79. /*
  80. * We first generate m 16-bit value. Values 0..n-1 go to x[].
  81. * Values n..2*n-1 go to tt1[]. Values 2*n and later go to tt2[].
  82. * We also reduce modulo q the values; rejected values are set
  83. * to 0xFFFF.
  84. */
  85. n = 1U << logn;
  86. n2 = n << 1;
  87. over = overtab[logn];
  88. m = n + over;
  89. tt1 = (uint16_t *)tmp;
  90. for (u = 0; u < m; u ++) {
  91. uint8_t buf[2];
  92. uint32_t w, wr;
  93. shake256_extract(sc, buf, sizeof buf);
  94. w = ((uint32_t)buf[0] << 8) | (uint32_t)buf[1];
  95. wr = w - ((uint32_t)24578 & (((w - 24578) >> 31) - 1));
  96. wr = wr - ((uint32_t)24578 & (((wr - 24578) >> 31) - 1));
  97. wr = wr - ((uint32_t)12289 & (((wr - 12289) >> 31) - 1));
  98. wr |= ((w - 61445) >> 31) - 1;
  99. if (u < n) {
  100. x[u] = (uint16_t)wr;
  101. } else if (u < n2) {
  102. tt1[u - n] = (uint16_t)wr;
  103. } else {
  104. tt2[u - n2] = (uint16_t)wr;
  105. }
  106. }
  107. /*
  108. * Now we must "squeeze out" the invalid values. We do this in
  109. * a logarithmic sequence of passes; each pass computes where a
  110. * value should go, and moves it down by 'p' slots if necessary,
  111. * where 'p' uses an increasing powers-of-two scale. It can be
  112. * shown that in all cases where the loop decides that a value
  113. * has to be moved down by p slots, the destination slot is
  114. * "free" (i.e. contains an invalid value).
  115. */
  116. for (p = 1; p <= over; p <<= 1) {
  117. unsigned v;
  118. /*
  119. * In the loop below:
  120. *
  121. * - v contains the index of the final destination of
  122. * the value; it is recomputed dynamically based on
  123. * whether values are valid or not.
  124. *
  125. * - u is the index of the value we consider ("source");
  126. * its address is s.
  127. *
  128. * - The loop may swap the value with the one at index
  129. * u-p. The address of the swap destination is d.
  130. */
  131. v = 0;
  132. for (u = 0; u < m; u ++) {
  133. uint16_t *s, *d;
  134. unsigned j, sv, dv, mk;
  135. if (u < n) {
  136. s = &x[u];
  137. } else if (u < n2) {
  138. s = &tt1[u - n];
  139. } else {
  140. s = &tt2[u - n2];
  141. }
  142. sv = *s;
  143. /*
  144. * The value in sv should ultimately go to
  145. * address v, i.e. jump back by u-v slots.
  146. */
  147. j = u - v;
  148. /*
  149. * We increment v for the next iteration, but
  150. * only if the source value is valid. The mask
  151. * 'mk' is -1 if the value is valid, 0 otherwise,
  152. * so we _subtract_ mk.
  153. */
  154. mk = (sv >> 15) - 1U;
  155. v -= mk;
  156. /*
  157. * In this loop we consider jumps by p slots; if
  158. * u < p then there is nothing more to do.
  159. */
  160. if (u < p) {
  161. continue;
  162. }
  163. /*
  164. * Destination for the swap: value at address u-p.
  165. */
  166. if ((u - p) < n) {
  167. d = &x[u - p];
  168. } else if ((u - p) < n2) {
  169. d = &tt1[(u - p) - n];
  170. } else {
  171. d = &tt2[(u - p) - n2];
  172. }
  173. dv = *d;
  174. /*
  175. * The swap should be performed only if the source
  176. * is valid AND the jump j has its 'p' bit set.
  177. */
  178. mk &= -(((j & p) + 0x1FF) >> 9);
  179. *s = (uint16_t)(sv ^ (mk & (sv ^ dv)));
  180. *d = (uint16_t)(dv ^ (mk & (sv ^ dv)));
  181. }
  182. }
  183. }
  184. /* see inner.h */
  185. int
  186. PQCLEAN_FALCON512_CLEAN_is_short(
  187. const int16_t *s1, const int16_t *s2, unsigned logn) {
  188. /*
  189. * We use the l2-norm. Code below uses only 32-bit operations to
  190. * compute the square of the norm with saturation to 2^32-1 if
  191. * the value exceeds 2^31-1.
  192. */
  193. size_t n, u;
  194. uint32_t s, ng;
  195. n = (size_t)1 << logn;
  196. s = 0;
  197. ng = 0;
  198. for (u = 0; u < n; u ++) {
  199. int32_t z;
  200. z = s1[u];
  201. s += (uint32_t)(z * z);
  202. ng |= s;
  203. z = s2[u];
  204. s += (uint32_t)(z * z);
  205. ng |= s;
  206. }
  207. s |= -(ng >> 31);
  208. /*
  209. * Acceptance bound on the l2-norm is:
  210. * 1.2*1.55*sqrt(q)*sqrt(2*N)
  211. * Value 7085 is floor((1.2^2)*(1.55^2)*2*1024).
  212. */
  213. return s < (((uint32_t)7085 * (uint32_t)12289) >> (10 - logn));
  214. }
  215. /* see inner.h */
  216. int
  217. PQCLEAN_FALCON512_CLEAN_is_short_half(
  218. uint32_t sqn, const int16_t *s2, unsigned logn) {
  219. size_t n, u;
  220. uint32_t ng;
  221. n = (size_t)1 << logn;
  222. ng = -(sqn >> 31);
  223. for (u = 0; u < n; u ++) {
  224. int32_t z;
  225. z = s2[u];
  226. sqn += (uint32_t)(z * z);
  227. ng |= sqn;
  228. }
  229. sqn |= -(ng >> 31);
  230. /*
  231. * Acceptance bound on the l2-norm is:
  232. * 1.2*1.55*sqrt(q)*sqrt(2*N)
  233. * Value 7085 is floor((1.2^2)*(1.55^2)*2*1024).
  234. */
  235. return sqn < (((uint32_t)7085 * (uint32_t)12289) >> (10 - logn));
  236. }