Commit Graph

103 Commits

Author SHA1 Message Date
Olaoluwa Osuntokun
0f49e10006
Merge pull request #1866 from darioush/bump-btcutils-versions
Bump btcd version in btcutil package
2022-08-10 17:21:39 -07:00
sputn1ck
06ce9608aa
btcec/schnorr/musig2: add infinity testvectors
This commit adds the testvectors from
20ba03106d
2022-08-09 06:44:42 +02:00
sputn1ck
44eb8c64f8
btcec/schnorr/musig2: Allow infinity nonces
This commit updates the musig2 module to allow
infinity nonces, as per Musig2 0.4.0.
2022-08-09 06:44:25 +02:00
sputn1ck
4b2fe9f83e
btcec/schnorr/musig2: Add CombineSig testvectors
This commit adds the testvectors from
cdc3520c07
2022-08-04 11:55:19 +02:00
sputn1ck
5e960074ea
btcec/schnorr/musig2: Add PartialSigVerify testvectors
Adds testvectors from
ebb6a7454b
and
6788ee5412
2022-08-04 11:51:02 +02:00
sputn1ck
85356e8174
btcec/schnorr/musig2: Throw error on invalid partial sig 2022-08-04 11:49:29 +02:00
sputn1ck
53f47d65f1
btcec/schnorr/musig2: Add Sign test vectors
This commit adds the testvectors from
4c06f31daf
to the testcases
2022-08-04 11:49:17 +02:00
sputn1ck
8b59e7aa6b
btcec/schnorr/musig2: Add AggregateNonce testvectors
This commit adds the testvectors from
0ec2aefdaa
to the testcases
2022-08-04 11:46:52 +02:00
sputn1ck
1b85a60b6d
btcec/schnorr/musig2: Add AggregateKeys testvectors
This commit adds the testvectors from
20f60b0f37
to the testcases
2022-08-04 11:44:25 +02:00
sputn1ck
478a2f78c4
btcec/schnorr/musig2: Add nonce generation testcases
This commit adds the testcases specified under version 0.3.1 from
https://github.com/jonasnick/bips/blob/musig2/bip-musig2.mediawiki#change-log

