Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
cx_mlkem_polyvec.c
Go to the documentation of this file.
1/*****************************************************************************
2 * (c) 2026 Ledger SAS.
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 *****************************************************************************/
25#include "lcx_mlkem.h"
26#include "cx_mlkem_polyvec.h"
27
28/* ========================================================================
29 * Polyvec operations (parameterized by k)
30 * ====================================================================== */
31
32void MLKEM_POLYVEC_tobytes(uint8_t *r, const polyvec *a, uint8_t k)
33{
34 for (uint32_t i = 0U; i < k; i++) {
35 MLKEM_POLY_tobytes(&r[i * MLKEM_POLYBYTES], &a->vec[i]);
36 }
37}
38
39void MLKEM_POLYVEC_frombytes(polyvec *r, const uint8_t *a, uint8_t k)
40{
41 for (uint32_t i = 0U; i < k; i++) {
43 }
44}
45
46void MLKEM_POLYVEC_ntt(polyvec *r, uint8_t k)
47{
48 for (uint32_t i = 0U; i < k; i++) {
49 MLKEM_POLY_ntt(&r->vec[i]);
50 }
51}
52
54{
55 for (uint32_t i = 0U; i < k; i++) {
57 }
58}
59
60void MLKEM_POLYVEC_reduce(polyvec *r, uint8_t k)
61{
62 for (uint32_t i = 0U; i < k; i++) {
63 MLKEM_POLY_reduce(&r->vec[i]);
64 }
65}
66
67void MLKEM_POLYVEC_add(polyvec *r, const polyvec *b, uint8_t k)
68{
69 for (uint32_t i = 0U; i < k; i++) {
70 MLKEM_POLY_add(&r->vec[i], &b->vec[i]);
71 }
72}
73
74void MLKEM_POLYVEC_tomont(polyvec *r, uint8_t k)
75{
76 for (uint32_t i = 0U; i < k; i++) {
77 MLKEM_POLY_tomont(&r->vec[i]);
78 }
79}
80
81void MLKEM_POLYVEC_compress(uint8_t *r, const polyvec *a, uint8_t k, uint8_t du)
82{
83 if (du == 10) {
84 for (uint32_t i = 0U; i < k; i++) {
86 }
87 }
88 else { // du == 11
89 for (uint32_t i = 0U; i < k; i++) {
91 }
92 }
93}
94
95void MLKEM_POLYVEC_decompress(polyvec *r, const uint8_t *a, uint8_t k, uint8_t du)
96{
97 if (du == 10) {
98 for (uint32_t i = 0U; i < k; i++) {
100 }
101 }
102 else { // du == 11
103 for (uint32_t i = 0U; i < k; i++) {
105 }
106 }
107}
108
109void MLKEM_POLYVEC_basemul_acc_montgomery(poly *r, const polyvec *a, const polyvec *b, uint8_t k)
110{
111 for (uint32_t i = 0U; i < k; i++) {
112 MLKEM_POLY_basemul_acc_montgomery(r, &a->vec[i], &b->vec[i], (i == 0U) ? 1 : 0);
113 }
114}
void MLKEM_POLY_basemul_acc_montgomery(poly *r, const poly *a, const poly *b, int32_t first)
Pointwise multiplication of two polynomials in NTT domain with accumulation.
void MLKEM_POLY_tomont(poly *r)
Converts a polynomial to Montgomery domain.
void MLKEM_POLY_reduce(poly *r)
Applies Barrett reduction to all coefficients and map to [0, q-1].
void MLKEM_POLY_ntt(poly *p)
Computes the NTT of a polynomial in place.
void MLKEM_POLY_tobytes(uint8_t *r, const poly *a)
Serializes a polynomial to bytes (12 bits per coefficient).
void MLKEM_POLY_compress_d11(uint8_t *r, const poly *a)
Compresses a polynomial with du=11 and serialize to bytes.
void MLKEM_POLY_invntt_tomont(poly *p)
Computes the inverse NTT and multiply by Montgomery factor.
void MLKEM_POLY_decompress_d11(poly *r, const uint8_t *a)
Decompresses a polynomial from bytes with du=11.
void MLKEM_POLY_decompress_d10(poly *r, const uint8_t *a)
Decompresses a polynomial from bytes with du=10.
void MLKEM_POLY_frombytes(poly *r, const uint8_t *a)
Deserializes a polynomial from bytes (12 bits per coefficient).
void MLKEM_POLY_add(poly *r, const poly *b)
Adds polynomial b to polynomial r in place.
void MLKEM_POLY_compress_d10(uint8_t *r, const poly *a)
Compresses a polynomial with du=10 and serialize to bytes.
void MLKEM_POLYVEC_decompress(polyvec *r, const uint8_t *a, uint8_t k, uint8_t du)
Decompresses and deserializes a polynomial vector.
void MLKEM_POLYVEC_invntt_tomont(polyvec *r, uint8_t k)
Applies the inverse NTT to each polynomial in a vector.
void MLKEM_POLYVEC_reduce(polyvec *r, uint8_t k)
Reduces all coefficients of each polynomial in a vector.
void MLKEM_POLYVEC_compress(uint8_t *r, const polyvec *a, uint8_t k, uint8_t du)
Compresses and serializes a polynomial vector.
void MLKEM_POLYVEC_tomont(polyvec *r, uint8_t k)
Converts each polynomial in a vector to Montgomery domain.
void MLKEM_POLYVEC_frombytes(polyvec *r, const uint8_t *a, uint8_t k)
Deserializes a polynomial vector from bytes.
void MLKEM_POLYVEC_tobytes(uint8_t *r, const polyvec *a, uint8_t k)
Serializes a polynomial vector to bytes.
void MLKEM_POLYVEC_add(polyvec *r, const polyvec *b, uint8_t k)
Adds polynomial vector b to r in place.
void MLKEM_POLYVEC_basemul_acc_montgomery(poly *r, const polyvec *a, const polyvec *b, uint8_t k)
Accumulated pointwise multiplication of two polynomial vectors in the NTT domain.
void MLKEM_POLYVEC_ntt(polyvec *r, uint8_t k)
Applies the NTT to each polynomial in a vector.
#define MLKEM_POLYBYTES
Definition lcx_mlkem.h:40
#define MLKEM_POLYCOMPRESSEDBYTES_D11
Definition lcx_mlkem.h:43
#define MLKEM_POLYCOMPRESSEDBYTES_D10
Definition lcx_mlkem.h:42
ML-KEM (Module-Lattice Key Encapsulation Mechanism) public API.
Polynomial with MLKEM_N coefficients.
Vector of polynomials with up to MLKEM_MAX_K elements.
poly vec[MLKEM_MAX_K]