Debian Codegraph @

crypto_kem/8192128pcf/vec/sk_gen.c

raw
1 /*
2 This file is for secret-key generation
3 */
4 // 20221230 djb: add linker lines
5
6 // linker define genpoly_gen
7 // linker use gf_iszero gf_mul gf_inv GF_mul
8
9 #include "sk_gen.h"
10
11 #include "randombytes.h"
12 #include "controlbits.h"
13 #include "params.h"
14 #include "util.h"
15 #include "gf.h"
16 #include "crypto_declassify.h"
17 #include "crypto_uint16.h"
18
19 static inline crypto_uint16 gf_is_zero_declassify(gf t)
20 {
21 crypto_uint16 mask = crypto_uint16_zero_mask(t);
22 crypto_declassify(&mask,sizeof mask);
23 return mask;
24 }
25
26 /* input: f, element in GF((2^m)^t) */
27 /* output: out, minimal polynomial of f */
28 /* return: 0 for success and -1 for failure */
29 int genpoly_gen(gf *out, gf *f)
30 {
31 int i, j, k, c;
32
33 gf mat[ SYS_T+1 ][ SYS_T ];
34 gf mask, inv, t;
35
36 // fill matrix
37
38 mat[0][0] = 1;
39
40 for (i = 1; i < SYS_T; i++)
41 mat[0][i] = 0;
42
43 for (i = 0; i < SYS_T; i++)
44 mat[1][i] = f[i];
45
46 for (j = 2; j <= SYS_T; j++)
47 GF_mul(mat[j], mat[j-1], f);
48
49 // gaussian
50
51 for (j = 0; j < SYS_T; j++)
52 {
53 for (k = j + 1; k < SYS_T; k++)
54 {
55 mask = gf_iszero(mat[ j ][ j ]);
56
57 for (c = j; c < SYS_T + 1; c++)
58 mat[ c ][ j ] ^= mat[ c ][ k ] & mask;
59
60 }
61
62 if ( gf_is_zero_declassify(mat[ j ][ j ]) ) // return if not systematic
63 {
64 return -1;
65 }
66
67 inv = gf_inv(mat[j][j]);
68
69 for (c = j; c < SYS_T + 1; c++)
70 mat[ c ][ j ] = gf_mul(mat[ c ][ j ], inv) ;
71
72 for (k = 0; k < SYS_T; k++)
73 {
74 if (k != j)
75 {
76 t = mat[ j ][ k ];
77
78 for (c = j; c < SYS_T + 1; c++)
79 mat[ c ][ k ] ^= gf_mul(mat[ c ][ j ], t);
80 }
81 }
82 }
83
84 for (i = 0; i < SYS_T; i++)
85 out[i] = mat[ SYS_T ][ i ];
86
87 return 0;
88 }
89