Skip to content

Commit 410abb2

Browse files
committed
batch: Generate speedup graphs
This commit generates two semi-log graphs that visualize the batch verification speed up over single verification (y-axis) wrt the number of signatures (or tweak checks) in the batch (x-axis). The input data points are taken from the batch verify benchmark. GNU plot was used to generate these graphs (plot.gp file). The instructions to reproduce these graphs (on your local machine) are given in doc/speedup-batch.md file. The value of `STRAUSS_MAX_TERMS_PER_BATCH` was calculated (approx) from the generated graphs. Relevant discussion: siv2r#2 (comment)
1 parent e5df505 commit 410abb2

File tree

9 files changed

+240
-0
lines changed

9 files changed

+240
-0
lines changed

doc/speedup-batch.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Schnorrsig Batch Verification Speedup
2+
3+
![Speedup over single verification](speedup-batch/schnorrsig-speedup-batch.png)
4+
5+
# Tweak Pubkey Check Batch Verification Speedup
6+
7+
![Speedup over single verification](speedup-batch/tweakcheck-speedup-batch.png)
8+
9+
Build steps
10+
-----------
11+
To generate the above graphs on your local machine:
12+
13+
$ cd doc/speedup-batch
14+
$ make
15+
$ make speedup-batch.png
16+
17+
<!-- TODO: add additional notes on the API -->

doc/speedup-batch/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.dat

doc/speedup-batch/Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
schnorrsig_data = schnorrsig_batch.dat schnorrsig_single.dat
2+
tweak_data = tweak_batch.dat tweak_single.dat
3+
4+
bench_output.txt: bench.sh
5+
SECP256K1_BENCH_ITERS=500000 ./bench.sh bench_output.txt
6+
7+
schnorrsig_batch.dat: bench_output.txt
8+
cat bench_output.txt | grep -v "batchverify_schnorrsigs_1 " | awk '{ gsub(/ /,""); print }' | gawk -F, 'match($$0, /batchverify_schnorrsigs_([0-9]+)/, arr) {print arr[1] " " $$3}' > schnorrsig_batch.dat
9+
10+
schnorrsig_single.dat: bench_output.txt
11+
cat bench_output.txt | awk '{ gsub(/ /,""); print }' | awk -F, 'match($$0, /schnorrsig_verify/) {print $$3}' > schnorrsig_single.dat
12+
13+
tweak_batch.dat: bench_output.txt
14+
cat bench_output.txt | grep -v "batchverify_tweak_checks_1 " | awk '{ gsub(/ /,""); print }' | gawk -F, 'match($$0, /batchverify_tweak_checks_([0-9]+)/, arr) {print arr[1] " " $$3}' > tweak_batch.dat
15+
16+
tweak_single.dat: bench_output.txt
17+
cat bench_output.txt | awk '{ gsub(/ /,""); print }' | awk -F, 'match($$0, /tweak_add_check/) {print $$3}' > tweak_single.dat
18+
19+
speedup-batch.png: $(schnorrsig_data) $(tweak_data) plot.gp
20+
gnuplot plot.gp
21+
22+
clean:
23+
rm *.log *.txt *.dat *.png

doc/speedup-batch/bench.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
output_file=$1
4+
cur_dir=$(pwd)
5+
6+
cd ../../
7+
echo "HEAD: $(git rev-parse --short HEAD)" > "$cur_dir/$output_file.log"
8+
make clean
9+
./autogen.sh
10+
./configure --enable-experimental --enable-module-batch --enable-module-schnorrsig >> "$cur_dir/$output_file.log"
11+
make
12+
./bench schnorrsig > "$cur_dir/$output_file"
13+
./bench extrakeys >> "$cur_dir/$output_file"

