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

Modify the function of SignCanonical in btcec, do makeCompact after isCanonicalSig #217

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions btcsuite/btcd/btcec/privkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ func (p *PrivateKey) SignCanonical(curve *KoblitzCurve, hash []byte) ([]byte, er
return nil, err
}

compactSig, err := makeCompact(curve, sig, p, hash, true)
if err != nil {
if !isCanonicalSig(sig.R.Bytes(), sig.S.Bytes()) {
continue
}

if isCanonical(compactSig) {
return compactSig, nil
compactSig, err := makeCompact(curve, sig, p, hash, true)
if err != nil {
continue
}
return compactSig, nil
}
return nil, errors.New("couldn't find a canonical signature")
}
Expand Down Expand Up @@ -110,3 +111,14 @@ func isCanonical(compactSig []byte) bool {
t4 := !(d[33] == 0 && ((d[34] & 0x80) == 0))
return t1 && t2 && t3 && t4
}

// add function for original signature, do isCanonical before compactSig
func isCanonicalSig(r []byte, s []byte) bool {
rlen := len(r)
slen := len(s)
t1 := rlen < 32 || (r[0] & 0x80) == 0
t2 := !( (rlen < 32 || r[0] == 0) && ((rlen >= 31 && (r[rlen-31] & 0x80) == 0) || rlen < 31) )
t3 := slen < 32 || (s[0] & 0x80) == 0
t4 := !( (slen < 32 || s[0] == 0) && ((slen >= 31 && (s[slen-31] & 0x80) == 0) || slen < 31) )
return t1 && t2 && t3 && t4
}
Loading