Precompiled.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file Precompiled.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #include "Precompiled.h"
  19. #include <libdevcore/Log.h>
  20. #include <libdevcore/SHA3.h>
  21. #include <libdevcore/Hash.h>
  22. #include <libdevcrypto/Common.h>
  23. #include <libethcore/Common.h>
  24. using namespace std;
  25. using namespace dev;
  26. using namespace dev::eth;
  27. PrecompiledRegistrar* PrecompiledRegistrar::s_this = nullptr;
  28. PrecompiledExecutor const& PrecompiledRegistrar::executor(std::string const& _name)
  29. {
  30. if (!get()->m_execs.count(_name))
  31. BOOST_THROW_EXCEPTION(ExecutorNotFound());
  32. return get()->m_execs[_name];
  33. }
  34. namespace
  35. {
  36. ETH_REGISTER_PRECOMPILED(ecrecover)(bytesConstRef _in, bytesRef _out)
  37. {
  38. struct inType
  39. {
  40. h256 hash;
  41. h256 v;
  42. h256 r;
  43. h256 s;
  44. } in;
  45. memcpy(&in, _in.data(), min(_in.size(), sizeof(in)));
  46. h256 ret;
  47. u256 v = (u256)in.v;
  48. if (v >= 27 && v <= 28)
  49. {
  50. SignatureStruct sig(in.r, in.s, (byte)((int)v - 27));
  51. if (sig.isValid())
  52. {
  53. try
  54. {
  55. if (Public rec = recover(sig, in.hash))
  56. {
  57. ret = dev::sha3(rec);
  58. memset(ret.data(), 0, 12);
  59. ret.ref().copyTo(_out);
  60. }
  61. }
  62. catch (...) {}
  63. }
  64. }
  65. }
  66. ETH_REGISTER_PRECOMPILED(sha256)(bytesConstRef _in, bytesRef _out)
  67. {
  68. dev::sha256(_in).ref().copyTo(_out);
  69. }
  70. ETH_REGISTER_PRECOMPILED(ripemd160)(bytesConstRef _in, bytesRef _out)
  71. {
  72. h256(dev::ripemd160(_in), h256::AlignRight).ref().copyTo(_out);
  73. }
  74. ETH_REGISTER_PRECOMPILED(identity)(bytesConstRef _in, bytesRef _out)
  75. {
  76. _in.copyTo(_out);
  77. }
  78. }