sha256_xmm.asm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ;; SHA-256 for X86 for Linux, based off of:
  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 8
  14. %define LAB_LOOP_UNROLL 8
  15. extern sha256_consts_m128i
  16. global CalcSha256_x86
  17. ; CalcSha256 hash(ecx), data(edx), init([esp+4])
  18. CalcSha256_x86:
  19. push esi
  20. push edi
  21. mov init, [esp+12]
  22. push ebx
  23. LAB_NEXT_NONCE:
  24. mov eax, 64*4 ; 256 - rcx is # of SHA-2 rounds
  25. mov ebx, 16*4 ; 64 - rax is where we expand to
  26. LAB_SHA:
  27. push eax
  28. lea eax, qword [data+eax*4] ; + 1024
  29. lea edi, qword [data+ebx*4] ; + 256
  30. LAB_CALC:
  31. %macro lab_calc_blk 1
  32. movdqa xmm0, [edi-(15-%1)*16] ; xmm0 = W[I-15]
  33. movdqa xmm4, [edi-(15-(%1+1))*16] ; xmm4 = W[I-15+1]
  34. movdqa xmm2, xmm0 ; xmm2 = W[I-15]
  35. movdqa xmm6, xmm4 ; xmm6 = W[I-15+1]
  36. psrld xmm0, 3 ; xmm0 = W[I-15] >> 3
  37. psrld xmm4, 3 ; xmm4 = W[I-15+1] >> 3
  38. movdqa xmm1, xmm0 ; xmm1 = W[I-15] >> 3
  39. movdqa xmm5, xmm4 ; xmm5 = W[I-15+1] >> 3
  40. pslld xmm2, 14 ; xmm2 = W[I-15] << 14
  41. pslld xmm6, 14 ; xmm6 = W[I-15+1] << 14
  42. psrld xmm1, 4 ; xmm1 = W[I-15] >> 7
  43. psrld xmm5, 4 ; xmm5 = W[I-15+1] >> 7
  44. pxor xmm0, xmm1 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7)
  45. pxor xmm4, xmm5 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7)
  46. psrld xmm1, 11 ; xmm1 = W[I-15] >> 18
  47. psrld xmm5, 11 ; xmm5 = W[I-15+1] >> 18
  48. pxor xmm0, xmm2 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14)
  49. pxor xmm4, xmm6 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14)
  50. pslld xmm2, 11 ; xmm2 = W[I-15] << 25
  51. pslld xmm6, 11 ; xmm6 = W[I-15+1] << 25
  52. pxor xmm0, xmm1 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14) ^ (W[I-15] >> 18)
  53. pxor xmm4, xmm5 ; xmm4 = (W[I-15+1] >> 3) ^ (W[I-15+1] >> 7) ^ (W[I-15+1] << 14) ^ (W[I-15+1] >> 18)
  54. pxor xmm0, xmm2 ; xmm0 = (W[I-15] >> 3) ^ (W[I-15] >> 7) ^ (W[I-15] << 14) ^ (W[I-15] >> 18) ^ (W[I-15] << 25)
  55. 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)
  56. movdqa xmm3, [edi-(2-%1)*16] ; xmm3 = W[I-2]
  57. movdqa xmm7, [edi-(2-(%1+1))*16] ; xmm7 = W[I-2+1]
  58. paddd xmm0, [edi-(16-%1)*16] ; xmm0 = s0(W[I-15]) + W[I-16]
  59. paddd xmm4, [edi-(16-(%1+1))*16] ; xmm4 = s0(W[I-15+1]) + W[I-16+1]
  60. ;;;;;;;;;;;;;;;;;;
  61. movdqa xmm2, xmm3 ; xmm2 = W[I-2]
  62. movdqa xmm6, xmm7 ; xmm6 = W[I-2+1]
  63. psrld xmm3, 10 ; xmm3 = W[I-2] >> 10
  64. psrld xmm7, 10 ; xmm7 = W[I-2+1] >> 10
  65. movdqa xmm1, xmm3 ; xmm1 = W[I-2] >> 10
  66. movdqa xmm5, xmm7 ; xmm5 = W[I-2+1] >> 10
  67. paddd xmm0, [edi-(7-%1)*16] ; xmm0 = s0(W[I-15]) + W[I-16] + W[I-7]
  68. pslld xmm2, 13 ; xmm2 = W[I-2] << 13
  69. pslld xmm6, 13 ; xmm6 = W[I-2+1] << 13
  70. psrld xmm1, 7 ; xmm1 = W[I-2] >> 17
  71. psrld xmm5, 7 ; xmm5 = W[I-2+1] >> 17
  72. paddd xmm4, [edi-(7-(%1+1))*16] ; xmm4 = s0(W[I-15+1]) + W[I-16+1] + W[I-7+1]
  73. pxor xmm3, xmm1 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17)
  74. pxor xmm7, xmm5 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17)
  75. psrld xmm1, 2 ; xmm1 = W[I-2] >> 19
  76. psrld xmm5, 2 ; xmm5 = W[I-2+1] >> 19
  77. pxor xmm3, xmm2 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13)
  78. pxor xmm7, xmm6 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13)
  79. pslld xmm2, 2 ; xmm2 = W[I-2] << 15
  80. pslld xmm6, 2 ; xmm6 = W[I-2+1] << 15
  81. pxor xmm3, xmm1 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13) ^ (W[I-2] >> 19)
  82. pxor xmm7, xmm5 ; xmm7 = (W[I-2+1] >> 10) ^ (W[I-2+1] >> 17) ^ (W[I-2+1] << 13) ^ (W[I-2+1] >> 19)
  83. pxor xmm3, xmm2 ; xmm3 = (W[I-2] >> 10) ^ (W[I-2] >> 17) ^ (W[I-2] << 13) ^ (W[I-2] >> 19) ^ (W[I-2] << 15)
  84. 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)
  85. paddd xmm0, xmm3 ; xmm0 = s0(W[I-15]) + W[I-16] + s1(W[I-2]) + W[I-7]
  86. paddd xmm4, xmm7 ; xmm4 = s0(W[I-15+1]) + W[I-16+1] + s1(W[I-2+1]) + W[I-7+1]
  87. movdqa [edi+(%1*16)], xmm0
  88. movdqa [edi+((%1+1)*16)], xmm4
  89. %endmacro
  90. %assign i 0
  91. %rep LAB_CALC_UNROLL
  92. lab_calc_blk i
  93. %assign i i+LAB_CALC_PARA
  94. %endrep
  95. add edi, LAB_CALC_UNROLL*LAB_CALC_PARA*16
  96. cmp edi, eax
  97. jb LAB_CALC
  98. pop eax
  99. mov ebx, 0
  100. ; Load the init values of the message into the hash.
  101. movdqa xmm7, [init]
  102. pshufd xmm5, xmm7, 0x55 ; xmm5 == b
  103. pshufd xmm4, xmm7, 0xAA ; xmm4 == c
  104. pshufd xmm3, xmm7, 0xFF ; xmm3 == d
  105. pshufd xmm7, xmm7, 0 ; xmm7 == a
  106. movdqa xmm0, [init+4*4]
  107. pshufd xmm1, xmm0, 0x55 ; [hash+0*16] == f
  108. movdqa [hash+0*16], xmm1
  109. pshufd xmm1, xmm0, 0xAA ; [hash+1*16] == g
  110. movdqa [hash+1*16], xmm1
  111. pshufd xmm1, xmm0, 0xFF ; [hash+2*16] == h
  112. movdqa [hash+2*16], xmm1
  113. pshufd xmm0, xmm0, 0 ; xmm0 == e
  114. LAB_LOOP:
  115. ;; 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]
  116. %macro lab_loop_blk 0
  117. movdqa xmm6, [data+ebx*4]
  118. paddd xmm6, sha256_consts_m128i[ebx*4]
  119. add ebx, 4
  120. paddd xmm6, [hash+2*16] ; +h
  121. movdqa xmm1, xmm0
  122. movdqa xmm2, [hash+1*16]
  123. pandn xmm1, xmm2 ; ~e & g
  124. movdqa [hash+2*16], xmm2 ; h = g
  125. movdqa xmm2, [hash+0*16] ; f
  126. movdqa [hash+1*16], xmm2 ; g = f
  127. pand xmm2, xmm0 ; e & f
  128. pxor xmm1, xmm2 ; (e & f) ^ (~e & g)
  129. movdqa [hash+0*16], xmm0 ; f = e
  130. paddd xmm6, xmm1 ; Ch + h + w[i] + k[i]
  131. movdqa xmm1, xmm0
  132. psrld xmm0, 6
  133. movdqa xmm2, xmm0
  134. pslld xmm1, 7
  135. psrld xmm2, 5
  136. pxor xmm0, xmm1
  137. pxor xmm0, xmm2
  138. pslld xmm1, 14
  139. psrld xmm2, 14
  140. pxor xmm0, xmm1
  141. pxor xmm0, xmm2
  142. pslld xmm1, 5
  143. pxor xmm0, xmm1 ; Rotr32(e, 6) ^ Rotr32(e, 11) ^ Rotr32(e, 25)
  144. paddd xmm6, xmm0 ; xmm6 = t1
  145. movdqa xmm0, xmm3 ; d
  146. paddd xmm0, xmm6 ; e = d+t1
  147. movdqa xmm1, xmm5 ; =b
  148. movdqa xmm3, xmm4 ; d = c
  149. movdqa xmm2, xmm4 ; c
  150. pand xmm2, xmm5 ; b & c
  151. pand xmm4, xmm7 ; a & c
  152. pand xmm1, xmm7 ; a & b
  153. pxor xmm1, xmm4
  154. movdqa xmm4, xmm5 ; c = b
  155. movdqa xmm5, xmm7 ; b = a
  156. pxor xmm1, xmm2 ; (a & c) ^ (a & d) ^ (c & d)
  157. paddd xmm6, xmm1 ; t1 + ((a & c) ^ (a & d) ^ (c & d))
  158. movdqa xmm2, xmm7
  159. psrld xmm7, 2
  160. movdqa xmm1, xmm7
  161. pslld xmm2, 10
  162. psrld xmm1, 11
  163. pxor xmm7, xmm2
  164. pxor xmm7, xmm1
  165. pslld xmm2, 9
  166. psrld xmm1, 9
  167. pxor xmm7, xmm2
  168. pxor xmm7, xmm1
  169. pslld xmm2, 11
  170. pxor xmm7, xmm2
  171. paddd xmm7, xmm6 ; a = t1 + (Rotr32(a, 2) ^ Rotr32(a, 13) ^ Rotr32(a, 22)) + ((a & c) ^ (a & d) ^ (c & d));
  172. %endmacro
  173. %assign i 0
  174. %rep LAB_LOOP_UNROLL
  175. lab_loop_blk
  176. %assign i i+1
  177. %endrep
  178. cmp ebx, eax
  179. jb LAB_LOOP
  180. ; Finished the 64 rounds, calculate hash and save
  181. movdqa xmm1, [init]
  182. pshufd xmm2, xmm1, 0x55
  183. pshufd xmm6, xmm1, 0xAA
  184. movdqa [hash+3*16], xmm6
  185. pshufd xmm6, xmm1, 0xFF
  186. movdqa [hash+4*16], xmm6
  187. pshufd xmm1, xmm1, 0
  188. paddd xmm5, xmm2
  189. paddd xmm4, [hash+3*16]
  190. paddd xmm3, [hash+4*16]
  191. paddd xmm7, xmm1
  192. movdqa xmm1, [init+4*4]
  193. pshufd xmm2, xmm1, 0x55
  194. pshufd xmm6, xmm1, 0xAA
  195. movdqa [hash+3*16], xmm6
  196. pshufd xmm6, xmm1, 0xFF
  197. movdqa [hash+4*16], xmm6
  198. pshufd xmm1, xmm1, 0
  199. movdqa xmm6, [hash+0*16]
  200. paddd xmm2, xmm6
  201. movdqa [hash+0*16], xmm2
  202. movdqa xmm2, [hash+3*16]
  203. movdqa xmm6, [hash+1*16]
  204. paddd xmm2, xmm6
  205. movdqa [hash+1*16], xmm2
  206. movdqa xmm2, [hash+4*16]
  207. movdqa xmm6, [hash+2*16]
  208. paddd xmm2, xmm6
  209. movdqa [hash+2*16], xmm2
  210. paddd xmm0, xmm1
  211. movdqa xmm1, [hash+0*16]
  212. movdqa xmm2, [hash+1*16]
  213. movdqa xmm6, [hash+2*16]
  214. movdqa [hash+0*16], xmm7
  215. movdqa [hash+1*16], xmm5
  216. movdqa [hash+2*16], xmm4
  217. movdqa [hash+3*16], xmm3
  218. movdqa [hash+4*16], xmm0
  219. movdqa [hash+5*16], xmm1
  220. movdqa [hash+6*16], xmm2
  221. movdqa [hash+7*16], xmm6
  222. LAB_RET:
  223. pop ebx
  224. pop edi
  225. pop esi
  226. retn 4