| 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 |
* Stuff 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 |
|
| 26 |
#define CP_D_MAX (12) |
| 27 |
#define CP_D_MIN (1) |
| 28 |
|
| 29 |
/* |
| 30 |
* Perform division of x by divisor with the rounding of x to the nearest integer |
| 31 |
*/ |
| 32 |
IPPCP_INLINE Ipp16s cp_divAndRoundToNearestInt(Ipp32s x, Ipp32s divisor) |
| 33 |
{ |
| 34 |
return (Ipp16s)((x + (divisor >> 1)) / divisor); |
| 35 |
} |
| 36 |
|
| 37 |
//-------------------------------// |
| 38 |
// Internal functions |
| 39 |
//-------------------------------// |
| 40 |
|
| 41 |
/* |
| 42 |
* Algorithm 3: Converts a bit array (of a length that is a multiple of eight) into an array of bytes. |
| 43 |
* |
| 44 |
* Input: bit array {0, 1}^{8*l} |
| 45 |
* Output: byte array B^l |
| 46 |
* |
| 47 |
* Note: works inplace (pInp == pOut), buffer's length has to be numElmBitArr bytes |
| 48 |
*/ |
| 49 |
IPPCP_INLINE void cp_bitsToBytes(const Ipp8u* pInp, Ipp8u* pOut, const Ipp32u numElmBitArr) |
| 50 |
{ |
| 51 |
IPPCP_GCC_IGNORE_PUSH("-Wmaybe-uninitialized") |
| 52 |
Ipp32u numElmByteArr = BITS2WORD8_SIZE(numElmBitArr); |
| 53 |
for (Ipp32u i = 0; i < numElmByteArr; i++) { |
| 54 |
Ipp8u B = 0; |
| 55 |
for (Ipp32u j = 0; j < 8; j++) { |
| 56 |
B = B + (Ipp8u)(pInp[8 * i + j] << j); |
| 57 |
} |
| 58 |
pOut[i] = B; |
| 59 |
} |
| 60 |
IPPCP_GCC_IGNORE_POP |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
* Algorithm 4: Performs the inverse of cp_bitsToBytes, converting a byte array into a bit array. |
| 65 |
* |
| 66 |
* Input: byte array B^l |
| 67 |
* Output: bit array {0, 1}^{8*l} |
| 68 |
* |
| 69 |
* Note: works inplace (pInp == pOut), buffer's length has to be 8*numElmByteArr bytes |
| 70 |
*/ |
| 71 |
/* clang-format on */ |
| 72 |
IPPCP_INLINE void cp_bytesToBits(const Ipp8u* pInp, |
| 73 |
Ipp8u* pOut, |
| 74 |
const Ipp32u numElmByteArr, |
| 75 |
const Ipp32u outByteSize) |
| 76 |
/* clang-format on */ |
| 77 |
{ |
| 78 |
for (Ipp32u i = 0; i < numElmByteArr; i++) { |
| 79 |
Ipp8u C = pInp[i]; |
| 80 |
for (Ipp32u j = 0; j < 8; j++) { |
| 81 |
Ipp32u position = (Ipp32u)(IPP_MIN((8 * i + j), outByteSize)); |
| 82 |
pOut[position] = C & 1; |
| 83 |
C >>= 1; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
* Formula 4.7: Compressing primitive |
| 90 |
* |
| 91 |
* Input: in - number in Z_{q}, q = 3329 |
| 92 |
* d - decompression base {1, 2, ..., 11} |
| 93 |
* Output: out - number in Z_{2^{d}} |
| 94 |
* |
| 95 |
* Z_{q} -> Z_{2^{d}}: x -> RoundToNearestInt((2^{d} / q) * x) mod 2^{d} |
| 96 |
*/ |
| 97 |
IPP_OWN_DEFN(IppStatus, cp_Compress, (Ipp16u * out, const Ipp16s in, const Ipp16u d)) |
| 98 |
{ |
| 99 |
IPP_BADARG_RET(((d < CP_D_MIN) || (d >= CP_D_MAX)), ippStsOutOfRangeErr); |
| 100 |
|
| 101 |
/* transform numbers from the Barrett reduced form to positive representation */ |
| 102 |
Ipp16s u = in; |
| 103 |
u += (u >> 15) & CP_ML_KEM_Q; |
| 104 |
|
| 105 |
const Ipp32s power = (Ipp32s)1 << d; // 2^{d} |
| 106 |
*out = (Ipp16u)cp_divAndRoundToNearestInt(power * u, CP_ML_KEM_Q) % power; |
| 107 |
|
| 108 |
return ippStsNoErr; |
| 109 |
} |
| 110 |
|
| 111 |
/* |
| 112 |
* Formula 4.8: Decompressing primitive |
| 113 |
* |
| 114 |
* Input: in - number in Z_{2^{d}} |
| 115 |
* d - decompression base {1, 2, ..., 11} |
| 116 |
* Output: out - number in Z_{q}, q = 3329 |
| 117 |
* |
| 118 |
* Z_{2^{d}} -> Z_{q}: y -> RoundToNearestInt((q / 2^{d}) * y) |
| 119 |
*/ |
| 120 |
|
| 121 |
IPP_OWN_DEFN(IppStatus, cp_Decompress, (Ipp16u * out, const Ipp16s in, const Ipp16u d)) |
| 122 |
{ |
| 123 |
IPP_BADARG_RET(((d < CP_D_MIN) || (d >= CP_D_MAX)), ippStsOutOfRangeErr); |
| 124 |
|
| 125 |
/* transform numbers from the Barrett reduced form to positive representation */ |
| 126 |
Ipp16s u = in; |
| 127 |
u += (u >> 15) & CP_ML_KEM_Q; |
| 128 |
|
| 129 |
const Ipp32s power = (Ipp32s)(1 << d); |
| 130 |
*out = (Ipp16u)cp_divAndRoundToNearestInt(CP_ML_KEM_Q * (Ipp16s)in, power); |
| 131 |
|
| 132 |
return ippStsNoErr; |
| 133 |
} |
| 134 |
|
| 135 |
/* |
| 136 |
* Algorithm 5: Encodes an array of d-bit integers into a byte array for 1 <= d <= 12. |
| 137 |
* |
| 138 |
* Input: pPolyF - integer array F in Z_{m}^{256}, where each m = 2^d if d < 12, otherwise m = q. |
| 139 |
* d - parameter specifying the number of bits. |
| 140 |
* Output: B - byte array B^{32*d}. |
| 141 |
* |
| 142 |
* Note: To reduce memory usage, the result is processed by chunk of size lcm(d, 8) |
| 143 |
which is suitable for any d(maximum chunk of size 88 is required for d = 11) |
| 144 |
*/ |
| 145 |
|
| 146 |
// Allow bigger buffer allocation for the latest platforms to speed up processing |
| 147 |
#if CP_ML_KEM_MEMORY_OPTIMIZED |
| 148 |
#define CP_B_BUFFERSIZE_MAX (88) |
| 149 |
#else |
| 150 |
#define CP_B_BUFFERSIZE_MAX (256 * CP_D_MAX) |
| 151 |
#endif /* #if CP_ML_KEM_MEMORY_OPTIMIZED */ |
| 152 |
|
| 153 |
IPP_OWN_DEFN(IppStatus, cp_byteEncode, (Ipp8u * B, const Ipp16u d, const Ipp16sPoly* pPolyF)) |
| 154 |
{ |
| 155 |
IPP_BADARG_RET(((d < CP_D_MIN) || (d > CP_D_MAX)), ippStsOutOfRangeErr); |
| 156 |
|
| 157 |
Ipp32u bits_accumulated = 0; |
| 158 |
Ipp8u b[CP_B_BUFFERSIZE_MAX]; |
| 159 |
|
| 160 |
/* Encode polynomial to byte array */ |
| 161 |
for (Ipp32u i = 0; i < 256; i++) { |
| 162 |
Ipp16u a = (Ipp16u)pPolyF->values[i]; |
| 163 |
|
| 164 |
for (Ipp32u j = 0; j < d; j++, bits_accumulated++) { |
| 165 |
/* Similar to the spec's logic: |
| 166 |
* b[bits_accumulated] = (a & 1); |
| 167 |
* a = (a - b[bits_accumulated]) >> 1; |
| 168 |
* The original write-modify pattern was replaced with a direct bit extraction. |
| 169 |
*/ |
| 170 |
b[bits_accumulated] = (Ipp8u)((a >> j) & 1); |
| 171 |
} |
| 172 |
|
| 173 |
// Process the buffer b to reuse the memory reduced for old platforms |
| 174 |
#if CP_ML_KEM_MEMORY_OPTIMIZED |
| 175 |
/* Check if we filled the current chunk for cp_bitsToBytes processing */ |
| 176 |
if ((bits_accumulated & 7) == 0) { |
| 177 |
cp_bitsToBytes(b, B, bits_accumulated); |
| 178 |
B += BITS2WORD8_SIZE(bits_accumulated); |
| 179 |
bits_accumulated = 0; |
| 180 |
} |
| 181 |
#endif |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
/* Process the last chunk which may be 0 or not full(less than lcm(d, 8)) */ |
| 186 |
cp_bitsToBytes(b, B, bits_accumulated); |
| 187 |
|
| 188 |
return ippStsNoErr; |
| 189 |
} |
| 190 |
|
| 191 |
/* |
| 192 |
* Algorithm 6: Decodes a byte array into an array of d-bit integers for 1 <= d <= 12. |
| 193 |
* |
| 194 |
* Input: d - parameter specifying the number of bits. |
| 195 |
* B - byte array B^{32*d}. |
| 196 |
* bByteSize - the size of the input byte array B in bytes. |
| 197 |
* Output: pPolyF - integer array F in Z_{m}^{256}, where each m = 2^d if d < 12, otherwise m = q. |
| 198 |
* |
| 199 |
* Note: To reduce memory usage, the input byte array is processed by chunk of size d*8. |
| 200 |
*/ |
| 201 |
IPP_OWN_DEFN(IppStatus, |
| 202 |
cp_byteDecode, |
| 203 |
(Ipp16sPoly * pPolyF, const Ipp16u d, const Ipp8u* B, const int bByteSize)) |
| 204 |
{ |
| 205 |
IPP_BADARG_RET(((d < CP_D_MIN) || (d > CP_D_MAX)), ippStsOutOfRangeErr); |
| 206 |
IPP_BADARG_RET((bByteSize < 32 * d), ippStsOutOfRangeErr); |
| 207 |
|
| 208 |
Ipp8u b[CP_D_MAX * 8] = { 0 }; |
| 209 |
/* Decode byte array to polynomial */ |
| 210 |
for (Ipp32u i = 0; i < 256; i++) { |
| 211 |
if ((i & 7) == 0) { |
| 212 |
// Read the next d bytes from B and put 8*d elements in b |
| 213 |
cp_bytesToBits(B, b, d, CP_D_MAX * 8); |
| 214 |
B += d; |
| 215 |
} |
| 216 |
|
| 217 |
pPolyF->values[i] = 0; |
| 218 |
for (Ipp32u j = 0; j < d; j++) { |
| 219 |
pPolyF->values[i] += b[(i & 7) * d + j] << j; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return ippStsNoErr; |
| 224 |
} |
| 225 |
|
| 226 |
/* |
| 227 |
* Algorithm 8: Takes a seed as input and outputs a pseudorandom sample from the distribution D_{eta}(R_{q}). |
| 228 |
* |
| 229 |
* Input: pSeed - byte array B^{64*eta} |
| 230 |
* eta - the value of eta, can be 1, 2 or 3 |
| 231 |
* Output: pPoly - array Z_{q}^{256} with values sampled from the distribution D_{eta}(R_{q}). |
| 232 |
* |
| 233 |
*/ |
| 234 |
IPP_OWN_DEFN(IppStatus, cp_samplePolyCBD, (Ipp16sPoly * pPoly, const Ipp8u* pSeed, const Ipp8u eta)) |
| 235 |
{ |
| 236 |
/* Byte array of size 32*eta bytes */ |
| 237 |
Ipp8u seedBits[4 * 8 * CP_ML_KEM_ETA_MAX]; |
| 238 |
for (Ipp16u i = 0; i < 256; i++) { |
| 239 |
if ((i & 7) == 0) { |
| 240 |
// Read the next 2*eta bytes from pSeed and put 8*2*eta elements in seedBits |
| 241 |
cp_bytesToBits(pSeed, seedBits, 2 * eta, (4 * 8 * CP_ML_KEM_ETA_MAX)); |
| 242 |
pSeed += 2 * eta; |
| 243 |
} |
| 244 |
Ipp16s x = 0; |
| 245 |
for (Ipp8u j = 0; j < eta; j++) { |
| 246 |
x += seedBits[2 * (i & 7) * eta + j]; |
| 247 |
} |
| 248 |
Ipp16s y = 0; |
| 249 |
for (Ipp8u j = 0; j < eta; j++) { |
| 250 |
y += seedBits[2 * (i & 7) * eta + eta + j]; |
| 251 |
} |
| 252 |
// The result will be mapped to the canonical positive representation in the reduction step |
| 253 |
Ipp16s result = x - y; |
| 254 |
pPoly->values[i] = cp_mlkemBarrettReduce((Ipp32s)result); |
| 255 |
} |
| 256 |
|
| 257 |
return ippStsNoErr; |
| 258 |
} |