/src/openssl/crypto/ml_dsa/ml_dsa_poly.h
Line | Count | Source |
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 | | #if !defined(OSSL_LIBCRYPTO_ML_DSA_ML_DSA_POLY_H) |
10 | | #define OSSL_LIBCRYPTO_ML_DSA_ML_DSA_POLY_H |
11 | | |
12 | | #include <openssl/crypto.h> |
13 | | |
14 | | #include "internal/common.h" |
15 | | #include "ml_dsa_local.h" |
16 | | |
17 | 110M | #define ML_DSA_NUM_POLY_COEFFICIENTS 256 |
18 | | |
19 | | /* Polynomial object with 256 coefficients. The coefficients are unsigned 32 bits */ |
20 | | struct poly_st { |
21 | | #if defined(VX_COMPILER_SUPPORT_VEC128) |
22 | | ALIGN16 uint32_t coeff[ML_DSA_NUM_POLY_COEFFICIENTS]; |
23 | | #else |
24 | | uint32_t coeff[ML_DSA_NUM_POLY_COEFFICIENTS]; |
25 | | #endif |
26 | | }; |
27 | | |
28 | | static ossl_inline ossl_unused void |
29 | | poly_zero(POLY *p) |
30 | 3.75k | { |
31 | 3.75k | memset(p->coeff, 0, sizeof(*p)); |
32 | 3.75k | } 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 | 30 | 3.75k | { | 31 | 3.75k | memset(p->coeff, 0, sizeof(*p)); | 32 | 3.75k | } |
Unexecuted instantiation: ml_dsa_sign.c:poly_zero |
33 | | |
34 | | /** |
35 | | * @brief Polynomial addition. |
36 | | * |
37 | | * @param lhs A polynomial with coefficients in the range (0..q-1) |
38 | | * @param rhs A polynomial with coefficients in the range (0..q-1) to add |
39 | | * to the 'lhs'. |
40 | | * @param out The returned addition result with the coefficients all in the |
41 | | * range 0..q-1 |
42 | | */ |
43 | | static ossl_inline ossl_unused void |
44 | | poly_add(const POLY *lhs, const POLY *rhs, POLY *out) |
45 | 196k | { |
46 | 196k | int i; |
47 | | |
48 | 50.3M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) |
49 | 50.1M | out->coeff[i] = reduce_once(lhs->coeff[i] + rhs->coeff[i]); |
50 | 196k | } Unexecuted instantiation: ml_dsa_encoders.c:poly_add Line | Count | Source | 45 | 9.70k | { | 46 | 9.70k | int i; | 47 | | | 48 | 2.49M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) | 49 | 2.48M | out->coeff[i] = reduce_once(lhs->coeff[i] + rhs->coeff[i]); | 50 | 9.70k | } |
Line | Count | Source | 45 | 170k | { | 46 | 170k | int i; | 47 | | | 48 | 43.8M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) | 49 | 43.6M | out->coeff[i] = reduce_once(lhs->coeff[i] + rhs->coeff[i]); | 50 | 170k | } |
Unexecuted instantiation: ml_dsa_ntt.c:poly_add Unexecuted instantiation: ml_dsa_sample.c:poly_add Line | Count | Source | 45 | 15.6k | { | 46 | 15.6k | int i; | 47 | | | 48 | 4.03M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) | 49 | 4.01M | out->coeff[i] = reduce_once(lhs->coeff[i] + rhs->coeff[i]); | 50 | 15.6k | } |
|
51 | | |
52 | | /** |
53 | | * @brief Polynomial subtraction. |
54 | | * |
55 | | * @param lhs A polynomial with coefficients in the range (0..q-1) |
56 | | * @param rhs A polynomial with coefficients in the range (0..q-1) to subtract |
57 | | * from the 'lhs'. |
58 | | * @param out The returned subtraction result with the coefficients all in the |
59 | | * range 0..q-1 |
60 | | */ |
61 | | static ossl_inline ossl_unused void |
62 | | poly_sub(const POLY *lhs, const POLY *rhs, POLY *out) |
63 | 21.5k | { |
64 | 21.5k | int i; |
65 | | |
66 | 5.54M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) |
67 | 5.52M | out->coeff[i] = mod_sub(lhs->coeff[i], rhs->coeff[i]); |
68 | 21.5k | } 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 Line | Count | Source | 63 | 21.5k | { | 64 | 21.5k | int i; | 65 | | | 66 | 5.54M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) | 67 | 5.52M | out->coeff[i] = mod_sub(lhs->coeff[i], rhs->coeff[i]); | 68 | 21.5k | } |
|
69 | | |
70 | | /* @returns 1 if the polynomials are equal, or 0 otherwise */ |
71 | | static ossl_inline ossl_unused int |
72 | | poly_equal(const POLY *a, const POLY *b) |
73 | 2 | { |
74 | 2 | return CRYPTO_memcmp(a, b, sizeof(*a)) == 0; |
75 | 2 | } Unexecuted instantiation: ml_dsa_encoders.c:poly_equal Line | Count | Source | 73 | 2 | { | 74 | 2 | return CRYPTO_memcmp(a, b, sizeof(*a)) == 0; | 75 | 2 | } |
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 |
76 | | |
77 | | static ossl_inline ossl_unused void |
78 | | poly_ntt(POLY *p) |
79 | 3.75k | { |
80 | 3.75k | ossl_ml_dsa_poly_ntt(p); |
81 | 3.75k | } 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 Line | Count | Source | 79 | 3.75k | { | 80 | 3.75k | ossl_ml_dsa_poly_ntt(p); | 81 | 3.75k | } |
|
82 | | |
83 | | static ossl_inline ossl_unused int |
84 | | poly_sample_in_ball_ntt(POLY *out, const uint8_t *seed, int seed_len, |
85 | | EVP_MD_CTX *h_ctx, const EVP_MD *md, uint32_t tau) |
86 | 3.75k | { |
87 | 3.75k | if (!ossl_ml_dsa_poly_sample_in_ball(out, seed, seed_len, h_ctx, md, tau)) |
88 | 0 | return 0; |
89 | 3.75k | poly_ntt(out); |
90 | 3.75k | return 1; |
91 | 3.75k | } 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 | 86 | 3.75k | { | 87 | 3.75k | if (!ossl_ml_dsa_poly_sample_in_ball(out, seed, seed_len, h_ctx, md, tau)) | 88 | 0 | return 0; | 89 | 3.75k | poly_ntt(out); | 90 | 3.75k | return 1; | 91 | 3.75k | } |
|
92 | | |
93 | | static ossl_inline ossl_unused int |
94 | | poly_expand_mask(POLY *out, const uint8_t *seed, size_t seed_len, |
95 | | uint32_t gamma1, EVP_MD_CTX *h_ctx, const EVP_MD *md) |
96 | 5.86k | { |
97 | 5.86k | return ossl_ml_dsa_poly_expand_mask(out, seed, seed_len, gamma1, h_ctx, md); |
98 | 5.86k | } 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 ml_dsa_sample.c:poly_expand_mask Line | Count | Source | 96 | 5.86k | { | 97 | 5.86k | return ossl_ml_dsa_poly_expand_mask(out, seed, seed_len, gamma1, h_ctx, md); | 98 | 5.86k | } |
|
99 | | |
100 | | /** |
101 | | * @brief Decompose the coefficients of a polynomial into (r1, r0) such that |
102 | | * coeff[i] == t1[i] * 2^13 + t0[i] mod q |
103 | | * See FIPS 204, Algorithm 35, Power2Round() |
104 | | * |
105 | | * @param t A polynomial containing coefficients in the range 0..q-1 |
106 | | * @param t1 The returned polynomial containing coefficients that represent |
107 | | * the top 10 MSB of each coefficient in t (i.e each ranging from 0..1023) |
108 | | * @param t0 The remainder coefficients of t in the range (0..4096 or q-4095..q-1) |
109 | | * Each t0 coefficient has an effective range of 8192 (i.e. 13 bits). |
110 | | */ |
111 | | static ossl_inline ossl_unused void |
112 | | poly_power2_round(const POLY *t, POLY *t1, POLY *t0) |
113 | 9.70k | { |
114 | 9.70k | int i; |
115 | | |
116 | 2.49M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) |
117 | 2.48M | ossl_ml_dsa_key_compress_power2_round(t->coeff[i], |
118 | 2.48M | t1->coeff + i, t0->coeff + i); |
119 | 9.70k | } Unexecuted instantiation: ml_dsa_encoders.c:poly_power2_round ml_dsa_key.c:poly_power2_round Line | Count | Source | 113 | 9.70k | { | 114 | 9.70k | int i; | 115 | | | 116 | 2.49M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) | 117 | 2.48M | ossl_ml_dsa_key_compress_power2_round(t->coeff[i], | 118 | 2.48M | t1->coeff + i, t0->coeff + i); | 119 | 9.70k | } |
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 |
120 | | |
121 | | static ossl_inline ossl_unused void |
122 | | poly_scale_power2_round(POLY *in, POLY *out) |
123 | 3.91k | { |
124 | 3.91k | int i; |
125 | | |
126 | 1.00M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) |
127 | 1.00M | out->coeff[i] = (in->coeff[i] << ML_DSA_D_BITS); |
128 | 3.91k | } 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 | 123 | 3.91k | { | 124 | 3.91k | int i; | 125 | | | 126 | 1.00M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) | 127 | 1.00M | out->coeff[i] = (in->coeff[i] << ML_DSA_D_BITS); | 128 | 3.91k | } |
|
129 | | |
130 | | static ossl_inline ossl_unused void |
131 | | poly_high_bits(const POLY *in, uint32_t gamma2, POLY *out) |
132 | 17.6k | { |
133 | 17.6k | int i; |
134 | | |
135 | 4.53M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) |
136 | 4.51M | out->coeff[i] = ossl_ml_dsa_key_compress_high_bits(in->coeff[i], gamma2); |
137 | 17.6k | } 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 | 132 | 17.6k | { | 133 | 17.6k | int i; | 134 | | | 135 | 4.53M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) | 136 | 4.51M | out->coeff[i] = ossl_ml_dsa_key_compress_high_bits(in->coeff[i], gamma2); | 137 | 17.6k | } |
|
138 | | |
139 | | static ossl_inline ossl_unused void |
140 | | poly_low_bits(const POLY *in, uint32_t gamma2, POLY *out) |
141 | 17.6k | { |
142 | 17.6k | int i; |
143 | | |
144 | 4.53M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) |
145 | 4.51M | out->coeff[i] = ossl_ml_dsa_key_compress_low_bits(in->coeff[i], gamma2); |
146 | 17.6k | } 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 | 141 | 17.6k | { | 142 | 17.6k | int i; | 143 | | | 144 | 4.53M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) | 145 | 4.51M | out->coeff[i] = ossl_ml_dsa_key_compress_low_bits(in->coeff[i], gamma2); | 146 | 17.6k | } |
|
147 | | |
148 | | static ossl_inline ossl_unused void |
149 | | poly_make_hint(const POLY *ct0, const POLY *cs2, const POLY *w, uint32_t gamma2, |
150 | | POLY *out) |
151 | 3.92k | { |
152 | 3.92k | int i; |
153 | | |
154 | 1.00M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) |
155 | 1.00M | out->coeff[i] = ossl_ml_dsa_key_compress_make_hint(ct0->coeff[i], |
156 | 1.00M | cs2->coeff[i], |
157 | 1.00M | gamma2, w->coeff[i]); |
158 | 3.92k | } 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 | 151 | 3.92k | { | 152 | 3.92k | int i; | 153 | | | 154 | 1.00M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) | 155 | 1.00M | out->coeff[i] = ossl_ml_dsa_key_compress_make_hint(ct0->coeff[i], | 156 | 1.00M | cs2->coeff[i], | 157 | 1.00M | gamma2, w->coeff[i]); | 158 | 3.92k | } |
|
159 | | |
160 | | static ossl_inline ossl_unused void |
161 | | poly_use_hint(const POLY *h, const POLY *r, uint32_t gamma2, POLY *out) |
162 | 3.91k | { |
163 | 3.91k | int i; |
164 | | |
165 | 1.00M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) |
166 | 1.00M | out->coeff[i] = ossl_ml_dsa_key_compress_use_hint(h->coeff[i], |
167 | 1.00M | r->coeff[i], gamma2); |
168 | 3.91k | } 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 | 162 | 3.91k | { | 163 | 3.91k | int i; | 164 | | | 165 | 1.00M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) | 166 | 1.00M | out->coeff[i] = ossl_ml_dsa_key_compress_use_hint(h->coeff[i], | 167 | 1.00M | r->coeff[i], gamma2); | 168 | 3.91k | } |
|
169 | | |
170 | | static ossl_inline ossl_unused void |
171 | | poly_max(const POLY *p, uint32_t *mx) |
172 | 23.1k | { |
173 | 23.1k | int i; |
174 | | |
175 | 5.94M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) { |
176 | 5.92M | uint32_t c = p->coeff[i]; |
177 | 5.92M | uint32_t abs = abs_mod_prime(c); |
178 | | |
179 | 5.92M | *mx = maximum(*mx, abs); |
180 | 5.92M | } |
181 | 23.1k | } 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 Line | Count | Source | 172 | 23.1k | { | 173 | 23.1k | int i; | 174 | | | 175 | 5.94M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) { | 176 | 5.92M | uint32_t c = p->coeff[i]; | 177 | 5.92M | uint32_t abs = abs_mod_prime(c); | 178 | | | 179 | 5.92M | *mx = maximum(*mx, abs); | 180 | 5.92M | } | 181 | 23.1k | } |
|
182 | | |
183 | | static ossl_inline ossl_unused void |
184 | | poly_max_signed(const POLY *p, uint32_t *mx) |
185 | 17.6k | { |
186 | 17.6k | int i; |
187 | | |
188 | 4.53M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) { |
189 | 4.51M | uint32_t c = p->coeff[i]; |
190 | 4.51M | uint32_t abs = abs_signed(c); |
191 | | |
192 | 4.51M | *mx = maximum(*mx, abs); |
193 | 4.51M | } |
194 | 17.6k | } 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 | 185 | 17.6k | { | 186 | 17.6k | int i; | 187 | | | 188 | 4.53M | for (i = 0; i < ML_DSA_NUM_POLY_COEFFICIENTS; i++) { | 189 | 4.51M | uint32_t c = p->coeff[i]; | 190 | 4.51M | uint32_t abs = abs_signed(c); | 191 | | | 192 | 4.51M | *mx = maximum(*mx, abs); | 193 | 4.51M | } | 194 | 17.6k | } |
|
195 | | |
196 | | #endif /* !defined(OSSL_LIBCRYPTO_ML_DSA_ML_DSA_POLY_H) */ |