Skip to content

Commit e1995a3

Browse files
committed
ML-KEM: consolidate poly_tomsg
This change now also applies the fix against Kyberslash issue to ARMv8. Signed-off-by: Stephan Mueller <smueller@chronox.de>
1 parent a0375d7 commit e1995a3

File tree

3 files changed

+60
-48
lines changed

3 files changed

+60
-48
lines changed

ml-kem/src/armv8/kyber_poly_armv8.h

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -216,28 +216,7 @@ static inline void poly_frommsg(poly *r,
216216
}
217217
}
218218

219-
/**
220-
* @brief poly_tomsg - Convert polynomial to 32-byte message
221-
*
222-
* @param [out] msg pointer to output message
223-
* @param [in] a pointer to input polynomial
224-
*/
225-
static inline void poly_tomsg(uint8_t msg[LC_KYBER_INDCPA_MSGBYTES],
226-
const poly *a)
227-
{
228-
unsigned int i, j;
229-
uint16_t t;
230-
231-
for (i = 0; i < LC_KYBER_N / 8; i++) {
232-
msg[i] = 0;
233-
for (j = 0; j < 8; j++) {
234-
t = (uint16_t)a->coeffs[8 * i + j];
235-
t += ((int16_t)t >> 15) & LC_KYBER_Q;
236-
t = (((t << 1) + LC_KYBER_Q / 2) / LC_KYBER_Q) & 1;
237-
msg[i] |= (uint8_t)(t << j);
238-
}
239-
}
240-
}
219+
#include "common/kyber_poly_tomsg.h"
241220

242221
#define POLY_GETNOISE_ETA1_BUFSIZE (LC_KYBER_ETA1 * LC_KYBER_N / 4)
243222
static inline void
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (C) 2025, Stephan Mueller <smueller@chronox.de>
3+
*
4+
* License: see LICENSE file in root directory
5+
*
6+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9+
* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12+
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13+
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16+
* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17+
* DAMAGE.
18+
*/
19+
20+
#ifndef KYBER_POLY_TOMSG_H
21+
#define KYBER_POLY_TOMSG_H
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
/**
28+
* @brief poly_tomsg - Convert polynomial to 32-byte message
29+
*
30+
* @param [out] msg pointer to output message
31+
* @param [in] a pointer to input polynomial
32+
*/
33+
static inline void poly_tomsg(uint8_t msg[LC_KYBER_INDCPA_MSGBYTES],
34+
const poly *a)
35+
{
36+
unsigned int i, j;
37+
uint32_t t;
38+
39+
for (i = 0; i < LC_KYBER_N / 8; i++) {
40+
msg[i] = 0;
41+
for (j = 0; j < 8; j++) {
42+
t = (uint32_t)a->coeffs[8 * i + j];
43+
44+
t <<= 1;
45+
t += LC_KYBER_Q - (LC_KYBER_Q / 2);
46+
t *= 80635;
47+
t >>= 28;
48+
t &= 1;
49+
msg[i] |= (uint8_t)(t << j);
50+
}
51+
}
52+
}
53+
54+
#ifdef __cplusplus
55+
}
56+
#endif
57+
58+
#endif /* KYBER_POLY_TOMSG_H */

ml-kem/src/kyber_poly.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -209,32 +209,7 @@ static inline void poly_frommsg(poly *r,
209209
}
210210
}
211211

212-
/**
213-
* @brief poly_tomsg - Convert polynomial to 32-byte message
214-
*
215-
* @param [out] msg pointer to output message
216-
* @param [in] a pointer to input polynomial
217-
*/
218-
static inline void poly_tomsg(uint8_t msg[LC_KYBER_INDCPA_MSGBYTES],
219-
const poly *a)
220-
{
221-
unsigned int i, j;
222-
uint32_t t;
223-
224-
for (i = 0; i < LC_KYBER_N / 8; i++) {
225-
msg[i] = 0;
226-
for (j = 0; j < 8; j++) {
227-
t = (uint32_t)a->coeffs[8 * i + j];
228-
229-
t <<= 1;
230-
t += LC_KYBER_Q - (LC_KYBER_Q / 2);
231-
t *= 80635;
232-
t >>= 28;
233-
t &= 1;
234-
msg[i] |= (uint8_t)(t << j);
235-
}
236-
}
237-
}
212+
#include "common/kyber_poly_tomsg.h"
238213

239214
/**
240215
* @brief poly_getnoise_eta1 - Sample a polynomial deterministically from a seed

0 commit comments

Comments
 (0)