Skip to content

Separate signature hash types#702

Merged
RCasatta merged 5 commits intorust-bitcoin:masterfrom
sanket1729:add_sig_types
Dec 15, 2021
Merged

Separate signature hash types#702
RCasatta merged 5 commits intorust-bitcoin:masterfrom
sanket1729:add_sig_types

Conversation

@sanket1729
Copy link
Copy Markdown
Member

@sanket1729 sanket1729 commented Nov 16, 2021

Fixes #670 . Separates SchnorrSigHashType and LegacySigHashType. Also adds the following new structs:

pub struct SchnorrSig {
    /// The underlying schnorr signature
    pub sig: secp256k1::schnorrsig::Signature,
    /// The corresponding hash type
    pub hash_ty: SchnorrSigHashType,
}

pub struct EcdsaSig {
    /// The underlying DER serialized Signature
    pub sig: secp256k1::Signature,
    /// The corresponding hash type
    pub hash_ty: LegacySigHashType,
}

This code is currently minimal to aid reviews. We can at a later point implement (Encodeable, psbt::Serialize, FromHex, ToHex) etc in follow-up PRs.

Copy link
Copy Markdown
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

Left some comments and questions

@sanket1729 sanket1729 force-pushed the add_sig_types branch 2 times, most recently from 48f15e4 to d6bd475 Compare November 19, 2021 18:55
@sanket1729
Copy link
Copy Markdown
Member Author

Ready for review again.

Copy link
Copy Markdown
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

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

Few ideas :)

Comment on lines 115 to 117
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This could be written as an ok_or_else after the split_last. That would get rid of the explicit pattern match.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also if this is kept, use return Err(...) instead of Err(...)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could early return here and get rid of the else block. Would reduce another indentation level of the code below.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

let (hash_ty, sig) = sl.split_last().ok_or(EcdsaSigError::InvalidEcdsaSig)?;
and get rid of indentation

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not convinced this helps much. Most people would probably blindly try ? and thus start implicitly relying on it.

Copy link
Copy Markdown
Member Author

@sanket1729 sanket1729 Nov 21, 2021

Choose a reason for hiding this comment

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

This is how it is done throughout the library. I never really thought if this was useful or not, but have been mechanically adding it whenever I from From error impl

Copy link
Copy Markdown
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

re-ACK with two small improvements since needs rebase anyway

@sanket1729
Copy link
Copy Markdown
Member Author

Rebased

dr-orlovsky
dr-orlovsky previously approved these changes Nov 23, 2021
Copy link
Copy Markdown
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

ACK ace924e1f7095ea3d5f8d366e43dbc1dd862519b

I think all of my comments are just nits on coding style. One really non-stylish thing is returning more clear error from a schnorr witness signature parser, but again I would not postpone further taproot progress just because of error handling UX.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: probably we create it from a byte value, not u8. I.e. from_byte may be more "consensus-style"

dr-orlovsky
dr-orlovsky previously approved these changes Nov 24, 2021
Copy link
Copy Markdown
Collaborator

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

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

utACK 681acd18a2f16eaca731da93e6f68fdeb28ea6fe

@dr-orlovsky
Copy link
Copy Markdown
Collaborator

Can pls @Kixunil or @RCasatta review this PR such we can move forward with one of these Taproot release blockers?

@Kixunil Kixunil added the one ack PRs that have one ACK, so one more can progress them label Dec 2, 2021
RCasatta
RCasatta previously approved these changes Dec 10, 2021
Copy link
Copy Markdown
Collaborator

@RCasatta RCasatta left a comment

Choose a reason for hiding this comment

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

ACK 681acd18a2f16eaca731da93e6f68fdeb28ea6fe

left a nit, but it's so small I will merge anyway to avoid turnover time

@RCasatta
Copy link
Copy Markdown
Collaborator

can't merge needs rebase

@RCasatta RCasatta added the waiting for author This can only progress if the author responds to a request. label Dec 10, 2021
@sanket1729 sanket1729 dismissed stale reviews from RCasatta and dr-orlovsky via 04c0bc6 December 12, 2021 05:48
@sanket1729 sanket1729 force-pushed the add_sig_types branch 2 times, most recently from 04c0bc6 to 3e693cd Compare December 12, 2021 05:56
@sanket1729
Copy link
Copy Markdown
Member Author

