sha256_via.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. uint32_t *nonce_inout = (uint32_t *)(data_inout + 64 + 12);
  31. unsigned long stat_ctr = 0;
  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. swap32yes(data32, data_inout, 128/4);
  38. while (1) {
  39. n++;
  40. *nonce = n;
  41. /* first SHA256 transform */
  42. memcpy(tmp_hash1, sha256_init_state, 32);
  43. via_sha256(tmp_hash1, data, 80); /* or maybe 128? */
  44. swap32yes(tmp_hash1, tmp_hash1, 32/4);
  45. /* second SHA256 transform */
  46. memcpy(tmp_hash, sha256_init_state, 32);
  47. via_sha256(tmp_hash, tmp_hash1, 32);
  48. stat_ctr++;
  49. if (unlikely((hash32[7] == 0) && fulltest(tmp_hash, target))) {
  50. /* swap nonce'd data back into original storage area;
  51. */
  52. *nonce_inout = bswap_32(n);
  53. *last_nonce = n;
  54. return true;
  55. }
  56. if ((n >= max_nonce) || thr->work_restart) {
  57. *last_nonce = n;
  58. return false;
  59. }
  60. }
  61. }
  62. #endif /* WANT_VIA_PADLOCK */