kyber512r3_poly.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include <stdint.h>
  2. #include "kyber512r3_params.h"
  3. #include "kyber512r3_poly.h"
  4. #include "kyber512r3_ntt.h"
  5. #include "kyber512r3_reduce.h"
  6. #include "kyber512r3_cbd.h"
  7. #include "kyber512r3_symmetric.h"
  8. S2N_ENSURE_PORTABLE_OPTIMIZATIONS
  9. /*************************************************
  10. * Name: poly_compress
  11. *
  12. * Description: Compression and subsequent serialization of a polynomial
  13. *
  14. * Arguments: - uint8_t *r: pointer to output byte array
  15. * (of length S2N_KYBER_512_R3_POLYCOMPRESSEDBYTES)
  16. * - poly *a: pointer to input polynomial
  17. **************************************************/
  18. void poly_compress(uint8_t r[S2N_KYBER_512_R3_POLYCOMPRESSEDBYTES], poly *a) {
  19. unsigned int i, j;
  20. uint8_t t[8];
  21. poly_csubq(a);
  22. for (i = 0; i < S2N_KYBER_512_R3_N / 8; i++) {
  23. for (j = 0; j < 8; j++) {
  24. t[j] = ((((uint16_t)a->coeffs[8 * i + j] << 4) + S2N_KYBER_512_R3_Q / 2) / S2N_KYBER_512_R3_Q) & 15;
  25. }
  26. r[0] = t[0] | (t[1] << 4);
  27. r[1] = t[2] | (t[3] << 4);
  28. r[2] = t[4] | (t[5] << 4);
  29. r[3] = t[6] | (t[7] << 4);
  30. r += 4;
  31. }
  32. }
  33. /*************************************************
  34. * Name: poly_decompress
  35. *
  36. * Description: De-serialization and subsequent decompression of a polynomial;
  37. * approximate inverse of poly_compress
  38. *
  39. * Arguments: - poly *r: pointer to output polynomial
  40. * - const uint8_t *a: pointer to input byte array
  41. * (of length S2N_KYBER_512_R3_POLYCOMPRESSEDBYTES bytes)
  42. **************************************************/
  43. void poly_decompress(poly *r, const uint8_t a[S2N_KYBER_512_R3_POLYCOMPRESSEDBYTES]) {
  44. unsigned int i;
  45. for (i = 0; i < S2N_KYBER_512_R3_N / 2; i++) {
  46. r->coeffs[2 * i + 0] = (((uint16_t)(a[0] & 15) * S2N_KYBER_512_R3_Q) + 8) >> 4;
  47. r->coeffs[2 * i + 1] = (((uint16_t)(a[0] >> 4) * S2N_KYBER_512_R3_Q) + 8) >> 4;
  48. a += 1;
  49. }
  50. }
  51. /*************************************************
  52. * Name: poly_tobytes
  53. *
  54. * Description: Serialization of a polynomial
  55. *
  56. * Arguments: - uint8_t *r: pointer to output byte array
  57. * (needs space for S2N_KYBER_512_R3_POLYBYTES bytes)
  58. * - poly *a: pointer to input polynomial
  59. **************************************************/
  60. void poly_tobytes(uint8_t r[S2N_KYBER_512_R3_POLYBYTES], poly *a) {
  61. unsigned int i;
  62. poly_csubq(a);
  63. for (i = 0; i < S2N_KYBER_512_R3_N / 2; i++) {
  64. uint16_t t0 = a->coeffs[2 * i];
  65. uint16_t t1 = a->coeffs[2 * i + 1];
  66. r[3 * i + 0] = (t0 >> 0);
  67. r[3 * i + 1] = (t0 >> 8) | (t1 << 4);
  68. r[3 * i + 2] = (t1 >> 4);
  69. }
  70. }
  71. /*************************************************
  72. * Name: poly_frombytes
  73. *
  74. * Description: De-serialization of a polynomial;
  75. * inverse of poly_tobytes
  76. *
  77. * Arguments: - poly *r: pointer to output polynomial
  78. * - const uint8_t *a: pointer to input byte array
  79. * (of S2N_KYBER_512_R3_POLYBYTES bytes)
  80. **************************************************/
  81. void poly_frombytes(poly *r, const uint8_t a[S2N_KYBER_512_R3_POLYBYTES]) {
  82. unsigned int i;
  83. for (i = 0; i < S2N_KYBER_512_R3_N / 2; i++) {
  84. r->coeffs[2 * i] = ((a[3 * i + 0] >> 0) | ((uint16_t)a[3 * i + 1] << 8)) & 0xFFF;
  85. r->coeffs[2 * i + 1] = ((a[3 * i + 1] >> 4) | ((uint16_t)a[3 * i + 2] << 4)) & 0xFFF;
  86. }
  87. }
  88. /*************************************************
  89. * Name: poly_frommsg
  90. *
  91. * Description: Convert 32-byte message to polynomial
  92. *
  93. * Arguments: - poly *r: pointer to output polynomial
  94. * - const uint8_t *msg: pointer to input message
  95. **************************************************/
  96. void poly_frommsg(poly *r, const uint8_t msg[S2N_KYBER_512_R3_INDCPA_MSGBYTES]) {
  97. unsigned int i, j;
  98. int16_t mask;
  99. for (i = 0; i < S2N_KYBER_512_R3_N / 8; i++) {
  100. for (j = 0; j < 8; j++) {
  101. mask = -(int16_t)((msg[i] >> j) & 1);
  102. r->coeffs[8 * i + j] = mask & ((S2N_KYBER_512_R3_Q + 1) / 2);
  103. }
  104. }
  105. }
  106. /*************************************************
  107. * Name: poly_tomsg
  108. *
  109. * Description: Convert polynomial to 32-byte message
  110. *
  111. * Arguments: - uint8_t *msg: pointer to output message
  112. * - poly *a: pointer to input polynomial
  113. **************************************************/
  114. void poly_tomsg(uint8_t msg[S2N_KYBER_512_R3_INDCPA_MSGBYTES], poly *a) {
  115. unsigned int i, j;
  116. uint16_t t;
  117. poly_csubq(a);
  118. for (i = 0; i < S2N_KYBER_512_R3_N / 8; i++) {
  119. msg[i] = 0;
  120. for (j = 0; j < 8; j++) {
  121. t = ((((uint16_t)a->coeffs[8 * i + j] << 1) + S2N_KYBER_512_R3_Q / 2) / S2N_KYBER_512_R3_Q) & 1;
  122. msg[i] |= t << j;
  123. }
  124. }
  125. }
  126. /*************************************************
  127. * Name: poly_getnoise_eta1
  128. *
  129. * Description: Sample a polynomial deterministically from a seed and a nonce,
  130. * with output polynomial close to centered binomial distribution
  131. * with parameter S2N_KYBER_512_R3_ETA1
  132. *
  133. * Arguments: - poly *r: pointer to output polynomial
  134. * - const uint8_t *seed: pointer to input seed
  135. * (of length S2N_KYBER_512_R3_SYMBYTES bytes)
  136. * - uint8_t nonce: one-byte input nonce
  137. **************************************************/
  138. void poly_getnoise_eta1(poly *r, const uint8_t seed[S2N_KYBER_512_R3_SYMBYTES], uint8_t nonce) {
  139. uint8_t buf[S2N_KYBER_512_R3_ETA1 * S2N_KYBER_512_R3_N / 4];
  140. shake256_prf(buf, sizeof(buf), seed, nonce);
  141. cbd_eta1(r, buf);
  142. }
  143. /*************************************************
  144. * Name: poly_getnoise_eta2
  145. *
  146. * Description: Sample a polynomial deterministically from a seed and a nonce,
  147. * with output polynomial close to centered binomial distribution
  148. * with parameter S2N_KYBER_512_R3_ETA2
  149. *
  150. * Arguments: - poly *r: pointer to output polynomial
  151. * - const uint8_t *seed: pointer to input seed
  152. * (of length S2N_KYBER_512_R3_SYMBYTES bytes)
  153. * - uint8_t nonce: one-byte input nonce
  154. **************************************************/
  155. void poly_getnoise_eta2(poly *r, const uint8_t seed[S2N_KYBER_512_R3_SYMBYTES], uint8_t nonce) {
  156. uint8_t buf[S2N_KYBER_512_R3_ETA2 * S2N_KYBER_512_R3_N / 4];
  157. shake256_prf(buf, sizeof(buf), seed, nonce);
  158. cbd_eta2(r, buf);
  159. }
  160. /*************************************************
  161. * Name: poly_ntt
  162. *
  163. * Description: Computes negacyclic number-theoretic transform (NTT) of
  164. * a polynomial in place;
  165. * inputs assumed to be in normal order, output in bitreversed order
  166. *
  167. * Arguments: - uint16_t *r: pointer to in/output polynomial
  168. **************************************************/
  169. void poly_ntt(poly *r) {
  170. ntt(r->coeffs);
  171. poly_reduce(r);
  172. }
  173. /*************************************************
  174. * Name: poly_invntt_tomont
  175. *
  176. * Description: Computes inverse of negacyclic number-theoretic transform (NTT)
  177. * of a polynomial in place;
  178. * inputs assumed to be in bitreversed order, output in normal order
  179. *
  180. * Arguments: - uint16_t *a: pointer to in/output polynomial
  181. **************************************************/
  182. void poly_invntt_tomont(poly *r) {
  183. invntt(r->coeffs);
  184. }
  185. /*************************************************
  186. * Name: poly_basemul_montgomery
  187. *
  188. * Description: Multiplication of two polynomials in NTT domain
  189. *
  190. * Arguments: - poly *r: pointer to output polynomial
  191. * - const poly *a: pointer to first input polynomial
  192. * - const poly *b: pointer to second input polynomial
  193. **************************************************/
  194. void poly_basemul_montgomery(poly *r, const poly *a, const poly *b) {
  195. unsigned int i;
  196. for (i = 0; i < S2N_KYBER_512_R3_N / 4; i++) {
  197. basemul(&r->coeffs[4 * i], &a->coeffs[4 * i], &b->coeffs[4 * i], zetas[64 + i]);
  198. basemul(&r->coeffs[4 * i + 2], &a->coeffs[4 * i + 2], &b->coeffs[4 * i + 2],
  199. -zetas[64 + i]);
  200. }
  201. }
  202. /*************************************************
  203. * Name: poly_tomont
  204. *
  205. * Description: Inplace conversion of all coefficients of a polynomial
  206. * from normal domain to Montgomery domain
  207. *
  208. * Arguments: - poly *r: pointer to input/output polynomial
  209. **************************************************/
  210. void poly_tomont(poly *r) {
  211. unsigned int i;
  212. const int16_t f = (1ULL << 32) % S2N_KYBER_512_R3_Q;
  213. for (i = 0; i < S2N_KYBER_512_R3_N; i++) {
  214. r->coeffs[i] = montgomery_reduce((int32_t)r->coeffs[i] * f);
  215. }
  216. }
  217. /*************************************************
  218. * Name: poly_reduce
  219. *
  220. * Description: Applies Barrett reduction to all coefficients of a polynomial
  221. * for details of the Barrett reduction see comments in reduce.c
  222. *
  223. * Arguments: - poly *r: pointer to input/output polynomial
  224. **************************************************/
  225. void poly_reduce(poly *r) {
  226. unsigned int i;
  227. for (i = 0; i < S2N_KYBER_512_R3_N; i++) {
  228. r->coeffs[i] = barrett_reduce(r->coeffs[i]);
  229. }
  230. }
  231. /*************************************************
  232. * Name: poly_csubq
  233. *
  234. * Description: Applies conditional subtraction of q to each coefficient
  235. * of a polynomial. For details of conditional subtraction
  236. * of q see comments in reduce.c
  237. *
  238. * Arguments: - poly *r: pointer to input/output polynomial
  239. **************************************************/
  240. void poly_csubq(poly *r) {
  241. unsigned int i;
  242. for (i = 0; i < S2N_KYBER_512_R3_N; i++) {
  243. r->coeffs[i] = csubq(r->coeffs[i]);
  244. }
  245. }
  246. /*************************************************
  247. * Name: poly_add
  248. *
  249. * Description: Add two polynomials
  250. *
  251. * Arguments: - poly *r: pointer to output polynomial
  252. * - const poly *a: pointer to first input polynomial
  253. * - const poly *b: pointer to second input polynomial
  254. **************************************************/
  255. void poly_add(poly *r, const poly *a, const poly *b) {
  256. unsigned int i;
  257. for (i = 0; i < S2N_KYBER_512_R3_N; i++) {
  258. r->coeffs[i] = a->coeffs[i] + b->coeffs[i];
  259. }
  260. }
  261. /*************************************************
  262. * Name: poly_sub
  263. *
  264. * Description: Subtract two polynomials
  265. *
  266. * Arguments: - poly *r: pointer to output polynomial
  267. * - const poly *a: pointer to first input polynomial
  268. * - const poly *b: pointer to second input polynomial
  269. **************************************************/
  270. void poly_sub(poly *r, const poly *a, const poly *b) {
  271. unsigned int i;
  272. for (i = 0; i < S2N_KYBER_512_R3_N; i++) {
  273. r->coeffs[i] = a->coeffs[i] - b->coeffs[i];
  274. }
  275. }