Skip to content

bench: Fix bench_whitelist hang#347

Merged
mllwchrry merged 1 commit intoBlockstreamResearch:masterfrom
mllwchrry:fix-bench-whitelist
Mar 18, 2026
Merged

bench: Fix bench_whitelist hang#347
mllwchrry merged 1 commit intoBlockstreamResearch:masterfrom
mllwchrry:fix-bench-whitelist

Conversation

@mllwchrry
Copy link
Copy Markdown
Collaborator

Addresses #346

Copy link
Copy Markdown
Member

@real-or-random real-or-random left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No other benchmark uses randomness, so it's clear that we should remove it here, too, even just for consistency. But one advantage of using random keys is that the compiler can't optimize because inputs are known. (A "perfect" compiler would optimize away the entire benchmark and replace it with the precomputed results...)

But this is an issue in all benchmarks, including those in upstream, so let's maybe not care about it here unless you're interested in looking into it.

Comment thread src/bench_whitelist.c Outdated
/* Start with subkey */
random_scalar_order(&ssub);
secp256k1_scalar_get_b32(data.csub, &ssub);
memset(data.csub, 1, 32);
Copy link
Copy Markdown
Member

@real-or-random real-or-random Mar 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This key has an exceptionally low Hamming weight, and variable-time algorithms such as secp256k1_ecmult may make use of this fact. It's probably a good idea to use "random" keys for a benchmark.

We could still keep the entire benchmark deterministic, e.g., we could hash a counter and use the resulting hashes as keys. (I'm aware that this is similar to using testrand but I believe it's still a good idea to remove the dependency on testrand.)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hashing is a good idea. I used an approach similar to that in bench_ecmult.
Fixed in d54270e

@real-or-random
Copy link
Copy Markdown
Member

The interesting question here is whether this hasn't been noticed on CI:

  • The Linux ci script has the benchmark binaries hardcoded and it currently doesn't run any of the -zkp benchmarks.
  • The native Windows CI jobs use bench*.exe but here the -zkp modules are not even enabled.

These things should be fixed, too.

@mllwchrry mllwchrry force-pushed the fix-bench-whitelist branch 2 times, most recently from 26b745c to d54270e Compare March 18, 2026 11:46
@mllwchrry
Copy link
Copy Markdown
Collaborator Author

I will fix CI to include -zkp benchmarks in a separate PR.
As for the compiler optimization concern, I'll also look into it later.

Comment thread src/bench_whitelist.c Outdated
Comment on lines +63 to +64
secp256k1_scalar_set_b32(scalar, seckey, &overflow);
CHECK(!overflow);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also secp256k1_scalar_set_b32_seckey

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in be075fe

Comment thread src/bench_whitelist.c Outdated
random_scalar_order(&son);
secp256k1_scalar_get_b32(data.online_seckey[i], &son);
/* Create two keys using different counter values to ensure different keys */
generate_scalar(i + 1, &son, data.online_seckey[i]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a convention that output arguments go first. (This is documented in secp256k1.h for API functions, and we use the same convention internally.)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the secp256k1_scalar_set_b32_seckey usage should also be changed in bench_ecmult.c. Since it's a minor issue, it is probably not worth opening a PR to upstream. However, we can keep this in mind and contribute it upstream separately if there's an opportunity (e.g., a PR with other style fixes).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in be075fe

@mllwchrry mllwchrry force-pushed the fix-bench-whitelist branch from d54270e to be075fe Compare March 18, 2026 12:29
Copy link
Copy Markdown
Member

@real-or-random real-or-random left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK be075fe

@mllwchrry mllwchrry merged commit f049578 into BlockstreamResearch:master Mar 18, 2026
122 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants