Skip to content

Commit 81e531e

Browse files
mrtcnkmathbunnyru
andauthored
Fix tests failing in release build (#11)
Co-authored-by: Ayaz Salikhov <mathbunnyru@users.noreply.github.com>
1 parent 954b995 commit 81e531e

16 files changed

+507
-628
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
build/
22
.DS_Store
33
cmake-build-*/
4+
.idea/
5+
.venv

CMakeLists.txt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,20 @@ add_library(mpt-crypto
2222
target_include_directories(mpt-crypto PUBLIC include)
2323

2424
# --- Set Compile Definitions ---
25-
target_compile_definitions(mpt-crypto PRIVATE USE_SCALAR_8X32 USE_FIELD_10X26 ECMULT_WINDOW_SIZE=15
26-
ECMULT_GEN_PREC_BITS=4)
25+
include(CheckTypeSize)
26+
# Check if the compiler supports __int128 (required for the optimized 64-bit code)
27+
check_type_size("__int128" HAVE___INT128)
28+
29+
if (HAVE___INT128)
30+
message(STATUS "Build: Detected 128-bit integer support.")
31+
target_compile_definitions(mpt-crypto PRIVATE SECP256K1_WIDEMUL_INT128)
32+
else ()
33+
message(STATUS "Build: No 128-bit support detected.")
34+
target_compile_definitions(mpt-crypto PRIVATE SECP256K1_WIDEMUL_INT64)
35+
endif ()
2736

2837
# --- Link Dependencies ---
29-
target_link_libraries(mpt-crypto PUBLIC secp256k1::secp256k1 PUBLIC OpenSSL::Crypto)
38+
target_link_libraries(mpt-crypto PUBLIC secp256k1::secp256k1 OpenSSL::Crypto)
3039

3140
# --- Testing ---
3241
option(ENABLE_TESTS "Enable building tests" ON)

src/mpt_scalar.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,15 @@
3636
#include <string.h>
3737
#include <openssl/crypto.h>
3838

39-
/* 1. Backend Configuration Definitions */
40-
#ifndef USE_SCALAR_8X32
41-
#define USE_SCALAR_8X32
42-
#endif
43-
#ifndef USE_FIELD_10X26
44-
#define USE_FIELD_10X26
45-
#endif
4639

47-
/* 2. Include low-level utilities first.
40+
/* Include low-level utilities first.
4841
On ARM64/Apple Silicon, the scalar math depends on 128-bit
4942
integer helpers defined in these headers. */
5043
#include <private/util.h>
5144
#include <private/int128.h>
5245
#include <private/int128_impl.h>
5346

54-
/* 3. Include the actual scalar implementations */
47+
/* Include the actual scalar implementations */
5548
#include <private/scalar.h>
5649
#include <private/scalar_impl.h>
5750

tests/test_bulletproof.c

Lines changed: 0 additions & 107 deletions
This file was deleted.

tests/test_bulletproof_agg.c

Lines changed: 20 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#include <stdio.h>
22
#include <string.h>
3-
#include <assert.h>
3+
#include <stdlib.h>
44
#include <time.h>
5-
#include <openssl/rand.h>
65
#include <secp256k1.h>
7-
86
#include "secp256k1_mpt.h"
7+
#include "test_utils.h"
98

109
/* ---- Aggregation parameters ---- */
1110
#define M 2
@@ -15,45 +14,6 @@
1514
/* ---- Benchmark parameters ---- */
1615
#define VERIFY_RUNS 5
1716

18-
/* ---- Prototypes ---- */
19-
int secp256k1_bulletproof_prove_agg(
20-
const secp256k1_context* ctx,
21-
unsigned char* proof_out,
22-
size_t* proof_len,
23-
const uint64_t* values,
24-
const unsigned char* blindings_flat,
25-
size_t m,
26-
const secp256k1_pubkey* pk_base,
27-
const unsigned char* context_id
28-
);
29-
30-
int secp256k1_bulletproof_verify_agg(
31-
const secp256k1_context* ctx,
32-
const secp256k1_pubkey* G_vec,
33-
const secp256k1_pubkey* H_vec,
34-
const unsigned char* proof,
35-
size_t proof_len,
36-
const secp256k1_pubkey* commitment_C_vec,
37-
size_t m,
38-
const secp256k1_pubkey* pk_base,
39-
const unsigned char* context_id
40-
);
41-
42-
int secp256k1_bulletproof_create_commitment(
43-
const secp256k1_context* ctx,
44-
secp256k1_pubkey* commitment_C,
45-
uint64_t value,
46-
const unsigned char* blinding_factor,
47-
const secp256k1_pubkey* pk_base
48-
);
49-
50-
extern int secp256k1_mpt_get_generator_vector(
51-
const secp256k1_context* ctx,
52-
secp256k1_pubkey* vec,
53-
size_t n,
54-
const unsigned char* label,
55-
size_t label_len
56-
);
5717

5818
/* ---- Helpers ---- */
5919

@@ -62,49 +22,32 @@ static inline double elapsed_ms(struct timespec a, struct timespec b) {
6222
(b.tv_nsec - a.tv_nsec) / 1e6;
6323
}
6424

65-
static void random_scalar(
66-
const secp256k1_context* ctx,
67-
unsigned char out[32]
68-
) {
69-
do {
70-
RAND_bytes(out, 32);
71-
} while (!secp256k1_ec_seckey_verify(ctx, out));
72-
}
73-
7425
/* ---- Main ---- */
7526
int main(void) {
7627
printf("[TEST] Aggregated Bulletproof test (m = %d)\n", M);
7728

7829
/* ---- Context ---- */
7930
secp256k1_context* ctx =
8031
secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
32+
EXPECT(ctx != NULL);
8133

8234
/* ---- Values ---- */
8335
uint64_t values[M] = { 5000, 123456 };
8436
unsigned char blindings[M][32];
8537
secp256k1_pubkey commitments[M];
8638

87-
/**
88-
* CONTEXT BINDING:
89-
* In the production system, this ID is derived deterministically:
90-
* TransactionContextID := H(TxType || Account || MPTokenIssuanceID || ...)
91-
* * See [Spec Section 3.3.1] for the derivation rules.
92-
* * For this library unit test, random bytes suffice to verify that the
93-
* proof binds correctly to *whatever* context ID is provided.
94-
*/
39+
/* ---- Context Binding ---- */
9540
unsigned char context_id[32];
96-
RAND_bytes(context_id, 32);
97-
41+
EXPECT(RAND_bytes(context_id, 32) == 1);
9842

9943
secp256k1_pubkey pk_base;
100-
/* Use the standard H generator from the library */
101-
assert(secp256k1_mpt_get_h_generator(ctx, &pk_base));
102-
44+
/* Use the standard H generator from the library */
45+
EXPECT(secp256k1_mpt_get_h_generator(ctx, &pk_base));
10346

10447
/* ---- Commitments ---- */
10548
for (size_t i = 0; i < M; i++) {
10649
random_scalar(ctx, blindings[i]);
107-
assert(secp256k1_bulletproof_create_commitment(
50+
EXPECT(secp256k1_bulletproof_create_commitment(
10851
ctx,
10952
&commitments[i],
11053
values[i],
@@ -116,11 +59,11 @@ int main(void) {
11659
const size_t n = BP_TOTAL_BITS(M);
11760
secp256k1_pubkey* G_vec = malloc(n * sizeof(secp256k1_pubkey));
11861
secp256k1_pubkey* H_vec = malloc(n * sizeof(secp256k1_pubkey));
119-
assert(G_vec && H_vec);
62+
EXPECT(G_vec && H_vec);
12063

121-
assert(secp256k1_mpt_get_generator_vector(
64+
EXPECT(secp256k1_mpt_get_generator_vector(
12265
ctx, G_vec, n, (const unsigned char*)"G", 1));
123-
assert(secp256k1_mpt_get_generator_vector(
66+
EXPECT(secp256k1_mpt_get_generator_vector(
12467
ctx, H_vec, n, (const unsigned char*)"H", 1));
12568

12669
/* ---- Prove (timed) ---- */
@@ -132,7 +75,8 @@ int main(void) {
13275
struct timespec t_p_start, t_p_end;
13376
clock_gettime(CLOCK_MONOTONIC, &t_p_start);
13477

135-
assert(secp256k1_bulletproof_prove_agg(
78+
/* Note: We cast the 2D array 'blindings' to flat pointer */
79+
EXPECT(secp256k1_bulletproof_prove_agg(
13680
ctx,
13781
proof,
13882
&proof_len,
@@ -167,10 +111,7 @@ int main(void) {
167111

168112
clock_gettime(CLOCK_MONOTONIC, &t_v_end);
169113

170-
if (!ok) {
171-
printf("FAILED\n");
172-
return 1;
173-
}
114+
EXPECT(ok);
174115

175116
printf("PASSED\n");
176117
printf("[BENCH] Verification time (single): %.3f ms\n",
@@ -196,7 +137,7 @@ int main(void) {
196137

197138
clock_gettime(CLOCK_MONOTONIC, &te);
198139

199-
assert(ok);
140+
EXPECT(ok);
200141
total_ms += elapsed_ms(ts, te);
201142
}
202143

@@ -212,10 +153,11 @@ int main(void) {
212153
unsigned char bad_blinding[32];
213154
random_scalar(ctx, bad_blinding);
214155

215-
assert(secp256k1_bulletproof_create_commitment(
156+
/* Create a fake commitment to (value + 1) to break the sum */
157+
EXPECT(secp256k1_bulletproof_create_commitment(
216158
ctx,
217159
&bad_commitments[1],
218-
values[1] + 1,
160+
values[M - 1] + 1,
219161
bad_blinding,
220162
&pk_base));
221163

@@ -231,8 +173,8 @@ int main(void) {
231173
context_id);
232174

233175
if (ok) {
234-
printf("FAILED (accepted invalid proof)\n");
235-
return 1;
176+
fprintf(stderr, "FAILED: Accepted invalid proof!\n");
177+
exit(EXIT_FAILURE);
236178
}
237179

238180
printf("PASSED (rejected invalid proof)\n");

0 commit comments

Comments
 (0)