Skip to content

Commit 3a3e384

Browse files
committed
ecdsa: added VerifyLowS helper function
1 parent 73aeb57 commit 3a3e384

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

btcec/ecdsa/signature.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,21 @@ func RecoverCompact(signature, hash []byte) (*btcec.PublicKey, bool, error) {
254254
func Sign(key *btcec.PrivateKey, hash []byte) *Signature {
255255
return secp_ecdsa.Sign(key, hash)
256256
}
257+
258+
// VerifyLowS verifies that the given ECDSA signature is strictly DER-encoded
259+
// and uses a canonical low-S value. It returns nil if the signature is valid;
260+
// otherwise it returns the encountered error.
261+
func VerifyLowS(sigStr []byte) error {
262+
sig, err := parseSig(sigStr, true)
263+
if err != nil {
264+
return err
265+
}
266+
sValue := sig.S()
267+
if sValue.IsOverHalfOrder() {
268+
// High-S, s > N/2.
269+
return fmt.Errorf("signature is not canonical due to unnecessarily " +
270+
"high S value")
271+
}
272+
// Low-S, s <= N/2.
273+
return nil
274+
}

0 commit comments

Comments
 (0)