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 broken serde::Deserialize and FromStr impl of keyPair #492

Merged
merged 2 commits into from
Oct 25, 2022

Conversation

elsirion
Copy link
Contributor

Fixes #491

@apoelstra
Copy link
Member

apoelstra commented Oct 21, 2022

When global-context is not enabled, can you switch to alternate code that creates a context inline?

Maybe we don't want to do that -- but disabling this at compile-time is a breaking change, so I'd rather first merge the non-breaking version (and backport it to several other versions) first, so that we can do a minor release. We can later remove the inefficient version if that's what we decide to do.

@elsirion
Copy link
Contributor Author

I also just noticed that my PR #379 (adding a serde module for KeyPair) was completely redundant?! I must have depended on an old release of libsecp and just upstreamed the fix I applied in Fedimint without noticing @dr-orlovsky had implemented decoding directly in the meantime.

src/key.rs Outdated
Comment on lines 1060 to 1064
#[cfg(feature = "global-context")]
let ctx = SECP256K1;

#[cfg(not(feature = "global-context"))]
let ctx = Secp256k1::signing_only();
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 wish I could deduplicate that code, but the global context is of type Secp256k1<All> while we apparently only need a signing context, so I can't easily build a function returning Cow<'static, Secp256k1<_>> (it would change its signature depending on the feature flags which is bad imo).

How much slower is creating an All context?

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand the question. What code needs to be deduplicated?

Copy link
Contributor Author

@elsirion elsirion Oct 23, 2022

Choose a reason for hiding this comment

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

The code highlighted with the comment which initializes cfg depending on the feature set. At first I had a get_context() -> Cow<'static, Secp256k1<Signing>> function for that till I noticed the global context type is incompatible.

It's not a big deal, I just noticed that I always had to fix little problems in both FromStr and Deserialize.

@elsirion elsirion force-pushed the 2022-10-serde-fix branch 2 times, most recently from e5cc609 to 9d5e477 Compare October 22, 2022 22:16
@apoelstra
Copy link
Member

Could you add a unit test for the serde impl as well?

@elsirion elsirion force-pushed the 2022-10-serde-fix branch 2 times, most recently from 8064ed5 to 0451abf Compare October 23, 2022 18:13
src/key.rs Outdated
@@ -1053,13 +1053,18 @@ impl<'a> From<&'a KeyPair> for PublicKey {
}
}

#[cfg(feature = "alloc")]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The alloc requirement due to calling Secp256k1::signing_only() is a bit annoying, but better than the global-context one

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should have a non-alloc non-global-context version that just returns an error 100% of the time.

My goal here is really just that we don't break compilation for anybody, while replacing the current "always aborts the process" code with something else...I don't care too much what else since the current behavior is so bad :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code has gotten a bit hairy, but I think it should have the desired behavior now.

@elsirion elsirion force-pushed the 2022-10-serde-fix branch 2 times, most recently from a7186ca to 73990f2 Compare October 24, 2022 00:05
@apoelstra
Copy link
Member

Lol! Ok, I'll accept this. I think we should return an actual error rather than panicking, but a panic is certainly better than an abort.

@apoelstra
Copy link
Member

Actually, could you change the panic message to include "please enable the global-context feature of rust-secp256k1"?

apoelstra
apoelstra previously approved these changes Oct 24, 2022
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 73990f2

@elsirion
Copy link
Contributor Author

Added the hint to turn on global-context.

@elsirion
Copy link
Contributor Author

Lol! Ok, I'll accept this. I think we should return an actual error rather than panicking, but a panic is certainly better than an abort.

Panicking re-creates the existing behavior. If you actually want to have a proper error I think that should work too (with some more cfg attributes on the return values).

apoelstra
apoelstra previously approved these changes Oct 24, 2022
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 53c1354

@apoelstra
Copy link
Member

@elsirion sorry, one more ask -- can you add a commit which bumps the minor version number of this crate and adds a CHANGELOG entry for this?

We have fixed a bug in `KeyPair` decoding and otherwise didn't change the API.
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 1f327b4

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.

KeyPair FromStr and Deserialize impls crash
2 participants