Coverage Report

Created: 2025-08-11 07:04

/src/openssl35/crypto/ml_dsa/ml_dsa_poly.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2024-2025 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
#include <openssl/crypto.h>
10
11
85.9M
#define ML_DSA_NUM_POLY_COEFFICIENTS 256
12
13
/* Polynomial object with 256 coefficients. The coefficients are unsigned 32 bits */
14
struct poly_st {
15
    uint32_t coeff[ML_DSA_NUM_POLY_COEFFICIENTS];
16
};
17
18
static ossl_inline ossl_unused void
19
poly_zero(POLY *p)
20
1.68k
{
21
1.68k
    memset(p->coeff, 0, sizeof(*p));
22
1.68k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_zero
Unexecuted instantiation: ml_dsa_key.c:poly_zero
Unexecuted instantiation: ml_dsa_matrix.c:poly_zero
Unexecuted instantiation: ml_dsa_ntt.c:poly_zero
ml_dsa_sample.c:poly_zero
Line
Count
Source
20
1.68k
{
21
1.68k
    memset(p->coeff, 0, sizeof(*p));
22
1.68k
}
Unexecuted instantiation: ml_dsa_sign.c:poly_zero
23
24
/**
25
 * @brief Polynomial addition.
26
 *
27
 * @param lhs A polynomial with coefficients in the range (0..q-1)
28
 * @param rhs A polynomial with coefficients in the range (0..q-1) to add
29
 *            to the 'lhs'.
30
 * @param out The returned addition result with the coefficients all in the
31
 *            range 0..q-1
32
 */
33
static ossl_inline ossl_unused void
34
poly_add(const POLY *lhs, const POLY *rhs, POLY *out)
35
90.1k
{
36
90.1k
    int i;
37
38
23.1M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
39
23.0M
        out->coeff[i] = reduce_once(lhs->coeff[i] + rhs->coeff[i]);
40
90.1k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_add
ml_dsa_key.c:poly_add
Line
Count
Source
35
4.27k
{
36
4.27k
    int i;
37
38
1.09M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
39
1.09M
        out->coeff[i] = reduce_once(lhs->coeff[i] + rhs->coeff[i]);
40
4.27k
}
ml_dsa_matrix.c:poly_add
Line
Count
Source
35
78.7k
{
36
78.7k
    int i;
37
38
20.2M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
39
20.1M
        out->coeff[i] = reduce_once(lhs->coeff[i] + rhs->coeff[i]);
40
78.7k
}
Unexecuted instantiation: ml_dsa_ntt.c:poly_add
Unexecuted instantiation: ml_dsa_sample.c:poly_add
ml_dsa_sign.c:poly_add
Line
Count
Source
35
7.11k
{
36
7.11k
    int i;
37
38
1.82M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
39
1.82M
        out->coeff[i] = reduce_once(lhs->coeff[i] + rhs->coeff[i]);
40
7.11k
}
41
42
/**
43
 * @brief Polynomial subtraction.
44
 *
45
 * @param lhs A polynomial with coefficients in the range (0..q-1)
46
 * @param rhs A polynomial with coefficients in the range (0..q-1) to subtract
47
 *            from the 'lhs'.
48
 * @param out The returned subtraction result with the coefficients all in the
49
 *            range 0..q-1
50
 */
51
static ossl_inline ossl_unused void
52
poly_sub(const POLY *lhs, const POLY *rhs, POLY *out)
53
9.94k
{
54
9.94k
    int i;
55
56
2.55M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
57
2.54M
        out->coeff[i] = mod_sub(lhs->coeff[i], rhs->coeff[i]);
58
9.94k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_sub
Unexecuted instantiation: ml_dsa_key.c:poly_sub
Unexecuted instantiation: ml_dsa_matrix.c:poly_sub
Unexecuted instantiation: ml_dsa_ntt.c:poly_sub
Unexecuted instantiation: ml_dsa_sample.c:poly_sub
ml_dsa_sign.c:poly_sub
Line
Count
Source
53
9.94k
{
54
9.94k
    int i;
55
56
2.55M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
57
2.54M
        out->coeff[i] = mod_sub(lhs->coeff[i], rhs->coeff[i]);
58
9.94k
}
59
60
/* @returns 1 if the polynomials are equal, or 0 otherwise */
61
static ossl_inline ossl_unused int
62
poly_equal(const POLY *a, const POLY *b)
63
0
{
64
0
    return CRYPTO_memcmp(a, b, sizeof(*a)) == 0;
65
0
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_equal
Unexecuted instantiation: ml_dsa_key.c:poly_equal
Unexecuted instantiation: ml_dsa_matrix.c:poly_equal
Unexecuted instantiation: ml_dsa_ntt.c:poly_equal
Unexecuted instantiation: ml_dsa_sample.c:poly_equal
Unexecuted instantiation: ml_dsa_sign.c:poly_equal
66
67
static ossl_inline ossl_unused void
68
poly_ntt(POLY *p)
69
1.68k
{
70
1.68k
    ossl_ml_dsa_poly_ntt(p);
71
1.68k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_ntt
Unexecuted instantiation: ml_dsa_key.c:poly_ntt
Unexecuted instantiation: ml_dsa_matrix.c:poly_ntt
Unexecuted instantiation: ml_dsa_ntt.c:poly_ntt
Unexecuted instantiation: ml_dsa_sample.c:poly_ntt
ml_dsa_sign.c:poly_ntt
Line
Count
Source
69
1.68k
{
70
1.68k
    ossl_ml_dsa_poly_ntt(p);
71
1.68k
}
72
73
static ossl_inline ossl_unused int
74
poly_sample_in_ball_ntt(POLY *out, const uint8_t *seed, int seed_len,
75
                        EVP_MD_CTX *h_ctx, const EVP_MD *md, uint32_t tau)
76
1.68k
{
77
1.68k
    if (!ossl_ml_dsa_poly_sample_in_ball(out, seed, seed_len, h_ctx, md, tau))
78
0
        return 0;
79
1.68k
    poly_ntt(out);
80
1.68k
    return 1;
81
1.68k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_sample_in_ball_ntt
Unexecuted instantiation: ml_dsa_key.c:poly_sample_in_ball_ntt
Unexecuted instantiation: ml_dsa_matrix.c:poly_sample_in_ball_ntt
Unexecuted instantiation: ml_dsa_ntt.c:poly_sample_in_ball_ntt
Unexecuted instantiation: ml_dsa_sample.c:poly_sample_in_ball_ntt
ml_dsa_sign.c:poly_sample_in_ball_ntt
Line
Count
Source
76
1.68k
{
77
1.68k
    if (!ossl_ml_dsa_poly_sample_in_ball(out, seed, seed_len, h_ctx, md, tau))
78
0
        return 0;
79
1.68k
    poly_ntt(out);
80
1.68k
    return 1;
81
1.68k
}
82
83
static ossl_inline ossl_unused int
84
poly_expand_mask(POLY *out, const uint8_t *seed, size_t seed_len,
85
                 uint32_t gamma1, EVP_MD_CTX *h_ctx, const EVP_MD *md)
86
7.11k
{
87
7.11k
    return ossl_ml_dsa_poly_expand_mask(out, seed, seed_len, gamma1, h_ctx, md);
88
7.11k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_expand_mask
Unexecuted instantiation: ml_dsa_key.c:poly_expand_mask
Unexecuted instantiation: ml_dsa_matrix.c:poly_expand_mask
Unexecuted instantiation: ml_dsa_ntt.c:poly_expand_mask
Unexecuted instantiation: ml_dsa_sample.c:poly_expand_mask
ml_dsa_sign.c:poly_expand_mask
Line
Count
Source
86
7.11k
{
87
7.11k
    return ossl_ml_dsa_poly_expand_mask(out, seed, seed_len, gamma1, h_ctx, md);
88
7.11k
}
89
90
/**
91
 * @brief Decompose the coefficients of a polynomial into (r1, r0) such that
92
 * coeff[i] == t1[i] * 2^13 + t0[i] mod q
93
 * See FIPS 204, Algorithm 35, Power2Round()
94
 *
95
 * @param t A polynomial containing coefficients in the range 0..q-1
96
 * @param t1 The returned polynomial containing coefficients that represent
97
 *           the top 10 MSB of each coefficient in t (i.e each ranging from 0..1023)
98
 * @param t0 The remainder coefficients of t in the range (0..4096 or q-4095..q-1)
99
 *           Each t0 coefficient has an effective range of 8192 (i.e. 13 bits).
100
 */
101
static ossl_inline ossl_unused void
102
poly_power2_round(const POLY *t, POLY *t1, POLY *t0)
103
4.27k
{
104
4.27k
    int i;
105
106
1.09M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
107
1.09M
        ossl_ml_dsa_key_compress_power2_round(t->coeff[i],
108
1.09M
                                              t1->coeff + i, t0->coeff + i);
109
4.27k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_power2_round
ml_dsa_key.c:poly_power2_round
Line
Count
Source
103
4.27k
{
104
4.27k
    int i;
105
106
1.09M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
107
1.09M
        ossl_ml_dsa_key_compress_power2_round(t->coeff[i],
108
1.09M
                                              t1->coeff + i, t0->coeff + i);
109
4.27k
}
Unexecuted instantiation: ml_dsa_matrix.c:poly_power2_round
Unexecuted instantiation: ml_dsa_ntt.c:poly_power2_round
Unexecuted instantiation: ml_dsa_sample.c:poly_power2_round
Unexecuted instantiation: ml_dsa_sign.c:poly_power2_round
110
111
static ossl_inline ossl_unused void
112
poly_scale_power2_round(POLY *in, POLY *out)
113
1.88k
{
114
1.88k
    int i;
115
116
484k
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
117
482k
        out->coeff[i] = (in->coeff[i] << ML_DSA_D_BITS);
118
1.88k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_scale_power2_round
Unexecuted instantiation: ml_dsa_key.c:poly_scale_power2_round
Unexecuted instantiation: ml_dsa_matrix.c:poly_scale_power2_round
Unexecuted instantiation: ml_dsa_ntt.c:poly_scale_power2_round
Unexecuted instantiation: ml_dsa_sample.c:poly_scale_power2_round
ml_dsa_sign.c:poly_scale_power2_round
Line
Count
Source
113
1.88k
{
114
1.88k
    int i;
115
116
484k
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
117
482k
        out->coeff[i] = (in->coeff[i] << ML_DSA_D_BITS);
118
1.88k
}
119
120
static ossl_inline ossl_unused void
121
poly_high_bits(const POLY *in, uint32_t gamma2, POLY *out)
122
8.05k
{
123
8.05k
    int i;
124
125
2.07M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
126
2.06M
        out->coeff[i] = ossl_ml_dsa_key_compress_high_bits(in->coeff[i], gamma2);
127
8.05k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_high_bits
Unexecuted instantiation: ml_dsa_key.c:poly_high_bits
Unexecuted instantiation: ml_dsa_matrix.c:poly_high_bits
Unexecuted instantiation: ml_dsa_ntt.c:poly_high_bits
Unexecuted instantiation: ml_dsa_sample.c:poly_high_bits
ml_dsa_sign.c:poly_high_bits
Line
Count
Source
122
8.05k
{
123
8.05k
    int i;
124
125
2.07M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
126
2.06M
        out->coeff[i] = ossl_ml_dsa_key_compress_high_bits(in->coeff[i], gamma2);
127
8.05k
}
128
129
static ossl_inline ossl_unused void
130
poly_low_bits(const POLY *in, uint32_t gamma2, POLY *out)
131
8.05k
{
132
8.05k
    int i;
133
134
2.07M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
135
2.06M
        out->coeff[i] = ossl_ml_dsa_key_compress_low_bits(in->coeff[i], gamma2);
136
8.05k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_low_bits
Unexecuted instantiation: ml_dsa_key.c:poly_low_bits
Unexecuted instantiation: ml_dsa_matrix.c:poly_low_bits
Unexecuted instantiation: ml_dsa_ntt.c:poly_low_bits
Unexecuted instantiation: ml_dsa_sample.c:poly_low_bits
ml_dsa_sign.c:poly_low_bits
Line
Count
Source
131
8.05k
{
132
8.05k
    int i;
133
134
2.07M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
135
2.06M
        out->coeff[i] = ossl_ml_dsa_key_compress_low_bits(in->coeff[i], gamma2);
136
8.05k
}
137
138
static ossl_inline ossl_unused void
139
poly_make_hint(const POLY *ct0, const POLY *cs2, const POLY *w, uint32_t gamma2,
140
               POLY *out)
141
1.90k
{
142
1.90k
    int i;
143
144
488k
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
145
486k
        out->coeff[i] = ossl_ml_dsa_key_compress_make_hint(ct0->coeff[i],
146
486k
                                                           cs2->coeff[i],
147
486k
                                                           gamma2, w->coeff[i]);
148
1.90k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_make_hint
Unexecuted instantiation: ml_dsa_key.c:poly_make_hint
Unexecuted instantiation: ml_dsa_matrix.c:poly_make_hint
Unexecuted instantiation: ml_dsa_ntt.c:poly_make_hint
Unexecuted instantiation: ml_dsa_sample.c:poly_make_hint
ml_dsa_sign.c:poly_make_hint
Line
Count
Source
141
1.90k
{
142
1.90k
    int i;
143
144
488k
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
145
486k
        out->coeff[i] = ossl_ml_dsa_key_compress_make_hint(ct0->coeff[i],
146
486k
                                                           cs2->coeff[i],
147
486k
                                                           gamma2, w->coeff[i]);
148
1.90k
}
149
150
static ossl_inline ossl_unused void
151
poly_use_hint(const POLY *h, const POLY *r, uint32_t gamma2, POLY *out)
152
1.88k
{
153
1.88k
    int i;
154
155
484k
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
156
482k
        out->coeff[i] = ossl_ml_dsa_key_compress_use_hint(h->coeff[i],
157
482k
                                                          r->coeff[i], gamma2);
158
1.88k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_use_hint
Unexecuted instantiation: ml_dsa_key.c:poly_use_hint
Unexecuted instantiation: ml_dsa_matrix.c:poly_use_hint
Unexecuted instantiation: ml_dsa_ntt.c:poly_use_hint
Unexecuted instantiation: ml_dsa_sample.c:poly_use_hint
ml_dsa_sign.c:poly_use_hint
Line
Count
Source
152
1.88k
{
153
1.88k
    int i;
154
155
484k
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++)
156
482k
        out->coeff[i] = ossl_ml_dsa_key_compress_use_hint(h->coeff[i],
157
482k
                                                          r->coeff[i], gamma2);
158
1.88k
}
159
160
static ossl_inline ossl_unused void
161
poly_max(const POLY *p, uint32_t *mx)
162
10.6k
{
163
10.6k
    int i;
164
165
2.74M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) {
166
2.73M
        uint32_t c = p->coeff[i];
167
2.73M
        uint32_t abs = abs_mod_prime(c);
168
169
2.73M
        *mx = maximum(*mx, abs);
170
2.73M
    }
171
10.6k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_max
Unexecuted instantiation: ml_dsa_key.c:poly_max
Unexecuted instantiation: ml_dsa_matrix.c:poly_max
Unexecuted instantiation: ml_dsa_ntt.c:poly_max
Unexecuted instantiation: ml_dsa_sample.c:poly_max
ml_dsa_sign.c:poly_max
Line
Count
Source
162
10.6k
{
163
10.6k
    int i;
164
165
2.74M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) {
166
2.73M
        uint32_t c = p->coeff[i];
167
2.73M
        uint32_t abs = abs_mod_prime(c);
168
169
2.73M
        *mx = maximum(*mx, abs);
170
2.73M
    }
171
10.6k
}
172
173
static ossl_inline ossl_unused void
174
poly_max_signed(const POLY *p, uint32_t *mx)
175
8.05k
{
176
8.05k
    int i;
177
178
2.07M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) {
179
2.06M
        uint32_t c = p->coeff[i];
180
2.06M
        uint32_t abs = abs_signed(c);
181
182
2.06M
        *mx = maximum(*mx, abs);
183
2.06M
    }
184
8.05k
}
Unexecuted instantiation: ml_dsa_encoders.c:poly_max_signed
Unexecuted instantiation: ml_dsa_key.c:poly_max_signed
Unexecuted instantiation: ml_dsa_matrix.c:poly_max_signed
Unexecuted instantiation: ml_dsa_ntt.c:poly_max_signed
Unexecuted instantiation: ml_dsa_sample.c:poly_max_signed
ml_dsa_sign.c:poly_max_signed
Line
Count
Source
175
8.05k
{
176
8.05k
    int i;
177
178
2.07M
    for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) {
179
2.06M
        uint32_t c = p->coeff[i];
180
2.06M
        uint32_t abs = abs_signed(c);
181
182
2.06M
        *mx = maximum(*mx, abs);
183
2.06M
    }
184
8.05k
}