Skip to content

Commit 64682a0

Browse files
committed
refactor: address reviewer comments for helper function and formatting
1 parent 63e41d6 commit 64682a0

File tree

2 files changed

+93
-63
lines changed

2 files changed

+93
-63
lines changed

src/bulletproof_aggregated.c

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,40 @@ int secp256k1_bulletproof_create_commitment(
10921092
return 1;
10931093
}
10941094

1095+
/* Helper to calculate commitment terms like A and S */
1096+
static int calculate_commitment_term(
1097+
const secp256k1_context* ctx,
1098+
secp256k1_pubkey* out,
1099+
const secp256k1_pubkey* pk_base,
1100+
const unsigned char* base_scalar,
1101+
const unsigned char* vec_l,
1102+
const unsigned char* vec_r,
1103+
const secp256k1_pubkey* G_vec,
1104+
const secp256k1_pubkey* H_vec,
1105+
size_t n
1106+
) {
1107+
secp256k1_pubkey tG, tH, tB;
1108+
const secp256k1_pubkey* pts[3];
1109+
int n_pts = 0;
1110+
1111+
/* 1. base_scalar * Base */
1112+
tB = *pk_base;
1113+
if (!secp256k1_ec_pubkey_tweak_mul(ctx, &tB, base_scalar)) return 0;
1114+
pts[n_pts++] = &tB;
1115+
1116+
/* 2. <vec_l, G> */
1117+
if (secp256k1_bulletproof_ipa_msm(ctx, &tG, G_vec, vec_l, n)) {
1118+
pts[n_pts++] = &tG;
1119+
}
1120+
1121+
/* 3. <vec_r, H> */
1122+
if (secp256k1_bulletproof_ipa_msm(ctx, &tH, H_vec, vec_r, n)) {
1123+
pts[n_pts++] = &tH;
1124+
}
1125+
1126+
return secp256k1_ec_pubkey_combine(ctx, out, pts, n_pts);
1127+
}
1128+
10951129
/**
10961130
* Generates an aggregated Bulletproof for m values.
10971131
*
@@ -1217,53 +1251,8 @@ int secp256k1_bulletproof_prove_agg(
12171251
* A = alpha*Base + <al,G> + <ar,H>
12181252
* S = rho*Base + <sl,G> + <sr,H>
12191253
*/
1220-
{
1221-
secp256k1_pubkey tG, tH, tB;
1222-
const secp256k1_pubkey* pts[3];
1223-
int n_pts = 0;
1224-
1225-
/* A Calculation */
1226-
/* 1. alpha * Base (Always exists) */
1227-
tB = *pk_base;
1228-
if (!secp256k1_ec_pubkey_tweak_mul(ctx, &tB, alpha)) goto cleanup;
1229-
pts[n_pts++] = &tB;
1230-
1231-
/* 2. <al, G> (Only add if al is non-zero) */
1232-
/* Note: ipa_msm returns 0 if result is infinity (all scalars 0). This is valid for value=0. */
1233-
if (secp256k1_bulletproof_ipa_msm(ctx, &tG, G_vec, al, n)) {
1234-
pts[n_pts++] = &tG;
1235-
}
1236-
1237-
/* 3. <ar, H> (Only add if ar is non-zero) */
1238-
if (secp256k1_bulletproof_ipa_msm(ctx, &tH, H_vec, ar, n)) {
1239-
pts[n_pts++] = &tH;
1240-
}
1241-
1242-
/* Combine valid points for A */
1243-
if (!secp256k1_ec_pubkey_combine(ctx, &A, pts, n_pts)) goto cleanup;
1244-
1245-
1246-
/* S Calculation (Follows same logic for consistency) */
1247-
n_pts = 0;
1248-
1249-
/* 1. rho * Base */
1250-
tB = *pk_base;
1251-
if (!secp256k1_ec_pubkey_tweak_mul(ctx, &tB, rho)) goto cleanup;
1252-
pts[n_pts++] = &tB;
1253-
1254-
/* 2. <sl, G> */
1255-
if (secp256k1_bulletproof_ipa_msm(ctx, &tG, G_vec, sl, n)) {
1256-
pts[n_pts++] = &tG;
1257-
}
1258-
1259-
/* 3. <sr, H> */
1260-
if (secp256k1_bulletproof_ipa_msm(ctx, &tH, H_vec, sr, n)) {
1261-
pts[n_pts++] = &tH;
1262-
}
1263-
1264-
/* Combine valid points for S */
1265-
if (!secp256k1_ec_pubkey_combine(ctx, &S, pts, n_pts)) goto cleanup;
1266-
}
1254+
if (!calculate_commitment_term(ctx, &A, pk_base, alpha, al, ar, G_vec, H_vec, n)) goto cleanup;
1255+
if (!calculate_commitment_term(ctx, &S, pk_base, rho, sl, sr, G_vec, H_vec, n)) goto cleanup;
12671256

12681257
/* ---- 6. Fiat–Shamir y,z ---- */
12691258
{

tests/test_bulletproof_agg.c

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,38 @@ static inline double elapsed_ms(struct timespec a, struct timespec b) {
1717
}
1818

1919
/* ---- Core Test Logic ---- */
20-
void run_test_case(secp256k1_context* ctx, const char* name, uint64_t* values, size_t m, int run_benchmarks) {
21-
printf("\n[TEST] %s (m = %zu)\n", name, m);
20+
void run_test_case(secp256k1_context* ctx, const char* name, uint64_t* values, size_t num_values, int run_benchmarks) {
21+
printf("\n[TEST] %s (num_values = %zu)\n", name, num_values);
2222

23-
unsigned char blindings[m][32];
24-
secp256k1_pubkey commitments[m];
23+
unsigned char blindings[num_values][32];
24+
secp256k1_pubkey commitments[num_values];
2525
unsigned char context_id[32];
2626
EXPECT(RAND_bytes(context_id, 32) == 1);
2727

2828
secp256k1_pubkey pk_base;
2929
EXPECT(secp256k1_mpt_get_h_generator(ctx, &pk_base));
3030

3131
/* ---- Commitments ---- */
32-
for (size_t i = 0; i < m; i++) {
32+
for (size_t i = 0; i < num_values; i++) {
3333
random_scalar(ctx, blindings[i]);
3434
EXPECT(secp256k1_bulletproof_create_commitment(
35-
ctx, &commitments[i], values[i], blindings[i], &pk_base));
35+
ctx,
36+
&commitments[i],
37+
values[i],
38+
blindings[i],
39+
&pk_base));
3640
}
3741

3842
/* ---- Generator vectors ---- */
39-
const size_t n = BP_TOTAL_BITS(m);
43+
const size_t n = BP_TOTAL_BITS(num_values);
4044
secp256k1_pubkey* G_vec = malloc(n * sizeof(secp256k1_pubkey));
4145
secp256k1_pubkey* H_vec = malloc(n * sizeof(secp256k1_pubkey));
4246
EXPECT(G_vec && H_vec);
4347

44-
EXPECT(secp256k1_mpt_get_generator_vector(ctx, G_vec, n, (const unsigned char*)"G", 1));
45-
EXPECT(secp256k1_mpt_get_generator_vector(ctx, H_vec, n, (const unsigned char*)"H", 1));
48+
EXPECT(secp256k1_mpt_get_generator_vector(
49+
ctx, G_vec, n, (const unsigned char*)"G", 1));
50+
EXPECT(secp256k1_mpt_get_generator_vector(
51+
ctx, H_vec, n, (const unsigned char*)"H", 1));
4652

4753
/* ---- Prove ---- */
4854
unsigned char proof[4096];
@@ -52,7 +58,14 @@ void run_test_case(secp256k1_context* ctx, const char* name, uint64_t* values, s
5258
clock_gettime(CLOCK_MONOTONIC, &t_p_start);
5359

5460
EXPECT(secp256k1_bulletproof_prove_agg(
55-
ctx, proof, &proof_len, values, (const unsigned char*)blindings, m, &pk_base, context_id));
61+
ctx,
62+
proof,
63+
&proof_len,
64+
values,
65+
(const unsigned char*)blindings,
66+
num_values,
67+
&pk_base,
68+
context_id));
5669

5770
clock_gettime(CLOCK_MONOTONIC, &t_p_end);
5871
printf(" Proof size: %zu bytes\n", proof_len);
@@ -63,7 +76,15 @@ void run_test_case(secp256k1_context* ctx, const char* name, uint64_t* values, s
6376
clock_gettime(CLOCK_MONOTONIC, &t_v_start);
6477

6578
int ok = secp256k1_bulletproof_verify_agg(
66-
ctx, G_vec, H_vec, proof, proof_len, commitments, m, &pk_base, context_id);
79+
ctx,
80+
G_vec,
81+
H_vec,
82+
proof,
83+
proof_len,
84+
commitments,
85+
num_values,
86+
&pk_base,
87+
context_id);
6788

6889
clock_gettime(CLOCK_MONOTONIC, &t_v_end);
6990
EXPECT(ok);
@@ -77,7 +98,15 @@ void run_test_case(secp256k1_context* ctx, const char* name, uint64_t* values, s
7798
struct timespec ts, te;
7899
clock_gettime(CLOCK_MONOTONIC, &ts);
79100
ok = secp256k1_bulletproof_verify_agg(
80-
ctx, G_vec, H_vec, proof, proof_len, commitments, m, &pk_base, context_id);
101+
ctx,
102+
G_vec,
103+
H_vec,
104+
proof,
105+
proof_len,
106+
commitments,
107+
num_values,
108+
&pk_base,
109+
context_id);
81110
clock_gettime(CLOCK_MONOTONIC, &te);
82111
EXPECT(ok);
83112
total_ms += elapsed_ms(ts, te);
@@ -86,17 +115,29 @@ void run_test_case(secp256k1_context* ctx, const char* name, uint64_t* values, s
86115
}
87116

88117
/* ---- Negative Test (Tamper) ---- */
89-
secp256k1_pubkey bad_commitments[m];
90-
memcpy(bad_commitments, commitments, sizeof(secp256k1_pubkey) * m);
118+
secp256k1_pubkey bad_commitments[num_values];
119+
memcpy(bad_commitments, commitments, sizeof(commitments));
91120
unsigned char bad_blinding[32];
92121
random_scalar(ctx, bad_blinding);
93122

94123
/* Create fake commitment to (value + 1) */
95124
EXPECT(secp256k1_bulletproof_create_commitment(
96-
ctx, &bad_commitments[m - 1], values[m - 1] + 1, bad_blinding, &pk_base));
125+
ctx,
126+
&bad_commitments[num_values - 1],
127+
values[num_values - 1] + 1,
128+
bad_blinding,
129+
&pk_base));
97130

98131
ok = secp256k1_bulletproof_verify_agg(
99-
ctx, G_vec, H_vec, proof, proof_len, bad_commitments, m, &pk_base, context_id);
132+
ctx,
133+
G_vec,
134+
H_vec,
135+
proof,
136+
proof_len,
137+
bad_commitments,
138+
num_values,
139+
&pk_base,
140+
context_id);
100141

101142
if (ok) {
102143
fprintf(stderr, "FAILED: Accepted invalid proof!\n");

0 commit comments

Comments
 (0)