and the fixes from
79438fd604
2022-08-04 11:38:20 +02:00
sputn1ck
4ad819e7af
btcec/schnorr/musig2: Update to MuSig 0.3.0
This commit changes the i's in GenNonces to 0 and 1 as per
https://github.com/jonasnick/bips/blob/musig2/bip-musig2.mediawiki#change-log
0.3
2022-08-04 11:34:10 +02:00
sputn1ck
3376655b9c
btcec/schnorr/musig2: XOR rand with secret key
This commit XORs the secret key (if a secret key is specified)
with the random bytes as per MuSig2 Spec
(https://github.com/jonasnick/bips/blob/musig2/bip-musig2.mediawiki#nonce-generation-1)
2022-08-04 11:32:57 +02:00
Darioush Jalali
149b0f09aa
go mod tidy in btcetc/ 2022-07-11 16:36:12 -07:00
Emmanuel T Odeke
4550049281 fuzz: add ParsePubKey fuzzer
Courtesy of the Cosmos Network security team, this change
adds a fuzzer to ParsePubKey, given that Tendermint uses
this code.
2022-05-05 20:35:35 -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
6fc4199ee4
txscript: add new RawTxInTapscriptSignature to generate tapsript sigs
In this commit, we add a new function `RawTxInTapscriptSignature` that
will be used to generate signatures in the _tapscript_ context. Note
that this differs from top-level taproot as a distinct sighash is used,
and we _always_ accept a root hash to perform the proper tweak.
2022-03-15 18:23:08 -07:00
Olaoluwa Osuntokun
3baa09f33e
build: update to btcec/v2.1.1 and chaincfg/chainhash/v1.00 2022-03-09 18:54:03 -08:00
Oliver Gugger
56dfa0f7e2
mod: add temporary replace 2022-03-09 11:58:42 +01:00
Oliver Gugger
4ad74cd4c2
mod: use chaincfg/chainhash module 2022-03-09 11:58:39 +01:00
Anup Chenthamarakshan
87e3d7e278 Replace github.com/btcsuite/goleveldb with github.com/syndtr/goleveldb 2022-03-08 10:07:52 -08:00
Olaoluwa Osuntokun
7572beb481
build: retract bogus tags from btcd fork
Fixes https://github.com/btcsuite/btcd/issues/1791.
2022-02-04 15:54:43 -08:00
Olaoluwa Osuntokun
b7a4622a6b
btcec+chaincfg: use pre-computed tag hash values
In this commit, we optimize our signature implementation slightly, by
defining pre-computed sha256(tag) variables for the commonly used
values.  If a tag matches this, then we'll use that hash value to avoid
an extra round of hashing.
2022-01-31 14:55:08 -08:00
Olaoluwa Osuntokun
973fb37600
btcec/schnorr: add benchmarks for sign/verify
Benchmarks run w/o fast sign (always verify after you generate a sig):
```
goos: darwin
goarch: amd64
pkg: github.com/btcsuite/btcd/btcec/v2/schnorr
cpu: VirtualApple @ 2.50GHz
BenchmarkSigVerify-8     	    8000	    152468 ns/op	     960 B/op	      16 allocs/op
BenchmarkSign-8          	    4939	    215489 ns/op	    1408 B/op	      27 allocs/op
BenchmarkSignRfc6979-8   	    5106	    217416 ns/op	    2129 B/op	      37 allocs/op
PASS
ok  	github.com/btcsuite/btcd/btcec/v2/schnorr	4.629s
```

Benchmarks w/ fast sign:
```
goos: darwin
goarch: amd64
pkg: github.com/btcsuite/btcd/btcec/v2/schnorr
cpu: VirtualApple @ 2.50GHz
BenchmarkSigVerify-8     	    7982	    142826 ns/op	     960 B/op	      16 allocs/op
BenchmarkSign-8          	   18210	     65908 ns/op	     496 B/op	      12 allocs/op
BenchmarkSignRfc6979-8   	   16537	     78161 ns/op	    1216 B/op	      22 allocs/op
PASS
ok  	github.com/btcsuite/btcd/btcec/v2/schnorr	5.418s
```
2022-01-31 14:40:35 -08:00
Olaoluwa Osuntokun
d6d38ad4ae
btcec/v2/schnorr: add initial BIP-340 schnorr sig implementation
In this commit, we add an initial implementation of BIP-340. Mirroring
the recently added `ecsda` package, we create a new `schnorr` package
with a unique `Signature` type and `ParsePubkey` function. The new
`Signature` type implements the fixed-sized 64-byte signatures, and the
`ParsePubkey` method only accepts pubkeys that are 32-bytes in length,
with an implicit sign byte.

The signing implementation by default, deviates from BIP-340 as it opts
to use rfc6979 deterministic signatures by default, which means callers
don't need to always pass in their own `auxNonce` randomness. A set of
functional arguments allows callers to pass in their own value, which is
the way all the included test vectors function.

The other optional functional argument added is the `FastSign` option
that allows callers to skip the final step of verifying each signature
they generate.
2022-01-31 14:25:47 -08:00
Olaoluwa Osuntokun
eb61742c5d
btcec/v2: create new ecdsa package
In this commit, we create a new package to house the ECDSA-specific
logic in the new `btcec/v2` pacakge. Thsi c hange is meant to mirror the
structure of the `dcrec` package, as we'll soon slot in our own custom
BIP-340 implementation.
2022-01-31 14:25:39 -08:00
Olaoluwa Osuntokun
3ee1211e78
btcec/v2: retain copyright notices from decred developers for all files 2022-01-26 16:10:26 -08:00
Olaoluwa Osuntokun
eee3c3b337
multi: switch project over to using btcec/v2 2022-01-26 16:10:17 -08:00
Olaoluwa Osuntokun
87e8fe92c9
btcec: convert package into go module, alias to dcrec
In this commit, we turn the package into a new Go module (version 2),
and then port over the current set of types and functions to mainly
alias to the more optimized and maintained dcrec variant.

Taking a look at the benchmarks, most operations other than
normalization (which IIRC is a bit slower now due to constant time
fixes) enjoy some nice speeds up:
```
benchcmp is deprecated in favor of benchstat: https://pkg.go.dev/golang.org/x/perf/cmd/benchstat
benchmark                            old ns/op     new ns/op     delta
BenchmarkAddJacobian-8               464           328           -29.20%
BenchmarkAddJacobianNotZOne-8        1138          372           -67.27%
BenchmarkScalarBaseMult-8            47336         31531         -33.39%
BenchmarkScalarBaseMultLarge-8       42465         32057         -24.51%
BenchmarkScalarMult-8                123355        117579        -4.68%
BenchmarkNAF-8                       582           168           -71.12%
BenchmarkSigVerify-8                 175414        120794        -31.14%
BenchmarkFieldNormalize-8            23.8          24.4          +2.39%
BenchmarkParseCompressedPubKey-8     24282         10907         -55.08%
```
2022-01-26 16:10:14 -08:00
Marius van der Wijden
5e6736aad5 btcec: added testcase for point at infinity 2021-09-13 15:59:28 -04:00
Marius van der Wijden
73f7eac903 btcec: check if recovered pk is at point of infinity 2021-09-13 15:59:28 -04:00
Jake Sylvestre
d08785547a docs: update shields 2021-03-05 07:45:19 -05:00
Anirudha Bose
31b66488b4 btcec: validate R and S signature components in RecoverCompact 2021-02-09 09:43:01 -05:00
Hanjun Kim
7cbf95675a btcec: add a comment indicating where curve name taken from
Related with #1565
2020-09-08 09:37:33 -04:00
Hanjun Kim
8facfdd04d btcec: set curve name in CurveParams
Set curve name(secp256k1) in KoblitzCurve.CurveParams

Fixes #1564
2020-09-08 09:37:33 -04:00
Anirudha Bose
d28c7167a5 btcec: Avoid panic in fieldVal.SetByteSlice for large inputs
The implementation has been adapted from the dcrec module in dcrd. The
bug was initially fixed in decred/dcrd@3d9cda1 while transitioning to a
constant time algorithm. A large set of test vectors were subsequently
added in decred/dcrd@8c6b52d.

The function signature has been preserved for backwards compatibility.
This means that returning whether the value has overflowed, and the
corresponding test vectors have not been backported.

This fixes #1170 and closes a previous attempt to fix the bug in #1178.
2020-07-13 09:43:36 -04:00