sha256_via.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2010-2011 Jeff Garzik
  3. * Copyright 2012-2013 Luke Dashjr
  4. * Copyright 2011-2012 Con Kolivas
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "config.h"
  12. #include "driver-cpu.h"
  13. #include <stdbool.h>
  14. #include <stdint.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <sys/time.h>
  19. #include "miner.h"
  20. #ifdef WANT_VIA_PADLOCK
  21. static void via_sha256(void *hash, void *buf, unsigned len)
  22. {
  23. unsigned stat = 0;
  24. asm volatile(".byte 0xf3, 0x0f, 0xa6, 0xd0"
  25. :"+S"(buf), "+a"(stat)
  26. :"c"(len), "D" (hash)
  27. :"memory");
  28. }
  29. bool scanhash_via(struct thr_info*thr, const unsigned char __maybe_unused *pmidstate,
  30. unsigned char *data_inout,
  31. unsigned char __maybe_unused *phash1, unsigned char __maybe_unused *phash,
  32. const unsigned char *target,
  33. uint32_t max_nonce, uint32_t *last_nonce,
  34. uint32_t n)
  35. {
  36. unsigned char data[128] __attribute__((aligned(128)));
  37. unsigned char tmp_hash[32] __attribute__((aligned(128)));
  38. unsigned char tmp_hash1[32] __attribute__((aligned(128)));
  39. uint32_t *data32 = (uint32_t *) data;
  40. uint32_t *hash32 = (uint32_t *) tmp_hash;
  41. uint32_t *nonce = (uint32_t *)(data + 64 + 12);
  42. uint32_t *nonce_inout = (uint32_t *)(data_inout + 64 + 12);
  43. unsigned long stat_ctr = 0;
  44. /* bitcoin gives us big endian input, but via wants LE,
  45. * so we reverse the swapping bitcoin has already done (extra work)
  46. * in order to permit the hardware to swap everything
  47. * back to BE again (extra work).
  48. */
  49. swap32yes(data32, data_inout, 128/4);
  50. while (1) {
  51. *nonce = n;
  52. /* first SHA256 transform */
  53. memcpy(tmp_hash1, sha256_init_state, 32);
  54. via_sha256(tmp_hash1, data, 80); /* or maybe 128? */
  55. swap32yes(tmp_hash1, tmp_hash1, 32/4);
  56. /* second SHA256 transform */
  57. memcpy(tmp_hash, sha256_init_state, 32);
  58. via_sha256(tmp_hash, tmp_hash1, 32);
  59. stat_ctr++;
  60. if (unlikely((hash32[7] == 0) && fulltest(tmp_hash, target))) {
  61. /* swap nonce'd data back into original storage area;
  62. */
  63. *nonce_inout = bswap_32(n);
  64. *last_nonce = n;
  65. return true;
  66. }
  67. if ((n >= max_nonce) || thr->work_restart) {
  68. *last_nonce = n;
  69. return false;
  70. }
  71. n++;
  72. }
  73. }
  74. #endif /* WANT_VIA_PADLOCK */