| 1 |
/* |
| 2 |
* Copyright (c) The mldsa-native project authors |
| 3 |
* SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT |
| 4 |
*/ |
| 5 |
#ifndef MLD_REDUCE_H |
| 6 |
#define MLD_REDUCE_H |
| 7 |
|
| 8 |
#include <stdint.h> |
| 9 |
#include "cbmc.h" |
| 10 |
#include "common.h" |
| 11 |
#include "ct.h" |
| 12 |
#include "debug.h" |
| 13 |
|
| 14 |
/* check-magic: -4186625 == pow(2,32,MLDSA_Q) */ |
| 15 |
#define MLD_MONT -4186625 |
| 16 |
|
| 17 |
/* Upper bound for domain of mld_reduce32() */ |
| 18 |
#define MLD_REDUCE32_DOMAIN_MAX (INT32_MAX - (1 << 22)) |
| 19 |
|
| 20 |
/* Absolute bound for range of mld_reduce32() */ |
| 21 |
/* check-magic: 6283009 == (MLD_REDUCE32_DOMAIN_MAX - 255 * MLDSA_Q + 1) */ |
| 22 |
#define MLD_REDUCE32_RANGE_MAX 6283009 |
| 23 |
|
| 24 |
/************************************************* |
| 25 |
* Name: mld_montgomery_reduce |
| 26 |
* |
| 27 |
* Description: Generic Montgomery reduction; given a 64-bit integer a, computes |
| 28 |
* 32-bit integer congruent to a * R^-1 mod q, where R=2^32 |
| 29 |
* |
| 30 |
* Arguments: - int64_t a: input integer to be reduced, of absolute value |
| 31 |
* smaller or equal to INT64_MAX - 2^31 * MLDSA_Q. |
| 32 |
* |
| 33 |
* Returns: Integer congruent to a * R^-1 modulo q, with absolute value |
| 34 |
* <= |a| / 2^32 + MLDSA_Q / 2 |
| 35 |
* |
| 36 |
* In particular, if |a| < 2^31 * MLDSA_Q, the absolute value |
| 37 |
* of the return value is < MLDSA_Q. |
| 38 |
**************************************************/ |
| 39 |
static MLD_INLINE int32_t mld_montgomery_reduce(int64_t a) |
| 40 |
__contract__( |
| 41 |
/* We don't attempt to express an input-dependent output bound |
| 42 |
* as the post-condition here, as all call-sites satisfy the |
| 43 |
* absolute input bound 2^31 * MLDSA_Q and higher-level |
| 44 |
* reasoning can be conducted using |return_value| < MLDSA_Q. */ |
| 45 |
requires(a > -(((int64_t)1 << 31) * MLDSA_Q) && |
| 46 |
a < (((int64_t)1 << 31) * MLDSA_Q)) |
| 47 |
ensures(return_value > -MLDSA_Q && return_value < MLDSA_Q) |
| 48 |
) |
| 49 |
{ |
| 50 |
/* check-magic: 58728449 == unsigned_mod(pow(MLDSA_Q, -1, 2^32), 2^32) */ |
| 51 |
const uint64_t QINV = 58728449; |
| 52 |
|
| 53 |
/* Compute a*q^{-1} mod 2^32 in unsigned representatives */ |
| 54 |
const uint32_t a_reduced = mld_cast_int64_to_uint32(a); |
| 55 |
const uint32_t a_inverted = (a_reduced * QINV) & UINT32_MAX; |
| 56 |
|
| 57 |
/* Lift to signed canonical representative mod 2^32. */ |
| 58 |
const int32_t t = mld_cast_uint32_to_int32(a_inverted); |
| 59 |
|
| 60 |
int64_t r; |
| 61 |
|
| 62 |
mld_assert(a < +(INT64_MAX - (((int64_t)1 << 31) * MLDSA_Q)) && |
| 63 |
a > -(INT64_MAX - (((int64_t)1 << 31) * MLDSA_Q))); |
| 64 |
|
| 65 |
r = a - (int64_t)t * MLDSA_Q; |
| 66 |
|
| 67 |
/* |
| 68 |
* PORTABILITY: Right-shift on a signed integer is, strictly-speaking, |
| 69 |
* implementation-defined for negative left argument. Here, |
| 70 |
* we assume it's sign-preserving "arithmetic" shift right. (C99 6.5.7 (5)) |
| 71 |
*/ |
| 72 |
r = r >> 32; |
| 73 |
|
| 74 |
/* Bounds: |
| 75 |
* |
| 76 |
* By construction of the Montgomery multiplication, by the time we |
| 77 |
* compute r >> 32, r is divisible by 2^32, and hence |
| 78 |
* |
| 79 |
* |r >> 32| = |r| / 2^32 |
| 80 |
* <= |a| / 2^32 + MLDSA_Q / 2 |
| 81 |
* |
| 82 |
* (In general, we would only have |x >> n| <= ceil(|x| / 2^n)). |
| 83 |
* |
| 84 |
* In particular, if |a| < 2^31 * MLDSA_Q, then |return_value| < MLDSA_Q. |
| 85 |
*/ |
| 86 |
return (int32_t)r; |
| 87 |
} |
| 88 |
|
| 89 |
/************************************************* |
| 90 |
* Name: mld_reduce32 |
| 91 |
* |
| 92 |
* Description: For finite field element a with a <= 2^{31} - 2^{22} - 1, |
| 93 |
* compute r \equiv a (mod MLDSA_Q) such that |
| 94 |
* -MLD_REDUCE32_RANGE_MAX <= r < MLD_REDUCE32_RANGE_MAX. |
| 95 |
* |
| 96 |
* Arguments: - int32_t: finite field element a |
| 97 |
* |
| 98 |
* Returns r. |
| 99 |
**************************************************/ |
| 100 |
static MLD_INLINE int32_t mld_reduce32(int32_t a) |
| 101 |
__contract__( |
| 102 |
requires(a <= MLD_REDUCE32_DOMAIN_MAX) |
| 103 |
ensures(return_value >= -MLD_REDUCE32_RANGE_MAX) |
| 104 |
ensures(return_value < MLD_REDUCE32_RANGE_MAX) |
| 105 |
) |
| 106 |
{ |
| 107 |
int32_t t; |
| 108 |
|
| 109 |
t = (a + (1 << 22)) >> 23; |
| 110 |
t = a - t * MLDSA_Q; |
| 111 |
mld_assert((t - a) % MLDSA_Q == 0); |
| 112 |
return t; |
| 113 |
} |
| 114 |
|
| 115 |
/************************************************* |
| 116 |
* Name: mld_caddq |
| 117 |
* |
| 118 |
* Description: Add MLDSA_Q if input coefficient is negative. |
| 119 |
* |
| 120 |
* Arguments: - int32_t: finite field element a |
| 121 |
* |
| 122 |
* Returns r. |
| 123 |
**************************************************/ |
| 124 |
static MLD_INLINE int32_t mld_caddq(int32_t a) |
| 125 |
__contract__( |
| 126 |
requires(a > -MLDSA_Q) |
| 127 |
requires(a < MLDSA_Q) |
| 128 |
ensures(return_value >= 0) |
| 129 |
ensures(return_value < MLDSA_Q) |
| 130 |
ensures(return_value == (a >= 0) ? a : (a + MLDSA_Q)) |
| 131 |
) |
| 132 |
{ |
| 133 |
return mld_ct_sel_int32(a + MLDSA_Q, a, mld_ct_cmask_neg_i32(a)); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
#endif /* !MLD_REDUCE_H */ |