func NewBBSG2SignatureVerifier() *BBSG2SignatureVerifier {
	return &BBSG2SignatureVerifier{
		baseSignatureVerifier{
			keyType:   "EC",
			curve:     "BLS12381_G2",
			algorithm: "",
		},
	}
}
NewBBSG2SignatureVerifier creates a new BBSG2SignatureVerifier.
package verifier
import (
	"crypto"
	"crypto/ecdsa"
	"crypto/ed25519"
	"crypto/elliptic"
	"crypto/rsa"
	"crypto/x509"
	"errors"
	"fmt"
	"math/big"
	"strings"
	"github.com/btcsuite/btcd/btcec"
	gojose "github.com/square/go-jose/v3"
	"github.com/hyperledger/aries-framework-go/pkg/crypto/primitive/bbs12381g2pub"
	"github.com/hyperledger/aries-framework-go/pkg/doc/jose/jwk"
)
type PublicKeyVerifier struct {
	exactType      string
	singleVerifier SignatureVerifier
	verifiers      []SignatureVerifier
}
type PublicKeyVerifierOpt func(opts *PublicKeyVerifier)
func NewPublicKeyVerifier(sigAlg SignatureVerifier, opts ...PublicKeyVerifierOpt) *PublicKeyVerifier {
	v := &PublicKeyVerifier{
		singleVerifier: sigAlg,
	}
	for _, opt := range opts {
		opt(v)
	}
	return v
}
func NewCompositePublicKeyVerifier(verifiers []SignatureVerifier, opts ...PublicKeyVerifierOpt) *PublicKeyVerifier {
	v := &PublicKeyVerifier{
		verifiers: verifiers,
	}
	for _, opt := range opts {
		opt(v)
	}
	return v
}
func (pkv *PublicKeyVerifier) Verify(pubKey *PublicKey, msg, signature []byte) error {
	if pkv.exactType != "" {
		if pubKey.Type != pkv.exactType {
			return fmt.Errorf("a type of public key is not '%s'", pkv.exactType)
		}
	}
	if pkv.singleVerifier != nil {
		if pubKey.JWK != nil && !pkv.matchVerifier(pkv.singleVerifier, pubKey.JWK) {
			return errors.New("verifier does not match JSON Web Key")
		}
		return pkv.singleVerifier.Verify(pubKey, msg, signature)
	}
	for _, v := range pkv.verifiers {
		if pkv.matchVerifier(v, pubKey.JWK) {
			return v.Verify(pubKey, msg, signature)
		}
	}
	return errors.New("no matching verifier found")
}
func (pkv *PublicKeyVerifier) matchVerifier(verifier SignatureVerifier, j *jwk.JWK) bool {
	if verifier.KeyType() != j.Kty {
		return false
	}
	if j.Crv != "" && verifier.Curve() != j.Crv {
		return false
	}
	if j.Algorithm != "" && verifier.Algorithm() != j.Algorithm {
		return false
	}
	return true
}
func WithExactPublicKeyType(jwkType string) PublicKeyVerifierOpt {
	return func(opts *PublicKeyVerifier) {
		opts.exactType = jwkType
	}
}
type SignatureVerifier interface {
	KeyType() string
	Curve() string
	Algorithm() string
	Verify(pubKey *PublicKey, msg, signature []byte) error
}
type baseSignatureVerifier struct {
	keyType   string
	curve     string
	algorithm string
}
func (sv baseSignatureVerifier) KeyType() string {
	return sv.keyType
}
func (sv baseSignatureVerifier) Curve() string {
	return sv.curve
}
func (sv baseSignatureVerifier) Algorithm() string {
	return sv.algorithm
}
type Ed25519SignatureVerifier struct {
	baseSignatureVerifier
}
func NewEd25519SignatureVerifier() *Ed25519SignatureVerifier {
	return &Ed25519SignatureVerifier{
		baseSignatureVerifier: baseSignatureVerifier{
			keyType:   "OKP",
			curve:     "Ed25519",
			algorithm: "EdDSA",
		},
	}
}
func (sv Ed25519SignatureVerifier) Verify(pubKey *PublicKey, msg, signature []byte) error {
	value := pubKey.Value
	if pubKey.JWK != nil {
		var ok bool
		value, ok = pubKey.JWK.Public().Key.(ed25519.PublicKey)
		if !ok {
			return fmt.Errorf("public key not ed25519.VerificationMethod")
		}
	}
	if len(value) != ed25519.PublicKeySize {
		return errors.New("ed25519: invalid key")
	}
	verified := ed25519.Verify(value, msg, signature)
	if !verified {
		return errors.New("ed25519: invalid signature")
	}
	return nil
}
type RSAPS256SignatureVerifier struct {
	baseSignatureVerifier
}
func NewRSAPS256SignatureVerifier() *RSAPS256SignatureVerifier {
	return &RSAPS256SignatureVerifier{
		baseSignatureVerifier: baseSignatureVerifier{
			keyType:   "RSA",
			algorithm: "PS256",
		},
	}
}
func (sv RSAPS256SignatureVerifier) Verify(key *PublicKey, msg, signature []byte) error {
	pubKey, err := x509.ParsePKCS1PublicKey(key.Value)
	if err != nil {
		return errors.New("rsa: invalid public key")
	}
	hash := crypto.SHA256
	hasher := hash.New()
	_, err = hasher.Write(msg)
	if err != nil {
		return errors.New("rsa: hash error")
	}
	hashed := hasher.Sum(nil)
	err = rsa.VerifyPSS(pubKey, hash, hashed, signature, nil)
	if err != nil {
		return errors.New("rsa: invalid signature")
	}
	return nil
}
const (
	p256KeySize      = 32
	p384KeySize      = 48
	p521KeySize      = 66
	secp256k1KeySize = 32
)
type ECDSASignatureVerifier struct {
	baseSignatureVerifier
	ec ellipticCurve
}
func (sv *ECDSASignatureVerifier) Verify(pubKey *PublicKey, msg, signature []byte) error {
	pubKeyJWK := pubKey.JWK
	if pubKeyJWK == nil {
		j, err := sv.createJWK(pubKey.Value)
		if err != nil {
			return fmt.Errorf("ecdsa: create JWK from public key bytes: %w", err)
		}
		pubKeyJWK = j
	}
	ec := sv.ec
	ecdsaPubKey, ok := pubKeyJWK.Key.(*ecdsa.PublicKey)
	if !ok {
		return errors.New("ecdsa: invalid public key type")
	}
	if len(signature) != 2*ec.keySize {
		return errors.New("ecdsa: invalid signature size")
	}
	hasher := ec.hash.New()
	_, err := hasher.Write(msg)
	if err != nil {
		return errors.New("ecdsa: hash error")
	}
	hash := hasher.Sum(nil)
	r := big.NewInt(0).SetBytes(signature[:ec.keySize])
	s := big.NewInt(0).SetBytes(signature[ec.keySize:])
	verified := ecdsa.Verify(ecdsaPubKey, hash, r, s)
	if !verified {
		return errors.New("ecdsa: invalid signature")
	}
	return nil
}
func (sv *ECDSASignatureVerifier) createJWK(pubKeyBytes []byte) (*jwk.JWK, error) {
	curve := sv.ec.curve
	x, y := elliptic.Unmarshal(curve, pubKeyBytes)
	if x == nil {
		return nil, errors.New("invalid public key")
	}
	ecdsaPubKey := &ecdsa.PublicKey{
		Curve: curve,
		X:     x,
		Y:     y,
	}
	return &jwk.JWK{
		JSONWebKey: gojose.JSONWebKey{
			Key:       ecdsaPubKey,
			Algorithm: sv.algorithm,
		},
		Kty: sv.keyType,
		Crv: sv.curve,
	}, nil
}
func NewECDSASecp256k1SignatureVerifier() *ECDSASignatureVerifier {
	return &ECDSASignatureVerifier{
		baseSignatureVerifier: baseSignatureVerifier{
			keyType:   "EC",
			curve:     "secp256k1",
			algorithm: "ES256K",
		},
		ec: ellipticCurve{
			curve:   btcec.S256(),
			keySize: secp256k1KeySize,
			hash:    crypto.SHA256,
		},
	}
}
func NewECDSAES256SignatureVerifier() *ECDSASignatureVerifier {
	return &ECDSASignatureVerifier{
		baseSignatureVerifier: baseSignatureVerifier{
			keyType:   "EC",
			curve:     "P-256",
			algorithm: "ES256",
		},
		ec: ellipticCurve{
			curve:   elliptic.P256(),
			keySize: p256KeySize,
			hash:    crypto.SHA256,
		},
	}
}
func NewECDSAES384SignatureVerifier() *ECDSASignatureVerifier {
	return &ECDSASignatureVerifier{
		baseSignatureVerifier: baseSignatureVerifier{
			keyType:   "EC",
			curve:     "P-384",
			algorithm: "ES384",
		},
		ec: ellipticCurve{
			curve:   elliptic.P384(),
			keySize: p384KeySize,
			hash:    crypto.SHA384,
		},
	}
}
func NewECDSAES521SignatureVerifier() *ECDSASignatureVerifier {
	return &ECDSASignatureVerifier{
		baseSignatureVerifier: baseSignatureVerifier{
			keyType:   "EC",
			curve:     "P-521",
			algorithm: "ES521",
		},
		ec: ellipticCurve{
			curve:   elliptic.P521(),
			keySize: p521KeySize,
			hash:    crypto.SHA512,
		},
	}
}