Rebased again

RCasatta
RCasatta previously approved these changes Dec 12, 2021
Copy link
Copy Markdown
Collaborator

@RCasatta RCasatta left a comment

Choose a reason for hiding this comment

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

utACK f60b14e50f2afa849c9019580de8721658d5a99d

@dr-orlovsky
Copy link
Copy Markdown
Collaborator

utACK f60b14e50f2afa849c9019580de8721658d5a99d

Was unable to test commit-wise since rust stable panics on one of the commits due to internal error.

Command that fails:
cargo +stable test --verbose --features=use-serde

Commit that fails: ed40f3d

@RCasatta
Copy link
Copy Markdown
Collaborator

Commit that fails: ed40f3d

I can't reproduce, and is it also out of this PR commit's?

@sanket1729
Copy link
Copy Markdown
Member Author

Rebased again after #721

@RCasatta
Copy link
Copy Markdown
Collaborator

ACK 5769744b74464be67f328e57b3f286a12703f6e6

Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

Would've ACKed but I really don't like throwing away of error information. From my experience, it makes debugging harder.

If there's a rationale why throw it away I'd like to know.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why throw out the errors?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The earlier versions of the PR did capture the underlying errors. I did this in order to

  • Avoid all but one unreachable error states in secp256k1::Error. In this scenario, I did check that only one variant of error was being returned in the corresponding secp functions.
  • It would have required to me have another return error type for empty signature. I don't know if the end-user really cares about this error type when the signature is empty or if the signature is malformed. I want to avoid creating unnecessary error enums that are not useful, but it makes matching with them downstream even harder.

I was recently convinced on rust discord by someone else that I should always capture because in case upstream secp changes error reporting, we might want to capture it. Changing it back to the way it was previously

@sanket1729
Copy link
Copy Markdown
Member Author

@Kixunil captured the underlying error types.

Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK 8361129

let (hash_ty, sig) = sl.split_last()
.ok_or(EcdsaSigError::EmptySignature)?;
let hash_ty = EcdsaSigHashType::from_u32_standard(*hash_ty as u32)
.map_err(|_| EcdsaSigError::NonStandardSigHashType(*hash_ty))?;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

May be nicer to match on NonStandardSigHashType (map_err(|NonStandardSigHashType| EcdsaSigError::NonStandardSigHashType(*hash_ty)) to protect against possible future refactors but not a big deal.

Copy link
Copy Markdown
Collaborator

@RCasatta RCasatta left a comment

Choose a reason for hiding this comment

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

ACK 8361129

@RCasatta RCasatta merged commit 970f574 into rust-bitcoin:master Dec 15, 2021
RCasatta added a commit that referenced this pull request Dec 27, 2021
3eea63e Re-export SigHashType in lib.rs (sanket1729)

Pull request description:

  Using the latest version of rust-bitcoin master on rust-miniscript
  errors on bitcoin::SigHashType not found. In the original PR, I only
  renamed the export to ECDSASigHashType, but original re-export should
  also be there in lib.rs to avoid breaking changes downstream.

  Fixup to #702

  Before this PR,

  ```
     |
  89 |         required: bitcoin::SigHashType,
     |                            ^^^^^^^^^^^ not found in `bitcoin

  ```

  After this PR,

  ```
  warning: use of deprecated type alias `bitcoin::SigHashType`: Please use [`EcdsaSigHashType`] instead
    --> src/psbt/mod.rs:89:28
     |
  89 |         required: bitcoin::SigHashType,

  ```

ACKs for top commit:
  apoelstra:
    ACK 3eea63e
  RCasatta:
    ACK 3eea63e

Tree-SHA512: 9cb8683835828e316ffa782c2a249559b4e534aa251b61667f3512b4e872fd88c726615c626ed9abf2061d31dc815ee77bb3d064962c93d0e0befd722fc2ceeb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

one ack PRs that have one ACK, so one more can progress them P-high High priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Why do we have two SigHashTypes?

6 participants