Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib/crypto: tests: Add Curve25519 benchmark

Add a benchmark to curve25519_kunit. This brings it in line with the
other crypto KUnit tests and provides an easy way to measure
performance.

Link: https://lore.kernel.org/r/20250906213523.84915-9-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>

+32 -1
+32 -1
lib/crypto/tests/curve25519_kunit.c
··· 5 5 6 6 #include <crypto/curve25519.h> 7 7 #include <kunit/test.h> 8 + #include <linux/timekeeping.h> 8 9 9 10 struct curve25519_test_vector { 10 11 u8 private[CURVE25519_KEY_SIZE]; ··· 1317 1316 } 1318 1317 } 1319 1318 1319 + static void benchmark_curve25519(struct kunit *test) 1320 + { 1321 + const u8 *private = curve25519_test_vectors[0].private; 1322 + const u8 *public = curve25519_test_vectors[0].public; 1323 + const size_t warmup_niter = 5000; 1324 + const size_t benchmark_niter = 1024; 1325 + u8 out[CURVE25519_KEY_SIZE]; 1326 + bool ok = true; 1327 + u64 t; 1328 + 1329 + if (!IS_ENABLED(CONFIG_CRYPTO_LIB_BENCHMARK)) 1330 + kunit_skip(test, "not enabled"); 1331 + 1332 + /* Warm-up */ 1333 + for (size_t i = 0; i < warmup_niter; i++) 1334 + ok &= curve25519(out, private, public); 1335 + 1336 + /* Benchmark */ 1337 + preempt_disable(); 1338 + t = ktime_get_ns(); 1339 + for (size_t i = 0; i < benchmark_niter; i++) 1340 + ok &= curve25519(out, private, public); 1341 + t = ktime_get_ns() - t; 1342 + preempt_enable(); 1343 + KUNIT_EXPECT_TRUE(test, ok); 1344 + kunit_info(test, "%llu ops/s", 1345 + div64_u64((u64)benchmark_niter * NSEC_PER_SEC, t ?: 1)); 1346 + } 1347 + 1320 1348 static struct kunit_case curve25519_test_cases[] = { 1321 1349 KUNIT_CASE(test_curve25519), 1322 1350 KUNIT_CASE(test_curve25519_basepoint), 1351 + KUNIT_CASE(benchmark_curve25519), 1323 1352 {}, 1324 1353 }; 1325 1354 ··· 1359 1328 }; 1360 1329 kunit_test_suite(curve25519_test_suite); 1361 1330 1362 - MODULE_DESCRIPTION("KUnit tests for Curve25519"); 1331 + MODULE_DESCRIPTION("KUnit tests and benchmark for Curve25519"); 1363 1332 MODULE_LICENSE("GPL");