references/NIST-PQ-Submission-Kyber-20201001/Additional_Implementations/avx2/crypto_kem/kyber768/poly.c
/*************************************************
* Name:        poly_frommsg
*
* Description: Convert 32-byte message to polynomial
*
* Arguments:   - poly *r:            pointer to output polynomial
*              - const uint8_t *msg: pointer to input message
**************************************************/
poly_frommsg
Description: Convert 32-byte message to polynomial

- poly *r:            pointer to output polynomial
const uint8_t *msg: pointer to input message
poly_frommsg Description : Convert 32 - byte message to polynomial - poly * r : pointer to output polynomial const uint8_t * msg : pointer to input message
void poly_frommsg(poly * restrict r,
                  const uint8_t msg[KYBER_INDCPA_MSGBYTES])
{
#if (KYBER_INDCPA_MSGBYTES != 32)
#error "KYBER_INDCPA_MSGBYTES must be equal to 32!"
#endif
  __m256i f, g0, g1, g2, g3, h0, h1, h2, h3;
  const __m256i shift = _mm256_broadcastsi128_si256(_mm_set_epi32(0,1,2,3));
  const __m256i idx = _mm256_broadcastsi128_si256(_mm_set_epi8(15,14,11,10,7,6,3,2,13,12,9,8,5,4,1,0));
  const __m256i hqs = _mm256_set1_epi16((KYBER_Q+1)/2);
#define FROMMSG64(i)						\
  g3 = _mm256_shuffle_epi32(f,0x55*i);				\
  g3 = _mm256_sllv_epi32(g3,shift);				\
  g3 = _mm256_shuffle_epi8(g3,idx);				\
  g0 = _mm256_slli_epi16(g3,12);				\
  g1 = _mm256_slli_epi16(g3,8);					\
  g2 = _mm256_slli_epi16(g3,4);					\
  g0 = _mm256_srai_epi16(g0,15);				\
  g1 = _mm256_srai_epi16(g1,15);				\
  g2 = _mm256_srai_epi16(g2,15);				\
  g3 = _mm256_srai_epi16(g3,15);				\
  g0 = _mm256_and_si256(g0,hqs);  /* 19 18 17 16  3  2  1  0 */	\
  g1 = _mm256_and_si256(g1,hqs);  /* 23 22 21 20  7  6  5  4 */	\
  g2 = _mm256_and_si256(g2,hqs);  /* 27 26 25 24 11 10  9  8 */	\
  g3 = _mm256_and_si256(g3,hqs);  /* 31 30 29 28 15 14 13 12 */	\
  h0 = _mm256_unpacklo_epi64(g0,g1);				\
  h2 = _mm256_unpackhi_epi64(g0,g1);				\
  h1 = _mm256_unpacklo_epi64(g2,g3);				\
  h3 = _mm256_unpackhi_epi64(g2,g3);				\
  g0 = _mm256_permute2x128_si256(h0,h1,0x20);			\
  g2 = _mm256_permute2x128_si256(h0,h1,0x31);			\
  g1 = _mm256_permute2x128_si256(h2,h3,0x20);			\
  g3 = _mm256_permute2x128_si256(h2,h3,0x31);			\
  _mm256_store_si256((__m256i *)&r->coeffs[  0+32*i+ 0],g0);	\
  _mm256_store_si256((__m256i *)&r->coeffs[  0+32*i+16],g1);	\
  _mm256_store_si256((__m256i *)&r->coeffs[128+32*i+ 0],g2);	\
  _mm256_store_si256((__m256i *)&r->coeffs[128+32*i+16],g3)
  f = _mm256_load_si256((__m256i *)msg);
  FROMMSG64(0);
  FROMMSG64(1);
  FROMMSG64(2);
  FROMMSG64(3);
}
void poly_frommsg ( poly * restrict r , const uint8_t msg [ KYBER_INDCPA_MSGBYTES ] ) { #if ( KYBER_INDCPA_MSGBYTES != 32 ) #error  "KYBER_INDCPA_MSGBYTES must be equal to 32!" #endif __m256i f , g0 , g1 , g2 , g3 , h0 , h1 , h2 , h3 ; const __m256i shift = _mm256_broadcastsi128_si256 ( _mm_set_epi32 ( 0 , 1 , 2 , 3 ) ) ; const __m256i idx = _mm256_broadcastsi128_si256 ( _mm_set_epi8 ( 15 , 14 , 11 , 10 , 7 , 6 , 3 , 2 , 13 , 12 , 9 , 8 , 5 , 4 , 1 , 0 ) ) ; const __m256i hqs = _mm256_set1_epi16 ( ( KYBER_Q + 1 ) / 2 ) ; #define FROMMSG64 ( i ) 						\
  g3 = _mm256_shuffle_epi32(f,0x55*i);				\
  g3 = _mm256_sllv_epi32(g3,shift);				\
  g3 = _mm256_shuffle_epi8(g3,idx);				\
  g0 = _mm256_slli_epi16(g3,12);				\
  g1 = _mm256_slli_epi16(g3,8);					\
  g2 = _mm256_slli_epi16(g3,4);					\
  g0 = _mm256_srai_epi16(g0,15);				\
  g1 = _mm256_srai_epi16(g1,15);				\
  g2 = _mm256_srai_epi16(g2,15);				\
  g3 = _mm256_srai_epi16(g3,15);				\
  g0 = _mm256_and_si256(g0,hqs);  /* 19 18 17 16  3  2  1  0 */	\
  g1 = _mm256_and_si256(g1,hqs);  /* 23 22 21 20  7  6  5  4 */	\
  g2 = _mm256_and_si256(g2,hqs);  /* 27 26 25 24 11 10  9  8 */	\
  g3 = _mm256_and_si256(g3,hqs);  /* 31 30 29 28 15 14 13 12 */	\
  h0 = _mm256_unpacklo_epi64(g0,g1);				\
  h2 = _mm256_unpackhi_epi64(g0,g1);				\
  h1 = _mm256_unpacklo_epi64(g2,g3);				\
  h3 = _mm256_unpackhi_epi64(g2,g3);				\
  g0 = _mm256_permute2x128_si256(h0,h1,0x20);			\
  g2 = _mm256_permute2x128_si256(h0,h1,0x31);			\
  g1 = _mm256_permute2x128_si256(h2,h3,0x20);			\
  g3 = _mm256_permute2x128_si256(h2,h3,0x31);			\
  _mm256_store_si256((__m256i *)&r->coeffs[  0+32*i+ 0],g0);	\
  _mm256_store_si256((__m256i *)&r->coeffs[  0+32*i+16],g1);	\
  _mm256_store_si256((__m256i *)&r->coeffs[128+32*i+ 0],g2);	\
  _mm256_store_si256((__m256i *)&r->coeffs[128+32*i+16],g3) f = _mm256_load_si256 ( ( __m256i * ) msg ) ; FROMMSG64 ( 0 ) ; FROMMSG64 ( 1 ) ; FROMMSG64 ( 2 ) ; FROMMSG64 ( 3 ) ; }
Name:        poly_frommsg
Description: Convert 32-byte message to polynomial
Name : poly_frommsg Description : Convert 32 - byte message to polynomial