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

Adding KeyPair::serialize_sec. Closes #298 #308

Merged
merged 2 commits into from
Sep 8, 2021

Conversation

dr-orlovsky
Copy link
Contributor

No description provided.

Comment on lines 216 to 217
pub fn serialize_sec<V: Verification>(&self, secp: &Secp256k1<V>) -> [u8; constants::SECRET_KEY_SIZE] {
*SecretKey::from_keypair(secp, self).as_ref()
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't really require a context, See #306 (review)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@dr-orlovsky dr-orlovsky added this to In review in Taproot Jun 20, 2021
@dr-orlovsky dr-orlovsky requested a review from elichai June 23, 2021 12:49
Taproot automation moved this from In review to Ready for merge Jun 23, 2021
Copy link
Member

@elichai elichai left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +214 to +218
/// Serialize the key pair as a secret key byte value
#[inline]
pub fn serialize_secret(&self) -> [u8; constants::SECRET_KEY_SIZE] {
*SecretKey::from_keypair(self).as_ref()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I know that adding this function was the original idea of this PR.

I am wondering on whether or not this is still worth to have given the now very convenient functions of converting a KeyPair into a SecretKey. Working with From and Into can sometimes be annoying though - esp. when chaining methods together - because the compiler sometimes requires type annotations.

If serializing a KeyPair is the ultimate goal, what about the following API:

impl KeyPair {
	pub fn secret_key(&self) -> SecretKey;
	pub fn public_key(&self) -> PublicKey;
}

These are easily accessible, will never require type hints but are more general than providing the secret-key bytes directly. As such, they compose better with the existing functionality. Serializing a KeyPair is then just a matter of:

let bytes = keypair.secret_key().as_ref();

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 overall does not like the current API on getting byte representation of different key types, which uses very distinct names and approaches and not cohesive at all (serialize* in one cases, [..] or as_ref() in other)...

I do not have any preference here other then overhauling the whole key API, which is not a task of this PR. As regarding this PR I will be happy to update it to whatever API will be supported by @apoelstra, @elichai, you and other lib mantainers.

@dr-orlovsky
Copy link
Contributor Author

@apoelstra any other suggestions regarding #308 (comment) for this PR or it can be merged?

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 8ee4e05

I agree this contributes even further to the mess of serialize methods that have different names and different allocation behavior.

In general I'm okay with this library being a bit weird/inconsistent, because it has pretty strong requirements to be zero-allocation and has to follow the contours of the underlying cryptography, and in this case deal with conflicting viewpoints about how accessible raw secret data should be.

Also note this is a breaking change because of the removal of the context requirement from from_keypair.

@apoelstra apoelstra merged commit c72e7cc into rust-bitcoin:master Sep 8, 2021
Taproot automation moved this from Ready for merge to Done Sep 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants