-
Notifications
You must be signed in to change notification settings - Fork 12
/
api.h
255 lines (239 loc) · 10.5 KB
/
api.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright (c) 2024 The mlkem-native project authors
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Native arithmetic interface
*
* This header is primarily for documentation purposes.
* It should not be included by backend implementations.
*
* To ensure consistency with backends, the header will be
* included automatically after inclusion of the active
* backend, to ensure consistency of function signatures,
* and run sanity checks.
*/
#ifdef MLKEM_NATIVE_ARITH_NATIVE_API_H
#error \
"The arithmetic backend API `mlkem/native/api.h` " \
"should not be directly included. Please include the relevant " \
"structure headers directly."
#else /* MLKEM_NATIVE_ARITH_NATIVE_API_H */
#define MLKEM_NATIVE_ARITH_NATIVE_API_H
#include <stdint.h>
#include "poly.h"
#include "polyvec.h"
/*
* This is the C<->native interface allowing for the drop-in of
* native code for performance critical arithmetic components of ML-KEM.
*
* A _backend_ is a specific implementation of (part of) this interface.
*
* To add a function to a backend, define MLKEM_USE_NATIVE_XXX and
* implement `static inline xxx(...)` in the profile header.
*
* The only exception is MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER. This option can
* be set if there are native implementations for all of NTT, invNTT, and
* base multiplication, and allows the native implementation to use a
* custom order of polynomial coefficients in NTT domain -- the use of such
* custom order is not an implementation-detail since the public matrix
* is generated in NTT domain. In this case, a permutation function
* poly_permute_bitrev_to_custom() needs to be provided that permutes
* polynomials in NTT domain from bitreversed to the custom order.
*/
/*
* Those functions are meant to be trivial wrappers around the chosen native
* implementation. The are static inline to avoid unnecessary calls.
* The macro before each declaration controls whether a native
* implementation is present.
*/
#if defined(MLKEM_USE_NATIVE_NTT)
/*************************************************
* Name: ntt_native
*
* Description: Computes negacyclic number-theoretic transform (NTT) of
* a polynomial in place.
*
* The input polynomial is assumed to be in normal order.
* The output polynomial is in bitreversed order, or of a
* custom order if MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER is set.
* See the documentation of MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER
* for more information.
*
* Arguments: - poly *p: pointer to in/output polynomial
**************************************************/
static INLINE void ntt_native(poly *);
#endif /* MLKEM_USE_NATIVE_NTT */
#if defined(MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER)
/*
* This must only be set if NTT, invNTT, basemul, mulcache, and
* to/from byte stream conversions all have native implementations
* that are adapted to the custom order.
*/
#if !defined(MLKEM_USE_NATIVE_NTT) || !defined(MLKEM_USE_NATIVE_INTT) || \
!defined(MLKEM_USE_NATIVE_POLY_MULCACHE_COMPUTE) || \
!defined(MLKEM_USE_NATIVE_POLYVEC_BASEMUL_ACC_MONTGOMERY_CACHED) || \
!defined(MLKEM_USE_NATIVE_POLY_TOBYTES) || \
!defined(MLKEM_USE_NATIVE_POLY_FROMBYTES)
#error \
"Invalid native profile: MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER can only be \
set if there are native implementations for NTT, invNTT, mulcache, basemul, \
and to/from bytes conversions."
#endif
/*************************************************
* Name: poly_permute_bitrev_to_custom
*
* Description: When MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER is defined,
* convert a polynomial in NTT domain from bitreversed
* order to the custom order output by the native NTT.
*
* This must only be defined if there is native code for
* all of (a) NTT, (b) invNTT, (c) basemul, (d) mulcache.
* Arguments: - poly *p: pointer to in/output polynomial
*
**************************************************/
static INLINE void poly_permute_bitrev_to_custom(poly *);
#endif /* MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER */
#if defined(MLKEM_USE_NATIVE_INTT)
/*************************************************
* Name: intt_native
*
* Description: Computes inverse of negacyclic number-theoretic transform (NTT)
* of a polynomial in place.
*
* The input polynomial is in bitreversed order, or of a
* custom order if MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER is set.
* See the documentation of MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER
* for more information.
* The output polynomial is assumed to be in normal order.
*
* Arguments: - uint16_t *a: pointer to in/output polynomial
**************************************************/
static INLINE void intt_native(poly *);
#endif /* MLKEM_USE_NATIVE_INTT */
#if defined(MLKEM_USE_NATIVE_POLY_REDUCE)
/*************************************************
* Name: poly_reduce_native
*
* Description: Applies modular reduction to all coefficients of a polynomial.
*
* Arguments: - poly *r: pointer to input/output polynomial
**************************************************/
static INLINE void poly_reduce_native(poly *);
#endif /* MLKEM_USE_NATIVE_POLY_REDUCE */
#if defined(MLKEM_USE_NATIVE_POLY_TOMONT)
/*************************************************
* Name: poly_tomont_native
*
* Description: Inplace conversion of all coefficients of a polynomial
* from normal domain to Montgomery domain
*
* Arguments: - poly *r: pointer to input/output polynomial
**************************************************/
static INLINE void poly_tomont_native(poly *);
#endif /* MLKEM_USE_NATIVE_POLY_TOMONT */
#if defined(MLKEM_USE_NATIVE_POLY_MULCACHE_COMPUTE)
/*************************************************
* Name: poly_mulcache_compute_native
*
* Description: Compute multiplication cache for a polynomial
* in NTT domain.
*
* The purpose of the multiplication cache is to
* cache repeated computations required during a
* base multiplication of polynomials in NTT domain.
* The structure of the multiplication-cache is
* implementation defined.
*
* Arguments: INPUT:
* - poly: const pointer to input polynomial.
* This must be in NTT domain and inin bitreversed order, or of
* a custom order if MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER is set.
* See the documentation of MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER
* for more information.
* OUTPUT
* - cache: pointer to multiplication cache
**************************************************/
static INLINE void poly_mulcache_compute_native(poly_mulcache *cache,
const poly *poly);
#endif /* MLKEM_USE_NATIVE_POLY_MULCACHE_COMPUTE */
#if defined(MLKEM_USE_NATIVE_POLYVEC_BASEMUL_ACC_MONTGOMERY_CACHED)
/*************************************************
* Name: poly_mulcache_compute_native
*
* Description: Compute multiplication of polynomials in NTT domain.
*
* Arguments: INPUT:
* - a: First polynomial operand.
* This must be in NTT domain and inin bitreversed order, or of
* a custom order if MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER is set.
* See the documentation of MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER
* for more information.
* - b: Second polynomial operand.
* As for a.
* - b_cache: Multiplication-cache for b.
* OUTPUT
* - r: Result of the base multiplication. This is again
* in NTT domain, and of the same order as a and b.
**************************************************/
static INLINE void polyvec_basemul_acc_montgomery_cached_native(
poly *r, const polyvec *a, const polyvec *b,
const polyvec_mulcache *b_cache);
#endif
#if defined(MLKEM_USE_NATIVE_POLY_TOBYTES)
/*************************************************
* Name: poly_tobytes_native
*
* Description: Serialization of a polynomial.
* Signed coefficients are converted to
* unsigned form before serialization.
*
* Arguments: INPUT:
* - a: const pointer to input polynomial,
* with each coefficient in the range -Q+1 .. Q-1
* OUTPUT
* - r: pointer to output byte array
* (of MLKEM_POLYBYTES bytes)
**************************************************/
static INLINE void poly_tobytes_native(uint8_t r[MLKEM_POLYBYTES],
const poly *a);
#endif /* MLKEM_USE_NATIVE_POLY_TOBYTES */
#if defined(MLKEM_USE_NATIVE_POLY_FROMBYTES)
/*************************************************
* Name: poly_frombytes_native
*
* Description: Serialization of a polynomial.
* Signed coefficients are converted to
* unsigned form before serialization.
*
* Arguments: INPUT:
* - r: pointer to output polynomial in NTT domain
* OUTPUT
* - a: const pointer to input byte aray
* (of MLKEM_POLYBYTES bytes)
**************************************************/
static INLINE void poly_frombytes_native(poly *a,
const uint8_t r[MLKEM_POLYBYTES]);
#endif /* MLKEM_USE_NATIVE_POLY_FROMBYTES */
#if defined(MLKEM_USE_NATIVE_REJ_UNIFORM)
/*************************************************
* Name: rej_uniform_native
*
* Description: Run rejection sampling on uniform random bytes to generate
* uniform random integers mod q
*
* Arguments: - int16_t *r: pointer to output buffer
* - unsigned int len: requested number of 16-bit integers
* (uniform mod q).
* - const uint8_t *buf: pointer to input buffer
* (assumed to be uniform random bytes)
* - unsigned int buflen: length of input buffer in bytes.
*
* Return -1 if the native implementation does not support the input lengths.
* Otherwise, returns non-negative number of sampled 16-bit integers (at most
* len).
**************************************************/
static INLINE int rej_uniform_native(int16_t *r, unsigned int len,
const uint8_t *buf, unsigned int buflen);
#endif /* MLKEM_USE_NATIVE_REJ_UNIFORM */
#endif /* MLKEM_NATIVE_ARITH_NATIVE_API_H */