Example #1
0
        public void ComputeHash(bool useScrypt)
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write(mVersion);
            bw.Write(mPrevBlock);
            bw.Write(mMerkleRoot);
            bw.Write(mTimestamp);
            bw.Write(mDifficultyBits);
            bw.Write(mNOnce);
            byte[] data = ms.ToArray();
            bw.Close();

            if (useScrypt)
            {
                Work work = new Work();
                Buffer.BlockCopy(data, 0, work.data, 0, 80);
                for (int i = 0; i < 20; i++)
                    work.data[i] = Program.Byteswap(work.data[i]);

                Scrypt scrypt = new Scrypt(work);
                mHash = scrypt.GetHash();
            }
            else
            {
                // SHA 256
                mHash = Scrypt.SHA2562(data);
            }
        }
Example #2
0
        public Scrypt(Work w)
        {
            work = w;
            work.data[20] = 0x80000000;
            work.data[31] = 0x00000280;

            pad36 = new uint[16];
            pad5c = new uint[16];
            dataBuffer = new uint[16];
            dataBuffer2 = new uint[16];
            tempHash = new uint[16];
            output = new uint[8];

            for (int i = 8; i < 16; i++)
            {
                pad36[i] = 0x36363636;
                pad5c[i] = 0x5c5c5c5c;
            }
            dataBuffer[5] = 0x80000000;
            dataBuffer[15] = 0x000004A0;
            dataBuffer2[0] = 0x00000001;
            dataBuffer2[1] = 0x80000000;
            dataBuffer2[15] = 0x00000620;
            tempHash[8] = 0x80000000;
            tempHash[15] = 0x00000300;

            dataBuffer[0] = work.data[16];
            dataBuffer[1] = work.data[17];
            dataBuffer[2] = work.data[18];
            dataBuffer[3] = work.data[19];

            byte[] diffTarget = new byte[32];
            Buffer.BlockCopy(work.target, 0, diffTarget, 0, 32);

            uint diffZeros = 0;
            for (int i = 31; i >= 0; i--)
            {
                if (diffTarget[i] != 0)
                    break;
                diffZeros++;
            }
            switch (diffZeros)
            {
                case 0:
                    outputMask = 0;
                    break;
                case 1:
                    outputMask = 0x00FFFFFF;
                    break;
                case 2:
                    outputMask = 0x0000FFFF;
                    break;
                case 3:
                    outputMask = 0xFF000000;
                    break;
                default:
                    outputMask = 0xFFFFFFFF;
                    break;
            }
        }
Example #3
0
        public WorkThread()
        {
            mWork = new Work();
            mHashCount = 0;

            mThread = new Thread(new ThreadStart(WorkFunction));
            mThread.Start();
        }
Example #4
0
 public void Copy(Work work)
 {
     hashStart = work.hashStart;
     hashCount = work.hashCount;
     hashAlgorithm = work.hashAlgorithm;
     Array.Copy(work.target, target, 8);
     Array.Copy(work.data, data, 32);
 }
Example #5
0
        public MTM(int threadCount, bool useSSE, float gpuPercentage)
        {
            mThreadCount = threadCount;
            mThreads = new WorkThread[mThreadCount];
            for (int i = 0; i < mThreadCount; i++)
                mThreads[i] = new WorkThread();

            mHashRate = 0;
            mCurrentWork = null;
            mTimer = new Timer();
        }
Example #6
0
        public static bool TestStandard()
        {
            Work work = new Work();
            Array.Copy(sData, work.data, 32);
            Array.Copy(sTarget, work.target, 8);

            Scrypt scrypt = new Scrypt(work);
            bool result = scrypt.Hash(sNOnce);

            return result;
        }
Example #7
0
        public void StartWork(uint hashStart, uint hashCount, Work work)
        {
            mHashStart = hashStart;
            mHashCount = hashCount;
            mWork.Copy(work);

            mHashesDone = 0;
            mSolutionFound = false;

            mWorkDone = false;
            mWorkToDo = true;
        }
Example #8
0
        public Connection(string memberName, string productName, string platform, uint requestedHashes = 5000)
        {
            mWorkBlock = null;

            mInputBuffer = new byte[1024 * 8];
            mInputBufferBytes = 0;

            mHashChunkSize = requestedHashes;
            mMember = memberName;
            mProduct = productName;
            mPlatform = platform;
            mState = ConnectionState.Disconnected;

            SetupSocket();
        }
