Skip to content

Commit 9f6155a

Browse files
committed
Fix CMake scalar/field macros and standardize RNG usage in tests.
1 parent 740a58a commit 9f6155a

13 files changed

+135
-209
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (HAVE___INT128)
1111
set(MPT_ARCH_DEFS SECP256K1_WIDEMUL_INT128 USE_FIELD_5X52 HAVE___INT128)
1212
else ()
1313
message(STATUS "Build: No 128-bit support detected. Using generic 32-bit arithmetic.")
14-
set(MPT_ARCH_DEFS USE_SCALAR_8X32 USE_FIELD_10X26)
14+
set(MPT_ARCH_DEFS SECP256K1_WIDEMUL_INT64 USE_FIELD_10X26)
1515
endif ()
1616

1717
# --- Find Dependencies ---

tests/test_bulletproof_agg.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
#include <string.h>
33
#include <stdlib.h>
44
#include <time.h>
5-
#include <openssl/rand.h>
65
#include <secp256k1.h>
7-
86
#include "secp256k1_mpt.h"
97
#include "test_utils.h"
108

@@ -24,18 +22,6 @@ static inline double elapsed_ms(struct timespec a, struct timespec b) {
2422
(b.tv_nsec - a.tv_nsec) / 1e6;
2523
}
2624

