Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
cx_mlkem_poly.h
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 *****************************************************************************/
16#ifndef MLKEM_POLY_H
17#define MLKEM_POLY_H
18
19#include "lcx_mlkem.h"
20
24typedef struct {
25 int16_t coeffs[MLKEM_N];
26} poly;
27
35int16_t MLKEM_POLY_montgomery_reduce(int32_t a);
36
45int16_t MLKEM_POLY_fqmul(int16_t a, int16_t b);
46
54int16_t MLKEM_POLY_barrett_reduce(int16_t a);
55
63int16_t MLKEM_POLY_signed_to_unsigned_q(int16_t c);
64
70void MLKEM_POLY_reduce(poly *r);
71
77void MLKEM_POLY_tomont(poly *r);
78
85void MLKEM_POLY_add(poly *r, const poly *b);
86
93void MLKEM_POLY_sub(poly *r, const poly *b);
94
100void MLKEM_POLY_ntt(poly *p);
101
108
118void MLKEM_POLY_basemul_acc_montgomery(poly *r, const poly *a, const poly *b, int32_t first);
119
126void MLKEM_POLY_tobytes(uint8_t *r, const poly *a);
127
134void MLKEM_POLY_frombytes(poly *r, const uint8_t *a);
135
143uint8_t MLKEM_POLY_scalar_compress_d1(int16_t u);
144
152uint8_t MLKEM_POLY_scalar_compress_d4(int16_t u);
153
161int16_t MLKEM_POLY_scalar_decompress_d4(uint8_t u);
162
170uint8_t MLKEM_POLY_scalar_compress_d5(int16_t u);
171
179int16_t MLKEM_POLY_scalar_decompress_d5(uint8_t u);
180
188uint16_t MLKEM_POLY_scalar_compress_d10(int16_t u);
189
197int16_t MLKEM_POLY_scalar_decompress_d10(uint16_t u);
198
206uint16_t MLKEM_POLY_scalar_compress_d11(int16_t u);
207
215int16_t MLKEM_POLY_scalar_decompress_d11(uint16_t u);
216
223void MLKEM_POLY_compress_d4(uint8_t *r, const poly *a);
224
231void MLKEM_POLY_decompress_d4(poly *r, const uint8_t *a);
232
239void MLKEM_POLY_compress_d5(uint8_t *r, const poly *a);
240
247void MLKEM_POLY_decompress_d5(poly *r, const uint8_t *a);
248
255void MLKEM_POLY_compress_d10(uint8_t *r, const poly *a);
256
263void MLKEM_POLY_decompress_d10(poly *r, const uint8_t *a);
264
271void MLKEM_POLY_compress_d11(uint8_t *r, const poly *a);
272
279void MLKEM_POLY_decompress_d11(poly *r, const uint8_t *a);
280
287void MLKEM_POLY_frommsg(poly *r, const uint8_t *msg);
288
295void MLKEM_POLY_tomsg(uint8_t *msg, const poly *a);
296
297#endif // MLKEM_POLY_H
int16_t MLKEM_POLY_scalar_decompress_d10(uint16_t u)
Scalar decompression with d=10.
void MLKEM_POLY_sub(poly *r, const poly *b)
Subtracts polynomial b from polynomial r in place.
int16_t MLKEM_POLY_fqmul(int16_t a, int16_t b)
Multiplication in the NTT domain followed by Montgomery reduction.
int16_t MLKEM_POLY_scalar_decompress_d4(uint8_t u)
Scalar decompression with d=4.
void MLKEM_POLY_decompress_d5(poly *r, const uint8_t *a)
Decompresses a polynomial from bytes with dv=5.
void MLKEM_POLY_decompress_d4(poly *r, const uint8_t *a)
Decompresses a polynomial from bytes with dv=4.
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].
uint8_t MLKEM_POLY_scalar_compress_d4(int16_t u)
Scalar compression with d=4.
uint8_t MLKEM_POLY_scalar_compress_d5(int16_t u)
Scalar compression with d=5.
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).
int16_t MLKEM_POLY_signed_to_unsigned_q(int16_t c)
Maps a signed representative to an unsigned one in [0, q-1].
void MLKEM_POLY_compress_d5(uint8_t *r, const poly *a)
Compresses a polynomial with dv=5 and serialize to bytes.
uint8_t MLKEM_POLY_scalar_compress_d1(int16_t u)
Scalar compression with d=1.
int16_t MLKEM_POLY_barrett_reduce(int16_t a)
Barrett reduction of a coefficient modulo q.
void MLKEM_POLY_tomsg(uint8_t *msg, const poly *a)
Encodes a polynomial into a 32-byte message.
int16_t MLKEM_POLY_montgomery_reduce(int32_t a)
Montgomery reduction of a 32-bit integer.
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).
int16_t MLKEM_POLY_scalar_decompress_d5(uint8_t u)
Scalar decompression with d=5.
uint16_t MLKEM_POLY_scalar_compress_d11(int16_t u)
Scalar compression with d=11.
void MLKEM_POLY_add(poly *r, const poly *b)
Adds polynomial b to polynomial r in place.
uint16_t MLKEM_POLY_scalar_compress_d10(int16_t u)
Scalar compression with d=10.
void MLKEM_POLY_frommsg(poly *r, const uint8_t *msg)
Decodes a 32-byte message into a polynomial.
void MLKEM_POLY_compress_d4(uint8_t *r, const poly *a)
Compresses a polynomial with dv=4 and serialize to bytes.
int16_t MLKEM_POLY_scalar_decompress_d11(uint16_t u)
Scalar decompression with d=11.
void MLKEM_POLY_compress_d10(uint8_t *r, const poly *a)
Compresses a polynomial with du=10 and serialize to bytes.
#define MLKEM_N
Definition lcx_mlkem.h:35
ML-KEM (Module-Lattice Key Encapsulation Mechanism) public API.
Polynomial with MLKEM_N coefficients.