Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

368 wiersze
13 KiB

  1. #include "polyvec.h"
  2. #include "params.h"
  3. #include "poly.h"
  4. #include <stdint.h>
  5. /**************************************************************/
  6. /************ Vectors of polynomials of length L **************/
  7. /**************************************************************/
  8. /*************************************************
  9. * Name: polyvecl_freeze
  10. *
  11. * Description: Reduce coefficients of polynomials in vector of length L
  12. * to standard representatives.
  13. *
  14. * Arguments: - polyvecl *v: pointer to input/output vector
  15. **************************************************/
  16. void PQCLEAN_DILITHIUMIII_CLEAN_polyvecl_freeze(polyvecl *v) {
  17. unsigned int i;
  18. for (i = 0; i < L; ++i) {
  19. PQCLEAN_DILITHIUMIII_CLEAN_poly_freeze(v->vec + i);
  20. }
  21. }
  22. /*************************************************
  23. * Name: polyvecl_add
  24. *
  25. * Description: Add vectors of polynomials of length L.
  26. * No modular reduction is performed.
  27. *
  28. * Arguments: - polyvecl *w: pointer to output vector
  29. * - const polyvecl *u: pointer to first summand
  30. * - const polyvecl *v: pointer to second summand
  31. **************************************************/
  32. void PQCLEAN_DILITHIUMIII_CLEAN_polyvecl_add(polyvecl *w, const polyvecl *u,
  33. const polyvecl *v) {
  34. unsigned int i;
  35. for (i = 0; i < L; ++i) {
  36. PQCLEAN_DILITHIUMIII_CLEAN_poly_add(w->vec + i, u->vec + i, v->vec + i);
  37. }
  38. }
  39. /*************************************************
  40. * Name: polyvecl_ntt
  41. *
  42. * Description: Forward NTT of all polynomials in vector of length L. Output
  43. * coefficients can be up to 16*Q larger than input coefficients.
  44. *
  45. * Arguments: - polyvecl *v: pointer to input/output vector
  46. **************************************************/
  47. void PQCLEAN_DILITHIUMIII_CLEAN_polyvecl_ntt(polyvecl *v) {
  48. unsigned int i;
  49. for (i = 0; i < L; ++i) {
  50. PQCLEAN_DILITHIUMIII_CLEAN_poly_ntt(v->vec + i);
  51. }
  52. }
  53. /*************************************************
  54. * Name: polyvecl_pointwise_acc_invmontgomery
  55. *
  56. * Description: Pointwise multiply vectors of polynomials of length L, multiply
  57. * resulting vector by 2^{-32} and add (accumulate) polynomials
  58. * in it. Input/output vectors are in NTT domain representation.
  59. * Input coefficients are assumed to be less than 22*Q. Output
  60. * coeffcient are less than 2*L*Q.
  61. *
  62. * Arguments: - poly *w: output polynomial
  63. * - const polyvecl *u: pointer to first input vector
  64. * - const polyvecl *v: pointer to second input vector
  65. **************************************************/
  66. void PQCLEAN_DILITHIUMIII_CLEAN_polyvecl_pointwise_acc_invmontgomery(
  67. poly *w, const polyvecl *u, const polyvecl *v) {
  68. unsigned int i;
  69. poly t;
  70. PQCLEAN_DILITHIUMIII_CLEAN_poly_pointwise_invmontgomery(w, u->vec + 0,
  71. v->vec + 0);
  72. for (i = 1; i < L; ++i) {
  73. PQCLEAN_DILITHIUMIII_CLEAN_poly_pointwise_invmontgomery(&t, u->vec + i,
  74. v->vec + i);
  75. PQCLEAN_DILITHIUMIII_CLEAN_poly_add(w, w, &t);
  76. }
  77. }
  78. /*************************************************
  79. * Name: polyvecl_chknorm
  80. *
  81. * Description: Check infinity norm of polynomials in vector of length L.
  82. * Assumes input coefficients to be standard representatives.
  83. *
  84. * Arguments: - const polyvecl *v: pointer to vector
  85. * - uint32_t B: norm bound
  86. *
  87. * Returns 0 if norm of all polynomials is strictly smaller than B and 1
  88. * otherwise.
  89. **************************************************/
  90. int PQCLEAN_DILITHIUMIII_CLEAN_polyvecl_chknorm(const polyvecl *v, uint32_t bound) {
  91. unsigned int i;
  92. int ret = 0;
  93. for (i = 0; i < L; ++i) {
  94. ret |= PQCLEAN_DILITHIUMIII_CLEAN_poly_chknorm(v->vec + i, bound);
  95. }
  96. return ret;
  97. }
  98. /**************************************************************/
  99. /************ Vectors of polynomials of length K **************/
  100. /**************************************************************/
  101. /*************************************************
  102. * Name: polyveck_reduce
  103. *
  104. * Description: Reduce coefficients of polynomials in vector of length K
  105. * to representatives in [0,2*Q[.
  106. *
  107. * Arguments: - polyveck *v: pointer to input/output vector
  108. **************************************************/
  109. void PQCLEAN_DILITHIUMIII_CLEAN_polyveck_reduce(polyveck *v) {
  110. unsigned int i;
  111. for (i = 0; i < K; ++i) {
  112. PQCLEAN_DILITHIUMIII_CLEAN_poly_reduce(v->vec + i);
  113. }
  114. }
  115. /*************************************************
  116. * Name: polyveck_csubq
  117. *
  118. * Description: For all coefficients of polynomials in vector of length K
  119. * subtract Q if coefficient is bigger than Q.
  120. *
  121. * Arguments: - polyveck *v: pointer to input/output vector
  122. **************************************************/
  123. void PQCLEAN_DILITHIUMIII_CLEAN_polyveck_csubq(polyveck *v) {
  124. unsigned int i;
  125. for (i = 0; i < K; ++i) {
  126. PQCLEAN_DILITHIUMIII_CLEAN_poly_csubq(v->vec + i);
  127. }
  128. }
  129. /*************************************************
  130. * Name: polyveck_freeze
  131. *
  132. * Description: Reduce coefficients of polynomials in vector of length K
  133. * to standard representatives.
  134. *
  135. * Arguments: - polyveck *v: pointer to input/output vector
  136. **************************************************/
  137. void PQCLEAN_DILITHIUMIII_CLEAN_polyveck_freeze(polyveck *v) {
  138. unsigned int i;
  139. for (i = 0; i < K; ++i) {
  140. PQCLEAN_DILITHIUMIII_CLEAN_poly_freeze(v->vec + i);
  141. }
  142. }
  143. /*************************************************
  144. * Name: polyveck_add
  145. *
  146. * Description: Add vectors of polynomials of length K.
  147. * No modular reduction is performed.
  148. *
  149. * Arguments: - polyveck *w: pointer to output vector
  150. * - const polyveck *u: pointer to first summand
  151. * - const polyveck *v: pointer to second summand
  152. **************************************************/
  153. void PQCLEAN_DILITHIUMIII_CLEAN_polyveck_add(polyveck *w, const polyveck *u,
  154. const polyveck *v) {
  155. unsigned int i;
  156. for (i = 0; i < K; ++i) {
  157. PQCLEAN_DILITHIUMIII_CLEAN_poly_add(w->vec + i, u->vec + i, v->vec + i);
  158. }
  159. }
  160. /*************************************************
  161. * Name: polyveck_sub
  162. *
  163. * Description: Subtract vectors of polynomials of length K.
  164. * Assumes coefficients of polynomials in second input vector
  165. * to be less than 2*Q. No modular reduction is performed.
  166. *
  167. * Arguments: - polyveck *w: pointer to output vector
  168. * - const polyveck *u: pointer to first input vector
  169. * - const polyveck *v: pointer to second input vector to be
  170. * subtracted from first input vector
  171. **************************************************/
  172. void PQCLEAN_DILITHIUMIII_CLEAN_polyveck_sub(polyveck *w, const polyveck *u,
  173. const polyveck *v) {
  174. unsigned int i;
  175. for (i = 0; i < K; ++i) {
  176. PQCLEAN_DILITHIUMIII_CLEAN_poly_sub(w->vec + i, u->vec + i, v->vec + i);
  177. }
  178. }
  179. /*************************************************
  180. * Name: polyveck_shiftl
  181. *
  182. * Description: Multiply vector of polynomials of Length K by 2^k without
  183. *modular reduction. Assumes input coefficients to be less than 2^{32-k}.
  184. *
  185. * Arguments: - polyveck *v: pointer to input/output vector
  186. * - unsigned int k: exponent
  187. **************************************************/
  188. void PQCLEAN_DILITHIUMIII_CLEAN_polyveck_shiftl(polyveck *v, unsigned int k) {
  189. unsigned int i;
  190. for (i = 0; i < K; ++i) {
  191. PQCLEAN_DILITHIUMIII_CLEAN_poly_shiftl(v->vec + i, k);
  192. }
  193. }
  194. /*************************************************
  195. * Name: polyveck_ntt
  196. *
  197. * Description: Forward NTT of all polynomials in vector of length K. Output
  198. * coefficients can be up to 16*Q larger than input coefficients.
  199. *
  200. * Arguments: - polyveck *v: pointer to input/output vector
  201. **************************************************/
  202. void PQCLEAN_DILITHIUMIII_CLEAN_polyveck_ntt(polyveck *v) {
  203. unsigned int i;
  204. for (i = 0; i < K; ++i) {
  205. PQCLEAN_DILITHIUMIII_CLEAN_poly_ntt(v->vec + i);
  206. }
  207. }
  208. /*************************************************
  209. * Name: polyveck_invntt_montgomery
  210. *
  211. * Description: Inverse NTT and multiplication by 2^{32} of polynomials
  212. * in vector of length K. Input coefficients need to be less
  213. * than 2*Q.
  214. *
  215. * Arguments: - polyveck *v: pointer to input/output vector
  216. **************************************************/
  217. void PQCLEAN_DILITHIUMIII_CLEAN_polyveck_invntt_montgomery(polyveck *v) {
  218. unsigned int i;
  219. for (i = 0; i < K; ++i) {
  220. PQCLEAN_DILITHIUMIII_CLEAN_poly_invntt_montgomery(v->vec + i);
  221. }
  222. }
  223. /*************************************************
  224. * Name: polyveck_chknorm
  225. *
  226. * Description: Check infinity norm of polynomials in vector of length K.
  227. * Assumes input coefficients to be standard representatives.
  228. *
  229. * Arguments: - const polyveck *v: pointer to vector
  230. * - uint32_t B: norm bound
  231. *
  232. * Returns 0 if norm of all polynomials are strictly smaller than B and 1
  233. * otherwise.
  234. **************************************************/
  235. int PQCLEAN_DILITHIUMIII_CLEAN_polyveck_chknorm(const polyveck *v, uint32_t bound) {
  236. unsigned int i;
  237. int ret = 0;
  238. for (i = 0; i < K; ++i) {
  239. ret |= PQCLEAN_DILITHIUMIII_CLEAN_poly_chknorm(v->vec + i, bound);
  240. }
  241. return ret;
  242. }
  243. /*************************************************
  244. * Name: polyveck_power2round
  245. *
  246. * Description: For all coefficients a of polynomials in vector of length K,
  247. * compute a0, a1 such that a mod Q = a1*2^D + a0
  248. * with -2^{D-1} < a0 <= 2^{D-1}. Assumes coefficients to be
  249. * standard representatives.
  250. *
  251. * Arguments: - polyveck *v1: pointer to output vector of polynomials with
  252. * coefficients a1
  253. * - polyveck *v0: pointer to output vector of polynomials with
  254. * coefficients Q + a0
  255. * - const polyveck *v: pointer to input vector
  256. **************************************************/
  257. void PQCLEAN_DILITHIUMIII_CLEAN_polyveck_power2round(polyveck *v1, polyveck *v0,
  258. const polyveck *v) {
  259. unsigned int i;
  260. for (i = 0; i < K; ++i) {
  261. PQCLEAN_DILITHIUMIII_CLEAN_poly_power2round(v1->vec + i, v0->vec + i,
  262. v->vec + i);
  263. }
  264. }
  265. /*************************************************
  266. * Name: polyveck_decompose
  267. *
  268. * Description: For all coefficients a of polynomials in vector of length K,
  269. * compute high and low bits a0, a1 such a mod Q = a1*ALPHA + a0
  270. * with -ALPHA/2 < a0 <= ALPHA/2 except a1 = (Q-1)/ALPHA where we
  271. * set a1 = 0 and -ALPHA/2 <= a0 = a mod Q - Q < 0.
  272. * Assumes coefficients to be standard representatives.
  273. *
  274. * Arguments: - polyveck *v1: pointer to output vector of polynomials with
  275. * coefficients a1
  276. * - polyveck *v0: pointer to output vector of polynomials with
  277. * coefficients Q + a0
  278. * - const polyveck *v: pointer to input vector
  279. **************************************************/
  280. void PQCLEAN_DILITHIUMIII_CLEAN_polyveck_decompose(polyveck *v1, polyveck *v0,
  281. const polyveck *v) {
  282. unsigned int i;
  283. for (i = 0; i < K; ++i) {
  284. PQCLEAN_DILITHIUMIII_CLEAN_poly_decompose(v1->vec + i, v0->vec + i,
  285. v->vec + i);
  286. }
  287. }
  288. /*************************************************
  289. * Name: polyveck_make_hint
  290. *
  291. * Description: Compute hint vector.
  292. *
  293. * Arguments: - polyveck *h: pointer to output vector
  294. * - const polyveck *u: pointer to first input vector
  295. * - const polyveck *u: pointer to second input vector
  296. *
  297. * Returns number of 1 bits.
  298. **************************************************/
  299. unsigned int PQCLEAN_DILITHIUMIII_CLEAN_polyveck_make_hint(polyveck *h,
  300. const polyveck *u,
  301. const polyveck *v) {
  302. unsigned int i, s = 0;
  303. for (i = 0; i < K; ++i) {
  304. s += PQCLEAN_DILITHIUMIII_CLEAN_poly_make_hint(h->vec + i, u->vec + i,
  305. v->vec + i);
  306. }
  307. return s;
  308. }
  309. /*************************************************
  310. * Name: polyveck_use_hint
  311. *
  312. * Description: Use hint vector to correct the high bits of input vector.
  313. *
  314. * Arguments: - polyveck *w: pointer to output vector of polynomials with
  315. * corrected high bits
  316. * - const polyveck *u: pointer to input vector
  317. * - const polyveck *h: pointer to input hint vector
  318. **************************************************/
  319. void PQCLEAN_DILITHIUMIII_CLEAN_polyveck_use_hint(polyveck *w, const polyveck *u,
  320. const polyveck *h) {
  321. unsigned int i;
  322. for (i = 0; i < K; ++i) {
  323. PQCLEAN_DILITHIUMIII_CLEAN_poly_use_hint(w->vec + i, u->vec + i, h->vec + i);
  324. }
  325. }