Example #9
0
        public void StartWork(Work work)
        {
            mTimer.Start();
            mCurrentWork = work;

            uint hashStart = work.hashStart;
            uint hashCount = work.hashCount;

            uint hashesPerThread = (uint)(hashCount / mThreadCount);
            for( int i = 0; i < mThreadCount; i++ )
            {
                uint hashes = hashesPerThread;
                if( hashes > hashCount )
                    hashes = hashCount;
                hashCount -= hashes;

                mThreads[i].StartWork(hashStart, hashes, work);
                hashStart += hashes;
            }
        }
Example #10
0
        public void Update(Connection conn)
        {
            if (mCurrentWork != null)
            {
                bool threadsAllDone = true;
                foreach (WorkThread t in mThreads)
                {
                    if (!t.IsWorkDone())
                    {
                        threadsAllDone = false;
                        break;
                    }
                }

                if (threadsAllDone)
                {
                    uint hashes = 0;
                    uint solution = 0;
                    bool solutionFound = false;

                    foreach (WorkThread t in mThreads)
                    {
                        hashes += t.HashesDone;
                        if (t.SolutionFound)
                        {
                            solutionFound = true;
                            solution = t.Solution;
                        }
                    }

                    conn.SendWorkComplete(solutionFound, Scrypt.ByteReverse(solution), hashes);
                    mCurrentWork = null;
                    mTimer.Stop();
                    mHashRate = (uint)((double)hashes / mTimer.GetDuration());
                }
            }
        }
Example #11
0
        void ProcessWorkCommand(byte[] data)
        {
            Work work = new Work();

            MemoryStream ms = new MemoryStream(data);
            BinaryReader br = new BinaryReader(ms);

            work.hashStart = br.ReadUInt32();
            work.hashCount = br.ReadUInt32();
            uint c = br.ReadUInt32();
            if (c == 0)
                work.hashAlgorithm = Work.Algorithm.SHA256;
            else
                work.hashAlgorithm = Work.Algorithm.Scrypt;
            byte[] target = br.ReadBytes(32);
            Buffer.BlockCopy(target, 0, work.target, 0, 32);

            byte[] blockHeader = br.ReadBytes(128);
            Buffer.BlockCopy(blockHeader, 0, work.data, 0, 128);

            mWorkBlock = work;

            br.Close();
        }
Example #12
0
 public Work GetWork()
 {
     Work ret = null;
     if (mWorkBlock != null)
     {
         ret = mWorkBlock;
         mWorkBlock = null;
     }
     return ret;
 }
Example #13
0
        public Scrypt(Work w)
        {
            work          = w;
            work.data[20] = 0x80000000;
            work.data[31] = 0x00000280;

            pad36       = new uint[16];
            pad5c       = new uint[16];
            dataBuffer  = new uint[16];
            dataBuffer2 = new uint[16];
            tempHash    = new uint[16];
            output      = new uint[8];

            for (int i = 8; i < 16; i++)
            {
                pad36[i] = 0x36363636;
                pad5c[i] = 0x5c5c5c5c;
            }
            dataBuffer[5]   = 0x80000000;
            dataBuffer[15]  = 0x000004A0;
            dataBuffer2[0]  = 0x00000001;
            dataBuffer2[1]  = 0x80000000;
            dataBuffer2[15] = 0x00000620;
            tempHash[8]     = 0x80000000;
            tempHash[15]    = 0x00000300;

            dataBuffer[0] = work.data[16];
            dataBuffer[1] = work.data[17];
            dataBuffer[2] = work.data[18];
            dataBuffer[3] = work.data[19];


            byte[] diffTarget = new byte[32];
            Buffer.BlockCopy(work.target, 0, diffTarget, 0, 32);

            uint diffZeros = 0;

            for (int i = 31; i >= 0; i--)
            {
                if (diffTarget[i] != 0)
                {
                    break;
                }
                diffZeros++;
            }
            switch (diffZeros)
            {
            case 0:
                outputMask = 0;
                break;

            case 1:
                outputMask = 0x00FFFFFF;
                break;

            case 2:
                outputMask = 0x0000FFFF;
                break;

            case 3:
                outputMask = 0xFF000000;
                break;

            default:
                outputMask = 0xFFFFFFFF;
                break;
            }
        }