secp256k1: Return normalized val from DecompressY. #3441
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The result of the
DecompressY
function, as the documentation describes, currently returns a value that either has a maximum magnitude of 1 or 2 which also implies that it is not necessarily normalized either. This means the caller is currently responsible for normalization.While there is no logic issue with that approach, it does mean that callers realistically have to unconditionally normalize the result for almost all realistic use cases even though it might not actually need it.
Those extra normalizations can result in a minor average comparative performance loss when amortized across millions of point decompressions. Further, putting the responsibility on the caller makes it easier make a mistake.
To improve both of those cases, this updates the
DecompressY
function to normalize the result in all cases when the result is a valid point on the secp256k1 curve (aka the function returns true) before returning it and updates the callers accordingly.This change also means the result will now always have a max magnitude of 1.