Debian Codegraph @

sources/ippcp/ml_kem/internal/k_pke/poly.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 /*
18 * Polynomial generation functions definition
19 */
20
21 #include "owncp.h"
22 #include "owndefs.h"
23 #include "ippcpdefs.h"
24 #include "stateless_pqc/ml_kem_internal/ml_kem.h"
25 #include "hash/sha3/sha3_stuff.h"
26
27 #if (_IPP32E >= _IPP32E_K0)
28 /*
29 * Multi-buffer kernel generating up to 4 Ipp16sPoly polynomials at once.
30 *
31 * Input: inRand_N - buffer with r data, the last byte is used to store N.
32 * N - pointer to the value of N, will be incremented in-place.
33 * eta - parameter defining the distribution to sample from.
34 * transformFlag - flag to indicate whether to apply NTT transform to each generated polynomial.
35 * numBuffers - number of buffers to be processed (up to 4).
36 * Output: pOutPoly - the generated polynomials (total number equals to numBuffers).
37 *
38 */
39 IPPCP_INLINE IppStatus cp_polyGenInternal_MB4(Ipp16sPoly* pOutPoly,
40 Ipp8u inRand_N[CP_RAND_DATA_BYTES + 1],
41 Ipp8u* N,
42 const Ipp8u eta,
43 nttTransformFlag transformFlag,
44 Ipp32s numBuffers)
45 /* clang-format on */
46 {
47 IppStatus sts = ippStsNoErr;
48
49 /* Allocate and fill 4 buffers of the data with the input passed in inRand_N */
50 Ipp8u inRand_N_0[CP_RAND_DATA_BYTES + 1];
51 Ipp8u inRand_N_1[CP_RAND_DATA_BYTES + 1];
52 Ipp8u inRand_N_2[CP_RAND_DATA_BYTES + 1];
53 Ipp8u inRand_N_3[CP_RAND_DATA_BYTES + 1];
54 CopyBlock(inRand_N, inRand_N_0, CP_RAND_DATA_BYTES);
55 CopyBlock(inRand_N, inRand_N_1, CP_RAND_DATA_BYTES);
56 CopyBlock(inRand_N, inRand_N_2, CP_RAND_DATA_BYTES);
57 CopyBlock(inRand_N, inRand_N_3, CP_RAND_DATA_BYTES);
58 inRand_N_0[32] = *N;
59 inRand_N_1[32] = *N + 1;
60 inRand_N_2[32] = *N + 2;
61 inRand_N_3[32] = *N + 3;
62
63 /* Outputs of the PRF function */
64 Ipp8u prfOutput_0[CP_ML_KEM_ETA_MAX * 64];
65 Ipp8u prfOutput_1[CP_ML_KEM_ETA_MAX * 64];
66 Ipp8u prfOutput_2[CP_ML_KEM_ETA_MAX * 64];
67 Ipp8u prfOutput_3[CP_ML_KEM_ETA_MAX * 64];
68
69 /* SHAKE256 multi-buffer processing */
70 Ipp8u state_buffer_mb4[STATE_x4_SIZE];
71 cpSHA3_SHAKE256Ctx_mb4 state_mb4;
72 state_mb4.ctx = state_buffer_mb4;
73
74 cp_SHA3_SHAKE256_InitMB4(&state_mb4);
75 cp_SHA3_SHAKE256_AbsorbMB4(&state_mb4,
76 inRand_N_0,
77 inRand_N_1,
78 inRand_N_2,
79 inRand_N_3,
80 CP_RAND_DATA_BYTES + 1);
81 cp_SHA3_SHAKE256_FinalizeMB4(&state_mb4);
82 cp_SHA3_SHAKE256_SqueezeMB4(prfOutput_0,
83 prfOutput_1,
84 prfOutput_2,
85 prfOutput_3,
86 64 * eta,
87 &state_mb4);
88
89 /* Final processing of the required amount of the buffers */
90 switch (numBuffers) {
91 case 4: {
92 sts = cp_samplePolyCBD(&pOutPoly[3], prfOutput_3, eta);
93 IPP_BADARG_RET((sts != ippStsNoErr), sts);
94 (*N)++;
95 if (transformFlag == nttTransform) {
96 /* 18: y` <- cp_NTT(𝐲) */
97 cp_NTT(&pOutPoly[3]);
98 }
99 IPPCP_FALLTHROUGH;
100 }
101 case 3: {
102 sts = cp_samplePolyCBD(&pOutPoly[2], prfOutput_2, eta);
103 IPP_BADARG_RET((sts != ippStsNoErr), sts);
104 (*N)++;
105 if (transformFlag == nttTransform) {
106 /* 18: y` <- cp_NTT(𝐲) */
107 cp_NTT(&pOutPoly[2]);
108 }
109 IPPCP_FALLTHROUGH;
110 }
111 case 2: {
112 sts = cp_samplePolyCBD(&pOutPoly[1], prfOutput_1, eta);
113 IPP_BADARG_RET((sts != ippStsNoErr), sts);
114 (*N)++;
115 if (transformFlag == nttTransform) {
116 /* 18: y` <- cp_NTT(𝐲) */
117 cp_NTT(&pOutPoly[1]);
118 }
119 IPPCP_FALLTHROUGH;
120 }
121 case 1: {
122 sts = cp_samplePolyCBD(&pOutPoly[0], prfOutput_0, eta);
123 IPP_BADARG_RET((sts != ippStsNoErr), sts);
124 (*N)++;
125 if (transformFlag == nttTransform) {
126 /* 18: y` <- cp_NTT(𝐲) */
127 cp_NTT(&pOutPoly[0]);
128 }
129 break;
130 }
131 default: {
132 return ippStsBadArgErr;
133 }
134 }
135
136 return sts;
137 }
138 #endif /* #if (_IPP32E >= _IPP32E_K0) */
139
140 /*
141 * Common kernel generating a Ipp16sPoly polynomial.
142 *
143 * Input: inRand_N - buffer with r data, the last byte is used to store N.
144 * N - pointer to the value of N, will be incremented in-place.
145 * eta - parameter defining the distribution to sample from.
146 * mlkemCtx - pointer to the ML KEM context.
147 * transformFlag - flag to indicate whether to apply NTT transform to each generated polynomial.
148 * Output: pOutPoly - the generated polynomial.
149 *
150 */
151 /* clang-format off */
152 IPPCP_INLINE IppStatus cp_polyGenInternal(Ipp16sPoly* pOutPoly,
153 Ipp8u inRand_N[CP_RAND_DATA_BYTES + 1],
154 Ipp8u* N,
155 const Ipp8u eta,
156 IppsMLKEMState* mlkemCtx,
157 nttTransformFlag transformFlag)
158 /* clang-format on */
159 {
160 IppStatus sts = ippStsNoErr;
161 _cpMLKEMStorage* pStorage = &mlkemCtx->storage;
162
163 Ipp8u* prfOutput = cp_mlStorageAllocate(pStorage, eta * 64 + CP_ML_KEM_ALIGNMENT);
164 CP_CHECK_FREE_RET(prfOutput == NULL, ippStsMemAllocErr, pStorage);
165 prfOutput = IPP_ALIGNED_PTR(prfOutput, CP_ML_KEM_ALIGNMENT);
166
167 inRand_N[32] = *N;
168 #if (_IPP32E >= _IPP32E_K0)
169 cp_SHA3_SHAKE256_HashMessage(prfOutput, 64 * eta, inRand_N, 33);
170 #else
171 sts = ippsHashMessage_rmf(inRand_N, 33, prfOutput, ippsHashMethod_SHAKE256(8 * 64 * eta));
172 IPP_BADARG_RET((sts != ippStsNoErr), sts);
173 #endif /* #if (_IPP32E >= _IPP32E_K0) */
174
175 sts = cp_samplePolyCBD(pOutPoly, prfOutput, eta);
176 IPP_BADARG_RET((sts != ippStsNoErr), sts);
177 (*N)++;
178
179 if (transformFlag == nttTransform) {
180 /* 18: y` <- cp_NTT(𝐲) */
181 cp_NTT(pOutPoly);
182 }
183 sts = cp_mlStorageRelease(pStorage, // Ipp8u prfOutput[eta * 64]
184 eta * 64 + CP_ML_KEM_ALIGNMENT);
185
186 return sts;
187 }
188
189 /*
190 * Generates a vector of k Ipp16sPoly polynomials.
191 *
192 * Input: inRand_N - buffer with randomness data, the last byte is used to store N.
193 * N - pointer to the value of N, will be incremented in-place.
194 * eta - parameter defining the distribution to sample from.
195 * mlkemCtx - pointer to the ML KEM context.
196 * transformFlag - flag to indicate whether to apply NTT transform to each generated polynomial.
197 * Output: pOutPolyVec - the vector of generated polynomials.
198 *
199 */
200 /* clang-format off */
201 IPP_OWN_DEFN(IppStatus, cp_polyVecGen, (Ipp16sPoly* pOutPolyVec,
202 Ipp8u inRand_N[CP_RAND_DATA_BYTES + 1],
203 Ipp8u* N,
204 const Ipp8u eta,
205 IppsMLKEMState* mlkemCtx,
206 nttTransformFlag transformFlag))
207 /* clang-format on */
208 {
209 IppStatus sts = ippStsNoErr;
210
211 #if (_IPP32E >= _IPP32E_K0)
212 sts = cp_polyGenInternal_MB4(pOutPolyVec, inRand_N, N, eta, transformFlag, mlkemCtx->params.k);
213 #else
214 for (Ipp8u i = 0; i < mlkemCtx->params.k && sts == ippStsNoErr; i++) {
215 sts = cp_polyGenInternal(&pOutPolyVec[i], inRand_N, N, eta, mlkemCtx, transformFlag);
216 }
217 #endif /* #if (_IPP32E >= _IPP32E_K0) */
218
219
220 return sts;
221 }
222
223 /*
224 * Generates a Ipp16sPoly polynomial.
225 *
226 * Input: inRand_N - buffer with randomness data, the last byte is used to store N.
227 * N - pointer to the value of N, will be incremented in-place.
228 * eta - parameter defining the distribution to sample from.
229 * mlkemCtx - pointer to the ML KEM context.
230 * transformFlag - flag to indicate whether to apply NTT transform to each generated polynomial.
231 * Output: pOutPolyVec - the vector of generated polynomials.
232 *
233 */
234 /* clang-format off */
235 IPP_OWN_DEFN(IppStatus, cp_polyGen, (Ipp16sPoly* pOutPolyVec,
236 Ipp8u inRand_N[CP_RAND_DATA_BYTES + 1],
237 Ipp8u* N,
238 const Ipp8u eta,
239 IppsMLKEMState* mlkemCtx,
240 nttTransformFlag transformFlag))
241 /* clang-format on */
242 {
243 return cp_polyGenInternal(pOutPolyVec, inRand_N, N, eta, mlkemCtx, transformFlag);
244 }