コード例 #1
0
ファイル: Secp256K1Manager.cs プロジェクト: UUSR/BitcoinABC
        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;
            var recoverableSignature = new EcdsaRecoverableSignature();

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

            if (!Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureSerializeCompact(Secp256K1Manager.Ctx, output64, out recid, recoverableSignature))
            {
                return((byte[])null);
            }
            recoveryId = (int)recid;
            return(output64);
        }
コード例 #2
0
ファイル: Secp256K1Manager.cs プロジェクト: UUSR/BitcoinABC
        private static bool Secp256K1EcdsaRecoverableSignatureSerializeCompact(Context ctx, byte[] outputxx, int skip, out byte recid, EcdsaRecoverableSignature sig)
        {
            var scalar1 = new Scalar();
            var scalar2 = new Scalar();

            recid = (byte)0;
            if (outputxx == null)
            {
                var illegalCallback = ctx.IllegalCallback;
                if (illegalCallback != null)
                {
                    illegalCallback((object)null, new Callback("(outputxx != null)"));
                }
                return(false);
            }
            if (sig == null)
            {
                var 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);
        }
コード例 #3
0
ファイル: Secp256K1Manager.cs プロジェクト: UUSR/BitcoinABC
 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);
 }
コード例 #4
0
ファイル: KeyPair.cs プロジェクト: UUSR/BitcoinABC
        public static KeyPair Generate(bool compressed)
        {
            var randomKey = Secp256K1Manager.GenerateRandomKey();
            var array1    = new byte[1].Concat(Ripemd160Manager.GetHash(Sha256Manager.GetHash(Secp256K1Manager.GetPublicKey(randomKey, compressed)))).ToArray();
            var pub       = Base58.Encode(array1.Concat(Sha256Manager.GetHash(Sha256Manager.GetHash(array1)).Take(4)).ToArray());

            byte[] array2;
            if (!compressed)
            {
                array2 = new byte[1]
                {
                    128
                }
            }
コード例 #5
0
ファイル: Secp256K1Manager.cs プロジェクト: UUSR/BitcoinABC
        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;
            var  recoverableSignature = new EcdsaRecoverableSignature();
            byte num1     = 0;
            var  num2     = 0;
            var  numArray = (byte[])null;
            var  randomNumberGenerator = RandomNumberGenerator.Create();

            do
            {
                if ((int)num1 == (int)byte.MaxValue)
                {
                    ++num2;
                    num1 = (byte)0;
                }
                if ((int)num1 > 0)
                {
                    numArray = new byte[32];
                    randomNumberGenerator.GetBytes(numArray);
                }
                ++num1;
            }while (!Secp256K1Manager.Secp256K1EcdsaSignRecoverable(Secp256K1Manager.Ctx, recoverableSignature, data, seckey, (NonceFunction)null, numArray) || !Secp256K1Manager.IsCanonical(recoverableSignature.Data));
            var outputxx = new byte[65];

            Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureSerializeCompact(Secp256K1Manager.Ctx, outputxx, 1, out recid, recoverableSignature);
            outputxx[0] = (byte)((int)recid + 4 + 27);
            return(outputxx);
        }
コード例 #6
0
ファイル: Secp256K1Manager.cs プロジェクト: UUSR/BitcoinABC
        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));
            }
            var pubKey = new PubKey();

            if (!Secp256K1T.EcPubKeyCreate(Secp256K1Manager.Ctx, pubKey, privateKey))
            {
                return((byte[])null);
            }
            return(Secp256K1Manager.SerializePublicKey(pubKey, compressed));
        }
コード例 #7
0
ファイル: Secp256K1Manager.cs プロジェクト: UUSR/BitcoinABC
        private static int SignCompact(Context ctx, byte[] msg32, byte[] seckey, byte[] output64, out byte recid)
        {
            var  recoverableSignature = new EcdsaRecoverableSignature();
            byte num       = 0;
            var  index     = 0;
            var  noncedata = new byte[32];

            do
            {
                noncedata[index] = num;
                ++num;
                if ((int)noncedata[index] == (int)byte.MaxValue)
                {
                    ++index;
                }
            }while (!Secp256K1Manager.Secp256K1EcdsaSignRecoverable(ctx, recoverableSignature, msg32, seckey, (NonceFunction)null, noncedata));
            Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureSerializeCompact(ctx, output64, out recid, recoverableSignature);
            return((int)num);
        }
コード例 #8
0
ファイル: Secp256K1Manager.cs プロジェクト: UUSR/BitcoinABC
        private static byte[] SignCompact(byte[] data, byte[] seckey, out byte recoveryId)
        {
            var  recoverableSignature = new EcdsaRecoverableSignature();
            byte num       = 0;
            var  index     = 0;
            var  noncedata = new byte[32];

            do
            {
                noncedata[index] = num++;
                if ((int)num == (int)byte.MaxValue)
                {
                    ++index;
                    num = (byte)0;
                }
            }while (!Secp256K1Manager.Secp256K1EcdsaSignRecoverable(Secp256K1Manager.Ctx, recoverableSignature, data, seckey, (NonceFunction)null, noncedata));
            var output64 = new byte[64];

            Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureSerializeCompact(Secp256K1Manager.Ctx, output64, out recoveryId, recoverableSignature);
            return(output64);
        }
コード例 #9
0
ファイル: Secp256K1Manager.cs プロジェクト: UUSR/BitcoinABC
        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;
            var  flag     = false;
            var  overflow = false;
            var  scalar1  = new Scalar();

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

            if (!overflow && !Scalar.IsZero(scalar1))
            {
                var  numArray = new byte[32];
                uint attempt  = 0;
                var  scalar4  = new Scalar();
                Scalar.SetB32(scalar4, msg32);
                var 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);
        }
コード例 #10
0
ファイル: Secp256K1Manager.cs プロジェクト: UUSR/BitcoinABC
 private static bool Secp256K1EcdsaRecoverableSignatureSerializeCompact(Context ctx, byte[] output64, out byte recid, EcdsaRecoverableSignature sig)
 {
     return(Secp256K1Manager.Secp256K1EcdsaRecoverableSignatureSerializeCompact(ctx, output64, 0, out recid, sig));
 }