Debian Codegraph @

crypto_kem/8192128pcf/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 /* output: e, an error vector of weight t */
30 static void gen_e(unsigned char *e)
31 {
32 int i, j;
33
34 int32_t ind[ SYS_T ]; // can also use uint16 or int16
35 crypto_int32 negifcollision;
36 unsigned char indbytes[ SYS_T*2 ];
37 unsigned char bytes[ SYS_T * 2 ];
38
39 while (1)
40 {
41 randombytes(bytes, sizeof(bytes));
42
43 for (i = 0; i < SYS_T; i++)
44 ind[i] = load_gf(bytes + i*2);
45
46 // check for repetition
47
48 crypto_sort_int32(ind, SYS_T);
49
50 negifcollision = 0;
51 for (i = 1; i < SYS_T; i++)
52 negifcollision |= (ind[i-1]^ind[i])-1;
53
54 negifcollision = crypto_int32_negative_mask(negifcollision);
55 crypto_declassify(&negifcollision,sizeof negifcollision);
56
57 if (!negifcollision)
58 break;
59 }
60
61 for (j = 0; j < SYS_T; j++)
62 crypto_uint16_store(indbytes+2*j,ind[j]);
63
64 crypto_xof_bitwrite16(e,SYS_N/8,indbytes,2*SYS_T);
65 }
66
67 /* input: public key pk, error vector e */
68 /* output: syndrome s */
69 static void syndrome(unsigned char *s, const unsigned char *pk, unsigned char *e)
70 {
71 const unsigned char *e_ptr = e + SYND_BYTES;
72 vec256 eword[PK_NCOLS/256+1];
73 int i, j;
74
75 for (i = 0; i < SYND_BYTES; i++)
76 s[i] = e[i];
77
78 for (j = 0;j < PK_NCOLS/256;++j)
79 eword[j] = vec256_load(e_ptr+32*j);
80
81 eword[j] = vec256_load(e_ptr+PK_ROW_BYTES-32);
82 eword[j] &= vec256_set8x(0,0,0,0,-1,-1,-1,-1);
83
84 xor_mat_vec256(s, pk, PK_NROWS, PK_ROW_BYTES, eword);
85 }
86
87 /* input: public key pk */
88 /* output: error vector e, syndrome s */
89 void pke_encrypt(unsigned char *s, const unsigned char *pk, unsigned char *e)
90 {
91 gen_e(e);
92 syndrome(s, pk, e);
93 }