| 1 |
/* |
| 2 |
* Copyright (c) The mldsa-native project authors |
| 3 |
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 4 |
*/ |
| 5 |
#ifndef MLD_PACKING_H |
| 6 |
#define MLD_PACKING_H |
| 7 |
|
| 8 |
#include <stdint.h> |
| 9 |
#include "polyvec.h" |
| 10 |
|
| 11 |
#define mld_pack_pk MLD_NAMESPACE_KL(pack_pk) |
| 12 |
/************************************************* |
| 13 |
* Name: mld_pack_pk |
| 14 |
* |
| 15 |
* Description: Bit-pack public key pk = (rho, t1). |
| 16 |
* |
| 17 |
* Arguments: - uint8_t pk[]: output byte array |
| 18 |
* - const uint8_t rho[]: byte array containing rho |
| 19 |
* - const mld_polyveck *t1: pointer to vector t1 |
| 20 |
**************************************************/ |
| 21 |
MLD_INTERNAL_API |
| 22 |
void mld_pack_pk(uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES], |
| 23 |
const uint8_t rho[MLDSA_SEEDBYTES], const mld_polyveck *t1) |
| 24 |
__contract__( |
| 25 |
requires(memory_no_alias(pk, MLDSA_CRYPTO_PUBLICKEYBYTES)) |
| 26 |
requires(memory_no_alias(rho, MLDSA_SEEDBYTES)) |
| 27 |
requires(memory_no_alias(t1, sizeof(mld_polyveck))) |
| 28 |
requires(forall(k0, 0, MLDSA_K, |
| 29 |
array_bound(t1->vec[k0].coeffs, 0, MLDSA_N, 0, 1 << 10))) |
| 30 |
assigns(memory_slice(pk, MLDSA_CRYPTO_PUBLICKEYBYTES)) |
| 31 |
); |
| 32 |
|
| 33 |
|
| 34 |
#define mld_pack_sk MLD_NAMESPACE_KL(pack_sk) |
| 35 |
/************************************************* |
| 36 |
* Name: mld_pack_sk |
| 37 |
* |
| 38 |
* Description: Bit-pack secret key sk = (rho, tr, key, t0, s1, s2). |
| 39 |
* |
| 40 |
* Arguments: - uint8_t sk[]: output byte array |
| 41 |
* - const uint8_t rho[]: byte array containing rho |
| 42 |
* - const uint8_t tr[]: byte array containing tr |
| 43 |
* - const uint8_t key[]: byte array containing key |
| 44 |
* - const mld_polyveck *t0: pointer to vector t0 |
| 45 |
* - const mld_polyvecl *s1: pointer to vector s1 |
| 46 |
* - const mld_polyveck *s2: pointer to vector s2 |
| 47 |
**************************************************/ |
| 48 |
MLD_INTERNAL_API |
| 49 |
void mld_pack_sk(uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES], |
| 50 |
const uint8_t rho[MLDSA_SEEDBYTES], |
| 51 |
const uint8_t tr[MLDSA_TRBYTES], |
| 52 |
const uint8_t key[MLDSA_SEEDBYTES], const mld_polyveck *t0, |
| 53 |
const mld_polyvecl *s1, const mld_polyveck *s2) |
| 54 |
__contract__( |
| 55 |
requires(memory_no_alias(sk, MLDSA_CRYPTO_SECRETKEYBYTES)) |
| 56 |
requires(memory_no_alias(rho, MLDSA_SEEDBYTES)) |
| 57 |
requires(memory_no_alias(tr, MLDSA_TRBYTES)) |
| 58 |
requires(memory_no_alias(key, MLDSA_SEEDBYTES)) |
| 59 |
requires(memory_no_alias(t0, sizeof(mld_polyveck))) |
| 60 |
requires(memory_no_alias(s1, sizeof(mld_polyvecl))) |
| 61 |
requires(memory_no_alias(s2, sizeof(mld_polyveck))) |
| 62 |
requires(forall(k0, 0, MLDSA_K, |
| 63 |
array_bound(t0->vec[k0].coeffs, 0, MLDSA_N, -(1<<(MLDSA_D-1)) + 1, (1<<(MLDSA_D-1)) + 1))) |
| 64 |
requires(forall(k1, 0, MLDSA_L, |
| 65 |
array_abs_bound(s1->vec[k1].coeffs, 0, MLDSA_N, MLDSA_ETA + 1))) |
| 66 |
requires(forall(k2, 0, MLDSA_K, |
| 67 |
array_abs_bound(s2->vec[k2].coeffs, 0, MLDSA_N, MLDSA_ETA + 1))) |
| 68 |
assigns(memory_slice(sk, MLDSA_CRYPTO_SECRETKEYBYTES)) |
| 69 |
); |
| 70 |
|
| 71 |
|
| 72 |
#define mld_pack_sig_c_h MLD_NAMESPACE_KL(pack_sig_c_h) |
| 73 |
/************************************************* |
| 74 |
* Name: mld_pack_sig_c_h |
| 75 |
* |
| 76 |
* Description: Bit-pack c and h component of sig = (c, z, h). |
| 77 |
* The z component is packed separately using mld_pack_sig_z. |
| 78 |
* |
| 79 |
* Arguments: - uint8_t sig[]: output byte array |
| 80 |
* - const uint8_t *c: pointer to challenge hash length |
| 81 |
* MLDSA_SEEDBYTES |
| 82 |
* - const mld_polyveck *h: pointer to hint vector h |
| 83 |
* - const unsigned int number_of_hints: total |
| 84 |
* hints in *h |
| 85 |
* |
| 86 |
* Note that the number_of_hints argument is not present |
| 87 |
* in the reference implementation. It is added here to ease |
| 88 |
* proof of type safety. |
| 89 |
**************************************************/ |
| 90 |
MLD_INTERNAL_API |
| 91 |
void mld_pack_sig_c_h(uint8_t sig[MLDSA_CRYPTO_BYTES], |
| 92 |
const uint8_t c[MLDSA_CTILDEBYTES], const mld_polyveck *h, |
| 93 |
const unsigned int number_of_hints) |
| 94 |
__contract__( |
| 95 |
requires(memory_no_alias(sig, MLDSA_CRYPTO_BYTES)) |
| 96 |
requires(memory_no_alias(c, MLDSA_CTILDEBYTES)) |
| 97 |
requires(memory_no_alias(h, sizeof(mld_polyveck))) |
| 98 |
requires(forall(k1, 0, MLDSA_K, |
| 99 |
array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2))) |
| 100 |
requires(number_of_hints <= MLDSA_OMEGA) |
| 101 |
assigns(memory_slice(sig, MLDSA_CRYPTO_BYTES)) |
| 102 |
); |
| 103 |
|
| 104 |
#define mld_pack_sig_z MLD_NAMESPACE_KL(pack_sig_z) |
| 105 |
/************************************************* |
| 106 |
* Name: mld_pack_sig_z |
| 107 |
* |
| 108 |
* Description: Bit-pack single polynomial of z component of sig = (c, z, h). |
| 109 |
* The c and h components are packed separately using |
| 110 |
* mld_pack_sig_c_h. |
| 111 |
* |
| 112 |
* Arguments: - uint8_t sig[]: output byte array |
| 113 |
* - const mld_poly *zi: pointer to a single polynomial in z |
| 114 |
* - const unsigned int i: index of zi in vector z |
| 115 |
* |
| 116 |
**************************************************/ |
| 117 |
MLD_INTERNAL_API |
| 118 |
void mld_pack_sig_z(uint8_t sig[MLDSA_CRYPTO_BYTES], const mld_poly *zi, |
| 119 |
unsigned i) |
| 120 |
__contract__( |
| 121 |
requires(memory_no_alias(sig, MLDSA_CRYPTO_BYTES)) |
| 122 |
requires(memory_no_alias(zi, sizeof(mld_poly))) |
| 123 |
requires(i < MLDSA_L) |
| 124 |
requires(array_bound(zi->coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1)) |
| 125 |
assigns(memory_slice(sig, MLDSA_CRYPTO_BYTES)) |
| 126 |
); |
| 127 |
|
| 128 |
#define mld_unpack_pk MLD_NAMESPACE_KL(unpack_pk) |
| 129 |
/************************************************* |
| 130 |
* Name: mld_unpack_pk |
| 131 |
* |
| 132 |
* Description: Unpack public key pk = (rho, t1). |
| 133 |
* |
| 134 |
* Arguments: - const uint8_t rho[]: output byte array for rho |
| 135 |
* - const mld_polyveck *t1: pointer to output vector t1 |
| 136 |
* - uint8_t pk[]: byte array containing bit-packed pk |
| 137 |
**************************************************/ |
| 138 |
MLD_INTERNAL_API |
| 139 |
void mld_unpack_pk(uint8_t rho[MLDSA_SEEDBYTES], mld_polyveck *t1, |
| 140 |
const uint8_t pk[MLDSA_CRYPTO_PUBLICKEYBYTES]) |
| 141 |
__contract__( |
| 142 |
requires(memory_no_alias(pk, MLDSA_CRYPTO_PUBLICKEYBYTES)) |
| 143 |
requires(memory_no_alias(rho, MLDSA_SEEDBYTES)) |
| 144 |
requires(memory_no_alias(t1, sizeof(mld_polyveck))) |
| 145 |
assigns(memory_slice(rho, MLDSA_SEEDBYTES)) |
| 146 |
assigns(memory_slice(t1, sizeof(mld_polyveck))) |
| 147 |
ensures(forall(k0, 0, MLDSA_K, |
| 148 |
array_bound(t1->vec[k0].coeffs, 0, MLDSA_N, 0, 1 << 10))) |
| 149 |
); |
| 150 |
|
| 151 |
|
| 152 |
#define mld_unpack_sk MLD_NAMESPACE_KL(unpack_sk) |
| 153 |
/************************************************* |
| 154 |
* Name: mld_unpack_sk |
| 155 |
* |
| 156 |
* Description: Unpack secret key sk = (rho, tr, key, t0, s1, s2). |
| 157 |
* |
| 158 |
* Arguments: - const uint8_t rho[]: output byte array for rho |
| 159 |
* - const uint8_t tr[]: output byte array for tr |
| 160 |
* - const uint8_t key[]: output byte array for key |
| 161 |
* - const mld_polyveck *t0: pointer to output vector t0 |
| 162 |
* - const mld_polyvecl *s1: pointer to output vector s1 |
| 163 |
* - const mld_polyveck *s2: pointer to output vector s2 |
| 164 |
* - uint8_t sk[]: byte array containing bit-packed sk |
| 165 |
**************************************************/ |
| 166 |
MLD_INTERNAL_API |
| 167 |
void mld_unpack_sk(uint8_t rho[MLDSA_SEEDBYTES], uint8_t tr[MLDSA_TRBYTES], |
| 168 |
uint8_t key[MLDSA_SEEDBYTES], mld_polyveck *t0, |
| 169 |
mld_polyvecl *s1, mld_polyveck *s2, |
| 170 |
const uint8_t sk[MLDSA_CRYPTO_SECRETKEYBYTES]) |
| 171 |
__contract__( |
| 172 |
requires(memory_no_alias(rho, MLDSA_SEEDBYTES)) |
| 173 |
requires(memory_no_alias(tr, MLDSA_TRBYTES)) |
| 174 |
requires(memory_no_alias(key, MLDSA_SEEDBYTES)) |
| 175 |
requires(memory_no_alias(t0, sizeof(mld_polyveck))) |
| 176 |
requires(memory_no_alias(s1, sizeof(mld_polyvecl))) |
| 177 |
requires(memory_no_alias(s2, sizeof(mld_polyveck))) |
| 178 |
requires(memory_no_alias(sk, MLDSA_CRYPTO_SECRETKEYBYTES)) |
| 179 |
assigns(memory_slice(rho, MLDSA_SEEDBYTES)) |
| 180 |
assigns(memory_slice(tr, MLDSA_TRBYTES)) |
| 181 |
assigns(memory_slice(key, MLDSA_SEEDBYTES)) |
| 182 |
assigns(memory_slice(t0, sizeof(mld_polyveck))) |
| 183 |
assigns(memory_slice(s1, sizeof(mld_polyvecl))) |
| 184 |
assigns(memory_slice(s2, sizeof(mld_polyveck))) |
| 185 |
ensures(forall(k0, 0, MLDSA_K, |
| 186 |
array_bound(t0->vec[k0].coeffs, 0, MLDSA_N, -(1<<(MLDSA_D-1)) + 1, (1<<(MLDSA_D-1)) + 1))) |
| 187 |
ensures(forall(k1, 0, MLDSA_L, |
| 188 |
array_bound(s1->vec[k1].coeffs, 0, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND, MLDSA_ETA + 1))) |
| 189 |
ensures(forall(k2, 0, MLDSA_K, |
| 190 |
array_bound(s2->vec[k2].coeffs, 0, MLDSA_N, MLD_POLYETA_UNPACK_LOWER_BOUND, MLDSA_ETA + 1))) |
| 191 |
); |
| 192 |
|
| 193 |
#define mld_unpack_sig MLD_NAMESPACE_KL(unpack_sig) |
| 194 |
/************************************************* |
| 195 |
* Name: mld_unpack_sig |
| 196 |
* |
| 197 |
* Description: Unpack signature sig = (c, z, h). |
| 198 |
* |
| 199 |
* Arguments: - uint8_t *c: pointer to output challenge hash |
| 200 |
* - mld_polyvecl *z: pointer to output vector z |
| 201 |
* - mld_polyveck *h: pointer to output hint vector h |
| 202 |
* - const uint8_t sig[]: byte array containing |
| 203 |
* bit-packed signature |
| 204 |
* |
| 205 |
* Returns 1 in case of malformed signature; otherwise 0. |
| 206 |
**************************************************/ |
| 207 |
MLD_INTERNAL_API |
| 208 |
MLD_MUST_CHECK_RETURN_VALUE |
| 209 |
int mld_unpack_sig(uint8_t c[MLDSA_CTILDEBYTES], mld_polyvecl *z, |
| 210 |
mld_polyveck *h, const uint8_t sig[MLDSA_CRYPTO_BYTES]) |
| 211 |
__contract__( |
| 212 |
requires(memory_no_alias(sig, MLDSA_CRYPTO_BYTES)) |
| 213 |
requires(memory_no_alias(c, MLDSA_CTILDEBYTES)) |
| 214 |
requires(memory_no_alias(z, sizeof(mld_polyvecl))) |
| 215 |
requires(memory_no_alias(h, sizeof(mld_polyveck))) |
| 216 |
assigns(memory_slice(c, MLDSA_CTILDEBYTES)) |
| 217 |
assigns(memory_slice(z, sizeof(mld_polyvecl))) |
| 218 |
assigns(memory_slice(h, sizeof(mld_polyveck))) |
| 219 |
ensures(forall(k0, 0, MLDSA_L, |
| 220 |
array_bound(z->vec[k0].coeffs, 0, MLDSA_N, -(MLDSA_GAMMA1 - 1), MLDSA_GAMMA1 + 1))) |
| 221 |
ensures(forall(k1, 0, MLDSA_K, |
| 222 |
array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2))) |
| 223 |
ensures(return_value >= 0 && return_value <= 1) |
| 224 |
); |
| 225 |
#endif /* !MLD_PACKING_H */ |