27-
static void random_scalar(
28-
const secp256k1_context* ctx,
29-
unsigned char out[32]
30-
) {
31-
do {
32-
if (RAND_bytes(out, 32) != 1) {
33-
fprintf(stderr, "RAND_bytes failed\n");
34-
exit(1);
35-
}
36-
} while (!secp256k1_ec_seckey_verify(ctx, out));
37-
}
38-
3925
/* ---- Main ---- */
4026
int main(void) {
4127
printf("[TEST] Aggregated Bulletproof test (m = %d)\n", M);

tests/test_commitments.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,9 @@
22
#include <string.h>
33
#include <stdlib.h>
44
#include "secp256k1_mpt.h"
5-
#include <openssl/rand.h>
65
#include "test_utils.h"
76

8-
/* --- Helper: Safe Random Scalar Generation --- */
9-
/* Ensures the random bytes form a valid curve scalar (0 < scalar < order) */
10-
static void random_scalar(const secp256k1_context* ctx, unsigned char* out) {
11-
int ret;
12-
do {
13-
ret = RAND_bytes(out, 32);
14-
EXPECT(ret == 1); // Crash if RNG fails
15-
} while (!secp256k1_ec_seckey_verify(ctx, out)); // Retry if scalar is invalid (>= curve order)
16-
}
17-
187
/* --- Tests --- */
19-
208
void test_pedersen_commitment_basic() {
219
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
2210

tests/test_elgamal.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include <stdio.h>
2-
#include <stdlib.h> // Required for abort()
2+
#include <stdlib.h>
33
#include <string.h>
44
#include <secp256k1.h>
5-
#include <openssl/rand.h>
65
#include "secp256k1_mpt.h"
76
#include "test_utils.h"
87

tests/test_elgamal_verify.c

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
#include <stdio.h>
2+
#include <stdlib.h>
23
#include <string.h>
3-
#include <stdlib.h> // Required for abort()
44
#include <secp256k1.h>
55
#include "secp256k1_mpt.h"
6-
#include <openssl/rand.h>
76
#include "test_utils.h"
87

9-
void test_elgamal_verify_encryption() {
8+
int main(void) {
109
// 1. Setup Context
1110
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
1211
EXPECT(ctx != NULL);
1312

13+
// Standardized Context Randomization
14+
unsigned char seed[32];
15+
random_bytes(seed);
16+
EXPECT(secp256k1_context_randomize(ctx, seed) == 1);
17+
1418
unsigned char priv_q[32], r[32], r_bad[32];
15-
secp256k1_pubkey pub_q, c1, c2, temp_pubkey;
19+
secp256k1_pubkey pub_q, c1, c2;
1620
uint64_t amount = 12345;
1721
uint64_t amount_bad = 54321;
1822

@@ -21,50 +25,33 @@ void test_elgamal_verify_encryption() {
2125
// 2. Generate Identity and Randomness
2226
EXPECT(secp256k1_elgamal_generate_keypair(ctx, priv_q, &pub_q) == 1);
2327

24-
// Use generate_keypair to get a valid random scalar for 'r' (safer than raw RAND_bytes)
25-
EXPECT(secp256k1_elgamal_generate_keypair(ctx, r, &temp_pubkey) == 1);
28+
// REFACTOR: Use standard helper instead of generating a full keypair
29+
random_scalar(ctx, r);
2630

2731
memcpy(r_bad, r, 32);
2832
r_bad[31] ^= 0xFF; // Flip bits to create a bad randomness factor
2933

3034
// 3. Perform Encryption
31-
// Note: Signature is (ctx, &c1, &c2, &pubkey, amount, r)
3235
EXPECT(secp256k1_elgamal_encrypt(ctx, &c1, &c2, &pub_q, amount, r) == 1);
3336
printf("DEBUG: Encryption successful.\n");
3437

3538
// 4. Test Case 1: Valid Verification (The Happy Path)
3639
int res1 = secp256k1_elgamal_verify_encryption(ctx, &c1, &c2, &pub_q, amount, r);
37-
if (res1 == 1) {
38-
printf("SUCCESS: Valid encryption correctly verified.\n");
39-
} else {
40-
printf("FAILURE: Valid encryption failed verification.\n");
41-
}
4240
EXPECT(res1 == 1);
41+
printf("SUCCESS: Valid encryption correctly verified.\n");
4342

4443
// 5. Test Case 2: Invalid Amount
4544
int res2 = secp256k1_elgamal_verify_encryption(ctx, &c1, &c2, &pub_q, amount_bad, r);
46-
if (res2 == 0) {
47-
printf("SUCCESS: Incorrect amount correctly rejected.\n");
48-
} else {
49-
printf("FAILURE: Incorrect amount was wrongly accepted!\n");
50-
}
5145
EXPECT(res2 == 0);
46+
printf("SUCCESS: Incorrect amount correctly rejected.\n");
5247

5348
// 6. Test Case 3: Invalid Randomness (r)
5449
int res3 = secp256k1_elgamal_verify_encryption(ctx, &c1, &c2, &pub_q, amount, r_bad);
55-
if (res3 == 0) {
56-
printf("SUCCESS: Incorrect randomness correctly rejected.\n");
57-
} else {
58-
printf("FAILURE: Incorrect randomness was wrongly accepted!\n");
59-
}
6050
EXPECT(res3 == 0);
51+
printf("SUCCESS: Incorrect randomness correctly rejected.\n");
6152

6253
// 7. Cleanup
6354
secp256k1_context_destroy(ctx);
6455
printf("DEBUG: All ElGamal Verification tests passed!\n");
65-
}
66-
67-
int main() {
68-
test_elgamal_verify_encryption();
6956
return 0;
7057
}

tests/test_equality_proof.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
#include <stdio.h>
2-
#include <stdlib.h> // Required for abort()
2+
#include <stdlib.h>
33
#include <string.h>
44
#include <secp256k1.h>
5-
#include <openssl/rand.h>
65
#include "secp256k1_mpt.h"
76
#include "test_utils.h"
87

9-
// Helper: Generates 32 random bytes (e.g., for randomness or tx_context_id)
10-
// Now returns void and uses EXPECT internally to enforce success.
11-
static void get_random_bytes(unsigned char* buffer32) {
12-
EXPECT(RAND_bytes(buffer32, 32) == 1);
13-
}
148

159
// Forward declarations for all test functions
1610
static void test_equality_proof_valid(const secp256k1_context* ctx);
@@ -25,7 +19,7 @@ int main() {
2519
EXPECT(ctx != NULL);
2620

2721
unsigned char seed[32];
28-
get_random_bytes(seed);
22+
random_bytes(seed);
2923
EXPECT(secp256k1_context_randomize(ctx, seed) == 1);
3024

3125
// Run tests for this module
@@ -55,15 +49,15 @@ static void test_equality_proof_valid(const secp256k1_context* ctx) {
5549

5650
// 1. Setup: Generate keys, randomness, and encrypt
5751
EXPECT(secp256k1_elgamal_generate_keypair(ctx, privkey, &pubkey) == 1);
58-
get_random_bytes(randomness_r);
52+
random_bytes(randomness_r);
5953

6054
// Ensure randomness is a valid scalar (needed for proof gen)
6155
while (secp256k1_ec_seckey_verify(ctx, randomness_r) != 1) {
62-
get_random_bytes(randomness_r);
56+
random_bytes(randomness_r);
6357
}
6458

6559
EXPECT(secp256k1_elgamal_encrypt(ctx, &c1, &c2, &pubkey, amount, randomness_r) == 1);
66-
get_random_bytes(tx_context_id);
60+
random_bytes(tx_context_id);
6761

6862
// 2. Generate the proof
6963
int prove_result = secp256k1_equality_plaintext_prove(
@@ -93,14 +87,14 @@ static void test_equality_proof_invalid_amount(const secp256k1_context* ctx) {
9387

9488
// 1. Setup: Generate keys, randomness, encrypt correct amount
9589
EXPECT(secp256k1_elgamal_generate_keypair(ctx, privkey, &pubkey) == 1);
96-
get_random_bytes(randomness_r);
90+
random_bytes(randomness_r);
9791

9892
while (secp256k1_ec_seckey_verify(ctx, randomness_r) != 1) {
99-
get_random_bytes(randomness_r);
93+
random_bytes(randomness_r);
10094
}
10195

10296
EXPECT(secp256k1_elgamal_encrypt(ctx, &c1, &c2, &pubkey, correct_amount, randomness_r) == 1);
103-
get_random_bytes(tx_context_id);
97+
random_bytes(tx_context_id);
10498

10599
// 2. Generate the proof for the correct amount
106100
EXPECT(secp256k1_equality_plaintext_prove(
@@ -128,14 +122,14 @@ static void test_equality_proof_invalid_tampered(const secp256k1_context* ctx) {
128122

129123
// 1. Setup and generate a valid proof
130124
EXPECT(secp256k1_elgamal_generate_keypair(ctx, privkey, &pubkey) == 1);
131-
get_random_bytes(randomness_r);
125+
random_bytes(randomness_r);
132126

133127
while (secp256k1_ec_seckey_verify(ctx, randomness_r) != 1) {
134-
get_random_bytes(randomness_r);
128+
random_bytes(randomness_r);
135129
}
136130

137131
EXPECT(secp256k1_elgamal_encrypt(ctx, &c1, &c2, &pubkey, amount, randomness_r) == 1);
138-
get_random_bytes(tx_context_id);
132+
random_bytes(tx_context_id);
139133
EXPECT(secp256k1_equality_plaintext_prove(
140134
ctx, proof, &c1, &c2, &pubkey, amount, randomness_r, tx_context_id) == 1);
141135

@@ -163,14 +157,14 @@ static void test_equality_proof_zero_amount(const secp256k1_context* ctx) {
163157

164158
// 1. Setup: Generate keys, randomness, and encrypt
165159
EXPECT(secp256k1_elgamal_generate_keypair(ctx, privkey, &pubkey) == 1);
166-
get_random_bytes(randomness_r);
160+
random_bytes(randomness_r);
167161

168162
while (secp256k1_ec_seckey_verify(ctx, randomness_r) != 1) {
169-
get_random_bytes(randomness_r);
163+
random_bytes(randomness_r);
170164
}
171165

172166
EXPECT(secp256k1_elgamal_encrypt(ctx, &c1, &c2, &pubkey, amount, randomness_r) == 1);
173-
get_random_bytes(tx_context_id);
167+
random_bytes(tx_context_id);
174168

175169
// 2. Generate the proof for amount 0
176170
int prove_result = secp256k1_equality_plaintext_prove(

tests/test_ipa.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include <stdio.h>
2-
#include <stdlib.h> // Required for abort()
2+
#include <stdlib.h>
33
#include <string.h>
44
#include <secp256k1.h>
5-
#include <openssl/rand.h>
6-
#include <openssl/sha.h>
75
#include "secp256k1_mpt.h"
86
#include "test_utils.h"
97

@@ -77,14 +75,6 @@ extern int secp256k1_bulletproof_ipa_dot(
7775
size_t n
7876
);
7977

80-
/* ---- Test Utils ---- */
81-
82-
static void random_scalar(const secp256k1_context* ctx, unsigned char s[32]) {
83-
do {
84-
EXPECT(RAND_bytes(s, 32) == 1);
85-
} while (!secp256k1_ec_seckey_verify(ctx, s));
86-
}
87-
8878
static int add_term(
8979
const secp256k1_context* ctx,
9080
secp256k1_pubkey* acc,

tests/test_link_proof.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
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 <secp256k1.h>
6-
#include <openssl/rand.h>
76
#include "test_utils.h"
87

9-
/* --- Helper: Safe Random Scalar Generation --- */
10-
static void random_scalar(const secp256k1_context* ctx, unsigned char* out) {
11-
do {
12-
EXPECT(RAND_bytes(out, 32) == 1);
13-
} while (!secp256k1_ec_seckey_verify(ctx, out));
14-
}
15-
168
void test_link_proof() {
179
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
1810
EXPECT(ctx != NULL);

tests/test_pok_sk.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
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 <secp256k1.h>
6-
#include <openssl/rand.h>
76
#include "test_utils.h"
87

9-
/* --- Helper: Safe Random Scalar Generation --- */
10-
static void random_scalar(const secp256k1_context* ctx, unsigned char* out) {
11-
do {
12-
EXPECT(RAND_bytes(out, 32) == 1);
13-
} while (!secp256k1_ec_seckey_verify(ctx, out));
14-
}
15-
168
void test_pok_sk() {
179
secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
1810
EXPECT(ctx != NULL);

0 commit comments

Comments
 (0)