Repository navigation
Expand file tree
/
Copy pathchacha20_fused.ea
More file actions
376 lines (355 loc) · 13.6 KB
/
Copy pathchacha20_fused.ea
File metadata and controls
376 lines (355 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// chacha20_fused.ea — Fused ChaCha20 encrypt + plaintext statistics kernel
// 4-block interleaved ILP (identical round loop to chacha20.ea) plus
// per-plaintext-byte sum/count/min/max in a single pass.
// Rotation helpers — identical to chacha20.ea
func rotl16(v: i32x4) -> i32x4 {
return (v .<< splat(16)) .| ((v .>> splat(16)) .& splat(65535))
}
func rotl12(v: i32x4) -> i32x4 {
return (v .<< splat(12)) .| ((v .>> splat(20)) .& splat(4095))
}
func rotl8(v: i32x4) -> i32x4 {
return (v .<< splat(8)) .| ((v .>> splat(24)) .& splat(255))
}
func rotl7(v: i32x4) -> i32x4 {
return (v .<< splat(7)) .| ((v .>> splat(25)) .& splat(127))
}
// Internal single-block function for tail handling
func chacha20_block(key: *i32, nonce: *i32, counter: i32, out: *mut i32) {
let row0_init: i32x4 = [1634760805, 857760878, 2036477234, 1797285236]i32x4
let row1_init: i32x4 = load(key, 0)
let row2_init: i32x4 = load(key, 4)
let n0: i32 = nonce[0]
let n1: i32 = nonce[1]
let n2: i32 = nonce[2]
let row3_init: i32x4 = [counter, n0, n1, n2]i32x4
let mut r0: i32x4 = row0_init
let mut r1: i32x4 = row1_init
let mut r2: i32x4 = row2_init
let mut r3: i32x4 = row3_init
let mut round: i32 = 0
while round < 10 {
r0 = r0 .+ r1
r3 = rotl16(r3 .^ r0)
r2 = r2 .+ r3
r1 = rotl12(r1 .^ r2)
r0 = r0 .+ r1
r3 = rotl8(r3 .^ r0)
r2 = r2 .+ r3
r1 = rotl7(r1 .^ r2)
r1 = shuffle(r1, [1, 2, 3, 0])
r2 = shuffle(r2, [2, 3, 0, 1])
r3 = shuffle(r3, [3, 0, 1, 2])
r0 = r0 .+ r1
r3 = rotl16(r3 .^ r0)
r2 = r2 .+ r3
r1 = rotl12(r1 .^ r2)
r0 = r0 .+ r1
r3 = rotl8(r3 .^ r0)
r2 = r2 .+ r3
r1 = rotl7(r1 .^ r2)
r1 = shuffle(r1, [3, 0, 1, 2])
r2 = shuffle(r2, [2, 3, 0, 1])
r3 = shuffle(r3, [1, 2, 3, 0])
round = round + 1
}
store(out, 0, r0 .+ row0_init)
store(out, 4, r1 .+ row1_init)
store(out, 8, r2 .+ row2_init)
store(out, 12, r3 .+ row3_init)
}
// Sum all 16 bytes of a u8x16 vector, returning i32
func sum_u8x16(chunk: u8x16) -> i32 {
let w0: i32x4 = widen_u8_i32x4(chunk)
let s1: u8x16 = shuffle(chunk, [4,5,6,7, 0,1,2,3, 8,9,10,11, 12,13,14,15])
let w1: i32x4 = widen_u8_i32x4(s1)
let s2: u8x16 = shuffle(chunk, [8,9,10,11, 0,1,2,3, 4,5,6,7, 12,13,14,15])
let w2: i32x4 = widen_u8_i32x4(s2)
let s3: u8x16 = shuffle(chunk, [12,13,14,15, 0,1,2,3, 4,5,6,7, 8,9,10,11])
let w3: i32x4 = widen_u8_i32x4(s3)
return reduce_add(w0) + reduce_add(w1) + reduce_add(w2) + reduce_add(w3)
}
// Fused ChaCha20 encrypt + statistics — 4-block ILP hot loop
export func chacha20_encrypt_stats(
key: *restrict i32, nonce: *restrict i32, counter: i32,
plaintext: *restrict u8, ciphertext: *restrict mut u8, len: i32,
ks_i32: *restrict mut i32, ks_u8: *restrict u8,
pt_i32: *restrict i32, ct_i32: *restrict mut i32,
out_sum: *mut i64, out_count: *mut i32,
out_min: *mut u8, out_max: *mut u8
) {
let row0_init: i32x4 = [1634760805, 857760878, 2036477234, 1797285236]i32x4
let row1_init: i32x4 = load(key, 0)
let row2_init: i32x4 = load(key, 4)
let n0: i32 = nonce[0]
let n1: i32 = nonce[1]
let n2: i32 = nonce[2]
let mut offset: i32 = 0
let mut elem_off: i32 = 0
let mut ctr: i32 = counter
let mut running_sum: i64 = to_i64(0)
let mut running_count: i32 = 0
let mut running_min: u8 = 255
let mut running_max: u8 = 0
// ---- 4-block ILP hot loop (256 bytes per iteration) ----
while offset + 256 <= len {
let r3a_init: i32x4 = [ctr, n0, n1, n2]i32x4
let r3b_init: i32x4 = [ctr + 1, n0, n1, n2]i32x4
let r3c_init: i32x4 = [ctr + 2, n0, n1, n2]i32x4
let r3d_init: i32x4 = [ctr + 3, n0, n1, n2]i32x4
let mut a0: i32x4 = row0_init
let mut a1: i32x4 = row1_init
let mut a2: i32x4 = row2_init
let mut a3: i32x4 = r3a_init
let mut b0: i32x4 = row0_init
let mut b1: i32x4 = row1_init
let mut b2: i32x4 = row2_init
let mut b3: i32x4 = r3b_init
let mut c0: i32x4 = row0_init
let mut c1: i32x4 = row1_init
let mut c2: i32x4 = row2_init
let mut c3: i32x4 = r3c_init
let mut d0: i32x4 = row0_init
let mut d1: i32x4 = row1_init
let mut d2: i32x4 = row2_init
let mut d3: i32x4 = r3d_init
let mut round: i32 = 0
while round < 10 {
a0 = a0 .+ a1
b0 = b0 .+ b1
c0 = c0 .+ c1
d0 = d0 .+ d1
a3 = rotl16(a3 .^ a0)
b3 = rotl16(b3 .^ b0)
c3 = rotl16(c3 .^ c0)
d3 = rotl16(d3 .^ d0)
a2 = a2 .+ a3
b2 = b2 .+ b3
c2 = c2 .+ c3
d2 = d2 .+ d3
a1 = rotl12(a1 .^ a2)
b1 = rotl12(b1 .^ b2)
c1 = rotl12(c1 .^ c2)
d1 = rotl12(d1 .^ d2)
a0 = a0 .+ a1
b0 = b0 .+ b1
c0 = c0 .+ c1
d0 = d0 .+ d1
a3 = rotl8(a3 .^ a0)
b3 = rotl8(b3 .^ b0)
c3 = rotl8(c3 .^ c0)
d3 = rotl8(d3 .^ d0)
a2 = a2 .+ a3
b2 = b2 .+ b3
c2 = c2 .+ c3
d2 = d2 .+ d3
a1 = rotl7(a1 .^ a2)
b1 = rotl7(b1 .^ b2)
c1 = rotl7(c1 .^ c2)
d1 = rotl7(d1 .^ d2)
a1 = shuffle(a1, [1, 2, 3, 0])
b1 = shuffle(b1, [1, 2, 3, 0])
c1 = shuffle(c1, [1, 2, 3, 0])
d1 = shuffle(d1, [1, 2, 3, 0])
a2 = shuffle(a2, [2, 3, 0, 1])
b2 = shuffle(b2, [2, 3, 0, 1])
c2 = shuffle(c2, [2, 3, 0, 1])
d2 = shuffle(d2, [2, 3, 0, 1])
a3 = shuffle(a3, [3, 0, 1, 2])
b3 = shuffle(b3, [3, 0, 1, 2])
c3 = shuffle(c3, [3, 0, 1, 2])
d3 = shuffle(d3, [3, 0, 1, 2])
a0 = a0 .+ a1
b0 = b0 .+ b1
c0 = c0 .+ c1
d0 = d0 .+ d1
a3 = rotl16(a3 .^ a0)
b3 = rotl16(b3 .^ b0)
c3 = rotl16(c3 .^ c0)
d3 = rotl16(d3 .^ d0)
a2 = a2 .+ a3
b2 = b2 .+ b3
c2 = c2 .+ c3
d2 = d2 .+ d3
a1 = rotl12(a1 .^ a2)
b1 = rotl12(b1 .^ b2)
c1 = rotl12(c1 .^ c2)
d1 = rotl12(d1 .^ d2)
a0 = a0 .+ a1
b0 = b0 .+ b1
c0 = c0 .+ c1
d0 = d0 .+ d1
a3 = rotl8(a3 .^ a0)
b3 = rotl8(b3 .^ b0)
c3 = rotl8(c3 .^ c0)
d3 = rotl8(d3 .^ d0)
a2 = a2 .+ a3
b2 = b2 .+ b3
c2 = c2 .+ c3
d2 = d2 .+ d3
a1 = rotl7(a1 .^ a2)
b1 = rotl7(b1 .^ b2)
c1 = rotl7(c1 .^ c2)
d1 = rotl7(d1 .^ d2)
a1 = shuffle(a1, [3, 0, 1, 2])
b1 = shuffle(b1, [3, 0, 1, 2])
c1 = shuffle(c1, [3, 0, 1, 2])
d1 = shuffle(d1, [3, 0, 1, 2])
a2 = shuffle(a2, [2, 3, 0, 1])
b2 = shuffle(b2, [2, 3, 0, 1])
c2 = shuffle(c2, [2, 3, 0, 1])
d2 = shuffle(d2, [2, 3, 0, 1])
a3 = shuffle(a3, [1, 2, 3, 0])
b3 = shuffle(b3, [1, 2, 3, 0])
c3 = shuffle(c3, [1, 2, 3, 0])
d3 = shuffle(d3, [1, 2, 3, 0])
round = round + 1
}
// XOR plaintext with keystream and store ciphertext (via i32 pointers)
store(ct_i32, elem_off, load(pt_i32, elem_off) .^ (a0 .+ row0_init))
store(ct_i32, elem_off + 4, load(pt_i32, elem_off + 4) .^ (a1 .+ row1_init))
store(ct_i32, elem_off + 8, load(pt_i32, elem_off + 8) .^ (a2 .+ row2_init))
store(ct_i32, elem_off + 12, load(pt_i32, elem_off + 12) .^ (a3 .+ r3a_init))
store(ct_i32, elem_off + 16, load(pt_i32, elem_off + 16) .^ (b0 .+ row0_init))
store(ct_i32, elem_off + 20, load(pt_i32, elem_off + 20) .^ (b1 .+ row1_init))
store(ct_i32, elem_off + 24, load(pt_i32, elem_off + 24) .^ (b2 .+ row2_init))
store(ct_i32, elem_off + 28, load(pt_i32, elem_off + 28) .^ (b3 .+ r3b_init))
store(ct_i32, elem_off + 32, load(pt_i32, elem_off + 32) .^ (c0 .+ row0_init))
store(ct_i32, elem_off + 36, load(pt_i32, elem_off + 36) .^ (c1 .+ row1_init))
store(ct_i32, elem_off + 40, load(pt_i32, elem_off + 40) .^ (c2 .+ row2_init))
store(ct_i32, elem_off + 44, load(pt_i32, elem_off + 44) .^ (c3 .+ r3c_init))
store(ct_i32, elem_off + 48, load(pt_i32, elem_off + 48) .^ (d0 .+ row0_init))
store(ct_i32, elem_off + 52, load(pt_i32, elem_off + 52) .^ (d1 .+ row1_init))
store(ct_i32, elem_off + 56, load(pt_i32, elem_off + 56) .^ (d2 .+ row2_init))
store(ct_i32, elem_off + 60, load(pt_i32, elem_off + 60) .^ (d3 .+ r3d_init))
// Stats on plaintext bytes (16 u8x16 chunks = 256 bytes)
let mut si: i32 = 0
while si < 16 {
let pt_chunk: u8x16 = load(plaintext, offset + si * 16)
let cmin: u8 = reduce_min(pt_chunk)
let cmax: u8 = reduce_max(pt_chunk)
if cmin < running_min {
running_min = cmin
}
if cmax > running_max {
running_max = cmax
}
running_sum = running_sum + to_i64(sum_u8x16(pt_chunk))
si = si + 1
}
running_count = running_count + 256
offset = offset + 256
elem_off = elem_off + 64
ctr = ctr + 4
}
// ---- Single-block loop for 64-byte remainders ----
while offset + 64 <= len {
let row3_init: i32x4 = [ctr, n0, n1, n2]i32x4
let mut r0: i32x4 = row0_init
let mut r1: i32x4 = row1_init
let mut r2: i32x4 = row2_init
let mut r3: i32x4 = row3_init
let mut round: i32 = 0
while round < 10 {
r0 = r0 .+ r1
r3 = rotl16(r3 .^ r0)
r2 = r2 .+ r3
r1 = rotl12(r1 .^ r2)
r0 = r0 .+ r1
r3 = rotl8(r3 .^ r0)
r2 = r2 .+ r3
r1 = rotl7(r1 .^ r2)
r1 = shuffle(r1, [1, 2, 3, 0])
r2 = shuffle(r2, [2, 3, 0, 1])
r3 = shuffle(r3, [3, 0, 1, 2])
r0 = r0 .+ r1
r3 = rotl16(r3 .^ r0)
r2 = r2 .+ r3
r1 = rotl12(r1 .^ r2)
r0 = r0 .+ r1
r3 = rotl8(r3 .^ r0)
r2 = r2 .+ r3
r1 = rotl7(r1 .^ r2)
r1 = shuffle(r1, [3, 0, 1, 2])
r2 = shuffle(r2, [2, 3, 0, 1])
r3 = shuffle(r3, [1, 2, 3, 0])
round = round + 1
}
store(ct_i32, elem_off, load(pt_i32, elem_off) .^ (r0 .+ row0_init))
store(ct_i32, elem_off + 4, load(pt_i32, elem_off + 4) .^ (r1 .+ row1_init))
store(ct_i32, elem_off + 8, load(pt_i32, elem_off + 8) .^ (r2 .+ row2_init))
store(ct_i32, elem_off + 12, load(pt_i32, elem_off + 12) .^ (r3 .+ row3_init))
// Stats on 4 u8x16 chunks (64 bytes)
let mut si: i32 = 0
while si < 4 {
let pt_chunk: u8x16 = load(plaintext, offset + si * 16)
let cmin: u8 = reduce_min(pt_chunk)
let cmax: u8 = reduce_max(pt_chunk)
if cmin < running_min {
running_min = cmin
}
if cmax > running_max {
running_max = cmax
}
running_sum = running_sum + to_i64(sum_u8x16(pt_chunk))
si = si + 1
}
running_count = running_count + 64
offset = offset + 64
elem_off = elem_off + 16
ctr = ctr + 1
}
// ---- Sub-block tail (< 64 bytes) ----
if offset < len {
chacha20_block(key, nonce, ctr, ks_i32)
let remaining: i32 = len - offset
let mut chunk_off: i32 = 0
// Full 16-byte chunks within the tail block
while chunk_off + 16 <= remaining {
let pt: u8x16 = load(plaintext, offset + chunk_off)
let ks: u8x16 = load(ks_u8, chunk_off)
store(ciphertext, offset + chunk_off, pt .^ ks)
// Stats
let cmin: u8 = reduce_min(pt)
let cmax: u8 = reduce_max(pt)
if cmin < running_min {
running_min = cmin
}
if cmax > running_max {
running_max = cmax
}
running_sum = running_sum + to_i64(sum_u8x16(pt))
running_count = running_count + 16
chunk_off = chunk_off + 16
}
// Byte-level tail (< 16 bytes)
let tail_count: i32 = remaining - chunk_off
if tail_count > 0 {
let tail_off: i32 = offset + chunk_off
let ks_tail: u8x16 = load_masked(ks_u8, chunk_off, tail_count)
let pt_tail: u8x16 = load_masked(plaintext, tail_off, tail_count)
store_masked(ciphertext, tail_off, pt_tail .^ ks_tail, tail_count)
// Stats on tail bytes — byte-by-byte to avoid zeroed-lane corruption
let mut ti: i32 = 0
let mut tail_sum: i32 = 0
while ti < tail_count {
let b: u8 = plaintext[tail_off + ti]
tail_sum = tail_sum + to_i32(b)
if b < running_min {
running_min = b
}
if b > running_max {
running_max = b
}
ti = ti + 1
}
running_sum = running_sum + to_i64(tail_sum)
running_count = running_count + tail_count
}
}
// Write outputs
out_sum[0] = running_sum
out_count[0] = running_count
out_min[0] = running_min
out_max[0] = running_max
}