Skip to content

Commit

Permalink
Merge pull request pq-code-package#623 from pq-code-package/nonpublic…
Browse files Browse the repository at this point in the history
…_symbols

Add marker for non-public API
  • Loading branch information
mkannwischer authored Jan 8, 2025
2 parents dc3d0cd + 6730696 commit 66dc45e
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ Macros:
# Make this artifically long to avoid function bodies after short contracts
- __contract__(x)={ void a; void b; void c; void d; void e; void f; } void abcdefghijklmnopqrstuvw()
- __loop__(x)={}
# Make this artifically long to force line break
- MLKEM_NATIVE_INTERNAL_API=void abcdefghijklmnopqrstuvwabcdefghijklmnopqrstuvwabcdefg();
2 changes: 2 additions & 0 deletions examples/monolithic_build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ CFLAGS := \
-std=c90 \
-pedantic \
-MMD
# Set this flag to give all non-global functions internal linkage
CFLAGS += -DMLKEM_NATIVE_MONOBUILD

BINARY_NAME_FULL=$(BUILD_DIR)/$(BIN)

Expand Down
10 changes: 10 additions & 0 deletions examples/monolithic_build/mlkem_native_monobuild.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@
#undef MLKEM_NATIVE_FIPS202_BACKEND_NAME
#endif

/* mlkem/common.h */
#if defined(MLKEM_NATIVE_INTERNAL_API)
#undef MLKEM_NATIVE_INTERNAL_API
#endif

/* mlkem/common.h */
#if defined(MLKEM_NATIVE_INTERNAL_API)
#undef MLKEM_NATIVE_INTERNAL_API
#endif

/* mlkem/common.h */
#if defined(MLKEM_ASM_NAMESPACE)
#undef MLKEM_ASM_NAMESPACE
Expand Down
2 changes: 2 additions & 0 deletions mlkem/cbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ static void cbd3(poly *r, const uint8_t buf[3 * MLKEM_N / 4])
}
#endif /* MLKEM_ETA1 == 3 */

MLKEM_NATIVE_INTERNAL_API
void poly_cbd_eta1(poly *r, const uint8_t buf[MLKEM_ETA1 * MLKEM_N / 4])
{
#if MLKEM_ETA1 == 2
Expand All @@ -142,6 +143,7 @@ void poly_cbd_eta1(poly *r, const uint8_t buf[MLKEM_ETA1 * MLKEM_N / 4])
#endif
}

