Commit Graph

3604 Commits

Author SHA1 Message Date
Oliver Gugger
a764afd44e
psbt: rename receiver to match rest of code 2022-05-02 16:25:11 +02:00
Oliver Gugger
a336854e27
psbt: fix typo, remove TODO 2022-05-02 16:25:09 +02:00
Olaoluwa Osuntokun
a86222c92d
Merge pull request #1820 from Roasbeef/musig2
btcec/schnorr/musig2: add new musig2 implementation based on musig2 draft BIP
2022-04-28 16:46:04 -07:00
Olaoluwa Osuntokun
1da361b04e
btcec/schnorr/musig2: add optional json dump command to gen test vectors 2022-04-28 16:20:11 -07:00
Olaoluwa Osuntokun
ba20c75aaf
btcec/schnorr/musig2: pass in aux info during nonce generation 2022-04-28 16:20:07 -07:00
Olaoluwa Osuntokun
953e2dd94a
btcec/schnorr/musig2: enable early nonce generation w/ a context
In this commit, we enable early nonce generation, allowing callers to
obtain generated nonces before the total set of signers is actually
known. This type of nonce generation is useful for contexts like LN
funding when we want to minimize the round trips and send nonces before
we know the pubkey of the other party.
2022-04-28 16:20:05 -07:00
Olaoluwa Osuntokun
55c8cab769
btcec/schnorr/musig2: add new key tweak combination test vectors 2022-04-28 16:20:03 -07:00
Olaoluwa Osuntokun
65e4fc0dea
btcec/schnorr/musig2: update nonce generation to support optional inputs
In this commit, we update the nonce generation to support optional
parameters defined in the latest BIP draft. These parameters are
optional, but if specified my mitigate the effect of weak randomness
when generating the nonce.

Given the protocol doesn't require signers to prove how they generate
their nonces, this update is mainly to ensure strict spec compliance,
and is effectively optional.
2022-04-28 16:20:01 -07:00
Olaoluwa Osuntokun
9d0d52708a
btcec/schnorr/musig2: add explicit support for BIP 86 multi-signing
In this commit, we add a series of new functional optinos to make
signing for an aggregated key where the final taproot output key was
derived using BIP 86. This can be used in cases where no script path
shuold be allowed, and only an n-of-n multi-sig should be used.
2022-04-28 16:19:59 -07:00
Olaoluwa Osuntokun
f7168c8663
schnorr/musig2: add native support for taproot output key tweaking
In this commit, we add a series of new options and methods to make it
easier to use the package in the context of a taproot output that
commits to a script root or some other value. Before this series of
changes, the API was hard to use in this context as the taproot tweak
actually includes the internal public key, which in this case is the
aggregated public key. So you actually needed to call that API w/o the
tweak, get that, then recompute the tweak itself.

To make things easier in the taproot context, we've added a series of
new options that'll return the aggregated key before any tweaks (to be
used as the internal key), and also handle computing the BIP 341 tweak
value for the caller.
2022-04-28 16:19:57 -07:00
Olaoluwa Osuntokun
08187eb786
btcec/schnorr/musig2: add support for tweaked aggregated keys
In this commit, we add support for signing with tweaked aggregated keys.
Such signing is required when signing for a taproot output key that
actually commits to a script tree root, or was generated using BIP 86.

