summaryrefslogtreecommitdiff
path: root/test/rng_benchmark.cc
diff options
context:
space:
mode:
authorAndrea Grandi <agrandi@google.com>2022-11-10 00:27:23 +0000
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-11-11 18:51:03 +0000
commit65bed7c6f418827aade0d3edecf178d251a3c377 (patch)
treeca47c392dcbdba33c246661596325db7aab43d52 /test/rng_benchmark.cc
parent829b707486770d97c524ef39cd85d5543bfb7bac (diff)
downloadchrome-ec-65bed7c6f418827aade0d3edecf178d251a3c377.tar.gz
test: Add rng_benchmark test
BRANCH=none BUG=b:246836252 TEST=./test/run_device_tests.py --board bloonchipper -t rng_benchmark TEST=./test/run_device_tests.py --board dartmonkey -t rng_benchmark Signed-off-by: Andrea Grandi <agrandi@google.com> Change-Id: Ib1a2a4b0fc8e3d55022e94727417c38e5e94359b Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4019773 Reviewed-by: Bobby Casey <bobbycasey@google.com> Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com> Reviewed-by: Tom Hughes <tomhughes@chromium.org>
Diffstat (limited to 'test/rng_benchmark.cc')
-rw-r--r--test/rng_benchmark.cc81
1 files changed, 81 insertions, 0 deletions
diff --git a/test/rng_benchmark.cc b/test/rng_benchmark.cc
new file mode 100644
index 0000000000..2e47b8ea27
--- /dev/null
+++ b/test/rng_benchmark.cc
@@ -0,0 +1,81 @@
+/* Copyright 2022 The ChromiumOS Authors
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Measure performance of the hardware True Random Number Generator (TRNG)
+ * compared to the std::rand().
+ */
+
+#include "benchmark.h"
+
+#include "console.h"
+#include <array>
+#include <cstdint>
+#include <cstdlib>
+
+extern "C" {
+#include "test_util.h"
+#include "trng.h"
+}
+
+test_static int test_rng()
+{
+ constexpr int num_iterations = 100;
+ Benchmark benchmark({ .num_iterations = num_iterations });
+ std::array<uint32_t, num_iterations> trng_out;
+ std::array<int, num_iterations> rand_out;
+
+ // Try the hardware true random number generator
+ trng_out.fill(0);
+
+ trng_init();
+ auto result = benchmark.run("trng", [&trng_out]() {
+ static int i = 0;
+ trng_out[i++] = trng_rand();
+ });
+ trng_exit();
+
+ TEST_ASSERT(result.has_value());
+ for (int i = 0; i < trng_out.size() - 1; ++i) {
+ TEST_NE(trng_out[i], trng_out[i + 1], "%d");
+ cflush();
+ }
+
+ // Repeat the test by turning the TNRG on and off at each iteration
+ trng_out.fill(0);
+ result = benchmark.run("trng_on_off", [&trng_out]() {
+ trng_init();
+ static int i = 0;
+ trng_out[i++] = trng_rand();
+ trng_exit();
+ });
+
+ TEST_ASSERT(result.has_value());
+ for (int i = 0; i < trng_out.size() - 1; ++i) {
+ TEST_NE(trng_out[i], trng_out[i + 1], "%d");
+ cflush();
+ }
+
+ // Repeat the test using std::rand() for comparison
+ rand_out.fill(0);
+ result = benchmark.run("std::rand", [&rand_out]() {
+ static int i = 0;
+ rand_out[i++] = std::rand();
+ });
+
+ TEST_ASSERT(result.has_value());
+ for (int i = 0; i < rand_out.size() - 1; ++i) {
+ TEST_NE(rand_out[i], rand_out[i + 1], "%d");
+ cflush();
+ }
+
+ benchmark.print_results();
+ return EC_SUCCESS;
+}
+
+void run_test(int argc, const char **argv)
+{
+ test_reset();
+ RUN_TEST(test_rng);
+ test_print_result();
+} \ No newline at end of file