Esempio n. 1
0
        public PublicKey[] GetPublicKeys(BIP0032Path path, uint count, uint startIndex = 0, uint step = 1)
        {
            if (isDisposed)
            {
                throw new ObjectDisposedException(nameof(BIP0032));
            }
            if (path is null)
            {
                throw new ArgumentNullException(nameof(path), "Path can not be null!");
            }
            if (ExtendedKeyDepth == byte.MaxValue || ExtendedKeyDepth + path.Indexes.Length + 1 > byte.MaxValue)
            {
                throw new ArgumentOutOfRangeException(nameof(ExtendedKeyDepth), "Can not get children since " +
                                                      "depth will be bigger than 1 byte");
            }

            PublicKey[] result = new PublicKey[count];
            if (!(PrvKey is null))
            {
                PrivateKey[] tempPK = GetPrivateKeys(path, count, startIndex, step);
                for (int j = 0; j < tempPK.Length; j++)
                {
                    result[j] = tempPK[j].ToPublicKey();
                    tempPK[j].Dispose();
                }
                return(result);
            }

            // If we are here it means PrivateKey was null
            bool anyHardened = path.Indexes.Any(x => IsHardendedIndex(x));

            if (anyHardened || IsHardendedIndex(startIndex))
            {
                throw new ArgumentException();
            }

            // Two quick fixes:
            if (count == 0)
            {
                return(null);
            }
            if (step < 1)
            {
                step = 1;
            }

            // First start deriving the extended keys for each index
            BigInteger prevPrvInt = PrvKey.ToBigInt();

            byte[]             prevPubBa    = PubKey.ToByteArray(true);
            EllipticCurvePoint prevPubPoint = PubKey.ToPoint();

            byte[] tempCC = ChainCode;
            byte[] tempLeft;
            EllipticCurveCalculator calc = new EllipticCurveCalculator();

            foreach (var index in path.Indexes)
            {
                // There is no hardened indexes thanks to first check
                FastStream stream = new FastStream(33 + 4);
                stream.Write(prevPubBa);
                stream.WriteBigEndian(index);
                HmacAndSplitData(stream.ToByteArray(), tempCC, out tempLeft, out tempCC);

                BigInteger kTemp = tempLeft.ToBigInt(true, true);
                // Note that we throw an exception here if the values were invalid (highly unlikely)
                // because it is the extended keys, we can't skip anything here.
                if (kTemp == 0 || kTemp >= N)
                {
                    throw new ArgumentException();
                }
                EllipticCurvePoint pt      = calc.AddChecked(calc.MultiplyByG(kTemp), prevPubPoint);
                PublicKey          tempPub = new PublicKey(pt);
                prevPubPoint = tempPub.ToPoint();
                prevPubBa    = tempPub.ToByteArray(true);
            }

            // Then derive the actual keys

            int  i          = 0;
            uint childIndex = startIndex;

            while (i < count)
            {
                // There is no hardened indexes thanks to first check
                FastStream stream = new FastStream(33 + 4);
                stream.Write(prevPubBa);
                stream.WriteBigEndian(childIndex);
                HmacAndSplitData(stream.ToByteArray(), tempCC, out tempLeft, out _);

                BigInteger kTemp = tempLeft.ToBigInt(true, true);
                // Note: we don't throw any exceptions here. We simply ignore invalid values (highly unlikely)
                // and move on to the next index. The returned value will always be filled with expected number of items.
                if (kTemp == 0 || kTemp >= N)
                {
                    continue;
                }
                EllipticCurvePoint pt   = calc.AddChecked(calc.MultiplyByG(kTemp), prevPubPoint);
                PublicKey          temp = new PublicKey(pt);
                result[i++] = temp;

                childIndex += step;
            }
            return(result);
        }
Esempio n. 2
0
        private unsafe bool LoopComp(string key, int missingCount, char missingChar, byte[] expectedHash)
        {
            int[]  missingIndexes = new int[missingCount];
            byte[] ba             = new byte[32];
            for (int i = 0, j = 0; i < ba.Length; i++)
            {
                int hi, lo;
                if (key[i * 2] == missingChar)
                {
                    hi = 0;
                    missingIndexes[j++] = i * 2;
                }
                else
                {
                    hi = key[i * 2] - 65;
                    hi = hi + 10 + ((hi >> 31) & 7);
                }
                if (key[i * 2 + 1] == '*')
                {
                    lo = 0;
                    missingIndexes[j++] = i * 2 + 1;
                }
                else
                {
                    lo = key[i * 2 + 1] - 65;
                    lo = lo + 10 + ((lo >> 31) & 7) & 0x0f;
                }

                ba[i] = (byte)(lo | hi << 4);
            }

            var cartesian = CartesianProduct.Create(Enumerable.Repeat(Enumerable.Range(0, 16), missingCount));
            EllipticCurveCalculator calc = new EllipticCurveCalculator();



            BigInteger         smallVal = new BigInteger(ba, true, true);
            EllipticCurvePoint smallPub = calc.MultiplyByG(smallVal);
            Ripemd160Sha256    hash     = new Ripemd160Sha256();

            Parallel.ForEach(cartesian, (item, loopState) =>
            {
                Span <byte> temp = new byte[32];

                int mis = 0;
                foreach (int keyItem in item)
                {
                    int misIndex = missingIndexes[mis];
                    if (misIndex % 2 == 0)
                    {
                        temp[misIndex / 2] |= (byte)(keyItem << 4);
                    }
                    else
                    {
                        temp[misIndex / 2] |= (byte)keyItem;
                    }
                    mis++;
                }

                BigInteger tempVal         = new BigInteger(temp, true, true);
                EllipticCurvePoint tempPub = calc.MultiplyByG(tempVal);
                EllipticCurvePoint pub     = calc.AddChecked(tempPub, smallPub);
                byte[] toHash = new byte[33];
                toHash[0]     = pub.Y.IsEven ? (byte)2 : (byte)3;
                byte[] xBytes = pub.X.ToByteArray(true, true);
                Buffer.BlockCopy(xBytes, 0, toHash, 33 - xBytes.Length, xBytes.Length);

                ReadOnlySpan <byte> actual = hash.ComputeHash(toHash);
                if (actual.SequenceEqual(expectedHash))
                {
                    char[] origHex = key.ToCharArray();
                    int index      = 0;
                    foreach (var keyItem in item)
                    {
                        origHex[missingIndexes[index++]] = GetHex(keyItem);
                    }
                    AddQueue($"Found a key: {new string(origHex)}");
                    loopState.Break();
                }
            });


            AddQueue("Failed to find any key.");
            return(false);
        }