Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/ec/curve448/field.h
Line
Count
Source
1
/*
2
 * Copyright 2017-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright 2014 Cryptography Research, Inc.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 *
10
 * Originally written by Mike Hamburg
11
 */
12
13
#ifndef OSSL_CRYPTO_EC_CURVE448_FIELD_H
14
#define OSSL_CRYPTO_EC_CURVE448_FIELD_H
15
16
#include "internal/constant_time.h"
17
#include <string.h>
18
#include <assert.h>
19
#include "word.h"
20
21
12.0M
#define NLIMBS (64 / sizeof(word_t))
22
20.3k
#define X_SER_BYTES 56
23
250
#define SER_BYTES 56
24
25
#if defined(__GNUC__) || defined(__clang__)
26
#define INLINE_UNUSED __inline__ __attribute__((__unused__, __always_inline__))
27
#define RESTRICT __restrict__
28
#define ALIGNED __attribute__((__aligned__(16)))
29
#else
30
#define INLINE_UNUSED ossl_inline
31
#define RESTRICT
32
#define ALIGNED
33
#endif
34
35
typedef struct gf_s {
36
    word_t limb[NLIMBS];
37
} ALIGNED gf_s, gf[1];
38
39
/* RFC 7748 support */
40
590
#define X_PUBLIC_BYTES X_SER_BYTES
41
590
#define X_PRIVATE_BYTES X_PUBLIC_BYTES
42
14.7k
#define X_PRIVATE_BITS 448
43
44
static INLINE_UNUSED void gf_copy(gf out, const gf a)
45
10.3k
{
46
10.3k
    *out = *a;
47
10.3k
}
curve448.c:gf_copy
Line
Count
Source
45
9.20k
{
46
9.20k
    *out = *a;
47
9.20k
}
Unexecuted instantiation: curve448_tables.c:gf_copy
Unexecuted instantiation: eddsa.c:gf_copy
f_generic.c:gf_copy
Line
Count
Source
45
1.13k
{
46
1.13k
    *out = *a;
47
1.13k
}
Unexecuted instantiation: scalar.c:gf_copy
Unexecuted instantiation: f_impl64.c:gf_copy
48
49
static INLINE_UNUSED void gf_add_RAW(gf out, const gf a, const gf b);
50
static INLINE_UNUSED void gf_sub_RAW(gf out, const gf a, const gf b);
51
static INLINE_UNUSED void gf_bias(gf inout, int amount);
52
static INLINE_UNUSED void gf_weak_reduce(gf inout);
53
54
void gf_strong_reduce(gf inout);
55
void gf_add(gf out, const gf a, const gf b);
56
void gf_sub(gf out, const gf a, const gf b);
57
void ossl_gf_mul(gf_s *RESTRICT out, const gf a, const gf b);
58
void ossl_gf_mulw_unsigned(gf_s *RESTRICT out, const gf a, uint32_t b);
59
void ossl_gf_sqr(gf_s *RESTRICT out, const gf a);
60
mask_t gf_isr(gf a, const gf x); /** a^2 x = 1, QNR, or 0 if x=0.  Return true if successful */
61
mask_t gf_eq(const gf x, const gf y);
62
mask_t gf_lobit(const gf x);
63
mask_t gf_hibit(const gf x);
64
65
void gf_serialize(uint8_t serial[SER_BYTES], const gf x, int with_highbit);
66
mask_t gf_deserialize(gf x, const uint8_t serial[SER_BYTES], int with_hibit,
67
    uint8_t hi_nmask);
68
69
/* clang-format off */
70
111k
#define LIMBPERM(i) (i)
71
#if (ARCH_WORD_BITS == 32)
72
#define GF_HEADROOM 2
73
#define LIMB(x) ((x) & ((1 << 28) - 1)), ((x) >> 28)
74
#define FIELD_LITERAL(a, b, c, d, e, f, g, h)                                      \
75
    {                                                                              \
76
        {                                                                          \
77
            LIMB(a), LIMB(b), LIMB(c), LIMB(d), LIMB(e), LIMB(f), LIMB(g), LIMB(h) \
78
        }                                                                          \
79
    }