A series of new functional arguments (that can likely be de-dup'd using
Go's new type params), have been added to allow callers to optionally
flip on this new behavior.
2022-04-28 16:19:55 -07:00
Olaoluwa Osuntokun
743cbc8403
btcec/schnorr/musig2: add safer signing API with Session+Context
In this commit, we introduce an easier to use API for musig2 signing in
the Session and Context structs.

The Context struct represents a particular musig2 signing context which
is defined by the set of signers. The struct can be serialized to disk
as it contains no volatile information. A given context can be kept for
each signer in the final set.

The Session struct represents an ephemeral musig2 signing session. It
handles nonce generation, key aggregation, nonce combination, signature
combination, and final sig verification all in one API. The API also
protects against nonce generation by not exposing nonces to the end user
and also attempting to catch nonce re-use (assuming no process forking)
across sessions.
2022-04-28 16:19:53 -07:00
Olaoluwa Osuntokun
e85e7c3ac7
btcec/schnorr/musig2: optimize signing+verification
In this commit, we optimize signing+verification mainly by only
computing values once, and reducing allocations when possible.

The following optimizations have been implemented:
  * Use a single buffer allocation in keyHashFingerprint to avoid
    dynamic buffer growth+re-sizing
  * Remove the isSecondKey computation and replace that with a single
    routine that computes the index of the second unique key.
  * Optimize keyHashFingerprint usage by only computing it once during
    signing +verification.

A further optimization is possible: use the x coordinate of a key for
comparisons instead of computing the full sexualision. We need to do
the latter atm, as the X() method of the public key struct will allocate
more memory as it allocate and sets the buffer in place.

The final benchmarks of before and after this commit:
benchmark                                                             old ns/op     new ns/op     delta
BenchmarkPartialSign/num_signers=10/fast_sign=true/sort=true-8        1227374       1194047       -2.72%
BenchmarkPartialSign/num_signers=10/fast_sign=true/sort=false-8       1217743       1191468       -2.16%
BenchmarkPartialSign/num_signers=10/fast_sign=false/sort=true-8       2755544       2698827       -2.06%
BenchmarkPartialSign/num_signers=10/fast_sign=false/sort=false-8      2754749       2694547       -2.19%
BenchmarkPartialSign/num_signers=100/fast_sign=true/sort=true-8       12382654      10561204      -14.71%
BenchmarkPartialSign/num_signers=100/fast_sign=true/sort=false-8      12260134      10315376      -15.86%
BenchmarkPartialSign/num_signers=100/fast_sign=false/sort=true-8      24832061      22009935      -11.36%
BenchmarkPartialSign/num_signers=100/fast_sign=false/sort=false-8     24650086      21022833      -14.71%
BenchmarkPartialVerify/sort_keys=true/num_signers=10-8                1485787       1473377       -0.84%
BenchmarkPartialVerify/sort_keys=false/num_signers=10-8               1447275       1465139       +1.23%
BenchmarkPartialVerify/sort_keys=true/num_signers=100-8               12503482      10672618      -14.64%
BenchmarkPartialVerify/sort_keys=false/num_signers=100-8              12388289      10581398      -14.59%
BenchmarkCombineSigs/num_signers=10-8                                 0.00          0.00          +0.00%
BenchmarkCombineSigs/num_signers=100-8                                0.00          0.00          -1.95%
BenchmarkAggregateNonces/num_signers=10-8                             0.00          0.00          -0.76%
BenchmarkAggregateNonces/num_signers=100-8                            0.00          0.00          +1.13%
BenchmarkAggregateKeys/num_signers=10/sort_keys=true-8                0.00          0.00          -0.09%
BenchmarkAggregateKeys/num_signers=10/sort_keys=false-8               0.00          0.01          +559.94%
BenchmarkAggregateKeys/num_signers=100/sort_keys=true-8               0.01          0.01          -11.30%
BenchmarkAggregateKeys/num_signers=100/sort_keys=false-8              0.01          0.01          -11.66%

benchmark                                                             old allocs     new allocs     delta
BenchmarkPartialSign/num_signers=10/fast_sign=true/sort=true-8        458            269            -41.27%
BenchmarkPartialSign/num_signers=10/fast_sign=true/sort=false-8       409            222            -45.72%
BenchmarkPartialSign/num_signers=10/fast_sign=false/sort=true-8       892            524            -41.26%
BenchmarkPartialSign/num_signers=10/fast_sign=false/sort=false-8      841            467            -44.47%
BenchmarkPartialSign/num_signers=100/fast_sign=true/sort=true-8       14366          3089           -78.50%
BenchmarkPartialSign/num_signers=100/fast_sign=true/sort=false-8      13143          1842           -85.98%
BenchmarkPartialSign/num_signers=100/fast_sign=false/sort=true-8      27596          4964           -82.01%
BenchmarkPartialSign/num_signers=100/fast_sign=false/sort=false-8     26309          3707           -85.91%
BenchmarkPartialVerify/sort_keys=true/num_signers=10-8                430            243            -43.49%
BenchmarkPartialVerify/sort_keys=false/num_signers=10-8               430            243            -43.49%
BenchmarkPartialVerify/sort_keys=true/num_signers=100-8               13164          1863           -85.85%
BenchmarkPartialVerify/sort_keys=false/num_signers=100-8              13164          1863           -85.85%
BenchmarkCombineSigs/num_signers=10-8                                 0              0              +0.00%
BenchmarkCombineSigs/num_signers=100-8                                0              0              +0.00%
BenchmarkAggregateNonces/num_signers=10-8                             0              0              +0.00%
BenchmarkAggregateNonces/num_signers=100-8                            0              0              +0.00%
BenchmarkAggregateKeys/num_signers=10/sort_keys=true-8                0              0              +0.00%
BenchmarkAggregateKeys/num_signers=10/sort_keys=false-8               0              0              +0.00%
BenchmarkAggregateKeys/num_signers=100/sort_keys=true-8               0              0              +0.00%
BenchmarkAggregateKeys/num_signers=100/sort_keys=false-8              0              0              +0.00%

benchmark                                                             old bytes     new bytes     delta
BenchmarkPartialSign/num_signers=10/fast_sign=true/sort=true-8        27854         14878         -46.59%
BenchmarkPartialSign/num_signers=10/fast_sign=true/sort=false-8       25508         12605         -50.58%
BenchmarkPartialSign/num_signers=10/fast_sign=false/sort=true-8       54982         29476         -46.39%
BenchmarkPartialSign/num_signers=10/fast_sign=false/sort=false-8      52581         26805         -49.02%
BenchmarkPartialSign/num_signers=100/fast_sign=true/sort=true-8       1880138       166996        -91.12%
BenchmarkPartialSign/num_signers=100/fast_sign=true/sort=false-8      1820561       106295        -94.16%
BenchmarkPartialSign/num_signers=100/fast_sign=false/sort=true-8      3706291       275344        -92.57%
BenchmarkPartialSign/num_signers=100/fast_sign=false/sort=false-8     3642725       214122        -94.12%
BenchmarkPartialVerify/sort_keys=true/num_signers=10-8                26995         14078         -47.85%
BenchmarkPartialVerify/sort_keys=false/num_signers=10-8               26980         14078         -47.82%
BenchmarkPartialVerify/sort_keys=true/num_signers=100-8               1822043       107767        -94.09%
BenchmarkPartialVerify/sort_keys=false/num_signers=100-8              1822046       107752        -94.09%
BenchmarkCombineSigs/num_signers=10-8                                 0             0             +0.00%
BenchmarkCombineSigs/num_signers=100-8                                0             0             +0.00%
BenchmarkAggregateNonces/num_signers=10-8                             0             0             +0.00%
BenchmarkAggregateNonces/num_signers=100-8                            0             0             +0.00%
BenchmarkAggregateKeys/num_signers=10/sort_keys=true-8                0             0             +0.00%
BenchmarkAggregateKeys/num_signers=10/sort_keys=false-8               0             0             +0.00%
BenchmarkAggregateKeys/num_signers=100/sort_keys=true-8               0             0             +0.00%
BenchmarkAggregateKeys/num_signers=100/sort_keys=false-8              0             0             +0.00%
2022-04-28 16:19:51 -07:00
Olaoluwa Osuntokun
4b46b2298a
btcec/schnorr/musig2: add benchmarks 2022-04-28 16:19:49 -07:00
Olaoluwa Osuntokun
69a42a3566
btcec/schnorr/musig2: add multi-party signing test case w/ 100 signers
In this commit, we add a final test case that exercises the act of
generating partial signatures amongst 100 signers, combining them into a
single signature, and finally verifying to make sure the final signature
is valid.
2022-04-28 16:19:47 -07:00
Olaoluwa Osuntokun
d25f072e71
btcec/schnorr/musig2: add test vectors from secp256k1-zkp
In this commit, we add test vectors which are extracted from the
secp256k1-zkp/ codebase and match up with the current draft
specification.
2022-04-28 16:19:44 -07:00
Olaoluwa Osuntokun
bb7ba7b1fc
btcec/schnorr/musig2: add partial sig generation, validation, and combination
In this commit, we build on the prior two commits by adding the ability
to generate partial musig2 signatures, validate them individually, and
finally combine them into a single signature.

Much of the logic here is unoptimized, and will be optimized in a later
commit. In addition, we also want to eventually have a nicer API to
support the book keeping necessary during multi signing.
2022-04-28 16:19:42 -07:00
Olaoluwa Osuntokun
8343e462a6
btcec/schnorr/musig2: add nonce generation & aggregation funcs
In this commit, we add the ability to generate the secret+public nonces,
as well as combine a series of nonces into a single combined nonce
(which is used when doing multi signing).
2022-04-28 16:19:40 -07:00
Olaoluwa Osuntokun
1a65f1ccf0
btcec/schnorr/musig2: add key musig2 key aggregation routines
In this commit, we add the set of key aggregation routines for musig2.
This includes the main public key aggregation method, as well as the
aggregation coefficient which is used to compute "mu" when signing.

The logic in this implementation is based on the musig2 paper, as well
as this spec:
https://github.com/ElementsProject/secp256k1-zkp/blob/master/doc/musig-spec.mediawiki.
2022-04-28 16:19:38 -07:00
Olaoluwa Osuntokun
8c5bfeecf0
Merge pull request #1842 from guggero/psbt-serialization-fix
psbt: always use non witness serialization format
2022-04-18 14:39:48 -07:00
Olaoluwa Osuntokun
bf64c8bdbb
Merge pull request #1812 from Crypt-iQ/btcd_addrv2
multi: implement BIP-155 addrv2 support
2022-04-13 10:25:12 -07:00
Oliver Gugger
eb2eeaf848
psbt: always use non witness serialization format
BIP-0174 states that the transaction must be in the old serialization
format (without witnesses).
2022-04-13 14:13:47 +02:00
vpereira01
e153fefbad Upgrade go docker builder image 2022-04-11 09:22:36 -04:00
vpereira01
b87a4f4835 Add LF to new files last line 2022-04-11 09:22:36 -04:00
vpereira01
1eede8721c Adds GH workflow to build docker images on version tags
Adds a GitHub workflow, and custom docker file, using docker buildx
which builds docker container images for common platforms and publishes
these images to GitHub packages.
2022-04-11 09:22:36 -04:00
Anup Chenthamarakshan
3986702b97 btcd: don't override explicitly set GOGC
If GOGC env var is explicitly set, use it. Otherwise, set GC to 10% (default).
2022-04-09 08:02:00 -04:00
Torkel Rogstad
85b6f7ed2a rpcclient: add getzmqnotifications RPC 2022-04-09 07:46:07 -04:00
Olaoluwa Osuntokun
d537492a5d chaincfg: use lower custom activation threshold for regtest+simnet
The existing values were copied over from the testnet deployment, which
uses a much larger miner confirmation window. As a result, the main
taproot deployment would require thousands of blocks to properly
activate in development environments.
2022-04-09 07:39:56 -04:00
Jonathan Chappelow
d14f18d329 mempool: fix t.Fatal call with formatting directive 2022-04-09 07:38:57 -04:00
Jonathan Chappelow
7eaf360063 btcjson: add addresstype arg to getrawchangeaddress 2022-04-09 07:38:57 -04:00
Jonathan Chappelow
061aef98af btcjson: add addresstype arg to getnewaddress 2022-04-09 07:38:57 -04:00
Harsha Goli
e0149d63a1 rpctest: ensure rpclisten is set to an available port 2022-04-09 07:25:55 -04:00
Tim Kuijsten
3faf68c8c5 go mod tidy 2022-04-09 07:22:22 -04:00
Tim Kuijsten
67aad53f5e harden btcd on OpenBSD
Restrict the available set of system calls to the daemon to the basic
network and filesystem operations on OpenBSD. Further reduce potential
harm by limiting file system access to the btcd data dir and the rpc
files.
2022-04-09 07:22:22 -04:00
dekokun
796f1746b3 btcjson: Update the fields of GetNetworkInfoResult
Update the fields of GetNetworkInfoResult to reflect the current number
of inbound and outbound peer connections.

* ConnectionsIn - The number of inbound peer connections
* ConnectionsOut - The number of outbound peer connections
2022-04-09 07:01:50 -04:00
James Smoot
71c844310a Default to JSONRPC 1.0 if the rpcVersion empty 2022-04-09 06:55:59 -04:00
Olaoluwa Osuntokun
074266215c
Merge pull request #1804 from Crypt-iQ/tor_resolver_fix
Revert "reduce redundant memory allocatio - resolves btcsuite/btcd#1699"
2022-03-30 15:17:28 -05:00
Olaoluwa Osuntokun
b34e376d12
Merge pull request #1834 from guggero/psbt-empty-deriv-path
psbt: allow empty bip32 derivation path
2022-03-29 19:56:26 -05:00
Oliver Gugger
56b048867b
psbt: allow empty bip32 derivation path
BIP-0174 defines the derivation path being encoded as
  "<32-bit uint> <32-bit uint>*"
with the asterisk meaning 0 to n times. Which in turn means that an
empty path is valid, only the key fingerprint is mandatory.
2022-03-28 10:14:43 +02:00
Olaoluwa Osuntokun
558fcde3dd
Merge pull request #1832 from Roasbeef/remove-bip9-stray-logs
blockchain: demote BIP 9 log statements
2022-03-18 16:47:47 -07:00
Olaoluwa Osuntokun
36e67158b2
blockchain: demote BIP 9 log statements
In this commit, we demote a series of log statements added while
debugging the modified BIP 9 state machine. These are rather spammy on
mainnet, so we demote the transition logs (moving to a new state) to
debug, and the remaining log (when we're still in started to trace).
2022-03-17 17:57:37 -07:00
Olaoluwa Osuntokun
8d5c75c289
Merge pull request #1831 from Roasbeef/post-taproot-module-updates
build: update to btcec/v2.1.3 and chaincfg/chainhash v1.0.1
2022-03-16 10:51:02 -07:00
Olaoluwa Osuntokun
c203b940f3
build: update to btcec/v2.1.3 and chaincfg/chainhash v1.0.1
In this commit, we update the top level module to use the newly tagged
sub-modules. Once we remove the circular dependant in these sub-modules,
then we'll no longer have to do things like this.
2022-03-15 18:48:50 -07:00
Olaoluwa Osuntokun
fc36cb25a4
Merge pull request #1787 from Roasbeef/taproot-impl
multi: implement BIP 341 and 342 a.k.a complete taproot and tapscript consensus verification logic
2022-03-15 18:34:59 -07:00
Olaoluwa Osuntokun
30d58b98a1
build: add temporary replace directives for btcec+chainhash
This PR includes some changes to them, so we'll need to use a temporary
replace directives to ensure the build passes.
2022-03-15 18:23:47 -07:00
Olaoluwa Osuntokun
99e4e00345
txscript: add more detailed taproot errors 2022-03-15 18:23:42 -07:00
Olaoluwa Osuntokun
6ab97a3dd8
blockchain: fix IsSpeedy() bug, add more logging in BIP 9 state machine 2022-03-15 18:23:39 -07:00
Olaoluwa Osuntokun
f7f7bb33c1
blockchain/indexers: add P2TR support to the addrindex 2022-03-15 18:23:37 -07:00
Olaoluwa Osuntokun
23cf18b050
blockchain: use taproot script flags for validation after activation 2022-03-15 18:23:34 -07:00
Olaoluwa Osuntokun
ba9fb8ece1
chaincfg: add taproot BIP deployment parameters
In this commit, we add the deployment parameters of taproot as specified
in the deployment section of BIp 341:
https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#deployment.

Take note of the custom activation threshold, as well as the specified
min activation heights for mainnet only.
2022-03-15 18:23:32 -07:00