diff --git a/CODING-GUIDELINES.md b/CODING-GUIDELINES.md index 83b9a258..58a299e4 100644 --- a/CODING-GUIDELINES.md +++ b/CODING-GUIDELINES.md @@ -7151,18 +7151,20 @@ and not built is in chapter 17. creates libsecp256k1's context in it with `secp256k1_context_preallocated_create`, aligned to `alignof(std::max_align_t)`, and randomizes it from `random_device`; - `verify_schnorr` and `verify_ecdsa` use `secp256k1_context_static` after - `secp256k1_selftest()` on every call and allocate nothing.** The resource + `crypto::verify` with `crypto::schnorr` and `crypto::ecdsa_sha256` uses + `secp256k1_context_static` after `secp256k1_selftest()` on every call and + allocates nothing.** The resource serves the context alone, so that the counting test sees every allocation; a run-once flag would be the static mutable state the rulebook forbids. Source: `include/webcpp/crypto/curves/secp256k1.hpp` (libs/crypto). -- **`ecdh` takes the secret into a local array, wipes it on every path, and - returns the SHA-256 of the compressed shared point.** libsecp256k1's - default hash. +- **`agree_hashed` takes the secret into a local array, wipes it on every + path, and returns the SHA-256 of the compressed shared point.** + libsecp256k1's default hash; `agree` gives the raw x and `agree_point` the + point. Source: `libraries/crypto/openspec/changes/genesis/reference/spec.md` section 2.8 (proposals). - **ECDSA's DER is strict (`to_der`, `from_der`, at most 72 bytes) and - `normalize_ecdsa` gives the low-S form; the x-only tweak takes any 32-byte + `normalize_low_s` gives the low-S form; the x-only tweak takes any 32-byte tweak, as a free `tweak_xonly` on an x-only key, giving the key and its parity, and as `keypair::tweak_xonly` in place, and a tweak not below the group order is `invalid_secret_key` and wipes the key pair.** A wallet's @@ -7197,32 +7199,48 @@ and not built is in chapter 17. WebCrypto write, checked by the oracles; anything else is `invalid_encoding`. Source: `include/webcpp/crypto/curves/p256/formats.hpp` (libs/crypto). -- **Low S is two primitives, not a mode: `sign_ecdsa` returns S as computed - and `verify_ecdsa` accepts both forms, as FIPS 186-5 and WebCrypto do; - `normalize_ecdsa` gives the low form and `check_low_s` refuses the high - one, neither calling a backend.** A protocol that requires low S - (Trystero's handshakes) applies them; the protocol itself is - webcpp::trystero's (D24), which the guide's section on signed handshakes +- **ECDSA signs low S on both curves; P-256's verification accepts both + forms, as FIPS 186-5 and WebCrypto do, and secp256k1's refuses a high S.** + P-256's `sign` normalizes RFC 6979's S after signing, selecting n - s by a + mask without a branch; `normalize_low_s` gives the low form of a signature + made elsewhere and `is_low_s` answers whether one is low, neither calling a + backend. A protocol that requires low S of its peers (Trystero's + handshakes) checks `is_low_s` before `crypto::verify`; the protocol itself + is webcpp::trystero's (D24), which the guide's section on signed handshakes shows with an example. Their verdict is marked public for the constant-time checkers, a signature being public once made. Source: `include/webcpp/crypto/curves/p256.hpp`, `doc/p2p.adoc` - (libs/crypto). + (libs/crypto); `libraries/crypto/openspec/changes/unified-sign/design.md` + D3 and D4b (proposals). +- **One name per signing operation, the scheme always named: `key.sign(scheme, + input)` and `crypto::verify(scheme, signature, input, public_key)`, and + their `async_` forms, on Ed25519, P-256 and secp256k1; a digest only as + `crypto::prehashed{digest}`; every key pair a `crypto::keypair`, with a + typed public key, `secret()` and `agree`.** A span is a message whatever + its size, so a 32-byte message is never read as a digest; a pair of scheme + and input that is not valid stops the build with its reason. `Backend` is + the first template parameter of every `crypto::verify`. + Source: `include/webcpp/crypto/signing.hpp`, `keypair.hpp` (libs/crypto); + `libraries/crypto/openspec/changes/unified-sign/design.md` (proposals). - **Constant time, sizes and throughput cover the family.** memcheck and MemorySanitizer run the key pair's making, signing with its low-S - normalization and check, `ecdh_x` and `decode_pkcs8` (`paths.hpp`), each + normalization and check, `agree` and `decode_pkcs8` (`paths.hpp`), each checker failing on a planted early exit in a copy of the scalar check; BearSSL's own branches on its signing's verdicts are listed for memcheck in - `bearssl_verdicts.supp`. dudect times `sign_ecdsa` and `ecdh_x` against the + `bearssl_verdicts.supp`. dudect times `sign` and `agree` against the scalar natively and on wasip2. The sizes lane measures signing, - verification and `ecdh_x`, and the throughput page the same three, from - release builds. The checkers ran on 2026-11-21 in an arm64 Linux container - (`linux-lint:arm64`, clang 18, Valgrind 3.22), the lane's CI command: both - found upstream BearSSL's signing branching on the RFC 6979 nonce's bit - length, `br_ecdsa_i31_bits2int` decoding it with its actual length and - `br_i31_rshift` looping by it (a Minerva-class leak), and nothing once - webcpp's BearSSL carried the fix, one commit setting the order's length + verification and `agree`, and the throughput page the same three, from + release builds. The checkers ran on 2026-11-21 and again on 2026-11-22 (the + rerun with the signing's low-S step found nothing under either) in an arm64 + Linux container (`linux-lint:arm64`, clang 18, Valgrind 3.22), the lane's CI + command: both found upstream BearSSL's signing branching on the RFC 6979 + nonce's bit length, `br_ecdsa_i31_bits2int` decoding it with its actual + length and `br_i31_rshift` looping by it (a Minerva-class leak), and nothing + once webcpp's BearSSL carried the fix, one commit setting the order's length before the shift (chapter 5 rule 1); the planted programs failed and dudect - passed there too. + passed there too: dudect judged P-256's signing with its low-S step (|t| + 1.70 on Linux, 1.63 natively on an Apple M5 Max, release, one run each; + provisional, measured again in the final quiet pass). Source: `libs/crypto/test/constant_time/Jamfile`, `test/sizes/Jamfile`, `bench/throughput.cpp`, `doc/backends.adoc` (libs/crypto); https://github.com/webcpporg/bearssl/commit/32381d6e787a32cbb7a687246349200d78b8ff7c @@ -7346,7 +7364,7 @@ and not built is in chapter 17. webcpp never calls `RAND_bytes`.** No backend chooses its own entropy. Source: `third_party/Jamfile` (webcpp). - **webcpp's code allocates only `keypair::make`'s context, in the resource - the user gives; no seal, open, `encrypt`, `fill`, signature, `ecdh` or + the user gives; no seal, open, `encrypt`, `fill`, signature, `agree` or verification allocates on any backend; OpenSSL's `make` allocates its context through OpenSSL's own allocator, which its page states and `allocation_openssl` counts, unless the program takes OpenSSL's memory @@ -8771,9 +8789,10 @@ increments 1 and 2, whose state is first. - **Built on the branch `crypto/secp256k1-modules` of the library and the superproject, on increment 1's merged trees, awaiting the owner's merge: - libsecp256k1's remaining modules.** ECDH in three forms (`ecdh` returning a - `secret<32>`, `ecdh_x`, `ecdh_point`), recoverable signatures - (`sign_recoverable` of the key pair and `recover`), the tweaks, negation and + libsecp256k1's remaining modules.** ECDH in three forms, each a `secret` + (`agree_hashed`, `agree`, `agree_point` since unified-sign), recoverable + signatures (`sign(crypto::ecdsa_sha256_recoverable, input)` and `recover`), + the tweaks, negation and combination of whole keys (`curves/secp256k1/tweak.hpp`), keys in SPKI, PKCS#8 and PEM (`formats.hpp`), BIP 324's ElligatorSwift (`ellswift.hpp`), BIP 327's MuSig2 (`musig.hpp`) and BIP 352's silent payments @@ -8805,9 +8824,9 @@ increments 1 and 2, whose state is first. `xdh`, MuSig2's `nonce_gen` and `partial_sign`, the silent payments' `create_outputs`, `create_label` and `scan_outputs`, each workspace's wipe included), written once in `test/constant_time/paths.hpp`; dudect times - `ecdh_x` natively and on wasip2, in a program of its own, so that + `agree` natively and on wasip2, in a program of its own, so that libsecp256k1 stays off BearSSL's. The Linux x86-64 run of the lane is the - CI's. The sizes lane measures `ecdh_x`, `recover`, ElligatorSwift's + CI's. The sizes lane measures `agree`, `recover`, ElligatorSwift's exchange, a two-signer MuSig2 signature and a silent payments output, all from release builds through `webcpp.program-sizes`. Source: `libs/crypto/test/constant_time/Jamfile` and `test/sizes/Jamfile`. diff --git a/libs/crypto b/libs/crypto index 185c1e04..02552068 160000 --- a/libs/crypto +++ b/libs/crypto @@ -1 +1 @@ -Subproject commit 185c1e049be95acbcef29cbac0c1d19989659ffe +Subproject commit 025520686003828307e7de26f17a645b492f5183 diff --git a/tools/test/fixtures/crypto_jamfiles/canary_secp256k1_missing/app.cpp b/tools/test/fixtures/crypto_jamfiles/canary_secp256k1_missing/app.cpp index b0df35ef..f6b4a1b1 100644 --- a/tools/test/fixtures/crypto_jamfiles/canary_secp256k1_missing/app.cpp +++ b/tools/test/fixtures/crypto_jamfiles/canary_secp256k1_missing/app.cpp @@ -7,6 +7,7 @@ // Makes a key pair and signs once: the program reads libsecp256k1's canary, so that a library // built other than by webcpp's third_party/Jamfile fails to link. #include +#include #include #include @@ -17,5 +18,5 @@ int main() { std::array const message{}; std::array const aux{}; auto key = webcpp::crypto::curves::secp256k1::keypair::make(secret); - return key && key->sign_schnorr(message, aux) ? 0 : 1; + return key && key->sign(webcpp::crypto::schnorr, message, aux) ? 0 : 1; } diff --git a/tools/test/fixtures/crypto_jamfiles/canary_secp256k1_small/app.cpp b/tools/test/fixtures/crypto_jamfiles/canary_secp256k1_small/app.cpp index b0df35ef..f6b4a1b1 100644 --- a/tools/test/fixtures/crypto_jamfiles/canary_secp256k1_small/app.cpp +++ b/tools/test/fixtures/crypto_jamfiles/canary_secp256k1_small/app.cpp @@ -7,6 +7,7 @@ // Makes a key pair and signs once: the program reads libsecp256k1's canary, so that a library // built other than by webcpp's third_party/Jamfile fails to link. #include +#include #include #include @@ -17,5 +18,5 @@ int main() { std::array const message{}; std::array const aux{}; auto key = webcpp::crypto::curves::secp256k1::keypair::make(secret); - return key && key->sign_schnorr(message, aux) ? 0 : 1; + return key && key->sign(webcpp::crypto::schnorr, message, aux) ? 0 : 1; } diff --git a/tools/test/fixtures/crypto_jamfiles/ed25519_default/app.cpp b/tools/test/fixtures/crypto_jamfiles/ed25519_default/app.cpp index 17bda9be..64f6c149 100644 --- a/tools/test/fixtures/crypto_jamfiles/ed25519_default/app.cpp +++ b/tools/test/fixtures/crypto_jamfiles/ed25519_default/app.cpp @@ -9,6 +9,7 @@ // it, and prints the backend that ran. The seed is the RFC's: a real program draws its own. #include #include +#include #include #include @@ -16,6 +17,7 @@ #include int main() { + namespace crypto = webcpp::crypto; namespace ed25519 = webcpp::crypto::curves::ed25519; std::array const seed{ 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a, @@ -33,10 +35,10 @@ int main() { auto const key = ed25519::keypair::make(seed); bool ok = false; if (key) { - auto const signature = key->sign(message); + auto const signature = key->sign(crypto::eddsa, message); auto const public_key = key->public_key(); - ok = signature && public_key && webcpp::crypto::equal(*signature, expected) && - ed25519::verify(*public_key, message, *signature).has_value(); + ok = signature && public_key && crypto::equal(*signature, expected) && + crypto::verify(crypto::eddsa, *signature, message, *public_key).has_value(); } auto const name = ed25519::keypair::backend_type::name; std::printf("ed25519 %s backend=%.*s\n", ok ? "ok" : "FAILED", static_cast(name.size()), diff --git a/tools/test/fixtures/crypto_jamfiles/ed25519_unknown_policy_on_emscripten/app.cpp b/tools/test/fixtures/crypto_jamfiles/ed25519_unknown_policy_on_emscripten/app.cpp index fc97fcc3..30ebac05 100644 --- a/tools/test/fixtures/crypto_jamfiles/ed25519_unknown_policy_on_emscripten/app.cpp +++ b/tools/test/fixtures/crypto_jamfiles/ed25519_unknown_policy_on_emscripten/app.cpp @@ -4,12 +4,14 @@ // accompanying file LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) -// A program that names a policy of its own in Ed25519's verify, on its default backend linked, -// stops where it calls it, with the one policy there is. +// A program that names a policy of its own in Ed25519's crypto::verify, on its default backend +// linked, stops where it calls it, with the one policy there is. #include +#include #include #include +#include namespace { @@ -19,7 +21,12 @@ struct lenient {}; } // namespace int main() { - std::array const public_key{}; + auto const public_key = + webcpp::crypto::curves::ed25519::public_key::make(std::array{}); std::array const signature{}; - return webcpp::crypto::curves::ed25519::verify(public_key, {}, signature) ? 0 : 1; + return public_key && + webcpp::crypto::verify(webcpp::crypto::eddsa, signature, + std::span{}, *public_key, lenient{}) + ? 0 + : 1; } diff --git a/tools/test/fixtures/crypto_jamfiles/encrypt_and_sign/app.cpp b/tools/test/fixtures/crypto_jamfiles/encrypt_and_sign/app.cpp index 355a9bc7..9f68789b 100644 --- a/tools/test/fixtures/crypto_jamfiles/encrypt_and_sign/app.cpp +++ b/tools/test/fixtures/crypto_jamfiles/encrypt_and_sign/app.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -48,12 +49,14 @@ int main() { auto pair = secp256k1::keypair::make(secret); bool signed_ok = false; if (pair) { - auto const signature = pair->sign_schnorr(message, aux); - auto const public_key = pair->xonly_public_key(); + auto const signature = pair->sign(webcpp::crypto::schnorr, message, aux); + auto const public_key = pair->public_key(); signed_ok = - signature && public_key && secp256k1::verify_schnorr(*signature, message, *public_key); + signature && public_key && + webcpp::crypto::verify(webcpp::crypto::schnorr, *signature, message, *public_key); message[0] ^= 1U; - signed_ok = signed_ok && !secp256k1::verify_schnorr(*signature, message, *public_key); + signed_ok = signed_ok && !webcpp::crypto::verify(webcpp::crypto::schnorr, *signature, + message, *public_key); } std::printf("schnorr %s\n", signed_ok ? "ok" : "FAILED"); return sealed && opened && signed_ok ? 0 : 1; diff --git a/tools/test/fixtures/crypto_jamfiles/secp256k1_default/app.cpp b/tools/test/fixtures/crypto_jamfiles/secp256k1_default/app.cpp index 84d7baca..e26d6122 100644 --- a/tools/test/fixtures/crypto_jamfiles/secp256k1_default/app.cpp +++ b/tools/test/fixtures/crypto_jamfiles/secp256k1_default/app.cpp @@ -9,6 +9,7 @@ // and prints the backend that ran. The secret key is the vector's: a real program draws its own. #include #include +#include #include #include @@ -30,10 +31,10 @@ int main() { auto const key = secp256k1::keypair::make(secret); bool ok = false; if (key) { - auto const signature = key->sign_schnorr(message, aux); - auto const public_key = key->xonly_public_key(); + auto const signature = key->sign(webcpp::crypto::schnorr, message, aux); + auto const public_key = key->public_key(); ok = signature && public_key && webcpp::crypto::equal(*signature, expected) && - secp256k1::verify_schnorr(*signature, message, *public_key); + webcpp::crypto::verify(webcpp::crypto::schnorr, *signature, message, *public_key); } auto const name = secp256k1::keypair::backend_type::name; std::printf("schnorr %s backend=%.*s\n", ok ? "ok" : "FAILED", static_cast(name.size()), diff --git a/tools/test/fixtures/crypto_jamfiles/secp256k1_ellswift_default/app.cpp b/tools/test/fixtures/crypto_jamfiles/secp256k1_ellswift_default/app.cpp index e6d0ec24..6f65e024 100644 --- a/tools/test/fixtures/crypto_jamfiles/secp256k1_ellswift_default/app.cpp +++ b/tools/test/fixtures/crypto_jamfiles/secp256k1_ellswift_default/app.cpp @@ -38,9 +38,8 @@ int main() { auto const initiator_key = initiator->public_key(); auto const responder_key = responder->public_key(); if (sent && received && initiator_key && responder_key && - secp256k1::serialize_public_key(secp256k1::ellswift_decode(*sent)) == *initiator_key && - secp256k1::serialize_public_key(secp256k1::ellswift_decode(*received)) == - *responder_key) { + secp256k1::ellswift_decode(*sent) == *initiator_key && + secp256k1::ellswift_decode(*received) == *responder_key) { auto const mine = secp256k1::ellswift_xdh(*initiator, *sent, *received, secp256k1::ellswift_party::initiator); auto const theirs = secp256k1::ellswift_xdh(*responder, *sent, *received, diff --git a/tools/test/fixtures/crypto_jamfiles/secp256k1_musig_default/app.cpp b/tools/test/fixtures/crypto_jamfiles/secp256k1_musig_default/app.cpp index 219f537b..149f598f 100644 --- a/tools/test/fixtures/crypto_jamfiles/secp256k1_musig_default/app.cpp +++ b/tools/test/fixtures/crypto_jamfiles/secp256k1_musig_default/app.cpp @@ -10,6 +10,7 @@ // inside nonce_gen. #include #include +#include #include #include @@ -30,13 +31,8 @@ namespace secp256k1 = webcpp::crypto::curves::secp256k1; namespace musig = webcpp::crypto::curves::secp256k1::musig; bool sign_and_verify(secp256k1::keypair const& first, secp256k1::keypair const& second) { - auto const first_encoding = first.public_key(); - auto const second_encoding = second.public_key(); - if (!first_encoding || !second_encoding) { - return false; - } - auto const first_key = secp256k1::parse_public_key(*first_encoding); - auto const second_key = secp256k1::parse_public_key(*second_encoding); + auto const first_key = first.public_key(); + auto const second_key = second.public_key(); if (!first_key || !second_key) { return false; } @@ -72,8 +68,8 @@ bool sign_and_verify(secp256k1::keypair const& first, secp256k1::keypair const& } std::array const parts{*first_part, *second_part}; auto const signature = musig::partial_signature_aggregate(*session, parts, space); - return signature && - secp256k1::verify_schnorr(*signature, message, aggregate->xonly_public_key()); + return signature && webcpp::crypto::verify(webcpp::crypto::schnorr, *signature, message, + aggregate->xonly_public_key()); } } // namespace diff --git a/tools/test/fixtures/crypto_jamfiles/secp256k1_silent_payments_default/app.cpp b/tools/test/fixtures/crypto_jamfiles/secp256k1_silent_payments_default/app.cpp index 3bd9eb7a..38b8343d 100644 --- a/tools/test/fixtures/crypto_jamfiles/secp256k1_silent_payments_default/app.cpp +++ b/tools/test/fixtures/crypto_jamfiles/secp256k1_silent_payments_default/app.cpp @@ -29,15 +29,9 @@ namespace silent_payments = webcpp::crypto::curves::secp256k1::silent_payments; bool pay_and_scan(secp256k1::keypair const& scan_pair, secp256k1::keypair const& spend_pair, secp256k1::keypair const& donor) { - auto const scan_encoding = scan_pair.public_key(); - auto const spend_encoding = spend_pair.public_key(); - auto const donor_encoding = donor.public_key(); - if (!scan_encoding || !spend_encoding || !donor_encoding) { - return false; - } - auto const scan_key = secp256k1::parse_public_key(*scan_encoding); - auto const spend_key = secp256k1::parse_public_key(*spend_encoding); - auto const donor_key = secp256k1::parse_public_key(*donor_encoding); + auto const scan_key = scan_pair.public_key(); + auto const spend_key = spend_pair.public_key(); + auto const donor_key = donor.public_key(); if (!scan_key || !spend_key || !donor_key) { return false; } diff --git a/tools/test/fixtures/crypto_jamfiles/sign/sign.cpp b/tools/test/fixtures/crypto_jamfiles/sign/sign.cpp index fe328c21..f6346234 100644 --- a/tools/test/fixtures/crypto_jamfiles/sign/sign.cpp +++ b/tools/test/fixtures/crypto_jamfiles/sign/sign.cpp @@ -6,6 +6,7 @@ // Signs a message with BIP340 through libsecp256k1, verifies it, and refuses a changed one. #include +#include #include #include @@ -21,11 +22,13 @@ int main() { auto key = secp256k1::keypair::make(secret); bool ok = false; if (key) { - auto const signature = key->sign_schnorr(message, aux); - auto const public_key = key->xonly_public_key(); - ok = signature && public_key && secp256k1::verify_schnorr(*signature, message, *public_key); + auto const signature = key->sign(webcpp::crypto::schnorr, message, aux); + auto const public_key = key->public_key(); + ok = signature && public_key && + webcpp::crypto::verify(webcpp::crypto::schnorr, *signature, message, *public_key); message[0] ^= 1U; - ok = ok && !secp256k1::verify_schnorr(*signature, message, *public_key); + ok = ok && + !webcpp::crypto::verify(webcpp::crypto::schnorr, *signature, message, *public_key); } std::printf("schnorr %s\n", ok ? "ok" : "FAILED"); return ok ? 0 : 1; diff --git a/tools/test/fixtures/crypto_jamfiles/sign_small/sign.cpp b/tools/test/fixtures/crypto_jamfiles/sign_small/sign.cpp index fe328c21..f6346234 100644 --- a/tools/test/fixtures/crypto_jamfiles/sign_small/sign.cpp +++ b/tools/test/fixtures/crypto_jamfiles/sign_small/sign.cpp @@ -6,6 +6,7 @@ // Signs a message with BIP340 through libsecp256k1, verifies it, and refuses a changed one. #include +#include #include #include @@ -21,11 +22,13 @@ int main() { auto key = secp256k1::keypair::make(secret); bool ok = false; if (key) { - auto const signature = key->sign_schnorr(message, aux); - auto const public_key = key->xonly_public_key(); - ok = signature && public_key && secp256k1::verify_schnorr(*signature, message, *public_key); + auto const signature = key->sign(webcpp::crypto::schnorr, message, aux); + auto const public_key = key->public_key(); + ok = signature && public_key && + webcpp::crypto::verify(webcpp::crypto::schnorr, *signature, message, *public_key); message[0] ^= 1U; - ok = ok && !secp256k1::verify_schnorr(*signature, message, *public_key); + ok = ok && + !webcpp::crypto::verify(webcpp::crypto::schnorr, *signature, message, *public_key); } std::printf("schnorr %s\n", ok ? "ok" : "FAILED"); return ok ? 0 : 1;