sha256_via.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "driver-cpu.h"
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <sys/time.h>
  7. #include "miner.h"
  8. #ifdef WANT_VIA_PADLOCK
  9. static void via_sha256(void *hash, void *buf, unsigned len)
  10. {
  11. unsigned stat = 0;
  12. asm volatile(".byte 0xf3, 0x0f, 0xa6, 0xd0"
  13. :"+S"(buf), "+a"(stat)
  14. :"c"(len), "D" (hash)
  15. :"memory");
  16. }
  17. bool scanhash_via(struct thr_info*thr, const unsigned char *pmidstate,
  18. unsigned char *data_inout,
  19. unsigned char *phash1, unsigned char *phash,
  20. const unsigned char *target,
  21. uint32_t max_nonce, uint32_t *last_nonce,
  22. uint32_t n)
  23. {
  24. unsigned char data[128] __attribute__((aligned(128)));
  25. unsigned char tmp_hash[32] __attribute__((aligned(128)));
  26. unsigned char tmp_hash1[32] __attribute__((aligned(128)));
  27. uint32_t *data32 = (uint32_t *) data;
  28. uint32_t *hash32 = (uint32_t *) tmp_hash;
  29. uint32_t *nonce = (uint32_t *)(data + 64 + 12);
  30. unsigned long stat_ctr = 0;
  31. int i;
  32. /* bitcoin gives us big endian input, but via wants LE,
  33. * so we reverse the swapping bitcoin has already done (extra work)
  34. * in order to permit the hardware to swap everything
  35. * back to BE again (extra work).
  36. */
  37. for (i = 0; i < 128/4; i++)
  38. data32[i] = swab32(((uint32_t *)data_inout)[i]);
  39. while (1) {
  40. n++;
  41. *nonce = n;
  42. /* first SHA256 transform */
  43. memcpy(tmp_hash1, sha256_init_state, 32);
  44. via_sha256(tmp_hash1, data, 80); /* or maybe 128? */
  45. for (i = 0; i < 32/4; i++)
  46. ((uint32_t *)tmp_hash1)[i] =
  47. swab32(((uint32_t *)tmp_hash1)[i]);
  48. /* second SHA256 transform */
  49. memcpy(tmp_hash, sha256_init_state, 32);
  50. via_sha256(tmp_hash, tmp_hash1, 32);
  51. stat_ctr++;
  52. if (unlikely((hash32[7] == 0) && fulltest(tmp_hash, target))) {
  53. /* swap nonce'd data back into original storage area;
  54. * TODO: only swap back the nonce, rather than all data
  55. */
  56. for (i = 0; i < 128/4; i++) {
  57. uint32_t *dout32 = (uint32_t *) data_inout;
  58. dout32[i] = swab32(data32[i]);
  59. }
  60. *last_nonce = n;
  61. return true;
  62. }
  63. if ((n >= max_nonce) || thr->work_restart) {
  64. *last_nonce = n;
  65. return false;
  66. }
  67. }
  68. }
  69. #endif /* WANT_VIA_PADLOCK */