From 446cddd0399c9eda7bf3b3b97daf5790ae09ca84 Mon Sep 17 00:00:00 2001 From: R Date: Thu, 3 Dec 2026 09:15:26 -0300 Subject: [PATCH] codecs: dudect's bech32 cases prepare their texts by the constant-time conversion bech32_stream_decode read |t| 104 and 132 in two of three runs on an AMD EPYC 9V74 (codecs#2's run) and passed elsewhere. A probe on seven 9V74 runners, eight fresh processes each, measured the program as the lane builds it in five variants. As it is, 23 of 56 processes read over 10 (up to 122). With the stream decoder changed to write its data apart from its hold, 54 of 56. Without its wipe, 32 of 56. Without address randomisation, 50 of 56. With the class's text encoded by the constant-time conversion of a secure::secret<32>, as base58's cases already are, 0 of 56 (1 to 4). So the decode is not where the classes differ: its x86-64 code (Clang 18, -O3) has no branch or table read on the text, and the A/A control was clean. What differs is what the preparation leaves behind. The public conversion of the public bytes runs the same instructions for both classes but leaves their values, unwiped, in memory the measured operation reuses. The secret conversion wipes them. Every bech32 case shares the one preparation, which now goes through the secret conversion. The library is unchanged. --- test/constant_time/dudect_test.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/test/constant_time/dudect_test.cpp b/test/constant_time/dudect_test.cpp index 12d57d0..ce5d702 100644 --- a/test/constant_time/dudect_test.cpp +++ b/test/constant_time/dudect_test.cpp @@ -23,7 +23,8 @@ // 1 also at the third to eighth places of the 7 groups before it, drawn at random), the verdict // invalid_padding in both; bech32's decode of the 61 characters // of 32 bytes under "bc" into a secure::secret<32> (class 0 the text of zeros, class 1 the text of -// random bytes, both valid, encoded unmeasured), the same text through the secret stream decoder, +// random bytes, both valid, encoded unmeasured by the constant-time conversion of a +// secure::secret<32>), the same text through the secret stream decoder, // bech32's encode of a secure::secret<32> (class 0 zeros, class 1 random), and the decode of an // invalid text whose one character outside the alphabet lies where a class puts it (class 0 the // first of the data, class 1 anywhere in it), invalid_character in both; base58's secret decode of @@ -626,11 +627,19 @@ int main(int argc, char** argv) { measure_base32_cases(results, *selection); // bech32 decode: the text of 32 zero bytes under "bc", against the text of random bytes, each - // encoded unmeasured: the same length, both valid. + // encoded unmeasured: the same length, both valid. Each text is encoded by the constant-time + // conversion of a secure::secret<32>, as base58's are. With the public conversion of the + // public bytes, bech32_stream_decode read |t| over 10 on AMD EPYC 9V74 runners in 23 of 56 + // processes (up to 122; seven runners, eight processes each, 2026-12-03), and at least as often + // with the stream decoder changed to write its data apart from its hold (54 of 56) or not to + // wipe it (32 of 56): the decode is not where the classes differ. With this preparation it read + // 1 to 4 in all 56. codecs#2's run had read 132 and 104 there. auto const bech32_part = formats::bech32("bc"); std::array bech32_text{}; + secure::secret<32> bech32_key; auto const encode_bech32_text = [&](std::array const& bytes) { - static_cast(codecs::encode(bech32_part, bytes, std::span{bech32_text})); + std::ranges::copy(bytes, bech32_key.writable().begin()); + static_cast(codecs::encode(bech32_part, bech32_key, std::span{bech32_text})); }; measure_with_control( results, *selection, "bech32_decode",