Skip to content

Conversation

uncomputable
Copy link
Contributor

@uncomputable uncomputable commented Feb 14, 2025

Clarify the conditions when Keypair construction fails and correct the error variant that is returned in one of the constructors. Fixes #758

Clarify the conditions for returning the InvalidSecretKey error.

Fixes rust-bitcoin#758
The constructor of a secret key should return InvalidSecretKey in case
of error. I guess there was a typo / copy-and-paste error that was never
tested.
Copy link
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 aaf5fee

Ok(constants::SECRET_KEY_SIZE) =>
Keypair::from_seckey_slice(secp, &res[0..constants::SECRET_KEY_SIZE]),
_ => Err(Error::InvalidPublicKey),
_ => Err(Error::InvalidSecretKey),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh yes, the entire error type needs a lot of work...

Copy link
Member

Choose a reason for hiding this comment

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

I think we should move deserialization/serialization into Rust and then we can provide sensible error types. Obviously in this case the variant was totally wrong, but in general the error types in this library are crappy because they're just thin wrappers around the numeric error returns from the C code.

Copy link
Member

Choose a reason for hiding this comment

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

...lol, and half this API is "take a &[u8] then length-check it" and should be replaced by an array-based on.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ye, I think we need to do all checks except "is on the curve"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I remember that there was an initiative to replace slice-based methods with array-based ones. IDK if this was Miniscript or secp. In any case, I'm happy to write a follow-up PR to move the constructors to array parameters.

Copy link
Member

Choose a reason for hiding this comment

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

@uncomputable that would be awesome.

If you want a fun weekend project you can also try to pull some parts out of #703 to replace point deserialization code with Rust. (That PR is out of sync and also IMO it does too much at once; we should practice Rust-ifying a couple functions and defining sensible APIs and error types.)

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK aaf5fee; successfully ran local tests

@apoelstra apoelstra merged commit 0ecd2a2 into rust-bitcoin:master Feb 15, 2025
29 of 30 checks passed
@uncomputable uncomputable deleted the 2025-02-fix-xonly-docs branch February 15, 2025 19:29
chain-forgexcr45 added a commit to chain-forgexcr45/rust-secp256k1 that referenced this pull request Sep 28, 2025
aaf5fee92e4200bb42c72cd67dd42b3dde07fb12 fix: Invalid secret key (Christian Lewe)
d1d4ff9f01695526c01198dee643d273f0c32b9d doc: Keypair constructors accept odd keys (Christian Lewe)

Pull request description:

  Clarify the conditions when `Keypair` construction fails and correct the error variant that is returned in one of the constructors. Fixes #758

ACKs for top commit:
  Kixunil:
    ACK aaf5fee92e4200bb42c72cd67dd42b3dde07fb12
  apoelstra:
    ACK aaf5fee92e4200bb42c72cd67dd42b3dde07fb12; successfully ran local tests

Tree-SHA512: 9b27c4ee1424619f9041183801e7c1214838f0b8d529e9c10a3e1f55264624b6b42b881e369bd9521bfa117d3dd5ce072caed4b8ee51c40e1016fc5be59a2894
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.

Wrong doc for creating Keypair
3 participants