您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

187 行
7.2 KiB

  1. #include "polyvec.h"
  2. #include "poly.h"
  3. #include <stdint.h>
  4. /*************************************************
  5. * Name: PQCLEAN_KYBER512_CLEAN_polyvec_compress
  6. *
  7. * Description: Compress and serialize vector of polynomials
  8. *
  9. * Arguments: - unsigned char *r: pointer to output byte array (needs space for KYBER_POLYVECCOMPRESSEDBYTES)
  10. * - const polyvec *a: pointer to input vector of polynomials
  11. **************************************************/
  12. void PQCLEAN_KYBER512_CLEAN_polyvec_compress(unsigned char *r, polyvec *a) {
  13. int i, j, k;
  14. PQCLEAN_KYBER512_CLEAN_polyvec_csubq(a);
  15. uint16_t t[4];
  16. for (i = 0; i < KYBER_K; i++) {
  17. for (j = 0; j < KYBER_N / 4; j++) {
  18. for (k = 0; k < 4; k++) {
  19. t[k] = ((((uint32_t)a->vec[i].coeffs[4 * j + k] << 10) + KYBER_Q / 2) / KYBER_Q) & 0x3ff;
  20. }
  21. r[5 * j + 0] = t[0] & 0xff;
  22. r[5 * j + 1] = (t[0] >> 8) | ((t[1] & 0x3f) << 2);
  23. r[5 * j + 2] = ((t[1] >> 6) | ((t[2] & 0x0f) << 4)) & 0xff;
  24. r[5 * j + 3] = ((t[2] >> 4) | ((t[3] & 0x03) << 6)) & 0xff;
  25. r[5 * j + 4] = (t[3] >> 2) & 0xff;
  26. }
  27. r += 320;
  28. }
  29. }
  30. /*************************************************
  31. * Name: PQCLEAN_KYBER512_CLEAN_polyvec_decompress
  32. *
  33. * Description: De-serialize and decompress vector of polynomials;
  34. * approximate inverse of PQCLEAN_KYBER512_CLEAN_polyvec_compress
  35. *
  36. * Arguments: - polyvec *r: pointer to output vector of polynomials
  37. * - unsigned char *a: pointer to input byte array (of length KYBER_POLYVECCOMPRESSEDBYTES)
  38. **************************************************/
  39. void PQCLEAN_KYBER512_CLEAN_polyvec_decompress(polyvec *r, const unsigned char *a) {
  40. int i, j;
  41. for (i = 0; i < KYBER_K; i++) {
  42. for (j = 0; j < KYBER_N / 4; j++) {
  43. r->vec[i].coeffs[4 * j + 0] = (((a[5 * j + 0] | (((uint32_t)a[5 * j + 1] & 0x03) << 8)) * KYBER_Q) + 512) >> 10;
  44. r->vec[i].coeffs[4 * j + 1] = ((((a[5 * j + 1] >> 2) | (((uint32_t)a[5 * j + 2] & 0x0f) << 6)) * KYBER_Q) + 512) >> 10;
  45. r->vec[i].coeffs[4 * j + 2] = ((((a[5 * j + 2] >> 4) | (((uint32_t)a[5 * j + 3] & 0x3f) << 4)) * KYBER_Q) + 512) >> 10;
  46. r->vec[i].coeffs[4 * j + 3] = ((((a[5 * j + 3] >> 6) | (((uint32_t)a[5 * j + 4] & 0xff) << 2)) * KYBER_Q) + 512) >> 10;
  47. }
  48. a += 320;
  49. }
  50. }
  51. /*************************************************
  52. * Name: PQCLEAN_KYBER512_CLEAN_polyvec_tobytes
  53. *
  54. * Description: Serialize vector of polynomials
  55. *
  56. * Arguments: - unsigned char *r: pointer to output byte array (needs space for KYBER_POLYVECBYTES)
  57. * - const polyvec *a: pointer to input vector of polynomials
  58. **************************************************/
  59. void PQCLEAN_KYBER512_CLEAN_polyvec_tobytes(unsigned char *r, polyvec *a) {
  60. int i;
  61. for (i = 0; i < KYBER_K; i++) {
  62. PQCLEAN_KYBER512_CLEAN_poly_tobytes(r + i * KYBER_POLYBYTES, &a->vec[i]);
  63. }
  64. }
  65. /*************************************************
  66. * Name: PQCLEAN_KYBER512_CLEAN_polyvec_frombytes
  67. *
  68. * Description: De-serialize vector of polynomials;
  69. * inverse of PQCLEAN_KYBER512_CLEAN_polyvec_tobytes
  70. *
  71. * Arguments: - unsigned char *r: pointer to output byte array
  72. * - const polyvec *a: pointer to input vector of polynomials (of length KYBER_POLYVECBYTES)
  73. **************************************************/
  74. void PQCLEAN_KYBER512_CLEAN_polyvec_frombytes(polyvec *r, const unsigned char *a) {
  75. int i;
  76. for (i = 0; i < KYBER_K; i++) {
  77. PQCLEAN_KYBER512_CLEAN_poly_frombytes(&r->vec[i], a + i * KYBER_POLYBYTES);
  78. }
  79. }
  80. /*************************************************
  81. * Name: PQCLEAN_KYBER512_CLEAN_polyvec_ntt
  82. *
  83. * Description: Apply forward NTT to all elements of a vector of polynomials
  84. *
  85. * Arguments: - polyvec *r: pointer to in/output vector of polynomials
  86. **************************************************/
  87. void PQCLEAN_KYBER512_CLEAN_polyvec_ntt(polyvec *r) {
  88. int i;
  89. for (i = 0; i < KYBER_K; i++) {
  90. PQCLEAN_KYBER512_CLEAN_poly_ntt(&r->vec[i]);
  91. }
  92. }
  93. /*************************************************
  94. * Name: PQCLEAN_KYBER512_CLEAN_polyvec_invntt
  95. *
  96. * Description: Apply inverse NTT to all elements of a vector of polynomials
  97. *
  98. * Arguments: - polyvec *r: pointer to in/output vector of polynomials
  99. **************************************************/
  100. void PQCLEAN_KYBER512_CLEAN_polyvec_invntt(polyvec *r) {
  101. int i;
  102. for (i = 0; i < KYBER_K; i++) {
  103. PQCLEAN_KYBER512_CLEAN_poly_invntt(&r->vec[i]);
  104. }
  105. }
  106. /*************************************************
  107. * Name: PQCLEAN_KYBER512_CLEAN_polyvec_pointwise_acc
  108. *
  109. * Description: Pointwise multiply elements of a and b and accumulate into r
  110. *
  111. * Arguments: - poly *r: pointer to output polynomial
  112. * - const polyvec *a: pointer to first input vector of polynomials
  113. * - const polyvec *b: pointer to second input vector of polynomials
  114. **************************************************/
  115. void PQCLEAN_KYBER512_CLEAN_polyvec_pointwise_acc(poly *r, const polyvec *a, const polyvec *b) {
  116. int i;
  117. poly t;
  118. PQCLEAN_KYBER512_CLEAN_poly_basemul(r, &a->vec[0], &b->vec[0]);
  119. for (i = 1; i < KYBER_K; i++) {
  120. PQCLEAN_KYBER512_CLEAN_poly_basemul(&t, &a->vec[i], &b->vec[i]);
  121. PQCLEAN_KYBER512_CLEAN_poly_add(r, r, &t);
  122. }
  123. PQCLEAN_KYBER512_CLEAN_poly_reduce(r);
  124. }
  125. /*************************************************
  126. * Name: PQCLEAN_KYBER512_CLEAN_polyvec_reduce
  127. *
  128. * Description: Applies Barrett reduction to each coefficient
  129. * of each element of a vector of polynomials
  130. * for details of the Barrett reduction see comments in reduce.c
  131. *
  132. * Arguments: - poly *r: pointer to input/output polynomial
  133. **************************************************/
  134. void PQCLEAN_KYBER512_CLEAN_polyvec_reduce(polyvec *r) {
  135. int i;
  136. for (i = 0; i < KYBER_K; i++) {
  137. PQCLEAN_KYBER512_CLEAN_poly_reduce(&r->vec[i]);
  138. }
  139. }
  140. /*************************************************
  141. * Name: PQCLEAN_KYBER512_CLEAN_polyvec_csubq
  142. *
  143. * Description: Applies conditional subtraction of q to each coefficient
  144. * of each element of a vector of polynomials
  145. * for details of conditional subtraction of q see comments in reduce.c
  146. *
  147. * Arguments: - poly *r: pointer to input/output polynomial
  148. **************************************************/
  149. void PQCLEAN_KYBER512_CLEAN_polyvec_csubq(polyvec *r) {
  150. int i;
  151. for (i = 0; i < KYBER_K; i++) {
  152. PQCLEAN_KYBER512_CLEAN_poly_csubq(&r->vec[i]);
  153. }
  154. }
  155. /*************************************************
  156. * Name: PQCLEAN_KYBER512_CLEAN_polyvec_add
  157. *
  158. * Description: Add vectors of polynomials
  159. *
  160. * Arguments: - polyvec *r: pointer to output vector of polynomials
  161. * - const polyvec *a: pointer to first input vector of polynomials
  162. * - const polyvec *b: pointer to second input vector of polynomials
  163. **************************************************/
  164. void PQCLEAN_KYBER512_CLEAN_polyvec_add(polyvec *r, const polyvec *a, const polyvec *b) {
  165. int i;
  166. for (i = 0; i < KYBER_K; i++) {
  167. PQCLEAN_KYBER512_CLEAN_poly_add(&r->vec[i], &a->vec[i], &b->vec[i]);
  168. }
  169. }