doc/speedup-batch/bench_output.txt

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
Benchmark , Min(us) , Avg(us) , Max(us)
2+
3+
schnorrsig_sign , 7.57 , 7.60 , 7.64
4+
schnorrsig_verify , 15.2 , 15.3 , 15.5
5+
batchverify_schnorrsigs_1 , 17.8 , 17.9 , 18.1
6+
batchverify_schnorrsigs_2 , 15.6 , 15.8 , 15.9
7+
batchverify_schnorrsigs_3 , 14.7 , 14.8 , 14.8
8+
batchverify_schnorrsigs_4 , 14.4 , 14.4 , 14.4
9+
batchverify_schnorrsigs_5 , 14.1 , 14.1 , 14.2
10+
batchverify_schnorrsigs_7 , 13.9 , 13.9 , 13.9
11+
batchverify_schnorrsigs_9 , 13.6 , 13.7 , 13.8
12+
batchverify_schnorrsigs_11 , 13.7 , 13.8 , 13.8
13+
batchverify_schnorrsigs_14 , 13.3 , 13.5 , 13.6
14+
batchverify_schnorrsigs_17 , 13.5 , 13.5 , 13.5
15+
batchverify_schnorrsigs_21 , 13.4 , 13.4 , 13.5
16+
batchverify_schnorrsigs_26 , 13.3 , 13.3 , 13.4
17+
batchverify_schnorrsigs_32 , 13.3 , 13.4 , 13.4
18+
batchverify_schnorrsigs_39 , 13.3 , 13.3 , 13.4
19+
batchverify_schnorrsigs_47 , 13.2 , 13.2 , 13.2
20+
batchverify_schnorrsigs_57 , 13.2 , 13.4 , 13.5
21+
batchverify_schnorrsigs_69 , 13.2 , 13.3 , 13.4
22+
batchverify_schnorrsigs_83 , 13.3 , 13.3 , 13.4
23+
batchverify_schnorrsigs_100 , 13.2 , 13.3 , 13.3
24+
batchverify_schnorrsigs_121 , 13.2 , 13.3 , 13.5
25+
batchverify_schnorrsigs_146 , 13.4 , 13.5 , 13.5
26+
batchverify_schnorrsigs_176 , 13.3 , 13.4 , 13.5
27+
batchverify_schnorrsigs_212 , 13.2 , 13.3 , 13.5
28+
batchverify_schnorrsigs_255 , 13.2 , 13.3 , 13.4
29+
batchverify_schnorrsigs_307 , 13.3 , 13.3 , 13.3
30+
batchverify_schnorrsigs_369 , 13.3 , 13.3 , 13.3
31+
batchverify_schnorrsigs_443 , 13.3 , 13.3 , 13.3
32+
batchverify_schnorrsigs_532 , 13.4 , 13.5 , 13.6
33+
batchverify_schnorrsigs_639 , 13.5 , 13.6 , 13.6
34+
batchverify_schnorrsigs_767 , 13.3 , 13.3 , 13.4
35+
batchverify_schnorrsigs_921 , 13.3 , 13.4 , 13.4
36+
batchverify_schnorrsigs_1106 , 13.3 , 13.3 , 13.3
37+
batchverify_schnorrsigs_1328 , 13.3 , 13.3 , 13.3
38+
batchverify_schnorrsigs_1594 , 13.3 , 13.3 , 13.3
39+
batchverify_schnorrsigs_1913 , 13.2 , 13.3 , 13.3
40+
batchverify_schnorrsigs_2296 , 13.3 , 13.3 , 13.3
41+
batchverify_schnorrsigs_2756 , 13.2 , 13.3 , 13.3
42+
batchverify_schnorrsigs_3308 , 13.3 , 13.3 , 13.3
43+
batchverify_schnorrsigs_3970 , 13.3 , 13.6 , 13.8
44+
batchverify_schnorrsigs_4765 , 13.3 , 13.3 , 13.4
45+
batchverify_schnorrsigs_5719 , 13.3 , 13.4 , 13.7
46+
batchverify_schnorrsigs_6863 , 13.4 , 13.5 , 13.7
47+
batchverify_schnorrsigs_8236 , 13.3 , 13.3 , 13.4
48+
batchverify_schnorrsigs_9884 , 13.2 , 13.3 , 13.3
49+
batchverify_schnorrsigs_11861 , 13.3 , 13.3 , 13.3
50+
batchverify_schnorrsigs_14234 , 13.2 , 13.3 , 13.3
51+
batchverify_schnorrsigs_17081 , 13.3 , 13.3 , 13.3
52+
batchverify_schnorrsigs_20498 , 13.3 , 13.3 , 13.3
53+
batchverify_schnorrsigs_24598 , 13.2 , 13.2 , 13.3
54+
batchverify_schnorrsigs_29518 , 13.2 , 13.3 , 13.3
55+
batchverify_schnorrsigs_35422 , 13.2 , 13.2 , 13.3
56+
batchverify_schnorrsigs_42507 , 13.2 , 13.3 , 13.3
57+
batchverify_schnorrsigs_51009 , 13.2 , 13.2 , 13.3
58+
batchverify_schnorrsigs_61211 , 13.2 , 13.2 , 13.2
59+
batchverify_schnorrsigs_73454 , 13.2 , 13.2 , 13.2
60+
batchverify_schnorrsigs_88145 , 13.2 , 13.2 , 13.3
61+
batchverify_schnorrsigs_105775 , 13.2 , 13.2 , 13.2
62+
batchverify_schnorrsigs_126931 , 13.2 , 13.2 , 13.3
63+
batchverify_schnorrsigs_152318 , 13.3 , 13.3 , 13.3
64+
batchverify_schnorrsigs_182782 , 13.2 , 13.3 , 13.3
65+
batchverify_schnorrsigs_219339 , 13.2 , 13.3 , 13.3
66+
batchverify_schnorrsigs_263207 , 13.2 , 13.2 , 13.3
67+
batchverify_schnorrsigs_315849 , 13.2 , 13.3 , 13.3
68+
batchverify_schnorrsigs_379019 , 13.1 , 13.2 , 13.3
69+
batchverify_schnorrsigs_454823 , 13.2 , 13.2 , 13.3
70+
Benchmark , Min(us) , Avg(us) , Max(us)
71+
72+
tweak_add_check , 11.4 , 11.5 , 11.6
73+
batchverify_tweak_checks_1 , 12.2 , 12.2 , 12.2
74+
batchverify_tweak_checks_2 , 10.2 , 10.2 , 10.2
75+
batchverify_tweak_checks_3 , 9.40 , 9.45 , 9.48
76+
batchverify_tweak_checks_4 , 9.01 , 9.03 , 9.05
77+
batchverify_tweak_checks_5 , 8.76 , 8.81 , 8.84
78+
batchverify_tweak_checks_7 , 8.52 , 8.55 , 8.58
79+
batchverify_tweak_checks_9 , 8.36 , 8.39 , 8.40
80+
batchverify_tweak_checks_11 , 8.24 , 8.27 , 8.29
81+
batchverify_tweak_checks_14 , 8.15 , 8.18 , 8.21
82+
batchverify_tweak_checks_17 , 8.11 , 8.12 , 8.14
83+
batchverify_tweak_checks_21 , 8.02 , 8.05 , 8.08
84+
batchverify_tweak_checks_26 , 7.99 , 8.00 , 8.00
85+
batchverify_tweak_checks_32 , 7.97 , 8.01 , 8.07
86+
batchverify_tweak_checks_39 , 7.91 , 7.92 , 7.94
87+
batchverify_tweak_checks_47 , 7.86 , 7.91 , 7.95
88+
batchverify_tweak_checks_57 , 7.93 , 7.94 , 7.95
89+
batchverify_tweak_checks_69 , 7.89 , 7.92 , 7.94
90+
batchverify_tweak_checks_83 , 7.85 , 7.88 , 7.90
91+
batchverify_tweak_checks_100 , 7.85 , 7.89 , 7.91
92+
batchverify_tweak_checks_121 , 7.93 , 7.95 , 7.98
93+
batchverify_tweak_checks_146 , 7.87 , 7.92 , 7.95
94+
batchverify_tweak_checks_176 , 7.91 , 7.92 , 7.93
95+
batchverify_tweak_checks_212 , 7.85 , 7.89 , 7.90
96+
batchverify_tweak_checks_255 , 7.89 , 7.91 , 7.93
97+
batchverify_tweak_checks_307 , 7.84 , 7.88 , 7.90
98+
batchverify_tweak_checks_369 , 7.86 , 7.89 , 7.91
99+
batchverify_tweak_checks_443 , 7.92 , 7.93 , 7.93
100+
batchverify_tweak_checks_532 , 7.91 , 7.91 , 7.92
101+
batchverify_tweak_checks_639 , 7.87 , 7.89 , 7.91
102+
batchverify_tweak_checks_767 , 7.90 , 7.90 , 7.90
103+
batchverify_tweak_checks_921 , 7.89 , 7.90 , 7.90
104+
batchverify_tweak_checks_1106 , 7.86 , 7.88 , 7.89
105+
batchverify_tweak_checks_1328 , 7.85 , 7.88 , 7.91
106+
batchverify_tweak_checks_1594 , 7.84 , 7.88 , 7.92
107+
batchverify_tweak_checks_1913 , 7.90 , 7.91 , 7.92
108+
batchverify_tweak_checks_2296 , 7.82 , 7.89 , 8.00
109+
batchverify_tweak_checks_2756 , 7.88 , 7.91 , 7.93
110+
batchverify_tweak_checks_3308 , 7.93 , 7.94 , 7.97
111+
batchverify_tweak_checks_3970 , 7.91 , 7.92 , 7.92
112+
batchverify_tweak_checks_4765 , 7.86 , 7.91 , 7.96
113+
batchverify_tweak_checks_5719 , 7.94 , 7.99 , 8.02
114+
batchverify_tweak_checks_6863 , 7.86 , 7.90 , 7.94
115+
batchverify_tweak_checks_8236 , 7.93 , 7.93 , 7.93
116+
batchverify_tweak_checks_9884 , 7.91 , 7.94 , 8.01
117+
batchverify_tweak_checks_11861 , 7.88 , 7.93 , 8.00
118+
batchverify_tweak_checks_14234 , 7.86 , 7.87 , 7.88
119+
batchverify_tweak_checks_17081 , 7.90 , 7.91 , 7.92
120+
batchverify_tweak_checks_20498 , 7.92 , 7.93 , 7.94
121+
batchverify_tweak_checks_24598 , 7.90 , 7.92 , 7.94
122+
batchverify_tweak_checks_29518 , 7.88 , 7.93 , 7.96
123+
batchverify_tweak_checks_35422 , 7.91 , 7.92 , 7.93
124+
batchverify_tweak_checks_42507 , 7.90 , 7.91 , 7.93
125+
batchverify_tweak_checks_51009 , 7.90 , 7.91 , 7.92
126+
batchverify_tweak_checks_61211 , 7.90 , 7.92 , 7.95
127+
batchverify_tweak_checks_73454 , 7.91 , 7.95 , 7.98
128+
batchverify_tweak_checks_88145 , 7.91 , 7.95 , 8.02
129+
batchverify_tweak_checks_105775 , 7.99 , 8.04 , 8.08
130+
batchverify_tweak_checks_126931 , 7.88 , 7.96 , 8.03
131+
batchverify_tweak_checks_152318 , 7.92 , 7.96 , 7.99
132+
batchverify_tweak_checks_182782 , 7.94 , 7.95 , 7.96
133+
batchverify_tweak_checks_219339 , 7.96 , 8.04 , 8.10
134+
batchverify_tweak_checks_263207 , 7.94 , 7.97 , 8.00
135+
batchverify_tweak_checks_315849 , 7.91 , 7.93 , 7.95
136+
batchverify_tweak_checks_379019 , 7.87 , 7.89 , 7.92
137+
batchverify_tweak_checks_454823 , 7.81 , 7.88 , 7.92

