| 1 |
/* |
| 2 |
* Copyright (c) The mldsa-native project authors |
| 3 |
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
/* References |
| 7 |
* ========== |
| 8 |
* |
| 9 |
* - [FIPS204] |
| 10 |
* FIPS 204 Module-Lattice-Based Digital Signature Standard |
| 11 |
* National Institute of Standards and Technology |
| 12 |
* https://csrc.nist.gov/pubs/fips/204/final |
| 13 |
*/ |
| 14 |
|
| 15 |
#ifndef MLD_ROUNDING_H |
| 16 |
#define MLD_ROUNDING_H |
| 17 |
|
| 18 |
#include <stdint.h> |
| 19 |
#include "cbmc.h" |
| 20 |
#include "common.h" |
| 21 |
#include "ct.h" |
| 22 |
#include "debug.h" |
| 23 |
|
| 24 |
/* Parameter set namespacing |
| 25 |
* This is to facilitate building multiple instances |
| 26 |
* of mldsa-native (e.g. with varying parameter sets) |
| 27 |
* within a single compilation unit. */ |
| 28 |
#define mld_power2round MLD_ADD_PARAM_SET(mld_power2round) |
| 29 |
#define mld_decompose MLD_ADD_PARAM_SET(mld_decompose) |
| 30 |
#define mld_make_hint MLD_ADD_PARAM_SET(mld_make_hint) |
| 31 |
#define mld_use_hint MLD_ADD_PARAM_SET(mld_use_hint) |
| 32 |
/* End of parameter set namespacing */ |
| 33 |
|
| 34 |
#define MLD_2_POW_D (1 << MLDSA_D) |
| 35 |
|
| 36 |
/************************************************* |
| 37 |
* Name: mld_power2round |
| 38 |
* |
| 39 |
* Description: For finite field element a, compute a0, a1 such that |
| 40 |
* a mod^+ MLDSA_Q = a1*2^MLDSA_D + a0 with -2^{MLDSA_D-1} < a0 <= |
| 41 |
* 2^{MLDSA_D-1}. Assumes a to be standard representative. |
| 42 |
* |
| 43 |
* Arguments: - int32_t a: input element |
| 44 |
* - int32_t *a0: pointer to output element a0 |
| 45 |
* - int32_t *a1: pointer to output element a1 |
| 46 |
* |
| 47 |
* Reference: In the reference implementation, a1 is passed as a |
| 48 |
* return value instead. |
| 49 |
**************************************************/ |
| 50 |
static MLD_INLINE void mld_power2round(int32_t *a0, int32_t *a1, int32_t a) |
| 51 |
__contract__( |
| 52 |
requires(memory_no_alias(a0, sizeof(int32_t))) |
| 53 |
requires(memory_no_alias(a1, sizeof(int32_t))) |
| 54 |
requires(a >= 0 && a < MLDSA_Q) |
| 55 |
assigns(memory_slice(a0, sizeof(int32_t))) |
| 56 |
assigns(memory_slice(a1, sizeof(int32_t))) |
| 57 |
ensures(*a0 > -(MLD_2_POW_D/2) && *a0 <= (MLD_2_POW_D/2)) |
| 58 |
ensures(*a1 >= 0 && *a1 <= (MLDSA_Q - 1) / MLD_2_POW_D) |
| 59 |
ensures((*a1 * MLD_2_POW_D + *a0 - a) % MLDSA_Q == 0) |
| 60 |
) |
| 61 |
{ |
| 62 |
*a1 = (a + (1 << (MLDSA_D - 1)) - 1) >> MLDSA_D; |
| 63 |
*a0 = a - (*a1 << MLDSA_D); |
| 64 |
} |
| 65 |
|
| 66 |
/************************************************* |
| 67 |
* Name: mld_decompose |
| 68 |
* |
| 69 |
* Description: For finite field element a, compute high and low bits a0, a1 |
| 70 |
* such that a mod^+ MLDSA_Q = a1* 2 * MLDSA_GAMMA2 + a0 with |
| 71 |
* -MLDSA_GAMMA2 < a0 <= MLDSA_GAMMA2 except |
| 72 |
* if a1 = (MLDSA_Q-1)/(MLDSA_GAMMA2*2) where we set a1 = 0 and |
| 73 |
* -MLDSA_GAMMA2 <= a0 = a mod^+ MLDSA_Q - MLDSA_Q < 0. |
| 74 |
* Assumes a to be standard representative. |
| 75 |
* |
| 76 |
* Arguments: - int32_t a: input element |
| 77 |
* - int32_t *a0: pointer to output element a0 |
| 78 |
* - int32_t *a1: pointer to output element a1 |
| 79 |
* |
| 80 |
* Reference: a1 is passed as a return value instead |
| 81 |
**************************************************/ |
| 82 |
static MLD_INLINE void mld_decompose(int32_t *a0, int32_t *a1, int32_t a) |
| 83 |
__contract__( |
| 84 |
requires(memory_no_alias(a0, sizeof(int32_t))) |
| 85 |
requires(memory_no_alias(a1, sizeof(int32_t))) |
| 86 |
requires(a >= 0 && a < MLDSA_Q) |
| 87 |
assigns(memory_slice(a0, sizeof(int32_t))) |
| 88 |
assigns(memory_slice(a1, sizeof(int32_t))) |
| 89 |
/* a0 = -MLDSA_GAMMA2 can only occur when (q-1) = a - (a mod MLDSA_GAMMA2), |
| 90 |
* then a1=1; and a0 = a - (a mod MLDSA_GAMMA2) - 1 (@[FIPS204, Algorithm 36 (Decompose)]) */ |
| 91 |
ensures(*a0 >= -MLDSA_GAMMA2 && *a0 <= MLDSA_GAMMA2) |
| 92 |
ensures(*a1 >= 0 && *a1 < (MLDSA_Q-1)/(2*MLDSA_GAMMA2)) |
| 93 |
ensures((*a1 * 2 * MLDSA_GAMMA2 + *a0 - a) % MLDSA_Q == 0) |
| 94 |
) |
| 95 |
{ |
| 96 |
/* |
| 97 |
* The goal is to compute f1 = round-(f / (2*GAMMA2)), which can be computed |
| 98 |
* alternatively as round-(f / (128B)) = round-(ceil(f / 128) / B) where |
| 99 |
* B = 2*GAMMA2 / 128. Here round-() denotes "round half down". |
| 100 |
* |
| 101 |
* The equality round-(f / (128B)) = round-(ceil(f / 128) / B) can deduced |
| 102 |
* as follows. Since changing f to align-up(f, 128) can move f onto but not |
| 103 |
* across a rounding boundary for division by 128*B (note that we need B to be |
| 104 |
* even for this to work), and round- rounds down on the boundary, we have |
| 105 |
* |
| 106 |
* round-(f / (128B)) = round-(align-up(f, 128) / (128B)) |
| 107 |
* = round-((align-up(f, 128) / 128) / B) |
| 108 |
* = round-(ceil(f / 128) / B). |
| 109 |
*/ |
| 110 |
*a1 = (a + 127) >> 7; |
| 111 |
/* We know a >= 0 and a < MLDSA_Q, so... */ |
| 112 |
/* check-magic: 65472 == round((MLDSA_Q-1)/128) */ |
| 113 |
mld_assert(*a1 >= 0 && *a1 <= 65472); |
| 114 |
|
| 115 |
#if MLD_CONFIG_PARAMETER_SET == 44 |
| 116 |
/* check-magic: 1488 == 2 * intdiv(intdiv(MLDSA_Q - 1, 88), 128) */ |
| 117 |
/* check-magic: 11275 == floor(2**24 / 1488) */ |
| 118 |
/* |
| 119 |
* Compute f1 = round-(f1' / B) ≈ round(f1' * 11275 / 2^24). This is exact |
| 120 |
* for 0 <= f1' < 2^16. Note that half is rounded down since 11275 / 2^24 ≲ |
| 121 |
* 1 / 1488. |
| 122 |
*/ |
| 123 |
*a1 = (*a1 * 11275 + (1 << 23)) >> 24; |
| 124 |
mld_assert(*a1 >= 0 && *a1 <= 44); |
| 125 |
|
| 126 |
*a1 = mld_ct_sel_int32(0, *a1, mld_ct_cmask_neg_i32(43 - *a1)); |
| 127 |
mld_assert(*a1 >= 0 && *a1 <= 43); |
| 128 |
#else /* MLD_CONFIG_PARAMETER_SET == 44 */ |
| 129 |
/* check-magic: 4092 == 2 * intdiv(intdiv(MLDSA_Q - 1, 32), 128) */ |
| 130 |
/* check-magic: 1025 == floor(2**22 / 4092) */ |
| 131 |
/* |
| 132 |
* Compute f1 = round-(f1' / B) ≈ round(f1' * 1025 / 2^22). This is exact |
| 133 |
* for 0 <= f1' < 2^16. Note that half is rounded down since 1025 / 2^22 ≲ |
| 134 |
* 1 / 4092. |
| 135 |
*/ |
| 136 |
*a1 = (*a1 * 1025 + (1 << 21)) >> 22; |
| 137 |
mld_assert(*a1 >= 0 && *a1 <= 16); |
| 138 |
|
| 139 |
*a1 &= 15; |
| 140 |
mld_assert(*a1 >= 0 && *a1 <= 15); |
| 141 |
|
| 142 |
#endif /* MLD_CONFIG_PARAMETER_SET != 44 */ |
| 143 |
|
| 144 |
*a0 = a - *a1 * 2 * MLDSA_GAMMA2; |
| 145 |
*a0 = mld_ct_sel_int32(*a0 - MLDSA_Q, *a0, |
| 146 |
mld_ct_cmask_neg_i32((MLDSA_Q - 1) / 2 - *a0)); |
| 147 |
} |
| 148 |
|
| 149 |
/************************************************* |
| 150 |
* Name: mld_make_hint |
| 151 |
* |
| 152 |
* Description: Compute hint bit indicating whether the low bits of the |
| 153 |
* input element overflow into the high bits. |
| 154 |
* |
| 155 |
* Arguments: - int32_t a0: low bits of input element |
| 156 |
* - int32_t a1: high bits of input element |
| 157 |
* |
| 158 |
* Returns 1 if overflow, 0 otherwise |
| 159 |
**************************************************/ |
| 160 |
static MLD_INLINE unsigned int mld_make_hint(int32_t a0, int32_t a1) |
| 161 |
__contract__( |
| 162 |
ensures(return_value >= 0 && return_value <= 1) |
| 163 |
) |
| 164 |
{ |
| 165 |
if (a0 > MLDSA_GAMMA2 || a0 < -MLDSA_GAMMA2 || |
| 166 |
(a0 == -MLDSA_GAMMA2 && a1 != 0)) |
| 167 |
{ |
| 168 |
return 1; |
| 169 |
} |
| 170 |
|
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
/************************************************* |
| 175 |
* Name: mld_use_hint |
| 176 |
* |
| 177 |
* Description: Correct high bits according to hint. |
| 178 |
* |
| 179 |
* Arguments: - int32_t a: input element |
| 180 |
* - int32_t hint: hint bit |
| 181 |
* |
| 182 |
* Returns corrected high bits. |
| 183 |
**************************************************/ |
| 184 |
static MLD_INLINE int32_t mld_use_hint(int32_t a, int32_t hint) |
| 185 |
__contract__( |
| 186 |
requires(hint >= 0 && hint <= 1) |
| 187 |
requires(a >= 0 && a < MLDSA_Q) |
| 188 |
ensures(return_value >= 0 && return_value < (MLDSA_Q-1)/(2*MLDSA_GAMMA2)) |
| 189 |
) |
| 190 |
{ |
| 191 |
int32_t a0, a1; |
| 192 |
|
| 193 |
mld_decompose(&a0, &a1, a); |
| 194 |
if (hint == 0) |
| 195 |
{ |
| 196 |
return a1; |
| 197 |
} |
| 198 |
|
| 199 |
#if MLD_CONFIG_PARAMETER_SET == 44 |
| 200 |
if (a0 > 0) |
| 201 |
{ |
| 202 |
return (a1 == 43) ? 0 : a1 + 1; |
| 203 |
} |
| 204 |
else |
| 205 |
{ |
| 206 |
return (a1 == 0) ? 43 : a1 - 1; |
| 207 |
} |
| 208 |
#else /* MLD_CONFIG_PARAMETER_SET == 44 */ |
| 209 |
if (a0 > 0) |
| 210 |
{ |
| 211 |
return (a1 + 1) & 15; |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
return (a1 - 1) & 15; |
| 216 |
} |
| 217 |
#endif /* MLD_CONFIG_PARAMETER_SET != 44 */ |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
#endif /* !MLD_ROUNDING_H */ |