Skip to content

Commit

Permalink
feat(logic): add Secp256k1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Oct 27, 2023
1 parent 1889e84 commit d612819
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
6 changes: 4 additions & 2 deletions x/logic/predicate/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func HexBytes(vm *engine.VM, hexa, bts engine.Term, cont engine.Cont, env *engin
// Examples:
//
// # Verify the signature of given hexadecimal data.
// - eddsa_verify([127, ...], '9b038f8ef6918cbb56040dfda401b56bb1ce79c472e7736e8677758c83367a9d', [23, 56, ...], [encoding(hex), type(ed25519)]).
// - eddsa_verify([127, ...], '9b038f8ef6918cbb56040dfda401b56b...', [23, 56, ...], [encoding(hex), type(ed25519)]).
//
// # Verify the signature of given binary data.
// - eddsa_verify([127, ...], [56, 90, ..], [23, 56, ...], [encoding(octet), type(ed25519)]).
Expand Down Expand Up @@ -158,7 +158,9 @@ func ECDSAVerify(_ *engine.VM, key, data, sig, options engine.Term, cont engine.
// xVerify return `true` if the Signature can be verified as the signature for Data, using the given PubKey for a
// considered algorithm.
// This is a generic predicate implementation that can be used to verify any signature.
func xVerify(functor string, key, data, sig, options engine.Term, defaultAlgo util.Alg, algos []util.Alg, cont engine.Cont, env *engine.Env) *engine.Promise {
func xVerify(functor string, key, data, sig, options engine.Term, defaultAlgo util.Alg,
algos []util.Alg, cont engine.Cont, env *engine.Env,
) *engine.Promise {
typeOpt := engine.NewAtom("type")
return engine.Delay(func(ctx context.Context) *engine.Promise {
typeTerm, err := util.GetOptionWithDefault(typeOpt, options, engine.NewAtom(defaultAlgo.String()), env)
Expand Down
28 changes: 14 additions & 14 deletions x/logic/util/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/x509"
"encoding/pem"
"fmt"

"github.com/dustinxie/ecc"
Expand Down Expand Up @@ -38,32 +36,34 @@ func VerifySignature(alg Alg, pubKey []byte, msg, sig []byte) (r bool, err error
r = ed25519.Verify(pubKey, msg, sig)
case Secp256r1:
curve := elliptic.P256()
x, y := elliptic.UnmarshalCompressed(curve, pubKey)
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 = ecdsa.VerifyASN1(pk, msg, sig)
r = ecc.VerifyASN1(pk, msg, sig)
case Secp256k1:
block, _ := pem.Decode(pubKey)
if block == nil {
err = fmt.Errorf("failed decode PEM public key")
break
}
genericPublicKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
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 := genericPublicKey.(*ecdsa.PublicKey)
if !ecc.VerifyBytes(pk, msg, sig, ecc.Normal) {
return false, nil

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

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

0 comments on commit d612819

Please sign in to comment.