Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
398acb8
crypto: secp256k1's ECDH returns secret<32>, and gives the raw x coor…
Nov 17, 2026
1e447a9
crypto: ECDH's page cites SEC 1 and Wycheproof as sources, its pitfal…
Nov 17, 2026
a89b2cd
crypto: the ECDH example's Jam comment wraps at 80 columns, as the li…
Nov 17, 2026
730f3fc
crypto: secp256k1's recoverable signatures, hedged ECDSA and the pars…
Nov 17, 2026
294bd2b
crypto: the curves page and its Ethereum rule say what Tasks 2.1 and …
Nov 17, 2026
b3dce1c
crypto: the curves page's paragraph on Ethereum wraps at 80 columns, …
Nov 17, 2026
9bbad30
crypto: secp256k1's tweaks, negation and combination, and its SPKI an…
Nov 17, 2026
8e3f54f
crypto: the SPKI and PKCS#8 encoders and pubkey_combine read the cana…
Nov 17, 2026
4573e95
crypto: ElligatorSwift on libsecp256k1, against BIP 324's vectors
Nov 17, 2026
aa3cdd0
crypto: pubkey_combine wipes its workspace on every return, its refus…
Nov 17, 2026
c7c8583
crypto: the tweak example wipes the HMAC's state, its digest and the …
Nov 17, 2026
ce84ca8
crypto: the ElligatorSwift example drops its exchange tag, which no p…
Nov 17, 2026
1bb9a70
crypto: the curves page lists what secp256k1's three module headers a…
Nov 17, 2026
37cab1d
crypto: secp256k1's Doc Comments name the random device in words, as …
Nov 17, 2026
8d92728
crypto: MuSig2 on libsecp256k1, against BIP 327's vectors, with a non…
Nov 17, 2026
93dba32
crypto: the tweak example drops its tweak and sign tags, which no pag…
Nov 17, 2026
5a7a51e
crypto: the MuSig2 section names invalid_input_size for a nonce or pa…
Nov 17, 2026
807d967
crypto: silent payments on libsecp256k1, against BIP 352's vectors
Nov 17, 2026
e234cbf
crypto: the allocation test counts a silent payments scan that calls …
Nov 18, 2026
7c4f3db
crypto: scan_outputs reads a label lookup's result as the type its co…
Nov 18, 2026
7bc7988
crypto: silent payments test the group limit counted after a refusal …
Nov 18, 2026
bda797d
crypto: create_outputs counts recipient groups after a refusal in O(n…
Nov 18, 2026
e9129b4
crypto: the C++ copy of BIP 352's vector structs holds std::array, un…
Nov 18, 2026
6a7e6d2
crypto: the silent payments section says a wallet always scans for th…
Nov 18, 2026
e08aa6d
crypto: the silent payments section cites Emscripten's fixed stack an…
Nov 18, 2026
b4ad83f
crypto: secp256k1's ElligatorSwift, MuSig2 and silent payments as fam…
Nov 18, 2026
cb7aa47
crypto: the table generator writes a possessive after an s and a fami…
Nov 18, 2026
2072e39
crypto: the secp256k1 refusal wrapper says why the generated macro ca…
Nov 18, 2026
b1f5516
crypto: the silent payments family is titled BIP 352 silent payments,…
Nov 18, 2026
f352317
crypto: the silent payments section says what the WASI targets' stack…
Nov 18, 2026
c5059f1
crypto: the changelog does not promise supports_v a type for the secp…
Nov 18, 2026
9e346c9
crypto: the table generator's two long lines are reflowed to 100 columns
Nov 18, 2026
178373d
crypto: the secp256k1 refusal wrapper says the generated macro assert…
Nov 18, 2026
4928468
crypto: the silent payments fixture's description names a recipient's…
Nov 18, 2026
8218b9e
crypto: secp256k1's keys in PEM
Nov 18, 2026
96486da
crypto: secp256k1's header passes clang-tidy: point_size's branches a…
Nov 18, 2026
6cafbfa
crypto: the secp256k1 examples pass clang-tidy: static size calls, a …
Nov 18, 2026
fad29ea
crypto: point_size is the compressed size, with the uncompressed one …
Nov 18, 2026
73f0fd9
crypto: the secp256k1 tests' seeded generators carry the reproducible…
Nov 18, 2026
a1cf7fc
crypto: the secp256k1 keys, formats and ECDH tests pass clang-tidy: s…
Nov 18, 2026
32bcf6b
crypto: the recovery and MuSig2 tests pass clang-tidy: smaller functi…
Nov 18, 2026
cd7ea7c
crypto: the silent payments and allocation tests pass clang-tidy: a r…
Nov 18, 2026
37ab99f
crypto: the changelog names secp256k1's modules beside supports_v and…
Nov 19, 2026
9742122
crypto: the constant-time lane runs webcpp's code around libsecp256k1…
Nov 19, 2026
590deb0
crypto: the sizes lane measures secp256k1's ecdh_x and recover, Ellig…
Nov 19, 2026
5e68fa1
crypto: the wallets and peer-to-peer chapters teach recoverable signa…
Nov 19, 2026
015d717
crypto: secp256k1's families give each checker its own lane note, so …
Nov 19, 2026
92b319f
crypto: a use case that links a teaching chapter names the operation …
Nov 19, 2026
62b0412
crypto: the teaching sections say only that they name no application,…
Nov 19, 2026
0f450ef
crypto: two MuSig2 examples, the signers' public pieces crossing the …
Nov 19, 2026
b8d9fae
crypto: a silent payments label lookup returns exactly an optional sp…
Nov 19, 2026
0a73ee4
crypto: curves.adoc wraps its secp256k1 list at 80 columns, the memor…
Nov 19, 2026
24397cd
crypto: musig_network checks the intact partial signatures before the…
Nov 19, 2026
e7c71db
crypto: secp256k1_keys and secp256k1_recovery loop over std::to_array…
Nov 20, 2026
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
255 changes: 255 additions & 0 deletions bench/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include <webcpp/crypto/error.hpp>
#ifdef WEBCPP_CRYPTO_HAS_SECP256K1
#include <webcpp/crypto/curves/secp256k1.hpp>
#include <webcpp/crypto/curves/secp256k1/ellswift.hpp>
#include <webcpp/crypto/curves/secp256k1/musig.hpp>
#include <webcpp/crypto/curves/secp256k1/silent_payments.hpp>
#endif

#include <boost/system/result.hpp>
Expand Down Expand Up @@ -195,6 +198,258 @@ class secp256k1 {
std::array<std::uint8_t, 32> aux_{};
curves::secp256k1::basic_keypair<Backend> key_;
};

// The x coordinate of the ECDH shared point of a key pair and a peer's public key, unhashed.
template <class Backend>
class secp256k1_ecdh_x {
public:
static constexpr std::string_view operation =
"x coordinate of the ECDH shared point with a peer's public key";

explicit secp256k1_ecdh_x(std::uint8_t seed)
: key_(take(curves::secp256k1::basic_keypair<Backend>::make(
filled<32>(static_cast<std::uint8_t>(seed | 1U))),
"make")),
peer_(take(curves::secp256k1::basic_keypair<Backend>::make(
filled<32>(static_cast<std::uint8_t>(seed + 6U))),
"make")),
peer_public_(take(peer_.public_key(), "public_key")) {}

std::uint8_t run() {
auto const shared = take(key_.ecdh_x(peer_public_), "ecdh_x");
return shared.bytes()[0];
}

bool check() {
auto const own_public = take(key_.public_key(), "public_key");
auto const shared = take(key_.ecdh_x(peer_public_), "ecdh_x");
auto const peer_shared = take(peer_.ecdh_x(own_public), "ecdh_x");
return std::ranges::equal(shared.bytes(), peer_shared.bytes());
}

private:
curves::secp256k1::basic_keypair<Backend> key_;
curves::secp256k1::basic_keypair<Backend> peer_;
std::array<std::uint8_t, 33> peer_public_{};
};

// The public key recovered from a recoverable ECDSA signature of a 32-byte hash; the signature
// is made once, outside what is measured.
template <class Backend>
class secp256k1_recover {
public:
static constexpr std::string_view operation =
"public key recovered from an ECDSA signature of a 32-byte hash";

explicit secp256k1_recover(std::uint8_t seed)
: hash_(filled<32>(static_cast<std::uint8_t>(seed + 3U))),
key_(take(curves::secp256k1::basic_keypair<Backend>::make(
filled<32>(static_cast<std::uint8_t>(seed | 1U))),
"make")),
signature_(take(key_.sign_recoverable(hash_), "sign_recoverable")) {}

std::uint8_t run() {
auto const recovered =
take(curves::secp256k1::recover<Backend>(signature_, hash_), "recover");
return curves::secp256k1::serialize_public_key<curves::secp256k1::point_form::compressed,
Backend>(recovered)[1];
}

bool check() {
auto const recovered =
take(curves::secp256k1::recover<Backend>(signature_, hash_), "recover");
return curves::secp256k1::serialize_public_key<curves::secp256k1::point_form::compressed,
Backend>(recovered) ==
take(key_.public_key(), "public_key");
}

private:
std::array<std::uint8_t, 32> hash_{};
curves::secp256k1::basic_keypair<Backend> key_;
std::array<std::uint8_t, curves::secp256k1::recoverable_signature_size> signature_{};
};

// BIP 324's shared secret of a key pair and a peer, each known by its ElligatorSwift encoding.
template <class Backend>
class secp256k1_ellswift {
public:
static constexpr std::string_view operation =
"BIP 324 shared secret of two ElligatorSwift encodings";

explicit secp256k1_ellswift(std::uint8_t seed)
: key_(take(curves::secp256k1::basic_keypair<Backend>::make(
filled<32>(static_cast<std::uint8_t>(seed | 1U))),
"make")),
peer_(take(curves::secp256k1::basic_keypair<Backend>::make(
filled<32>(static_cast<std::uint8_t>(seed + 6U))),
"make")),
own_encoding_(
take(curves::secp256k1::ellswift_create(key_, filled<32>(seed)), "ellswift_create")),
peer_encoding_(take(curves::secp256k1::ellswift_create(
peer_, filled<32>(static_cast<std::uint8_t>(seed + 7U))),
"ellswift_create")) {}

std::uint8_t run() {
auto const shared =
take(curves::secp256k1::ellswift_xdh(key_, own_encoding_, peer_encoding_,
curves::secp256k1::ellswift_party::initiator),
"ellswift_xdh");
return shared.bytes()[0];
}

bool check() {
auto const shared =
take(curves::secp256k1::ellswift_xdh(key_, own_encoding_, peer_encoding_,
curves::secp256k1::ellswift_party::initiator),
"ellswift_xdh");
auto const peer_shared =
take(curves::secp256k1::ellswift_xdh(peer_, own_encoding_, peer_encoding_,
curves::secp256k1::ellswift_party::responder),
"ellswift_xdh");
return std::ranges::equal(shared.bytes(), peer_shared.bytes());
}

private:
curves::secp256k1::basic_keypair<Backend> key_;
curves::secp256k1::basic_keypair<Backend> peer_;
std::array<std::uint8_t, curves::secp256k1::ellswift_size> own_encoding_{};
std::array<std::uint8_t, curves::secp256k1::ellswift_size> peer_encoding_{};
};

// A two-signer MuSig2 signature of a 32-byte message: both signers' nonces, the session, both
// partial signatures and their aggregate, the keys aggregated once outside what is measured.
template <class Backend>
class secp256k1_musig {
public:
static constexpr std::string_view operation =
"two-signer MuSig2 signature of a 32-byte message";

explicit secp256k1_musig(std::uint8_t seed)
: message_(filled<32>(static_cast<std::uint8_t>(seed + 3U))),
first_(take(curves::secp256k1::basic_keypair<Backend>::make(
filled<32>(static_cast<std::uint8_t>(seed | 1U))),
"make")),
second_(take(curves::secp256k1::basic_keypair<Backend>::make(
filled<32>(static_cast<std::uint8_t>(seed + 6U))),
"make")),
signers_key_(aggregate(first_, second_, space_)) {}

std::uint8_t run() { return sign()[0]; }

bool check() {
return curves::secp256k1::verify_schnorr<Backend>(sign(), message_,
signers_key_.xonly_public_key())
.has_value();
}

private:
using space_type = std::array<std::byte, curves::secp256k1::musig::workspace_size(2)>;

static curves::secp256k1::musig::aggregate_key aggregate(
curves::secp256k1::basic_keypair<Backend> const& first,
curves::secp256k1::basic_keypair<Backend> const& second, space_type& space) {
std::array<curves::secp256k1::public_key, 2> const keys{
take(curves::secp256k1::parse_public_key<Backend>(take(first.public_key(), "key")),
"parse_public_key"),
take(curves::secp256k1::parse_public_key<Backend>(take(second.public_key(), "key")),
"parse_public_key"),
};
return take(curves::secp256k1::musig::key_aggregate<Backend>(keys, space), "key_aggregate");
}

std::array<std::uint8_t, 64> sign() {
namespace musig = curves::secp256k1::musig;
auto first_nonce = take(musig::nonce_gen(first_, signers_key_, message_), "nonce_gen");
auto second_nonce = take(musig::nonce_gen(second_, signers_key_, message_), "nonce_gen");
std::array<musig::public_nonce, 2> const nonces{first_nonce.public_part,
second_nonce.public_part};
auto const nonce_sum = take(musig::nonce_aggregate<Backend>(nonces, space_), "nonce_sum");
auto const signing_session =
take(musig::nonce_process<Backend>(nonce_sum, message_, signers_key_), "session");
std::array<musig::partial_signature, 2> const parts{
take(musig::partial_sign(first_, std::move(first_nonce.secret_part), signers_key_,
signing_session),
"partial_sign"),
take(musig::partial_sign(second_, std::move(second_nonce.secret_part), signers_key_,
signing_session),
"partial_sign"),
};
return take(musig::partial_signature_aggregate<Backend>(signing_session, parts, space_),
"partial_signature_aggregate");
}

std::array<std::uint8_t, 32> message_{};
alignas(std::max_align_t) space_type space_{};
curves::secp256k1::basic_keypair<Backend> first_;
curves::secp256k1::basic_keypair<Backend> second_;
curves::secp256k1::musig::aggregate_key signers_key_;
};

// A silent payment output for one recipient, from one input's key pair.
template <class Backend>
class secp256k1_silent_payments {
public:
static constexpr std::string_view operation = "a silent payment output for one recipient";

explicit secp256k1_silent_payments(std::uint8_t seed)
: scan_(take(curves::secp256k1::basic_keypair<Backend>::make(
filled<32>(static_cast<std::uint8_t>(seed | 1U))),
"make")),
spend_key_(public_of(take(curves::secp256k1::basic_keypair<Backend>::make(
filled<32>(static_cast<std::uint8_t>(seed + 6U))),
"make"))),
input_(take(curves::secp256k1::basic_keypair<Backend>::make(
filled<32>(static_cast<std::uint8_t>(seed + 8U))),
"make")),
outpoint_(filled<curves::secp256k1::silent_payments::outpoint_size>(seed)) {}

std::uint8_t run() { return send()[0]; }

bool check() {
namespace silent_payments = curves::secp256k1::silent_payments;
std::array<std::array<std::uint8_t, 32>, 1> const outputs{send()};
std::array<curves::secp256k1::public_key, 1> const input_keys{public_of(input_)};
alignas(std::max_align_t)
std::array<std::byte, silent_payments::summary_workspace_size(0, 1)>
summary_space{};
auto const summary = take(silent_payments::make_prevouts_summary<Backend>(
outpoint_, {}, input_keys, summary_space),
"make_prevouts_summary");
std::array<silent_payments::found_output, 1> found{};
alignas(std::max_align_t) std::array<std::byte, silent_payments::scan_workspace_size(1)>
scan_space{};
return take(silent_payments::scan_outputs(scan_, spend_key_, summary, outputs, found,
scan_space),
"scan_outputs") == 1;
}

private:
static curves::secp256k1::public_key public_of(
curves::secp256k1::basic_keypair<Backend> const& key) {
return take(curves::secp256k1::parse_public_key<Backend>(take(key.public_key(), "key")),
"parse_public_key");
}

std::array<std::uint8_t, 32> send() {
namespace silent_payments = curves::secp256k1::silent_payments;
std::array<silent_payments::recipient, 1> const recipients{
silent_payments::recipient{.scan_key = public_of(scan_), .spend_key = spend_key_}};
std::array<curves::secp256k1::basic_keypair<Backend> const*, 1> const plain_inputs{&input_};
std::array<std::array<std::uint8_t, 32>, 1> outputs{};
alignas(std::max_align_t)
std::array<std::byte, silent_payments::send_workspace_size(1, 0, 1)>
space{};
take(silent_payments::create_outputs<Backend>(recipients, outpoint_, {}, plain_inputs,
outputs, space),
"create_outputs");
return outputs[0];
}

curves::secp256k1::basic_keypair<Backend> scan_;
curves::secp256k1::public_key spend_key_;
curves::secp256k1::basic_keypair<Backend> input_;
std::array<std::uint8_t, curves::secp256k1::silent_payments::outpoint_size> outpoint_{};
};
#endif

// An Ed25519 signature of a 32-byte message.
Expand Down
33 changes: 33 additions & 0 deletions build.jam
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,37 @@ rule sealed_box-default-linked ( properties * )
}
}
alias sealed_box-default : : : : <conditional>@sealed_box-default-linked ;
rule secp256k1_ellswift-default-linked ( properties * )
{
switch [ webcpp-target.target-of $(properties) ]
{
case native : return <library>/third_party//secp256k1 ;
case wasip2 : return <library>/third_party//secp256k1 ;
case wasip3 : return <library>/third_party//secp256k1 ;
case emscripten : return <library>/third_party//secp256k1 ;
}
}
alias secp256k1_ellswift-default : : : : <conditional>@secp256k1_ellswift-default-linked ;
rule secp256k1_musig-default-linked ( properties * )
{
switch [ webcpp-target.target-of $(properties) ]
{
case native : return <library>/third_party//secp256k1 ;
case wasip2 : return <library>/third_party//secp256k1 ;
case wasip3 : return <library>/third_party//secp256k1 ;
case emscripten : return <library>/third_party//secp256k1 ;
}
}
alias secp256k1_musig-default : : : : <conditional>@secp256k1_musig-default-linked ;
rule secp256k1_silent_payments-default-linked ( properties * )
{
switch [ webcpp-target.target-of $(properties) ]
{
case native : return <library>/third_party//secp256k1 ;
case wasip2 : return <library>/third_party//secp256k1 ;
case wasip3 : return <library>/third_party//secp256k1 ;
case emscripten : return <library>/third_party//secp256k1 ;
}
}
alias secp256k1_silent_payments-default : : : : <conditional>@secp256k1_silent_payments-default-linked ;
# generate.py: end the defaults' aliases
35 changes: 24 additions & 11 deletions doc/backends.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,23 @@ ChaCha20-Poly1305 `make`, `seal` and `open`; OpenSSL's `make` and `seal` of
both; ctaes's AES; libsodium's Ed25519 key pair and signature, X25519's public
key and agreement, its three ChaCha20-Poly1305 AEADs and two secret boxes,
`hybrid::box` in both families, the sealed box's `seal` and `open`, and `kx`;
and libsecp256k1's own `ctime_tests.c`. Both checkers run the same paths,
written once (`paths.hpp`), memcheck OpenSSL's besides. Each check is shown to
fail on a planted branch on a key byte, and each path's output is checked to
carry the mark, so that the path is traced, where the checker's shadow keeps
it: MemorySanitizer's loses the mark through a constant-time reduction to one
bit, so the result of `is_all_zero`, X25519's public key and the bytes a
destructor wiped are checked by memcheck alone. dudect's statistics
libsecp256k1's own `ctime_tests.c`; and webcpp's code around libsecp256k1:
the key pair's making, its BIP 340, recoverable and hedged ECDSA signatures and
x-only tweak, ECDH in its three forms, the secret key's tweaks and negation,
PKCS#8's encoding and decoding, ElligatorSwift's `ellswift_create` and
`ellswift_xdh`, MuSig2's `nonce_gen` and `partial_sign`, and silent payments'
`create_outputs`, `create_label` and `scan_outputs`, with the wipe of their
workspaces. Both checkers run the same paths, written once (`paths.hpp`),
memcheck OpenSSL's besides. Each check is shown to fail on a planted branch on
a key byte, and each path's output is checked to carry the mark, so that the
path is traced, where the checker's shadow keeps it: MemorySanitizer's loses
the mark through a constant-time reduction to one bit, so the result of
`is_all_zero`, X25519's public key and the bytes a destructor wiped are checked
by memcheck alone. dudect's statistics
(link:https://eprint.iacr.org/2016/1123[Reparaz, Balasch and Verbauwhede]) time
BearSSL's two AEADs' seal and open, ctaes, `equal`, and libsodium's X25519
agreement and Ed25519 signing natively, on Linux and macOS, and under wasmtime.
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
Expand All @@ -118,8 +125,14 @@ everything else.
Whether a tag matched, or a shared secret came out all zero, is public by
design, since the operation reports it, but it is computed from the secrets: it
is the one kind of value webcpp marks public, where webcpp's code first
branches on it (BearSSL's engines, libsodium's engines and X25519's `agree`),
so that the checkers report every other branch. libsodium branches on its own
branches on it (BearSSL's engines, libsodium's engines, X25519's `agree`, and
each verdict of libsecp256k1's that webcpp reads), so that the checkers report
every other branch. libsecp256k1 marks its own public values (a nonce's retry,
a verdict, a public key it loads) when its context is made with
`SECP256K1_CONTEXT_DECLASSIFY`, which webcpp's contexts are under the
checkers, as its `ctime_tests.c` makes its own; a key pair's public key and a
silent payments label, which webcpp reads from libsecp256k1's structures, are
marked public where webcpp reads them. libsodium branches on its own
verdicts inside the functions that compute them; the lane tells memcheck of
each of those functions (`sodium_verdicts.supp`), and libsodium is used
unchanged. MemorySanitizer approximates a subtraction without its carry, so it
Expand Down
Loading
Loading