diff --git a/btcsuite/btcd/btcec/privkey.go b/btcsuite/btcd/btcec/privkey.go index d8c6417f..5fe3164c 100644 --- a/btcsuite/btcd/btcec/privkey.go +++ b/btcsuite/btcd/btcec/privkey.go @@ -72,14 +72,16 @@ func (p *PrivateKey) SignCanonical(curve *KoblitzCurve, hash []byte) ([]byte, er return nil, err } + if !isCanonicalSig(sig.R.Bytes(), sig.S.Bytes()) { + continue + } + compactSig, err := makeCompact(curve, sig, p, hash, true) if err != nil { continue } - if isCanonical(compactSig) { - return compactSig, nil - } + return compactSig, nil } return nil, errors.New("couldn't find a canonical signature") } @@ -110,3 +112,11 @@ func isCanonical(compactSig []byte) bool { t4 := !(d[33] == 0 && ((d[34] & 0x80) == 0)) return t1 && t2 && t3 && t4 } + +func isCanonicalSig(r []byte, s []byte) bool { + t1 := (r[0] & 0x80) == 0 + t2 := !(r[0] == 0 && ((r[1] & 0x80) == 0)) + t3 := (s[0] & 0x80) == 0 + t4 := !(s[0] == 0 && ((s[1] & 0x80) == 0)) + return t1 && t2 && t3 && t4 +}