Ejemplo n.º 1
0
        /// <summary>
        /// ES: Cálculo de Nonce
        /// </summary>
        /// <param name="data">ES: Datos a labrar</param>
        /// <param name="config">ES: Configuración de labrado</param>
        /// <returns>ES: Resultado incluyendo Nonce y estadísticas</returns>
        public static FarmingResult Compute(byte[] data, FarmingConfig config)
        {
            var watch = Stopwatch.StartNew();

            var challenge  = config.Challenge ?? FarmingBytes.LessOrEqualThan;
            var hashRecipe = new HashRecipe(config.HashRecipe);

            byte[] hash;

            // Nonce initilization
            var nonceLength = config.NonceLength;

            byte[] nonce;
            if (config.NonceFromZero)
            {
                nonce = new byte[nonceLength];  // ES: inicia en 0
            }
            else
            {
                nonce = FarmingBytes.SeedNonce(nonceLength);   // ES: inicia en un valor aleatorio
            }
            // Nonce position
            int noncePosition;

            if (config.NoncePosition == FarmingNoncePosition.Head)
            {
                noncePosition = data.ChangeHead(nonce);
            }
            else
            {
                noncePosition = data.ChangeTail(nonce);
            }

            // Compute hash
            UInt64 hashCount = 0;

            do
            {
                data.IncNonce(noncePosition, nonceLength);  // TODO: handle when returns false (cycle)
                hash = hashRecipe.ComputeHash(data);
                hashCount++;
            }while (!challenge(hash, config.ChallengeValue));
            watch.Stop();

            nonce = data.ExtractNonce(noncePosition, nonceLength);

            return(new FarmingResult
            {
                Data = data,
                Nonce = nonce,
                Hash = hash,
                HashCount = hashCount,
                HashPerSecond = (hashCount * 1000.0F) / watch.ElapsedMilliseconds,
                FarmingTime = watch.ElapsedMilliseconds
            });
        }
Ejemplo n.º 2
0
        public FarmingConfig()
        {
            this.NoncePosition = FarmingNoncePosition.Tail;
            this.NonceLength   = 8;
            this.NonceInData   = true;
            this.NonceFromZero = false;
            //this.FlexNonce = false;

            this.Challenge      = FarmingBytes.LessOrEqualThan;
            this.ChallengeValue = FarmingBytes.Unpack(0x1D00FFFF);

            this.HashRecipe = new[] { CryptoHashName.Sha256 };
        }
Ejemplo n.º 3
0
        /// <summary>
        /// ES: Comprobación del Nonce. Calcula el Hash con el Nonce indicado y comprueba que coincide
        /// con el Hash propuesto
        /// </summary>
        /// <param name="data">ES: Datos origen del Hash</param>
        /// <param name="nonce">ES: Nonce propuesto</param>
        /// <param name="config">ES: Configuración de labrado</param>
        /// <returns>ES: Resultados incluyendo el Nonce y Hash recalculados</returns>
        public static FarmingResult CheckNonce(byte[] data, byte[] nonce, FarmingConfig config)
        {
            var challenge   = config.Challenge ?? FarmingBytes.LessOrEqualThan;
            var hasherStack = new HashRecipe(config.HashRecipe);

            byte[] hash = hasherStack.ComputeHash(data);

            var result = new FarmingResult
            {
                Data      = data,
                Nonce     = nonce,
                Hash      = hash,
                HashCount = 0
            };

            if (challenge(hash, config.ChallengeValue))
            {
                int noncePosition;

                if (config.NoncePosition == FarmingNoncePosition.Head)
                {
                    noncePosition = 0;
                }
                else
                {
                    noncePosition = data.Length - config.NonceLength;
                }

                var dataNonce = data.ExtractNonce(noncePosition, config.NonceLength);
                result.Nonce = dataNonce;

                if (FarmingBytes.NoncesAreEqual(dataNonce, nonce))
                {
                    result.HashCount = 1;
                }
            }

            return(result);
        }