doc/speedup-batch/plot.gp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
set style line 80 lt rgb "#808080"
2+
set style line 81 lt 0
3+
set style line 81 lt rgb "#808080"
4+
set grid back linestyle 81
5+
set border 3 back linestyle 80
6+
set xtics nomirror
7+
set ytics nomirror
8+
set style line 1 lt rgb "#A00000" lw 2 pt 1
9+
set style line 2 lt rgb "#00A000" lw 2 pt 6
10+
set style line 3 lt rgb "#5060D0" lw 2 pt 2
11+
set style line 4 lt rgb "#F25900" lw 2 pt 9
12+
set key bottom right
13+
set autoscale
14+
unset log
15+
unset label
16+
set xtic auto
17+
set ytic auto
18+
set title "Batch signature verification in libsecp256k1"
19+
set xlabel "Number of signatures (logarithmic)"
20+
set ylabel "Verification time per signature in us"
21+
set grid
22+
set logscale x
23+
set mxtics 10
24+
25+
# Generate graph of Schnorr signature benchmark
26+
schnorrsig_single_val=system("cat schnorrsig_single.dat")
27+
set xrange [1.1:]
28+
set xtics add ("2" 2)
29+
set yrange [0.9:]
30+
set ytics -1,0.1,3
31+
set ylabel "Speedup over single verification"
32+
set term png size 800,600
33+
set output 'schnorrsig-speedup-batch.png'
34+
plot "schnorrsig_batch.dat" using 1:(schnorrsig_single_val/$2) with points title "" ls 1
35+
36+
# Generate graph of tweaked x-only pubkey check benchmark
37+
set title "Batch tweaked x-only pubkey check in libsecp256k1"
38+
set xlabel "Number of tweak checks (logarithmic)"
39+
tweak_single_val=system("cat tweak_single.dat")
40+
set output 'tweakcheck-speedup-batch.png'
41+
plot "tweak_batch.dat" using 1:(tweak_single_val/$2) with points title "" ls 1
Loading
21.9 KB
Loading

src/modules/batch/main_impl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
#include "../../../include/secp256k1_batch.h"
55

6+
/* Maximum number of scalar-point pairs on the batch
7+
* for which `secp256k1_batch_verify` remains efficient */
8+
#define STRAUSS_MAX_TERMS_PER_BATCH 106
9+
610
/* Assume two batch objects (batch1 and batch2) and we call
711
* `batch_add_tweak_check` on batch1 and `batch_add_schnorrsig` on batch2.
812
* In this case, the same randomizer will be generated if the input bytes to
@@ -102,6 +106,10 @@ secp256k1_batch* secp256k1_batch_create(const secp256k1_context* ctx, size_t max
102106
secp256k1_batch* batch;
103107
size_t batch_scratch_size;
104108
unsigned char zeros[16] = {0};
109+
/* max number of scalar-point pairs on scratch up to which Strauss multi multiplication is efficient */
110+
if (max_terms > STRAUSS_MAX_TERMS_PER_BATCH) {
111+
max_terms = STRAUSS_MAX_TERMS_PER_BATCH;
112+
}
105113

106114
VERIFY_CHECK(ctx != NULL);
107115
ARG_CHECK(max_terms != 0);

0 commit comments

Comments
 (0)