Commit Graph

3542 Commits

Author SHA1 Message Date
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
37f8c8ba0a
txscript: add VerifyTaprootLeafCommitment function
In this commit, we add a new function to verify the taproot merkle
commitment of a given tapscript leaf. Along the way we add some helper
functions which can be used to construct a taproot output given the raw
script root.
2022-03-15 18:23:06 -07:00
Olaoluwa Osuntokun
5c4a29b9d1
txscript: introduce new ControlBlock struct along w/ parsing routine
In this commit, we add a new struct to represent the ControlBlock
structure used to feed in the tapscript leaf inclusion proof into the
witness tack. The `ParseControlBlock` parses a would-be control block
and returns an error if it's incorrectly formatted.
2022-03-15 18:23:03 -07:00
Olaoluwa Osuntokun
11dd820740
txscript: add new ScriptHasOpSuccess utility method
We'll use this to examine if a script has any OP_SUCCESS op codes during
pre-processing before we attempt full tapscript execution.
2022-03-15 18:23:01 -07:00
Olaoluwa Osuntokun
a7c3db40af
txscript: use keyBytes instead of ScriptHash for segwit utilities 2022-03-15 18:22:58 -07:00
Olaoluwa Osuntokun
2ac743dc9f
txscript: add VM verification logic for top-level taproot keyspends
In this commit, we add the initial verification logic for top-level
taproot keyspends. Keyspends use the base BIP 341 sighash digest and
don't require any tapscript level functionality for validation.
2022-03-15 18:22:56 -07:00
Olaoluwa Osuntokun
938c1930da
txscript: add new functions for signing a top-level taproot output
In this commit, we add two new functions: one for signing a raw
top-level taproot keyspend, and another for generating a valid witness
for a keyspend.
2022-03-15 18:22:53 -07:00
Olaoluwa Osuntokun
1ac34b75dc
txscript: use new signature verifiers for existing CHECKSIG ops
In this commit, we use the recently added checksig verifiers to validate
signatures for pre-segwit, and segwit v0 scripts.
2022-03-15 18:22:51 -07:00
Olaoluwa Osuntokun
abeaf4e334
txscript: introduce new signatureVerifier interface to abstract over schnorr/ecdsa
In this commit, we add a new signatureVerifier interface that will allow
us to consolidate a lot of code as we'll now have 4 distinct sig+sighash
types to verify:
  1. pre-segwit
  2. segwit v0
  3. segwit v1 (taproot key spend)
  4. tapscript spends

We'll need to be able to handle 3 of the cases for the modified
OP_CHECKSIG operator. This new abstraction allows us to keep the
implementation of the function somewhat succinct.

In this commit we implement a verifier for #3 which is needed to verify
the top-level taproot keyspend. We expose the verifier using a new
VerifyTaprootKeySpend function.
2022-03-15 18:22:48 -07:00
Olaoluwa Osuntokun
1cd509d9a5
txscript: update SigCache to cache both ECDSA and Schnorr signatures
In this commit, we make the sigCache slightly more general in order to
be able to cache both ECDSA and Schnorr signatures. The cache is now
based off of byte slices (the values) rather than the direct objects. We
rely on the fact that the sighash for ecdsa and the schnorr types are
distinct, so we can keep using the same top-level sighash key.

In the future with Go type params, we can use a type param here instead
as they all have an `IsEqual` method.
2022-03-15 18:22:45 -07:00
Olaoluwa Osuntokun
e781b66e2f
txscript: implement BIP 341+342 segwit v1 taproot+tapscript
In this commit, we implement the new BIP 341+342 taproot sighash digest
computation. The digest is similar, but re-orders some fragments and
also starts to commit to the input values of all the transactions in the
SIGHASH_ALL case. A new implicit sighash flag, SIGHASH_DEFAULT has been
added that allows signatures to always be 64-bytes for the common case.

The hashcache has been updated as well to store both the v0 and v1 mid
state hashes. The v0 hashes are a double-sha of the contents, while the
v1 hash is a single sha. As a result, if a transaction spends both v0
and v1 inputs, then we 're able to re-use all the intermediate hashes.

As the sighash computation needs the input values and scripts, we create
an abstraction: the PrevOutFetcher to give the caller flexibility w.r.t
how this is done. We also create a `CannedPrevOutputFetcher` that holds
the information in a map for a single input.

A series of function options are also added to allow re-use of the same
base sig hash calculation for both BIP 341 and 342.
2022-03-15 18:22:43 -07:00
Olaoluwa Osuntokun
6ecc72e5e6
txscript: move sighash computations to new file 2022-03-15 18:22:40 -07:00
Olaoluwa Osuntokun
30d93272a8
txscript: add new IsPayToTaproot utility func 2022-03-15 18:22:38 -07:00
Olaoluwa Osuntokun
81a546bded
wire: export WriteOutPoint to public module 2022-03-15 18:22:35 -07:00
Olaoluwa Osuntokun
cfe801fe2e
txscript: move hash cache mid-state computation to hashcache.go file 2022-03-15 18:22:32 -07:00
Brian Stafford
3ddf1b51a6
txscript: add taproot support to PayToAddrScript
Add taproot address handling in PayToAddrScript. Adds a test and
also some missing tests for p2wsh and p2wpkh addresses.
2022-03-15 18:22:30 -07:00
Brian Stafford
37964e550b
txscript: add some txscript support for parsing taproot scripts 2022-03-15 18:22:27 -07:00
Brian Stafford
bfd0f4a492
txscript: add taproot script type
Add the WitnessV1TaprootTy script class and return it from
GetScriptClass / typeOfScript.