MLKEM_NATIVE_INTERNAL_API
void poly_cbd_eta2(poly *r, const uint8_t buf[MLKEM_ETA2 * MLKEM_N / 4])
{
#if MLKEM_ETA2 == 2
Expand Down
2 changes: 2 additions & 0 deletions mlkem/cbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Arguments: - poly *r: pointer to output polynomial
* - const uint8_t *buf: pointer to input byte array
**************************************************/
MLKEM_NATIVE_INTERNAL_API
void poly_cbd_eta1(poly *r, const uint8_t buf[MLKEM_ETA1 * MLKEM_N / 4])
__contract__(
requires(memory_no_alias(r, sizeof(poly)))
Expand All @@ -39,6 +40,7 @@ __contract__(
* Arguments: - poly *r: pointer to output polynomial
* - const uint8_t *buf: pointer to input byte array
**************************************************/
MLKEM_NATIVE_INTERNAL_API
void poly_cbd_eta2(poly *r, const uint8_t buf[MLKEM_ETA2 * MLKEM_N / 4])
__contract__(
requires(memory_no_alias(r, sizeof(poly)))
Expand Down
8 changes: 8 additions & 0 deletions mlkem/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
#define MLKEM_NATIVE_FIPS202_BACKEND_NAME C
#endif

/* For a monobuild (where all compilation units are merged into one), mark
* all non-public API as static since they don't need external linkage. */
#if !defined(MLKEM_NATIVE_MONOBUILD)
#define MLKEM_NATIVE_INTERNAL_API
#else
#define MLKEM_NATIVE_INTERNAL_API static
#endif

/* On Apple platforms, we need to emit leading underscore
* in front of assembly symbols. We thus introducee a separate
* namespace wrapper for ASM symbols. */
Expand Down
4 changes: 4 additions & 0 deletions mlkem/indcpa.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ __contract__(
#endif /* MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER */

/* Not static for benchmarking */
MLKEM_NATIVE_INTERNAL_API
void gen_matrix(polyvec *a, const uint8_t seed[MLKEM_SYMBYTES], int transposed)
{
int i;
Expand Down Expand Up @@ -409,6 +410,7 @@ __contract__(

STATIC_ASSERT(NTT_BOUND + MLKEM_Q < INT16_MAX, indcpa_enc_bound_0)

MLKEM_NATIVE_INTERNAL_API
void indcpa_keypair_derand(uint8_t pk[MLKEM_INDCPA_PUBLICKEYBYTES],
uint8_t sk[MLKEM_INDCPA_SECRETKEYBYTES],
const uint8_t coins[MLKEM_SYMBYTES])
Expand Down Expand Up @@ -472,6 +474,7 @@ STATIC_ASSERT(INVNTT_BOUND + MLKEM_ETA1 < INT16_MAX, indcpa_enc_bound_0)
STATIC_ASSERT(INVNTT_BOUND + MLKEM_ETA2 + MLKEM_Q < INT16_MAX,
indcpa_enc_bound_1)

MLKEM_NATIVE_INTERNAL_API
void indcpa_enc(uint8_t c[MLKEM_INDCPA_BYTES],
const uint8_t m[MLKEM_INDCPA_MSGBYTES],
const uint8_t pk[MLKEM_INDCPA_PUBLICKEYBYTES],
Expand Down Expand Up @@ -531,6 +534,7 @@ void indcpa_enc(uint8_t c[MLKEM_INDCPA_BYTES],
/* Check that the arithmetic in indcpa_dec() does not overflow */
STATIC_ASSERT(INVNTT_BOUND + MLKEM_Q < INT16_MAX, indcpa_dec_bound_0)

MLKEM_NATIVE_INTERNAL_API
void indcpa_dec(uint8_t m[MLKEM_INDCPA_MSGBYTES],
const uint8_t c[MLKEM_INDCPA_BYTES],
const uint8_t sk[MLKEM_INDCPA_SECRETKEYBYTES])
Expand Down
4 changes: 4 additions & 0 deletions mlkem/indcpa.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* - const uint8_t *seed: pointer to input seed
* - int transposed: boolean deciding whether A or A^T is generated
**************************************************/
MLKEM_NATIVE_INTERNAL_API
void gen_matrix(polyvec *a, const uint8_t seed[MLKEM_SYMBYTES], int transposed)
__contract__(
requires(memory_no_alias(a, sizeof(polyvec) * MLKEM_K))
Expand All @@ -47,6 +48,7 @@ __contract__(
* - const uint8_t *coins: pointer to input randomness
* (of length MLKEM_SYMBYTES bytes)
**************************************************/
MLKEM_NATIVE_INTERNAL_API
void indcpa_keypair_derand(uint8_t pk[MLKEM_INDCPA_PUBLICKEYBYTES],
uint8_t sk[MLKEM_INDCPA_SECRETKEYBYTES],
const uint8_t coins[MLKEM_SYMBYTES])
Expand Down Expand Up @@ -74,6 +76,7 @@ __contract__(
* - const uint8_t *coins: pointer to input random coins used as
*seed (of length MLKEM_SYMBYTES) to deterministically generate all randomness
**************************************************/
MLKEM_NATIVE_INTERNAL_API
void indcpa_enc(uint8_t c[MLKEM_INDCPA_BYTES],
const uint8_t m[MLKEM_INDCPA_MSGBYTES],
const uint8_t pk[MLKEM_INDCPA_PUBLICKEYBYTES],
Expand All @@ -100,6 +103,7 @@ __contract__(
* - const uint8_t *sk: pointer to input secret key
* (of length MLKEM_INDCPA_SECRETKEYBYTES)
**************************************************/
MLKEM_NATIVE_INTERNAL_API
void indcpa_dec(uint8_t m[MLKEM_INDCPA_MSGBYTES],
const uint8_t c[MLKEM_INDCPA_BYTES],
const uint8_t sk[MLKEM_INDCPA_SECRETKEYBYTES])
Expand Down
5 changes: 5 additions & 0 deletions mlkem/ntt.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ __contract__(
* the proof may need strengthening.
*/

MLKEM_NATIVE_INTERNAL_API
void poly_ntt(poly *p)
{
int len, layer;
Expand All @@ -150,6 +151,7 @@ void poly_ntt(poly *p)
/* Check that bound for native NTT implies contractual bound */
STATIC_ASSERT(NTT_BOUND_NATIVE <= NTT_BOUND, invntt_bound)

MLKEM_NATIVE_INTERNAL_API
void poly_ntt(poly *p)
{
POLY_BOUND_MSG(p, MLKEM_Q, "native ntt input");
Expand Down Expand Up @@ -201,6 +203,7 @@ __contract__(
}
}

MLKEM_NATIVE_INTERNAL_API
void poly_invntt_tomont(poly *p)
{
/*
Expand Down Expand Up @@ -236,13 +239,15 @@ void poly_invntt_tomont(poly *p)
/* Check that bound for native invNTT implies contractual bound */
STATIC_ASSERT(INVNTT_BOUND_NATIVE <= INVNTT_BOUND, invntt_bound)

MLKEM_NATIVE_INTERNAL_API
void poly_invntt_tomont(poly *p)
{
intt_native(p);
POLY_BOUND_MSG(p, INVNTT_BOUND_NATIVE, "native intt output");
}
#endif /* MLKEM_USE_NATIVE_INTT */

MLKEM_NATIVE_INTERNAL_API
void basemul_cached(int16_t r[2], const int16_t a[2], const int16_t b[2],
int16_t b_cached)
{
Expand Down
3 changes: 3 additions & 0 deletions mlkem/ntt.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern const int16_t zetas[128];
*
* Arguments: - poly *p: pointer to in/output polynomial
**************************************************/
MLKEM_NATIVE_INTERNAL_API
void poly_ntt(poly *r)
__contract__(
requires(memory_no_alias(r, sizeof(poly)))
Expand All @@ -57,6 +58,7 @@ __contract__(
*
* Arguments: - uint16_t *a: pointer to in/output polynomial
**************************************************/
MLKEM_NATIVE_INTERNAL_API
void poly_invntt_tomont(poly *r)
__contract__(
requires(memory_no_alias(r, sizeof(poly)))
Expand Down Expand Up @@ -85,6 +87,7 @@ __contract__(
* - b_cached: Some precomputed value, typically derived from
* b1 and a twiddle factor. Can be an arbitary int16_t.
************************************************************/
MLKEM_NATIVE_INTERNAL_API
void basemul_cached(int16_t r[2], const int16_t a[2], const int16_t b[2],
int16_t b_cached)
__contract__(
Expand Down
22 changes: 22 additions & 0 deletions mlkem/poly.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "symmetric.h"
#include "verify.h"

MLKEM_NATIVE_INTERNAL_API
void poly_compress_du(uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_DU], const poly *a)
{
int j;
Expand Down Expand Up @@ -80,6 +81,7 @@ void poly_compress_du(uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_DU], const poly *a)
}


MLKEM_NATIVE_INTERNAL_API
void poly_decompress_du(poly *r, const uint8_t a[MLKEM_POLYCOMPRESSEDBYTES_DU])
{
int j;
Expand Down Expand Up @@ -139,6 +141,7 @@ void poly_decompress_du(poly *r, const uint8_t a[MLKEM_POLYCOMPRESSEDBYTES_DU])
#endif
}

MLKEM_NATIVE_INTERNAL_API
void poly_compress_dv(uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_DV], const poly *a)
{
int i;
Expand Down Expand Up @@ -193,6 +196,7 @@ void poly_compress_dv(uint8_t r[MLKEM_POLYCOMPRESSEDBYTES_DV], const poly *a)
#endif
}

MLKEM_NATIVE_INTERNAL_API
void poly_decompress_dv(poly *r, const uint8_t a[MLKEM_POLYCOMPRESSEDBYTES_DV])
{
int i;
Expand Down Expand Up @@ -250,6 +254,7 @@ void poly_decompress_dv(poly *r, const uint8_t a[MLKEM_POLYCOMPRESSEDBYTES_DV])
}

#if !defined(MLKEM_USE_NATIVE_POLY_TOBYTES)
MLKEM_NATIVE_INTERNAL_API
void poly_tobytes(uint8_t r[MLKEM_POLYBYTES], const poly *a)
{
unsigned int i;
Expand Down Expand Up @@ -282,6 +287,7 @@ void poly_tobytes(uint8_t r[MLKEM_POLYBYTES], const poly *a)
}
}
#else /* MLKEM_USE_NATIVE_POLY_TOBYTES */
MLKEM_NATIVE_INTERNAL_API
void poly_tobytes(uint8_t r[MLKEM_POLYBYTES], const poly *a)
{
POLY_UBOUND(a, MLKEM_Q);
Expand All @@ -290,6 +296,7 @@ void poly_tobytes(uint8_t r[MLKEM_POLYBYTES], const poly *a)
#endif /* MLKEM_USE_NATIVE_POLY_TOBYTES */

#if !defined(MLKEM_USE_NATIVE_POLY_FROMBYTES)
MLKEM_NATIVE_INTERNAL_API
void poly_frombytes(poly *r, const uint8_t a[MLKEM_POLYBYTES])
{
int i;
Expand All @@ -309,12 +316,14 @@ void poly_frombytes(poly *r, const uint8_t a[MLKEM_POLYBYTES])
POLY_UBOUND(r, 4096);
}
#else /* MLKEM_USE_NATIVE_POLY_FROMBYTES */
MLKEM_NATIVE_INTERNAL_API
void poly_frombytes(poly *r, const uint8_t a[MLKEM_POLYBYTES])
{
poly_frombytes_native(r, a);
}
#endif /* MLKEM_USE_NATIVE_POLY_FROMBYTES */

MLKEM_NATIVE_INTERNAL_API
void poly_frommsg(poly *r, const uint8_t msg[MLKEM_INDCPA_MSGBYTES])
{
int i;
Expand All @@ -341,6 +350,7 @@ void poly_frommsg(poly *r, const uint8_t msg[MLKEM_INDCPA_MSGBYTES])
POLY_BOUND_MSG(r, MLKEM_Q, "poly_frommsg output");
}

MLKEM_NATIVE_INTERNAL_API
void poly_tomsg(uint8_t msg[MLKEM_INDCPA_MSGBYTES], const poly *a)
{
int i;
Expand All @@ -361,6 +371,7 @@ void poly_tomsg(uint8_t msg[MLKEM_INDCPA_MSGBYTES], const poly *a)
}
}

MLKEM_NATIVE_INTERNAL_API
void poly_getnoise_eta1_4x(poly *r0, poly *r1, poly *r2, poly *r3,
const uint8_t seed[MLKEM_SYMBYTES], uint8_t nonce0,
uint8_t nonce1, uint8_t nonce2, uint8_t nonce3)
Expand Down Expand Up @@ -388,6 +399,7 @@ void poly_getnoise_eta1_4x(poly *r0, poly *r1, poly *r2, poly *r3,
POLY_BOUND_MSG(r3, MLKEM_ETA1 + 1, "poly_getnoise_eta1_4x output 3");
}

MLKEM_NATIVE_INTERNAL_API
void poly_getnoise_eta2(poly *r, const uint8_t seed[MLKEM_SYMBYTES],
uint8_t nonce)
{
Expand All @@ -403,6 +415,7 @@ void poly_getnoise_eta2(poly *r, const uint8_t seed[MLKEM_SYMBYTES],
POLY_BOUND_MSG(r, MLKEM_ETA1 + 1, "poly_getnoise_eta2 output");
}

MLKEM_NATIVE_INTERNAL_API
void poly_getnoise_eta1122_4x(poly *r0, poly *r1, poly *r2, poly *r3,
const uint8_t seed[MLKEM_SYMBYTES],
uint8_t nonce0, uint8_t nonce1, uint8_t nonce2,
Expand Down Expand Up @@ -441,6 +454,7 @@ void poly_getnoise_eta1122_4x(poly *r0, poly *r1, poly *r2, poly *r3,
POLY_BOUND_MSG(r3, MLKEM_ETA2 + 1, "poly_getnoise_eta1122_4x output 3");
}

MLKEM_NATIVE_INTERNAL_API
void poly_basemul_montgomery_cached(poly *r, const poly *a, const poly *b,
const poly_mulcache *b_cache)
{
Expand All @@ -461,6 +475,7 @@ void poly_basemul_montgomery_cached(poly *r, const poly *a, const poly *b,
}

#if !defined(MLKEM_USE_NATIVE_POLY_TOMONT)
MLKEM_NATIVE_INTERNAL_API
void poly_tomont(poly *r)
{
int i;
Expand All @@ -476,6 +491,7 @@ void poly_tomont(poly *r)
POLY_BOUND(r, MLKEM_Q);
}
#else /* MLKEM_USE_NATIVE_POLY_TOMONT */
MLKEM_NATIVE_INTERNAL_API
void poly_tomont(poly *r)
{
poly_tomont_native(r);
Expand All @@ -484,6 +500,7 @@ void poly_tomont(poly *r)
#endif /* MLKEM_USE_NATIVE_POLY_TOMONT */

#if !defined(MLKEM_USE_NATIVE_POLY_REDUCE)
MLKEM_NATIVE_INTERNAL_API
void poly_reduce(poly *r)
{
int i;
Expand All @@ -501,13 +518,15 @@ void poly_reduce(poly *r)
POLY_UBOUND(r, MLKEM_Q);
}
#else /* MLKEM_USE_NATIVE_POLY_REDUCE */
MLKEM_NATIVE_INTERNAL_API
void poly_reduce(poly *r)
{
poly_reduce_native(r);
POLY_UBOUND(r, MLKEM_Q);
}
#endif /* MLKEM_USE_NATIVE_POLY_REDUCE */

MLKEM_NATIVE_INTERNAL_API
void poly_add(poly *r, const poly *b)
{
int i;
Expand All @@ -521,6 +540,7 @@ void poly_add(poly *r, const poly *b)
}
}

MLKEM_NATIVE_INTERNAL_API
void poly_sub(poly *r, const poly *b)
{
int i;
Expand All @@ -535,6 +555,7 @@ void poly_sub(poly *r, const poly *b)
}

#if !defined(MLKEM_USE_NATIVE_POLY_MULCACHE_COMPUTE)
MLKEM_NATIVE_INTERNAL_API
void poly_mulcache_compute(poly_mulcache *x, const poly *a)
{
int i;
Expand All @@ -547,6 +568,7 @@ void poly_mulcache_compute(poly_mulcache *x, const poly *a)
POLY_BOUND(x, MLKEM_Q);
}
#else /* MLKEM_USE_NATIVE_POLY_MULCACHE_COMPUTE */
MLKEM_NATIVE_INTERNAL_API
void poly_mulcache_compute(poly_mulcache *x, const poly *a)
{
poly_mulcache_compute_native(x, a);
Expand Down
Loading

0 comments on commit 66dc45e

Please sign in to comment.