OhGodAPet

Original (dumb) Minotaur CPU Miner code

Jun 24th, 2022
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.49 KB | None | 0 0
  1. #include <miner.h>
  2.  
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7.  
  8. #include <sha3/sph_blake.h>
  9. #include <sha3/sph_bmw.h>
  10. #include <sha3/sph_groestl.h>
  11. #include <sha3/sph_jh.h>
  12. #include <sha3/sph_keccak.h>
  13. #include <sha3/sph_skein.h>
  14. #include <sha3/sph_luffa.h>
  15. #include <sha3/sph_cubehash.h>
  16. #include <sha3/sph_shavite.h>
  17. #include <sha3/sph_simd.h>
  18. #include <sha3/sph_echo.h>
  19. #include <sha3/sph_hamsi.h>
  20. #include <sha3/sph_fugue.h>
  21. #include <sha3/sph_shabal.h>
  22. #include <sha3/sph_whirlpool.h>
  23. #include <sha3/sph_sha2.h>
  24.  
  25. // Config
  26. #define MINOTAUR_ALGO_COUNT     16
  27. //#define MINOTAUR_DEBUG
  28.  
  29. typedef struct TortureNode TortureNode;
  30. typedef struct TortureGarden TortureGarden;
  31.  
  32. // Graph of hash algos plus SPH contexts
  33. struct TortureGarden {
  34.     sph_blake512_context context_blake;
  35.     sph_bmw512_context context_bmw;
  36.     sph_cubehash512_context context_cubehash;
  37.     sph_echo512_context context_echo;
  38.     sph_fugue512_context context_fugue;
  39.     sph_groestl512_context context_groestl;
  40.     sph_hamsi512_context context_hamsi;
  41.     sph_jh512_context context_jh;
  42.     sph_keccak512_context context_keccak;
  43.     sph_luffa512_context context_luffa;
  44.     sph_shabal512_context context_shabal;
  45.     sph_shavite512_context context_shavite;
  46.     sph_simd512_context context_simd;
  47.     sph_skein512_context context_skein;
  48.     sph_whirlpool_context context_whirlpool;
  49.     sph_sha512_context context_sha2;
  50.  
  51.     struct TortureNode {
  52.         unsigned int algo;
  53.         TortureNode *childLeft;
  54.         TortureNode *childRight;
  55.     } nodes[22];
  56. };
  57.  
  58. // Get a 64-byte hash for given 64-byte input, using given TortureGarden contexts and given algo index
  59. void get_hash(void *output, const void *input, TortureGarden *garden, unsigned int algo)
  60. {
  61.     unsigned char _ALIGN(64) hash[64];
  62.  
  63.     printf("Executing algo #%d!\n", algo);
  64.     printf("Input hash: ");
  65.    
  66.     for(int i = 0; i < 64; ++i) printf("%02X", ((uint8_t *)input)[i]);
  67.    
  68.     switch (algo) {
  69.         case 0:
  70.             sph_blake512_init(&garden->context_blake);
  71.             sph_blake512(&garden->context_blake, input, 64);
  72.             sph_blake512_close(&garden->context_blake, hash);
  73.             break;
  74.         case 1:
  75.             sph_bmw512_init(&garden->context_bmw);
  76.             sph_bmw512(&garden->context_bmw, input, 64);
  77.             sph_bmw512_close(&garden->context_bmw, hash);
  78.             break;
  79.         case 2:
  80.             sph_cubehash512_init(&garden->context_cubehash);
  81.             sph_cubehash512(&garden->context_cubehash, input, 64);
  82.             sph_cubehash512_close(&garden->context_cubehash, hash);
  83.             break;
  84.         case 3:
  85.             sph_echo512_init(&garden->context_echo);
  86.             sph_echo512(&garden->context_echo, input, 64);
  87.             sph_echo512_close(&garden->context_echo, hash);
  88.             break;
  89.         case 4:
  90.             sph_fugue512_init(&garden->context_fugue);
  91.             sph_fugue512(&garden->context_fugue, input, 64);
  92.             sph_fugue512_close(&garden->context_fugue, hash);
  93.             break;
  94.         case 5:
  95.             sph_groestl512_init(&garden->context_groestl);
  96.             sph_groestl512(&garden->context_groestl, input, 64);
  97.             sph_groestl512_close(&garden->context_groestl, hash);
  98.             break;
  99.         case 6:
  100.             sph_hamsi512_init(&garden->context_hamsi);
  101.             sph_hamsi512(&garden->context_hamsi, input, 64);
  102.             sph_hamsi512_close(&garden->context_hamsi, hash);
  103.             break;
  104.         case 7:
  105.             sph_sha512_init(&garden->context_sha2);
  106.             sph_sha512(&garden->context_sha2, input, 64);
  107.             sph_sha512_close(&garden->context_sha2, hash);
  108.             break;
  109.         case 8:
  110.             sph_jh512_init(&garden->context_jh);
  111.             sph_jh512(&garden->context_jh, input, 64);
  112.             sph_jh512_close(&garden->context_jh, hash);
  113.             break;
  114.         case 9:
  115.             sph_keccak512_init(&garden->context_keccak);
  116.             sph_keccak512(&garden->context_keccak, input, 64);
  117.             sph_keccak512_close(&garden->context_keccak, hash);
  118.             break;
  119.         case 10:
  120.             sph_luffa512_init(&garden->context_luffa);
  121.             sph_luffa512(&garden->context_luffa, input, 64);
  122.             sph_luffa512_close(&garden->context_luffa, hash);
  123.             break;
  124.         case 11:
  125.             sph_shabal512_init(&garden->context_shabal);
  126.             sph_shabal512(&garden->context_shabal, input, 64);
  127.             sph_shabal512_close(&garden->context_shabal, hash);
  128.             break;
  129.         case 12:
  130.             sph_shavite512_init(&garden->context_shavite);
  131.             sph_shavite512(&garden->context_shavite, input, 64);
  132.             sph_shavite512_close(&garden->context_shavite, hash);
  133.             break;
  134.         case 13:
  135.             sph_simd512_init(&garden->context_simd);
  136.             sph_simd512(&garden->context_simd, input, 64);
  137.             sph_simd512_close(&garden->context_simd, hash);
  138.             break;
  139.         case 14:
  140.             sph_skein512_init(&garden->context_skein);
  141.             sph_skein512(&garden->context_skein, input, 64);
  142.             sph_skein512_close(&garden->context_skein, hash);
  143.             break;
  144.         case 15:
  145.             sph_whirlpool_init(&garden->context_whirlpool);
  146.             sph_whirlpool(&garden->context_whirlpool, input, 64);
  147.             sph_whirlpool_close(&garden->context_whirlpool, hash);
  148.             break;
  149.     }
  150.  
  151.     for(int i = 0; i < 64; ++i) printf("%02X", hash[i]);
  152.  
  153.     putchar('\n');
  154.  
  155.     // Output the hash
  156.     memcpy(output, hash, 64);
  157. }
  158.  
  159. // Recursively traverse a given torture garden starting with a given hash and given node within the garden. The hash is overwritten with the final hash.
  160. void traverse_garden(TortureGarden *garden, void *hash, TortureNode *node)
  161. {
  162.     unsigned char _ALIGN(64) partialHash[64];
  163.     get_hash(partialHash, hash, garden, node->algo);
  164.  
  165.     if (partialHash[63] % 2 == 0) {                                     // Last byte of output hash is even
  166.         if (node->childLeft != NULL)
  167.             traverse_garden(garden, partialHash, node->childLeft);
  168.     } else {                                                            // Last byte of output hash is odd
  169.         if (node->childRight != NULL)
  170.             traverse_garden(garden, partialHash, node->childRight);
  171.     }
  172.  
  173.     memcpy(hash, partialHash, 64);
  174. }
  175.  
  176. // Associate child nodes with a parent node
  177. inline void link_nodes(TortureNode *parent, TortureNode *childLeft, TortureNode *childRight)
  178. {
  179.     parent->childLeft = childLeft;
  180.     parent->childRight = childRight;
  181. }
  182.  
  183. // Produce a 32-byte hash from 80-byte input data
  184. void minotaurhash(void *output, const void *input)
  185. {
  186.     // Create torture garden nodes. Note that both sides of 19 and 20 lead to 21, and 21 has no children (to make traversal complete).
  187.     // Every path through the garden stops at 7 nodes.
  188.     TortureGarden garden;
  189.     link_nodes(&garden.nodes[0], &garden.nodes[1], &garden.nodes[2]);
  190.     link_nodes(&garden.nodes[1], &garden.nodes[3], &garden.nodes[4]);
  191.     link_nodes(&garden.nodes[2], &garden.nodes[5], &garden.nodes[6]);
  192.     link_nodes(&garden.nodes[3], &garden.nodes[7], &garden.nodes[8]);
  193.     link_nodes(&garden.nodes[4], &garden.nodes[9], &garden.nodes[10]);
  194.     link_nodes(&garden.nodes[5], &garden.nodes[11], &garden.nodes[12]);
  195.     link_nodes(&garden.nodes[6], &garden.nodes[13], &garden.nodes[14]);
  196.     link_nodes(&garden.nodes[7], &garden.nodes[15], &garden.nodes[16]);
  197.     link_nodes(&garden.nodes[8], &garden.nodes[15], &garden.nodes[16]);
  198.     link_nodes(&garden.nodes[9], &garden.nodes[15], &garden.nodes[16]);
  199.     link_nodes(&garden.nodes[10], &garden.nodes[15], &garden.nodes[16]);
  200.     link_nodes(&garden.nodes[11], &garden.nodes[17], &garden.nodes[18]);
  201.     link_nodes(&garden.nodes[12], &garden.nodes[17], &garden.nodes[18]);
  202.     link_nodes(&garden.nodes[13], &garden.nodes[17], &garden.nodes[18]);
  203.     link_nodes(&garden.nodes[14], &garden.nodes[17], &garden.nodes[18]);
  204.     link_nodes(&garden.nodes[15], &garden.nodes[19], &garden.nodes[20]);
  205.     link_nodes(&garden.nodes[16], &garden.nodes[19], &garden.nodes[20]);
  206.     link_nodes(&garden.nodes[17], &garden.nodes[19], &garden.nodes[20]);
  207.     link_nodes(&garden.nodes[18], &garden.nodes[19], &garden.nodes[20]);
  208.     link_nodes(&garden.nodes[19], &garden.nodes[21], &garden.nodes[21]);
  209.     link_nodes(&garden.nodes[20], &garden.nodes[21], &garden.nodes[21]);
  210.     garden.nodes[21].childLeft = NULL;
  211.     garden.nodes[21].childRight = NULL;
  212.    
  213.     // Find initial sha512 hash
  214.     unsigned char _ALIGN(64) hash[64];
  215.     sph_sha512_init(&garden.context_sha2);
  216.     sph_sha512(&garden.context_sha2, input, 80);
  217.     sph_sha512_close(&garden.context_sha2, hash);
  218.  
  219.     printf("First hash!\n");
  220.     for(int i = 0; i < 32; ++i) printf("%02X", hash[i]);
  221.  
  222.     printf("\n\n");
  223.  
  224.     // Assign algos to torture garden nodes based on initial hash
  225.     for (int i = 0; i < 22; i++)
  226.         garden.nodes[i].algo = hash[i] % MINOTAUR_ALGO_COUNT;
  227.    
  228.     printf("Order: %d %d %d %d %d %d %d %d\n", garden.nodes[0].algo, garden.nodes[1].algo, garden.nodes[2].algo, garden.nodes[3].algo, garden.nodes[4].algo, garden.nodes[5].algo, garden.nodes[6].algo, garden.nodes[7].algo);
  229.    
  230.     // Send the initial hash through the torture garden
  231.     traverse_garden(&garden, hash, &garden.nodes[0]);
  232.  
  233.     // Truncate the result to 32 bytes
  234.     memcpy(output, hash, 32);
  235.  
  236. #ifdef MINOTAUR_DEBUG
  237.     printf("*** Final hash:\t\t");
  238.     for (int i = 31; i >= 0; i--) printf("%02x", output[i]);
  239.     printf("\n");
  240.  
  241.     fflush(0);
  242. #endif
  243. }
Advertisement
Add Comment
Please, Sign In to add comment