/// <summary>
        /// Create a blinded commitment of messages for use in producing a blinded BBS+ signature
        /// </summary>
        /// <param name="request">Request for producing the blinded commitment</param>
        /// <returns></returns>
        public static BlindedCommitment CreateBlindedCommitment(CreateBlindedCommitmentRequest request)
        {
            using var context = new UnmanagedMemoryContext();

            var handle = Native.bbs_blind_commitment_context_init(out var error);

            context.ThrowIfNeeded(error);

            foreach (var item in request.Messages)
            {
                Native.bbs_blind_commitment_context_add_message_string(handle, item.Index, item.Message, out error);
                context.ThrowIfNeeded(error);
            }

            Native.bbs_blind_commitment_context_set_nonce_string(handle, request.Nonce, out error);
            context.ThrowIfNeeded(error);

            Native.bbs_blind_commitment_context_set_public_key(handle, context.ToBuffer(request.PublicKey), out error);
            context.ThrowIfNeeded(error);

            Native.bbs_blind_commitment_context_finish(handle, out var commitment, out var outContext, out var blindingFactor, out error);
            context.ThrowIfNeeded(error);

            return(new BlindedCommitment(context.ToByteArray(outContext), context.ToByteArray(blindingFactor), context.ToByteArray(commitment)));
        }
        /// <summary>
        /// Creates the proof asynchronous.
        /// </summary>
        /// <param name="myKey">My key.</param>
        /// <param name="nonce">The nonce.</param>
        /// <param name="messages">The messages.</param>
        /// <returns></returns>
        public static byte[] CreateProof(CreateProofRequest proofRequest)
        {
            using var context = new UnmanagedMemoryContext();

            var handle = Native.bbs_create_proof_context_init(out var error);

            context.ThrowIfNeeded(error);

            foreach (var message in proofRequest.Messages)
            {
                Native.bbs_create_proof_context_add_proof_message_string(handle, message.Message, message.ProofType, context.ToBuffer(proofRequest.BlindingFactor ?? Array.Empty <byte>()), out error);
                context.ThrowIfNeeded(error);
            }

            Native.bbs_create_proof_context_set_nonce_string(handle, proofRequest.Nonce, out error);
            context.ThrowIfNeeded(error);

            Native.bbs_create_proof_context_set_public_key(handle, context.ToBuffer(proofRequest.PublicKey), out error);

            context.ThrowIfNeeded(error);

            Native.bbs_create_proof_context_set_signature(handle, context.ToBuffer(proofRequest.Signature), out error);
            context.ThrowIfNeeded(error);

            Native.bbs_create_proof_context_finish(handle, out var proof, out error);
            context.ThrowIfNeeded(error);

            return(context.ToByteArray(proof));
        }
        /// <summary>
        /// Signs a set of messages featuring both known and blinded messages to the signer and produces a BBS+ signature
        /// </summary>
        /// <param name="keyPair">The signing key containing the secret BLS key.</param>
        /// <param name="commitment">The commitment.</param>
        /// <param name="messages">The messages.</param>
        /// <returns></returns>
        public static byte[] BlindSign(BlindSignRequest request)
        {
            using var context = new UnmanagedMemoryContext();

            var handle = Native.bbs_blind_sign_context_init(out var error);

            context.ThrowIfNeeded(error);

            foreach (var item in request.Messages)
            {
                Native.bbs_blind_sign_context_add_message_string(handle, item.Index, item.Message, out error);
                context.ThrowIfNeeded(error);
            }

            Native.bbs_blind_sign_context_set_public_key(handle, context.ToBuffer(request.PublicKey), out error);
            context.ThrowIfNeeded(error);

            Native.bbs_blind_sign_context_set_secret_key(handle, context.ToBuffer(request.SecretKey.SecretKey.ToArray()), out error);
            context.ThrowIfNeeded(error);

            Native.bbs_blind_sign_context_set_commitment(handle, context.ToBuffer(request.Commitment), out error);
            context.ThrowIfNeeded(error);

            Native.bbs_blind_sign_context_finish(handle, out var blindedSignature, out error);
            context.ThrowIfNeeded(error);

            return(context.ToByteArray(blindedSignature));
        }
        /// <summary>
        /// Unblinds the signature asynchronous.
        /// </summary>
        /// <param name="request">Unbling signature request</param>
        /// <returns></returns>
        public static byte[] UnblindSignature(UnblindSignatureRequest request)
        {
            using var context = new UnmanagedMemoryContext();

            Native.bbs_unblind_signature(context.ToBuffer(request.BlindedSignature), context.ToBuffer(request.BlindingFactor), out var unblindedSignature, out var error);
            context.ThrowIfNeeded(error);

            return(context.ToByteArray(unblindedSignature));
        }
        /// <summary>
        /// Generates new BBS+ public key from the current BLS12-381
        /// </summary>
        /// <param name="messageCount">The message count.</param>
        /// <returns></returns>
        public BbsKeyPair GeyBbsKeyPair(uint messageCount)
        {
            using var context = new UnmanagedMemoryContext();

            if (SecretKey is null)
            {
                Native.bls_public_key_to_bbs_key(context.ToBuffer(PublicKey), messageCount, out var publicKey, out var error);
                context.ThrowIfNeeded(error);

                return(new BbsKeyPair(context.ToByteArray(publicKey), messageCount));
            }
            else
            {
                Native.bls_secret_key_to_bbs_key(context.ToBuffer(SecretKey), messageCount, out var publicKey, out var error);
                context.ThrowIfNeeded(error);

                return(new BbsKeyPair(context.ToByteArray(publicKey), messageCount));
            }
        }
        /// <summary>
        /// Signs a set of messages with a BBS key pair and produces a BBS signature
        /// </summary>
        /// <param name="signRequest">Request for the sign operation</param>
        /// <returns>The raw signature value</returns>
        /// <exception cref="BbsException">
        /// Secret key not found
        /// or
        /// Messages cannot be null
        /// </exception>
        public static byte[] Sign(SignRequest signRequest)
        {
            if (signRequest?.KeyPair?.SecretKey is null)
            {
                throw new BbsException("Secret key not found");
            }
            if (signRequest?.Messages is null)
            {
                throw new BbsException("Messages cannot be null");
            }

            var bbsKeyPair = signRequest.KeyPair.GeyBbsKeyPair((uint)signRequest.Messages.Length);

            using var context = new UnmanagedMemoryContext();

            var handle = Native.bbs_sign_context_init(out var error);

            context.ThrowIfNeeded(error);

            foreach (var message in signRequest.Messages)
            {
                Native.bbs_sign_context_add_message_string(handle, message, out error);
                context.ThrowIfNeeded(error);
            }

            Native.bbs_sign_context_set_public_key(handle, context.ToBuffer(bbsKeyPair.PublicKey), out error);
            context.ThrowIfNeeded(error);

            Native.bbs_sign_context_set_secret_key(handle, context.ToBuffer(signRequest.KeyPair.SecretKey !), out error);
            context.ThrowIfNeeded(error);

            Native.bbs_sign_context_finish(handle, out var signature, out error);
            context.ThrowIfNeeded(error);

            return(context.ToByteArray(signature));
        }