sha256_via.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "config.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(int thr_id, unsigned char *data_inout,
  18. const unsigned char *target,
  19. uint32_t max_nonce, unsigned long *hashes_done)
  20. {
  21. unsigned char data[128] __attribute__((aligned(128)));
  22. unsigned char tmp_hash[32] __attribute__((aligned(128)));
  23. unsigned char tmp_hash1[32] __attribute__((aligned(128)));
  24. uint32_t *data32 = (uint32_t *) data;
  25. uint32_t *hash32 = (uint32_t *) tmp_hash;
  26. uint32_t *nonce = (uint32_t *)(data + 64 + 12);
  27. uint32_t n = 0;
  28. unsigned long stat_ctr = 0;
  29. int i;
  30. work_restart[thr_id].restart = 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. for (i = 0; i < 128/4; i++)
  37. data32[i] = swab32(((uint32_t *)data_inout)[i]);
  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. for (i = 0; i < 32/4; i++)
  45. ((uint32_t *)tmp_hash1)[i] =
  46. swab32(((uint32_t *)tmp_hash1)[i]);
  47. /* second SHA256 transform */
  48. memcpy(tmp_hash, sha256_init_state, 32);
  49. via_sha256(tmp_hash, tmp_hash1, 32);
  50. stat_ctr++;
  51. if (unlikely((hash32[7] == 0) && fulltest(tmp_hash, target))) {
  52. /* swap nonce'd data back into original storage area;
  53. * TODO: only swap back the nonce, rather than all data
  54. */
  55. for (i = 0; i < 128/4; i++) {
  56. uint32_t *dout32 = (uint32_t *) data_inout;
  57. dout32[i] = swab32(data32[i]);
  58. }
  59. *hashes_done = stat_ctr;
  60. return true;
  61. }
  62. if ((n >= max_nonce) || work_restart[thr_id].restart) {
  63. *hashes_done = stat_ctr;
  64. return false;
  65. }
  66. }
  67. }
  68. #endif /* WANT_VIA_PADLOCK */