| 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 "stateless_pqc/ml_kem_internal/ml_kem.h" |
| 20 |
#include "stateless_pqc/zetas.h" |
| 21 |
|
| 22 |
/* |
| 23 |
* Algorithm 9: NTT(f) - Computes the NTT representation f` of the given polynomial f in R_{q}. |
| 24 |
* |
| 25 |
* Input/Output: f - polynomial Z_{q}^{256}, which is transformed to |
| 26 |
* its NTT representation f` in T_{q}. |
| 27 |
*/ |
| 28 |
IPP_OWN_DEFN(void, cp_NTT, (Ipp16sPoly * f)) |
| 29 |
{ |
| 30 |
Ipp16u i = 1; |
| 31 |
for (Ipp16u len = 128; len >= 2; len /= 2) { |
| 32 |
for (Ipp16u start = 0; start < 256; start += 2 * len) { |
| 33 |
Ipp16s zeta = cp_mlkem_zetas_ntt[i]; |
| 34 |
i++; |
| 35 |
for (Ipp16u j = start; j < start + len; j++) { |
| 36 |
// delay reduction, as the minimum possible value here will be in the function's range |
| 37 |
Ipp32s t = (Ipp32s)zeta * f->values[j + len]; |
| 38 |
f->values[j + len] = cp_mlkemBarrettReduce(((Ipp32s)f->values[j] - t)); |
| 39 |
f->values[j] = cp_mlkemBarrettReduce(((Ipp32s)f->values[j] + t)); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/* |
| 46 |
* Algorithm 10: NTT{-1}(f`) - Computes the polynomial f in R_{q} that corresponds to the |
| 47 |
* given NTT representation f` in T_{q}. |
| 48 |
* |
| 49 |
* Input/Output: f - polynomial in T_{q}, which is transformed to |
| 50 |
* the normal representation Z_{q}^{256}. |
| 51 |
*/ |
| 52 |
IPP_OWN_DEFN(void, cp_inverseNTT, (Ipp16sPoly * f)) |
| 53 |
{ |
| 54 |
Ipp8u i = 127; |
| 55 |
for (Ipp16u len = 2; len <= 128; len *= 2) { |
| 56 |
for (Ipp16u start = 0; start < 256; start += 2 * len) { |
| 57 |
Ipp16s zeta = cp_mlkem_zetas_ntt[i]; |
| 58 |
i--; |
| 59 |
for (Ipp16u j = start; j < start + len; j++) { |
| 60 |
Ipp16s t = f->values[j]; |
| 61 |
f->values[j] = cp_mlkemBarrettReduce((Ipp32s)(t + f->values[j + len])); |
| 62 |
// delay reduction, as the minimum possible value here will be in the function's range |
| 63 |
f->values[j + len] = f->values[j + len] - t; |
| 64 |
f->values[j + len] = cp_mlkemBarrettReduce((Ipp32s)zeta * f->values[j + len]); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
for (Ipp16u n = 0; n < 256; n++) { |
| 69 |
f->values[n] = cp_mlkemBarrettReduce((Ipp32s)f->values[n] * 3303); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/* |
| 74 |
* Algorithm 12: Computes the product of two degree-one polynomials with respect to |
| 75 |
* a quadratic modulus. |
| 76 |
* |
| 77 |
* Input: a0, a1 - coefficients of the first polynomial |
| 78 |
* b0, b1 - coefficients of the second polynomial |
| 79 |
* gamma - zeta^((2*BitReverse_7(i) + 1)) mod q |
| 80 |
* Output: c0_ptr, c1_ptr - pointers to the resulting coefficients |
| 81 |
*/ |
| 82 |
/* clang-format off */ |
| 83 |
IPPCP_INLINE void cp_baseCaseMultiply(Ipp16s a0, Ipp16s a1, |
| 84 |
Ipp16s b0, Ipp16s b1, |
| 85 |
Ipp16s gamma, |
| 86 |
Ipp16s* c0_ptr, Ipp16s* c1_ptr) |
| 87 |
/* clang-format on */ |
| 88 |
{ |
| 89 |
Ipp32s tmpC0 = cp_mlkemBarrettReduce((Ipp32s)a1 * b1); |
| 90 |
tmpC0 = cp_mlkemBarrettReduce((Ipp32s)gamma * tmpC0); |
| 91 |
*c0_ptr = cp_mlkemBarrettReduce(tmpC0 + (Ipp32s)a0 * b0); |
| 92 |
|
| 93 |
Ipp32s tmpC1 = cp_mlkemBarrettReduce((Ipp32s)a0 * b1); |
| 94 |
*c1_ptr = cp_mlkemBarrettReduce((Ipp32s)a1 * b0 + tmpC1); |
| 95 |
} |
| 96 |
|
| 97 |
/* |
| 98 |
* Algorithm 11: Computes the product (in the ring T_{q}) of two NTT representations. |
| 99 |
* |
| 100 |
* Input: f, g - polynomials Z_{q}^{256} in NTT representation |
| 101 |
* Output: h - polynomial Z_{q}^{256} in NTT representation |
| 102 |
*/ |
| 103 |
IPP_OWN_DEFN(void, cp_multiplyNTT, (const Ipp16sPoly* f, const Ipp16sPoly* g, Ipp16sPoly* h)) |
| 104 |
{ |
| 105 |
for (Ipp16u i = 0; i < 128; i++) { |
| 106 |
cp_baseCaseMultiply(f->values[2 * i], |
| 107 |
f->values[2 * i + 1], |
| 108 |
g->values[2 * i], |
| 109 |
g->values[2 * i + 1], |
| 110 |
cp_mlkem_zetas_multiply_ntt[i], |
| 111 |
&h->values[2 * i], |
| 112 |
&h->values[2 * i + 1]); |
| 113 |
} |
| 114 |
} |