btcec/pubkey: normalize sqrt(x^3) before checking parity

This commit fixes an issue introduced in the recent #1429, where
the output of SqrtVal is not normalized before using IsOdd() to compare
with the expected parity of the y-coordinate. The IsOdd() is only
guaranteed to work if the value has been denormalized, so a denormalized
sqrt >= p would report the opposite parity. We fix this by normalizing
both after compute sqrt(x^3) and when negating the root as directed by
the ybit.
This commit is contained in:
Conner Fromknecht 2019-10-10 18:00:37 -07:00
parent 988181ef23
commit 069ec701df
No known key found for this signature in database
GPG key ID: E7D737B67FA592C7

View file

@ -38,11 +38,10 @@ func decompressPoint(curve *KoblitzCurve, bigX *big.Int, ybit bool) (*big.Int, e
// but this was replaced by the algorithms referenced in
// https://bitcointalk.org/index.php?topic=162805.msg1712294#msg1712294
var y fieldVal
y.SqrtVal(&x3)
y.SqrtVal(&x3).Normalize()
if ybit != y.IsOdd() {
y.Negate(1)
y.Negate(1).Normalize()
}
y.Normalize()
// Check that y is a square root of x^3 + B.
var y2 fieldVal