Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
cx_mldsa_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 *****************************************************************************/
21#include "cx_mldsa_polyvec.h"
22#include "cx_mldsa_rounding.h"
23#include "cx_mldsa_packing.h"
24
26{
27 for (uint8_t i = 0U; i < l; i++) {
28 MLDSA_POLY_ntt(&v->vec[i]);
29 }
30}
31
33{
34 for (uint8_t i = 0U; i < k; i++) {
35 MLDSA_POLY_ntt(&v->vec[i]);
36 }
37}
38
40{
41 for (uint8_t i = 0U; i < k; i++) {
43 }
44}
45
47 const mldsa_polyvecl *u,
48 const mldsa_polyvecl *v,
49 uint8_t l)
50{
51 MLDSA_POLY_pointwise_montgomery(w, &u->vec[0], &v->vec[0], 1);
52 for (uint8_t i = 1U; i < l; i++) {
53 MLDSA_POLY_pointwise_montgomery(w, &u->vec[i], &v->vec[i], 0);
54 }
55}
56
58{
59 for (uint8_t i = 0U; i < k; i++) {
60 MLDSA_POLY_add(&a->vec[i], &b->vec[i]);
61 }
62}
63
65{
66 for (uint8_t i = 0U; i < k; i++) {
67 MLDSA_POLY_sub(&a->vec[i], &b->vec[i]);
68 }
69}
70
72{
73 for (uint8_t i = 0U; i < k; i++) {
74 MLDSA_POLY_reduce(&v->vec[i]);
75 }
76}
77
79{
80 for (uint8_t i = 0U; i < k; i++) {
82 }
83}
84
85int MLDSA_POLYVEC_chknorm_l(const mldsa_polyvecl *v, int32_t B, uint8_t l)
86{
87 for (uint8_t i = 0U; i < l; i++) {
88 if (MLDSA_POLY_chknorm(&v->vec[i], B)) {
89 return 1;
90 }
91 }
92 return 0;
93}
94
95int MLDSA_POLYVEC_chknorm_k(const mldsa_polyveck *v, int32_t B, uint8_t k)
96{
97 for (uint8_t i = 0U; i < k; i++) {
98 if (MLDSA_POLY_chknorm(&v->vec[i], B)) {
99 return 1;
100 }
101 }
102 return 0;
103}
104
106{
107 for (uint8_t i = 0U; i < k; i++) {
108 MLDSA_POLY_shiftl(&v->vec[i]);
109 }
110}
111
113 mldsa_polyveck *v0,
114 const mldsa_polyveck *v,
115 uint8_t k)
116{
117 for (uint8_t i = 0U; i < k; i++) {
118 MLDSA_ROUNDING_poly_power2round(&v1->vec[i], &v0->vec[i], &v->vec[i]);
119 }
120}
121
123 mldsa_polyveck *v0,
124 const mldsa_polyveck *v,
125 int32_t gamma2,
126 uint8_t k)
127{
128 for (uint8_t i = 0U; i < k; i++) {
129 MLDSA_ROUNDING_poly_decompose(&v1->vec[i], &v0->vec[i], &v->vec[i], gamma2);
130 }
131}
132
134 const mldsa_polyveck *v0,
135 const mldsa_polyveck *v1,
136 int32_t gamma2,
137 uint8_t k)
138{
139 uint32_t s = 0U;
140 for (uint8_t i = 0U; i < k; i++) {
141 for (uint32_t j = 0U; j < MLDSA_N; j++) {
142 h->vec[i].coeffs[j] = (int32_t) MLDSA_ROUNDING_make_hint(
143 v0->vec[i].coeffs[j], v1->vec[i].coeffs[j], gamma2);
144 s += (uint32_t) h->vec[i].coeffs[j];
145 }
146 }
147 return s;
148}
149
151 const mldsa_polyveck *u,
152 const mldsa_polyveck *h,
153 int32_t gamma2,
154 uint8_t k)
155{
156 for (uint8_t i = 0U; i < k; i++) {
157 MLDSA_ROUNDING_poly_use_hint(&w->vec[i], &u->vec[i], &h->vec[i], gamma2);
158 }
159}
160
161void MLDSA_POLYVEC_pack_w1(uint8_t *r, const mldsa_polyveck *w1, int32_t gamma2, uint8_t k)
162{
163 uint32_t poly_bytes;
164 if (gamma2 == (MLDSA_Q - 1) / 88) {
165 poly_bytes = 192U;
166 }
167 else {
168 poly_bytes = 128U;
169 }
170 for (uint8_t i = 0U; i < k; i++) {
171 MLDSA_PACK_polyw1(r + i * poly_bytes, &w1->vec[i], gamma2);
172 }
173}
uint32_t MLDSA_PACK_polyw1(uint8_t *r, const mldsa_poly *a, int32_t gamma2)
Bit-pack polynomial w1 with coefficients fitting in ceil(log2((q-1)/(2*gamma2))) bits.
void MLDSA_POLY_reduce(mldsa_poly *a)
Applies reduce32 to all coefficients of a polynomial.
int MLDSA_POLY_chknorm(const mldsa_poly *a, int32_t B)
Checks infinity norm of polynomial against bound B.
void MLDSA_POLY_ntt(mldsa_poly *a)
Forward NTT in place.
void MLDSA_POLY_sub(mldsa_poly *a, const mldsa_poly *b)
Subtracts polynomial b from polynomial a in place.
void MLDSA_POLY_shiftl(mldsa_poly *a)
Shifts all coefficients left by D bits.
void MLDSA_POLY_caddq_all(mldsa_poly *a)
Applies caddq to all coefficients of a polynomial.
void MLDSA_POLY_add(mldsa_poly *a, const mldsa_poly *b)
Adds polynomial b to polynomial a in place.
void MLDSA_POLY_invntt_tomont(mldsa_poly *a)
Inverse NTT and multiply by Montgomery factor.
void MLDSA_POLY_pointwise_montgomery(mldsa_poly *c, const mldsa_poly *a, const mldsa_poly *b, int first)
Pointwise multiplication (Montgomery) with accumulation.
void MLDSA_POLYVEC_ntt_k(mldsa_polyveck *v, uint8_t k)
Apply NTT to all polynomials in a K-vector.
void MLDSA_POLYVEC_add_k(mldsa_polyveck *a, const mldsa_polyveck *b, uint8_t k)
Add K-vector b to K-vector a.
void MLDSA_POLYVEC_pack_w1(uint8_t *r, const mldsa_polyveck *w1, int32_t gamma2, uint8_t k)
Pack w1 vector.
uint32_t MLDSA_POLYVEC_make_hint_k(mldsa_polyveck *h, const mldsa_polyveck *v0, const mldsa_polyveck *v1, int32_t gamma2, uint8_t k)
Make hint for K-vectors.
void MLDSA_POLYVEC_use_hint_k(mldsa_polyveck *w, const mldsa_polyveck *u, const mldsa_polyveck *h, int32_t gamma2, uint8_t k)
Use hint to correct high bits of K-vector.
void MLDSA_POLYVEC_pointwise_acc_montgomery(mldsa_poly *w, const mldsa_polyvecl *u, const mldsa_polyvecl *v, uint8_t l)
Inner product of L-vectors in NTT domain with accumulation.
void MLDSA_POLYVEC_decompose_k(mldsa_polyveck *v1, mldsa_polyveck *v0, const mldsa_polyveck *v, int32_t gamma2, uint8_t k)
Decompose all polynomials in K-vector.
void MLDSA_POLYVEC_power2round_k(mldsa_polyveck *v1, mldsa_polyveck *v0, const mldsa_polyveck *v, uint8_t k)
Power2round all polynomials in K-vector.
void MLDSA_POLYVEC_invntt_tomont_k(mldsa_polyveck *v, uint8_t k)
Apply inverse NTT to all polynomials in a K-vector.
int MLDSA_POLYVEC_chknorm_l(const mldsa_polyvecl *v, int32_t B, uint8_t l)
Check infinity norm of an L-vector.
void MLDSA_POLYVEC_sub_k(mldsa_polyveck *a, const mldsa_polyveck *b, uint8_t k)
Subtract K-vector b from K-vector a.
int MLDSA_POLYVEC_chknorm_k(const mldsa_polyveck *v, int32_t B, uint8_t k)
Check infinity norm of a K-vector.
void MLDSA_POLYVEC_shiftl_k(mldsa_polyveck *v, uint8_t k)
Shift left all polynomials in K-vector by D bits.
void MLDSA_POLYVEC_reduce_k(mldsa_polyveck *v, uint8_t k)
Apply reduce to all polynomials in a K-vector.
void MLDSA_POLYVEC_ntt_l(mldsa_polyvecl *v, uint8_t l)
Apply NTT to all polynomials in an L-vector.
void MLDSA_POLYVEC_caddq_k(mldsa_polyveck *v, uint8_t k)
Apply caddq to all polynomials in a K-vector.
void MLDSA_ROUNDING_poly_use_hint(mldsa_poly *b, const mldsa_poly *a, const mldsa_poly *h, int32_t gamma2)
Applies use_hint to all coefficients of a polynomial.
void MLDSA_ROUNDING_poly_decompose(mldsa_poly *a1, mldsa_poly *a0, const mldsa_poly *a, int32_t gamma2)
Applies decompose to all coefficients of a polynomial.
uint32_t MLDSA_ROUNDING_make_hint(int32_t a0, int32_t a1, int32_t gamma2)
Compute hint bit. Returns 1 if adding ct0 to w - ct0 would change the high bits (i....
void MLDSA_ROUNDING_poly_power2round(mldsa_poly *a1, mldsa_poly *a0, const mldsa_poly *a)
Applies power2round to all coefficients of a polynomial.
#define MLDSA_N
Definition lcx_mldsa.h:37
#define MLDSA_Q
Definition lcx_mldsa.h:38
Polynomial with MLDSA_N int32_t coefficients.
int32_t coeffs[MLDSA_N]
Polynomial vector of up to MLDSA_MAX_K polynomials.
mldsa_poly vec[MLDSA_MAX_K]
Polynomial vector of up to MLDSA_MAX_L polynomials.
mldsa_poly vec[MLDSA_MAX_L]