Skip to content

Commit 7833fa2

Browse files
committed
Refactor: Deduplicate EXPECT macro to test_utils.h and use target_compile_definitions
1 parent a60261f commit 7833fa2

14 files changed

+94
-156
lines changed

CMakeLists.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
cmake_minimum_required(VERSION 3.16)
22
project(mpt-crypto C CXX)
33

4-
# --- 1. Global Architecture Detection ---
4+
# --- Global Architecture Detection ---
55
include(CheckTypeSize)
66
check_type_size("unsigned __int128" HAVE_INT128_T)
77

8+
# Store definitions in a variable
89
if (HAVE_INT128_T OR CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "arm64"
910
OR CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
10-
message(STATUS "Build: Detected 64-bit system. Enforcing 64-bit arithmetic globally.")
11-
add_compile_definitions(USE_SCALAR_4X64 USE_FIELD_5X52 HAVE___INT128)
11+
message(STATUS "Build: Detected 64-bit system. Enforcing 64-bit arithmetic.")
12+
set(MPT_ARCH_DEFS USE_SCALAR_4X64 USE_FIELD_5X52 HAVE___INT128)
1213
else ()
13-
message(STATUS "Build: Detected 32-bit system. Enforcing generic arithmetic globally.")
14-
add_compile_definitions(USE_SCALAR_8X32 USE_FIELD_10X26)
14+
message(STATUS "Build: Detected 32-bit system. Enforcing generic arithmetic.")
15+
set(MPT_ARCH_DEFS USE_SCALAR_8X32 USE_FIELD_10X26)
1516
endif ()
1617

17-
# --- Find Dependencies (Maintainer's Way) ---
18+
# --- Find Dependencies ---
1819
find_package(OpenSSL REQUIRED)
1920
find_package(secp256k1 REQUIRED)
2021

@@ -35,7 +36,7 @@ add_library(mpt-crypto
3536
target_include_directories(mpt-crypto PUBLIC include)
3637

3738
# --- Set Compile Definitions ---
38-
target_compile_definitions(mpt-crypto PRIVATE ECMULT_WINDOW_SIZE=15 ECMULT_GEN_PREC_BITS=4)
39+
target_compile_definitions(mpt-crypto PRIVATE ECMULT_WINDOW_SIZE=15 ECMULT_GEN_PREC_BITS=4 PUBLIC ${MPT_ARCH_DEFS})
3940

4041
# --- Link Dependencies ---
4142
target_link_libraries(mpt-crypto PUBLIC secp256k1::secp256k1 PUBLIC OpenSSL::Crypto)

src/mpt_scalar.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#include <private/int128.h>
4545
#include <private/int128_impl.h>
4646

47-
/* 3. Include the actual scalar implementations */
47+
/* Include the actual scalar implementations */
4848
#include <private/scalar.h>
4949
#include <private/scalar_impl.h>
5050

tests/test_bulletproof_agg.c

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <secp256k1.h>
77

88
#include "secp256k1_mpt.h"
9+
#include "test_utils.h"
910

1011
/* ---- Aggregation parameters ---- */
1112
#define M 2
@@ -15,14 +16,6 @@
1516
/* ---- Benchmark parameters ---- */
1617
#define VERIFY_RUNS 5
1718

18-
/* --- Macro: Persistent Assertion --- */
19-
#define EXPECT(cond, msg) do { \
20-
if (!(cond)) { \
21-
fprintf(stderr, "CRITICAL FAILURE: %s\nFile: %s, Line: %d\nCode: %s\n", \
22-
msg, __FILE__, __LINE__, #cond); \
23-
exit(EXIT_FAILURE); \
24-
} \
25-
} while(0)
2619

2720
/* ---- Helpers ---- */
2821

@@ -50,7 +43,7 @@ int main(void) {
5043
/* ---- Context ---- */
5144
secp256k1_context* ctx =
5245
secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
53-
EXPECT(ctx != NULL, "Failed to create context");
46+
EXPECT(ctx != NULL);
5447

5548
/* ---- Values ---- */
5649
uint64_t values[M] = { 5000, 123456 };
@@ -59,11 +52,11 @@ int main(void) {
5952

6053
/* ---- Context Binding ---- */
6154
unsigned char context_id[32];
62-
EXPECT(RAND_bytes(context_id, 32) == 1, "Failed to generate context_id");
55+
EXPECT(RAND_bytes(context_id, 32) == 1);
6356

6457
secp256k1_pubkey pk_base;
6558
/* Use the standard H generator from the library */
66-
EXPECT(secp256k1_mpt_get_h_generator(ctx, &pk_base), "Failed to get H generator");
59+
EXPECT(secp256k1_mpt_get_h_generator(ctx, &pk_base));
6760

6861
/* ---- Commitments ---- */
6962
for (size_t i = 0; i < M; i++) {
@@ -73,19 +66,19 @@ int main(void) {
7366
&commitments[i],
7467
values[i],
7568
blindings[i],
76-
&pk_base), "Failed to create commitment");
69+
&pk_base));
7770
}
7871

7972
/* ---- Generator vectors ---- */
8073
const size_t n = BP_TOTAL_BITS(M);
8174
secp256k1_pubkey* G_vec = malloc(n * sizeof(secp256k1_pubkey));
8275
secp256k1_pubkey* H_vec = malloc(n * sizeof(secp256k1_pubkey));
83-
EXPECT(G_vec && H_vec, "Malloc failed");
76+
EXPECT(G_vec && H_vec);
8477

8578
EXPECT(secp256k1_mpt_get_generator_vector(
86-
ctx, G_vec, n, (const unsigned char*)"G", 1), "Failed to get G vector");
79+
ctx, G_vec, n, (const unsigned char*)"G", 1));
8780
EXPECT(secp256k1_mpt_get_generator_vector(
88-
ctx, H_vec, n, (const unsigned char*)"H", 1), "Failed to get H vector");
81+
ctx, H_vec, n, (const unsigned char*)"H", 1));
8982

