Fabcoin Core  0.16.2
P2P Digital Currency
Precompiled.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include "Precompiled.h"
23 #include <libdevcore/Log.h>
24 #include <libdevcore/SHA3.h>
25 #include <libdevcore/Hash.h>
26 #include <libdevcrypto/Common.h>
27 #include <libethcore/Common.h>
28 using namespace std;
29 using namespace dev;
30 using namespace dev::eth;
31 
32 PrecompiledRegistrar* PrecompiledRegistrar::s_this = nullptr;
33 
34 PrecompiledExecutor const& PrecompiledRegistrar::executor(std::string const& _name)
35 {
36  if (!get()->m_execs.count(_name))
37  BOOST_THROW_EXCEPTION(ExecutorNotFound());
38  return get()->m_execs[_name];
39 }
40 
41 namespace
42 {
43 
45 {
46  struct
47  {
48  h256 hash;
49  h256 v;
50  h256 r;
51  h256 s;
52  } in;
53 
54  memcpy(&in, _in.data(), min(_in.size(), sizeof(in)));
55 
56  h256 ret;
57  u256 v = (u256)in.v;
58  if (v >= 27 && v <= 28)
59  {
60  SignatureStruct sig(in.r, in.s, (byte)((int)v - 27));
61  if (sig.isValid())
62  {
63  try
64  {
65  if (Public rec = recover(sig, in.hash))
66  {
67  ret = dev::sha3(rec);
68  memset(ret.data(), 0, 12);
69  return {true, ret.asBytes()};
70  }
71  }
72  catch (...) {}
73  }
74  }
75  return {true, {}};
76 }
77 
79 {
80  return {true, dev::sha256(_in).asBytes()};
81 }
82 
84 {
85  return {true, h256(dev::ripemd160(_in), h256::AlignRight).asBytes()};
86 }
87 
89 {
90  return {true, _in.toBytes()};
91 }
92 
93 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
uint8_t byte
Definition: Common.h:57
std::hash for asio::adress
Definition: Common.h:323
ExecStats::duration min
Definition: ExecStats.cpp:35
if(a.IndicesBefore(b, len, lenIndices))
Definition: equihash.cpp:243
std::function< std::pair< bool, bytes >(bytesConstRef _in)> PrecompiledExecutor
Definition: Precompiled.h:34
Public recover(Signature const &_sig, h256 const &_hash)
Recovers Public key from signed message hash.
Definition: Common.cpp:203
FixedHash< 32 > h256
Definition: FixedHash.h:340
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
bytes asBytes() const
Definition: FixedHash.h:145
h160 ripemd160(bytesConstRef _input)
Definition: Hash.cpp:400
void * memcpy(void *a, const void *b, size_t c)
h256 sha256(bytesConstRef _input)
Definition: Hash.cpp:33
Internal SHA-256 implementation.
Definition: sha256.cpp:26
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
#define ETH_REGISTER_PRECOMPILED(Name)
Definition: Precompiled.h:57
bool isValid() const noexcept
Definition: Common.cpp:57
Internal RIPEMD-160 implementation.
Definition: ripemd160.cpp:15