sol-trade-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +390 -0
- package/dist/chunk-MMQAMIKR.mjs +3735 -0
- package/dist/chunk-NEZDFAYA.mjs +7744 -0
- package/dist/clients-VITWK7B6.mjs +1370 -0
- package/dist/index-1BK_FXsW.d.mts +2327 -0
- package/dist/index-1BK_FXsW.d.ts +2327 -0
- package/dist/index.d.mts +2659 -0
- package/dist/index.d.ts +2659 -0
- package/dist/index.js +13265 -0
- package/dist/index.mjs +562 -0
- package/dist/perf/index.d.mts +2 -0
- package/dist/perf/index.d.ts +2 -0
- package/dist/perf/index.js +3742 -0
- package/dist/perf/index.mjs +214 -0
- package/package.json +101 -0
- package/src/__tests__/complete_sdk.test.ts +354 -0
- package/src/__tests__/hotpath.test.ts +486 -0
- package/src/__tests__/nonce.test.ts +45 -0
- package/src/__tests__/sdk.test.ts +425 -0
- package/src/address-lookup/index.ts +197 -0
- package/src/cache/cache.ts +308 -0
- package/src/calc/index.ts +1058 -0
- package/src/calc/pumpfun.ts +124 -0
- package/src/common/bonding_curve.ts +272 -0
- package/src/common/compute-budget.ts +148 -0
- package/src/common/confirm-any-signature.ts +184 -0
- package/src/common/fast-timing.ts +481 -0
- package/src/common/fast_fn.ts +150 -0
- package/src/common/gas-fee-strategy.ts +253 -0
- package/src/common/map-pool.ts +23 -0
- package/src/common/nonce.ts +40 -0
- package/src/common/sdk-log.ts +460 -0
- package/src/common/seed.ts +381 -0
- package/src/common/spl-token.ts +578 -0
- package/src/common/subscription-handle.ts +644 -0
- package/src/common/trading-utils.ts +239 -0
- package/src/common/wsol-manager.ts +325 -0
- package/src/compute/compute_budget_manager.ts +187 -0
- package/src/compute/index.ts +21 -0
- package/src/constants/index.ts +96 -0
- package/src/execution/execution.ts +532 -0
- package/src/execution/index.ts +42 -0
- package/src/hotpath/executor.ts +464 -0
- package/src/hotpath/index.ts +64 -0
- package/src/hotpath/state.ts +435 -0
- package/src/index.ts +2117 -0
- package/src/instruction/bonk_builder.ts +730 -0
- package/src/instruction/index.ts +24 -0
- package/src/instruction/meteora_damm_v2_builder.ts +509 -0
- package/src/instruction/pumpfun_builder.ts +1183 -0
- package/src/instruction/pumpswap.ts +1123 -0
- package/src/instruction/raydium_amm_v4_builder.ts +692 -0
- package/src/instruction/raydium_cpmm_builder.ts +795 -0
- package/src/middleware/traits.ts +407 -0
- package/src/params/index.ts +483 -0
- package/src/perf/compiler-optimization.ts +529 -0
- package/src/perf/hardware.ts +631 -0
- package/src/perf/index.ts +9 -0
- package/src/perf/kernel-bypass.ts +656 -0
- package/src/perf/protocol.ts +682 -0
- package/src/perf/realtime.ts +592 -0
- package/src/perf/simd.ts +668 -0
- package/src/perf/syscall-bypass.ts +331 -0
- package/src/perf/ultra-low-latency.ts +505 -0
- package/src/perf/zero-copy.ts +589 -0
- package/src/pool/pool.ts +294 -0
- package/src/rpc/client.ts +345 -0
- package/src/sdk-errors.ts +13 -0
- package/src/security/index.ts +26 -0
- package/src/security/secure-key.ts +303 -0
- package/src/security/validators.ts +281 -0
- package/src/seed/pda.ts +262 -0
- package/src/serialization/index.ts +28 -0
- package/src/serialization/serialization.ts +288 -0
- package/src/swqos/clients.ts +1754 -0
- package/src/swqos/index.ts +50 -0
- package/src/swqos/providers.ts +1707 -0
- package/src/trading/core/async-executor.ts +702 -0
- package/src/trading/core/confirmation-monitor.ts +711 -0
- package/src/trading/core/index.ts +82 -0
- package/src/trading/core/retry-handler.ts +683 -0
- package/src/trading/core/transaction-pool.ts +780 -0
- package/src/trading/executor.ts +385 -0
- package/src/trading/factory.ts +282 -0
- package/src/trading/index.ts +30 -0
- package/src/types.ts +8 -0
- package/src/utils/index.ts +155 -0
package/src/perf/simd.ts
ADDED
|
@@ -0,0 +1,668 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* SIMD (Single Instruction Multiple Data) Module for Sol Trade SDK
|
|
4
|
+
* Provides SIMD operations, detection, and crypto optimizations.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// ===== Types =====
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* SIMD configuration
|
|
11
|
+
*/
|
|
12
|
+
export interface SIMDConfig {
|
|
13
|
+
/** Enable SIMD operations */
|
|
14
|
+
enableSIMD: boolean;
|
|
15
|
+
/** Preferred SIMD width (128, 256, 512) */
|
|
16
|
+
preferredWidth: number;
|
|
17
|
+
/** Enable WebAssembly SIMD */
|
|
18
|
+
enableWasmSIMD: boolean;
|
|
19
|
+
/** Fallback to scalar on failure */
|
|
20
|
+
fallbackToScalar: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Default SIMD configuration
|
|
25
|
+
*/
|
|
26
|
+
export function defaultSIMDConfig(): SIMDConfig {
|
|
27
|
+
return {
|
|
28
|
+
enableSIMD: true,
|
|
29
|
+
preferredWidth: 128,
|
|
30
|
+
enableWasmSIMD: true,
|
|
31
|
+
fallbackToScalar: true,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* SIMD capabilities
|
|
37
|
+
*/
|
|
38
|
+
export interface SIMDCapabilities {
|
|
39
|
+
/** SSE support */
|
|
40
|
+
sse: boolean;
|
|
41
|
+
/** SSE2 support */
|
|
42
|
+
sse2: boolean;
|
|
43
|
+
/** SSE3 support */
|
|
44
|
+
sse3: boolean;
|
|
45
|
+
/** SSSE3 support */
|
|
46
|
+
ssse3: boolean;
|
|
47
|
+
/** SSE4.1 support */
|
|
48
|
+
sse41: boolean;
|
|
49
|
+
/** SSE4.2 support */
|
|
50
|
+
sse42: boolean;
|
|
51
|
+
/** AVX support */
|
|
52
|
+
avx: boolean;
|
|
53
|
+
/** AVX2 support */
|
|
54
|
+
avx2: boolean;
|
|
55
|
+
/** AVX-512 support */
|
|
56
|
+
avx512: boolean;
|
|
57
|
+
/** NEON support (ARM) */
|
|
58
|
+
neon: boolean;
|
|
59
|
+
/** WebAssembly SIMD128 support */
|
|
60
|
+
wasmSimd128: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Vector types
|
|
65
|
+
*/
|
|
66
|
+
export type Float32Vector = Float32Array;
|
|
67
|
+
export type Float64Vector = Float64Array;
|
|
68
|
+
export type Int32Vector = Int32Array;
|
|
69
|
+
export type Int64Vector = BigInt64Array;
|
|
70
|
+
|
|
71
|
+
// ===== SIMD Detector =====
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Detects SIMD capabilities of the runtime environment.
|
|
75
|
+
*/
|
|
76
|
+
export class SIMDDetector {
|
|
77
|
+
private capabilities: SIMDCapabilities | null = null;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Detect SIMD capabilities
|
|
81
|
+
*/
|
|
82
|
+
detect(): SIMDCapabilities {
|
|
83
|
+
if (this.capabilities) {
|
|
84
|
+
return this.capabilities;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// In JavaScript/Node.js, we can't directly detect CPU features
|
|
88
|
+
// We infer based on the environment
|
|
89
|
+
this.capabilities = {
|
|
90
|
+
sse: false,
|
|
91
|
+
sse2: false,
|
|
92
|
+
sse3: false,
|
|
93
|
+
ssse3: false,
|
|
94
|
+
sse41: false,
|
|
95
|
+
sse42: false,
|
|
96
|
+
avx: false,
|
|
97
|
+
avx2: false,
|
|
98
|
+
avx512: false,
|
|
99
|
+
neon: this.detectNEON(),
|
|
100
|
+
wasmSimd128: this.detectWasmSIMD128(),
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
return this.capabilities;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Check if WebAssembly SIMD128 is available
|
|
108
|
+
*/
|
|
109
|
+
private detectWasmSIMD128(): boolean {
|
|
110
|
+
if (typeof WebAssembly === 'undefined') {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
// Try to compile a simple SIMD module
|
|
116
|
+
const simdTest = new Uint8Array([
|
|
117
|
+
0x00, 0x61, 0x73, 0x6d, // WASM magic
|
|
118
|
+
0x01, 0x00, 0x00, 0x00, // version
|
|
119
|
+
0x01, 0x05, 0x01, // type section
|
|
120
|
+
0x60, 0x00, 0x01, 0x7b, // func type with v128 return
|
|
121
|
+
0x03, 0x02, 0x01, 0x00, // func section
|
|
122
|
+
0x0a, 0x0b, 0x01, // code section
|
|
123
|
+
0x09, 0x00, // func body
|
|
124
|
+
0xfd, 0x0f, 0x00, 0x00, // v128.const
|
|
125
|
+
0x00, 0x00, 0x00, 0x00,
|
|
126
|
+
0x00, 0x00, 0x00, 0x00,
|
|
127
|
+
0x0b, // end
|
|
128
|
+
]);
|
|
129
|
+
|
|
130
|
+
WebAssembly.compile(simdTest);
|
|
131
|
+
return true;
|
|
132
|
+
} catch {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Detect NEON support (ARM)
|
|
139
|
+
*/
|
|
140
|
+
private detectNEON(): boolean {
|
|
141
|
+
// In Node.js, we can't directly detect NEON
|
|
142
|
+
// Assume true on ARM64 platforms
|
|
143
|
+
if (typeof process !== 'undefined' && process.arch === 'arm64') {
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Check if specific capability is available
|
|
151
|
+
*/
|
|
152
|
+
hasCapability(capability: keyof SIMDCapabilities): boolean {
|
|
153
|
+
const caps = this.detect();
|
|
154
|
+
return caps[capability];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Get optimal vector width
|
|
159
|
+
*/
|
|
160
|
+
getOptimalWidth(): number {
|
|
161
|
+
const caps = this.detect();
|
|
162
|
+
|
|
163
|
+
if (caps.avx512) return 512;
|
|
164
|
+
if (caps.avx || caps.avx2) return 256;
|
|
165
|
+
if (caps.sse || caps.sse2 || caps.neon || caps.wasmSimd128) return 128;
|
|
166
|
+
|
|
167
|
+
return 64; // Scalar
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Check if any SIMD is available
|
|
172
|
+
*/
|
|
173
|
+
hasAnySIMD(): boolean {
|
|
174
|
+
const caps = this.detect();
|
|
175
|
+
return Object.values(caps).some(v => v);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// ===== Vectorized Math =====
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Vectorized mathematical operations using SIMD where available.
|
|
183
|
+
*/
|
|
184
|
+
export class VectorizedMath {
|
|
185
|
+
private config: SIMDConfig;
|
|
186
|
+
private detector: SIMDDetector;
|
|
187
|
+
|
|
188
|
+
constructor(config: SIMDConfig = defaultSIMDConfig()) {
|
|
189
|
+
this.config = config;
|
|
190
|
+
this.detector = new SIMDDetector();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Vector addition: result = a + b
|
|
195
|
+
*/
|
|
196
|
+
add(a: Float32Array, b: Float32Array, result?: Float32Array): Float32Array {
|
|
197
|
+
const len = Math.min(a.length, b.length);
|
|
198
|
+
const out = result || new Float32Array(len);
|
|
199
|
+
|
|
200
|
+
if (this.config.enableSIMD && this.detector.hasAnySIMD()) {
|
|
201
|
+
// Process 4 elements at a time (SIMD width)
|
|
202
|
+
const simdLen = len - (len % 4);
|
|
203
|
+
for (let i = 0; i < simdLen; i += 4) {
|
|
204
|
+
out[i] = a[i] + b[i];
|
|
205
|
+
out[i + 1] = a[i + 1] + b[i + 1];
|
|
206
|
+
out[i + 2] = a[i + 2] + b[i + 2];
|
|
207
|
+
out[i + 3] = a[i + 3] + b[i + 3];
|
|
208
|
+
}
|
|
209
|
+
// Scalar remainder
|
|
210
|
+
for (let i = simdLen; i < len; i++) {
|
|
211
|
+
out[i] = a[i] + b[i];
|
|
212
|
+
}
|
|
213
|
+
} else {
|
|
214
|
+
for (let i = 0; i < len; i++) {
|
|
215
|
+
out[i] = a[i] + b[i];
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Vector subtraction: result = a - b
|
|
224
|
+
*/
|
|
225
|
+
subtract(a: Float32Array, b: Float32Array, result?: Float32Array): Float32Array {
|
|
226
|
+
const len = Math.min(a.length, b.length);
|
|
227
|
+
const out = result || new Float32Array(len);
|
|
228
|
+
|
|
229
|
+
if (this.config.enableSIMD && this.detector.hasAnySIMD()) {
|
|
230
|
+
const simdLen = len - (len % 4);
|
|
231
|
+
for (let i = 0; i < simdLen; i += 4) {
|
|
232
|
+
out[i] = a[i] - b[i];
|
|
233
|
+
out[i + 1] = a[i + 1] - b[i + 1];
|
|
234
|
+
out[i + 2] = a[i + 2] - b[i + 2];
|
|
235
|
+
out[i + 3] = a[i + 3] - b[i + 3];
|
|
236
|
+
}
|
|
237
|
+
for (let i = simdLen; i < len; i++) {
|
|
238
|
+
out[i] = a[i] - b[i];
|
|
239
|
+
}
|
|
240
|
+
} else {
|
|
241
|
+
for (let i = 0; i < len; i++) {
|
|
242
|
+
out[i] = a[i] - b[i];
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Vector multiplication: result = a * b
|
|
251
|
+
*/
|
|
252
|
+
multiply(a: Float32Array, b: Float32Array, result?: Float32Array): Float32Array {
|
|
253
|
+
const len = Math.min(a.length, b.length);
|
|
254
|
+
const out = result || new Float32Array(len);
|
|
255
|
+
|
|
256
|
+
if (this.config.enableSIMD && this.detector.hasAnySIMD()) {
|
|
257
|
+
const simdLen = len - (len % 4);
|
|
258
|
+
for (let i = 0; i < simdLen; i += 4) {
|
|
259
|
+
out[i] = a[i] * b[i];
|
|
260
|
+
out[i + 1] = a[i + 1] * b[i + 1];
|
|
261
|
+
out[i + 2] = a[i + 2] * b[i + 2];
|
|
262
|
+
out[i + 3] = a[i + 3] * b[i + 3];
|
|
263
|
+
}
|
|
264
|
+
for (let i = simdLen; i < len; i++) {
|
|
265
|
+
out[i] = a[i] * b[i];
|
|
266
|
+
}
|
|
267
|
+
} else {
|
|
268
|
+
for (let i = 0; i < len; i++) {
|
|
269
|
+
out[i] = a[i] * b[i];
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return out;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Vector division: result = a / b
|
|
278
|
+
*/
|
|
279
|
+
divide(a: Float32Array, b: Float32Array, result?: Float32Array): Float32Array {
|
|
280
|
+
const len = Math.min(a.length, b.length);
|
|
281
|
+
const out = result || new Float32Array(len);
|
|
282
|
+
|
|
283
|
+
if (this.config.enableSIMD && this.detector.hasAnySIMD()) {
|
|
284
|
+
const simdLen = len - (len % 4);
|
|
285
|
+
for (let i = 0; i < simdLen; i += 4) {
|
|
286
|
+
out[i] = a[i] / b[i];
|
|
287
|
+
out[i + 1] = a[i + 1] / b[i + 1];
|
|
288
|
+
out[i + 2] = a[i + 2] / b[i + 2];
|
|
289
|
+
out[i + 3] = a[i + 3] / b[i + 3];
|
|
290
|
+
}
|
|
291
|
+
for (let i = simdLen; i < len; i++) {
|
|
292
|
+
out[i] = a[i] / b[i];
|
|
293
|
+
}
|
|
294
|
+
} else {
|
|
295
|
+
for (let i = 0; i < len; i++) {
|
|
296
|
+
out[i] = a[i] / b[i];
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return out;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Vector dot product
|
|
305
|
+
*/
|
|
306
|
+
dot(a: Float32Array, b: Float32Array): number {
|
|
307
|
+
const len = Math.min(a.length, b.length);
|
|
308
|
+
let sum = 0;
|
|
309
|
+
|
|
310
|
+
if (this.config.enableSIMD && this.detector.hasAnySIMD()) {
|
|
311
|
+
// Pairwise summation for better precision
|
|
312
|
+
const simdLen = len - (len % 4);
|
|
313
|
+
let sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
|
|
314
|
+
|
|
315
|
+
for (let i = 0; i < simdLen; i += 4) {
|
|
316
|
+
sum0 += a[i] * b[i];
|
|
317
|
+
sum1 += a[i + 1] * b[i + 1];
|
|
318
|
+
sum2 += a[i + 2] * b[i + 2];
|
|
319
|
+
sum3 += a[i + 3] * b[i + 3];
|
|
320
|
+
}
|
|
321
|
+
sum = sum0 + sum1 + sum2 + sum3;
|
|
322
|
+
|
|
323
|
+
for (let i = simdLen; i < len; i++) {
|
|
324
|
+
sum += a[i] * b[i];
|
|
325
|
+
}
|
|
326
|
+
} else {
|
|
327
|
+
for (let i = 0; i < len; i++) {
|
|
328
|
+
sum += a[i] * b[i];
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return sum;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Vector sum
|
|
337
|
+
*/
|
|
338
|
+
sum(a: Float32Array): number {
|
|
339
|
+
let sum = 0;
|
|
340
|
+
|
|
341
|
+
if (this.config.enableSIMD && this.detector.hasAnySIMD()) {
|
|
342
|
+
const len = a.length;
|
|
343
|
+
const simdLen = len - (len % 4);
|
|
344
|
+
let sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0;
|
|
345
|
+
|
|
346
|
+
for (let i = 0; i < simdLen; i += 4) {
|
|
347
|
+
sum0 += a[i];
|
|
348
|
+
sum1 += a[i + 1];
|
|
349
|
+
sum2 += a[i + 2];
|
|
350
|
+
sum3 += a[i + 3];
|
|
351
|
+
}
|
|
352
|
+
sum = sum0 + sum1 + sum2 + sum3;
|
|
353
|
+
|
|
354
|
+
for (let i = simdLen; i < len; i++) {
|
|
355
|
+
sum += a[i];
|
|
356
|
+
}
|
|
357
|
+
} else {
|
|
358
|
+
for (let i = 0; i < a.length; i++) {
|
|
359
|
+
sum += a[i];
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return sum;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Vector minimum
|
|
368
|
+
*/
|
|
369
|
+
min(a: Float32Array): number {
|
|
370
|
+
if (a.length === 0) return NaN;
|
|
371
|
+
|
|
372
|
+
let min = a[0];
|
|
373
|
+
for (let i = 1; i < a.length; i++) {
|
|
374
|
+
if (a[i] < min) min = a[i];
|
|
375
|
+
}
|
|
376
|
+
return min;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Vector maximum
|
|
381
|
+
*/
|
|
382
|
+
max(a: Float32Array): number {
|
|
383
|
+
if (a.length === 0) return NaN;
|
|
384
|
+
|
|
385
|
+
let max = a[0];
|
|
386
|
+
for (let i = 1; i < a.length; i++) {
|
|
387
|
+
if (a[i] > max) max = a[i];
|
|
388
|
+
}
|
|
389
|
+
return max;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Element-wise absolute value
|
|
394
|
+
*/
|
|
395
|
+
abs(a: Float32Array, result?: Float32Array): Float32Array {
|
|
396
|
+
const out = result || new Float32Array(a.length);
|
|
397
|
+
|
|
398
|
+
for (let i = 0; i < a.length; i++) {
|
|
399
|
+
out[i] = Math.abs(a[i]);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return out;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Element-wise square root
|
|
407
|
+
*/
|
|
408
|
+
sqrt(a: Float32Array, result?: Float32Array): Float32Array {
|
|
409
|
+
const out = result || new Float32Array(a.length);
|
|
410
|
+
|
|
411
|
+
for (let i = 0; i < a.length; i++) {
|
|
412
|
+
out[i] = Math.sqrt(a[i]);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return out;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// ===== Crypto SIMD =====
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* SIMD-optimized cryptographic operations.
|
|
423
|
+
* Provides accelerated hashing and verification.
|
|
424
|
+
*/
|
|
425
|
+
export class CryptoSIMD {
|
|
426
|
+
private config: SIMDConfig;
|
|
427
|
+
private detector: SIMDDetector;
|
|
428
|
+
|
|
429
|
+
constructor(config: SIMDConfig = defaultSIMDConfig()) {
|
|
430
|
+
this.config = config;
|
|
431
|
+
this.detector = new SIMDDetector();
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* XOR two byte arrays (SIMD-accelerated)
|
|
436
|
+
*/
|
|
437
|
+
xor(a: Uint8Array, b: Uint8Array, result?: Uint8Array): Uint8Array {
|
|
438
|
+
const len = Math.min(a.length, b.length);
|
|
439
|
+
const out = result || new Uint8Array(len);
|
|
440
|
+
|
|
441
|
+
if (this.config.enableSIMD && this.detector.hasAnySIMD()) {
|
|
442
|
+
// Process 16 bytes at a time
|
|
443
|
+
const simdLen = len - (len % 16);
|
|
444
|
+
for (let i = 0; i < simdLen; i += 16) {
|
|
445
|
+
for (let j = 0; j < 16; j++) {
|
|
446
|
+
out[i + j] = a[i + j] ^ b[i + j];
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
// Scalar remainder
|
|
450
|
+
for (let i = simdLen; i < len; i++) {
|
|
451
|
+
out[i] = a[i] ^ b[i];
|
|
452
|
+
}
|
|
453
|
+
} else {
|
|
454
|
+
for (let i = 0; i < len; i++) {
|
|
455
|
+
out[i] = a[i] ^ b[i];
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
return out;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Compare two byte arrays for equality (SIMD-accelerated)
|
|
464
|
+
*/
|
|
465
|
+
equals(a: Uint8Array, b: Uint8Array): boolean {
|
|
466
|
+
if (a.length !== b.length) return false;
|
|
467
|
+
|
|
468
|
+
if (this.config.enableSIMD && this.detector.hasAnySIMD()) {
|
|
469
|
+
const len = a.length;
|
|
470
|
+
const simdLen = len - (len % 16);
|
|
471
|
+
|
|
472
|
+
for (let i = 0; i < simdLen; i += 16) {
|
|
473
|
+
for (let j = 0; j < 16; j++) {
|
|
474
|
+
if (a[i + j] !== b[i + j]) return false;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
for (let i = simdLen; i < len; i++) {
|
|
479
|
+
if (a[i] !== b[i]) return false;
|
|
480
|
+
}
|
|
481
|
+
} else {
|
|
482
|
+
for (let i = 0; i < a.length; i++) {
|
|
483
|
+
if (a[i] !== b[i]) return false;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Fill array with value (SIMD-accelerated)
|
|
492
|
+
*/
|
|
493
|
+
fill(buffer: Uint8Array, value: number): void {
|
|
494
|
+
if (this.config.enableSIMD && this.detector.hasAnySIMD()) {
|
|
495
|
+
const len = buffer.length;
|
|
496
|
+
const simdLen = len - (len % 16);
|
|
497
|
+
|
|
498
|
+
for (let i = 0; i < simdLen; i += 16) {
|
|
499
|
+
for (let j = 0; j < 16; j++) {
|
|
500
|
+
buffer[i + j] = value;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
for (let i = simdLen; i < len; i++) {
|
|
505
|
+
buffer[i] = value;
|
|
506
|
+
}
|
|
507
|
+
} else {
|
|
508
|
+
buffer.fill(value);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* Copy bytes (SIMD-accelerated)
|
|
514
|
+
*/
|
|
515
|
+
copy(src: Uint8Array, dst: Uint8Array, srcOffset: number = 0, dstOffset: number = 0, length?: number): void {
|
|
516
|
+
const len = length || Math.min(src.length - srcOffset, dst.length - dstOffset);
|
|
517
|
+
|
|
518
|
+
if (this.config.enableSIMD && this.detector.hasAnySIMD()) {
|
|
519
|
+
const simdLen = len - (len % 16);
|
|
520
|
+
|
|
521
|
+
for (let i = 0; i < simdLen; i += 16) {
|
|
522
|
+
for (let j = 0; j < 16; j++) {
|
|
523
|
+
dst[dstOffset + i + j] = src[srcOffset + i + j];
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
for (let i = simdLen; i < len; i++) {
|
|
528
|
+
dst[dstOffset + i] = src[srcOffset + i];
|
|
529
|
+
}
|
|
530
|
+
} else {
|
|
531
|
+
dst.set(src.subarray(srcOffset, srcOffset + len), dstOffset);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Compute parity of byte array
|
|
537
|
+
*/
|
|
538
|
+
parity(data: Uint8Array): number {
|
|
539
|
+
let parity = 0;
|
|
540
|
+
|
|
541
|
+
for (let i = 0; i < data.length; i++) {
|
|
542
|
+
parity ^= data[i];
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
return parity;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* Rotate bytes left
|
|
550
|
+
*/
|
|
551
|
+
rotateLeft(data: Uint8Array, bits: number, result?: Uint8Array): Uint8Array {
|
|
552
|
+
const out = result || new Uint8Array(data.length);
|
|
553
|
+
const bytes = Math.floor(bits / 8);
|
|
554
|
+
const remainingBits = bits % 8;
|
|
555
|
+
|
|
556
|
+
if (remainingBits === 0) {
|
|
557
|
+
// Byte-aligned rotation
|
|
558
|
+
for (let i = 0; i < data.length; i++) {
|
|
559
|
+
out[i] = data[(i + bytes) % data.length];
|
|
560
|
+
}
|
|
561
|
+
} else {
|
|
562
|
+
// Bit rotation
|
|
563
|
+
for (let i = 0; i < data.length; i++) {
|
|
564
|
+
const srcIdx = (i + bytes) % data.length;
|
|
565
|
+
const nextIdx = (srcIdx + 1) % data.length;
|
|
566
|
+
out[i] = (data[srcIdx] << remainingBits) | (data[nextIdx] >> (8 - remainingBits));
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
return out;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// ===== SIMD Vector Types =====
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* 128-bit SIMD vector (4 floats)
|
|
578
|
+
*/
|
|
579
|
+
export class Float32x4 {
|
|
580
|
+
private data: Float32Array;
|
|
581
|
+
|
|
582
|
+
constructor(x?: number, y?: number, z?: number, w?: number) {
|
|
583
|
+
this.data = new Float32Array(4);
|
|
584
|
+
if (x !== undefined) this.data[0] = x;
|
|
585
|
+
if (y !== undefined) this.data[1] = y;
|
|
586
|
+
if (z !== undefined) this.data[2] = z;
|
|
587
|
+
if (w !== undefined) this.data[3] = w;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
static fromArray(arr: Float32Array, offset: number = 0): Float32x4 {
|
|
591
|
+
return new Float32x4(arr[offset], arr[offset + 1], arr[offset + 2], arr[offset + 3]);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
add(other: Float32x4): Float32x4 {
|
|
595
|
+
return new Float32x4(
|
|
596
|
+
this.data[0] + other.data[0],
|
|
597
|
+
this.data[1] + other.data[1],
|
|
598
|
+
this.data[2] + other.data[2],
|
|
599
|
+
this.data[3] + other.data[3]
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
subtract(other: Float32x4): Float32x4 {
|
|
604
|
+
return new Float32x4(
|
|
605
|
+
this.data[0] - other.data[0],
|
|
606
|
+
this.data[1] - other.data[1],
|
|
607
|
+
this.data[2] - other.data[2],
|
|
608
|
+
this.data[3] - other.data[3]
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
multiply(other: Float32x4): Float32x4 {
|
|
613
|
+
return new Float32x4(
|
|
614
|
+
this.data[0] * other.data[0],
|
|
615
|
+
this.data[1] * other.data[1],
|
|
616
|
+
this.data[2] * other.data[2],
|
|
617
|
+
this.data[3] * other.data[3]
|
|
618
|
+
);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
toArray(): Float32Array {
|
|
622
|
+
return this.data.slice();
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
get x(): number { return this.data[0]; }
|
|
626
|
+
get y(): number { return this.data[1]; }
|
|
627
|
+
get z(): number { return this.data[2]; }
|
|
628
|
+
get w(): number { return this.data[3]; }
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// ===== Convenience Functions =====
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* Create a SIMD detector
|
|
635
|
+
*/
|
|
636
|
+
export function createSIMDDetector(): SIMDDetector {
|
|
637
|
+
return new SIMDDetector();
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* Create a vectorized math instance
|
|
642
|
+
*/
|
|
643
|
+
export function createVectorizedMath(config?: Partial<SIMDConfig>): VectorizedMath {
|
|
644
|
+
const fullConfig = { ...defaultSIMDConfig(), ...config };
|
|
645
|
+
return new VectorizedMath(fullConfig);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* Create a crypto SIMD instance
|
|
650
|
+
*/
|
|
651
|
+
export function createCryptoSIMD(config?: Partial<SIMDConfig>): CryptoSIMD {
|
|
652
|
+
const fullConfig = { ...defaultSIMDConfig(), ...config };
|
|
653
|
+
return new CryptoSIMD(fullConfig);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* Check if SIMD is available
|
|
658
|
+
*/
|
|
659
|
+
export function isSIMDAvailable(): boolean {
|
|
660
|
+
return new SIMDDetector().hasAnySIMD();
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
/**
|
|
664
|
+
* Get optimal SIMD width
|
|
665
|
+
*/
|
|
666
|
+
export function getOptimalSIMDWidth(): number {
|
|
667
|
+
return new SIMDDetector().getOptimalWidth();
|
|
668
|
+
}
|