9083
/* ---- Prove (timed) ---- */
9184
unsigned char proof[4096];
@@ -105,7 +98,7 @@ int main(void) {
10598
(const unsigned char*)blindings,
10699
M,
107100
&pk_base,
108-
context_id), "Proving failed");
101+
context_id));
109102

110103
clock_gettime(CLOCK_MONOTONIC, &t_p_end);
111104

@@ -132,7 +125,7 @@ int main(void) {
132125

133126
clock_gettime(CLOCK_MONOTONIC, &t_v_end);
134127

135-
EXPECT(ok, "Verification failed (single run)");
128+
EXPECT(ok);
136129

137130
printf("PASSED\n");
138131
printf("[BENCH] Verification time (single): %.3f ms\n",
@@ -158,7 +151,7 @@ int main(void) {
158151

159152
clock_gettime(CLOCK_MONOTONIC, &te);
160153

161-
EXPECT(ok, "Verification failed during benchmark");
154+
EXPECT(ok);
162155
total_ms += elapsed_ms(ts, te);
163156
}
164157

@@ -178,9 +171,9 @@ int main(void) {
178171
EXPECT(secp256k1_bulletproof_create_commitment(
179172
ctx,
180173
&bad_commitments[1],
181-
values[1] + 1,
174+
values[M - 1] + 1,
182175
bad_blinding,
183-
&pk_base), "Failed to create bad commitment");
176+
&pk_base));
184177

185178
ok = secp256k1_bulletproof_verify_agg(
186179
ctx,

tests/test_commitments.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
#include <stdio.h>
22
#include <string.h>
3-
#include <stdlib.h> // Required for abort()
3+
#include <stdlib.h>
44
#include "secp256k1_mpt.h"
55
#include <openssl/rand.h>
6-
7-
/* --- Macro: Persistent Assertion --- */
8-
#define EXPECT(condition) do { \
9-
if (!(condition)) { \
10-
fprintf(stderr, "TEST FAILED: %s at line %d\n", #condition, __LINE__); \
11-
abort(); \
12-
} \
13-
} while(0)
6+
#include "test_utils.h"
147

158
/* --- Helper: Safe Random Scalar Generation --- */
169
/* Ensures the random bytes form a valid curve scalar (0 < scalar < order) */

tests/test_elgamal.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44
#include <secp256k1.h>
55
#include <openssl/rand.h>
66
#include "secp256k1_mpt.h"
7-
8-
/* --- Macro: Persistent Assertion --- */
9-
#define EXPECT(condition) do { \
10-
if (!(condition)) { \
11-
fprintf(stderr, "TEST FAILED: %s at line %d\n", #condition, __LINE__); \
12-
abort(); \
13-
} \
14-
} while(0)
7+
#include "test_utils.h"
158

169
// Forward declarations for all test functions
1710
static void test_key_generation(const secp256k1_context* ctx);

tests/test_elgamal_verify.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44
#include <secp256k1.h>
55
#include "secp256k1_mpt.h"
66
#include <openssl/rand.h>
7-
8-
/* --- Macro: Persistent Assertion --- */
9-
#define EXPECT(condition) do { \
10-
if (!(condition)) { \
11-
fprintf(stderr, "TEST FAILED: %s at line %d\n", #condition, __LINE__); \
12-
abort(); \
13-
} \
14-
} while(0)
7+
#include "test_utils.h"
158

169
void test_elgamal_verify_encryption() {
1710
// 1. Setup Context

tests/test_equality_proof.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44
#include <secp256k1.h>
55
#include <openssl/rand.h>
66
#include "secp256k1_mpt.h"
7-
8-
/* --- Macro: Persistent Assertion --- */
9-
#define EXPECT(condition) do { \
10-
if (!(condition)) { \
11-
fprintf(stderr, "TEST FAILED: %s at line %d\n", #condition, __LINE__); \
12-
abort(); \
13-
} \
14-
} while(0)
7+
#include "test_utils.h"
158

169
// Helper: Generates 32 random bytes (e.g., for randomness or tx_context_id)
1710
// Now returns void and uses EXPECT internally to enforce success.

tests/test_ipa.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,12 @@
55
#include <openssl/rand.h>
66
#include <openssl/sha.h>
77
#include "secp256k1_mpt.h"
8+
#include "test_utils.h"
89

910
#define N_BITS 64
1011
/* log2(64) = 6 rounds */
1112
#define IPA_ROUNDS 6
1213

13-
/* --- Macro: Persistent Assertion --- */
14-
#define EXPECT(condition) do { \
15-
if (!(condition)) { \
16-
fprintf(stderr, "TEST FAILED: %s at line %d\n", #condition, __LINE__); \
17-
abort(); \
18-
} \
19-
} while(0)
20-
2114
/* ---- Helper Macros ---- */
2215
static int scalar_is_zero(const unsigned char s[32]) {
2316
unsigned char z[32] = {0};

tests/test_link_proof.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44
#include "secp256k1_mpt.h"
55
#include <secp256k1.h>
66
#include <openssl/rand.h>
7-
8-
/* --- Macro: Persistent Assertion --- */
9-
#define EXPECT(condition) do { \
10-
if (!(condition)) { \
11-
fprintf(stderr, "TEST FAILED: %s at line %d\n", #condition, __LINE__); \
12-
abort(); \
13-
} \
14-
} while(0)
7+
#include "test_utils.h"
158

169
/* --- Helper: Safe Random Scalar Generation --- */
1710
static void random_scalar(const secp256k1_context* ctx, unsigned char* out) {

tests/test_pok_sk.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44
#include "secp256k1_mpt.h"
55
#include <secp256k1.h>
66
#include <openssl/rand.h>
7-
8-
/* --- Macro: Persistent Assertion --- */
9-
#define EXPECT(condition) do { \
10-
if (!(condition)) { \
11-
fprintf(stderr, "TEST FAILED: %s at line %d\n", #condition, __LINE__); \
12-
abort(); \
13-
} \
14-
} while(0)
7+
#include "test_utils.h"
158

169
/* --- Helper: Safe Random Scalar Generation --- */
1710
static void random_scalar(const secp256k1_context* ctx, unsigned char* out) {

0 commit comments

Comments
 (0)