Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix taproot sig validation again. #1935

Merged
merged 2 commits into from
Jun 9, 2023
Merged

Fix taproot sig validation again. #1935

merged 2 commits into from
Jun 9, 2023

Conversation

junderw
Copy link
Member

@junderw junderw commented Jun 7, 2023

Fixes #1934

The last PR made the "should be invalid" sig invalid because I was chopping off the wrong byte of the 65 byte signature.

After fixing it, I noticed we didn't actually fix the previous issue either.

This time we actually fixed the issue.

The problem was the assumption that the transaction is always using the "tweak the internal pubkey with a hash of itself" method.

Removing that assumption should fix the issue.

@motorina0 Please review the source and see my comments below first.

@junderw junderw requested a review from motorina0 June 7, 2023 05:57
@junderw
Copy link
Member Author

junderw commented Jun 7, 2023

Unit tests are broken...

This code is used in signing too...

ts_src/psbt.ts Outdated
@@ -1743,8 +1754,8 @@ function getTaprootHashesForSig(

const hashes = [];
if (input.tapInternalKey && !tapLeafHashToSign) {
const outputKey = tweakInternalPubKey(inputIndex, input);
if (toXOnly(pubkey).equals(outputKey)) {
const outputKey = getPrevoutTaprootKey(inputIndex, input, cache);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is breaking one of the test vectors in the unit test for signing.

The test vector says Sign PSBT with 3 inputs [P2PKH, P2TR (key-path), P2WPKH] and at this point.

This is confusing...

ECPair keypair (pubkey)   4fef5c5163bea69a93e74a59672bbeb081837077cb94cfa6e481a5cf00d8ab18
tweak(tapInternalKey)     4fef5c5163bea69a93e74a59672bbeb081837077cb94cfa6e481a5cf00d8ab18

taproot prevOutput key    9421e734b0f9d2c467ea7dd197c61acb4467cdcbc9f4cb0c571f8b63a5c40cae
input.tapInternalKey      9421e734b0f9d2c467ea7dd197c61acb4467cdcbc9f4cb0c571f8b63a5c40cae

The tapInternalKey and the prevout script key are the same, and the WIF for the keypair is the self-tweaked version of tapInternalKey/prevOutputKey.

I think this test vector is incorrect...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> p.data.inputs[1].witnessUtxo
{
  script: <Buffer 51 20 94 21 e7 34 b0 f9 d2 c4 67 ea 7d d1 97 c6 1a cb 44 67 cd cb c9 f4 cb 0c 57 1f 8b 63 a5 c4 0c ae>,
  value: 67000
}
> p.data.inputs[1].tapInternalKey
<Buffer 94 21 e7 34 b0 f9 d2 c4 67 ea 7d d1 97 c6 1a cb 44 67 cd cb c9 f4 cb 0c 57 1f 8b 63 a5 c4 0c ae>

Output keys are not guaranteed to be tweaked against their pubkey's hash.
That is only a convention decided upon for NUMS properties.
@junderw
Copy link
Member Author

junderw commented Jun 7, 2023

In the end, a taproot signature is a signature with the key in the previous output, so when we call validateSignatures with a pubkey it should be only the prevout script pubkey we care about.

Perhaps we should make another method to verify if a given pubkey will match the prevout script if it is tweaked...?

Anywho... this is a big change so I've decided not to rush the publish.

@junderw
Copy link
Member Author

junderw commented Jun 7, 2023

Re: test vector... how were these generated? btw

@junderw junderw merged commit 1a9119b into master Jun 9, 2023
@junderw
Copy link
Member Author

junderw commented Jun 9, 2023

@motorina0 Please post-publish review if possible. 6.1.2 is broken, so I will release this ASAP.

@junderw junderw deleted the fix/p2tr-sigs-pt2 branch July 16, 2024 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PSBT Taproot Input Validation Issue
1 participant