Bump the btcutil dep to leverage new taproot address type.
2022-03-15 18:22:25 -07:00
Olaoluwa Osuntokun
74e9690d0e
Merge pull request #1824 from Roasbeef/update-btcecv2-no-dep
build: update to btcec/v2.1.1 and chaincfg/chainhash/v1.00
2022-03-11 17:04:59 -08:00
Marnix
6aac863a25 Update LICENSE
Update LICENSE to 2022
2022-03-10 10:48:37 -05:00
vpereira01
10cff8777a Fixes coveralls coverage report
With PR #1785 multiple coverage reports are sent to coveralls. This
makes coveralls only consider the last report sent. This fix treat all
reports has parallel reports so they can all be compiled later by coveralls.
2022-03-10 10:44:09 -05:00
Olaoluwa Osuntokun
3baa09f33e
build: update to btcec/v2.1.1 and chaincfg/chainhash/v1.00 2022-03-09 18:54:03 -08:00
Olaoluwa Osuntokun
425ed7c987
Merge pull request #1823 from guggero/go-mod-pain-relief
Remove circular dependency issue between `btcec/v2` and main package
2022-03-09 16:49:17 -08:00
Oliver Gugger
999514c1fe
mod: add TODOs for removing local replaces 2022-03-09 11:58:43 +01:00
Oliver Gugger
46f5eec021
mod: bump btcec/v2 module version 2022-03-09 11:58:43 +01: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
Oliver Gugger
7cc824e9b5
chainhash: make module 2022-03-09 11:57:23 +01:00
Olaoluwa Osuntokun
21b37c0bd0
Merge pull request #1780 from anupcshan/replace-imports
Replace github.com/btcsuite/goleveldb imports with github.com/syndtr/goleveldb
2022-03-08 15:31:08 -08:00
Anup Chenthamarakshan
87e3d7e278 Replace github.com/btcsuite/goleveldb with github.com/syndtr/goleveldb 2022-03-08 10:07:52 -08:00
Olaoluwa Osuntokun
0c6dbfc3ea
Merge pull request #1813 from kcalvinalvin/fix-mempool-estimatefee-bug
mempool/estimatefee: Fix negative index bug
2022-02-24 17:17:38 -08:00
Calvin Kim
2ce1c60ee4 mempool/estimatefee: Fix negative index bug
Fixes a negative index bug that makes the node crash on chain
reorganizations.  The bug is detailed in
github.com/btcsuite/btcd/issues/1660.

A better design than just skipping the transaction would make
the fee estimator more accurate and that should implemented
at a later date.
2022-02-21 15:43:32 +09:00
Olaoluwa Osuntokun
4dc4ff7963
Merge pull request #1805 from Roasbeef/bchd-retract
build: retract bogus tags from btcd fork
2022-02-07 11:10:57 -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
eaf0459ff8
Merge pull request #1801 from Roasbeef/btcec-v2-1-0
build: update to btcec v2.1.0
2022-02-04 13:30:55 -08:00
Olaoluwa Osuntokun
0847f7a476
build: update to btcec v2.1.0
This was the version tagged that created the new `ecdsa` and `schnorr`
packages. Updating these pinned version lets importers properly use
these packages and build. Things build as is since we use replace
directive to point to the latest version _in the repo_ when we build.
2022-02-01 12:52:14 -08:00
Olaoluwa Osuntokun
81fbd9b67e
Merge pull request #1777 from Roasbeef/bip340
btcec/v2: create new schnorr package for BIP-340, move existing ecdsa implementation into new ecdsa package
2022-02-01 12:44:04 -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
0bbc831040
build: run go mod tidy to refresh go.sum for btcutil 2022-01-31 14:25:52 -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
3b3a6fc671
chaincfg: add BIP-340 tagged hash implementation
In this commit, we add an implementation of the BIP-340 tagged hash
scheme. This initial version can be optimized quite a bit, for example,
we can hard code the output of frequently used `sha256(tag)` values and
save two `sha256` invocations.
2022-01-31 14:25:44 -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
a27738721a
Merge pull request #1773 from Roasbeef/dcr-ec
btcec: create new btcec/v2 module that type aliases into the dcrec module
2022-01-26 16:36:21 -08:00
Olaoluwa Osuntokun
b3d263e902
build: use sed to remove v2 path from btcec/coverage.txt
The `goveralls` tool we use to handle code coverage upload seems to not
understand that a `v2 module can exist, without having a v2 file path on
disk. We use a `sed` command to remove the `v2` module prefix so the
tool can reach into the correct file to extract the source code.
2022-01-26 16:32:54 -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
d2960c83cc
build: don't run the integration tests w/ -race 2022-01-26 16:10:20 -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
Olaoluwa Osuntokun
588c0714c3
Merge pull request #1700 from Roasbeef/bip-8-prep
chaincfg+blockchain: abstract/refactor BIP 9 version bits implementation to work w/ BIP 8 block heights
2022-01-26 12:53:38 -08:00