Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ecmult_gen, ecmult_const and ecmult to benchmark #662

Merged
merged 3 commits into from
Jun 6, 2021

Conversation

jonasnick
Copy link
Contributor

I was trying to determine the impact of ecmult_gen in schnorrsig signing and noticed that there is no way to bench this right now. The new benchmarks look like this:

$ ./bench_ecmult
ecmult_gen: min 20.9us / avg 21.2us / max 21.7us
ecmult_const: min 63.9us / avg 64.3us / max 64.8us
ecmult 1: min 49.4us / avg 49.7us / max 50.3us
ecmult 1g: min 39.8us / avg 40.0us / max 40.3us
ecmult 2g: min 27.2us / avg 27.3us / max 27.8us
ecmult_multi 1g: min 39.8us / avg 40.0us / max 40.2us
ecmult_multi 2g: min 27.2us / avg 27.4us / max 27.7us
ecmult_multi 3g: min 22.8us / avg 22.9us / max 23.1us
ecmult_multi 4g: min 20.6us / avg 20.8us / max 21.1us
ecmult_multi 5g: min 19.3us / avg 19.5us / max 19.7us

(Turns out ecmult_gen is 37% of the 55.8us that schnorrsig sign takes)

Copy link
Contributor

@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.

Approach ACK

/* Hashes x into [0, POINTS) twice and store the result in offset1 and offset2. */
static void hash_into_offset(bench_data* data, size_t x) {
data->offset1 = (x * 0x537b7f6f + 0x8f66a481) % POINTS;
data->offset2 = (x * 0x7f6f537b + 0x6a1a8f49) % POINTS;
Copy link
Contributor

Choose a reason for hiding this comment

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

where's this thing from?
I see it's also in bench_ecmult_setup but can't find the source of this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think is this is just a standard integer hash function (akin to https://en.wikipedia.org/wiki/Universal_hashing#Hashing_integers) whose parameters sipa selected. But looking at it now it may be give poor results because POINTS is not prime.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think points needs to be prime, so long as the multipliers are coprime to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fwiw they're all coprime to POINTS.

Copy link
Contributor

@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.

Looks good otherwise

bench_ecmult_teardown_helper(data, &data->offset1, &data->offset2, NULL);
}

static void bench_ecmult_1g(void* arg) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
static void bench_ecmult_1g(void* arg) {
static void bench_ecmult_0g(void* arg) {

}
}

static void bench_ecmult_1g_teardown(void* arg) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
static void bench_ecmult_1g_teardown(void* arg) {
static void bench_ecmult_0g_teardown(void* arg) {

and so on..

bench_ecmult_teardown_helper(data, NULL, NULL, &data->offset1);
}

static void bench_ecmult_2g(void* arg) {
Copy link
Contributor

Choose a reason for hiding this comment

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

1g

@jonasnick
Copy link
Contributor Author

jonasnick commented Oct 28, 2019

@real-or-random I don't think so (unless we change it for ecmult_multi) but it took me a while to figure this out again. I added a commit that printfs an explanation. It's not pretty, but less confusing.

@real-or-random
Copy link
Contributor

@real-or-random I don't think so (unless we change it for ecmult_multi) but it took me a while to figure this out again. I added a commit that printfs and explanation. It's not pretty, but less confusing.

Certainly pretty enough for a benchmark. Can you squash?

@jonasnick
Copy link
Contributor Author

squashed

src/bench_ecmult.c Outdated Show resolved Hide resolved
@real-or-random
Copy link
Contributor

ACK a419f3d I read the code and tried it

@jonasnick jonasnick force-pushed the ecmult-bench branch 2 times, most recently from 5ebe192 to 82a5911 Compare April 30, 2020 14:05
@jonasnick
Copy link
Contributor Author

Rebased and added a --help command instead of printing an explanation of the output every time this is run.

} else {
fprintf(stderr, "%s: unrecognized argument '%s'.\n", argv[0], argv[1]);
fprintf(stderr, "Use 'pippenger_wnaf', 'strauss_wnaf', 'simple' or no argument to benchmark a combined algorithm.\n");
fprintf(stderr, "Use '--help', 'pippenger_wnaf', 'strauss_wnaf', 'simple' or no argument to benchmark a combined algorithm.\n");
Copy link
Contributor

@real-or-random real-or-random May 1, 2020

Choose a reason for hiding this comment

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

simply call help here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah sorry, my suggestion was just to replace the entire else branch with help(), not to replace the --help by help. Feel free to change back, or to ignore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

data.ecmult_multi = secp256k1_ecmult_multi_var;
secp256k1_scratch_space_destroy(data.ctx, data.scratch);
data.scratch = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

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

That line got lost, and this was what made the difference between simple and combined? I see you merged this into "simple combined" in the printfs. Was this change on purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I also forgot what the simple algorithm actually does. I fixed this and added an explanation to the help().

@jonasnick
Copy link
Contributor Author

@real-or-random care to reACK :) ?

@real-or-random
Copy link
Contributor

ACK ee83dbf

@jonasnick jonasnick force-pushed the ecmult-bench branch 2 times, most recently from c6867e1 to 8f879c2 Compare May 31, 2021 20:45
@jonasnick
Copy link
Contributor Author

Rebased to let CI do its thing

@sipa
Copy link
Contributor

sipa commented May 31, 2021

While we're at it: are no-g variants not interesting?

@jonasnick
Copy link
Contributor Author

Hm with-g seems more interesting (schnorrsig batch verify, taproot tweaks, pedersen commitments,...) but we could add a flag or something for no-g (preferably in a separate PR imho).

@real-or-random
Copy link
Contributor

ACK 8f879c2

@elichai
Copy link
Contributor

elichai commented Jun 6, 2021

tACK 8f879c2

nit, It is a little confusing that the first 5 benchmarks are unrelated to the flag being passed (Maybe we want to move the "Using XXX algorithm" print to just before the relevant benchmarks?)

@real-or-random
Copy link
Contributor

nit, It is a little confusing that the first 5 benchmarks are unrelated to the flag being passed (Maybe we want to move the "Using XXX algorithm" print to just before the relevant benchmarks?)

Indeed but this can be done in a separate PR, too.

Let me take the opportunity to merge this PR now that we have two ACKs.

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.

None yet

5 participants