Skip to content

Commit 54ba0b1

Browse files
authored
Merge pull request #359 from pq-code-package/namespacing
Namespace structs, static functions, and static arrays
2 parents f2d8abd + b68ab5a commit 54ba0b1

File tree

107 files changed

+1092
-1078
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+1092
-1078
lines changed

mldsa/ntt.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ __contract__(
1313
ensures(return_value > -MLDSA_Q && return_value < MLDSA_Q)
1414
)
1515
{
16-
return montgomery_reduce((int64_t)a * (int64_t)b);
16+
return mld_montgomery_reduce((int64_t)a * (int64_t)b);
1717
/* TODO: reason about bounds */
1818
}
1919

@@ -35,7 +35,7 @@ __contract__(
3535
)
3636
{
3737
const int32_t f = 41978; /* mont^2/256 */
38-
return montgomery_reduce((int64_t)a * f);
38+
return mld_montgomery_reduce((int64_t)a * f);
3939
/* TODO: reason about bounds */
4040
}
4141

@@ -139,13 +139,13 @@ __contract__(
139139
invariant(array_abs_bound(r, 0, start, layer * MLDSA_Q + MLDSA_Q))
140140
invariant(array_abs_bound(r, start, MLDSA_N, layer * MLDSA_Q)))
141141
{
142-
int32_t zeta = zetas[k++];
142+
int32_t zeta = mld_zetas[k++];
143143
mld_ntt_butterfly_block(r, zeta, start, len, layer * MLDSA_Q);
144144
}
145145
}
146146

147147

