sha256_via.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 __maybe_unused *pmidstate,
  18. unsigned char *data_inout,
  19. unsigned char __maybe_unused *phash1, unsigned char __maybe_unused *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. /* bitcoin gives us big endian input, but via wants LE,
  32. * so we reverse the swapping bitcoin has already done (extra work)
  33. * in order to permit the hardware to swap everything
  34. * back to BE again (extra work).
  35. */
  36. swap32yes(data32, data_inout, 128/4);
  37. while (1) {
  38. *nonce = n;
  39. /* first SHA256 transform */
  40. memcpy(tmp_hash1, sha256_init_state, 32);
  41. via_sha256(tmp_hash1, data, 80); /* or maybe 128? */
  42. swap32yes(tmp_hash1, tmp_hash1, 32/4);
  43. /* second SHA256 transform */
  44. memcpy(tmp_hash, sha256_init_state, 32);
  45. via_sha256(tmp_hash, tmp_hash1, 32);
  46. stat_ctr++;
  47. if (unlikely((hash32[7] == 0) && fulltest(tmp_hash, target))) {
  48. /* swap nonce'd data back into original storage area;
  49. * TODO: only swap back the nonce, rather than all data
  50. */
  51. swap32yes(data_inout, data32, 128/4);
  52. *last_nonce = n;
  53. return true;
  54. }
  55. if ((n >= max_nonce) || thr->work_restart) {
  56. *last_nonce = n;
  57. return false;
  58. }
  59. n++;
  60. }
  61. }
  62. #endif /* WANT_VIA_PADLOCK */