public static byte[] SignCompact(byte[] data, byte[] seckey, out int recoveryId)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (data.Length == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(data));
            }
            if (seckey == null)
            {
                throw new ArgumentNullException(nameof(seckey));
            }
            if (seckey.Length != 32)
            {
                throw new ArgumentOutOfRangeException(nameof(seckey));
            }
            recoveryId = 0;
            EcdsaRecoverableSignature recoverableSignature = new EcdsaRecoverableSignature();

            if (!Secp256K1Manager.Secp256K1EcdsaSignRecoverable(Secp256K1Manager.Ctx, recoverableSignature, data, seckey, (NonceFunction)null, (byte[])null))
            {
                return((byte[])null);
            }
            byte[] output64 = new byte[64];
            byte   recid;

            if (!Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureSerializeCompact(Secp256K1Manager.Ctx, output64, out recid, recoverableSignature))
            {
                return((byte[])null);
            }
            recoveryId = (int)recid;
            return(output64);
        }
        private static bool Secp256K1EcdsaRecoverableSignatureSerializeCompact(Context ctx, byte[] outputxx, int skip, out byte recid, EcdsaRecoverableSignature sig)
        {
            Scalar scalar1 = new Scalar();
            Scalar scalar2 = new Scalar();

            recid = (byte)0;
            if (outputxx == null)
            {
                EventHandler <Callback> illegalCallback = ctx.IllegalCallback;
                if (illegalCallback != null)
                {
                    illegalCallback((object)null, new Callback("(outputxx != null)"));
                }
                return(false);
            }
            if (sig == null)
            {
                EventHandler <Callback> illegalCallback = ctx.IllegalCallback;
                if (illegalCallback != null)
                {
                    illegalCallback((object)null, new Callback("(sig != null)"));
                }
                return(false);
            }
            Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureLoad(scalar1, scalar2, out recid, sig);
            Scalar.GetB32(outputxx, skip, scalar1);
            Scalar.GetB32(outputxx, skip + 32, scalar2);
            return(true);
        }
 static Secp256K1Manager()
 {
     Secp256K1Manager.IllegalCallback += new EventHandler <Callback>(Secp256K1Manager.OnIllegalCallback);
     Secp256K1Manager.ErrorCallback   += new EventHandler <Callback>(Secp256K1Manager.OnErrorCallback);
     Secp256K1Manager.Ctx              = new Context();
     Secp256K1Manager.Ctx              = Secp256K1Manager.Secp256K1ContextCreate(Options.ContextVerify | Options.FlagsBitContextSign);
 }
        public static byte[] SignCompressedCompact(byte[] data, byte[] seckey)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (data.Length == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(data));
            }
            if (seckey == null)
            {
                throw new ArgumentNullException(nameof(seckey));
            }
            if (seckey.Length != 32)
            {
                throw new ArgumentOutOfRangeException(nameof(seckey));
            }
            byte recid = 0;
            EcdsaRecoverableSignature recoverableSignature = new EcdsaRecoverableSignature();
            byte num1 = 0;
            int  num2 = 0;

            byte[] numArray = (byte[])null;
            RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create();

            do
            {
                if (num1 == byte.MaxValue)
                {
                    ++num2;
                    num1 = (byte)0;
                }
                if (num1 > (byte)0)
                {
                    numArray = new byte[32];
                    randomNumberGenerator.GetBytes(numArray);
                }
                ++num1;
            }while (!Secp256K1Manager.Secp256K1EcdsaSignRecoverable(Secp256K1Manager.Ctx, recoverableSignature, data, seckey, (NonceFunction)null, numArray) || !Secp256K1Manager.IsCanonical(recoverableSignature.Data));
            byte[] outputxx = new byte[65];
            Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureSerializeCompact(Secp256K1Manager.Ctx, outputxx, 1, out recid, recoverableSignature);
            outputxx[0] = (byte)((int)recid + 4 + 27);
            return(outputxx);
        }
        public static byte[] GetPublicKey(byte[] privateKey, bool compressed)
        {
            if (privateKey == null)
            {
                throw new ArgumentNullException(nameof(privateKey));
            }
            if (privateKey.Length != 32)
            {
                throw new ArgumentOutOfRangeException(nameof(privateKey));
            }
            PubKey pubKey = new PubKey();

            if (!Secp256K1T.EcPubKeyCreate(Secp256K1Manager.Ctx, pubKey, privateKey))
            {
                return((byte[])null);
            }
            return(Secp256K1Manager.SerializePublicKey(pubKey, compressed));
        }
        private static int SignCompact(Context ctx, byte[] msg32, byte[] seckey, byte[] output64, out byte recid)
        {
            EcdsaRecoverableSignature recoverableSignature = new EcdsaRecoverableSignature();
            byte num   = 0;
            int  index = 0;

            byte[] noncedata = new byte[32];
            do
            {
                noncedata[index] = num;
                ++num;
                if (noncedata[index] == byte.MaxValue)
                {
                    ++index;
                }
            }while (!Secp256K1Manager.Secp256K1EcdsaSignRecoverable(ctx, recoverableSignature, msg32, seckey, (NonceFunction)null, noncedata));
            Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureSerializeCompact(ctx, output64, out recid, recoverableSignature);
            return((int)num);
        }
        private static byte[] SignCompact(byte[] data, byte[] seckey, out byte recoveryId)
        {
            EcdsaRecoverableSignature recoverableSignature = new EcdsaRecoverableSignature();
            byte num   = 0;
            int  index = 0;

            byte[] noncedata = new byte[32];
            do
            {
                noncedata[index] = num++;
                if (num == byte.MaxValue)
                {
                    ++index;
                    num = (byte)0;
                }
            }while (!Secp256K1Manager.Secp256K1EcdsaSignRecoverable(Secp256K1Manager.Ctx, recoverableSignature, data, seckey, (NonceFunction)null, noncedata));
            byte[] output64 = new byte[64];
            Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureSerializeCompact(Secp256K1Manager.Ctx, output64, out recoveryId, recoverableSignature);
            return(output64);
        }
        private static bool Secp256K1EcdsaSignRecoverable(Context ctx, EcdsaRecoverableSignature signature, byte[] msg32, byte[] seckey, NonceFunction noncefp, byte[] noncedata)
        {
            if (ctx == null || msg32 == null || (signature == null || seckey == null))
            {
                throw new NullReferenceException();
            }
            if (!EcMultGen.ContextIsBuilt(ctx.EcMultGenCtx))
            {
                throw new ArithmeticException();
            }
            if (noncefp == null)
            {
                noncefp = Secp256K1T.NonceFunctionDefault;
            }
            byte   recid    = 1;
            bool   flag     = false;
            bool   overflow = false;
            Scalar scalar1  = new Scalar();

            Scalar.SetB32(scalar1, seckey, ref overflow);
            Scalar scalar2 = new Scalar();
            Scalar scalar3 = new Scalar();

            if (!overflow && !Scalar.IsZero(scalar1))
            {
                byte[] numArray = new byte[32];
                uint   attempt  = 0;
                Scalar scalar4  = new Scalar();
                Scalar.SetB32(scalar4, msg32);
                Scalar scalar5 = new Scalar();
                while (true)
                {
                    flag = noncefp(numArray, msg32, seckey, (byte[])null, noncedata, attempt);
                    if (flag)
                    {
                        Scalar.SetB32(scalar5, numArray, ref overflow);
                        if (Scalar.IsZero(scalar5) || overflow || !Secp256K1Manager.Secp256K1EcdsaSigSign(ctx.EcMultGenCtx, scalar2, scalar3, scalar1, scalar4, scalar5, out recid))
                        {
                            ++attempt;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                Util.MemSet(numArray, (byte)0, 32);
                Scalar.Clear(scalar4);
                Scalar.Clear(scalar5);
                Scalar.Clear(scalar1);
            }
            if (flag)
            {
                Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureSave(signature, scalar2, scalar3, recid);
            }
            else
            {
                Util.MemSet(signature.Data, (byte)0, 65);
            }
            return(flag);
        }
 private static bool Secp256K1EcdsaRecoverableSignatureSerializeCompact(Context ctx, byte[] output64, out byte recid, EcdsaRecoverableSignature sig)
 {
     return(Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureSerializeCompact(ctx, output64, 0, out recid, sig));
 }