Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 48 additions & 29 deletions CODING-GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<N>`
(`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
Expand Down Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion libs/crypto
Submodule crypto updated 238 files
Original file line number Diff line number Diff line change
Expand Up @@ -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 <webcpp/crypto/curves/secp256k1.hpp>
#include <webcpp/crypto/signing.hpp>

#include <array>
#include <cstdint>
Expand All @@ -17,5 +18,5 @@ int main() {
std::array<std::uint8_t, 32> const message{};
std::array<std::uint8_t, 32> 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <webcpp/crypto/curves/secp256k1.hpp>
#include <webcpp/crypto/signing.hpp>

#include <array>
#include <cstdint>
Expand All @@ -17,5 +18,5 @@ int main() {
std::array<std::uint8_t, 32> const message{};
std::array<std::uint8_t, 32> 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;
}
8 changes: 5 additions & 3 deletions tools/test/fixtures/crypto_jamfiles/ed25519_default/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
// it, and prints the backend that ran. The seed is the RFC's: a real program draws its own.
#include <webcpp/crypto/curves/ed25519.hpp>
#include <webcpp/crypto/equal.hpp>
#include <webcpp/crypto/signing.hpp>

#include <array>
#include <cstdint>
#include <cstdio>
#include <span>

int main() {
namespace crypto = webcpp::crypto;
namespace ed25519 = webcpp::crypto::curves::ed25519;
std::array<std::uint8_t, 32> const seed{
0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a,
Expand All @@ -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<int>(name.size()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <webcpp/crypto/curves/ed25519.hpp>
#include <webcpp/crypto/signing.hpp>

#include <array>
#include <cstdint>
#include <span>

namespace {

Expand All @@ -19,7 +21,12 @@ struct lenient {};
} // namespace

int main() {
std::array<std::uint8_t, 32> const public_key{};
auto const public_key =
webcpp::crypto::curves::ed25519::public_key::make(std::array<std::uint8_t, 32>{});
std::array<std::uint8_t, 64> const signature{};
return webcpp::crypto::curves::ed25519::verify<lenient>(public_key, {}, signature) ? 0 : 1;
return public_key &&
webcpp::crypto::verify(webcpp::crypto::eddsa, signature,
std::span<std::uint8_t const>{}, *public_key, lenient{})
? 0
: 1;
}
11 changes: 7 additions & 4 deletions tools/test/fixtures/crypto_jamfiles/encrypt_and_sign/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <webcpp/crypto/ciphers/aes256_gcm.hpp>
#include <webcpp/crypto/curves/secp256k1.hpp>
#include <webcpp/crypto/equal.hpp>
#include <webcpp/crypto/signing.hpp>

#include <array>
#include <cstdint>
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions tools/test/fixtures/crypto_jamfiles/secp256k1_default/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// and prints the backend that ran. The secret key is the vector's: a real program draws its own.
#include <webcpp/crypto/curves/secp256k1.hpp>
#include <webcpp/crypto/equal.hpp>
#include <webcpp/crypto/signing.hpp>

#include <array>
#include <cstdint>
Expand All @@ -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<int>(name.size()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// inside nonce_gen.
#include <webcpp/crypto/curves/secp256k1.hpp>
#include <webcpp/crypto/curves/secp256k1/musig.hpp>
#include <webcpp/crypto/signing.hpp>

#include <algorithm>
#include <array>
Expand All @@ -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;
}
Expand Down Expand Up @@ -72,8 +68,8 @@ bool sign_and_verify(secp256k1::keypair const& first, secp256k1::keypair const&
}
std::array<musig::partial_signature, 2> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 7 additions & 4 deletions tools/test/fixtures/crypto_jamfiles/sign/sign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

// Signs a message with BIP340 through libsecp256k1, verifies it, and refuses a changed one.
#include <webcpp/crypto/curves/secp256k1.hpp>
#include <webcpp/crypto/signing.hpp>

#include <array>
#include <cstdint>
Expand All @@ -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;
Expand Down
Loading
Loading