80
81
#define LIMB_PLACE_VALUE(i) 28
82
83
void gf_add_RAW(gf out, const gf a, const gf b)
84
{
85
    unsigned int i;
86
87
    for (i = 0; i < NLIMBS; i++)
88
        out->limb[i] = a->limb[i] + b->limb[i];
89
}
90
91
void gf_sub_RAW(gf out, const gf a, const gf b)
92
{
93
    unsigned int i;
94
95
    for (i = 0; i < NLIMBS; i++)
96
        out->limb[i] = a->limb[i] - b->limb[i];
97
}
98
99
void gf_bias(gf a, int amt)
100
{
101
    unsigned int i;
102
    uint32_t co1 = ((1 << 28) - 1) * amt, co2 = co1 - amt;
103
104
    for (i = 0; i < NLIMBS; i++)
105
        a->limb[i] += (i == NLIMBS / 2) ? co2 : co1;
106
}
107
108
void gf_weak_reduce(gf a)
109
{
110
    uint32_t mask = (1 << 28) - 1;
111
    uint32_t tmp = a->limb[NLIMBS - 1] >> 28;
112
    unsigned int i;
113
114
    a->limb[NLIMBS / 2] += tmp;
115
    for (i = NLIMBS - 1; i > 0; i--)
116
        a->limb[i] = (a->limb[i] & mask) + (a->limb[i - 1] >> 28);
117
    a->limb[0] = (a->limb[0] & mask) + tmp;
118
}
119
#define LIMB_MASK(i) (((1) << LIMB_PLACE_VALUE(i)) - 1)
120
#elif (ARCH_WORD_BITS == 64)
121
374k
#define GF_HEADROOM 9999 /* Everything is reduced anyway */
122
#define FIELD_LITERAL(a, b, c, d, e, f, g, h) \
123
    {                                         \
124
        {                                     \
125
            a, b, c, d, e, f, g, h            \
126
        }                                     \
127
    }
