From 12f6ec16fb5d528bb8b58f0b385fbc741ca96dae Mon Sep 17 00:00:00 2001 From: Piotr Gankiewicz Date: Fri, 23 Aug 2019 14:56:19 +0200 Subject: [PATCH] Added Blake2Fast lib #816 --- src/Nethermind/Nethermind.Evm/Metrics.cs | 1 + .../Nethermind.Evm/Nethermind.Evm.csproj | 1 + .../Precompiles/Blake2BPrecompiledContract.cs | 44 +++++++++++++++++++ .../Nethermind.Evm/VirtualMachine.cs | 3 +- 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/Nethermind/Nethermind.Evm/Precompiles/Blake2BPrecompiledContract.cs diff --git a/src/Nethermind/Nethermind.Evm/Metrics.cs b/src/Nethermind/Nethermind.Evm/Metrics.cs index 48c93772bd5..d06428b88e6 100644 --- a/src/Nethermind/Nethermind.Evm/Metrics.cs +++ b/src/Nethermind/Nethermind.Evm/Metrics.cs @@ -34,5 +34,6 @@ public class Metrics public static long ModExpPrecompile { get; set; } public static long Ripemd160Precompile { get; set; } public static long Sha256Precompile { get; set; } + public static long Blake2BPrecompile { get; set; } } } \ No newline at end of file diff --git a/src/Nethermind/Nethermind.Evm/Nethermind.Evm.csproj b/src/Nethermind/Nethermind.Evm/Nethermind.Evm.csproj index 7048bab7558..b03f86303a6 100644 --- a/src/Nethermind/Nethermind.Evm/Nethermind.Evm.csproj +++ b/src/Nethermind/Nethermind.Evm/Nethermind.Evm.csproj @@ -13,6 +13,7 @@ + diff --git a/src/Nethermind/Nethermind.Evm/Precompiles/Blake2BPrecompiledContract.cs b/src/Nethermind/Nethermind.Evm/Precompiles/Blake2BPrecompiledContract.cs new file mode 100644 index 00000000000..c1423b0ef0e --- /dev/null +++ b/src/Nethermind/Nethermind.Evm/Precompiles/Blake2BPrecompiledContract.cs @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2018 Demerzel Solutions Limited + * This file is part of the Nethermind library. + * + * The Nethermind library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * The Nethermind library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with the Nethermind. If not, see . + */ + +using Nethermind.Core; +using Nethermind.Core.Crypto.ZkSnarks; +using Nethermind.Core.Extensions; +using Nethermind.Core.Specs; +using SauceControl.Blake2Fast; + +namespace Nethermind.Evm.Precompiles +{ + public class Blake2BPrecompiledContract : IPrecompiledContract + { + public static readonly IPrecompiledContract Instance = new Blake2BPrecompiledContract(); + + public Address Address { get; } = Address.FromNumber(9); + + public long BaseGasCost(IReleaseSpec releaseSpec) => 0; + + public long DataGasCost(byte[] inputData, IReleaseSpec releaseSpec) => 12; + + public (byte[], bool) Run(byte[] inputData) + { + Metrics.Blake2BPrecompile++; + + return (inputData, true); + } + } +} \ No newline at end of file diff --git a/src/Nethermind/Nethermind.Evm/VirtualMachine.cs b/src/Nethermind/Nethermind.Evm/VirtualMachine.cs index 3e68ffe5b6a..e87e07e9f4c 100644 --- a/src/Nethermind/Nethermind.Evm/VirtualMachine.cs +++ b/src/Nethermind/Nethermind.Evm/VirtualMachine.cs @@ -395,7 +395,8 @@ private void InitializePrecompiledContracts() [Bn128AddPrecompiledContract.Instance.Address] = Bn128AddPrecompiledContract.Instance, [Bn128MulPrecompiledContract.Instance.Address] = Bn128MulPrecompiledContract.Instance, [Bn128PairingPrecompiledContract.Instance.Address] = Bn128PairingPrecompiledContract.Instance, - [ModExpPrecompiledContract.Instance.Address] = ModExpPrecompiledContract.Instance + [ModExpPrecompiledContract.Instance.Address] = ModExpPrecompiledContract.Instance, + [Blake2BPrecompiledContract.Instance.Address] = Blake2BPrecompiledContract.Instance, }; }