UltrafastSecp256k1
3.50.0
Ultra high-performance secp256k1 elliptic curve cryptography library
Loading...
Searching...
No Matches
test_framework.hpp
Go to the documentation of this file.
1
#ifndef SECP256K1_TEST_FRAMEWORK_HPP
2
#define SECP256K1_TEST_FRAMEWORK_HPP
3
#pragma once
4
5
// ============================================================================
6
// UltrafastSecp256k1 -- Built-in Test Framework
7
// ============================================================================
8
//
9
// Self-contained test infrastructure that is an organic part of the library.
10
// No external test frameworks needed. Portable to ANY platform.
11
//
12
// Architecture:
13
// TestCategory (enum) -> groups related tests (field, scalar, point, ...)
14
// TestResult -> per-check pass/fail with message
15
// TestSuite -> collects results, runs categories, reports
16
//
17
// Usage:
18
// secp256k1::test::TestSuite suite;
19
// suite.run(TestCategory::All); // run everything
20
// suite.run(TestCategory::FieldArith); // single category
21
// suite.run({TestCategory::Point, TestCategory::CT}); // multiple
22
// suite.summary(); // print results
23
// return suite.failed() ? 1 : 0;
24
//
25
// On embedded/RTOS, individual categories can run standalone without
26
// any I/O dependency -- results are stored in a POD counter struct.
27
// ============================================================================
28
29
#include <cstdint>
30
#include <cstddef>
31
32
namespace
secp256k1::test
{
33
34
// -- Test Categories ----------------------------------------------------------
35
// Each enum maps to a logical group of tests. Port authors can select
36
// which categories to run based on platform capabilities.
37
38
enum class
TestCategory
: uint32_t {
39
// -- Core Arithmetic --
40
FieldArith
= 0x0001,
// FieldElement +, -, *, sqr, inv, normalize
41
FieldConversions
= 0x0002,
// from_hex, to_hex, from_bytes, to_bytes, from_limbs
42
FieldEdgeCases
= 0x0004,
// zero, one, p-1, max limbs, double overflow
43
FieldInverse
= 0x0008,
// standalone inverse, batch inverse, all algos
44
FieldRepresentations
= 0x0010,
// 4x64 vs 5x52 vs 10x26 cross-check
45
FieldBranchless
= 0x0020,
// cmov, cmovznz, select, is_zero, eq
46
FieldOptimal
= 0x0040,
// to_optimal / from_optimal roundtrip
47
48
ScalarArith
= 0x0100,
// Scalar +, -, *, inv, neg, is_zero
49
ScalarConversions
= 0x0200,
// from_hex, to_hex, from_bytes, to_bytes, bit()
50
ScalarEdgeCases
= 0x0400,
// 0, 1, n-1, n (wraps to 0), n+1 (wraps to 1)
51
ScalarEncoding
= 0x0800,
// NAF, wNAF encoding correctness
52
53
// -- Point Operations --
54
PointBasic
= 0x1000,
// add, dbl, negate, infinity, generator
55
PointScalarMul
= 0x2000,
// scalar_mul, known vectors, iterated
56
PointInplace
= 0x4000,
// next/prev/dbl/negate_inplace, add_inplace
57
PointPrecomputed
= 0x8000,
// KPlan, predecomposed, precomputed_wnaf
58
PointSerialization
= 0x00010000,
// to_compressed, to_uncompressed, from_affine
59
PointEdgeCases
= 0x00020000,
// P+O, O+P, P+(-P), O+O, 2*O, n*G=O
60
61
// -- Constant-Time Layer --
62
CTOps
= 0x00040000,
// cmov, cswap, select, masks
63
CTField
= 0x00080000,
// ct field arithmetic
64
CTScalar
= 0x00100000,
// ct scalar arithmetic
65
CTPoint
= 0x00200000,
// complete add, ct scalar_mul
66
67
// -- Advanced Algorithms --
68
GLV
= 0x00400000,
// GLV decomposition, endomorphism
69
MSM
= 0x00800000,
// Strauss, Pippenger, unified MSM
70
CombGen
= 0x01000000,
// Comb generator, CT comb, cache I/O
71
BatchInverse
= 0x02000000,
// Montgomery batch inverse, SIMD batch
72
73
// -- Protocols --
74
ECDSA
= 0x04000000,
// sign, verify, deterministic nonce
75
Schnorr
= 0x08000000,
// BIP-340 sign/verify
76
ECDH
= 0x10000000,
// shared secret computation
77
Recovery
= 0x20000000,
// recoverable sig, key recovery
78
79
// -- Aggregate categories --
80
// Use these helper values programmatically:
81
AllField
= 0x007F,
// all field tests
82
AllScalar
= 0x0F00,
// all scalar tests
83
AllPoint
= 0x003F0000 & 0xFFFF0000,
// all point tests -- computed below
84
AllCT
= 0x003C0000,
// all CT tests
85
AllCore
= 0x0FFFFFFF,
// field + scalar + point + CT + algorithms
86
87
All
= 0xFFFFFFFF,
// everything
88
};
89
90
// Bitwise operators for combining categories
91
inline
constexpr
TestCategory
operator|
(
TestCategory
a,
TestCategory
b) {
92
return
static_cast<
TestCategory
>
(
static_cast<
uint32_t
>
(a) |
static_cast<
uint32_t
>
(b));
93
}
94
inline
constexpr
TestCategory
operator&
(
TestCategory
a,
TestCategory
b) {
95
return
static_cast<
TestCategory
>
(
static_cast<
uint32_t
>
(a) &
static_cast<
uint32_t
>
(b));
96
}
97
inline
constexpr
bool
has_flag
(
TestCategory
set,
TestCategory
flag) {
98
return
(
static_cast<
uint32_t
>
(set) &
static_cast<
uint32_t
>
(flag)) != 0;
99
}
100
101
// -- Test Counters (POD, embeddable) ------------------------------------------
102
struct
TestCounters
{
103
uint32_t
passed
= 0;
104
uint32_t
failed
= 0;
105
uint32_t
skipped
= 0;
106
107
uint32_t
total
()
const
{
return
passed
+
failed
+
skipped
; }
108
bool
all_pass
()
const
{
return
failed
== 0; }
109
110
void
merge
(
const
TestCounters
& other) {
111
passed
+= other.
passed
;
112
failed
+= other.
failed
;
113
skipped
+= other.
skipped
;
114
}
115
};
116
117
// -- Category name mapping ----------------------------------------------------
118
inline
const
char
*
category_name
(
TestCategory
cat) {
119
switch
(cat) {
120
case
TestCategory::FieldArith
:
return
"Field Arithmetic"
;
121
case
TestCategory::FieldConversions
:
return
"Field Conversions"
;
122
case
TestCategory::FieldEdgeCases
:
return
"Field Edge Cases"
;
123
case
TestCategory::FieldInverse
:
return
"Field Inverse"
;
124
case
TestCategory::FieldRepresentations
:
return
"Field Representations"
;
125
case
TestCategory::FieldBranchless
:
return
"Field Branchless"
;
126
case
TestCategory::FieldOptimal
:
return
"Field Optimal Dispatch"
;
127
case
TestCategory::ScalarArith
:
return
"Scalar Arithmetic"
;
128
case
TestCategory::ScalarConversions
:
return
"Scalar Conversions"
;
129
case
TestCategory::ScalarEdgeCases
:
return
"Scalar Edge Cases"
;
130
case
TestCategory::ScalarEncoding
:
return
"Scalar NAF/wNAF"
;
131
case
TestCategory::PointBasic
:
return
"Point Basic"
;
132
case
TestCategory::PointScalarMul
:
return
"Point Scalar Mul"
;
133
case
TestCategory::PointInplace
:
return
"Point In-Place"
;
134
case
TestCategory::PointPrecomputed
:
return
"Point Precomputed"
;
135
case
TestCategory::PointSerialization
:
return
"Point Serialization"
;
136
case
TestCategory::PointEdgeCases
:
return
"Point Edge Cases"
;
137
case
TestCategory::CTOps
:
return
"CT Primitives"
;
138
case
TestCategory::CTField
:
return
"CT Field"
;
139
case
TestCategory::CTScalar
:
return
"CT Scalar"
;
140
case
TestCategory::CTPoint
:
return
"CT Point"
;
141
case
TestCategory::GLV
:
return
"GLV Endomorphism"
;
142
case
TestCategory::MSM
:
return
"Multi-Scalar Mul"
;
143
case
TestCategory::CombGen
:
return
"Comb Generator"
;
144
case
TestCategory::BatchInverse
:
return
"Batch Inverse"
;
145
case
TestCategory::ECDSA
:
return
"ECDSA"
;
146
case
TestCategory::Schnorr
:
return
"Schnorr"
;
147
case
TestCategory::ECDH
:
return
"ECDH"
;
148
case
TestCategory::Recovery
:
return
"Key Recovery"
;
149
default
:
return
"Unknown"
;
150
}
151
}
152
153
// -- Complete list of individual categories for iteration ---------------------
154
inline
constexpr
TestCategory
ALL_CATEGORIES
[] = {
155
TestCategory::FieldArith
,
156
TestCategory::FieldConversions
,
157
TestCategory::FieldEdgeCases
,
158
TestCategory::FieldInverse
,
159
TestCategory::FieldRepresentations
,
160
TestCategory::FieldBranchless
,
161
TestCategory::FieldOptimal
,
162
TestCategory::ScalarArith
,
163
TestCategory::ScalarConversions
,
164
TestCategory::ScalarEdgeCases
,
165
TestCategory::ScalarEncoding
,
166
TestCategory::PointBasic
,
167
TestCategory::PointScalarMul
,
168
TestCategory::PointInplace
,
169
TestCategory::PointPrecomputed
,
170
TestCategory::PointSerialization
,
171
TestCategory::PointEdgeCases
,
172
TestCategory::CTOps
,
173
TestCategory::CTField
,
174
TestCategory::CTScalar
,
175
TestCategory::CTPoint
,
176
TestCategory::GLV
,
177
TestCategory::MSM
,
178
TestCategory::CombGen
,
179
TestCategory::BatchInverse
,
180
TestCategory::ECDSA
,
181
TestCategory::Schnorr
,
182
TestCategory::ECDH
,
183
TestCategory::Recovery
,
184
};
185
inline
constexpr
int
NUM_CATEGORIES
=
sizeof
(
ALL_CATEGORIES
) /
sizeof
(
ALL_CATEGORIES
[0]);
186
187
}
// namespace secp256k1::test
188
189
#endif
// SECP256K1_TEST_FRAMEWORK_HPP
secp256k1::test
Definition
test_framework.hpp:32
secp256k1::test::has_flag
constexpr bool has_flag(TestCategory set, TestCategory flag)
Definition
test_framework.hpp:97
secp256k1::test::operator|
constexpr TestCategory operator|(TestCategory a, TestCategory b)
Definition
test_framework.hpp:91
secp256k1::test::TestCategory
TestCategory
Definition
test_framework.hpp:38
secp256k1::test::TestCategory::ECDH
@ ECDH
secp256k1::test::TestCategory::CTOps
@ CTOps
secp256k1::test::TestCategory::MSM
@ MSM
secp256k1::test::TestCategory::PointSerialization
@ PointSerialization
secp256k1::test::TestCategory::ScalarEdgeCases
@ ScalarEdgeCases
secp256k1::test::TestCategory::AllField
@ AllField
secp256k1::test::TestCategory::ScalarConversions
@ ScalarConversions
secp256k1::test::TestCategory::FieldArith
@ FieldArith
secp256k1::test::TestCategory::AllPoint
@ AllPoint
secp256k1::test::TestCategory::PointScalarMul
@ PointScalarMul
secp256k1::test::TestCategory::FieldInverse
@ FieldInverse
secp256k1::test::TestCategory::PointBasic
@ PointBasic
secp256k1::test::TestCategory::CTPoint
@ CTPoint
secp256k1::test::TestCategory::CombGen
@ CombGen
secp256k1::test::TestCategory::FieldRepresentations
@ FieldRepresentations
secp256k1::test::TestCategory::CTField
@ CTField
secp256k1::test::TestCategory::PointEdgeCases
@ PointEdgeCases
secp256k1::test::TestCategory::FieldConversions
@ FieldConversions
secp256k1::test::TestCategory::PointInplace
@ PointInplace
secp256k1::test::TestCategory::AllCore
@ AllCore
secp256k1::test::TestCategory::AllScalar
@ AllScalar
secp256k1::test::TestCategory::ECDSA
@ ECDSA
secp256k1::test::TestCategory::All
@ All
secp256k1::test::TestCategory::FieldEdgeCases
@ FieldEdgeCases
secp256k1::test::TestCategory::PointPrecomputed
@ PointPrecomputed
secp256k1::test::TestCategory::CTScalar
@ CTScalar
secp256k1::test::TestCategory::Schnorr
@ Schnorr
secp256k1::test::TestCategory::GLV
@ GLV
secp256k1::test::TestCategory::Recovery
@ Recovery
secp256k1::test::TestCategory::ScalarEncoding
@ ScalarEncoding
secp256k1::test::TestCategory::BatchInverse
@ BatchInverse
secp256k1::test::TestCategory::AllCT
@ AllCT
secp256k1::test::TestCategory::ScalarArith
@ ScalarArith
secp256k1::test::TestCategory::FieldBranchless
@ FieldBranchless
secp256k1::test::TestCategory::FieldOptimal
@ FieldOptimal
secp256k1::test::operator&
constexpr TestCategory operator&(TestCategory a, TestCategory b)
Definition
test_framework.hpp:94
secp256k1::test::category_name
const char * category_name(TestCategory cat)
Definition
test_framework.hpp:118
secp256k1::test::NUM_CATEGORIES
constexpr int NUM_CATEGORIES
Definition
test_framework.hpp:185
secp256k1::test::ALL_CATEGORIES
constexpr TestCategory ALL_CATEGORIES[]
Definition
test_framework.hpp:154
secp256k1::test::TestCounters
Definition
test_framework.hpp:102
secp256k1::test::TestCounters::total
uint32_t total() const
Definition
test_framework.hpp:107
secp256k1::test::TestCounters::all_pass
bool all_pass() const
Definition
test_framework.hpp:108
secp256k1::test::TestCounters::skipped
uint32_t skipped
Definition
test_framework.hpp:105
secp256k1::test::TestCounters::failed
uint32_t failed
Definition
test_framework.hpp:104
secp256k1::test::TestCounters::merge
void merge(const TestCounters &other)
Definition
test_framework.hpp:110
secp256k1::test::TestCounters::passed
uint32_t passed
Definition
test_framework.hpp:103
cpu
include
secp256k1
test_framework.hpp
Generated by
1.9.8