Skip to content

Commit

Permalink
added prefix for all mldsa functions to prevent conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
jakemas committed Dec 24, 2024
1 parent 363d631 commit 2bbdfc9
Show file tree
Hide file tree
Showing 16 changed files with 544 additions and 530 deletions.
10 changes: 5 additions & 5 deletions crypto/dilithium/pqcrystals_dilithium_ref_common/ntt.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void ml_dsa_ntt(int32_t a[ML_DSA_N]) {
for(start = 0; start < ML_DSA_N; start = j + len) {
zeta = zetas[++k];
for(j = start; j < start + len; ++j) {
t = fqmul(zeta, a[j + len]);
t = ml_dsa_fqmul(zeta, a[j + len]);
a[j + len] = a[j] - t;
a[j] = a[j] + t;
}
Expand All @@ -65,7 +65,7 @@ void ml_dsa_ntt(int32_t a[ML_DSA_N]) {
}

/*************************************************
* Name: invntt_tomont
* Name: ml_dsa_invntt_tomont
*
* Description: FIPS 204: Algorithm 42.
* Inverse NTT and multiplication by Montgomery factor 2^32.
Expand All @@ -76,7 +76,7 @@ void ml_dsa_ntt(int32_t a[ML_DSA_N]) {
*
* Arguments: - uint32_t p[N]: input/output coefficient array
**************************************************/
void invntt_tomont(int32_t a[ML_DSA_N]) {
void ml_dsa_invntt_tomont(int32_t a[ML_DSA_N]) {
unsigned int start, len, j, k;
int32_t t, zeta;
const int32_t f = 41978; // mont^2/256
Expand All @@ -89,12 +89,12 @@ void invntt_tomont(int32_t a[ML_DSA_N]) {
t = a[j];
a[j] = t + a[j + len];
a[j + len] = t - a[j + len];
a[j + len] = fqmul(zeta, a[j + len]);
a[j + len] = ml_dsa_fqmul(zeta, a[j + len]);
}
}
}

for(j = 0; j < ML_DSA_N; ++j) {
a[j] = fqmul(f, a[j]);
a[j] = ml_dsa_fqmul(f, a[j]);
}
}
2 changes: 1 addition & 1 deletion crypto/dilithium/pqcrystals_dilithium_ref_common/ntt.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

void ml_dsa_ntt(int32_t a[ML_DSA_N]);

void invntt_tomont(int32_t a[ML_DSA_N]);
void ml_dsa_invntt_tomont(int32_t a[ML_DSA_N]);

#endif
132 changes: 66 additions & 66 deletions crypto/dilithium/pqcrystals_dilithium_ref_common/packing.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "poly.h"

/*************************************************
* Name: pack_pk
* Name: ml_dsa_pack_pk
*
* Description: FIPS 204: Algorithm 22 pkEncode.
* Bit-pack public key pk = (rho, t1).
Expand All @@ -14,25 +14,25 @@
* - const uint8_t rho[]: byte array containing rho
* - const polyveck *t1: pointer to vector t1
**************************************************/
void pack_pk(ml_dsa_params *params,
uint8_t *pk,
const uint8_t rho[SEEDBYTES],
const polyveck *t1)
void ml_dsa_pack_pk(ml_dsa_params *params,
uint8_t *pk,
const uint8_t rho[ML_DSA_SEEDBYTES],
const polyveck *t1)
{
unsigned int i;

for(i = 0; i < SEEDBYTES; ++i) {
for(i = 0; i < ML_DSA_SEEDBYTES; ++i) {
pk[i] = rho[i];
}
pk += SEEDBYTES;
pk += ML_DSA_SEEDBYTES;

for(i = 0; i < params->k; ++i) {
polyt1_pack(pk + i*POLYT1_PACKEDBYTES, &t1->vec[i]);
ml_dsa_polyt1_pack(pk + i * ML_DSA_POLYT1_PACKEDBYTES, &t1->vec[i]);
}
}

/*************************************************
* Name: unpack_pk
* Name: ml_dsa_unpack_pk
*
* Description: FIPS 204: Algorithm 23 pkDecode.
* Unpack public key pk = (rho, t1).
Expand All @@ -42,25 +42,25 @@ void pack_pk(ml_dsa_params *params,
* - const polyveck *t1: pointer to output vector t1
* - uint8_t pk[]: pointer to byte array containing bit-packed pk
**************************************************/
void unpack_pk(ml_dsa_params *params,
uint8_t rho[SEEDBYTES],
polyveck *t1,
const uint8_t *pk)
void ml_dsa_unpack_pk(ml_dsa_params *params,
uint8_t rho[ML_DSA_SEEDBYTES],
polyveck *t1,
const uint8_t *pk)
{
unsigned int i;

for(i = 0; i < SEEDBYTES; ++i) {
for(i = 0; i < ML_DSA_SEEDBYTES; ++i) {
rho[i] = pk[i];
}
pk += SEEDBYTES;
pk += ML_DSA_SEEDBYTES;

for(i = 0; i < params->k; ++i) {
polyt1_unpack(&t1->vec[i], pk + i*POLYT1_PACKEDBYTES);
ml_dsa_polyt1_unpack(&t1->vec[i], pk + i * ML_DSA_POLYT1_PACKEDBYTES);
}
}

/*************************************************
* Name: pack_sk
* Name: ml_dsa_pack_sk
*
* Description: FIPS 204: Algorithm 24 skEncode.
* Bit-pack secret key sk = (rho, tr, key, t0, s1, s2).
Expand All @@ -74,49 +74,49 @@ void unpack_pk(ml_dsa_params *params,
* - const polyvecl *s1: pointer to vector s1
* - const polyveck *s2: pointer to vector s2
**************************************************/
void pack_sk(ml_dsa_params *params,
uint8_t *sk,
const uint8_t rho[SEEDBYTES],
const uint8_t tr[TRBYTES],
const uint8_t key[SEEDBYTES],
const polyveck *t0,
const polyvecl *s1,
const polyveck *s2)
void ml_dsa_pack_sk(ml_dsa_params *params,
uint8_t *sk,
const uint8_t rho[ML_DSA_SEEDBYTES],
const uint8_t tr[ML_DSA_TRBYTES],
const uint8_t key[ML_DSA_SEEDBYTES],
const polyveck *t0,
const polyvecl *s1,
const polyveck *s2)
{
unsigned int i;

for(i = 0; i < SEEDBYTES; ++i) {
for(i = 0; i < ML_DSA_SEEDBYTES; ++i) {
sk[i] = rho[i];
}
sk += SEEDBYTES;
sk += ML_DSA_SEEDBYTES;

for(i = 0; i < SEEDBYTES; ++i) {
for(i = 0; i < ML_DSA_SEEDBYTES; ++i) {
sk[i] = key[i];
}
sk += SEEDBYTES;
sk += ML_DSA_SEEDBYTES;

for(i = 0; i < TRBYTES; ++i) {
for(i = 0; i < ML_DSA_TRBYTES; ++i) {
sk[i] = tr[i];
}
sk += TRBYTES;
sk += ML_DSA_TRBYTES;

for(i = 0; i < params->l; ++i) {
polyeta_pack(params, sk + i * params->poly_eta_packed_bytes, &s1->vec[i]);
ml_dsa_polyeta_pack(params, sk + i * params->poly_eta_packed_bytes, &s1->vec[i]);
}
sk += params->l * params->poly_eta_packed_bytes;

for(i = 0; i < params->k; ++i) {
polyeta_pack(params,sk + i * params->poly_eta_packed_bytes, &s2->vec[i]);
ml_dsa_polyeta_pack(params,sk + i * params->poly_eta_packed_bytes, &s2->vec[i]);
}
sk += params->k * params->poly_eta_packed_bytes;

for(i = 0; i < params->k; ++i) {
polyt0_pack(sk + i * POLYT0_PACKEDBYTES, &t0->vec[i]);
ml_dsa_polyt0_pack(sk + i * ML_DSA_POLYT0_PACKEDBYTES, &t0->vec[i]);
}
}

/*************************************************
* Name: unpack_sk
* Name: ml_dsa_unpack_sk
*
* Description: FIPS 204: Algorithm 25 skDecode.
* Unpack secret key sk = (rho, tr, key, t0, s1, s2).
Expand All @@ -130,49 +130,49 @@ void pack_sk(ml_dsa_params *params,
* - const polyveck *s2: pointer to output vector s2
* - uint8_t sk[]: pointer to byte array containing bit-packed sk
**************************************************/
void unpack_sk(ml_dsa_params *params,
uint8_t rho[SEEDBYTES],
uint8_t tr[TRBYTES],
uint8_t key[SEEDBYTES],
polyveck *t0,
polyvecl *s1,
polyveck *s2,
const uint8_t *sk)
void ml_dsa_unpack_sk(ml_dsa_params *params,
uint8_t rho[ML_DSA_SEEDBYTES],
uint8_t tr[ML_DSA_TRBYTES],
uint8_t key[ML_DSA_SEEDBYTES],
polyveck *t0,
polyvecl *s1,
polyveck *s2,
const uint8_t *sk)
{
unsigned int i;

for(i = 0; i < SEEDBYTES; ++i) {
for(i = 0; i < ML_DSA_SEEDBYTES; ++i) {
rho[i] = sk[i];
}
sk += SEEDBYTES;
sk += ML_DSA_SEEDBYTES;

for(i = 0; i < SEEDBYTES; ++i) {
for(i = 0; i < ML_DSA_SEEDBYTES; ++i) {
key[i] = sk[i];
}
sk += SEEDBYTES;
sk += ML_DSA_SEEDBYTES;

for(i = 0; i < TRBYTES; ++i) {
for(i = 0; i < ML_DSA_TRBYTES; ++i) {
tr[i] = sk[i];
}
sk += TRBYTES;
sk += ML_DSA_TRBYTES;

for(i=0; i < params->l; ++i) {
polyeta_unpack(params, &s1->vec[i], sk + i * params->poly_eta_packed_bytes);
ml_dsa_polyeta_unpack(params, &s1->vec[i], sk + i * params->poly_eta_packed_bytes);
}
sk += params->l * params->poly_eta_packed_bytes;

for(i=0; i < params->k; ++i) {
polyeta_unpack(params, &s2->vec[i], sk + i * params->poly_eta_packed_bytes);
ml_dsa_polyeta_unpack(params, &s2->vec[i], sk + i * params->poly_eta_packed_bytes);
}
sk += params->k * params->poly_eta_packed_bytes;

for(i=0; i < params->k; ++i) {
polyt0_unpack(&t0->vec[i], sk + i * POLYT0_PACKEDBYTES);
ml_dsa_polyt0_unpack(&t0->vec[i], sk + i * ML_DSA_POLYT0_PACKEDBYTES);
}
}

/*************************************************
* Name: pack_sig
* Name: ml_dsa_pack_sig
*
* Description: FIPS 204: Algorithm 26 sigEncode.
* Bit-pack signature sig = (c, z, h).
Expand All @@ -183,11 +183,11 @@ void unpack_sk(ml_dsa_params *params,
* - const polyvecl *z: pointer to vector z
* - const polyveck *h: pointer to hint vector h
**************************************************/
void pack_sig(ml_dsa_params *params,
uint8_t *sig,
const uint8_t *c,
const polyvecl *z,
const polyveck *h)
void ml_dsa_pack_sig(ml_dsa_params *params,
uint8_t *sig,
const uint8_t *c,
const polyvecl *z,
const polyveck *h)
{
unsigned int i, j, k;

Expand All @@ -197,7 +197,7 @@ void pack_sig(ml_dsa_params *params,
sig += params->c_tilde_bytes;

for(i = 0; i < params->l; ++i) {
polyz_pack(params, sig + i * params->poly_z_packed_bytes, &z->vec[i]);
ml_dsa_polyz_pack(params, sig + i * params->poly_z_packed_bytes, &z->vec[i]);
}
sig += params->l * params->poly_z_packed_bytes;

Expand All @@ -219,7 +219,7 @@ void pack_sig(ml_dsa_params *params,
}

/*************************************************
* Name: unpack_sig
* Name: ml_dsa_unpack_sig
*
* Description: FIPS 204: Algorithm 27 sigDecode.
* Unpack signature sig = (c, z, h).
Expand All @@ -233,11 +233,11 @@ void pack_sig(ml_dsa_params *params,
*
* Returns 1 in case of malformed signature; otherwise 0.
**************************************************/
int unpack_sig(ml_dsa_params *params,
uint8_t *c,
polyvecl *z,
polyveck *h,
const uint8_t *sig)
int ml_dsa_unpack_sig(ml_dsa_params *params,
uint8_t *c,
polyvecl *z,
polyveck *h,
const uint8_t *sig)
{
unsigned int i, j, k;

Expand All @@ -247,7 +247,7 @@ int unpack_sig(ml_dsa_params *params,
sig += params->c_tilde_bytes;

for(i = 0; i < params->l; ++i) {
polyz_unpack(params, &z->vec[i], sig + i * params->poly_z_packed_bytes);
ml_dsa_polyz_unpack(params, &z->vec[i], sig + i * params->poly_z_packed_bytes);
}
sig += params->l * params->poly_z_packed_bytes;

Expand Down
Loading

0 comments on commit 2bbdfc9

Please sign in to comment.