Debian Codegraph @

crypto_kem/6688128f/avx/encrypt.c

raw
1 /*
2 This file is for Niederreiter encryption
3 */
4 // 20240508 djb: switch to crypto_sort_int32
5 // 20240508 djb: use xor_mat_vec256
6 // 20240504 djb: negifcollision
7 // 20240504 djb: use crypto_xof_bitwrite16
8 // 20240503 djb: remove #ifdef KAT ... #endif
9 // 20230102 djb: rename encrypt() as pke_encrypt()
10 // 20221231 djb: move encrypt.h last for macos portability; tnx thom wiggers
11 // 20221230 djb: add linker lines
12
13 // linker define pke_encrypt
14 // linker use xor_mat_vec256
15
16 #include "util.h"
17 #include "params.h"
18 #include "crypto_sort_int32.h"
19 #include "randombytes.h"
20
21 #include "crypto_declassify.h"
22 #include "crypto_uint16.h"
23 #include "crypto_int32.h"
24 #include "crypto_xof_bitwrite16.h"
25
26 #include "xor_mat_vec256.h"
27 #include "encrypt.h"
28
29 static inline crypto_uint16 uint16_is_smaller_declassify(uint16_t t,uint16_t u)
30 {
31 crypto_uint16 mask = crypto_uint16_smaller_mask(t,u);
32 crypto_declassify(&mask,sizeof mask);
33 return mask;
34 }
35
36 /* output: e, an error vector of weight t */
37 static void gen_e(unsigned char *e)
38 {
39 int i, j, count;
40
41 union
42 {
43 uint16_t nums[ SYS_T*2 ];
44 unsigned char bytes[ SYS_T*2 * sizeof(uint16_t) ];
45 } buf;
46
47 int32_t ind[ SYS_T ]; // can also use uint16 or int16
48 crypto_int32 negifcollision;
49 unsigned char indbytes[ SYS_T*2 ];
50
51 while (1)
52 {
53 randombytes(buf.bytes, sizeof(buf));
54
55 for (i = 0; i < SYS_T*2; i++)
56 buf.nums[i] = load_gf(buf.bytes + i*2);
57
58 // moving and counting indices in the correct range
59
60 count = 0;
61 for (i = 0; i < SYS_T*2 && count < SYS_T; i++)
62 if (uint16_is_smaller_declassify(buf.nums[i],SYS_N))
63 ind[ count++ ] = buf.nums[i];
64
65 if (count < SYS_T) continue;
66
67 // check for repetition
68
69 crypto_sort_int32(ind, SYS_T);
70
71 negifcollision = 0;
72 for (i = 1; i < SYS_T; i++)
73 negifcollision |= (ind[i-1]^ind[i])-1;
74
75 negifcollision = crypto_int32_negative_mask(negifcollision);
76 crypto_declassify(&negifcollision,sizeof negifcollision);
77
78 if (!negifcollision)
79 break;
80 }
81
82 for (j = 0; j < SYS_T; j++)
83 crypto_uint16_store(indbytes+2*j,ind[j]);
84
85 crypto_xof_bitwrite16(e,SYS_N/8,indbytes,2*SYS_T);
86 }
87
88 /* input: public key pk, error vector e */
89 /* output: syndrome s */
90 static void syndrome(unsigned char *s, const unsigned char *pk, unsigned char *e)
91 {
92 const unsigned char *e_ptr = e + SYND_BYTES;
93 vec256 eword[PK_NCOLS/256+1];
94 int i, j;
95
96 for (i = 0; i < SYND_BYTES; i++)
97 s[i] = e[i];
98
99 for (j = 0;j < PK_NCOLS/256;++j)
100 eword[j] = vec256_load(e_ptr+32*j);
101
102 eword[j] = vec256_load(e_ptr+PK_ROW_BYTES-32);
103 eword[j] &= vec256_set8x(0,0,0,-1,-1,-1,-1,-1);
104
105 xor_mat_vec256(s, pk, PK_NROWS, PK_ROW_BYTES, eword);
106 }
107
108 /* input: public key pk */
109 /* output: error vector e, syndrome s */
110 void pke_encrypt(unsigned char *s, const unsigned char *pk, unsigned char *e)
111 {
112 gen_e(e);
113 syndrome(s, pk, e);
114 }