Skip to content

Commit

Permalink
style(util): improve VerifySignature function code style
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Oct 21, 2023
1 parent 3bf1725 commit f306246
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions x/logic/util/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,30 @@ func VerifySignature(alg Alg, pubKey []byte, msg, sig []byte) (r bool, err error
case Ed25519:
r = ed25519.Verify(pubKey, msg, sig)
case Secp256r1:
curve := elliptic.P256()
x, y := ecc.UnmarshalCompressed(curve, pubKey)
if x == nil || y == nil {
err = fmt.Errorf("failed to parse compressed public key")
break
}

pk := &ecdsa.PublicKey{
Curve: curve,
X: x,
Y: y,
}

r = ecc.VerifyASN1(pk, msg, sig)
return verifySignatureWithCurve(elliptic.P256(), pubKey, msg, sig)
case Secp256k1:
curve := ecc.P256k1()
x, y := ecc.UnmarshalCompressed(curve, pubKey)
if x == nil || y == nil {
err = fmt.Errorf("failed to parse compressed public key")
break
}

pk := &ecdsa.PublicKey{
Curve: curve,
X: x,
Y: y,
}

r = ecc.VerifyASN1(pk, msg, sig)
return verifySignatureWithCurve(ecc.P256k1(), pubKey, msg, sig)
default:
err = fmt.Errorf("algo %s not supported", alg)
}

return r, err
}

// verifySignatureWithCurve verifies the ASN1 signature of the given message with the given
// public key (in compressed form specified in section 4.3.6 of ANSI X9.62.) using the given
// elliptic curve.
func verifySignatureWithCurve(curve elliptic.Curve, pubKey, msg, sig []byte) (bool, error) {
x, y := ecc.UnmarshalCompressed(curve, pubKey)
if x == nil || y == nil {
return false, fmt.Errorf("failed to parse compressed public key")
}

pk := &ecdsa.PublicKey{
Curve: curve,
X: x,
Y: y,
}

return ecc.VerifyASN1(pk, msg, sig), nil
}

0 comments on commit f306246

Please sign in to comment.