-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor: split up internal pubkey serialization function into compressed/uncompressed variants #1774
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
base: master
Are you sure you want to change the base?
refactor: split up internal pubkey serialization function into compressed/uncompressed variants #1774
Conversation
w0xlt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approach ACK
real-or-random
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK f5e815f
w0xlt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK f5e815f
furszy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just left a minor nit. No need to tackle it.
| if (secp256k1_pubkey_load(ctx, &Q, pubkey)) { | ||
| ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, !!(flags & SECP256K1_FLAGS_BIT_COMPRESSION)); | ||
| if (ret) { | ||
| *outputlen = len; | ||
| if (flags & SECP256K1_FLAGS_BIT_COMPRESSION) { | ||
| secp256k1_eckey_pubkey_serialize33(&Q, output); | ||
| *outputlen = 33; | ||
| } else { | ||
| secp256k1_eckey_pubkey_serialize65(&Q, output); | ||
| *outputlen = 65; | ||
| } | ||
| ret = 1; | ||
| } | ||
| return ret; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In adb76f8:
nit: could remove ret if you write it as:
if (!secp256k1_pubkey_load(ctx, &Q, pubkey)) return 0;
if (flags & SECP256K1_FLAGS_BIT_COMPRESSION) {
secp256k1_eckey_pubkey_serialize33(&Q, output);
*outputlen = 33;
} else {
secp256k1_eckey_pubkey_serialize65(&Q, output);
*outputlen = 65;
}
return 1;There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense yeah, will do if I have to retouch
This PR splits up the pubkey serialization function
secp256k1_eckey_pubkey_serializeinto two variants for the compressed (33 bytes) and uncompressed (65 bytes) public key output format each, where only non-infinity group elements as input are allowed. The motivation is to simplify call-sites significantly, as they currently need to introduce two variables and a VERIFY_CHECKs on the return value and the in/out size parameter within a pre-processor block, typically leading to 8 lines of code. By using the new functions, the code is reduced to a single line of code that just calls the function (see #1773). This is helpful for already existing modules on master (ellswift, musig) and upcoming ones (silentpayments, see #1765).One drawback is that the public API function
secp256k1_ec_pubkey_serializeis now slightly more complex (we now call one of two functions instead of a single one, depending on whether the compressed flag is set or not), but that should hopefully not be a problem.The commits are intentionally kept small to ease review, happy to squash them if that is preferred.
(Kudos to w0xlt for the initial idea (#1765 (review)) and to real-or-random for the suggestion to split the already existing function (#1773 (comment)).)