sha256_via.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 *data_inout, unsigned long *hashes_done)
  26. {
  27. unsigned char data[128] __attribute__((aligned(128)));
  28. unsigned char tmp_hash[32] __attribute__((aligned(128)));
  29. unsigned char tmp_hash1[32] __attribute__((aligned(128)));
  30. uint32_t *data32 = (uint32_t *) data;
  31. uint32_t *hash32 = (uint32_t *) tmp_hash;
  32. uint32_t *nonce = (uint32_t *)(data + 64 + 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. data32[i] = swab32(((uint32_t *)data_inout)[i]);
  43. while (1) {
  44. n++;
  45. *nonce = n;
  46. /* first SHA256 transform */
  47. memcpy(tmp_hash1, sha256_init_state, 32);
  48. via_sha256(tmp_hash1, data, 80); /* or maybe 128? */
  49. for (i = 0; i < 32/4; i++)
  50. ((uint32_t *)tmp_hash1)[i] =
  51. swab32(((uint32_t *)tmp_hash1)[i]);
  52. /* second SHA256 transform */
  53. memcpy(tmp_hash, sha256_init_state, 32);
  54. via_sha256(tmp_hash, tmp_hash1, 32);
  55. stat_ctr++;
  56. if (hash32[7] == 0) {
  57. char *hexstr;
  58. hexstr = bin2hex(tmp_hash, 32);
  59. fprintf(stderr,
  60. "DBG: found zeroes in hash:\n%s\n",
  61. hexstr);
  62. free(hexstr);
  63. /* swap nonce'd data back into original storage area;
  64. * TODO: only swap back the nonce, rather than all data
  65. */
  66. for (i = 0; i < 128/4; i++) {
  67. uint32_t *dout32 = (uint32_t *) data_inout;
  68. dout32[i] = swab32(data32[i]);
  69. }
  70. *hashes_done = stat_ctr;
  71. return true;
  72. }
  73. if ((n & 0xffffff) == 0) {
  74. if (opt_debug)
  75. fprintf(stderr, "DBG: end of nonce range\n");
  76. *hashes_done = stat_ctr;
  77. return false;
  78. }
  79. }
  80. }
  81. #endif /* WANT_VIA_PADLOCK */