public unsafe void Compress16Test()
        {
            int dataLen = 16;

            byte[] data     = GetRandomBytes(dataLen);
            byte[] expected = ComputeSingleSha(data);

            using Sha256Fo sha = new Sha256Fo();
            fixed(uint *hPt = &sha.hashState[0], wPt = &sha.w[0])
            {
                int dIndex = 0;

                for (int i = 0; i < 4; i++, dIndex += 4)
                {
                    wPt[i] = (uint)((data[dIndex] << 24) | (data[dIndex + 1] << 16) | (data[dIndex + 2] << 8) | data[dIndex + 3]);
                }
                wPt[4] = 0b10000000_00000000_00000000_00000000U;

                wPt[15] = (uint)dataLen * 8;
                sha.Init(hPt);
                sha.Compress16(hPt, wPt);
                byte[] actual = sha.GetBytes(hPt);

                Assert.Equal(expected, actual);
            }
        }
示例#2
0
        private unsafe void Loop12()
        {
            if (missCount > 1)
            {
                report.AddMessageSafe("Running in parallel.");
                report.SetProgressStep(2048);
                int firstIndex = missingIndexes[0];
                Parallel.For(0, 2048, (firstItem, state) => Loop12(firstItem, firstIndex, state));
            }
            else
            {
                // We can't call the same parallel method due to usage of LoopState so we at least optimize this by
                // avoiding the inner loop over the IEnumerable
                using Sha512Fo sha512 = new Sha512Fo();
                ulong[] ipad = new ulong[80];
                ulong[] opad = new ulong[80];

                using Sha256Fo sha256 = new Sha256Fo();

                int misIndex = missingIndexes[0];

                fixed(ulong *iPt = ipad, oPt = opad)
                fixed(uint *wPt  = &sha256.w[0], hPt = &sha256.hashState[0], wrd = &wordIndexes[0])
                fixed(byte *mnPt = &mnBytes[0])
                {
                    wPt[4]  = 0b10000000_00000000_00000000_00000000U;
                    wPt[15] = 128;

                    for (uint item = 0; item < 2048; item++)
                    {
                        wrd[misIndex] = item;

                        wPt[0] = wrd[0] << 21 | wrd[1] << 10 | wrd[2] >> 1;
                        wPt[1] = wrd[2] << 31 | wrd[3] << 20 | wrd[4] << 9 | wrd[5] >> 2;
                        wPt[2] = wrd[5] << 30 | wrd[6] << 19 | wrd[7] << 8 | wrd[8] >> 3;
                        wPt[3] = wrd[8] << 29 | wrd[9] << 18 | wrd[10] << 7 | wrd[11] >> 4;

                        sha256.Init(hPt);
                        sha256.Compress16(hPt, wPt);

                        if ((wrd[11] & 0b1111) == hPt[0] >> 28)
                        {
                            int mnLen = 0;
                            for (int i = 0; i < 12; i++)
                            {
                                var temp = allWordsBytes[wrd[i]];
                                Buffer.BlockCopy(temp, 0, mnBytes, mnLen, temp.Length);
                                mnLen += temp.Length;
                            }

                            if (SetBip32(sha512, mnPt, --mnLen, iPt, oPt))
                            {
                                SetResultParallel(mnPt, mnLen);
                                break;
                            }
                        }
                    }
                }
            }
        }
