diff --git a/AGENTS.md b/AGENTS.md index c355acfe..aa184ecd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -32,14 +32,20 @@ specific to crypto. same commit (`fixes` for each message, `fixtures` for each fixture, `lane` for the constant-time lane's paths); the generator names what is missing. - `generate.py --sizes ...` reads the own lane `sizes` (built at `variant=release`) into - `doc/tables/sizes.json`; `doc/performance.json` is `bench/collect.py`'s, release builds only. - Nothing that states a size or a speed is typed by hand. + `doc/tables/sizes.json`; `doc/performance.json` is `bench/collect.py`'s, release builds only, + the throughput (`--exe`) and the timing guard's cost (`--timing`, natively). Nothing that states + a size or a speed is typed by hand. ## Cautions - An algorithm type does every guard; a backend's header supplies only the six traits of `detail/engines.hpp` (`block_state`, `block_engine`, `gcm_state`, `gcm_engine`, `aead_state`, `aead_engine`) and, for the curves and `hybrid`, the functions of its own header. +- A new public function that calls a backend holds `detail::timing_guard` as its + first statement (it sets arm64's DIT for the call, `detail/timing.hpp`) and joins the coverage + test of its backend, `test/timing_coverage__test.cpp`, which checks through the seam + that each call entered its own guard. A function that only copies or encodes bytes, with no + backend call, holds none. - A test of a backend is a `.cpp` of its own per backend, never a define that changes the backend of one source (spec section 4.3's pitfall). - Every C library is unchanged upstream code. A moved pin is a change of its own diff --git a/bench/Jamfile b/bench/Jamfile index 4364b615..89452daa 100644 --- a/bench/Jamfile +++ b/bench/Jamfile @@ -5,11 +5,14 @@ # https://www.boost.org/LICENSE_1_0.txt) # The throughput of each family's fixed operation per backend (throughput.cpp, -# bench/operations.hpp). throughput_check is the correctness test of the -# measured program, run in every lane of test/ (test/Jamfile builds this -# project): each operation checked once, never timed. The measurement itself -# is made by hand by collect.py, which runs the program built here at -# variant=release and writes doc/performance.adoc; no lane measures it. +# bench/operations.hpp), and the cost of the timing guard every operation runs +# in (timing.cpp). throughput_check and timing_check are the correctness tests +# of the measured programs, run in every lane of test/ (test/Jamfile builds +# this project): each operation checked once, never timed, and the guard's +# measurement run at 10^3 iterations, its line's form and the thread's DIT +# checked. The measurements themselves are made by hand by collect.py, which +# runs the programs built here at variant=release and writes +# doc/performance.adoc; no lane measures them. project : requirements /webcpp/crypto//crypto ; @@ -36,3 +39,11 @@ webcpp.run throughput_check : throughput.cpp : $(.requirements) ; # run by hand (doc/performance.adoc); no lane measures it. exe throughput : throughput.cpp : $(.requirements) release ; explicit throughput ; + +# The guard's cost needs no backend: detail/timing.hpp alone. +webcpp.run timing_check : timing.cpp ; + +# The same program at variant=release, the one collect.py --timing measures, +# built and run by hand (doc/performance.adoc); no lane measures it. +exe timing : timing.cpp : release ; +explicit timing ; diff --git a/bench/collect.py b/bench/collect.py index 4a9eb484..bbee5de7 100755 --- a/bench/collect.py +++ b/bench/collect.py @@ -5,19 +5,22 @@ # accompanying file LICENSE_1_0.txt or copy at # https://www.boost.org/LICENSE_1_0.txt) -"""Measures bench/throughput.cpp on each target and renders its data, after webcpp::hash's. +"""Measures bench/throughput.cpp on each target and bench/timing.cpp natively, and renders their +data, after webcpp::hash's. collect.py --exe native= --exe wasip2= ... --invocations 5 \\ + --timing \\ --data libs/crypto/doc/performance.json --page libs/crypto/doc/performance.adoc Run from the superproject's root (the launchers' paths, tools/boost/asio-wasip2/run.sh and its -wasip3 twin, are relative to it), with the program bench/Jamfile builds at variant=release, -`b2 libs/crypto/bench//throughput` on each target; --data and --page are relative to the same -root. +wasip3 twin, are relative to it), with the programs bench/Jamfile builds at variant=release, +`b2 libs/crypto/bench//throughput` on each target and `b2 libs/crypto/bench//timing` natively; +--data and --page are relative to the same root. `--timing` alone, without `--exe`, measures the +guard again and keeps the data's throughput. The page is built only from release builds: a program whose build directory, b2's bench///..., is not variant=release, or does not say, is refused before anything -runs, and `--render-only` refuses data whose build is not BUILD. +runs, and `--render-only` refuses data whose build, or whose guard's build, is not BUILD. Each cell (a family on a backend, on a target) is run INVOCATIONS times, round-robin over the cells so that a change of load on a shared machine falls on all of them alike; each invocation @@ -28,6 +31,27 @@ never fails this script: only a program that does not run, or a cell it does not know, does. `--render-only` rewrites the page from the data. +`--provisional` marks figures as provisional, measured on a shared, loaded machine and to be +measured again in a final pass on a quiet one. Each part has its own mark: the throughput's is the +data's `"provisional": true`, the guard's is `"provisional": true` in its "timing_guard", and each +table's NOTE renders from its own. A measurement marks, with `--provisional`, or clears, without +it, the mark of the part it measured alone (`--exe` the throughput's, `--timing` the guard's), and +keeps the other part's as it was. With `--render-only` it marks both parts the data holds, numbers +unchanged. + +Data that is not what this script writes (not a JSON object, a key missing or of another type, a +family, backend or target the page does not know), and a program's line that is not the form this +script reads (an older bench/timing.cpp's, without the fastest and slowest runs, among them), are +refused with a message naming what is wrong, before the page is written. + +The timing guard's cost (detail::timing_guard, which every public operation holds and which sets +64-bit ARM's DIT where the CPU has it) is `timing --measure`'s one line: whether the guard sets DIT +on this machine, and the nanoseconds a guard adds to a call entered from a thread whose DIT is +clear and from one where it is already set, each the median of five runs of 10^7 guards minus an +empty loop's, with the fastest and slowest run. The data keeps it under "timing_guard" with the +date, the machine and the load; the page renders it as a paragraph and a two-row table under the +throughput. Every date is UTC's, as the constant-time lane's are. + libsodium's x86-64 assembly (HAVE_AMD64_ASM, design D7) is recorded beside the table as not measured yet, with its reason, until an x86-64 machine runs a scratch build of it by hand. """ @@ -77,7 +101,8 @@ def variant_of(exe): def check_release(exes): - """Exits naming each program not built at variant=release, before anything runs.""" + """Exits naming each program not built at variant=release, before anything runs; exes maps + a target, or "timing" for the guard's program, to its path.""" others = [] for target, exe in exes.items(): variant = variant_of(exe) @@ -95,6 +120,11 @@ def crypto_json(): return json.load(f) +def utc_today(): + """Today's date in UTC, the date every measurement of crypto's states.""" + return datetime.datetime.now(datetime.timezone.utc).date().isoformat() + + def median(values): values = sorted(values) n = len(values) @@ -129,6 +159,97 @@ def machine(): } +# The shapes of the data and of the programs' lines: a key's type, the shape of a nested object, a +# one-element list for a list of that shape, or a function that says whether a value is right. A +# key of OPTIONAL may be missing. +NUMBER = (int, float) + + +def load_pair(value): + """Whether value is the load averages before and after: two lists of numbers, neither empty.""" + return (isinstance(value, list) and len(value) == 2 and + all(isinstance(loads, list) and loads and + all(isinstance(x, NUMBER) and not isinstance(x, bool) for x in loads) + for loads in value)) + + +MACHINE_SHAPE = {"cpu": str, "architecture": str, "cores": (int, type(None)), + "memory_gib": int, "os": str, "wasmtime": str, "node": str} +CASE_SHAPE = {"family": str, "backend": str, "target": str, "operation": str, "compiler": str, + "median_us": NUMBER, "min_us": NUMBER, "max_us": NUMBER} +DATA_SHAPE = {"date": str, "machine": MACHINE_SHAPE, "load_average_before_after": load_pair, + "build": str, "cases": [CASE_SHAPE], + "amd64_asm": {"rows": [str], "status": str, "reason": str}} +TIMING_SHAPE = {"date": str, "machine": MACHINE_SHAPE, "load_average_before_after": load_pair, + "build": str, "target": str, "compiler": str, "iterations": int, "runs": int, + "dit": bool, "clear_ns": NUMBER, "set_ns": NUMBER} +# Recorded since the guard's spread was: data measured before it lacks them, and the page says +# "not recorded". +TIMING_OPTIONAL = {"clear_fastest_ns": NUMBER, "clear_slowest_ns": NUMBER, + "set_fastest_ns": NUMBER, "set_slowest_ns": NUMBER, "provisional": bool} +# bench/timing.cpp's line, every key since the guard's spread is recorded. +TIMING_LINE_SHAPE = {"target": str, "compiler": str, "iterations": int, "runs": int, "dit": bool, + "clear": NUMBER, "clear_fastest": NUMBER, "clear_slowest": NUMBER, + "set": NUMBER, "set_fastest": NUMBER, "set_slowest": NUMBER} +# bench/throughput.cpp's line. +THROUGHPUT_LINE_SHAPE = {"operation": str, "compiler": str, "repetitions": int, + "median_us": NUMBER, "runs_us": [NUMBER]} + + +def wrong(value, shape, where, optional=None): + """The first place in value that shape requires and value lacks or holds otherwise, named as + a path from where; None when value has the shape.""" + if callable(shape) and not isinstance(shape, type): + return None if shape(value) else where + if isinstance(shape, dict): + if not isinstance(value, dict): + return f"{where} (not an object)" + for key, inner in shape.items(): + if key not in value: + return f"{where}.{key} (missing)" + found = wrong(value[key], inner, f"{where}.{key}") + if found is not None: + return found + for key, inner in (optional or {}).items(): + if key in value and value[key] is not None: + found = wrong(value[key], inner, f"{where}.{key}") + if found is not None: + return found + return None + if isinstance(shape, list): + if not isinstance(value, list) or not value: + return f"{where} (not a list, or empty)" + for index, item in enumerate(value): + found = wrong(item, shape[0], f"{where}[{index}]") + if found is not None: + return found + return None + types = shape if isinstance(shape, tuple) else (shape,) + if isinstance(value, bool) and bool not in types: + return where + return None if isinstance(value, types) else where + + +def program_line(target, command, output, shape): + """The last line of a program's output, read as JSON of shape; exits naming the program and + what is wrong when it is not.""" + lines = output.strip().splitlines() + try: + line = json.loads(lines[-1]) if lines else None + except ValueError as error: + sys.exit(f"collect.py: {target} {' '.join(command)} printed a line that is not JSON " + f"({error}): {lines[-1]!r}") + found = wrong(line, shape, "line") + # Each shape is an object's, so wrong finds nothing only in a dict; the isinstance states + # that to the type checker, and the line comes back a dict, never None. + if found is None and isinstance(line, dict): + return line + printed = lines[-1] if lines else "" + sys.exit(f"collect.py: {target} {' '.join(command)} printed {printed!r}, which is not " + f"the form collect.py reads: {found or 'line (not an object)'} is wrong; build " + "bench/Jamfile's program again from this tree") + + def run(target, command): done = subprocess.run(LAUNCHERS[target] + command, capture_output=True, text=True, check=False) @@ -159,8 +280,9 @@ def measure(exes, invocations): loads = [os.getloadavg()] for _ in range(invocations): for (target, family, backend), case in cases.items(): - line = run(target, [case["exe"], family, backend, "--runs", "5"]).strip() - case["results"].append(json.loads(line.splitlines()[-1])) + command = [case["exe"], family, backend, "--runs", "5"] + case["results"].append(program_line(target, command, run(target, command), + THROUGHPUT_LINE_SHAPE)) loads.append(os.getloadavg()) out = [] for (target, family, backend), case in cases.items(): @@ -188,7 +310,7 @@ def measure(exes, invocations): return { "measurement": "each family's fixed operation per backend and target, " "bench/operations.hpp", - "date": datetime.date.today().isoformat(), + "date": utc_today(), "machine": machine(), "load_average_before_after": [list(loads[0]), list(loads[1])], "build": BUILD, @@ -197,6 +319,41 @@ def measure(exes, invocations): } +def measure_timing(exe): + """The guard's cost, timing --measure's line, natively, with when, where and under what load + it was measured.""" + loads = [os.getloadavg()] + output = run("native", [exe, "--measure"]) + loads.append(os.getloadavg()) + result = program_line("native", [exe, "--measure"], output, TIMING_LINE_SHAPE) + return { + "measurement": "the nanoseconds detail::timing_guard adds to a call, bench/timing.cpp", + "date": utc_today(), + "machine": machine(), + "load_average_before_after": [list(loads[0]), list(loads[1])], + "build": BUILD, + "target": result["target"], + "compiler": result["compiler"], + "iterations": result["iterations"], + "runs": result["runs"], + "dit": result["dit"], + "clear_ns": result["clear"], + "clear_fastest_ns": result["clear_fastest"], + "clear_slowest_ns": result["clear_slowest"], + "set_ns": result["set"], + "set_fastest_ns": result["set_fastest"], + "set_slowest_ns": result["set_slowest"], + } + + +# What the page says above a table whose figures are provisional (--provisional). +PROVISIONAL = ( + "NOTE: these figures are provisional. They were measured on a shared machine under load " + "(the load averages above), and are measured again in a final measurement pass on a quiet " + "machine, once the work in progress has merged; read them as an upper bound." +) + + # When the page warns that the machine was loaded: a shared machine shows in the spread before a # single core is short, so the threshold is half the cores. LOADED_RULE = "the larger of the two 1-minute load averages exceeds half the core count" @@ -212,6 +369,12 @@ def fmt_load(load): return "/".join(f"{x:.0f}" for x in load) +def fmt_ns(value): + """Nanoseconds as the page shows them: one decimal; "not recorded" for a figure the data, + measured before it was recorded, does not hold.""" + return "not recorded" if value is None else f"{value:.1f}" + + def fmt_us(value): """Microseconds as the page shows them: two decimals, or three significant digits below 1.""" return f"{value:,.2f}" if value >= 1 else f"{value:.3g}" @@ -224,6 +387,80 @@ def per_second(case): return f"{1e6 / case['median_us']:,.0f} operations/s" +def render_timing(data, timing, titles, backends): + """The guard's paragraph and table, from the data's "timing_guard", or the line saying it is + not measured yet.""" + lines = [ + "[#crypto-performance-timing]", + "==== The timing guard's cost", + "", + "Every public operation runs inside `detail::timing_guard`, which on 64-bit ARM under", + "macOS, iOS, Linux and Android, where the CPU has FEAT_DIT, sets DIT (data-independent", + "timing) for the call and restores the thread's state after it", + "(<>): one `mrs`, and where DIT was clear, two `msr`", + "each followed by an `isb`. Elsewhere (x86, WebAssembly, Windows on ARM64) it executes", + "no instruction.", + "", + ] + if timing is None: + return lines + ["Not measured yet.", ""] + machine_info = timing["machine"] + sets = ("The guard sets DIT on this machine." if timing["dit"] else + "The guard does not set DIT on this machine (no FEAT_DIT, or a target it does not " + "cover): both rows are the loop's noise.") + load = timing["load_average_before_after"] + lines += [ + f"Measured natively on {timing['date']} on {machine_info['cpu']}", + f"({machine_info['architecture']}), {machine_info['os']}, built with {timing['build']}", + f"by {timing['compiler']} (bench/timing.cpp): each row is the median of", + f"{timing['runs']} runs of {timing['iterations']:,} guards around a store, with the", + "fastest and slowest run beside it, each minus the median of the same loop without the", + f"guard, in nanoseconds per call. {sets}", + f"The machine's 1, 5 and 15 minute load averages were {fmt_load(load[0])} before and", + f"{fmt_load(load[1])} after, on {machine_info['cores']} cores.", + "", + ] + if timing.get("provisional"): + lines += [PROVISIONAL, ""] + if loaded(timing): + lines += [ + "WARNING: the machine was shared and loaded while this was measured (rule: " + + LOADED_RULE + ").", + "", + ] + lines += [ + '[cols="2,4,1,1,1",options="header"]', + "|===", + "| The thread's DIT on entry | What the guard does | Median (ns) | Fastest (ns) " + "| Slowest (ns)", + "", + "| clear, as a program that sets nothing has it", + "| reads DIT, sets it, and clears it on the way out", + f"| {fmt_ns(timing['clear_ns'])}", + f"| {fmt_ns(timing.get('clear_fastest_ns'))}", + f"| {fmt_ns(timing.get('clear_slowest_ns'))}", + "", + "| already set, by the program or by an outer guard", + "| reads DIT and leaves it set", + f"| {fmt_ns(timing['set_ns'])}", + f"| {fmt_ns(timing.get('set_fastest_ns'))}", + f"| {fmt_ns(timing.get('set_slowest_ns'))}", + "|===", + "", + ] + native = [c for c in data["cases"] if c["target"] == "native"] + if native: + shortest = min(native, key=lambda c: c["median_us"]) + lines += [ + "For scale, the shortest operation of the table above natively,", + f"{titles[shortest['family']]} ({shortest['operation']}) on", + f"{backends[shortest['backend']]}, takes {fmt_ns(shortest['median_us'] * 1000)} ns,", + "its guard included.", + "", + ] + return lines + + def render(data, page): table = crypto_json() titles = {family["name"]: family["title"] for family in table["families"]} @@ -264,6 +501,8 @@ def render(data, page): f"{fmt_load(load[1])} after, on {machine_info['cores']} cores.", "", ] + if data.get("provisional"): + lines += [PROVISIONAL, ""] if loaded(data): lines += [ "WARNING: the machine was shared and loaded while this was measured (rule: " @@ -304,35 +543,111 @@ def render(data, page): + data["amd64_asm"]["reason"], "", ] + lines += render_timing(data, data.get("timing_guard"), titles, backends) with open(page, "w", encoding="utf-8") as f: f.write("\n".join(lines)) +def check_shape(path, data): + """Exits naming the first thing in data that is not what this script writes: a key missing or + of another type, or a family, backend or target the page does not know.""" + found = wrong(data, DATA_SHAPE, "data") + if found is None and data.get("timing_guard") is not None: + found = wrong(data["timing_guard"], TIMING_SHAPE, "data.timing_guard", TIMING_OPTIONAL) + if found is None and not isinstance(data.get("provisional", False), bool): + found = "data.provisional" + if found is None: + table = crypto_json() + known = {"family": {family["name"] for family in table["families"]}, + "backend": set(table["backends"]), "target": set(TARGETS)} + found = next((f"data.cases[{index}].{key} ({case[key]!r} is not one of crypto.json's)" + for index, case in enumerate(data["cases"]) + for key, names in known.items() if case[key] not in names), None) + if found is not None: + sys.exit(f"collect.py: {path} is not the data collect.py writes: {found} is wrong; " + "measure it again with --exe") + + +def read_release_data(path): + """The data at path; exits when it cannot be read, is not the data this script writes, or + when it, or its guard's measurement, was not built at BUILD.""" + try: + with open(path, encoding="utf-8") as f: + data = json.load(f) + except (OSError, ValueError) as error: + sys.exit(f"collect.py: {path} cannot be read ({error}); --timing alone and --render-only " + "read the data that --exe wrote") + if not isinstance(data, dict): + sys.exit(f"collect.py: {path} is not the data collect.py writes: not a JSON object; " + "measure it again with --exe") + if data.get("build") != BUILD: + sys.exit(f"collect.py: {path} was measured with build {data.get('build')}, not " + f"{BUILD}: {RELEASE_ONLY}") + timing = data.get("timing_guard") + if isinstance(timing, dict) and timing.get("build") != BUILD: + sys.exit(f"collect.py: {path}'s timing guard was measured with build " + f"{timing.get('build')}, not {BUILD}: {RELEASE_ONLY}") + check_shape(path, data) + return data + + +def write_data(path, data): + """Writes data to path as the committed file has it.""" + with open(path, "w", encoding="utf-8") as f: + json.dump(data, f, indent=2) + f.write("\n") + + def main(): parser = argparse.ArgumentParser(description="Measures bench/throughput.cpp on each target.") parser.add_argument("--exe", action="append", default=[], metavar="TARGET=PATH") parser.add_argument("--invocations", type=int, default=5) parser.add_argument("--data", required=True) parser.add_argument("--page", required=True) + parser.add_argument("--timing", metavar="PATH", + help="bench/Jamfile's timing at variant=release, run natively") parser.add_argument("--render-only", action="store_true") + parser.add_argument("--provisional", action="store_true", + help="mark the figures of the part measured (--exe: the throughput, " + "--timing: the guard) as measured under load, to be measured again; " + "without it, a measurement clears its part's mark and keeps the " + "other's; with --render-only, mark both parts the data holds, " + "numbers unchanged") args = parser.parse_args() if args.render_only: - with open(args.data, encoding="utf-8") as f: - data = json.load(f) - if data.get("build") != BUILD: - sys.exit(f"collect.py: {args.data} was measured with build {data.get('build')}, not " - f"{BUILD}: {RELEASE_ONLY}") + data = read_release_data(args.data) + if args.provisional: + parts = [data] + ([data["timing_guard"]] if data.get("timing_guard") else []) + unmarked = [part for part in parts if not part.get("provisional")] + for part in unmarked: + part["provisional"] = True + if unmarked: + write_data(args.data, data) else: exes = dict(item.split("=", 1) for item in args.exe) unknown = sorted(set(exes) - set(TARGETS)) if unknown: sys.exit(f"collect.py: no target {', '.join(unknown)}; the targets are " f"{', '.join(TARGETS)}") - check_release(exes) - data = measure(exes, args.invocations) - with open(args.data, "w", encoding="utf-8") as f: - json.dump(data, f, indent=2) - f.write("\n") + if not exes and args.timing is None: + sys.exit("collect.py: nothing to measure: give --exe, --timing or --render-only") + check_release({**exes, **({"timing": args.timing} if args.timing else {})}) + if exes: + data = measure(exes, args.invocations) + if args.timing is None and Path(args.data).exists(): + # The guard's cost is kept until it is measured again, with its own mark. + kept = read_release_data(args.data).get("timing_guard") + if kept is not None: + data["timing_guard"] = kept + if args.provisional: + data["provisional"] = True + else: + data = read_release_data(args.data) + if args.timing is not None: + data["timing_guard"] = measure_timing(args.timing) + if args.provisional: + data["timing_guard"]["provisional"] = True + write_data(args.data, data) render(data, args.page) diff --git a/bench/timing.cpp b/bench/timing.cpp new file mode 100644 index 00000000..a5ae1368 --- /dev/null +++ b/bench/timing.cpp @@ -0,0 +1,226 @@ +// Copyright (c) 2026 WebCpp.org +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +// The cost of detail::timing_guard, the scope every public operation of crypto runs in, which sets +// 64-bit ARM's DIT (data-independent timing) bit where the CPU has it and restores the thread's +// state after it. +// +// Run as `timing --measure`, it times, in one process, 10^7 iterations each of an empty loop body, +// of a guard entered from a thread whose DIT is clear (it sets DIT and clears it again), and of a +// guard entered with DIT already set (it reads DIT and leaves it; the program sets DIT for that +// part with the same `msr` as the guard, and clears it after), five runs of each, interleaved, and +// prints one JSON line: `{"dit": true|false, "clear": , "set": , ...}`, each cost the +// median of its runs minus the empty loop's median, in nanoseconds per guard, then each part's +// fastest and slowest run (minus the same median), the iterations, the runs, the target and the +// compiler. "dit" says whether the guard sets DIT here (a target it covers, on a CPU that has it); +// where it does not, the guard executes no instruction and both costs are the loop's noise. Both +// modes fail when the process starts with DIT set, or when the thread's DIT is not as the guard +// must leave it after a part. Run with no argument, it is the correctness test: the same +// measurement at 10^3 iterations, whose line must also have that form, and whose "dit" must be +// what the library reads. A measurement never fails on its time; bench/collect.py --timing runs +// it and writes doc/performance.adoc. + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace detail = webcpp::crypto::detail; + +namespace { + +#ifdef WEBCPP_TARGET_WASIP2 +constexpr std::string_view target = "wasip2"; +#elif defined(WEBCPP_TARGET_WASIP3) +constexpr std::string_view target = "wasip3"; +#elif defined(WEBCPP_TARGET_EMSCRIPTEN) +constexpr std::string_view target = "emscripten"; +#else +constexpr std::string_view target = "native"; +#endif + +// The compiler, as it names itself: MSVC defines no __VERSION__. +#ifdef __VERSION__ +constexpr std::string_view compiler = __VERSION__; +#elif defined(_MSC_FULL_VER) +#define WEBCPP_CRYPTO_BENCH_STRING(x) #x +#define WEBCPP_CRYPTO_BENCH_VERSION(x) WEBCPP_CRYPTO_BENCH_STRING(x) +constexpr std::string_view compiler = "MSVC " WEBCPP_CRYPTO_BENCH_VERSION(_MSC_FULL_VER); +#else +constexpr std::string_view compiler = "unknown"; +#endif + +// The iterations of a measured run, and of the correctness test's. +constexpr std::uint64_t measured_iterations = 10'000'000; +constexpr std::uint64_t checked_iterations = 1'000; + +// The runs of each part; their median is the part's time. +constexpr std::size_t runs = 5; + +// Whether the guard sets DIT on this target and CPU. +bool guard_sets_dit() noexcept { +#ifdef WEBCPP_CRYPTO_DETAIL_TIMING_COVERED + return detail::cpu_has_dit(); +#else + return false; +#endif +} + +// Sets or clears this thread's DIT where the guard would, as a program that asks for it does. +void force_dit([[maybe_unused]] bool set) noexcept { +#ifdef WEBCPP_CRYPTO_DETAIL_TIMING_COVERED + if (detail::cpu_has_dit()) { + detail::write_dit(set ? detail::dit_bit : 0); + } +#endif +} + +// The nanoseconds of one iteration of body, over iterations of it. +template +double nanoseconds_per_iteration(std::uint64_t iterations, Body body) { + auto const start = std::chrono::steady_clock::now(); + for (std::uint64_t i = 0; i < iterations; ++i) { + body(i); + } + std::chrono::duration const elapsed = + std::chrono::steady_clock::now() - start; + return elapsed.count() / static_cast(iterations); +} + +// A part's runs, each less the empty loop's median: their median, fastest and slowest. +struct summary { + double median = 0; + double fastest = 0; + double slowest = 0; +}; + +summary summarise(std::array values, double loop) { + std::ranges::sort(values); + return { + .median = values[runs / 2] - loop, + .fastest = values.front() - loop, + .slowest = values.back() - loop, + }; +} + +// What one measurement gives: the guard's two costs, and whether DIT was as the guard must leave +// it before and after each part. +struct result { + summary clear; + summary set; + bool restored = true; +}; + +result measure(std::uint64_t iterations) { + std::array empty{}; + std::array clear{}; + std::array set{}; + // A process that starts with DIT set would measure the set part twice. + bool restored = !detail::dit_is_set(); + // Written by every iteration of every part, so that no loop is removed or merged. + std::uint64_t volatile sink = 0; + for (std::size_t run = 0; run < runs; ++run) { + empty[run] = nanoseconds_per_iteration(iterations, [&sink](std::uint64_t i) { sink = i; }); + clear[run] = nanoseconds_per_iteration(iterations, [&sink](std::uint64_t i) { + detail::timing_guard const guard; + sink = i; + }); + restored = restored && !detail::dit_is_set(); + force_dit(true); + set[run] = nanoseconds_per_iteration(iterations, [&sink](std::uint64_t i) { + detail::timing_guard const guard; + sink = i; + }); + restored = restored && detail::dit_is_set() == guard_sets_dit(); + force_dit(false); + restored = restored && !detail::dit_is_set(); + } + double const loop = summarise(empty, 0).median; + return {.clear = summarise(clear, loop), .set = summarise(set, loop), .restored = restored}; +} + +// Writes the JSON line of a measurement into line; the number of characters, or -1. +int format(std::span line, result const& measured, std::uint64_t iterations) { + return std::snprintf( + line.data(), line.size(), + R"({"dit": %s, "clear": %.3f, "set": %.3f, "clear_fastest": %.3f, )" + R"("clear_slowest": %.3f, "set_fastest": %.3f, "set_slowest": %.3f, )" + R"("iterations": %llu, "runs": %zu, "target": "%.*s", "compiler": "%.*s"})", + guard_sets_dit() ? "true" : "false", measured.clear.median, measured.set.median, + measured.clear.fastest, measured.clear.slowest, measured.set.fastest, measured.set.slowest, + static_cast(iterations), runs, static_cast(target.size()), + target.data(), static_cast(compiler.size()), compiler.data()); +} + +// Skips prefix at the start of text, or returns null when text does not start with it. +char const* skip(char const* text, std::string_view prefix) { + return text != nullptr && std::string_view(text).starts_with(prefix) ? text + prefix.size() + : nullptr; +} + +// Reads a finite number at the start of text into value; what follows it, or null. +char const* number(char const* text, double& value) { + if (text == nullptr) { + return nullptr; + } + char* end = nullptr; + value = std::strtod(text, &end); + return end != text && std::isfinite(value) ? end : nullptr; +} + +// Whether line opens with the form collect.py reads: "dit" as the library reads it, then the two +// costs as finite numbers. +bool well_formed(char const* line) { + double clear = 0; + double set = 0; + char const* rest = + skip(line, guard_sets_dit() ? R"({"dit": true, "clear": )" : R"({"dit": false, "clear": )"); + rest = number(skip(number(rest, clear), R"(, "set": )"), set); + return skip(rest, R"(, "clear_fastest": )") != nullptr; +} + +int usage() { + static_cast(std::fprintf(stderr, "usage: timing [--measure]\n")); + return 2; +} + +} // namespace + +int main(int argument_count, char** arguments) { + std::span const given(arguments, static_cast(argument_count)); + bool const measuring = given.size() == 2 && std::string_view(given[1]) == "--measure"; + if (given.size() != 1 && !measuring) { + return usage(); + } + std::uint64_t const iterations = measuring ? measured_iterations : checked_iterations; + result const measured = measure(iterations); + std::array line{}; + int const written = format(line, measured, iterations); + if (written < 0 || static_cast(written) >= line.size()) { + static_cast(std::fprintf(stderr, "timing: the line does not fit\n")); + return 1; + } + std::printf("%s\n", line.data()); + if (!measured.restored) { + static_cast(std::fprintf( + stderr, "timing: the thread's DIT was set at the start or not restored by a guard\n")); + } + if (measuring) { + return measured.restored ? 0 : 1; + } + bool const formed = well_formed(line.data()); + std::printf("form %s\nstate restored %s\n", formed ? "ok" : "FAILED", + measured.restored ? "ok" : "FAILED"); + return formed && measured.restored ? 0 : 1; +} diff --git a/doc/backends.adoc b/doc/backends.adoc index dc5c6530..82df30ed 100644 --- a/doc/backends.adoc +++ b/doc/backends.adoc @@ -99,24 +99,18 @@ by memcheck alone. dudect's statistics BearSSL's two AEADs' seal and open, ctaes, `equal`, libsodium's X25519 agreement and Ed25519 signing, and libsecp256k1's `ecdh_x` natively, on Linux and macOS, and under wasmtime. -On 64-bit ARM under macOS and Linux, each dudect program first sets the CPU's -DIT (data-independent timing) bit, with which the instructions Arm lists take -a time independent of their data, and which Apple requires of constant-time -code -(link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, enable DIT for constant-time cryptographic operations]); -it prints whether it is set, and fails where the CPU has DIT and it is not, or -where macOS cannot say whether the CPU has it. Elsewhere (x86, WebAssembly, and -64-bit ARM systems the harness does not cover, MSVC on ARM64 among them) it -sets nothing, prints so, and measures without DIT. The lane's `dudect_planted` -shows that DIT does not hide a leak: X25519's `agree` after an early-exit scan -of its secret key, which dudect must find. -None of the C libraries sets DIT, and neither does webcpp: a program on Apple -silicon sets it around its secret operations, as Apple describes -(`timingsafe_enable_if_supported` from macOS 15.2). Without it, libsodium's -X25519 is measurably not constant time there, listed under the table. +dudect's programs set nothing themselves: they measure what a program gets, +which on 64-bit ARM is the DIT webcpp sets in each call (<>). +Each prints whether the CPU has DIT, and fails where DIT is set on its thread +outside the measured calls, which would measure something a program does not +get. The lane's `dudect_planted` shows that DIT does not hide a leak: X25519's +`agree` after an early-exit scan of its secret key, the scan run inside the +library's own guard as a public operation's code would be, which dudect must +find: |t| 66.72 and 65.53 against the threshold 10 (2026-11-21 UTC, two runs, +an Apple M5 Max, a release build). One measurement is an open finding, listed under the table with its numbers: -libsodium's X25519 `agree` under wasmtime, on wasip2. Its cause is the same: +libsodium's X25519 `agree` under wasmtime, on wasip2. Its cause is measured: wasmtime does not set DIT, and with DIT set in wasmtime's main thread, an experiment and not the lane, the case passes. The WebAssembly cannot set it, so the lane prints its value on wasip2 without judging it, and judges @@ -153,6 +147,49 @@ marks. OpenSSL's verdict is OpenSSL's own internal branch, which webcpp cannot mark: OpenSSL is checked with the key secret during `seal` only. +[#crypto-backends-dit] +==== Data-independent timing (DIT) + +64-bit ARM's DIT (data-independent timing) bit, PSTATE.DIT, makes the +instructions Arm lists take a time independent of their data, and Apple +requires it of constant-time code +(link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, enable DIT for constant-time cryptographic operations]). +None of the C libraries sets it, so webcpp does: every public function that +runs a backend's cryptographic code, and `equal`, runs its whole body with DIT +set on 64-bit ARM under macOS, iOS, Linux and Android where the CPU has +FEAT_DIT (`hw.optional.arm.FEAT_DIT` on Apple's systems, `HWCAP_DIT` on +Linux), and returns with the thread's DIT as it found it: clear when it was +clear, set when the program set it itself (Apple's +`timingsafe_enable_if_supported`, from macOS 15.2) or an outer call did. An +error and an exception leave it as found too, and DIT is a thread's own +state, so a call never changes another thread's. A program does nothing. + +Elsewhere webcpp sets nothing: + +* On x86, the counterpart is Intel's DOITM (data operand independent timing +mode), a model-specific register that only the kernel writes: it is the +system's choice, out of webcpp's reach. +* On WebAssembly, the engine that compiles the code decides, and the +WebAssembly cannot set DIT: X25519 on wasip2 is the open finding above. +* On Windows on ARM64, no documented way to ask Windows whether the CPU has +FEAT_DIT was found, and DIT's instructions trap on a CPU without it: the +guard is empty there, and untested. + +What the guard adds to a call, measured from a release build beside the +shortest operation's time, is in <>. + +`silent_payments::scan_outputs` calls the program's `LabelLookup` inside its +guard: the lookup runs with DIT set, which only makes it data-independent too. + +The lane's dudect used to set DIT itself; it now sets nothing and +measures the library's guard. libsodium's X25519 `agree`, an all-zero secret +key against random ones, on an Apple M5 Max from a release build, against the +threshold 10: with the library's guard, |t| 1.8 to 2.8 (2026-11-21, five runs); +with no DIT at all, |t| 4.3 to 7.8 (2026-11-21, three runs), below the +threshold but higher, and 6.7 to 14.5 (2026-11-19), above it; with the +harness setting DIT, as the lane did before, |t| 1.5 to 2.4 (2026-11-21, three +runs). Dates are UTC. + [#crypto-backends-sizes] === Size diff --git a/doc/changelog.adoc b/doc/changelog.adoc index 62c66c95..d7d2209a 100644 --- a/doc/changelog.adoc +++ b/doc/changelog.adoc @@ -16,7 +16,7 @@ of every version are on webcpp's index page. === Unreleased // tag::unreleased[] -* Added crypto, primitives over C backends: ciphers, curves and randomness, errors of fixed values in webcpp.crypto, every result `+[[nodiscard]]+`, and WEBCPP_CRYPTO_HAS_THREADS, defined natively and with Emscripten's -pthread unless WEBCPP_CRYPTO_NO_THREADS is. +* Added crypto, C-backed primitives setting DIT on arm64 macOS, iOS, Linux and Android: ciphers, curves, randomness, fixed-value webcpp.crypto errors, `+[[nodiscard]]+` results, WEBCPP_CRYPTO_HAS_THREADS natively and with Emscripten's -pthread unless WEBCPP_CRYPTO_NO_THREADS is. * Added equal, random_device, pem (PEM on libsodium), ciphers::aes128, aes192 and aes256, the AES block cipher, and ciphers::gcm, aes128_gcm and aes256_gcm: AES-GCM in place, incremental seal, failed opens zeroing data. * Added supports_v with its family tags, the capability table per target, default_backend_t and /webcpp/crypto//-default per family and target (secp256k1's modules among them), and refusals naming their Jamfile line. * Added backends::bearssl (AES-GCM alone, allocating nothing), backends::openssl via /webcpp/crypto//openssl natively and on WASI (allocating at make only, or natively in openssl_memory_resource with WEBCPP_CRYPTO_OPENSSL_MEMORY_RESOURCE), and backends::ctaes (block cipher alone). diff --git a/doc/generated/constant-time.adoc b/doc/generated/constant-time.adoc index 5cc30c44..c2abade5 100644 --- a/doc/generated/constant-time.adoc +++ b/doc/generated/constant-time.adoc @@ -63,5 +63,5 @@ Measured where a family differs from its backend's design: -* X25519 on libsodium, native: nothing found with arm64's DIT (data-independent timing) bit set, as the lane runs dudect on macOS and Linux on arm64 (elsewhere its harness sets no DIT) and as Apple requires of constant-time code (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]); without it, dudect times `+agree+` with an all-zero secret key against random ones at |t| 6.7 to 14.5, against the threshold 10, on an Apple M5 Max, from a release build (2026-11-19): about one nanosecond in 15 µs. libsodium does not set DIT: a program on Apple silicon sets it around its secret operations (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]) -* X25519 on libsodium, wasip2: an open finding, its cause measured: dudect times `+agree+` with an all-zero secret key against random ones at |t| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads |t| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next +* X25519 on libsodium, native: nothing found with arm64's DIT (data-independent timing) bit set, which webcpp sets around every call on 64-bit ARM under macOS, iOS, Linux and Android where the CPU has it, as Apple requires of constant-time code (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]); the lane's dudect sets nothing itself and measures what a program gets: |t| 1.8 to 2.8 against the threshold 10 (2026-11-21, five runs), while its `+dudect_planted+`, an early-exit scan of the secret key run inside the same guard, reads |t| 66.72 and 65.53 (2026-11-21): DIT does not hide a leak. Without DIT, dudect times `+agree+` with an all-zero secret key against random ones at |t| 6.7 to 14.5 (2026-11-19) and 4.3 to 7.8 (2026-11-21, three runs): about one nanosecond in 15 µs. Each on an Apple M5 Max, from a release build. libsodium does not set DIT, and webcpp sets nothing on x86, WebAssembly or Windows on ARM64 +* X25519 on libsodium, wasip2: an open finding, its cause measured: dudect times `+agree+` with an all-zero secret key against random ones at |t| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), and 25.25 in the lane on 2026-11-21, against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads |t| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next diff --git a/doc/generated/namespace-ciphers.adoc b/doc/generated/namespace-ciphers.adoc index ebd8ee24..2cb396fc 100644 --- a/doc/generated/namespace-ciphers.adoc +++ b/doc/generated/namespace-ciphers.adoc @@ -33,8 +33,8 @@ |none: a block cipher takes no nonce, and equal blocks give equal ciphertext |Speed, with and without hardware support -|seal of 16 KiB, 256-bit key: native, BearSSL 91.6 µs, OpenSSL 1.86 µs; wasip2, BearSSL 97.7 µs, OpenSSL 94.8 µs; wasip3, BearSSL 97.3 µs, OpenSSL 94.7 µs; emscripten, BearSSL 94.7 µs (2026-11-19, Apple M5 Max; <>) -|encryption of one 16-byte block, 256-bit key: native, OpenSSL 0.0096 µs, ctaes 0.31 µs; wasip2, OpenSSL 0.0719 µs, ctaes 0.349 µs; wasip3, OpenSSL 0.0729 µs, ctaes 0.346 µs; emscripten, ctaes 0.297 µs (2026-11-19, Apple M5 Max; <>) +|seal of 16 KiB, 256-bit key: native, BearSSL 103 µs, OpenSSL 2.14 µs; wasip2, BearSSL 107 µs, OpenSSL 97 µs; wasip3, BearSSL 107 µs, OpenSSL 112 µs; emscripten, BearSSL 101 µs (2026-11-20, Apple M5 Max, under load: read the spread; <>) +|encryption of one 16-byte block, 256-bit key: native, OpenSSL 0.0507 µs, ctaes 0.394 µs; wasip2, OpenSSL 0.0736 µs, ctaes 0.399 µs; wasip3, OpenSSL 0.0889 µs, ctaes 0.39 µs; emscripten, ctaes 0.304 µs (2026-11-20, Apple M5 Max, under load: read the spread; <>) |Constant time, by design |BearSSL and OpenSSL with assembly: yes (link:https://www.bearssl.org/constanttime.html[BearSSL, constant-time crypto]; link:https://github.com/openssl/openssl/tree/master/crypto/aes/asm[OpenSSL's AES assembly for ARMv8 and x86-64]); OpenSSL no-asm (MSVC, wasip2, wasip3): no (link:https://github.com/openssl/openssl/blob/master/INSTALL.md[OpenSSL's INSTALL.md, no-asm]) @@ -92,9 +92,9 @@ |not resistant to a repeated nonce, but 24 random bytes never repeat in practice (link:https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha[draft-irtf-cfrg-xchacha, XChaCha20-Poly1305]) |Speed, with and without hardware support -|seal of 16 KiB, 256-bit key: native, BearSSL 18.8 µs, OpenSSL 6.93 µs, libsodium 19.1 µs; wasip2, BearSSL 48.8 µs, OpenSSL 33.7 µs, libsodium 26.2 µs; wasip3, BearSSL 52.4 µs, OpenSSL 33.6 µs, libsodium 25.7 µs; emscripten, BearSSL 33.5 µs, libsodium 21.2 µs (2026-11-19, Apple M5 Max; <>) -|seal of 16 KiB, 256-bit key: native, libsodium 19.5 µs; wasip2, libsodium 25.6 µs; wasip3, libsodium 25.5 µs; emscripten, libsodium 21.1 µs (2026-11-19, Apple M5 Max; <>) -|seal of 16 KiB, 256-bit key: native, libsodium 19.2 µs; wasip2, libsodium 25.6 µs; wasip3, libsodium 25.9 µs; emscripten, libsodium 21.6 µs (2026-11-19, Apple M5 Max; <>) +|seal of 16 KiB, 256-bit key: native, BearSSL 20.8 µs, OpenSSL 7.54 µs, libsodium 21.1 µs; wasip2, BearSSL 51.2 µs, OpenSSL 35.8 µs, libsodium 30.2 µs; wasip3, BearSSL 66.6 µs, OpenSSL 41.1 µs, libsodium 32.2 µs; emscripten, BearSSL 41.5 µs, libsodium 25.5 µs (2026-11-20, Apple M5 Max, under load: read the spread; <>) +|seal of 16 KiB, 256-bit key: native, libsodium 23 µs; wasip2, libsodium 27.2 µs; wasip3, libsodium 28.9 µs; emscripten, libsodium 23.5 µs (2026-11-20, Apple M5 Max, under load: read the spread; <>) +|seal of 16 KiB, 256-bit key: native, libsodium 21.7 µs; wasip2, libsodium 28.4 µs; wasip3, libsodium 27.6 µs; emscripten, libsodium 22.4 µs (2026-11-20, Apple M5 Max, under load: read the spread; <>) |Constant time, by design |libsodium, BearSSL and OpenSSL: yes, additions, XORs and rotations with no table indexed by a secret (link:https://www.rfc-editor.org/rfc/rfc8439#section-2.1[RFC 8439, section 2.1: ChaCha's quarter round, additions, XORs and rotations of 32-bit words]; link:https://doc.libsodium.org/secret-key_cryptography/aead[libsodium's documentation, AEAD constructions: ChaCha20 and timing attacks]) diff --git a/doc/generated/namespace-curves.adoc b/doc/generated/namespace-curves.adoc index 63d39838..f1b85eae 100644 --- a/doc/generated/namespace-curves.adoc +++ b/doc/generated/namespace-curves.adoc @@ -32,14 +32,14 @@ |none: no signatures |Speed -|BIP340 signature of a 32-byte message: native, libsecp256k1 7.98 µs; wasip2, libsecp256k1 50.8 µs; wasip3, libsecp256k1 56.6 µs; emscripten, libsecp256k1 43.1 µs (2026-11-19, Apple M5 Max; <>) -|signature of a 32-byte message: native, libsodium 7.51 µs; wasip2, libsodium 17.4 µs; wasip3, libsodium 17.5 µs; emscripten, libsodium 15.3 µs (2026-11-19, Apple M5 Max; <>) -|agreement of a secret key and a public key: native, libsodium 15.9 µs; wasip2, libsodium 40.4 µs; wasip3, libsodium 40.4 µs; emscripten, libsodium 35.5 µs (2026-11-19, Apple M5 Max; <>) +|BIP340 signature of a 32-byte message: native, libsecp256k1 9.02 µs; wasip2, libsecp256k1 53.9 µs; wasip3, libsecp256k1 65.9 µs; emscripten, libsecp256k1 63.9 µs (2026-11-20, Apple M5 Max, under load: read the spread; <>) +|signature of a 32-byte message: native, libsodium 8.2 µs; wasip2, libsodium 19.8 µs; wasip3, libsodium 25.5 µs; emscripten, libsodium 20.6 µs (2026-11-20, Apple M5 Max, under load: read the spread; <>) +|agreement of a secret key and a public key: native, libsodium 17.9 µs; wasip2, libsodium 49.5 µs; wasip3, libsodium 56 µs; emscripten, libsodium 41.3 µs (2026-11-20, Apple M5 Max, under load: read the spread; <>) |Constant time, measured |libsecp256k1: native, Valgrind's memcheck (Linux; libsecp256k1's own `+ctime_tests.c+`, and webcpp's code around it: the key pair's making, its signatures and x-only tweak, ECDH in three forms, the secret key's tweaks and negation, and PKCS#8), MemorySanitizer (Linux, clang; the same paths as memcheck's) and dudect (judged off the runners GitHub hosts; `+ecdh_x+`), nothing found; wasip2, dudect (judged off the runners GitHub hosts; `+ecdh_x+`), nothing found; not measured yet on wasip3, emscripten (<>) |libsodium: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged off the runners GitHub hosts), nothing found; wasip2, dudect (judged off the runners GitHub hosts), nothing found; not measured yet on wasip3, emscripten (<>) -|libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found, and dudect (judged off the runners GitHub hosts): nothing found with arm64's DIT (data-independent timing) bit set, as the lane runs dudect on macOS and Linux on arm64 (elsewhere its harness sets no DIT) and as Apple requires of constant-time code (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]); without it, dudect times `agree` with an all-zero secret key against random ones at \|t\| 6.7 to 14.5, against the threshold 10, on an Apple M5 Max, from a release build (2026-11-19): about one nanosecond in 15 µs. libsodium does not set DIT: a program on Apple silicon sets it around its secret operations (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]); wasip2, dudect (judged off the runners GitHub hosts): an open finding, its cause measured: dudect times `agree` with an all-zero secret key against random ones at \|t\| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads \|t\| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next; not measured yet on wasip3, emscripten (<>) +|libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found, and dudect (judged off the runners GitHub hosts): nothing found with arm64's DIT (data-independent timing) bit set, which webcpp sets around every call on 64-bit ARM under macOS, iOS, Linux and Android where the CPU has it, as Apple requires of constant-time code (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]); the lane's dudect sets nothing itself and measures what a program gets: \|t\| 1.8 to 2.8 against the threshold 10 (2026-11-21, five runs), while its `dudect_planted`, an early-exit scan of the secret key run inside the same guard, reads \|t\| 66.72 and 65.53 (2026-11-21): DIT does not hide a leak. Without DIT, dudect times `agree` with an all-zero secret key against random ones at \|t\| 6.7 to 14.5 (2026-11-19) and 4.3 to 7.8 (2026-11-21, three runs): about one nanosecond in 15 µs. Each on an Apple M5 Max, from a release build. libsodium does not set DIT, and webcpp sets nothing on x86, WebAssembly or Windows on ARM64; wasip2, dudect (judged off the runners GitHub hosts): an open finding, its cause measured: dudect times `agree` with an all-zero secret key against random ones at \|t\| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), and 25.25 in the lane on 2026-11-21, against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set (link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations]): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads \|t\| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next; not measured yet on wasip3, emscripten (<>) |Standards |link:https://www.secg.org/sec2-v2.pdf[SEC 2, Recommended Elliptic Curve Domain Parameters], link:https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki[BIP 340, Schnorr signatures for secp256k1], link:https://www.rfc-editor.org/rfc/rfc6979[RFC 6979, deterministic ECDSA nonces], link:https://github.com/bitcoin/bips/blob/master/bip-0324.mediawiki[BIP 324, version 2 P2P encrypted transport protocol], link:https://github.com/bitcoin/bips/blob/master/bip-0327.mediawiki[BIP 327, MuSig2 for BIP340-compatible multi-signatures], link:https://github.com/bitcoin/bips/blob/master/bip-0352.mediawiki[BIP 352, Silent Payments] diff --git a/doc/llms.txt b/doc/llms.txt index d4a3061d..1a4650e4 100644 --- a/doc/llms.txt +++ b/doc/llms.txt @@ -214,7 +214,7 @@ Generated by doc/tables/generate.py with the page's tables. Page: https://webcpp - AES block cipher: OpenSSL: not measured yet. ctaes: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged off the runners GitHub hosts), nothing found; wasip2, dudect (judged off the runners GitHub hosts), nothing found; not measured yet on wasip3, emscripten. - secp256k1: libsecp256k1: native, Valgrind's memcheck (Linux; libsecp256k1's own `ctime_tests.c`, and webcpp's code around it: the key pair's making, its signatures and x-only tweak, ECDH in three forms, the secret key's tweaks and negation, and PKCS#8), MemorySanitizer (Linux, clang; the same paths as memcheck's) and dudect (judged off the runners GitHub hosts; `ecdh_x`), nothing found; wasip2, dudect (judged off the runners GitHub hosts; `ecdh_x`), nothing found; not measured yet on wasip3, emscripten. - Ed25519: libsodium: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged off the runners GitHub hosts), nothing found; wasip2, dudect (judged off the runners GitHub hosts), nothing found; not measured yet on wasip3, emscripten. -- X25519: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found, and dudect (judged off the runners GitHub hosts): nothing found with arm64's DIT (data-independent timing) bit set, as the lane runs dudect on macOS and Linux on arm64 (elsewhere its harness sets no DIT) and as Apple requires of constant-time code ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)); without it, dudect times `agree` with an all-zero secret key against random ones at |t| 6.7 to 14.5, against the threshold 10, on an Apple M5 Max, from a release build (2026-11-19): about one nanosecond in 15 µs. libsodium does not set DIT: a program on Apple silicon sets it around its secret operations ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)); wasip2, dudect (judged off the runners GitHub hosts): an open finding, its cause measured: dudect times `agree` with an all-zero secret key against random ones at |t| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads |t| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next; not measured yet on wasip3, emscripten. +- X25519: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found, and dudect (judged off the runners GitHub hosts): nothing found with arm64's DIT (data-independent timing) bit set, which webcpp sets around every call on 64-bit ARM under macOS, iOS, Linux and Android where the CPU has it, as Apple requires of constant-time code ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)); the lane's dudect sets nothing itself and measures what a program gets: |t| 1.8 to 2.8 against the threshold 10 (2026-11-21, five runs), while its `dudect_planted`, an early-exit scan of the secret key run inside the same guard, reads |t| 66.72 and 65.53 (2026-11-21): DIT does not hide a leak. Without DIT, dudect times `agree` with an all-zero secret key against random ones at |t| 6.7 to 14.5 (2026-11-19) and 4.3 to 7.8 (2026-11-21, three runs): about one nanosecond in 15 µs. Each on an Apple M5 Max, from a release build. libsodium does not set DIT, and webcpp sets nothing on x86, WebAssembly or Windows on ARM64; wasip2, dudect (judged off the runners GitHub hosts): an open finding, its cause measured: dudect times `agree` with an all-zero secret key against random ones at |t| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), and 25.25 in the lane on 2026-11-21, against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set ([Apple, Writing ARM64 code for Apple platforms: enable DIT for constant-time cryptographic operations](https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations)): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads |t| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next; not measured yet on wasip3, emscripten. - crypto_kx: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. - PEM: libsodium: not measured yet. - ChaCha20-Poly1305: libsodium: native, Valgrind's memcheck (Linux) and MemorySanitizer (Linux, clang), nothing found; not measured yet on wasip2, wasip3, emscripten. BearSSL: native, Valgrind's memcheck (Linux), MemorySanitizer (Linux, clang) and dudect (judged off the runners GitHub hosts), nothing found; wasip2, dudect (judged off the runners GitHub hosts), nothing found; not measured yet on wasip3, emscripten. OpenSSL (seal alone: its open branches on the verdict inside OpenSSL): native, Valgrind's memcheck (Linux), nothing found; not measured yet on wasip2, wasip3. diff --git a/doc/memory.adoc b/doc/memory.adoc index f4d53d64..32689bfb 100644 --- a/doc/memory.adoc +++ b/doc/memory.adoc @@ -11,7 +11,10 @@ An audio worklet, a game loop or an embedded device must not allocate on its hot path, or must allocate only from its own arena. crypto's own code allocates one thing: a secp256k1 key pair's signing context, once, in `keypair::make`, from the `std::pmr::memory_resource` the program passes, which must outlive the -key pair. Every buffer is the caller's. +key pair. A resource that cannot allocate throws, as the standard has it, and +`make` lets the exception through; without exceptions such a resource ends the +process. It never returns null (<> says why). Every +buffer is the caller's. * *Nothing allocates per message, on any backend:* `seal`, `open`, `begin_seal`, `update`, `result`, `encrypt`, `decrypt`, `fill`, the signatures @@ -183,9 +186,14 @@ prints how many blocks stay; with threads, as `fail_init_threads`, it prints how many blocks the first `make` took instead. After the initialisation, `make` gives `not_enough_memory` or `backend_failure`. `seal` and `open` allocate nothing. A resource of a fixed size needs room for the first `make`, whose -allocations the test `allocation_openssl` prints on each run. Without -exceptions, a resource that throws ends the process; one that returns null -reports the failure. +allocations the test `allocation_openssl` prints on each run. A resource +reports that it cannot allocate as the standard has `do_allocate` report it, +by throwing, `std::bad_alloc` or another exception, which the adapter turns +into OpenSSL's null. It never returns null: libc++ and libstdc++ declare +`allocate` never to return null, so the optimiser removes any check of its +result, and a null is written through in a release build. Without +exceptions, a resource that cannot allocate ends the process, as `std::pmr`'s +own resources do, and the failures above are not reached. * *Two allocations stay outside the resource,* because no hook of OpenSSL's reaches them: the C library's `FILE` and its buffer while OpenSSL reads its configuration file at initialisation (one or two blocks, by C library, freed diff --git a/doc/performance.adoc b/doc/performance.adoc index de7185ee..aca03cc4 100644 --- a/doc/performance.adoc +++ b/doc/performance.adoc @@ -14,7 +14,7 @@ the size matrix's programs call (bench/operations.hpp). A dated reference, measu hand, that decides nothing: every default is chosen by the reason <> states, whatever these figures say. -Measured on 2026-11-19 on Apple M5 Max (arm64), +Measured on 2026-11-20 on Apple M5 Max (arm64), 18 cores, 128 GiB, Darwin 27.0.0; WebAssembly under wasmtime 47.0.3 (5554cc1a6 2026-07-31) (wasip2, wasip3) and node v26.13.0 (Emscripten); built with variant=release. The CI's runners @@ -24,8 +24,12 @@ Each cell is the median of its invocations' medians, each of five timed runs, in microseconds per operation, with the fastest and slowest single run beside it: the spread is part of the result. -The machine's 1, 5 and 15 minute load averages were 3/3/5 before and -3/3/5 after, on 18 cores. +The machine's 1, 5 and 15 minute load averages were 11/70/106 before and +20/56/96 after, on 18 cores. + +NOTE: these figures are provisional. They were measured on a shared machine under load (the load averages above), and are measured again in a final measurement pass on a quiet machine, once the work in progress has merged; read them as an upper bound. + +WARNING: the machine was shared and loaded while this was measured (rule: the larger of the two 1-minute load averages exceeds half the core count). Read the spread, not a single figure. [cols="3,2,1,1,1,1,2",options="header"] |=== @@ -34,362 +38,362 @@ The machine's 1, 5 and 15 minute load averages were 3/3/5 before and | AES-GCM: seal of 16 KiB, 256-bit key | BearSSL | native -| 91.61 -| 90.34 -| 95.78 -| 171 MiB/s +| 103.32 +| 96.38 +| 127.68 +| 151 MiB/s | AES-GCM: seal of 16 KiB, 256-bit key | BearSSL | wasip2 -| 97.67 -| 96.67 -| 100.85 -| 160 MiB/s +| 107.11 +| 99.31 +| 153.44 +| 146 MiB/s | AES-GCM: seal of 16 KiB, 256-bit key | BearSSL | wasip3 -| 97.25 -| 96.19 -| 99.90 -| 161 MiB/s +| 107.06 +| 98.50 +| 150.95 +| 146 MiB/s | AES-GCM: seal of 16 KiB, 256-bit key | BearSSL | emscripten -| 94.73 -| 94.20 -| 98.68 -| 165 MiB/s +| 100.81 +| 97.42 +| 142.90 +| 155 MiB/s | AES-GCM: seal of 16 KiB, 256-bit key | OpenSSL | native -| 1.86 -| 1.85 -| 1.89 -| 8,389 MiB/s +| 2.14 +| 1.95 +| 2.71 +| 7,311 MiB/s | AES-GCM: seal of 16 KiB, 256-bit key | OpenSSL | wasip2 -| 94.82 -| 93.54 -| 98.69 -| 165 MiB/s +| 96.97 +| 96.30 +| 131.54 +| 161 MiB/s | AES-GCM: seal of 16 KiB, 256-bit key | OpenSSL | wasip3 -| 94.72 -| 94.24 -| 97.04 -| 165 MiB/s +| 112.04 +| 96.40 +| 130.99 +| 139 MiB/s | AES block cipher: encryption of one 16-byte block, 256-bit key | ctaes | native -| 0.31 -| 0.305 -| 0.326 -| 3,225,806 operations/s +| 0.394 +| 0.378 +| 0.436 +| 2,536,140 operations/s | AES block cipher: encryption of one 16-byte block, 256-bit key | ctaes | wasip2 -| 0.349 -| 0.344 -| 0.357 -| 2,866,972 operations/s +| 0.399 +| 0.352 +| 0.647 +| 2,508,781 operations/s | AES block cipher: encryption of one 16-byte block, 256-bit key | ctaes | wasip3 -| 0.346 -| 0.344 -| 0.356 -| 2,891,845 operations/s +| 0.39 +| 0.351 +| 0.507 +| 2,565,418 operations/s | AES block cipher: encryption of one 16-byte block, 256-bit key | ctaes | emscripten -| 0.297 -| 0.292 -| 0.306 -| 3,368,137 operations/s +| 0.304 +| 0.299 +| 0.472 +| 3,292,723 operations/s | AES block cipher: encryption of one 16-byte block, 256-bit key | OpenSSL | native -| 0.0096 -| 0.0094 -| 0.0099 -| 104,166,667 operations/s +| 0.0507 +| 0.05 +| 0.0559 +| 19,723,866 operations/s | AES block cipher: encryption of one 16-byte block, 256-bit key | OpenSSL | wasip2 -| 0.0719 -| 0.0698 | 0.0736 -| 13,908,206 operations/s +| 0.073 +| 0.128 +| 13,586,957 operations/s | AES block cipher: encryption of one 16-byte block, 256-bit key | OpenSSL | wasip3 -| 0.0729 -| 0.0722 -| 0.0742 -| 13,717,421 operations/s +| 0.0889 +| 0.0739 +| 0.113 +| 11,248,594 operations/s | secp256k1: BIP340 signature of a 32-byte message | libsecp256k1 | native -| 7.98 -| 7.85 -| 8.24 -| 125,356 operations/s +| 9.02 +| 8.62 +| 14.22 +| 110,806 operations/s | secp256k1: BIP340 signature of a 32-byte message | libsecp256k1 | wasip2 -| 50.83 -| 50.58 -| 51.46 -| 19,674 operations/s +| 53.88 +| 51.32 +| 79.66 +| 18,560 operations/s | secp256k1: BIP340 signature of a 32-byte message | libsecp256k1 | wasip3 -| 56.65 -| 56.29 -| 57.39 -| 17,654 operations/s +| 65.87 +| 57.31 +| 86.43 +| 15,182 operations/s | secp256k1: BIP340 signature of a 32-byte message | libsecp256k1 | emscripten -| 43.06 -| 42.85 -| 44.00 -| 23,224 operations/s +| 63.86 +| 44.52 +| 71.52 +| 15,658 operations/s | Ed25519: signature of a 32-byte message | libsodium | native -| 7.51 -| 7.35 -| 7.82 -| 133,073 operations/s +| 8.20 +| 7.81 +| 11.53 +| 121,892 operations/s | Ed25519: signature of a 32-byte message | libsodium | wasip2 -| 17.40 -| 17.32 -| 17.68 -| 57,482 operations/s +| 19.84 +| 17.56 +| 27.42 +| 50,397 operations/s | Ed25519: signature of a 32-byte message | libsodium | wasip3 -| 17.51 -| 17.32 -| 17.81 -| 57,103 operations/s +| 25.52 +| 17.95 +| 29.76 +| 39,183 operations/s | Ed25519: signature of a 32-byte message | libsodium | emscripten -| 15.29 -| 15.22 -| 15.53 -| 65,386 operations/s +| 20.63 +| 15.49 +| 24.06 +| 48,464 operations/s | X25519: agreement of a secret key and a public key | libsodium | native -| 15.89 -| 15.64 -| 16.89 -| 62,936 operations/s +| 17.93 +| 16.57 +| 22.21 +| 55,762 operations/s | X25519: agreement of a secret key and a public key | libsodium | wasip2 -| 40.41 -| 40.24 -| 46.86 -| 24,749 operations/s +| 49.46 +| 40.99 +| 85.84 +| 20,217 operations/s | X25519: agreement of a secret key and a public key | libsodium | wasip3 -| 40.39 -| 40.26 -| 41.00 -| 24,759 operations/s +| 56.05 +| 41.43 +| 77.40 +| 17,842 operations/s | X25519: agreement of a secret key and a public key | libsodium | emscripten -| 35.50 -| 35.22 -| 35.89 -| 28,167 operations/s +| 41.28 +| 35.73 +| 66.91 +| 24,225 operations/s | ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | BearSSL | native -| 18.78 -| 18.47 -| 19.68 -| 832 MiB/s +| 20.77 +| 20.29 +| 31.24 +| 752 MiB/s | ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | BearSSL | wasip2 -| 48.84 -| 48.33 -| 50.03 -| 320 MiB/s +| 51.25 +| 49.60 +| 66.06 +| 305 MiB/s | ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | BearSSL | wasip3 -| 52.39 -| 51.89 -| 54.07 -| 298 MiB/s +| 66.64 +| 54.25 +| 79.08 +| 234 MiB/s | ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | BearSSL | emscripten -| 33.52 -| 33.16 -| 34.53 -| 466 MiB/s +| 41.53 +| 34.15 +| 56.87 +| 376 MiB/s | ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | OpenSSL | native -| 6.93 -| 6.91 -| 6.99 -| 2,255 MiB/s +| 7.54 +| 7.08 +| 8.32 +| 2,071 MiB/s | ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | OpenSSL | wasip2 -| 33.67 -| 33.02 -| 34.53 -| 464 MiB/s +| 35.81 +| 34.43 +| 41.95 +| 436 MiB/s | ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | OpenSSL | wasip3 -| 33.64 -| 33.15 -| 35.27 -| 464 MiB/s +| 41.13 +| 34.34 +| 48.83 +| 380 MiB/s | ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | libsodium | native -| 19.11 -| 19.05 -| 20.43 -| 818 MiB/s +| 21.10 +| 20.40 +| 24.40 +| 741 MiB/s | ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | libsodium | wasip2 -| 26.16 -| 25.27 -| 26.76 -| 597 MiB/s +| 30.22 +| 26.43 +| 48.54 +| 517 MiB/s | ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | libsodium | wasip3 -| 25.66 -| 25.16 -| 26.73 -| 609 MiB/s +| 32.20 +| 26.43 +| 37.54 +| 485 MiB/s | ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | libsodium | emscripten -| 21.20 -| 20.93 -| 22.19 -| 737 MiB/s +| 25.48 +| 22.17 +| 43.63 +| 613 MiB/s | original ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | libsodium | native -| 19.48 -| 19.03 -| 20.42 -| 802 MiB/s +| 23.05 +| 20.40 +| 29.65 +| 678 MiB/s | original ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | libsodium | wasip2 -| 25.63 -| 25.24 -| 26.40 -| 610 MiB/s +| 27.18 +| 26.41 +| 33.08 +| 575 MiB/s | original ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | libsodium | wasip3 -| 25.52 -| 25.12 -| 25.91 -| 612 MiB/s +| 28.92 +| 26.42 +| 35.30 +| 540 MiB/s | original ChaCha20-Poly1305: seal of 16 KiB, 256-bit key | libsodium | emscripten -| 21.14 -| 20.86 -| 22.23 -| 739 MiB/s +| 23.46 +| 22.33 +| 34.39 +| 666 MiB/s | XChaCha20-Poly1305: seal of 16 KiB, 256-bit key | libsodium | native -| 19.20 -| 19.10 -| 20.50 -| 814 MiB/s +| 21.73 +| 20.47 +| 30.67 +| 719 MiB/s | XChaCha20-Poly1305: seal of 16 KiB, 256-bit key | libsodium | wasip2 -| 25.62 -| 25.22 -| 25.80 -| 610 MiB/s +| 28.42 +| 26.48 +| 36.93 +| 550 MiB/s | XChaCha20-Poly1305: seal of 16 KiB, 256-bit key | libsodium | wasip3 -| 25.87 -| 25.37 -| 26.49 -| 604 MiB/s +| 27.58 +| 26.48 +| 36.58 +| 567 MiB/s | XChaCha20-Poly1305: seal of 16 KiB, 256-bit key | libsodium | emscripten -| 21.57 -| 21.02 -| 22.29 -| 724 MiB/s +| 22.39 +| 22.24 +| 32.54 +| 698 MiB/s |=== Compilers: native: Apple LLVM 21.0.0 (clang-2100.4.1.1); wasip2: Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141); wasip3: Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141); emscripten: Clang 24.0.0git (https:/github.com/llvm/llvm-project f718ebe873411a562ef6e5c27da36d75b7aaec98). @@ -398,3 +402,48 @@ OpenSSL is not built for Emscripten, so its rows are native and WASI only; on WA is built `no-asm`, natively with its assembly. libsodium's x86-64 assembly (design D7): Ed25519 signature, libsodium with HAVE_AMD64_ASM; X25519 agreement, libsodium with HAVE_AMD64_ASM: not measured yet. x86-64 assembly runs only on an x86-64 machine; this page's machine is arm64. D7 asks for the rows recorded, never used: webcpp builds libsodium without it. + +[#crypto-performance-timing] +==== The timing guard's cost + +Every public operation runs inside `detail::timing_guard`, which on 64-bit ARM under +macOS, iOS, Linux and Android, where the CPU has FEAT_DIT, sets DIT (data-independent +timing) for the call and restores the thread's state after it +(<>): one `mrs`, and where DIT was clear, two `msr` +each followed by an `isb`. Elsewhere (x86, WebAssembly, Windows on ARM64) it executes +no instruction. + +Measured natively on 2026-11-20 on Apple M5 Max +(arm64), Darwin 27.0.0, built with variant=release +by Apple LLVM 21.0.0 (clang-2100.4.1.1) (bench/timing.cpp): each row is the median of +5 runs of 10,000,000 guards around a store, with the +fastest and slowest run beside it, each minus the median of the same loop without the +guard, in nanoseconds per call. The guard sets DIT on this machine. +The machine's 1, 5 and 15 minute load averages were 20/56/96 before and +20/56/96 after, on 18 cores. + +NOTE: these figures are provisional. They were measured on a shared machine under load (the load averages above), and are measured again in a final measurement pass on a quiet machine, once the work in progress has merged; read them as an upper bound. + +WARNING: the machine was shared and loaded while this was measured (rule: the larger of the two 1-minute load averages exceeds half the core count). + +[cols="2,4,1,1,1",options="header"] +|=== +| The thread's DIT on entry | What the guard does | Median (ns) | Fastest (ns) | Slowest (ns) + +| clear, as a program that sets nothing has it +| reads DIT, sets it, and clears it on the way out +| 40.3 +| not recorded +| not recorded + +| already set, by the program or by an outer guard +| reads DIT and leaves it set +| 0.7 +| not recorded +| not recorded +|=== + +For scale, the shortest operation of the table above natively, +AES block cipher (encryption of one 16-byte block, 256-bit key) on +OpenSSL, takes 50.7 ns, +its guard included. diff --git a/doc/performance.json b/doc/performance.json index 236bc523..7dd6c1c8 100644 --- a/doc/performance.json +++ b/doc/performance.json @@ -1,6 +1,6 @@ { "measurement": "each family's fixed operation per backend and target, bench/operations.hpp", - "date": "2026-11-19", + "date": "2026-11-20", "machine": { "cpu": "Apple M5 Max", "architecture": "arm64", @@ -12,14 +12,14 @@ }, "load_average_before_after": [ [ - 2.744140625, - 3.103515625, - 4.697265625 + 10.6513671875, + 69.86572265625, + 105.9052734375 ], [ - 3.36376953125, - 3.23876953125, - 4.5390625 + 19.90380859375, + 56.12353515625, + 95.6845703125 ] ], "build": "variant=release", @@ -31,24 +31,24 @@ "operation": "seal of 16 KiB, 256-bit key", "compiler": "Apple LLVM 21.0.0 (clang-2100.4.1.1)", "repetitions": [ - 1024, - 1024, - 1024, + 512, + 512, + 512, 1024, 1024 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 90.6494, - 91.3634, - 91.6895, - 91.609, - 92.1367 - ], - "median_us": 91.609, - "min_us": 90.342, - "max_us": 95.7801 + 98.8047, + 104.23, + 110.7644, + 98.2049, + 103.3168 + ], + "median_us": 103.3168, + "min_us": 96.3768, + "max_us": 127.6825 }, { "family": "gcm", @@ -66,15 +66,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 1.8624, - 1.8818, - 1.8528, - 1.8763, - 1.8626 - ], - "median_us": 1.8626, - "min_us": 1.8475, - "max_us": 1.891 + 2.1371, + 2.1671, + 2.4846, + 1.9977, + 2.0276 + ], + "median_us": 2.1371, + "min_us": 1.9515, + "max_us": 2.7085 }, { "family": "aes_block", @@ -83,24 +83,24 @@ "operation": "encryption of one 16-byte block, 256-bit key", "compiler": "Apple LLVM 21.0.0 (clang-2100.4.1.1)", "repetitions": [ - 8388608, - 8388608, - 8388608, - 8388608, - 8388608 + 1048576, + 1048576, + 1048576, + 1048576, + 1048576 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 0.0094, - 0.0096, - 0.0098, - 0.0096, - 0.0096 - ], - "median_us": 0.0096, - "min_us": 0.0094, - "max_us": 0.0099 + 0.0507, + 0.0539, + 0.0541, + 0.0507, + 0.0505 + ], + "median_us": 0.0507, + "min_us": 0.05, + "max_us": 0.0559 }, { "family": "aes_block", @@ -109,24 +109,24 @@ "operation": "encryption of one 16-byte block, 256-bit key", "compiler": "Apple LLVM 21.0.0 (clang-2100.4.1.1)", "repetitions": [ - 262144, - 262144, - 262144, - 262144, + 131072, + 131072, + 131072, + 131072, 262144 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 0.31, - 0.31, - 0.3062, - 0.3056, - 0.3211 - ], - "median_us": 0.31, - "min_us": 0.3051, - "max_us": 0.3258 + 0.3943, + 0.3959, + 0.4189, + 0.3784, + 0.38 + ], + "median_us": 0.3943, + "min_us": 0.3775, + "max_us": 0.4365 }, { "family": "secp256k1", @@ -144,15 +144,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 7.8526, - 7.9191, - 7.9773, - 8.0798, - 8.0029 - ], - "median_us": 7.9773, - "min_us": 7.8465, - "max_us": 8.2447 + 9.0248, + 11.8183, + 10.0879, + 8.6328, + 8.7116 + ], + "median_us": 9.0248, + "min_us": 8.6193, + "max_us": 14.2206 }, { "family": "ed25519", @@ -170,15 +170,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 7.4126, - 7.4519, - 7.5147, - 7.6017, - 7.5249 - ], - "median_us": 7.5147, - "min_us": 7.353, - "max_us": 7.8211 + 8.204, + 10.1714, + 8.64, + 7.854, + 7.8646 + ], + "median_us": 8.204, + "min_us": 7.8098, + "max_us": 11.5266 }, { "family": "x25519", @@ -196,15 +196,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 15.8216, - 15.8207, - 15.8891, - 15.9141, - 16.0397 - ], - "median_us": 15.8891, - "min_us": 15.6381, - "max_us": 16.8862 + 17.6831, + 18.1857, + 18.3415, + 16.8713, + 17.9335 + ], + "median_us": 17.9335, + "min_us": 16.5724, + "max_us": 22.2107 }, { "family": "chacha20_poly1305", @@ -222,15 +222,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 19.0745, - 19.5807, - 19.0675, - 19.1498, - 19.1088 - ], - "median_us": 19.1088, - "min_us": 19.0482, - "max_us": 20.4328 + 21.1765, + 22.2371, + 21.0199, + 21.0981, + 20.4914 + ], + "median_us": 21.0981, + "min_us": 20.4033, + "max_us": 24.4028 }, { "family": "chacha20_poly1305", @@ -239,7 +239,7 @@ "operation": "seal of 16 KiB, 256-bit key", "compiler": "Apple LLVM 21.0.0 (clang-2100.4.1.1)", "repetitions": [ - 4096, + 2048, 4096, 4096, 4096, @@ -248,15 +248,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 18.8026, - 18.6083, - 18.7849, - 18.6994, - 19.3057 - ], - "median_us": 18.7849, - "min_us": 18.4655, - "max_us": 19.679 + 28.5672, + 21.2116, + 20.7705, + 20.3037, + 20.317 + ], + "median_us": 20.7705, + "min_us": 20.2856, + "max_us": 31.2372 }, { "family": "chacha20_poly1305", @@ -274,15 +274,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 6.9211, - 6.9286, - 6.931, - 6.9266, - 6.9436 - ], - "median_us": 6.9286, - "min_us": 6.9103, - "max_us": 6.9903 + 7.9891, + 7.5442, + 8.2329, + 7.1994, + 7.0862 + ], + "median_us": 7.5442, + "min_us": 7.0829, + "max_us": 8.3154 }, { "family": "chacha20_poly1305_original", @@ -291,24 +291,24 @@ "operation": "seal of 16 KiB, 256-bit key", "compiler": "Apple LLVM 21.0.0 (clang-2100.4.1.1)", "repetitions": [ + 2048, 4096, - 4096, - 4096, + 2048, 4096, 4096 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 19.4815, - 19.0913, - 19.5023, - 19.1187, - 19.5372 - ], - "median_us": 19.4815, - "min_us": 19.0344, - "max_us": 20.4157 + 26.3642, + 23.0471, + 26.4208, + 20.4068, + 20.4075 + ], + "median_us": 23.0471, + "min_us": 20.4021, + "max_us": 29.6451 }, { "family": "xchacha20_poly1305", @@ -317,24 +317,24 @@ "operation": "seal of 16 KiB, 256-bit key", "compiler": "Apple LLVM 21.0.0 (clang-2100.4.1.1)", "repetitions": [ + 2048, 4096, - 4096, - 4096, + 2048, 4096, 4096 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 19.2753, - 19.1505, - 19.1992, - 19.9426, - 19.1831 - ], - "median_us": 19.1992, - "min_us": 19.0975, - "max_us": 20.4979 + 27.8997, + 21.7305, + 26.1057, + 20.4797, + 20.584 + ], + "median_us": 21.7305, + "min_us": 20.4655, + "max_us": 30.6661 }, { "family": "gcm", @@ -344,23 +344,23 @@ "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ 512, - 1024, 512, 512, - 1024 + 512, + 512 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 96.938, - 98.0875, - 97.802, - 97.6379, - 97.6748 - ], - "median_us": 97.6748, - "min_us": 96.6731, - "max_us": 100.8546 + 145.9158, + 107.1113, + 128.3739, + 99.3409, + 99.3902 + ], + "median_us": 107.1113, + "min_us": 99.3074, + "max_us": 153.4439 }, { "family": "gcm", @@ -369,24 +369,24 @@ "operation": "seal of 16 KiB, 256-bit key", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ + 512, + 512, + 512, 1024, - 1024, - 1024, - 1024, - 1024 + 512 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 94.2485, - 94.7267, - 94.9155, - 95.8748, - 94.8213 - ], - "median_us": 94.8213, - "min_us": 93.5433, - "max_us": 98.6871 + 119.6616, + 101.186, + 96.4217, + 96.3689, + 96.9688 + ], + "median_us": 96.9688, + "min_us": 96.3007, + "max_us": 131.5442 }, { "family": "aes_block", @@ -395,7 +395,7 @@ "operation": "encryption of one 16-byte block, 256-bit key", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ - 1048576, + 524288, 1048576, 1048576, 1048576, @@ -404,15 +404,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 0.0701, - 0.0719, - 0.0729, - 0.0721, - 0.0707 - ], - "median_us": 0.0719, - "min_us": 0.0698, - "max_us": 0.0736 + 0.1101, + 0.0989, + 0.0735, + 0.0736, + 0.0733 + ], + "median_us": 0.0736, + "min_us": 0.073, + "max_us": 0.1279 }, { "family": "aes_block", @@ -421,24 +421,24 @@ "operation": "encryption of one 16-byte block, 256-bit key", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ + 131072, + 131072, 262144, - 262144, - 262144, - 262144, + 131072, 262144 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 0.3449, - 0.3461, - 0.3489, - 0.3488, - 0.3512 - ], - "median_us": 0.3488, - "min_us": 0.3439, - "max_us": 0.3573 + 0.5754, + 0.4596, + 0.3607, + 0.3907, + 0.3986 + ], + "median_us": 0.3986, + "min_us": 0.3525, + "max_us": 0.6475 }, { "family": "secp256k1", @@ -456,15 +456,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 51.3193, - 50.7191, - 51.0461, - 50.708, - 50.8283 - ], - "median_us": 50.8283, - "min_us": 50.5786, - "max_us": 51.4637 + 73.0942, + 72.2137, + 53.6623, + 52.0558, + 53.8806 + ], + "median_us": 53.8806, + "min_us": 51.3226, + "max_us": 79.6647 }, { "family": "ed25519", @@ -473,8 +473,8 @@ "operation": "signature of a 32-byte message", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ - 4096, - 4096, + 2048, + 2048, 4096, 4096, 4096 @@ -482,15 +482,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 17.3699, - 17.3753, - 17.5286, - 17.4165, - 17.3966 - ], - "median_us": 17.3966, - "min_us": 17.3243, - "max_us": 17.6842 + 25.3588, + 24.5698, + 19.8426, + 17.7641, + 18.0283 + ], + "median_us": 19.8426, + "min_us": 17.5626, + "max_us": 27.4213 }, { "family": "x25519", @@ -499,24 +499,24 @@ "operation": "agreement of a secret key and a public key", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ + 1024, + 1024, 2048, - 2048, - 2048, - 2048, + 1024, 2048 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 40.3779, - 40.2767, - 40.5718, - 40.6907, - 40.405 - ], - "median_us": 40.405, - "min_us": 40.2391, - "max_us": 46.8617 + 60.9593, + 49.4645, + 54.333, + 43.2848, + 42.8718 + ], + "median_us": 49.4645, + "min_us": 40.9906, + "max_us": 85.8366 }, { "family": "chacha20_poly1305", @@ -525,7 +525,7 @@ "operation": "seal of 16 KiB, 256-bit key", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ - 2048, + 1024, 2048, 2048, 2048, @@ -534,15 +534,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 26.4228, - 25.3649, - 26.1591, - 25.724, - 26.2533 - ], - "median_us": 26.1591, - "min_us": 25.2685, - "max_us": 26.7573 + 38.4281, + 34.7759, + 30.2173, + 26.4405, + 26.521 + ], + "median_us": 30.2173, + "min_us": 26.4344, + "max_us": 48.5391 }, { "family": "chacha20_poly1305", @@ -551,24 +551,24 @@ "operation": "seal of 16 KiB, 256-bit key", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ - 2048, 1024, 1024, 1024, - 2048 + 1024, + 1024 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 48.3821, - 48.7019, - 48.8429, - 48.9285, - 48.8516 - ], - "median_us": 48.8429, - "min_us": 48.3288, - "max_us": 50.0331 + 58.5653, + 62.7726, + 51.2478, + 49.7384, + 49.6995 + ], + "median_us": 51.2478, + "min_us": 49.6035, + "max_us": 66.058 }, { "family": "chacha20_poly1305", @@ -586,15 +586,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 33.1224, - 33.67, - 33.485, - 33.6729, - 33.9442 - ], - "median_us": 33.67, - "min_us": 33.0168, - "max_us": 34.5261 + 35.8136, + 40.4412, + 35.4365, + 38.2327, + 34.4407 + ], + "median_us": 35.8136, + "min_us": 34.4331, + "max_us": 41.95 }, { "family": "chacha20_poly1305_original", @@ -612,15 +612,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 25.3614, - 25.7012, - 25.5611, - 25.629, - 25.6372 - ], - "median_us": 25.629, - "min_us": 25.2448, - "max_us": 26.4018 + 27.1777, + 31.8569, + 28.515, + 26.4807, + 26.4299 + ], + "median_us": 27.1777, + "min_us": 26.4106, + "max_us": 33.0793 }, { "family": "xchacha20_poly1305", @@ -638,15 +638,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 25.2413, - 25.4398, - 25.6218, - 25.625, - 25.6732 - ], - "median_us": 25.6218, - "min_us": 25.2172, - "max_us": 25.7966 + 27.7744, + 28.4214, + 29.1949, + 34.8716, + 26.56 + ], + "median_us": 28.4214, + "min_us": 26.483, + "max_us": 36.927 }, { "family": "gcm", @@ -655,24 +655,24 @@ "operation": "seal of 16 KiB, 256-bit key", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ - 1024, 512, - 1024, 512, - 1024 + 512, + 512, + 512 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 96.2976, - 96.9141, - 97.4487, - 99.5458, - 97.254 - ], - "median_us": 97.254, - "min_us": 96.1885, - "max_us": 99.8963 + 142.5138, + 107.0623, + 103.0632, + 138.6147, + 98.5474 + ], + "median_us": 107.0623, + "min_us": 98.4974, + "max_us": 150.9495 }, { "family": "gcm", @@ -681,24 +681,24 @@ "operation": "seal of 16 KiB, 256-bit key", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ - 1024, - 1024, - 1024, - 1024, + 512, + 512, + 512, + 512, 1024 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 94.8742, - 94.5252, - 94.7161, - 94.5872, - 95.1761 - ], - "median_us": 94.7161, - "min_us": 94.2394, - "max_us": 97.0423 + 123.2382, + 112.0401, + 106.2601, + 112.3486, + 96.4527 + ], + "median_us": 112.0401, + "min_us": 96.4042, + "max_us": 130.9873 }, { "family": "aes_block", @@ -707,24 +707,24 @@ "operation": "encryption of one 16-byte block, 256-bit key", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ + 524288, 1048576, 1048576, - 1048576, - 1048576, + 524288, 1048576 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 0.0729, - 0.0738, - 0.073, - 0.0729, - 0.0726 - ], - "median_us": 0.0729, - "min_us": 0.0722, - "max_us": 0.0742 + 0.1056, + 0.0889, + 0.0762, + 0.0953, + 0.0744 + ], + "median_us": 0.0889, + "min_us": 0.0739, + "max_us": 0.1133 }, { "family": "aes_block", @@ -733,24 +733,24 @@ "operation": "encryption of one 16-byte block, 256-bit key", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ + 131072, + 131072, 262144, - 262144, - 262144, - 262144, + 131072, 262144 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 0.3443, - 0.3487, - 0.3455, - 0.3458, - 0.3502 - ], - "median_us": 0.3458, - "min_us": 0.3438, - "max_us": 0.3565 + 0.421, + 0.3898, + 0.3599, + 0.4705, + 0.3583 + ], + "median_us": 0.3898, + "min_us": 0.3513, + "max_us": 0.507 }, { "family": "secp256k1", @@ -768,15 +768,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 57.054, - 56.5695, - 56.6458, - 56.5872, - 56.6555 - ], - "median_us": 56.6458, - "min_us": 56.2922, - "max_us": 57.3857 + 79.6713, + 66.5381, + 65.8654, + 59.2526, + 57.3269 + ], + "median_us": 65.8654, + "min_us": 57.3121, + "max_us": 86.4301 }, { "family": "ed25519", @@ -785,24 +785,24 @@ "operation": "signature of a 32-byte message", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ + 2048, 4096, - 4096, - 4096, + 2048, 4096, 4096 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 17.5312, - 17.5189, - 17.4542, - 17.5122, - 17.4737 - ], - "median_us": 17.5122, - "min_us": 17.317, - "max_us": 17.8121 + 26.4126, + 21.2944, + 25.5214, + 25.8049, + 18.3788 + ], + "median_us": 25.5214, + "min_us": 17.9491, + "max_us": 29.7626 }, { "family": "x25519", @@ -811,24 +811,24 @@ "operation": "agreement of a secret key and a public key", "compiler": "Clang 23.1.0-wasi-sdk (https://github.com/llvm/llvm-project 895aa2c896ada719451be2e3673c83da8ddf1141)", "repetitions": [ + 1024, 2048, - 2048, - 2048, + 1024, 2048, 2048 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 40.3236, - 40.3895, - 40.349, - 40.7703, - 40.5246 - ], - "median_us": 40.3895, - "min_us": 40.2574, - "max_us": 40.9982 + 60.4835, + 43.9704, + 62.2465, + 56.047, + 42.3789 + ], + "median_us": 56.047, + "min_us": 41.4276, + "max_us": 77.4012 }, { "family": "chacha20_poly1305", @@ -846,15 +846,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 25.3058, - 25.4098, - 25.9224, - 25.6575, - 25.6681 - ], - "median_us": 25.6575, - "min_us": 25.1622, - "max_us": 26.7291 + 29.7965, + 35.5215, + 32.196, + 34.7059, + 26.4432 + ], + "median_us": 32.196, + "min_us": 26.4286, + "max_us": 37.5371 }, { "family": "chacha20_poly1305", @@ -872,15 +872,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 52.03, - 53.2385, - 52.3852, - 52.2324, - 52.4299 - ], - "median_us": 52.3852, - "min_us": 51.8946, - "max_us": 54.0748 + 56.2223, + 66.6398, + 68.1445, + 73.0147, + 55.8983 + ], + "median_us": 66.6398, + "min_us": 54.2463, + "max_us": 79.0842 }, { "family": "chacha20_poly1305", @@ -898,15 +898,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 33.7922, - 33.5039, - 33.4093, - 33.6401, - 34.5593 - ], - "median_us": 33.6401, - "min_us": 33.1465, - "max_us": 35.2685 + 36.4446, + 41.1329, + 42.8451, + 42.5562, + 34.3694 + ], + "median_us": 41.1329, + "min_us": 34.3429, + "max_us": 48.8345 }, { "family": "chacha20_poly1305_original", @@ -924,15 +924,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 25.2406, - 25.3776, - 25.5179, - 25.6873, - 25.8282 - ], - "median_us": 25.5179, - "min_us": 25.1152, - "max_us": 25.9053 + 28.9174, + 26.8683, + 32.0724, + 30.6579, + 26.4273 + ], + "median_us": 28.9174, + "min_us": 26.4181, + "max_us": 35.3021 }, { "family": "xchacha20_poly1305", @@ -950,15 +950,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 26.4602, - 25.4515, - 25.5671, - 25.9878, - 25.8671 - ], - "median_us": 25.8671, - "min_us": 25.374, - "max_us": 26.4882 + 28.2508, + 26.5443, + 36.0236, + 27.5792, + 26.5317 + ], + "median_us": 27.5792, + "min_us": 26.483, + "max_us": 36.5752 }, { "family": "gcm", @@ -967,24 +967,24 @@ "operation": "seal of 16 KiB, 256-bit key", "compiler": "Clang 24.0.0git (https:/github.com/llvm/llvm-project f718ebe873411a562ef6e5c27da36d75b7aaec98)", "repetitions": [ + 512, 1024, - 1024, - 1024, - 1024, + 512, + 512, 1024 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 94.2373, - 96.1922, - 94.5433, - 94.8529, - 94.7276 - ], - "median_us": 94.7276, - "min_us": 94.1955, - "max_us": 98.6753 + 101.8162, + 98.4979, + 138.6268, + 99.7449, + 100.8054 + ], + "median_us": 100.8054, + "min_us": 97.4165, + "max_us": 142.901 }, { "family": "aes_block", @@ -995,22 +995,22 @@ "repetitions": [ 262144, 262144, - 262144, + 131072, 262144, 262144 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 0.2969, - 0.294, - 0.2981, - 0.295, - 0.2971 - ], - "median_us": 0.2969, - "min_us": 0.2916, - "max_us": 0.3064 + 0.3219, + 0.3037, + 0.4414, + 0.3003, + 0.2994 + ], + "median_us": 0.3037, + "min_us": 0.2992, + "max_us": 0.472 }, { "family": "secp256k1", @@ -1019,24 +1019,24 @@ "operation": "BIP340 signature of a 32-byte message", "compiler": "Clang 24.0.0git (https:/github.com/llvm/llvm-project f718ebe873411a562ef6e5c27da36d75b7aaec98)", "repetitions": [ + 1024, 2048, - 2048, - 2048, + 1024, 2048, 2048 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 42.9939, - 43.0167, - 43.3495, - 43.0597, - 43.1487 - ], - "median_us": 43.0597, - "min_us": 42.8472, - "max_us": 43.999 + 65.6552, + 70.716, + 63.8631, + 45.1886, + 45.2857 + ], + "median_us": 63.8631, + "min_us": 44.5174, + "max_us": 71.5211 }, { "family": "ed25519", @@ -1054,15 +1054,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 15.2357, - 15.3326, - 15.2861, - 15.2938, - 15.4284 - ], - "median_us": 15.2938, - "min_us": 15.2181, - "max_us": 15.5338 + 21.3037, + 22.7337, + 20.6338, + 16.1659, + 15.8556 + ], + "median_us": 20.6338, + "min_us": 15.4948, + "max_us": 24.0628 }, { "family": "x25519", @@ -1072,23 +1072,23 @@ "compiler": "Clang 24.0.0git (https:/github.com/llvm/llvm-project f718ebe873411a562ef6e5c27da36d75b7aaec98)", "repetitions": [ 2048, - 2048, - 2048, + 1024, + 1024, 2048, 2048 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 35.5211, - 35.2802, - 35.5026, - 35.3328, - 35.6034 - ], - "median_us": 35.5026, - "min_us": 35.217, - "max_us": 35.8882 + 41.28, + 58.6005, + 50.125, + 36.7375, + 36.5724 + ], + "median_us": 41.28, + "min_us": 35.7279, + "max_us": 66.9067 }, { "family": "chacha20_poly1305", @@ -1097,24 +1097,24 @@ "operation": "seal of 16 KiB, 256-bit key", "compiler": "Clang 24.0.0git (https:/github.com/llvm/llvm-project f718ebe873411a562ef6e5c27da36d75b7aaec98)", "repetitions": [ - 4096, - 4096, - 4096, + 2048, + 2048, + 2048, 4096, 4096 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 21.1534, - 21.876, - 21.2048, - 21.1056, - 21.2286 - ], - "median_us": 21.2048, - "min_us": 20.9255, - "max_us": 22.1851 + 26.6769, + 31.6361, + 25.4823, + 22.6587, + 22.283 + ], + "median_us": 25.4823, + "min_us": 22.1689, + "max_us": 43.6315 }, { "family": "chacha20_poly1305", @@ -1132,15 +1132,15 @@ "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 33.3075, - 33.4356, - 34.0257, - 34.1108, - 33.5164 - ], - "median_us": 33.5164, - "min_us": 33.1617, - "max_us": 34.5254 + 37.0352, + 55.3973, + 41.5324, + 35.2327, + 44.3917 + ], + "median_us": 41.5324, + "min_us": 34.1455, + "max_us": 56.8689 }, { "family": "chacha20_poly1305_original", @@ -1149,24 +1149,24 @@ "operation": "seal of 16 KiB, 256-bit key", "compiler": "Clang 24.0.0git (https:/github.com/llvm/llvm-project f718ebe873411a562ef6e5c27da36d75b7aaec98)", "repetitions": [ + 2048, + 2048, 4096, 4096, - 4096, - 4096, - 4096 + 2048 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 21.0655, - 20.9883, - 21.1394, - 21.1653, - 21.4892 - ], - "median_us": 21.1394, - "min_us": 20.8582, - "max_us": 22.2334 + 23.4612, + 32.601, + 23.1197, + 22.9875, + 27.0586 + ], + "median_us": 23.4612, + "min_us": 22.3345, + "max_us": 34.3943 }, { "family": "xchacha20_poly1305", @@ -1176,23 +1176,23 @@ "compiler": "Clang 24.0.0git (https:/github.com/llvm/llvm-project f718ebe873411a562ef6e5c27da36d75b7aaec98)", "repetitions": [ 4096, + 2048, 4096, 4096, - 4096, - 4096 + 2048 ], "invocations": 5, "runs_per_invocation": 5, "invocation_medians_us": [ - 21.0549, - 21.6713, - 21.7389, - 21.1254, - 21.5713 - ], - "median_us": 21.5713, - "min_us": 21.0205, - "max_us": 22.2928 + 23.9379, + 32.1158, + 22.387, + 22.3196, + 22.3591 + ], + "median_us": 22.387, + "min_us": 22.2395, + "max_us": 32.5425 } ], "amd64_asm": { @@ -1202,5 +1202,40 @@ ], "status": "not measured yet", "reason": "x86-64 assembly runs only on an x86-64 machine; this page's machine is arm64. D7 asks for the rows recorded, never used: webcpp builds libsodium without it." - } + }, + "timing_guard": { + "measurement": "the nanoseconds detail::timing_guard adds to a call, bench/timing.cpp", + "date": "2026-11-20", + "machine": { + "cpu": "Apple M5 Max", + "architecture": "arm64", + "cores": 18, + "memory_gib": 128, + "os": "Darwin 27.0.0", + "wasmtime": "wasmtime 47.0.3 (5554cc1a6 2026-07-31)", + "node": "v26.13.0" + }, + "load_average_before_after": [ + [ + 19.90380859375, + 56.12353515625, + 95.6845703125 + ], + [ + 19.90380859375, + 56.12353515625, + 95.6845703125 + ] + ], + "build": "variant=release", + "target": "native", + "compiler": "Apple LLVM 21.0.0 (clang-2100.4.1.1)", + "iterations": 10000000, + "runs": 5, + "dit": true, + "clear_ns": 40.297, + "set_ns": 0.681, + "provisional": true + }, + "provisional": true } diff --git a/doc/tables/crypto.json b/doc/tables/crypto.json index e9fb413d..65ec1a93 100644 --- a/doc/tables/crypto.json +++ b/doc/tables/crypto.json @@ -742,7 +742,7 @@ {"targets": ["native", "wasip2", "wasip3", "emscripten"], "backend": "sodium", "tests": ["x25519_sodium", "default_backend_none_linked"], "why": "the only backend in this version, on every target; it refuses an all-zero shared secret itself ({rfc7748})"} ], "backends": { - "sodium": {"targets": "all", "lane": {"native": ["memcheck", "msan", "dudect"], "wasip2": ["dudect"]}, "constant_time_checker": {"native": "dudect"}, "constant_time": {"native": "nothing found with arm64's DIT (data-independent timing) bit set, as the lane runs dudect on macOS and Linux on arm64 (elsewhere its harness sets no DIT) and as Apple requires of constant-time code ({apple-dit}); without it, dudect times `agree` with an all-zero secret key against random ones at |t| 6.7 to 14.5, against the threshold 10, on an Apple M5 Max, from a release build (2026-11-19): about one nanosecond in 15 µs. libsodium does not set DIT: a program on Apple silicon sets it around its secret operations ({apple-dit})", "wasip2": "an open finding, its cause measured: dudect times `agree` with an all-zero secret key against random ones at |t| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set ({apple-dit}): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads |t| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next"}} + "sodium": {"targets": "all", "lane": {"native": ["memcheck", "msan", "dudect"], "wasip2": ["dudect"]}, "constant_time_checker": {"native": "dudect"}, "constant_time": {"native": "nothing found with arm64's DIT (data-independent timing) bit set, which webcpp sets around every call on 64-bit ARM under macOS, iOS, Linux and Android where the CPU has it, as Apple requires of constant-time code ({apple-dit}); the lane's dudect sets nothing itself and measures what a program gets: |t| 1.8 to 2.8 against the threshold 10 (2026-11-21, five runs), while its `dudect_planted`, an early-exit scan of the secret key run inside the same guard, reads |t| 66.72 and 65.53 (2026-11-21): DIT does not hide a leak. Without DIT, dudect times `agree` with an all-zero secret key against random ones at |t| 6.7 to 14.5 (2026-11-19) and 4.3 to 7.8 (2026-11-21, three runs): about one nanosecond in 15 µs. Each on an Apple M5 Max, from a release build. libsodium does not set DIT, and webcpp sets nothing on x86, WebAssembly or Windows on ARM64", "wasip2": "an open finding, its cause measured: dudect times `agree` with an all-zero secret key against random ones at |t| 13 to 32 (2026-11-18; a fixed key against another fixed key: 22.9) and 107 to 260 (2026-11-19), and 25.25 in the lane on 2026-11-21, against the threshold 10, under wasmtime 47 on an Apple M5 Max, from a release build. The cause is Apple silicon's DIT (data-independent timing), which wasmtime does not set ({apple-dit}): with DIT set in wasmtime's main thread, an experiment and not the lane, the same case reads |t| 1.5 to 2.1; no branch on a secret was found in the WebAssembly or in wasmtime's compiled code. The lane cannot set DIT from inside the WebAssembly, so it prints this value on wasip2 without judging it; the CI's x86-64 run measures it next"}} } }, { diff --git a/doc/x25519.adoc b/doc/x25519.adoc index b061bd1c..8b8c098f 100644 --- a/doc/x25519.adoc +++ b/doc/x25519.adoc @@ -72,18 +72,17 @@ them against keys it already trusts. secret key: `agree` refuses it, and a program never replaces the refusal with a secret of its own choosing. * On Apple silicon (and other 64-bit ARM CPUs with DIT, data-independent -timing), neither webcpp nor libsodium sets DIT, and without it `agree` took a -time that depends on the secret key: about one nanosecond less for the all-zero -key, enough for dudect to tell it apart (<>). -The lane's dudect sets DIT itself on macOS and Linux on arm64 only, and -measures without it elsewhere. A program sets DIT around its secret -operations, as Apple describes -(link:https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Enable-DIT-for-constant-time-cryptographic-operations[Apple, enable DIT for constant-time cryptographic operations]; -`timingsafe_enable_if_supported` from macOS 15.2). +timing), libsodium's `agree` takes a time that depends on the secret key when +DIT is clear: about one nanosecond less for the all-zero key, enough for dudect +to tell it apart (<>). libsodium does not set +DIT; webcpp sets it around every call on 64-bit ARM under macOS, iOS, Linux and +Android where the CPU has it, and restores the thread's state after it, so a +program does nothing there; on Windows on ARM64 it sets nothing +(<>). * On WebAssembly, timing is an open finding: under wasmtime, on wasip2, dudect times `agree` against its secret key above the lane's threshold, where every -native build passes (with DIT set on macOS and Linux on arm64, as the lane -runs it). The cause is measured: Apple silicon's DIT, which wasmtime does not +native build passes (with the DIT webcpp sets on 64-bit ARM). The cause is +measured: Apple silicon's DIT, which wasmtime does not set; with DIT set in wasmtime's thread, as an experiment, the case passes. The measurement, with its numbers, is in <> and in the curves' comparison table (<>); a program that agrees diff --git a/include/webcpp/crypto/backends/openssl.hpp b/include/webcpp/crypto/backends/openssl.hpp index e6f8778a..973385dd 100644 --- a/include/webcpp/crypto/backends/openssl.hpp +++ b/include/webcpp/crypto/backends/openssl.hpp @@ -88,6 +88,11 @@ namespace webcpp::crypto { * On Windows a thread that used OpenSSL keeps its blocks in the resource until the process ends * unless it calls `OPENSSL_thread_stop()` before it ends. * + * A resource that cannot allocate fails as the standard has `do_allocate` fail, by throwing, + * never by returning null: libc++ and libstdc++ declare `allocate` never to return null, so an + * optimiser removes any check of its result. With exceptions the adapter gives OpenSSL null for + * what it throws; without them such a resource ends the process, as `std::pmr`'s own do. + * * @return The program's resource. * @see "OpenSSL's allocations in your own memory resource", in the guide. */ @@ -117,7 +122,8 @@ extern "C" { * that holds its size. * * @param size The bytes OpenSSL asks for; 0, or a size whose header overflows, gives null. - * @return The block, or null, which OpenSSL takes as a failure. + * @return The block, or null, which OpenSSL takes as a failure: for those sizes, and with + * exceptions for a resource that throws. */ inline void* webcpp_crypto_openssl_malloc(std::size_t size, char const* /*file*/, int /*line*/) noexcept { @@ -129,11 +135,9 @@ inline void* webcpp_crypto_openssl_malloc(std::size_t size, char const* /*file*/ #ifndef WEBCPP_CRYPTO_NO_EXCEPTIONS try { #endif + // Never null: a resource fails by throwing (openssl_memory_resource()). void* const block = webcpp::crypto::openssl_memory_resource().allocate(header + size, header); - if (block == nullptr) { - return nullptr; - } std::memcpy(block, &size, sizeof size); return static_cast(block) + header; #ifndef WEBCPP_CRYPTO_NO_EXCEPTIONS @@ -533,10 +537,13 @@ struct gcm_engine { target_state.context = std::exchange(source_state.context, nullptr); } - // EVP_CIPHER_CTX_free cleanses the expanded key before it frees. + // EVP_CIPHER_CTX_free cleanses the expanded key before it frees. The pointer is then + // zeroed by wipe_object: a plain store of null is dead at the end of an object's life, and + // any optimiser may drop it (GCC 15 at -O3 does). /** Frees the context. */ static void reset(state& engine_state) noexcept { - EVP_CIPHER_CTX_free(std::exchange(engine_state.context, nullptr)); + EVP_CIPHER_CTX_free(engine_state.context); + wipe_object(engine_state); } }; @@ -645,10 +652,12 @@ struct aead_engine { target_state.context = std::exchange(source_state.context, nullptr); } - // EVP_CIPHER_CTX_free cleanses the key before it frees. + // EVP_CIPHER_CTX_free cleanses the key before it frees. The pointer is then zeroed by + // wipe_object, as GCM's is. /** Frees the context. */ static void reset(state& engine_state) noexcept { - EVP_CIPHER_CTX_free(std::exchange(engine_state.context, nullptr)); + EVP_CIPHER_CTX_free(engine_state.context); + wipe_object(engine_state); } }; @@ -707,10 +716,11 @@ struct block_engine { target_state.decrypting = std::exchange(source_state.decrypting, nullptr); } - /** Frees both contexts. */ + /** Frees both contexts, then zeroes the pointers by wipe_object, as GCM's are. */ static void reset(state& engine_state) noexcept { - EVP_CIPHER_CTX_free(std::exchange(engine_state.encrypting, nullptr)); - EVP_CIPHER_CTX_free(std::exchange(engine_state.decrypting, nullptr)); + EVP_CIPHER_CTX_free(engine_state.encrypting); + EVP_CIPHER_CTX_free(engine_state.decrypting); + wipe_object(engine_state); } }; diff --git a/include/webcpp/crypto/ciphers/aead.hpp b/include/webcpp/crypto/ciphers/aead.hpp index 942f7fe9..b1293f08 100644 --- a/include/webcpp/crypto/ciphers/aead.hpp +++ b/include/webcpp/crypto/ciphers/aead.hpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -314,6 +315,7 @@ class aead { */ [[nodiscard]] static boost::system::result make( std::span key_bytes) noexcept { + detail::timing_guard const timing; if (key_bytes.size() != key_size) { return error::invalid_key_size; } @@ -389,6 +391,7 @@ class aead { [[nodiscard]] boost::system::result seal( std::span iv_bytes, std::span additional_data_bytes, std::span data_bytes) noexcept { + detail::timing_guard const timing; auto const guard_result = guard_message(iv_bytes, additional_data_bytes, data_bytes.size(), tag_size); if (!guard_result) { @@ -423,6 +426,7 @@ class aead { [[nodiscard]] boost::system::result open( std::span iv_bytes, std::span additional_data_bytes, std::span data_bytes, std::span tag_bytes) noexcept { + detail::timing_guard const timing; auto const guard_result = guard_message(iv_bytes, additional_data_bytes, data_bytes.size(), tag_bytes.size()); if (!guard_result) { diff --git a/include/webcpp/crypto/ciphers/aes.hpp b/include/webcpp/crypto/ciphers/aes.hpp index 7553e99f..9d6b8ddb 100644 --- a/include/webcpp/crypto/ciphers/aes.hpp +++ b/include/webcpp/crypto/ciphers/aes.hpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -150,6 +151,7 @@ class basic_aes { */ [[nodiscard]] boost::system::result encrypt( std::span block_bytes) noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -168,6 +170,7 @@ class basic_aes { */ [[nodiscard]] boost::system::result decrypt( std::span block_bytes) noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -229,6 +232,7 @@ class aes128 : public detail::basic_aes<16, Backend> { */ [[nodiscard]] static boost::system::result make( std::span key_bytes) noexcept { + detail::timing_guard const timing; return detail::basic_aes<16, Backend>::template make_as(key_bytes); } @@ -254,6 +258,7 @@ class aes192 : public detail::basic_aes<24, Backend> { */ [[nodiscard]] static boost::system::result make( std::span key_bytes) noexcept { + detail::timing_guard const timing; return detail::basic_aes<24, Backend>::template make_as(key_bytes); } @@ -279,6 +284,7 @@ class aes256 : public detail::basic_aes<32, Backend> { */ [[nodiscard]] static boost::system::result make( std::span key_bytes) noexcept { + detail::timing_guard const timing; return detail::basic_aes<32, Backend>::template make_as(key_bytes); } diff --git a/include/webcpp/crypto/ciphers/gcm.hpp b/include/webcpp/crypto/ciphers/gcm.hpp index d7b613e8..be223699 100644 --- a/include/webcpp/crypto/ciphers/gcm.hpp +++ b/include/webcpp/crypto/ciphers/gcm.hpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -151,6 +152,7 @@ class gcm { */ [[nodiscard]] static boost::system::result make( std::span key_bytes) noexcept { + detail::timing_guard const timing; if (key_bytes.size() != key_size) { return error::invalid_key_size; } @@ -172,6 +174,7 @@ class gcm { * @param moved_source The source, empty afterwards. */ gcm(gcm&& moved_source) noexcept : keyed_(std::exchange(moved_source.keyed_, false)) { + detail::timing_guard const timing; if (keyed_) { moved_source.abandon_message(); engine::move(state_, moved_source.state_); @@ -187,6 +190,7 @@ class gcm { * @return This object. */ gcm& operator=(gcm&& moved_source) noexcept { + detail::timing_guard const timing; if (this != &moved_source) { engine::reset(state_); keyed_ = std::exchange(moved_source.keyed_, false); @@ -223,6 +227,7 @@ class gcm { [[nodiscard]] boost::system::result seal( std::span iv_bytes, std::span additional_data_bytes, std::span data_bytes) noexcept { + detail::timing_guard const timing; auto const guard_result = guard_message(iv_bytes, additional_data_bytes, data_bytes.size(), tag_size); if (!guard_result) { @@ -258,6 +263,7 @@ class gcm { [[nodiscard]] boost::system::result open( std::span iv_bytes, std::span additional_data_bytes, std::span data_bytes, std::span tag_bytes) noexcept { + detail::timing_guard const timing; auto const guard_result = guard_message(iv_bytes, additional_data_bytes, data_bytes.size(), tag_bytes.size()); if (!guard_result) { @@ -293,6 +299,7 @@ class gcm { [[nodiscard]] boost::system::result begin_seal( std::span iv_bytes, std::span additional_data_bytes) noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -322,6 +329,7 @@ class gcm { * pass `max_message_size`, which ends the message; `backend_failure`, which ends it. */ [[nodiscard]] boost::system::result update(std::span data_bytes) noexcept { + detail::timing_guard const timing; if (!keyed_ || !in_message_) { return error::invalid_state; } @@ -345,6 +353,7 @@ class gcm { * too. */ [[nodiscard]] boost::system::result result() noexcept { + detail::timing_guard const timing; if (!keyed_ || !in_message_) { return error::invalid_state; } diff --git a/include/webcpp/crypto/curves/ed25519.hpp b/include/webcpp/crypto/curves/ed25519.hpp index 2517ebc2..4597e49a 100644 --- a/include/webcpp/crypto/curves/ed25519.hpp +++ b/include/webcpp/crypto/curves/ed25519.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include // lint-world: random_device draws generate()'s seed #include @@ -110,6 +111,7 @@ class basic_keypair { */ [[nodiscard]] static boost::system::result make( std::span seed) noexcept { + detail::timing_guard const timing; if (seed.size() != seed_size) { return error::invalid_key_size; } @@ -129,6 +131,7 @@ class basic_keypair { * none; `backend_failure`. */ [[nodiscard]] static boost::system::result generate() noexcept { + detail::timing_guard const timing; secret seed; auto const drawn = random_device().fill(seed.writable()); // lint-world: the seed if (!drawn) { @@ -179,6 +182,7 @@ class basic_keypair { * @return 32 bytes; `invalid_state` moved from. */ [[nodiscard]] boost::system::result> public_key() const noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -195,6 +199,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> sign( std::span message) const noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -213,6 +218,7 @@ class basic_keypair { * @return 32 secret bytes; `invalid_state`; `backend_failure`. */ [[nodiscard]] boost::system::result> x25519_secret() const noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -254,6 +260,7 @@ template verify(std::span public_key, std::span message, std::span signature) noexcept { + detail::timing_guard const timing; static_assert(std::is_same_v, "webcpp::crypto: Ed25519's only verification policy is curves::ed25519::strict."); if (public_key.size() != 32) { @@ -279,6 +286,7 @@ template > [[nodiscard]] boost::system::result> to_x25519_public( std::span ed25519_public_key) noexcept { + detail::timing_guard const timing; if (ed25519_public_key.size() != 32) { return error::invalid_public_key; } @@ -332,6 +340,7 @@ class ph { * @return The signer, ready for the message's pieces; `backend_failure`. */ [[nodiscard]] static boost::system::result begin() noexcept { + detail::timing_guard const timing; ph begun; auto const started = engine::begin(begun.state_); if (!started) { @@ -382,6 +391,7 @@ class ph { * @return Nothing; `invalid_state` once the message is signed or verified, or moved from. */ [[nodiscard]] boost::system::result update(std::span piece) noexcept { + detail::timing_guard const timing; if (!begun_) { return error::invalid_state; } @@ -398,6 +408,7 @@ class ph { */ [[nodiscard]] boost::system::result> sign( basic_keypair const& key) noexcept { + detail::timing_guard const timing; if (!begun_ || !key.keyed_) { return error::invalid_state; } @@ -421,6 +432,7 @@ class ph { [[nodiscard]] boost::system::result verify( std::span public_key, std::span signature) noexcept { + detail::timing_guard const timing; if (!begun_) { return error::invalid_state; } diff --git a/include/webcpp/crypto/curves/ed25519/formats.hpp b/include/webcpp/crypto/curves/ed25519/formats.hpp index de46b540..b1a6175b 100644 --- a/include/webcpp/crypto/curves/ed25519/formats.hpp +++ b/include/webcpp/crypto/curves/ed25519/formats.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -131,6 +132,7 @@ template > template > [[nodiscard]] boost::system::result> decode_spki( std::span spki) noexcept { + detail::timing_guard const timing; if (spki.size() != max_spki_size) { return error::invalid_encoding; } @@ -181,6 +183,7 @@ template > template > [[nodiscard]] boost::system::result> decode_pkcs8( std::span pkcs8) noexcept { + detail::timing_guard const timing; if (pkcs8.size() != max_pkcs8_size) { return error::invalid_encoding; } diff --git a/include/webcpp/crypto/curves/secp256k1.hpp b/include/webcpp/crypto/curves/secp256k1.hpp index d9ba1c7e..a792b333 100644 --- a/include/webcpp/crypto/curves/secp256k1.hpp +++ b/include/webcpp/crypto/curves/secp256k1.hpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include // lint-world: the context's randomisation, its purpose @@ -485,6 +486,7 @@ class public_key { */ [[nodiscard]] friend bool operator==(public_key const& left_key, public_key const& right_key) noexcept { + detail::timing_guard const timing; return compare(left_key, right_key) == 0; } @@ -497,6 +499,7 @@ class public_key { */ [[nodiscard]] friend std::strong_ordering operator<=>(public_key const& left_key, public_key const& right_key) noexcept { + detail::timing_guard const timing; return compare(left_key, right_key) <=> 0; } @@ -565,6 +568,7 @@ class basic_keypair { [[nodiscard]] static boost::system::result make( std::span secret_bytes, std::pmr::memory_resource* memory_resource = std::pmr::get_default_resource()) { + detail::timing_guard const timing; if (secret_bytes.size() != 32) { return error::invalid_key_size; } @@ -641,6 +645,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> xonly_public_key() const noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -658,6 +663,7 @@ class basic_keypair { * @return 33 bytes; `invalid_state` moved from. */ [[nodiscard]] boost::system::result> public_key() const noexcept { + detail::timing_guard const timing; return encoded_public_key<33>(webcpp::crypto::detail::secp256k1_compressed); } @@ -668,6 +674,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> public_key_uncompressed() const noexcept { + detail::timing_guard const timing; return encoded_public_key<65>(webcpp::crypto::detail::secp256k1_uncompressed); } @@ -681,6 +688,7 @@ class basic_keypair { [[nodiscard]] boost::system::result> sign_schnorr( std::span message_bytes, std::span aux_random_bytes) const noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -706,6 +714,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> sign_ecdsa( std::span message_hash) const noexcept { + detail::timing_guard const timing; return signed_ecdsa(message_hash, nullptr); } @@ -721,6 +730,7 @@ class basic_keypair { [[nodiscard]] boost::system::result> sign_ecdsa( std::span message_hash, std::span extra_entropy) const noexcept { + detail::timing_guard const timing; webcpp::crypto::detail::secp256k1_secret nonce_data; std::ranges::copy(extra_entropy, nonce_data.bytes.begin()); return signed_ecdsa(message_hash, nonce_data.bytes.data()); @@ -735,6 +745,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> sign_recoverable(std::span message_hash) const noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -764,6 +775,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> ecdh( std::span public_key_bytes) const noexcept { + detail::timing_guard const timing; return agree<32>(public_key_bytes, nullptr); } @@ -779,6 +791,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> ecdh_x( std::span public_key_bytes) const noexcept { + detail::timing_guard const timing; return agree<32>(public_key_bytes, webcpp::crypto::detail::webcpp_crypto_detail_secp256k1_copy_x); } @@ -791,6 +804,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result> ecdh_point( std::span public_key_bytes) const noexcept { + detail::timing_guard const timing; return agree<65>(public_key_bytes, webcpp::crypto::detail::webcpp_crypto_detail_secp256k1_copy_point); } @@ -811,6 +825,7 @@ class basic_keypair { */ [[nodiscard]] boost::system::result tweak_xonly( std::span tweak_bytes) noexcept { + detail::timing_guard const timing; if (!keyed_) { return error::invalid_state; } @@ -984,6 +999,7 @@ namespace webcpp::crypto::curves::secp256k1 { template > [[nodiscard]] boost::system::result parse_public_key( std::span encoded_key) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); @@ -1008,6 +1024,7 @@ template > [[nodiscard]] std::array> serialize_public_key( public_key const& parsed_key) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); @@ -1038,6 +1055,7 @@ template > [[nodiscard]] boost::system::result recover( std::span recoverable_signature, std::span message_hash) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); // libsecp256k1 checks the id with ARG_CHECK, whose callback aborts: refused first here. @@ -1073,6 +1091,7 @@ template > [[nodiscard]] boost::system::result verify_schnorr( std::span signature_bytes, std::span message_bytes, std::span xonly_key_bytes) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); @@ -1103,6 +1122,7 @@ template > std::span signature_bytes, std::span message_hash, std::span public_key_bytes) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); @@ -1135,6 +1155,7 @@ template > [[nodiscard]] boost::system::result to_der( std::span compact_signature, std::span output_bytes) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); if (output_bytes.size() < max_der_size) { return error::invalid_input_size; @@ -1166,6 +1187,7 @@ template > template > [[nodiscard]] boost::system::result> from_der( std::span der_signature) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); if (der_signature.empty()) { return error::invalid_signature; @@ -1197,6 +1219,7 @@ template > template > [[nodiscard]] boost::system::result> normalize_ecdsa( std::span compact_signature) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); @@ -1228,6 +1251,7 @@ template > [[nodiscard]] boost::system::result tweak_xonly( std::span xonly_key_bytes, std::span tweak_bytes) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); diff --git a/include/webcpp/crypto/curves/secp256k1/ellswift.hpp b/include/webcpp/crypto/curves/secp256k1/ellswift.hpp index 284a1c2d..72cd1c52 100644 --- a/include/webcpp/crypto/curves/secp256k1/ellswift.hpp +++ b/include/webcpp/crypto/curves/secp256k1/ellswift.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -73,6 +74,7 @@ enum class ellswift_party { template > [[nodiscard]] std::array ellswift_encode( public_key const& parsed_key, std::span randomness) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -95,6 +97,7 @@ template > template > [[nodiscard]] public_key ellswift_decode( std::span encoded_key) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -120,6 +123,7 @@ template > template [[nodiscard]] boost::system::result> ellswift_create( basic_keypair const& key_pair, std::span randomness) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -161,6 +165,7 @@ template std::span initiator_encoding, std::span responder_encoding, ellswift_party our_party) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); diff --git a/include/webcpp/crypto/curves/secp256k1/formats.hpp b/include/webcpp/crypto/curves/secp256k1/formats.hpp index e3587029..dd2609f0 100644 --- a/include/webcpp/crypto/curves/secp256k1/formats.hpp +++ b/include/webcpp/crypto/curves/secp256k1/formats.hpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -97,6 +98,7 @@ template > [[nodiscard]] boost::system::result encode_spki( public_key const& parsed_key, std::span output_bytes, point_form encoded_form = point_form::compressed) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); if (output_bytes.size() < max_spki_size) { @@ -131,6 +133,7 @@ template > template > [[nodiscard]] boost::system::result decode_spki( std::span spki_bytes) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); @@ -168,6 +171,7 @@ template > template > [[nodiscard]] boost::system::result encode_pkcs8( basic_keypair const& key_pair, std::span output_bytes) noexcept { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); using access = webcpp::crypto::detail::secp256k1_access; @@ -210,6 +214,7 @@ template > [[nodiscard]] boost::system::result> decode_pkcs8( std::span pkcs8_bytes, std::pmr::memory_resource* memory_resource = std::pmr::get_default_resource()) { + detail::timing_guard const timing; static_assert(webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); diff --git a/include/webcpp/crypto/curves/secp256k1/musig.hpp b/include/webcpp/crypto/curves/secp256k1/musig.hpp index e66e27bd..0778a9cb 100644 --- a/include/webcpp/crypto/curves/secp256k1/musig.hpp +++ b/include/webcpp/crypto/curves/secp256k1/musig.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include // lint-world: the nonce's session randomness @@ -85,6 +86,7 @@ class aggregate_key { * @return 32 bytes, the tweaks included. */ [[nodiscard]] std::array xonly_public_key() const noexcept { + detail::timing_guard const timing; webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); public_key full_key = full_public_key(); @@ -105,6 +107,7 @@ class aggregate_key { * @return The key, the tweaks included. */ [[nodiscard]] public_key full_public_key() const noexcept { + detail::timing_guard const timing; webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); secp256k1_selftest(); using access = webcpp::crypto::detail::secp256k1_access; @@ -127,6 +130,7 @@ class aggregate_key { */ [[nodiscard]] boost::system::result tweak_add( std::span tweak_bytes) noexcept { + detail::timing_guard const timing; return tweaked(tweak_bytes, secp256k1_musig_pubkey_ec_tweak_add); } @@ -140,6 +144,7 @@ class aggregate_key { */ [[nodiscard]] boost::system::result tweak_xonly_add( std::span tweak_bytes) noexcept { + detail::timing_guard const timing; return tweaked(tweak_bytes, secp256k1_musig_pubkey_xonly_tweak_add); } @@ -310,6 +315,7 @@ struct nonce_pair { template > [[nodiscard]] boost::system::result key_aggregate( std::span signer_keys, workspace pointer_space) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -358,6 +364,7 @@ template basic_keypair const& signer, aggregate_key const& signers_key, std::span message_hash = {}, std::span extra_input = {}) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -412,6 +419,7 @@ template template > [[nodiscard]] boost::system::result nonce_aggregate( std::span signer_nonces, workspace pointer_space) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -452,6 +460,7 @@ template > [[nodiscard]] boost::system::result nonce_process( aggregate_nonce const& nonce_sum, std::span message_bytes, aggregate_key const& signers_key) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -482,6 +491,7 @@ template [[nodiscard]] boost::system::result partial_sign( basic_keypair const& signer, secret_nonce signer_nonce, aggregate_key const& signers_key, session const& signing_session) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -529,6 +539,7 @@ template > partial_signature const& signature_part, public_nonce const& signer_nonce, public_key const& signer_key, aggregate_key const& signers_key, session const& signing_session) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -559,6 +570,7 @@ template > [[nodiscard]] boost::system::result> partial_signature_aggregate( session const& signing_session, std::span signature_parts, workspace pointer_space) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -598,6 +610,7 @@ template > template > [[nodiscard]] boost::system::result parse_public_nonce( std::span encoded_nonce) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -624,6 +637,7 @@ template > template > [[nodiscard]] std::array serialize_public_nonce( public_nonce const& parsed_nonce) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -648,6 +662,7 @@ template > template > [[nodiscard]] boost::system::result parse_aggregate_nonce( std::span encoded_nonce) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -674,6 +689,7 @@ template > template > [[nodiscard]] std::array serialize_aggregate_nonce( aggregate_nonce const& parsed_nonce) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -697,6 +713,7 @@ template > template > [[nodiscard]] boost::system::result parse_partial_signature( std::span encoded_signature) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); @@ -724,6 +741,7 @@ template > template > [[nodiscard]] std::array serialize_partial_signature( partial_signature const& parsed_signature) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); webcpp::crypto::detail::read_canary(WEBCPP_CRYPTO_DETAIL_SECP256K1_CANARY); diff --git a/include/webcpp/crypto/curves/secp256k1/silent_payments.hpp b/include/webcpp/crypto/curves/secp256k1/silent_payments.hpp index 87269d68..d9c5a560 100644 --- a/include/webcpp/crypto/curves/secp256k1/silent_payments.hpp +++ b/include/webcpp/crypto/curves/secp256k1/silent_payments.hpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -299,6 +300,7 @@ template std::span> const* const> taproot_inputs, std::span> const* const> plain_inputs, std::span> output_keys, workspace pointer_space) noexcept { + detail::timing_guard const timing; static_assert( webcpp::crypto::detail::secp256k1_refuses_others); @@ -390,6 +392,7 @@ template template [[nodiscard]] boost::system::result