code-generator.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright (c) 2018-2019, tevador <tevador@gmail.com>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  18. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "utility.hpp"
  26. #include "../common.hpp"
  27. #include "../assembly_generator_x86.hpp"
  28. #include "../superscalar.hpp"
  29. #include "../aes_hash.hpp"
  30. #include "../blake2/blake2.h"
  31. #include "../program.hpp"
  32. const uint8_t seed[32] = { 191, 182, 222, 175, 249, 89, 134, 104, 241, 68, 191, 62, 162, 166, 61, 64, 123, 191, 227, 193, 118, 60, 188, 53, 223, 133, 175, 24, 123, 230, 55, 74 };
  33. const uint8_t blockTemplate_[] = {
  34. 0x07, 0x07, 0xf7, 0xa4, 0xf0, 0xd6, 0x05, 0xb3, 0x03, 0x26, 0x08, 0x16, 0xba, 0x3f, 0x10, 0x90, 0x2e, 0x1a, 0x14,
  35. 0x5a, 0xc5, 0xfa, 0xd3, 0xaa, 0x3a, 0xf6, 0xea, 0x44, 0xc1, 0x18, 0x69, 0xdc, 0x4f, 0x85, 0x3f, 0x00, 0x2b, 0x2e,
  36. 0xea, 0x00, 0x00, 0x00, 0x00, 0x77, 0xb2, 0x06, 0xa0, 0x2c, 0xa5, 0xb1, 0xd4, 0xce, 0x6b, 0xbf, 0xdf, 0x0a, 0xca,
  37. 0xc3, 0x8b, 0xde, 0xd3, 0x4d, 0x2d, 0xcd, 0xee, 0xf9, 0x5c, 0xd2, 0x0c, 0xef, 0xc1, 0x2f, 0x61, 0xd5, 0x61, 0x09
  38. };
  39. template<bool softAes>
  40. void generateAsm(uint32_t nonce) {
  41. alignas(16) uint64_t hash[8];
  42. uint8_t blockTemplate[sizeof(blockTemplate_)];
  43. memcpy(blockTemplate, blockTemplate_, sizeof(blockTemplate));
  44. store32(blockTemplate + 39, nonce);
  45. blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0);
  46. uint8_t scratchpad[randomx::ScratchpadSize];
  47. fillAes1Rx4<softAes>((void*)hash, randomx::ScratchpadSize, scratchpad);
  48. randomx::AssemblyGeneratorX86 asmX86;
  49. randomx::Program p;
  50. fillAes4Rx4<softAes>(hash, sizeof(p), &p);
  51. asmX86.generateProgram(p);
  52. asmX86.printCode(std::cout);
  53. }
  54. template<bool softAes>
  55. void generateNative(uint32_t nonce) {
  56. alignas(16) uint64_t hash[8];
  57. uint8_t blockTemplate[sizeof(blockTemplate_)];
  58. memcpy(blockTemplate, blockTemplate_, sizeof(blockTemplate));
  59. store32(blockTemplate + 39, nonce);
  60. blake2b(hash, sizeof(hash), blockTemplate, sizeof(blockTemplate), nullptr, 0);
  61. uint8_t scratchpad[randomx::ScratchpadSize];
  62. fillAes1Rx4<softAes>((void*)hash, randomx::ScratchpadSize, scratchpad);
  63. alignas(16) randomx::Program prog;
  64. fillAes1Rx4<softAes>((void*)hash, sizeof(prog), &prog);
  65. std::cout << prog << std::endl;
  66. }
  67. void printUsage(const char* executable) {
  68. std::cout << "Usage: " << executable << " [OPTIONS]" << std::endl;
  69. std::cout << "Supported options:" << std::endl;
  70. std::cout << " --softAes use software AES (default: x86 AES-NI)" << std::endl;
  71. std::cout << " --nonce N seed nonce (default: 1000)" << std::endl;
  72. std::cout << " --genAsm generate x86-64 asm code for nonce N" << std::endl;
  73. std::cout << " --genNative generate RandomX code for nonce N" << std::endl;
  74. std::cout << " --genSuperscalar generate superscalar program for nonce N" << std::endl;
  75. }
  76. int main(int argc, char** argv) {
  77. bool softAes, genAsm, genNative, genSuperscalar;
  78. int nonce;
  79. readOption("--softAes", argc, argv, softAes);
  80. readOption("--genAsm", argc, argv, genAsm);
  81. readIntOption("--nonce", argc, argv, nonce, 1000);
  82. readOption("--genNative", argc, argv, genNative);
  83. readOption("--genSuperscalar", argc, argv, genSuperscalar);
  84. if (genSuperscalar) {
  85. randomx::SuperscalarProgram p;
  86. randomx::Blake2Generator gen(seed, nonce);
  87. randomx::generateSuperscalar(p, gen);
  88. randomx::AssemblyGeneratorX86 asmX86;
  89. asmX86.generateAsm(p);
  90. asmX86.printCode(std::cout);
  91. return 0;
  92. }
  93. if (genAsm) {
  94. if (softAes)
  95. generateAsm<true>(nonce);
  96. else
  97. generateAsm<false>(nonce);
  98. return 0;
  99. }
  100. if (genNative) {
  101. if (softAes)
  102. generateNative<true>(nonce);
  103. else
  104. generateNative<false>(nonce);
  105. return 0;
  106. }
  107. printUsage(argv[0]);
  108. return 0;
  109. }