diff options
author | jsing <> | 2023-01-21 13:24:39 +0000 |
---|---|---|
committer | jsing <> | 2023-01-21 13:24:39 +0000 |
commit | 3a20ca756814113d7d16b311e6fd40cd15819098 (patch) | |
tree | b700c4688682d90bb44aefe4d474a6f6ed82c0aa /src | |
parent | 71fbf41bd2b9e40f88e44c107ffcde8405fa4f14 (diff) | |
download | openbsd-3a20ca756814113d7d16b311e6fd40cd15819098.tar.gz openbsd-3a20ca756814113d7d16b311e6fd40cd15819098.tar.bz2 openbsd-3a20ca756814113d7d16b311e6fd40cd15819098.zip |
Provide basic benchmarks for BN_mul() and BN_sqr().
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libcrypto/bn/Makefile | 6 | ||||
-rw-r--r-- | src/regress/lib/libcrypto/bn/bn_mul.c | 271 |
2 files changed, 275 insertions, 2 deletions
diff --git a/src/regress/lib/libcrypto/bn/Makefile b/src/regress/lib/libcrypto/bn/Makefile index 6e89e026b2..ad15acfac4 100644 --- a/src/regress/lib/libcrypto/bn/Makefile +++ b/src/regress/lib/libcrypto/bn/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile,v 1.18 2022/12/17 22:31:10 tb Exp $ | 1 | # $OpenBSD: Makefile,v 1.19 2023/01/21 13:24:39 jsing Exp $ |
2 | 2 | ||
3 | PROGS += bn_add_sub | 3 | PROGS += bn_add_sub |
4 | PROGS += bn_cmp | 4 | PROGS += bn_cmp |
@@ -8,6 +8,7 @@ PROGS += bn_mod_exp2_mont | |||
8 | PROGS += bn_mod_exp_zero | 8 | PROGS += bn_mod_exp_zero |
9 | PROGS += bn_mod_sqrt | 9 | PROGS += bn_mod_sqrt |
10 | PROGS += bn_mont | 10 | PROGS += bn_mont |
11 | PROGS += bn_mul | ||
11 | PROGS += bn_primes | 12 | PROGS += bn_primes |
12 | PROGS += bn_rand_interval | 13 | PROGS += bn_rand_interval |
13 | PROGS += bn_shift | 14 | PROGS += bn_shift |
@@ -60,7 +61,8 @@ run-bc: bn_test.out | |||
60 | 61 | ||
61 | CLEANFILES += bn_test.out bc.out | 62 | CLEANFILES += bn_test.out bc.out |
62 | 63 | ||
63 | benchmark: bn_shift | 64 | benchmark: bn_mul bn_shift |
65 | ./bn_mul --benchmark | ||
64 | ./bn_shift --benchmark | 66 | ./bn_shift --benchmark |
65 | .PHONY: benchmark | 67 | .PHONY: benchmark |
66 | 68 | ||
diff --git a/src/regress/lib/libcrypto/bn/bn_mul.c b/src/regress/lib/libcrypto/bn/bn_mul.c new file mode 100644 index 0000000000..de1f5c6b76 --- /dev/null +++ b/src/regress/lib/libcrypto/bn/bn_mul.c | |||
@@ -0,0 +1,271 @@ | |||
1 | /* $OpenBSD: bn_mul.c,v 1.1 2023/01/21 13:24:39 jsing Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #include <sys/time.h> | ||
19 | |||
20 | #include <err.h> | ||
21 | #include <signal.h> | ||
22 | #include <stdio.h> | ||
23 | #include <string.h> | ||
24 | #include <time.h> | ||
25 | #include <unistd.h> | ||
26 | |||
27 | #include <openssl/bn.h> | ||
28 | |||
29 | static int | ||
30 | benchmark_bn_mul_setup(BIGNUM *a, size_t a_bits, BIGNUM *b, size_t b_bits, | ||
31 | BIGNUM *r) | ||
32 | { | ||
33 | if (!BN_rand(a, a_bits - 1, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) | ||
34 | return 0; | ||
35 | if (!BN_rand(b, b_bits - 1, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) | ||
36 | return 0; | ||
37 | if (!BN_set_bit(r, (a_bits + b_bits) - 1)) | ||
38 | return 0; | ||
39 | |||
40 | return 1; | ||
41 | } | ||
42 | |||
43 | static void | ||
44 | benchmark_bn_mul_run_once(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *bn_ctx) | ||
45 | { | ||
46 | if (!BN_mul(r, a, b, bn_ctx)) | ||
47 | errx(1, "BN_mul"); | ||
48 | } | ||
49 | |||
50 | static int | ||
51 | benchmark_bn_sqr_setup(BIGNUM *a, size_t a_bits, BIGNUM *b, size_t b_bits, | ||
52 | BIGNUM *r) | ||
53 | { | ||
54 | if (!BN_rand(a, a_bits - 1, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) | ||
55 | return 0; | ||
56 | if (!BN_set_bit(r, (a_bits + a_bits) - 1)) | ||
57 | return 0; | ||
58 | |||
59 | return 1; | ||
60 | } | ||
61 | |||
62 | static void | ||
63 | benchmark_bn_sqr_run_once(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *bn_ctx) | ||
64 | { | ||
65 | if (!BN_sqr(r, a, bn_ctx)) | ||
66 | errx(1, "BN_sqr"); | ||
67 | } | ||
68 | |||
69 | struct benchmark { | ||
70 | const char *desc; | ||
71 | int (*setup)(BIGNUM *, size_t, BIGNUM *, size_t, BIGNUM *); | ||
72 | void (*run_once)(BIGNUM *, BIGNUM *, BIGNUM *, BN_CTX *); | ||
73 | size_t a_bits; | ||
74 | size_t b_bits; | ||
75 | }; | ||
76 | |||
77 | struct benchmark benchmarks[] = { | ||
78 | { | ||
79 | .desc = "BN_mul (128 bit x 128 bit)", | ||
80 | .setup = benchmark_bn_mul_setup, | ||
81 | .run_once = benchmark_bn_mul_run_once, | ||
82 | .a_bits = 128, | ||
83 | .b_bits = 128, | ||
84 | }, | ||
85 | { | ||
86 | .desc = "BN_mul (128 bit x 256 bit)", | ||
87 | .setup = benchmark_bn_mul_setup, | ||
88 | .run_once = benchmark_bn_mul_run_once, | ||
89 | .a_bits = 128, | ||
90 | .b_bits = 256, | ||
91 | }, | ||
92 | { | ||
93 | .desc = "BN_mul (256 bit x 256 bit)", | ||
94 | .setup = benchmark_bn_mul_setup, | ||
95 | .run_once = benchmark_bn_mul_run_once, | ||
96 | .a_bits = 256, | ||
97 | .b_bits = 256, | ||
98 | }, | ||
99 | { | ||
100 | .desc = "BN_mul (512 bit x 512 bit)", | ||
101 | .setup = benchmark_bn_mul_setup, | ||
102 | .run_once = benchmark_bn_mul_run_once, | ||
103 | .a_bits = 512, | ||
104 | .b_bits = 512, | ||
105 | }, | ||
106 | { | ||
107 | .desc = "BN_mul (1024 bit x 1024 bit)", | ||
108 | .setup = benchmark_bn_mul_setup, | ||
109 | .run_once = benchmark_bn_mul_run_once, | ||
110 | .a_bits = 1024, | ||
111 | .b_bits = 1024, | ||
112 | }, | ||
113 | { | ||
114 | .desc = "BN_mul (1024 bit x 2048 bit)", | ||
115 | .setup = benchmark_bn_mul_setup, | ||
116 | .run_once = benchmark_bn_mul_run_once, | ||
117 | .a_bits = 1024, | ||
118 | .b_bits = 2048, | ||
119 | }, | ||
120 | { | ||
121 | .desc = "BN_mul (2048 bit x 2048 bit)", | ||
122 | .setup = benchmark_bn_mul_setup, | ||
123 | .run_once = benchmark_bn_mul_run_once, | ||
124 | .a_bits = 2048, | ||
125 | .b_bits = 2048, | ||
126 | }, | ||
127 | { | ||
128 | .desc = "BN_mul (4096 bit x 4096 bit)", | ||
129 | .setup = benchmark_bn_mul_setup, | ||
130 | .run_once = benchmark_bn_mul_run_once, | ||
131 | .a_bits = 4096, | ||
132 | .b_bits = 4096, | ||
133 | }, | ||
134 | { | ||
135 | .desc = "BN_mul (4096 bit x 8192 bit)", | ||
136 | .setup = benchmark_bn_mul_setup, | ||
137 | .run_once = benchmark_bn_mul_run_once, | ||
138 | .a_bits = 4096, | ||
139 | .b_bits = 8192, | ||
140 | }, | ||
141 | { | ||
142 | .desc = "BN_mul (8192 bit x 8192 bit)", | ||
143 | .setup = benchmark_bn_mul_setup, | ||
144 | .run_once = benchmark_bn_mul_run_once, | ||
145 | .a_bits = 8192, | ||
146 | .b_bits = 8192, | ||
147 | }, | ||
148 | { | ||
149 | .desc = "BN_sqr (128 bit)", | ||
150 | .setup = benchmark_bn_sqr_setup, | ||
151 | .run_once = benchmark_bn_sqr_run_once, | ||
152 | .a_bits = 128, | ||
153 | }, | ||
154 | { | ||
155 | .desc = "BN_sqr (256 bit)", | ||
156 | .setup = benchmark_bn_sqr_setup, | ||
157 | .run_once = benchmark_bn_sqr_run_once, | ||
158 | .a_bits = 256, | ||
159 | }, | ||
160 | { | ||
161 | .desc = "BN_sqr (512 bit)", | ||
162 | .setup = benchmark_bn_sqr_setup, | ||
163 | .run_once = benchmark_bn_sqr_run_once, | ||
164 | .a_bits = 512, | ||
165 | }, | ||
166 | { | ||
167 | .desc = "BN_sqr (1024 bit)", | ||
168 | .setup = benchmark_bn_sqr_setup, | ||
169 | .run_once = benchmark_bn_sqr_run_once, | ||
170 | .a_bits = 1024, | ||
171 | }, | ||
172 | { | ||
173 | .desc = "BN_sqr (2048 bit)", | ||
174 | .setup = benchmark_bn_sqr_setup, | ||
175 | .run_once = benchmark_bn_sqr_run_once, | ||
176 | .a_bits = 2048, | ||
177 | }, | ||
178 | { | ||
179 | .desc = "BN_sqr (4096 bit)", | ||
180 | .setup = benchmark_bn_sqr_setup, | ||
181 | .run_once = benchmark_bn_sqr_run_once, | ||
182 | .a_bits = 4096, | ||
183 | }, | ||
184 | { | ||
185 | .desc = "BN_sqr (8192 bit)", | ||
186 | .setup = benchmark_bn_sqr_setup, | ||
187 | .run_once = benchmark_bn_sqr_run_once, | ||
188 | .a_bits = 8192, | ||
189 | }, | ||
190 | }; | ||
191 | |||
192 | #define N_BENCHMARKS (sizeof(benchmarks) / sizeof(benchmarks[0])) | ||
193 | |||
194 | static volatile sig_atomic_t benchmark_stop; | ||
195 | |||
196 | static void | ||
197 | benchmark_sig_alarm(int sig) | ||
198 | { | ||
199 | benchmark_stop = 1; | ||
200 | } | ||
201 | |||
202 | static void | ||
203 | benchmark_run(const struct benchmark *bm, int seconds) | ||
204 | { | ||
205 | struct timespec start, end, duration; | ||
206 | BIGNUM *a, *b, *r; | ||
207 | BN_CTX *bn_ctx; | ||
208 | int i; | ||
209 | |||
210 | signal(SIGALRM, benchmark_sig_alarm); | ||
211 | |||
212 | if ((bn_ctx = BN_CTX_new()) == NULL) | ||
213 | errx(1, "BN_CTX_new"); | ||
214 | |||
215 | BN_CTX_start(bn_ctx); | ||
216 | |||
217 | if ((a = BN_CTX_get(bn_ctx)) == NULL) | ||
218 | errx(1, "BN_CTX_get"); | ||
219 | if ((b = BN_CTX_get(bn_ctx)) == NULL) | ||
220 | errx(1, "BN_CTX_get"); | ||
221 | if ((r = BN_CTX_get(bn_ctx)) == NULL) | ||
222 | errx(1, "BN_CTX_get"); | ||
223 | |||
224 | if (!bm->setup(a, bm->a_bits, b, bm->b_bits, r)) | ||
225 | errx(1, "benchmark setup failed"); | ||
226 | |||
227 | benchmark_stop = 0; | ||
228 | i = 0; | ||
229 | alarm(seconds); | ||
230 | |||
231 | clock_gettime(CLOCK_MONOTONIC, &start); | ||
232 | |||
233 | fprintf(stderr, "Benchmarking %s for %ds: ", bm->desc, seconds); | ||
234 | while (!benchmark_stop) { | ||
235 | bm->run_once(r, a, b, bn_ctx); | ||
236 | i++; | ||
237 | } | ||
238 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
239 | timespecsub(&end, &start, &duration); | ||
240 | fprintf(stderr, "%d iterations in %f seconds\n", i, | ||
241 | duration.tv_sec + duration.tv_nsec / 1000000000.0); | ||
242 | |||
243 | BN_CTX_end(bn_ctx); | ||
244 | BN_CTX_free(bn_ctx); | ||
245 | } | ||
246 | |||
247 | static void | ||
248 | benchmark_bn_mul_sqr(void) | ||
249 | { | ||
250 | const struct benchmark *bm; | ||
251 | size_t i; | ||
252 | |||
253 | for (i = 0; i < N_BENCHMARKS; i++) { | ||
254 | bm = &benchmarks[i]; | ||
255 | benchmark_run(bm, 5); | ||
256 | } | ||
257 | } | ||
258 | |||
259 | int | ||
260 | main(int argc, char **argv) | ||
261 | { | ||
262 | int benchmark = 0, failed = 0; | ||
263 | |||
264 | if (argc == 2 && strcmp(argv[1], "--benchmark") == 0) | ||
265 | benchmark = 1; | ||
266 | |||
267 | if (benchmark && !failed) | ||
268 | benchmark_bn_mul_sqr(); | ||
269 | |||
270 | return failed; | ||
271 | } | ||