sha256_via.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <sys/time.h>
  6. #include "miner.h"
  7. #ifdef WANT_VIA_PADLOCK
  8. #define ___constant_swab32(x) ((uint32_t)( \
  9. (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
  10. (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
  11. (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
  12. (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
  13. static inline uint32_t swab32(uint32_t v)
  14. {
  15. return ___constant_swab32(v);
  16. }
  17. static void via_sha256(void *hash, void *buf, unsigned len)
  18. {
  19. unsigned stat = 0;
  20. asm volatile(".byte 0xf3, 0x0f, 0xa6, 0xd0"
  21. :"+S"(buf), "+a"(stat)
  22. :"c"(len), "D" (hash)
  23. :"memory");
  24. }
  25. bool scanhash_via(unsigned char *midstate, const unsigned char *data_in,
  26. unsigned char *hash1, unsigned char *hash,
  27. unsigned long *hashes_done)
  28. {
  29. unsigned char data[128] __attribute__((aligned(128)));
  30. unsigned char tmp_hash1[32] __attribute__((aligned(32)));
  31. uint32_t *hash32 = (uint32_t *) hash;
  32. uint32_t *nonce = (uint32_t *)(data + 12);
  33. uint32_t n = 0;
  34. unsigned long stat_ctr = 0;
  35. int i;
  36. /* bitcoin gives us big endian input, but via wants LE,
  37. * so we reverse the swapping bitcoin has already done (extra work)
  38. * in order to permit the hardware to swap everything
  39. * back to BE again (extra work).
  40. */
  41. for (i = 0; i < 128/4; i++) {
  42. uint32_t *data32 = (uint32_t *) data;
  43. data32[i] = swab32(((uint32_t *)data_in)[i]);
  44. }
  45. while (1) {
  46. n++;
  47. *nonce = n;
  48. /* first SHA256 transform */
  49. memcpy(tmp_hash1, sha256_init_state, 32);
  50. via_sha256(tmp_hash1, data, 80); /* or maybe 128? */
  51. for (i = 0; i < 32/4; i++)
  52. ((uint32_t *)tmp_hash1)[i] =
  53. swab32(((uint32_t *)tmp_hash1)[i]);
  54. /* second SHA256 transform */
  55. memcpy(hash, sha256_init_state, 32);
  56. via_sha256(hash, tmp_hash1, 32);
  57. stat_ctr++;
  58. if (hash32[7] == 0) {
  59. char *hexstr;
  60. hexstr = bin2hex(hash, 32);
  61. fprintf(stderr,
  62. "DBG: found zeroes in hash:\n%s\n",
  63. hexstr);
  64. free(hexstr);
  65. *hashes_done = stat_ctr;
  66. return true;
  67. }
  68. if ((n & 0xffffff) == 0) {
  69. if (opt_debug)
  70. fprintf(stderr, "DBG: end of nonce range\n");
  71. *hashes_done = stat_ctr;
  72. return false;
  73. }
  74. }
  75. }
  76. #endif /* WANT_VIA_PADLOCK */