Example #1
0
        public static void randomx_init(bool miningMode = false)
        {
            if (_Cache != IntPtr.Zero)
            {
                return;
            }
            lock (myKey)
            {
                if (_Cache == IntPtr.Zero)
                {
                    //Log.Debug("randomx_init ...");

                    if (!miningMode)
                    {
                        _Flags = RandomXDLL.randomx_get_flags();
                        _Cache = RandomXDLL.randomx_alloc_cache(_Flags);
                        RandomXDLL.randomx_init_cache(_Cache, myKey, Convert.ToUInt32(myKey.Length));
                    }
                    else
                    {
                        Log.Debug("randomx_init FullMem");
                        _Flags = RandomXDLL.randomx_get_flags() | Flags.FullMem;
                        _Cache = RandomXDLL.randomx_alloc_cache(_Flags);
                        RandomXDLL.randomx_init_cache(_Cache, myKey, Convert.ToUInt32(myKey.Length));
                        _Dataset = RandomXDLL.randomx_alloc_dataset(_Flags);
                        var datasetItemCount = RandomXDLL.randomx_dataset_item_count();

                        uint initThreadCount = Math.Max((uint)Environment.ProcessorCount - 1, 1);
                        uint finishCount     = 0;
                        uint startItem       = 0;
                        uint perThread       = datasetItemCount / initThreadCount;
                        uint remainder       = datasetItemCount % initThreadCount;
                        for (uint ii = 0; ii < initThreadCount; ii++)
                        {
                            var  count = perThread + (ii == initThreadCount - 1 ? remainder : 0);
                            uint start = startItem;

                            Thread thread = new Thread(new ParameterizedThreadStart(
                                                           (object data) =>
                            {
                                RandomXDLL.randomx_init_dataset(_Dataset, _Cache, start, count);
                                finishCount++;
                            }
                                                           )
                                                       );
                            startItem += count;
                            thread.Start(null);
                        }
                        while (finishCount < initThreadCount)
                        {
                            Thread.Sleep(10);
                        }
                    }

                    //Log.Debug("randomx_init ok");
                }
            }
        }
Example #2
0
        public static byte[] CaculateHash(byte[] input)
        {
            randomx_init();

            var buffer = _buffer.Value;

            RandomXDLL.randomx_calculate_hash(_Machine.Value, input, Convert.ToUInt32(input.Length), buffer);
            return(buffer);
        }
Example #3
0
        public static string CaculateHash(string text)
        {
            randomx_init();

            var input  = text.HexToBytes();
            var buffer = _buffer.Value;

            RandomXDLL.randomx_calculate_hash(_Machine.Value, input, Convert.ToUInt32(input.Length), buffer);
            return(buffer.ToHexString());
        }