-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytelevel.go
More file actions
166 lines (157 loc) · 3.98 KB
/
Copy pathbytelevel.go
File metadata and controls
166 lines (157 loc) · 3.98 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
package itb
import (
"encoding/binary"
"sync"
)
// splitForTripleParallel is the parallel counterpart of [splitForTriple].
// Both branches run 3-way parallel kernels - byte-level via
// [splitTripleParallel] (3 strided goroutines, one per lane), bit-soup via
// [splitTripleBitsParallel] (period-3-byte chunks across CPU cores).
// Test only function ; Not used in production.
func splitForTripleParallel(data []byte) (p0, p1, p2 []byte) {
if isBitSoupEnabled() {
p0, p1, p2, _ = splitTripleBitsParallel(prependTripleLen(data))
return
}
return splitTripleParallel(data)
}
// interleaveForTripleParallel is the parallel counterpart of
// [interleaveForTriple]. Same plausible-decryption invariant - never errors,
// returns wrong-seed garbage clamped to the recovered payload extent.
// Both branches run 3-way parallel kernels - byte-level via
// [interleaveTripleParallel], bit-soup via [interleaveTripleBitsParallel].
// Test only function ; Not used in production.
func interleaveForTripleParallel(p0, p1, p2 []byte) []byte {
if !isBitSoupEnabled() {
return interleaveTripleParallel(p0, p1, p2)
}
totalBits := recoverTripleBitsLen(len(p0), len(p1), len(p2))
framed := interleaveTripleBitsParallel(p0, p1, p2, totalBits)
if len(framed) < 4 {
return framed
}
length := binary.BigEndian.Uint32(framed[:4])
end := uint64(length) + 4
if end > uint64(len(framed)) {
end = uint64(len(framed))
}
return framed[4:int(end)]
}
// splitTriple splits data into 3 parts: bytes[0::3], bytes[1::3], bytes[2::3].
func splitTriple(data []byte) (p0, p1, p2 []byte) {
n := len(data)
s0 := (n + 2) / 3
s1 := (n + 1) / 3
s2 := n / 3
p0 = make([]byte, s0)
p1 = make([]byte, s1)
p2 = make([]byte, s2)
for i, b := range data {
switch i % 3 {
case 0:
p0[i/3] = b
case 1:
p1[i/3] = b
case 2:
p2[i/3] = b
}
}
return
}
// interleaveTriple reassembles 3 parts into original byte order.
func interleaveTriple(p0, p1, p2 []byte) []byte {
total := len(p0) + len(p1) + len(p2)
result := make([]byte, total)
for i := 0; i < len(p0); i++ {
idx := i * 3
if idx < total {
result[idx] = p0[i]
}
}
for i := 0; i < len(p1); i++ {
idx := i*3 + 1
if idx < total {
result[idx] = p1[i]
}
}
for i := 0; i < len(p2); i++ {
idx := i*3 + 2
if idx < total {
result[idx] = p2[i]
}
}
return result
}
// splitTripleParallel produces output bit-identical to [splitTriple] via
// 3 parallel goroutines. Each goroutine reads strided bytes from data
// (lane k reads data[k], data[k+3], data[k+6], ...) and writes to its
// own lane buffer. No synchronization beyond the final wg.Wait - each
// goroutine writes to a disjoint buffer.
func splitTripleParallel(data []byte) (p0, p1, p2 []byte) {
n := len(data)
p0 = make([]byte, (n+2)/3)
p1 = make([]byte, (n+1)/3)
p2 = make([]byte, n/3)
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
for i := 0; i < n; i += 3 {
p0[i/3] = data[i]
}
}()
go func() {
defer wg.Done()
for i := 1; i < n; i += 3 {
p1[i/3] = data[i]
}
}()
go func() {
defer wg.Done()
for i := 2; i < n; i += 3 {
p2[i/3] = data[i]
}
}()
wg.Wait()
return
}
// interleaveTripleParallel produces output bit-identical to
// [interleaveTriple] via 3 parallel goroutines. Each goroutine writes to
// its own striped subset of the output (lane 0: indices 0, 3, 6, ...;
// lane 1: 1, 4, 7, ...; lane 2: 2, 5, 8, ...) - disjoint write set, no
// race between goroutines.
func interleaveTripleParallel(p0, p1, p2 []byte) []byte {
total := len(p0) + len(p1) + len(p2)
result := make([]byte, total)
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
for i := 0; i < len(p0); i++ {
idx := i * 3
if idx < total {
result[idx] = p0[i]
}
}
}()
go func() {
defer wg.Done()
for i := 0; i < len(p1); i++ {
idx := i*3 + 1
if idx < total {
result[idx] = p1[i]
}
}
}()
go func() {
defer wg.Done()
for i := 0; i < len(p2); i++ {
idx := i*3 + 2
if idx < total {
result[idx] = p2[i]
}
}
}()
wg.Wait()
return result
}