示例#3
0
        private unsafe bool Loop12()
        {
            using Sha512Fo sha512 = new Sha512Fo();
            ulong[] ipad = new ulong[80];
            ulong[] opad = new ulong[80];

            using Sha256Fo sha256 = new Sha256Fo();
            var cartesian = CartesianProduct.Create(Enumerable.Repeat(Enumerable.Range(0, 2048), missCount));

            fixed(ulong *iPt = ipad, oPt = opad)
            fixed(uint *wPt  = &sha256.w[0], hPt = &sha256.hashState[0], wrd = &wordIndexes[0])
            fixed(int *mi    = &missingIndexes[0])
            fixed(byte *mnPt = &mnBytes[0])
            {
                wPt[4]  = 0b10000000_00000000_00000000_00000000U;
                wPt[15] = 128;

                foreach (var item in cartesian)
                {
                    int j = 0;
                    foreach (var k in item)
                    {
                        wrd[mi[j]] = (uint)k;
                        j++;
                    }

                    wPt[0] = wrd[0] << 21 | wrd[1] << 10 | wrd[2] >> 1;
                    wPt[1] = wrd[2] << 31 | wrd[3] << 20 | wrd[4] << 9 | wrd[5] >> 2;
                    wPt[2] = wrd[5] << 30 | wrd[6] << 19 | wrd[7] << 8 | wrd[8] >> 3;
                    wPt[3] = wrd[8] << 29 | wrd[9] << 18 | wrd[10] << 7 | wrd[11] >> 4;

                    sha256.Init(hPt);
                    sha256.Compress16(hPt, wPt);

                    if ((wrd[11] & 0b1111) == hPt[0] >> 28)
                    {
                        int mnLen = 0;
                        for (int i = 0; i < 12; i++)
                        {
                            foreach (byte b in wordBytes[wrd[i]])
                            {
                                mnPt[mnLen++] = b;
                            }
                            mnPt[mnLen++] = SpaceByte;
                        }

                        if (SetBip32(sha512, mnPt, --mnLen, iPt, oPt))
                        {
                            return(SetResult(mnLen));
                        }
                    }
                }
            }

            return(false);
        }
示例#4
0
        private unsafe void Loop12(int firstItem, int firstIndex, ParallelLoopState loopState)
        {
            var cartesian = CartesianProduct.Create(Enumerable.Repeat(Enumerable.Range(0, 2048), missCount - 1));

            using Sha512Fo sha512 = new Sha512Fo();
            ulong[] ipad = new ulong[80];
            ulong[] opad = new ulong[80];

            using Sha256Fo sha256 = new Sha256Fo();
            byte[] localMnBytes = new byte[mnBytes.Length];

            var localCopy = new byte[allWordsBytes.Length][];

            Array.Copy(allWordsBytes, localCopy, allWordsBytes.Length);

            uint[] localWIndex = new uint[wordIndexes.Length];
            Array.Copy(wordIndexes, localWIndex, wordIndexes.Length);

            fixed(ulong *iPt = &ipad[0], oPt = &opad[0])
            fixed(uint *wPt  = &sha256.w[0], hPt = &sha256.hashState[0], wrd = &localWIndex[0])
            fixed(int *mi    = &missingIndexes[1])
            fixed(byte *mnPt = &localMnBytes[0])
            {
                wPt[4]  = 0b10000000_00000000_00000000_00000000U;
                wPt[15] = 128;

                wrd[firstIndex] = (uint)firstItem;

                foreach (var item in cartesian)
                {
                    if (loopState.IsStopped)
                    {
                        return;
                    }

                    int j = 0;
                    foreach (var k in item)
                    {
                        wrd[mi[j]] = (uint)k;
                        j++;
                    }

                    wPt[0] = wrd[0] << 21 | wrd[1] << 10 | wrd[2] >> 1;
                    wPt[1] = wrd[2] << 31 | wrd[3] << 20 | wrd[4] << 9 | wrd[5] >> 2;
                    wPt[2] = wrd[5] << 30 | wrd[6] << 19 | wrd[7] << 8 | wrd[8] >> 3;
                    wPt[3] = wrd[8] << 29 | wrd[9] << 18 | wrd[10] << 7 | wrd[11] >> 4;

                    sha256.Init(hPt);
                    sha256.Compress16(hPt, wPt);

                    if ((wrd[11] & 0b1111) == hPt[0] >> 28)
                    {
                        int mnLen = 0;
                        for (int i = 0; i < 12; i++)
                        {
                            var temp = localCopy[wrd[i]];
                            Buffer.BlockCopy(temp, 0, mnBytes, mnLen, temp.Length);
                            mnLen += temp.Length;
                        }

                        if (SetBip32(sha512, mnPt, --mnLen, iPt, oPt))
                        {
                            SetResultParallel(mnPt, mnLen);
                            loopState.Stop();
                            break;
                        }
                    }
                }
            }

            report.IncrementProgress();
        }