summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2023-01-29 15:26:55 +0000
committerjsing <>2023-01-29 15:26:55 +0000
commitaf65aea58ca04917a71384b3049d8888bb88e79d (patch)
treee3b644b3f86acf56750b56b2785b8eb302fad23b /src
parentac3a3e69b377c91c63fb467b1d62980b111016f2 (diff)
downloadopenbsd-af65aea58ca04917a71384b3049d8888bb88e79d.tar.gz
openbsd-af65aea58ca04917a71384b3049d8888bb88e79d.tar.bz2
openbsd-af65aea58ca04917a71384b3049d8888bb88e79d.zip
Add benchmarks for BN_div()
Diffstat (limited to 'src')
-rw-r--r--src/regress/lib/libcrypto/bn/bn_mul_div.c157
1 files changed, 147 insertions, 10 deletions
diff --git a/src/regress/lib/libcrypto/bn/bn_mul_div.c b/src/regress/lib/libcrypto/bn/bn_mul_div.c
index 2615904c45..28f8f75646 100644
--- a/src/regress/lib/libcrypto/bn/bn_mul_div.c
+++ b/src/regress/lib/libcrypto/bn/bn_mul_div.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mul_div.c,v 1.2 2023/01/29 15:22:12 jsing Exp $ */ 1/* $OpenBSD: bn_mul_div.c,v 1.3 2023/01/29 15:26:55 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2023 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -27,8 +27,31 @@
27#include <openssl/bn.h> 27#include <openssl/bn.h>
28 28
29static int 29static int
30benchmark_bn_div_setup(BIGNUM *a, size_t a_bits, BIGNUM *b, size_t b_bits,
31 BIGNUM *r, BIGNUM *q)
32{
33 if (!BN_rand(a, a_bits, 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))
38 return 0;
39 if (!BN_set_bit(q, b_bits))
40 return 0;
41
42 return 1;
43}
44
45static void
46benchmark_bn_div_run_once(BIGNUM *r, BIGNUM *q, BIGNUM *a, BIGNUM *b, BN_CTX *bn_ctx)
47{
48 if (!BN_div(r, q, a, b, bn_ctx))
49 errx(1, "BN_div");
50}
51
52static int
30benchmark_bn_mul_setup(BIGNUM *a, size_t a_bits, BIGNUM *b, size_t b_bits, 53benchmark_bn_mul_setup(BIGNUM *a, size_t a_bits, BIGNUM *b, size_t b_bits,
31 BIGNUM *r) 54 BIGNUM *r, BIGNUM *q)
32{ 55{
33 if (!BN_rand(a, a_bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) 56 if (!BN_rand(a, a_bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
34 return 0; 57 return 0;
@@ -41,7 +64,7 @@ benchmark_bn_mul_setup(BIGNUM *a, size_t a_bits, BIGNUM *b, size_t b_bits,
41} 64}
42 65
43static void 66static void
44benchmark_bn_mul_run_once(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *bn_ctx) 67benchmark_bn_mul_run_once(BIGNUM *r, BIGNUM *q, BIGNUM *a, BIGNUM *b, BN_CTX *bn_ctx)
45{ 68{
46 if (!BN_mul(r, a, b, bn_ctx)) 69 if (!BN_mul(r, a, b, bn_ctx))
47 errx(1, "BN_mul"); 70 errx(1, "BN_mul");
@@ -49,7 +72,7 @@ benchmark_bn_mul_run_once(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *bn_ctx)
49 72
50static int 73static int
51benchmark_bn_sqr_setup(BIGNUM *a, size_t a_bits, BIGNUM *b, size_t b_bits, 74benchmark_bn_sqr_setup(BIGNUM *a, size_t a_bits, BIGNUM *b, size_t b_bits,
52 BIGNUM *r) 75 BIGNUM *r, BIGNUM *q)
53{ 76{
54 if (!BN_rand(a, a_bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) 77 if (!BN_rand(a, a_bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
55 return 0; 78 return 0;
@@ -60,7 +83,7 @@ benchmark_bn_sqr_setup(BIGNUM *a, size_t a_bits, BIGNUM *b, size_t b_bits,
60} 83}
61 84
62static void 85static void
63benchmark_bn_sqr_run_once(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *bn_ctx) 86benchmark_bn_sqr_run_once(BIGNUM *r, BIGNUM *q, BIGNUM *a, BIGNUM *b, BN_CTX *bn_ctx)
64{ 87{
65 if (!BN_sqr(r, a, bn_ctx)) 88 if (!BN_sqr(r, a, bn_ctx))
66 errx(1, "BN_sqr"); 89 errx(1, "BN_sqr");
@@ -68,14 +91,126 @@ benchmark_bn_sqr_run_once(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *bn_ctx)
68 91
69struct benchmark { 92struct benchmark {
70 const char *desc; 93 const char *desc;
71 int (*setup)(BIGNUM *, size_t, BIGNUM *, size_t, BIGNUM *); 94 int (*setup)(BIGNUM *, size_t, BIGNUM *, size_t, BIGNUM *, BIGNUM *);
72 void (*run_once)(BIGNUM *, BIGNUM *, BIGNUM *, BN_CTX *); 95 void (*run_once)(BIGNUM *, BIGNUM *, BIGNUM *, BIGNUM *, BN_CTX *);
73 size_t a_bits; 96 size_t a_bits;
74 size_t b_bits; 97 size_t b_bits;
75}; 98};
76 99
77struct benchmark benchmarks[] = { 100struct benchmark benchmarks[] = {
78 { 101 {
102 .desc = "BN_div (64 bit / 64 bit)",
103 .setup = benchmark_bn_div_setup,
104 .run_once = benchmark_bn_div_run_once,
105 .a_bits = 64,
106 .b_bits = 64,
107 },
108 {
109 .desc = "BN_div (128 bit / 128 bit)",
110 .setup = benchmark_bn_div_setup,
111 .run_once = benchmark_bn_div_run_once,
112 .a_bits = 128,
113 .b_bits = 128,
114 },
115 {
116 .desc = "BN_div (196 bit / 196 bit)",
117 .setup = benchmark_bn_div_setup,
118 .run_once = benchmark_bn_div_run_once,
119 .a_bits = 196,
120 .b_bits = 196,
121 },
122 {
123 .desc = "BN_div (256 bit / 256 bit)",
124 .setup = benchmark_bn_div_setup,
125 .run_once = benchmark_bn_div_run_once,
126 .a_bits = 256,
127 .b_bits = 256,
128 },
129 {
130 .desc = "BN_div (320 bit / 320 bit)",
131 .setup = benchmark_bn_div_setup,
132 .run_once = benchmark_bn_div_run_once,
133 .a_bits = 320,
134 .b_bits = 320,
135 },
136 {
137 .desc = "BN_div (384 bit / 384 bit)",
138 .setup = benchmark_bn_div_setup,
139 .run_once = benchmark_bn_div_run_once,
140 .a_bits = 384,
141 .b_bits = 384,
142 },
143 {
144 .desc = "BN_div (384 bit / 128 bit)",
145 .setup = benchmark_bn_div_setup,
146 .run_once = benchmark_bn_div_run_once,
147 .a_bits = 384,
148 .b_bits = 128,
149 },
150 {
151 .desc = "BN_div (448 bit / 256 bit)",
152 .setup = benchmark_bn_div_setup,
153 .run_once = benchmark_bn_div_run_once,
154 .a_bits = 448,
155 .b_bits = 256,
156 },
157 {
158 .desc = "BN_div (512 bit / 512 bit)",
159 .setup = benchmark_bn_div_setup,
160 .run_once = benchmark_bn_div_run_once,
161 .a_bits = 512,
162 .b_bits = 512,
163 },
164 {
165 .desc = "BN_div (768 bit / 256 bit)",
166 .setup = benchmark_bn_div_setup,
167 .run_once = benchmark_bn_div_run_once,
168 .a_bits = 768,
169 .b_bits = 256,
170 },
171 {
172 .desc = "BN_div (1024 bit / 128 bit)",
173 .setup = benchmark_bn_div_setup,
174 .run_once = benchmark_bn_div_run_once,
175 .a_bits = 1024,
176 .b_bits = 128,
177 },
178 {
179 .desc = "BN_div (2048 bit / 512 bit)",
180 .setup = benchmark_bn_div_setup,
181 .run_once = benchmark_bn_div_run_once,
182 .a_bits = 2048,
183 .b_bits = 128,
184 },
185 {
186 .desc = "BN_div (3072 bit / 1024 bit)",
187 .setup = benchmark_bn_div_setup,
188 .run_once = benchmark_bn_div_run_once,
189 .a_bits = 2048,
190 .b_bits = 1024,
191 },
192 {
193 .desc = "BN_div (4288 bit / 2176 bit)",
194 .setup = benchmark_bn_div_setup,
195 .run_once = benchmark_bn_div_run_once,
196 .a_bits = 2048,
197 .b_bits = 1024,
198 },
199 {
200 .desc = "BN_div (6144 bit / 2048 bit)",
201 .setup = benchmark_bn_div_setup,
202 .run_once = benchmark_bn_div_run_once,
203 .a_bits = 2048,
204 .b_bits = 1024,
205 },
206 {
207 .desc = "BN_div (16384 bit / 8192 bit)",
208 .setup = benchmark_bn_div_setup,
209 .run_once = benchmark_bn_div_run_once,
210 .a_bits = 16384,
211 .b_bits = 8192,
212 },
213 {
79 .desc = "BN_mul (128 bit x 128 bit)", 214 .desc = "BN_mul (128 bit x 128 bit)",
80 .setup = benchmark_bn_mul_setup, 215 .setup = benchmark_bn_mul_setup,
81 .run_once = benchmark_bn_mul_run_once, 216 .run_once = benchmark_bn_mul_run_once,
@@ -203,7 +338,7 @@ static void
203benchmark_run(const struct benchmark *bm, int seconds) 338benchmark_run(const struct benchmark *bm, int seconds)
204{ 339{
205 struct timespec start, end, duration; 340 struct timespec start, end, duration;
206 BIGNUM *a, *b, *r; 341 BIGNUM *a, *b, *r, *q;
207 BN_CTX *bn_ctx; 342 BN_CTX *bn_ctx;
208 int i; 343 int i;
209 344
@@ -220,8 +355,10 @@ benchmark_run(const struct benchmark *bm, int seconds)
220 errx(1, "BN_CTX_get"); 355 errx(1, "BN_CTX_get");
221 if ((r = BN_CTX_get(bn_ctx)) == NULL) 356 if ((r = BN_CTX_get(bn_ctx)) == NULL)
222 errx(1, "BN_CTX_get"); 357 errx(1, "BN_CTX_get");
358 if ((q = BN_CTX_get(bn_ctx)) == NULL)
359 errx(1, "BN_CTX_get");
223 360
224 if (!bm->setup(a, bm->a_bits, b, bm->b_bits, r)) 361 if (!bm->setup(a, bm->a_bits, b, bm->b_bits, r, q))
225 errx(1, "benchmark setup failed"); 362 errx(1, "benchmark setup failed");
226 363
227 benchmark_stop = 0; 364 benchmark_stop = 0;
@@ -232,7 +369,7 @@ benchmark_run(const struct benchmark *bm, int seconds)
232 369
233 fprintf(stderr, "Benchmarking %s for %ds: ", bm->desc, seconds); 370 fprintf(stderr, "Benchmarking %s for %ds: ", bm->desc, seconds);
234 while (!benchmark_stop) { 371 while (!benchmark_stop) {
235 bm->run_once(r, a, b, bn_ctx); 372 bm->run_once(r, q, a, b, bn_ctx);
236 i++; 373 i++;
237 } 374 }
238 clock_gettime(CLOCK_MONOTONIC, &end); 375 clock_gettime(CLOCK_MONOTONIC, &end);