128
129
102k
#define LIMB_PLACE_VALUE(i) 56
130
131
void gf_add_RAW(gf out, const gf a, const gf b)
132
327k
{
133
327k
    unsigned int i;
134
135
2.94M
    for (i = 0; i < NLIMBS; i++)
136
2.61M
        out->limb[i] = a->limb[i] + b->limb[i];
137
138
327k
    gf_weak_reduce(out);
139
327k
}
curve448.c:gf_add_RAW
Line
Count
Source
132
324k
{
133
324k
    unsigned int i;
134
135
2.91M
    for (i = 0; i < NLIMBS; i++)
136
2.59M
        out->limb[i] = a->limb[i] + b->limb[i];
137
138
324k
    gf_weak_reduce(out);
139
324k
}
Unexecuted instantiation: curve448_tables.c:gf_add_RAW
Unexecuted instantiation: eddsa.c:gf_add_RAW
f_generic.c:gf_add_RAW
Line
Count
Source
132
3.16k
{
133
3.16k
    unsigned int i;
134
135
28.4k
    for (i = 0; i < NLIMBS; i++)
136
25.3k
        out->limb[i] = a->limb[i] + b->limb[i];
137
138
3.16k
    gf_weak_reduce(out);
139
3.16k
}
Unexecuted instantiation: scalar.c:gf_add_RAW
Unexecuted instantiation: f_impl64.c:gf_add_RAW
140
141
void gf_sub_RAW(gf out, const gf a, const gf b)
142
357k
{
143
357k
    uint64_t co1 = ((1ULL << 56) - 1) * 2, co2 = co1 - 2;
144
357k
    unsigned int i;
145
146
3.21M
    for (i = 0; i < NLIMBS; i++)
147
2.86M
        out->limb[i] = a->limb[i] - b->limb[i] + ((i == NLIMBS / 2) ? co2 : co1);
148
149
357k
    gf_weak_reduce(out);
150
357k
}
curve448.c:gf_sub_RAW
Line
Count
Source
142
324k
{
143
324k
    uint64_t co1 = ((1ULL << 56) - 1) * 2, co2 = co1 - 2;
144
324k
    unsigned int i;
145
146
2.91M
    for (i = 0; i < NLIMBS; i++)
147
2.59M
        out->limb[i] = a->limb[i] - b->limb[i] + ((i == NLIMBS / 2) ? co2 : co1);
148
149
324k
    gf_weak_reduce(out);
150
324k
}
Unexecuted instantiation: curve448_tables.c:gf_sub_RAW
Unexecuted instantiation: eddsa.c:gf_sub_RAW
f_generic.c:gf_sub_RAW
Line
Count
Source
142
33.5k
{
143
33.5k
    uint64_t co1 = ((1ULL << 56) - 1) * 2, co2 = co1 - 2;
144
33.5k
    unsigned int i;
145
146
301k
    for (i = 0; i < NLIMBS; i++)
147
268k
        out->limb[i] = a->limb[i] - b->limb[i] + ((i == NLIMBS / 2) ? co2 : co1);
148
149
33.5k
    gf_weak_reduce(out);
150
33.5k
}
Unexecuted instantiation: scalar.c:gf_sub_RAW
Unexecuted instantiation: f_impl64.c:gf_sub_RAW
151
152
void gf_bias(gf a, int amt)
153
357k
{
154
357k
}
curve448.c:gf_bias
Line
Count
Source
153
324k
{
154
324k
}
Unexecuted instantiation: curve448_tables.c:gf_bias
Unexecuted instantiation: eddsa.c:gf_bias
f_generic.c:gf_bias
Line
Count
Source
153
33.5k
{
154
33.5k
}
Unexecuted instantiation: scalar.c:gf_bias
Unexecuted instantiation: f_impl64.c:gf_bias
155
156
void gf_weak_reduce(gf a)
157
723k
{
158
723k
    uint64_t mask = (1ULL << 56) - 1;
159
723k
    uint64_t tmp = a->limb[NLIMBS - 1] >> 56;
160
723k
    unsigned int i;
161
162
723k
    a->limb[NLIMBS / 2] += tmp;
163
5.78M
    for (i = NLIMBS - 1; i > 0; i--)
164
5.06M
        a->limb[i] = (a->limb[i] & mask) + (a->limb[i - 1] >> 56);
165
723k
    a->limb[0] = (a->limb[0] & mask) + tmp;
166
723k
}
curve448.c:gf_weak_reduce
Line
Count
Source
157
648k
{
158
648k
    uint64_t mask = (1ULL << 56) - 1;
159
648k
    uint64_t tmp = a->limb[NLIMBS - 1] >> 56;
160
648k
    unsigned int i;
161
162
648k
    a->limb[NLIMBS / 2] += tmp;
163
5.18M
    for (i = NLIMBS - 1; i > 0; i--)
164
4.53M
        a->limb[i] = (a->limb[i] & mask) + (a->limb[i - 1] >> 56);
165
648k
    a->limb[0] = (a->limb[0] & mask) + tmp;
166
648k
}
Unexecuted instantiation: curve448_tables.c:gf_weak_reduce
Unexecuted instantiation: eddsa.c:gf_weak_reduce
f_generic.c:gf_weak_reduce
Line
Count
Source
157
75.2k
{
158
75.2k
    uint64_t mask = (1ULL << 56) - 1;
159
75.2k
    uint64_t tmp = a->limb[NLIMBS - 1] >> 56;
160
75.2k
    unsigned int i;
161
162
75.2k
    a->limb[NLIMBS / 2] += tmp;
163
602k
    for (i = NLIMBS - 1; i > 0; i--)
164
526k
        a->limb[i] = (a->limb[i] & mask) + (a->limb[i - 1] >> 56);
165
75.2k
    a->limb[0] = (a->limb[0] & mask) + tmp;
166
75.2k
}
Unexecuted instantiation: scalar.c:gf_weak_reduce
Unexecuted instantiation: f_impl64.c:gf_weak_reduce
167
32.5k
#define LIMB_MASK(i) (((1ULL) << LIMB_PLACE_VALUE(i)) - 1)
168
#endif
169
/* clang-format on */
170
171
static const gf ZERO = { { { 0 } } }, ONE = { { { 1 } } };
172
173
/* Square x, n times. */
174
static ossl_inline void gf_sqrn(gf_s *RESTRICT y, const gf x, int n)
175
4.48k
{
176
4.48k
    gf tmp;
177
178
4.48k
    assert(n > 0);
179
4.48k
    if (n & 1) {
180
3.92k
        ossl_gf_sqr(y, x);
181
3.92k
        n--;
182
3.92k
    } else {
183
560
        ossl_gf_sqr(tmp, x);
184
560
        ossl_gf_sqr(y, tmp);
185
560
        n -= 2;
186
560
    }
187
125k
    for (; n; n -= 2) {
188
120k
        ossl_gf_sqr(tmp, y);
189
120k
        ossl_gf_sqr(y, tmp);
190
120k
    }
191
4.48k
}
Unexecuted instantiation: curve448.c:gf_sqrn
Unexecuted instantiation: curve448_tables.c:gf_sqrn
Unexecuted instantiation: eddsa.c:gf_sqrn
f_generic.c:gf_sqrn
Line
Count
Source
175
4.48k
{
176
4.48k
    gf tmp;
177
178
4.48k
    assert(n > 0);
179
4.48k
    if (n & 1) {
180
3.92k
        ossl_gf_sqr(y, x);
181
3.92k
        n--;
182
3.92k
    } else {
183
560
        ossl_gf_sqr(tmp, x);
184
560
        ossl_gf_sqr(y, tmp);
185
560
        n -= 2;
186
560
    }
187
125k
    for (; n; n -= 2) {
188
120k
        ossl_gf_sqr(tmp, y);
189
120k
        ossl_gf_sqr(y, tmp);
190
120k
    }
191
4.48k
}
Unexecuted instantiation: scalar.c:gf_sqrn
Unexecuted instantiation: f_impl64.c:gf_sqrn
192
193
324k
#define gf_add_nr gf_add_RAW
194
195
/* Subtract mod p.  Bias by 2 and don't reduce  */
196
static ossl_inline void gf_sub_nr(gf c, const gf a, const gf b)
197
223k
{
198
223k
    gf_sub_RAW(c, a, b);
199
223k
    gf_bias(c, 2);
200
223k
    if (GF_HEADROOM < 3)
201
0
        gf_weak_reduce(c);
202
223k
}
curve448.c:gf_sub_nr
Line
Count
Source
197
223k
{
198
223k
    gf_sub_RAW(c, a, b);
199
223k
    gf_bias(c, 2);
200
223k
    if (GF_HEADROOM < 3)
201
0
        gf_weak_reduce(c);
202
223k
}
Unexecuted instantiation: curve448_tables.c:gf_sub_nr
Unexecuted instantiation: eddsa.c:gf_sub_nr
Unexecuted instantiation: f_generic.c:gf_sub_nr
Unexecuted instantiation: scalar.c:gf_sub_nr
Unexecuted instantiation: f_impl64.c:gf_sub_nr
203
204
/* Subtract mod p. Bias by amt but don't reduce.  */
205
static ossl_inline void gf_subx_nr(gf c, const gf a, const gf b, int amt)
206
100k
{
207
100k
    gf_sub_RAW(c, a, b);
208
100k
    gf_bias(c, amt);
209
100k
    if (GF_HEADROOM < amt + 1)
210
0
        gf_weak_reduce(c);
211
100k
}
curve448.c:gf_subx_nr
Line
Count
Source
206
100k
{
207
100k
    gf_sub_RAW(c, a, b);
208
100k
    gf_bias(c, amt);
209
100k
    if (GF_HEADROOM < amt + 1)
210
0
        gf_weak_reduce(c);
211
100k
}
Unexecuted instantiation: curve448_tables.c:gf_subx_nr
Unexecuted instantiation: eddsa.c:gf_subx_nr
Unexecuted instantiation: f_generic.c:gf_subx_nr
Unexecuted instantiation: scalar.c:gf_subx_nr
Unexecuted instantiation: f_impl64.c:gf_subx_nr
212
213
/* Mul by signed int.  Not constant-time WRT the sign of that int. */
214
static ossl_inline void gf_mulw(gf c, const gf a, int32_t w)
215
15.6k
{
216
15.6k
    if (w > 0) {
217
14.3k
        ossl_gf_mulw_unsigned(c, a, w);
218
14.3k
    } else {
219
1.35k
        ossl_gf_mulw_unsigned(c, a, -w);
220
1.35k
        gf_sub(c, ZERO, c);
221
1.35k
    }
222
15.6k
}
curve448.c:gf_mulw
Line
Count
Source
215
15.6k
{
216
15.6k
    if (w > 0) {
217
14.3k
        ossl_gf_mulw_unsigned(c, a, w);
218
14.3k
    } else {
219
1.35k
        ossl_gf_mulw_unsigned(c, a, -w);
220
1.35k
        gf_sub(c, ZERO, c);
221
1.35k
    }
222
15.6k
}
Unexecuted instantiation: curve448_tables.c:gf_mulw
Unexecuted instantiation: eddsa.c:gf_mulw
Unexecuted instantiation: f_generic.c:gf_mulw
Unexecuted instantiation: scalar.c:gf_mulw
Unexecuted instantiation: f_impl64.c:gf_mulw
223
224
/* Constant time, x = is_z ? z : y */
225
static ossl_inline void gf_cond_sel(gf x, const gf y, const gf z, mask_t is_z)
226
28.1k
{
227
28.1k
    size_t i;
228
229
253k
    for (i = 0; i < NLIMBS; i++) {
230
#if ARCH_WORD_BITS == 32
231
        x[0].limb[i] = constant_time_select_32(is_z, z[0].limb[i],
232
            y[0].limb[i]);
233
#else
234
        /* Must be 64 bit */
235
224k
        x[0].limb[i] = constant_time_select_64(is_z, z[0].limb[i],
236
224k
            y[0].limb[i]);
237
224k
#endif
238
224k
    }
239
28.1k
}
curve448.c:gf_cond_sel
Line
Count
Source
226
28.1k
{
227
28.1k
    size_t i;
228
229
253k
    for (i = 0; i < NLIMBS; i++) {
230
#if ARCH_WORD_BITS == 32
231
        x[0].limb[i] = constant_time_select_32(is_z, z[0].limb[i],
232
            y[0].limb[i]);
233
#else
234
        /* Must be 64 bit */
235
224k
        x[0].limb[i] = constant_time_select_64(is_z, z[0].limb[i],
236
224k
            y[0].limb[i]);
237
224k
#endif
238
224k
    }
239
28.1k
}
Unexecuted instantiation: curve448_tables.c:gf_cond_sel
Unexecuted instantiation: eddsa.c:gf_cond_sel
Unexecuted instantiation: f_generic.c:gf_cond_sel
Unexecuted instantiation: scalar.c:gf_cond_sel
Unexecuted instantiation: f_impl64.c:gf_cond_sel
240
241
/* Constant time, if (neg) x=-x; */
242
static ossl_inline void gf_cond_neg(gf x, mask_t neg)
243
28.1k
{
244
28.1k
    gf y;
245
246
28.1k
    gf_sub(y, ZERO, x);
247
28.1k
    gf_cond_sel(x, x, y, neg);
248
28.1k
}
curve448.c:gf_cond_neg
Line
Count
Source
243
28.1k
{
244
28.1k
    gf y;
245
246
28.1k
    gf_sub(y, ZERO, x);
247
28.1k
    gf_cond_sel(x, x, y, neg);
248
28.1k
}
Unexecuted instantiation: curve448_tables.c:gf_cond_neg
Unexecuted instantiation: eddsa.c:gf_cond_neg
Unexecuted instantiation: f_generic.c:gf_cond_neg
Unexecuted instantiation: scalar.c:gf_cond_neg
Unexecuted instantiation: f_impl64.c:gf_cond_neg
249
250
/* Constant time, if (swap) (x,y) = (y,x); */
251
static ossl_inline void gf_cond_swap(gf x, gf_s *RESTRICT y, mask_t swap)
252
56.6k
{
253
56.6k
    size_t i;
254
255
509k
    for (i = 0; i < NLIMBS; i++) {
256
#if ARCH_WORD_BITS == 32
257
        constant_time_cond_swap_32(swap, &(x[0].limb[i]), &(y->limb[i]));
258
#else
259
        /* Must be 64 bit */
260
453k
        constant_time_cond_swap_64(swap, &(x[0].limb[i]), &(y->limb[i]));
261
453k
#endif
262
453k
    }
263
56.6k
}
curve448.c:gf_cond_swap
Line
Count
Source
252
56.6k
{
253
56.6k
    size_t i;
254
255
509k
    for (i = 0; i < NLIMBS; i++) {
256
#if ARCH_WORD_BITS == 32
257
        constant_time_cond_swap_32(swap, &(x[0].limb[i]), &(y->limb[i]));
258
#else
259
        /* Must be 64 bit */
260
453k
        constant_time_cond_swap_64(swap, &(x[0].limb[i]), &(y->limb[i]));
261
453k
#endif
262
453k
    }
263
56.6k
}
Unexecuted instantiation: curve448_tables.c:gf_cond_swap
Unexecuted instantiation: eddsa.c:gf_cond_swap
Unexecuted instantiation: f_generic.c:gf_cond_swap
Unexecuted instantiation: scalar.c:gf_cond_swap
Unexecuted instantiation: f_impl64.c:gf_cond_swap
264
265
#endif /* OSSL_CRYPTO_EC_CURVE448_FIELD_H */