Debian Codegraph @

sources/ippcp/ml_kem/internal/k_pke/matrix_a_gen.c

raw
1 /*************************************************************************
2 * Copyright (C) 2025 Intel Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *************************************************************************/
16
17 #include "owncp.h"
18 #include "owndefs.h"
19 #include "ippcpdefs.h"
20 #include "stateless_pqc/ml_kem_internal/ml_kem.h"
21 #include "hash/pcphash_rmf.h"
22 #include "hash/sha3/sha3_stuff.h"
23
24 /*
25 * Algorithm 7: Takes a 32-byte seed and two indices as input and outputs a pseudorandom element of T_{q}
26 */
27 #if (_IPP32E >= _IPP32E_K0)
28 /*
29 * cp_SampleNTT_MB4()
30 *
31 * Input: B1 - byte array in B^{34}, buffer #1.
32 * B2 - byte array in B^{34}, buffer #2.
33 * B3 - byte array in B^{34}, buffer #3.
34 * B4 - byte array in B^{34}, buffer #4.
35 * numBuffers - number of buffers to be processed (up to 4).
36 * mlkemCtx - pointer to state.
37 * Output: polyA - pointer to the numBuffers polynomials Z_{q}^{256} with sampled values.
38 */
39 #define CP_ML_KEM_SAMPLENTT_BUFF_SIZE (258)
40 /* clang-format off */
41 IPPCP_INLINE IppStatus cp_SampleNTT_MB4(Ipp16sPoly* polyA, const Ipp8u B1[34], const Ipp8u B2[34],
42 const Ipp8u B3[34], const Ipp8u B4[34], const Ipp32s numBuffers,
43 IppsMLKEMState* mlkemCtx)
44 /* clang-format on */
45 {
46 /* Prepare the multi-buffer hash state */
47 Ipp8u state_buffer_mb4[STATE_x4_SIZE];
48 cpSHA3_SHAKE128Ctx_mb4 state_mb4;
49 state_mb4.ctx = state_buffer_mb4;
50
51 /* Update hash state */
52 cp_SHA3_SHAKE128_InitMB4(&state_mb4);
53 cp_SHA3_SHAKE128_AbsorbMB4(&state_mb4, B1, B2, B3, B4, 34);
54 cp_SHA3_SHAKE128_FinalizeMB4(&state_mb4);
55
56 /* The hash squeeze loop for up to 4 buffers */
57 Ipp32u buffer_bytes_used = 0;
58 Ipp8u arrC[CP_ML_KEM_NUM_BUFFERS][CP_ML_KEM_SAMPLENTT_BUFF_SIZE];
59 /* Squeeze the first big block unconditionally */
60 cp_SHA3_SHAKE128_SqueezeMB4(arrC[0],
61 arrC[1],
62 arrC[2],
63 arrC[3],
64 CP_ML_KEM_SAMPLENTT_BUFF_SIZE,
65 &state_mb4);
66
67 /* Looping index is separate for each buffer */
68 Ipp16u j[CP_ML_KEM_NUM_BUFFERS] = { 0, 0, 0, 0 };
69 while ((((numBuffers - 1) >= 0) && (j[0] < 256)) || (((numBuffers - 2) >= 0) && (j[1] < 256)) ||
70 (((numBuffers - 3) >= 0) && (j[2] < 256)) || (((numBuffers - 4) >= 0) && (j[3] < 256))) {
71
72 if (buffer_bytes_used >= CP_ML_KEM_SAMPLENTT_BUFF_SIZE) {
73 cp_SHA3_SHAKE128_SqueezeMB4(arrC[0],
74 arrC[1],
75 arrC[2],
76 arrC[3],
77 CP_ML_KEM_SAMPLENTT_BUFF_SIZE,
78 &state_mb4);
79 buffer_bytes_used = 0;
80 }
81
82 Ipp16u d1[CP_ML_KEM_NUM_BUFFERS];
83 d1[0] = arrC[0][buffer_bytes_used + 0] + 256 * (arrC[0][buffer_bytes_used + 1] % 16);
84 d1[1] = arrC[1][buffer_bytes_used + 0] + 256 * (arrC[1][buffer_bytes_used + 1] % 16);
85 d1[2] = arrC[2][buffer_bytes_used + 0] + 256 * (arrC[2][buffer_bytes_used + 1] % 16);
86 d1[3] = arrC[3][buffer_bytes_used + 0] + 256 * (arrC[3][buffer_bytes_used + 1] % 16);
87
88 Ipp16u d2[CP_ML_KEM_NUM_BUFFERS];
89 d2[0] = arrC[0][buffer_bytes_used + 1] / 16 + 16 * arrC[0][buffer_bytes_used + 2];
90 d2[1] = arrC[1][buffer_bytes_used + 1] / 16 + 16 * arrC[1][buffer_bytes_used + 2];
91 d2[2] = arrC[2][buffer_bytes_used + 1] / 16 + 16 * arrC[2][buffer_bytes_used + 2];
92 d2[3] = arrC[3][buffer_bytes_used + 1] / 16 + 16 * arrC[3][buffer_bytes_used + 2];
93
94 // Fill elements of up to 4 polynomials
95 for (Ipp32s buf = 0; buf < numBuffers; buf++) {
96 if ((d1[buf] < mlkemCtx->params.q) && (j[buf] < 256)) {
97 polyA[buf].values[j[buf]] = (Ipp16s)d1[buf];
98 j[buf]++;
99 }
100 if ((d2[buf] < mlkemCtx->params.q) && (j[buf] < 256)) {
101 polyA[buf].values[j[buf]] = (Ipp16s)d2[buf];
102 j[buf]++;
103 }
104 }
105 buffer_bytes_used += 3;
106 }
107
108 return ippStsNoErr;
109 }
110 #undef CP_ML_KEM_SAMPLENTT_BUFF_SIZE
111
112 #else
113 /*
114 * cp_SampleNTT()
115 *
116 * Input: B - byte array in B^{34}.
117 * mlkemCtx - pointer to state.
118 * Output: polyA - polynomial Z_{q}^{256} with sampled values.
119 */
120 /* clang-format off */
121 IPPCP_INLINE IppStatus cp_SampleNTT(Ipp16sPoly* polyA, const Ipp8u B[34], IppsMLKEMState* mlkemCtx)
122 /* clang-format on */
123 {
124 IppStatus sts = ippStsNoErr;
125 _cpMLKEMStorage* pStorage = &mlkemCtx->storage;
126
127 const IppsHashMethod* hash_method = ippsHashMethod_SHAKE128(3 * 8 * 256);
128 int hash_size = 0;
129 sts = ippsHashGetSizeOptimal_rmf(&hash_size, hash_method);
130 IPP_BADARG_RET((sts != ippStsNoErr), sts);
131 IppsHashState_rmf* hash_state =
132 (IppsHashState_rmf*)cp_mlStorageAllocate(pStorage, hash_size + CP_ML_KEM_ALIGNMENT);
133 CP_CHECK_FREE_RET(hash_state == NULL, ippStsMemAllocErr, pStorage);
134 hash_state = IPP_ALIGNED_PTR(hash_state, CP_ML_KEM_ALIGNMENT);
135 sts = ippsHashInit_rmf(hash_state, hash_method);
136 IPP_BADARG_RET((sts != ippStsNoErr), sts);
137 sts = ippsHashUpdate_rmf(B, 34, hash_state);
138 IPP_BADARG_RET((sts != ippStsNoErr), sts);
139
140 for (Ipp16u j = 0; (j < 256) && (sts == ippStsNoErr);) {
141 Ipp8u arrC[3];
142 sts = ippsHashSqueeze_rmf(arrC, 3, hash_state);
143
144 Ipp16u d1 = arrC[0] + 256 * (arrC[1] % 16);
145 Ipp16u d2 = arrC[1] / 16 + 16 * arrC[2];
146 if (d1 < mlkemCtx->params.q) {
147 polyA->values[j] = (Ipp16s)d1;
148 j++;
149 }
150 if ((d2 < mlkemCtx->params.q) && (j < 256)) {
151 polyA->values[j] = (Ipp16s)d2;
152 j++;
153 }
154 }
155
156 /* Release locally used storage */
157 sts = cp_mlStorageRelease(pStorage, hash_size + CP_ML_KEM_ALIGNMENT); // hash_state
158
159 return sts;
160 }
161 #endif /* #if (_IPP32E >= _IPP32E_K0) */
162
163 /*
164 * Generates the matrix A for the ML KEM scheme.
165 *
166 * Input: rho_j_i - byte array of size 34 bytes, where the first 32 bytes are the seed
167 * and the last two bytes are indices i and j
168 * matrixType - flag reflecting the type of matrix to be generated
169 * mlkemCtx - pointer to state.
170 * Output: matrixA - output pointer to the matrix A of size k*k elements
171 *
172 * Note: cp_SampleNTT is the main computation kernel.
173 */
174 /* clang-format off */
175 IPP_OWN_DEFN(IppStatus, cp_matrixAGen,
176 (Ipp16sPoly* matrixA, Ipp8u rho_j_i_0[34], matrixAGenType matrixType, IppsMLKEMState* mlkemCtx))
177 /* clang-format on */
178 {
179 IppStatus sts = ippStsNoErr;
180 const Ipp8u k = mlkemCtx->params.k;
181
182 /* Multi-buffer approach */
183 #if (_IPP32E >= _IPP32E_K0)
184 /* Prepare rho for the multi-buffer processing */
185 Ipp8u rho_j_i_1[34];
186 Ipp8u rho_j_i_2[34];
187 Ipp8u rho_j_i_3[34];
188 CopyBlock(rho_j_i_0, rho_j_i_1, 32);
189 CopyBlock(rho_j_i_0, rho_j_i_2, 32);
190 CopyBlock(rho_j_i_0, rho_j_i_3, 32);
191
192 Ipp32u writeIdx = (matrixType == matrixAOrigin) ? 32 : 33;
193 rho_j_i_0[writeIdx] = 0;
194 rho_j_i_1[writeIdx] = 1;
195 rho_j_i_2[writeIdx] = 2;
196 rho_j_i_3[writeIdx] = 3;
197
198 // Change the index for the processing in the loop
199 writeIdx = (matrixType == matrixAOrigin) ? 33 : 32;
200
201 for (Ipp8u i = 0; i < k; i++) {
202 rho_j_i_0[writeIdx] = i;
203 rho_j_i_1[writeIdx] = i;
204 rho_j_i_2[writeIdx] = i;
205 rho_j_i_3[writeIdx] = i;
206 Ipp16sPoly* pMatrixAij = &matrixA[i * k];
207
208 /* A[i, j] <- cp_SampleNTT_MB4(rho||i||j) */
209 sts = cp_SampleNTT_MB4(pMatrixAij, rho_j_i_0, rho_j_i_1, rho_j_i_2, rho_j_i_3, k, mlkemCtx);
210 IPP_BADARG_RET((sts != ippStsNoErr), sts);
211 }
212 #else
213 for (Ipp8u i = 0; i < k; i++) {
214 for (Ipp8u j = 0; j < k; j++) {
215 if (matrixType == matrixAOrigin) {
216 rho_j_i_0[32] = j;
217 rho_j_i_0[33] = i;
218 } else { // matrixType == matrixATransposed
219 rho_j_i_0[32] = i;
220 rho_j_i_0[33] = j;
221 }
222 Ipp16sPoly* pMatrixAij = &matrixA[i * k + j];
223
224 /* A[i, j] <- cp_SampleNTT(rho||i||j) */
225 sts = cp_SampleNTT(pMatrixAij, rho_j_i_0, mlkemCtx);
226 IPP_BADARG_RET((sts != ippStsNoErr), sts);
227 }
228 }
229
230 #endif /* #if (_IPP32E >= _IPP32E_K0) */
231
232 return sts;
233 }