1
2
3
4
5 package main
6
7 import (
8 . "github.com/mmcloughlin/avo/build"
9 . "github.com/mmcloughlin/avo/operand"
10 . "github.com/mmcloughlin/avo/reg"
11 )
12
13
14
15
16
17
18
19
20
21 func blockSHANI() {
22 Implement("blockSHANI")
23 Load(Param("dig"), digestPtr)
24 Load(Param("p").Base(), dataPtr)
25 Load(Param("p").Len(), numBytes)
26 SHRQ(Imm(6), numBytes)
27 SHLQ(Imm(6), numBytes)
28 CMPQ(numBytes, Imm(0))
29 JEQ(LabelRef("done"))
30 ADDQ(dataPtr, numBytes)
31 VMOVDQU(Mem{Base: digestPtr}.Offset(0*16), state0)
32 VMOVDQU(Mem{Base: digestPtr}.Offset(1*16), state1)
33 PSHUFD(Imm(0xb1), state0, state0)
34 PSHUFD(Imm(0x1b), state1, state1)
35 VMOVDQA(state0, m4)
36 PALIGNR(Imm(8), state1, state0)
37 PBLENDW(Imm(0xf0), m4, state1)
38 flip_mask := flip_mask_DATA()
39 VMOVDQA(flip_mask, shufMask)
40 LEAQ(K256_DATA(), sha256Constants)
41
42 roundLoop()
43 done()
44 }
45
46 func roundLoop() {
47 Label("roundLoop")
48 Comment("save hash values for addition after rounds")
49 VMOVDQA(state0, abefSave)
50 VMOVDQA(state1, cdghSave)
51
52 Comment("do rounds 0-59")
53 rounds0to11(m0, nil, 0, nop)
54 rounds0to11(m1, m0, 1, sha256msg1)
55 rounds0to11(m2, m1, 2, sha256msg1)
56 VMOVDQU(Mem{Base: dataPtr}.Offset(3*16), msg)
57 PSHUFB(shufMask, msg)
58 rounds12to59(m3, 3, m2, m0, sha256msg1, vmovrev)
59 rounds12to59(m0, 4, m3, m1, sha256msg1, vmov)
60 rounds12to59(m1, 5, m0, m2, sha256msg1, vmov)
61 rounds12to59(m2, 6, m1, m3, sha256msg1, vmov)
62 rounds12to59(m3, 7, m2, m0, sha256msg1, vmov)
63 rounds12to59(m0, 8, m3, m1, sha256msg1, vmov)
64 rounds12to59(m1, 9, m0, m2, sha256msg1, vmov)
65 rounds12to59(m2, 10, m1, m3, sha256msg1, vmov)
66 rounds12to59(m3, 11, m2, m0, sha256msg1, vmov)
67 rounds12to59(m0, 12, m3, m1, sha256msg1, vmov)
68 rounds12to59(m1, 13, m0, m2, nop, vmov)
69 rounds12to59(m2, 14, m1, m3, nop, vmov)
70
71 Comment("do rounds 60-63")
72 VMOVDQA(m3, msg)
73 PADDD(Mem{Base: sha256Constants}.Offset(15*32), msg)
74 SHA256RNDS2(msg, state0, state1)
75 PSHUFD(Imm(0x0e), msg, msg)
76 SHA256RNDS2(msg, state1, state0)
77
78 Comment("add current hash values with previously saved")
79 PADDD(abefSave, state0)
80 PADDD(cdghSave, state1)
81
82 Comment("advance data pointer; loop until buffer empty")
83 ADDQ(Imm(64), dataPtr)
84 CMPQ(numBytes, dataPtr)
85 JNE(LabelRef("roundLoop"))
86
87 Comment("write hash values back in the correct order")
88 PSHUFD(Imm(0x1b), state0, state0)
89 PSHUFD(Imm(0xb1), state1, state1)
90 VMOVDQA(state0, m4)
91 PBLENDW(Imm(0xf0), state1, state0)
92 PALIGNR(Imm(8), m4, state1)
93 VMOVDQU(state0, Mem{Base: digestPtr}.Offset(0*16))
94 VMOVDQU(state1, Mem{Base: digestPtr}.Offset(1*16))
95 }
96
97 func done() {
98 Label("done")
99 RET()
100 }
101
102 var (
103 digestPtr GPPhysical = RDI
104 dataPtr = RSI
105 numBytes = RDX
106 sha256Constants = RAX
107 msg VecPhysical = X0
108 state0 = X1
109 state1 = X2
110 m0 = X3
111 m1 = X4
112 m2 = X5
113 m3 = X6
114 m4 = X7
115 shufMask = X8
116 abefSave = X9
117 cdghSave = X10
118 )
119
120
121 func nop(m, a VecPhysical) {
122 }
123
124
125 func sha256msg1(m, a VecPhysical) {
126 SHA256MSG1(m, a)
127 }
128
129
130 func vmov(a, b VecPhysical) {
131 VMOVDQA(a, b)
132 }
133
134
135 func vmovrev(a, b VecPhysical) {
136 VMOVDQA(b, a)
137 }
138
139 type VecFunc func(a, b VecPhysical)
140
141
142
143
144
145
146 func rounds0to11(m, a VecPhysical, c int, sha256msg1 VecFunc) {
147 VMOVDQU(Mem{Base: dataPtr}.Offset(c*16), msg)
148 PSHUFB(shufMask, msg)
149 VMOVDQA(msg, m)
150 PADDD(Mem{Base: sha256Constants}.Offset(c*32), msg)
151 SHA256RNDS2(msg, state0, state1)
152 PSHUFD(U8(0x0e), msg, msg)
153 SHA256RNDS2(msg, state1, state0)
154 sha256msg1(m, a)
155 }
156
157
158
159
160
161
162
163 func rounds12to59(m VecPhysical, c int, a, t VecPhysical, sha256msg1, movop VecFunc) {
164 movop(m, msg)
165 PADDD(Mem{Base: sha256Constants}.Offset(c*32), msg)
166 SHA256RNDS2(msg, state0, state1)
167 VMOVDQA(m, m4)
168 PALIGNR(Imm(4), a, m4)
169 PADDD(m4, t)
170 SHA256MSG2(m, t)
171 PSHUFD(Imm(0x0e), msg, msg)
172 SHA256RNDS2(msg, state1, state0)
173 sha256msg1(m, a)
174 }
175
View as plain text