cheatah
Source

tests/benchmarks/p256_bench.cpp

1// Copyright (c) 2026 BigBrain LLC. MIT-licensed (see LICENSE).
2// Original work; see ACKNOWLEDGMENTS.md for the open-source ideas we build upon.
3// p256_bench — ECDSA P-256 sign/verify throughput. Verify is the TLS hot path
4// (run once per handshake); sign is the JWT/ES256 path. Run with the release
5// preset: ./build/release/bin/cheatah_benchmarks --benchmark_filter=P256
7#include <benchmark/benchmark.h>
9#include <string>
11#include "hashlib.hpp"
12#include "p256.hpp"
14namespace {
16std::string unhex(const std::string& h) {
17 auto nib = [](char c) -> int {
18 if (c >= '0' && c <= '9') return c - '0';
19 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
20 return c - 'A' + 10;
21 };
22 std::string out;
23 for (std::size_t i = 0; i + 1 < h.size(); i += 2)
24 out.push_back(static_cast<char>((nib(h[i]) << 4) | nib(h[i + 1])));
25 return out;
28const std::string kPriv = unhex("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721");
29const std::string kPub =
30 unhex("60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6"
31 "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299");
33void BM_P256_Verify(benchmark::State& state) {
34 const std::string hash = cheatah::hashlib::sha256_digest("sample");
35 const std::string sig = cheatah::p256::sign_raw(kPriv, hash);
36 for (auto _ : state) {
37 benchmark::DoNotOptimize(cheatah::p256::verify_raw(kPub, hash, sig));
38 }
39 state.SetItemsProcessed(state.iterations());
41BENCHMARK(BM_P256_Verify);
43void BM_P256_Sign(benchmark::State& state) {
44 const std::string hash = cheatah::hashlib::sha256_digest("sample");
45 for (auto _ : state) {
46 benchmark::DoNotOptimize(cheatah::p256::sign_raw(kPriv, hash));
47 }
48 state.SetItemsProcessed(state.iterations());
50BENCHMARK(BM_P256_Sign);
52} // namespace