148-
void ntt(int32_t a[MLDSA_N])
148+
void mld_ntt(int32_t a[MLDSA_N])
149149
{
150150
unsigned int layer;
151151

@@ -184,7 +184,7 @@ __contract__(
184184
invariant(array_abs_bound(r, start, MLDSA_N, (MLDSA_N >> layer) * MLDSA_Q)))
185185
{
186186
unsigned j;
187-
int32_t zeta = -zetas[k--];
187+
int32_t zeta = -mld_zetas[k--];
188188

189189
for (j = start; j < start + len; j++)
190190
__loop__(
@@ -202,20 +202,7 @@ __contract__(
202202
}
203203
}
204204

205-
/*************************************************
206-
* Name: invntt_tomont
207-
*
208-
* Description: Inverse NTT and multiplication by Montgomery factor mont^2 /256.
209-
* In-place. No modular reductions after additions or subtractions;
210-
* Input coefficients need to be smaller than MLDSA_Q
211-
* in absolute value.
212-
* Output coefficient are smaller than MLD_INTT_BOUND
213-
* in absolute value.
214-
*
215-
* Arguments: - int32_t a[MLDSA_N]: input/output coefficient array
216-
**************************************************/
217-
void invntt_tomont(int32_t a[MLDSA_N])
218-
205+
void mld_invntt_tomont(int32_t a[MLDSA_N])
219206
{
220207
unsigned int layer, j;
221208

mldsa/ntt.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
/* Absolute exclusive upper bound for the output of the inverse NTT*/
1515
#define MLD_INTT_BOUND 4211139
1616

17-
#define ntt MLD_NAMESPACE(ntt)
17+
#define mld_ntt MLD_NAMESPACE(ntt)
1818
/*************************************************
19-
* Name: ntt
19+
* Name: mld_ntt
2020
*
2121
* Description: Computes number-theoretic transform (NTT) of
2222
* a polynomial in place.
@@ -35,17 +35,17 @@
3535
* Specification: Implements [FIPS 204, Algorithm 41, NTT]
3636
*
3737
**************************************************/
38-
void ntt(int32_t a[MLDSA_N])
38+
void mld_ntt(int32_t a[MLDSA_N])
3939
__contract__(
4040
requires(memory_no_alias(a, MLDSA_N * sizeof(int32_t)))
4141
requires(array_abs_bound(a, 0, MLDSA_N, MLDSA_Q))
4242
assigns(memory_slice(a, MLDSA_N * sizeof(int32_t)))
4343
ensures(array_abs_bound(a, 0, MLDSA_N, MLD_NTT_BOUND))
4444
);
4545

46-
#define invntt_tomont MLD_NAMESPACE(invntt_tomont)
46+
#define mld_invntt_tomont MLD_NAMESPACE(invntt_tomont)
4747
/*************************************************
48-
* Name: invntt_tomont
48+
* Name: mld_invntt_tomont
4949
*
5050
* Description: Inverse NTT and multiplication by
5151
* Montgomery factor mont^2 /256. In-place.
@@ -57,7 +57,7 @@ __contract__(
5757
*
5858
* Arguments: - int32_t a[MLDSA_N]: input/output coefficient array
5959
**************************************************/
60-
void invntt_tomont(int32_t a[MLDSA_N])
60+
void mld_invntt_tomont(int32_t a[MLDSA_N])
6161
__contract__(
6262
requires(memory_no_alias(a, MLDSA_N * sizeof(int32_t)))
6363
requires(array_abs_bound(a, 0, MLDSA_N, MLDSA_Q))

mldsa/packing.c

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include "poly.h"
1010
#include "polyvec.h"
1111

12-
void pack_pk(uint8_t pk[CRYPTO_PUBLICKEYBYTES],
13-
const uint8_t rho[MLDSA_SEEDBYTES], const polyveck *t1)
12+
void mld_pack_pk(uint8_t pk[CRYPTO_PUBLICKEYBYTES],
13+
const uint8_t rho[MLDSA_SEEDBYTES], const mld_polyveck *t1)
1414
{
1515
unsigned int i;
1616

@@ -19,12 +19,12 @@ void pack_pk(uint8_t pk[CRYPTO_PUBLICKEYBYTES],
1919

2020
for (i = 0; i < MLDSA_K; ++i)
2121
{
22-
polyt1_pack(pk + i * MLDSA_POLYT1_PACKEDBYTES, &t1->vec[i]);
22+
mld_polyt1_pack(pk + i * MLDSA_POLYT1_PACKEDBYTES, &t1->vec[i]);
2323
}
2424
}
2525

26-
void unpack_pk(uint8_t rho[MLDSA_SEEDBYTES], polyveck *t1,
27-
const uint8_t pk[CRYPTO_PUBLICKEYBYTES])
26+
void mld_unpack_pk(uint8_t rho[MLDSA_SEEDBYTES], mld_polyveck *t1,
27+
const uint8_t pk[CRYPTO_PUBLICKEYBYTES])
2828
{
2929
unsigned int i;
3030

@@ -33,15 +33,15 @@ void unpack_pk(uint8_t rho[MLDSA_SEEDBYTES], polyveck *t1,
3333

3434
for (i = 0; i < MLDSA_K; ++i)
3535
{
36-
polyt1_unpack(&t1->vec[i], pk + i * MLDSA_POLYT1_PACKEDBYTES);
36+
mld_polyt1_unpack(&t1->vec[i], pk + i * MLDSA_POLYT1_PACKEDBYTES);
3737
}
3838
}
3939

40-
void pack_sk(uint8_t sk[CRYPTO_SECRETKEYBYTES],
41-
const uint8_t rho[MLDSA_SEEDBYTES],
42-
const uint8_t tr[MLDSA_TRBYTES],
43-
const uint8_t key[MLDSA_SEEDBYTES], const polyveck *t0,
44-
const polyvecl *s1, const polyveck *s2)
40+
void mld_pack_sk(uint8_t sk[CRYPTO_SECRETKEYBYTES],
41+
const uint8_t rho[MLDSA_SEEDBYTES],
42+
const uint8_t tr[MLDSA_TRBYTES],
43+
const uint8_t key[MLDSA_SEEDBYTES], const mld_polyveck *t0,
44+
const mld_polyvecl *s1, const mld_polyveck *s2)
4545
{
4646
memcpy(sk, rho, MLDSA_SEEDBYTES);
4747
sk += MLDSA_SEEDBYTES;
@@ -52,18 +52,19 @@ void pack_sk(uint8_t sk[CRYPTO_SECRETKEYBYTES],
5252
memcpy(sk, tr, MLDSA_TRBYTES);
5353
sk += MLDSA_TRBYTES;
5454

55-
polyvecl_pack_eta(sk, s1);
55+
mld_polyvecl_pack_eta(sk, s1);
5656
sk += MLDSA_L * MLDSA_POLYETA_PACKEDBYTES;
5757

58-
polyveck_pack_eta(sk, s2);
58+
mld_polyveck_pack_eta(sk, s2);
5959
sk += MLDSA_K * MLDSA_POLYETA_PACKEDBYTES;
6060

61-
polyveck_pack_t0(sk, t0);
61+
mld_polyveck_pack_t0(sk, t0);
6262
}
6363

64-
void unpack_sk(uint8_t rho[MLDSA_SEEDBYTES], uint8_t tr[MLDSA_TRBYTES],
65-
uint8_t key[MLDSA_SEEDBYTES], polyveck *t0, polyvecl *s1,
66-
polyveck *s2, const uint8_t sk[CRYPTO_SECRETKEYBYTES])
64+
void mld_unpack_sk(uint8_t rho[MLDSA_SEEDBYTES], uint8_t tr[MLDSA_TRBYTES],
65+
uint8_t key[MLDSA_SEEDBYTES], mld_polyveck *t0,
66+
mld_polyvecl *s1, mld_polyveck *s2,
67+
const uint8_t sk[CRYPTO_SECRETKEYBYTES])
6768
{
6869
memcpy(rho, sk, MLDSA_SEEDBYTES);
6970
sk += MLDSA_SEEDBYTES;
@@ -74,25 +75,25 @@ void unpack_sk(uint8_t rho[MLDSA_SEEDBYTES], uint8_t tr[MLDSA_TRBYTES],
7475
memcpy(tr, sk, MLDSA_TRBYTES);
7576
sk += MLDSA_TRBYTES;
7677

77-
polyvecl_unpack_eta(s1, sk);
78+
mld_polyvecl_unpack_eta(s1, sk);
7879
sk += MLDSA_L * MLDSA_POLYETA_PACKEDBYTES;
7980

80-
polyveck_unpack_eta(s2, sk);
81+
mld_polyveck_unpack_eta(s2, sk);
8182
sk += MLDSA_K * MLDSA_POLYETA_PACKEDBYTES;
8283

83-
polyveck_unpack_t0(t0, sk);
84+
mld_polyveck_unpack_t0(t0, sk);
8485
}
8586

86-
void pack_sig(uint8_t sig[CRYPTO_BYTES], const uint8_t c[MLDSA_CTILDEBYTES],
87-
const polyvecl *z, const polyveck *h,
88-
const unsigned int number_of_hints)
87+
void mld_pack_sig(uint8_t sig[CRYPTO_BYTES], const uint8_t c[MLDSA_CTILDEBYTES],
88+
const mld_polyvecl *z, const mld_polyveck *h,
89+
const unsigned int number_of_hints)
8990
{
9091
unsigned int i, j, k;
9192

9293
memcpy(sig, c, MLDSA_CTILDEBYTES);
9394
sig += MLDSA_CTILDEBYTES;
9495

95-
polyvecl_pack_z(sig, z);
96+
mld_polyvecl_pack_z(sig, z);
9697
sig += MLDSA_L * MLDSA_POLYZ_PACKEDBYTES;
9798

9899
/* Encode hints h */
@@ -153,22 +154,22 @@ void pack_sig(uint8_t sig[CRYPTO_BYTES], const uint8_t c[MLDSA_CTILDEBYTES],
153154
}
154155

155156
/*************************************************
156-
* Name: unpack_hints
157+
* Name: mld_unpack_hints
157158
*
158159
* Description: Unpack raw hint bytes into a polyveck
159160
* struct
160161
*
161-
* Arguments: - polyveck *h: pointer to output hint vector h
162+
* Arguments: - mld_polyveck *h: pointer to output hint vector h
162163
* - const uint8_t packed_hints[MLDSA_POLYVECH_PACKEDBYTES]:
163164
* raw hint bytes
164165
*
165166
* Returns 1 in case of malformed hints; otherwise 0.
166167
**************************************************/
167-
static int unpack_hints(polyveck *h,
168-
const uint8_t packed_hints[MLDSA_POLYVECH_PACKEDBYTES])
168+
static int mld_unpack_hints(
169+
mld_polyveck *h, const uint8_t packed_hints[MLDSA_POLYVECH_PACKEDBYTES])
169170
__contract__(
170171
requires(memory_no_alias(packed_hints, MLDSA_POLYVECH_PACKEDBYTES))
171-
requires(memory_no_alias(h, sizeof(polyveck)))
172+
requires(memory_no_alias(h, sizeof(mld_polyveck)))
172173
assigns(object_whole(h))
173174
/* All returned coefficients are either 0 or 1 */
174175
ensures(forall(k1, 0, MLDSA_K,
@@ -182,7 +183,7 @@ __contract__(
182183
/* Set all coefficients of all polynomials to 0. */
183184
/* Only those that are actually non-zero hints will */
184185
/* be overwritten below. */
185-
memset(h, 0, sizeof(polyveck));
186+
memset(h, 0, sizeof(mld_polyveck));
186187

187188
old_hint_count = 0;
188189
for (i = 0; i < MLDSA_K; ++i)
@@ -243,14 +244,14 @@ __contract__(
243244
return 0;
244245
}
245246

246-
int unpack_sig(uint8_t c[MLDSA_CTILDEBYTES], polyvecl *z, polyveck *h,
247-
const uint8_t sig[CRYPTO_BYTES])
247+
int mld_unpack_sig(uint8_t c[MLDSA_CTILDEBYTES], mld_polyvecl *z,
248+
mld_polyveck *h, const uint8_t sig[CRYPTO_BYTES])
248249
{
249250
memcpy(c, sig, MLDSA_CTILDEBYTES);
250251
sig += MLDSA_CTILDEBYTES;
251252

252-
polyvecl_unpack_z(z, sig);
253+
mld_polyvecl_unpack_z(z, sig);
253254
sig += MLDSA_L * MLDSA_POLYZ_PACKEDBYTES;
254255

255-
return unpack_hints(h, sig);
256+
return mld_unpack_hints(h, sig);
256257
}

0 commit comments

Comments
 (0)