sha256_xmm.asm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. ;; SHA-256 for X86 for Linux, based off of:A
  2. ; (c) Ufasoft 2011 http://ufasoft.com mailto:support@ufasoft.com
  3. ; Version 2011
  4. ; This software is Public Domain
  5. ; SHA-256 CPU SSE cruncher for Bitcoin Miner
  6. ALIGN 32
  7. BITS 32
  8. %define hash ecx
  9. %define data edx
  10. %define init esi
  11. ; 0 = (1024 - 256) (mod (LAB_CALC_UNROLL*LAB_CALC_PARA*16))
  12. %define LAB_CALC_PARA 2
  13. %define LAB_CALC_UNROLL 24
  14. %define LAB_LOOP_UNROLL 64
  15. extern _sha256_consts_m128i
  16. global $@CalcSha256_x86@12
  17. ; CalcSha256 hash(ecx), data(edx), init([esp+4])
  18. @CalcSha256_x86@12:
  19. push esi
  20. push edi
  21. mov init, [esp+12]
  22. LAB_SHA:
  23. lea edi, qword [data+256] ; + 256
  24. LAB_CALC:
  25. %macro lab_calc_blk 1
  26. movdqa xmm0, [edi-(15-%1)*16] ; xmm0 = W[I-15]
  27. movdqa xmm4, [edi-(15-(%1+1))*16] ; xmm4 = W[I-15+1]
  28. movdqa xmm2, xmm0 ; xmm2 = W[I-15]
  29. movdqa xmm6, xmm4 ; xmm6 = W[I-15+1]
  30. psrld xmm0, 3 ; xmm0 = W[I-15] >> 3
  31. psrld xmm4, 3 ; xmm4 = W[I-15+1] >> 3
  32. movdqa xmm1, xmm0 ; xmm1 = W[I-15] >> 3
  33. movdqa xmm5, xmm4 ; xmm5 = W[I-15+1] >> 3
  34. pslld xmm2, 14 ; xmm2 = W[I-15] << 14
  35. pslld xmm6, 14 ; xmm6 = W[I-15+1] << 14
  36. psrld xmm1, 4 ; xmm1 = W[I-15] >> 7
  37. psrld xmm5, 4 ; xmm5 = W[I-15+1] >> 7
  38. pxor xmm0, xmm1 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7)
  39. pxor xmm4, xmm5 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7)
  40. psrld xmm1, 11 ; xmm1 = W[I-15] >> 18
  41. psrld xmm5, 11 ; xmm5 = W[I-15+1] >> 18
  42. pxor xmm0, xmm2 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14)
  43. pxor xmm4, xmm6 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14)
  44. pslld xmm2, 11 ; xmm2 = W[I-15] << 25
  45. pslld xmm6, 11 ; xmm6 = W[I-15+1] << 25
  46. pxor xmm0, xmm1 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14) ^ (W[I-15] >> 18)
  47. pxor xmm4, xmm5 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14) ^ (W[I-15+1] >> 18)
  48. pxor xmm0, xmm2 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14) ^ (W[I-15] >> 18) ^ (W[I-15] << 25)
  49. pxor xmm4, xmm6 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14) ^ (W[I-15+1] >> 18) ^ (W[I-15+1] << 25)
  50. movdqa xmm3, [edi-(2-%1)*16] ; xmm3 = W[I-2]
  51. movdqa xmm7, [edi-(2-(%1+1))*16] ; xmm7 = W[I-2+1]
  52. paddd xmm0, [edi-(16-%1)*16] ; xmm0 = s0(W[I-15]) + W[I-16]
  53. paddd xmm4, [edi-(16-(%1+1))*16] ; xmm4 = s0(W[I-15+1]) + W[I-16+1]
  54. ;;;;;;;;;;;;;;;;;;
  55. movdqa xmm2, xmm3 ; xmm2 = W[I-2]
  56. movdqa xmm6, xmm7 ; xmm6 = W[I-2+1]
  57. psrld xmm3, 10 ; xmm3 = W[I-2] >> 10
  58. psrld xmm7, 10 ; xmm7 = W[I-2+1] >> 10
  59. movdqa xmm1, xmm3 ; xmm1 = W[I-2] >> 10
  60. movdqa xmm5, xmm7 ; xmm5 = W[I-2+1] >> 10
  61. paddd xmm0, [edi-(7-%1)*16] ; xmm0 = s0(W[I-15]) + W[I-16] + W[I-7]
  62. pslld xmm2, 13 ; xmm2 = W[I-2] << 13
  63. pslld xmm6, 13 ; xmm6 = W[I-2+1] << 13
  64. psrld xmm1, 7 ; xmm1 = W[I-2] >> 17
  65. psrld xmm5, 7 ; xmm5 = W[I-2+1] >> 17
  66. paddd xmm4, [edi-(7-(%1+1))*16] ; xmm4 = s0(W[I-15+1]) + W[I-16+1] + W[I-7+1]
  67. pxor xmm3, xmm1 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17)
  68. pxor xmm7, xmm5 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17)
  69. psrld xmm1, 2 ; xmm1 = W[I-2] >> 19
  70. psrld xmm5, 2 ; xmm5 = W[I-2+1] >> 19
  71. pxor xmm3, xmm2 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13)
  72. pxor xmm7, xmm6 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13)
  73. pslld xmm2, 2 ; xmm2 = W[I-2] << 15
  74. pslld xmm6, 2 ; xmm6 = W[I-2+1] << 15
  75. pxor xmm3, xmm1 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13) ^ (W[I-2] >> 19)
  76. pxor xmm7, xmm5 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13) ^ (W[I-2+1] >> 19)
  77. pxor xmm3, xmm2 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13) ^ (W[I-2] >> 19) ^ (W[I-2] << 15)
  78. pxor xmm7, xmm6 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13) ^ (W[I-2+1] >> 19) ^ (W[I-2+1] << 15)
  79. paddd xmm0, xmm3 ; xmm0 = s0(W[I-15]) + W[I-16] + s1(W[I-2]) + W[I-7]
  80. paddd xmm4, xmm7 ; xmm4 = s0(W[I-15+1]) + W[I-16+1] + s1(W[I-2+1]) + W[I-7+1]
  81. movdqa [edi+(%1*16)], xmm0
  82. movdqa [edi+((%1+1)*16)], xmm4
  83. %endmacro
  84. %assign i 0
  85. %rep LAB_CALC_UNROLL
  86. lab_calc_blk i
  87. %assign i i+LAB_CALC_PARA
  88. %endrep
  89. ; Load the init values of the message into the hash.
  90. movdqa xmm7, [init]
  91. pshufd xmm5, xmm7, 0x55 ; xmm5 == b
  92. pshufd xmm4, xmm7, 0xAA ; xmm4 == c
  93. pshufd xmm3, xmm7, 0xFF ; xmm3 == d
  94. pshufd xmm7, xmm7, 0 ; xmm7 == a
  95. movdqa xmm0, [init+4*4]
  96. pshufd xmm1, xmm0, 0x55 ; [hash+0*16] == f
  97. movdqa [hash+0*16], xmm1
  98. pshufd xmm1, xmm0, 0xAA ; [hash+1*16] == g
  99. movdqa [hash+1*16], xmm1
  100. pshufd xmm1, xmm0, 0xFF ; [hash+2*16] == h
  101. movdqa [hash+2*16], xmm1
  102. pshufd xmm0, xmm0, 0 ; xmm0 == e
  103. LAB_LOOP:
  104. ;; T t1 = h + (Rotr32(e, 6) ^ Rotr32(e, 11) ^ Rotr32(e, 25)) + ((e & f) ^ AndNot(e, g)) + Expand32<T>(g_sha256_k[j]) + w[j]
  105. %macro lab_loop_blk 1
  106. movdqa xmm6, [data+%1]
  107. paddd xmm6, _sha256_consts_m128i[%1]
  108. paddd xmm6, [hash+2*16] ; +h
  109. movdqa xmm1, xmm0
  110. movdqa xmm2, [hash+1*16]
  111. pandn xmm1, xmm2 ; ~e & g
  112. movdqa [hash+2*16], xmm2 ; h = g
  113. movdqa xmm2, [hash+0*16] ; f
  114. movdqa [hash+1*16], xmm2 ; g = f
  115. pand xmm2, xmm0 ; e & f
  116. pxor xmm1, xmm2 ; (e & f) ^ (~e & g)
  117. movdqa [hash+0*16], xmm0 ; f = e
  118. paddd xmm6, xmm1 ; Ch + h + w[i] + k[i]
  119. movdqa xmm1, xmm0
  120. psrld xmm0, 6
  121. movdqa xmm2, xmm0
  122. pslld xmm1, 7
  123. psrld xmm2, 5
  124. pxor xmm0, xmm1
  125. pxor xmm0, xmm2
  126. pslld xmm1, 14
  127. psrld xmm2, 14
  128. pxor xmm0, xmm1
  129. pxor xmm0, xmm2
  130. pslld xmm1, 5
  131. pxor xmm0, xmm1 ; Rotr32(e, 6) ^ Rotr32(e, 11) ^ Rotr32(e, 25)
  132. paddd xmm6, xmm0 ; xmm6 = t1
  133. movdqa xmm0, xmm3 ; d
  134. paddd xmm0, xmm6 ; e = d+t1
  135. movdqa xmm1, xmm5 ; =b
  136. movdqa xmm3, xmm4 ; d = c
  137. movdqa xmm2, xmm4 ; c
  138. pand xmm2, xmm5 ; b & c
  139. pand xmm4, xmm7 ; a & c
  140. pand xmm1, xmm7 ; a & b
  141. pxor xmm1, xmm4
  142. movdqa xmm4, xmm5 ; c = b
  143. movdqa xmm5, xmm7 ; b = a
  144. pxor xmm1, xmm2 ; (a & c) ^ (a & d) ^ (c & d)
  145. paddd xmm6, xmm1 ; t1 + ((a & c) ^ (a & d) ^ (c & d))
  146. movdqa xmm2, xmm7
  147. psrld xmm7, 2
  148. movdqa xmm1, xmm7
  149. pslld xmm2, 10
  150. psrld xmm1, 11
  151. pxor xmm7, xmm2
  152. pxor xmm7, xmm1
  153. pslld xmm2, 9
  154. psrld xmm1, 9
  155. pxor xmm7, xmm2
  156. pxor xmm7, xmm1
  157. pslld xmm2, 11
  158. pxor xmm7, xmm2
  159. paddd xmm7, xmm6 ; a = t1 + (Rotr32(a, 2) ^ Rotr32(a, 13) ^ Rotr32(a, 22)) + ((a & c) ^ (a & d) ^ (c & d));
  160. %endmacro
  161. %assign i 0
  162. %rep LAB_LOOP_UNROLL
  163. lab_loop_blk i
  164. %assign i i+16
  165. %endrep
  166. ; Finished the 64 rounds, calculate hash and save
  167. movdqa xmm1, [init+16]
  168. pshufd xmm2, xmm1, 0xFF
  169. movdqa xmm6, [hash+2*16]
  170. paddd xmm2, xmm6
  171. movdqa [hash+7*16], xmm2
  172. pshufd xmm2, xmm1, 0xAA
  173. movdqa xmm6, [hash+1*16]
  174. paddd xmm2, xmm6
  175. movdqa [hash+6*16], xmm2
  176. pshufd xmm2, xmm1, 0x55
  177. movdqa xmm6, [hash+0*16]
  178. paddd xmm2, xmm6
  179. movdqa [hash+5*16], xmm2
  180. pshufd xmm1, xmm1, 0
  181. paddd xmm0, xmm1
  182. movdqa [hash+4*16], xmm0
  183. movdqa xmm1, [init]
  184. pshufd xmm2, xmm1, 0xFF
  185. paddd xmm3, xmm2
  186. movdqa [hash+3*16], xmm3
  187. pshufd xmm2, xmm1, 0xAA
  188. paddd xmm4, xmm2
  189. movdqa [hash+2*16], xmm4
  190. pshufd xmm2, xmm1, 0x55
  191. paddd xmm5, xmm2
  192. movdqa [hash+1*16], xmm5
  193. pshufd xmm1, xmm1, 0
  194. paddd xmm7, xmm1
  195. movdqa [hash+0*16], xmm7
  196. LAB_RET:
  197. pop edi
  198. pop esi
  199. retn 4
  200. %ifidn __OUTPUT_FORMAT__,elf
  201. section .note.GNU-stack noalloc noexec nowrite progbits
  202. %endif
  203. %ifidn __OUTPUT_FORMAT__,elf32
  204. section .note.GNU-stack noalloc noexec nowrite progbits
  205. %endif