@svrnsec/pulse 0.6.0 → 0.8.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/LICENSE +21 -21
- package/README.md +883 -622
- package/SECURITY.md +86 -86
- package/bin/svrnsec-pulse.js +7 -7
- package/dist/{pulse.cjs.js → pulse.cjs} +6379 -6420
- package/dist/pulse.cjs.map +1 -0
- package/dist/pulse.esm.js +6380 -6421
- package/dist/pulse.esm.js.map +1 -1
- package/index.d.ts +895 -846
- package/package.json +185 -165
- package/pkg/pulse_core.js +174 -173
- package/src/analysis/audio.js +213 -213
- package/src/analysis/authenticityAudit.js +408 -390
- package/src/analysis/coherence.js +502 -502
- package/src/analysis/coordinatedBehavior.js +825 -0
- package/src/analysis/heuristic.js +428 -428
- package/src/analysis/jitter.js +446 -446
- package/src/analysis/llm.js +473 -472
- package/src/analysis/populationEntropy.js +404 -403
- package/src/analysis/provider.js +248 -248
- package/src/analysis/refraction.js +392 -0
- package/src/analysis/trustScore.js +356 -356
- package/src/cli/args.js +36 -36
- package/src/cli/commands/scan.js +192 -192
- package/src/cli/runner.js +157 -157
- package/src/collector/adaptive.js +200 -200
- package/src/collector/bio.js +297 -287
- package/src/collector/canvas.js +247 -239
- package/src/collector/dram.js +203 -203
- package/src/collector/enf.js +311 -311
- package/src/collector/entropy.js +195 -195
- package/src/collector/gpu.js +248 -245
- package/src/collector/idleAttestation.js +480 -480
- package/src/collector/sabTimer.js +189 -191
- package/src/fingerprint.js +475 -475
- package/src/index.js +342 -342
- package/src/integrations/react-native.js +462 -459
- package/src/integrations/react.js +184 -185
- package/src/middleware/express.js +155 -155
- package/src/middleware/next.js +174 -175
- package/src/proof/challenge.js +249 -249
- package/src/proof/engagementToken.js +426 -394
- package/src/proof/fingerprint.js +268 -268
- package/src/proof/validator.js +83 -143
- package/src/registry/serializer.js +349 -349
- package/src/terminal.js +263 -263
- package/src/update-notifier.js +259 -264
- package/dist/pulse.cjs.js.map +0 -1
package/src/collector/bio.js
CHANGED
|
@@ -1,287 +1,297 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @
|
|
3
|
-
*
|
|
4
|
-
* Captures mouse-movement micro-stutters and keystroke-cadence dynamics
|
|
5
|
-
* WHILE the hardware entropy probe is running. Computes the
|
|
6
|
-
* "Interference Coefficient": how much human input jitters hardware timing.
|
|
7
|
-
*
|
|
8
|
-
* PRIVACY NOTE: Only timing deltas are retained. No key labels, no raw
|
|
9
|
-
* (x, y) coordinates, no content of any kind is stored or transmitted.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
// ---------------------------------------------------------------------------
|
|
13
|
-
// Internal state
|
|
14
|
-
// ---------------------------------------------------------------------------
|
|
15
|
-
const MAX_EVENTS
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
25
|
-
this.
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
this._active
|
|
40
|
-
this.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
window.addEventListener('
|
|
45
|
-
window.addEventListener('
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
this._active
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
window.removeEventListener('
|
|
56
|
-
window.removeEventListener('
|
|
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
|
-
this.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if
|
|
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
|
-
const
|
|
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
|
-
const
|
|
203
|
-
|
|
204
|
-
return
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
if (
|
|
210
|
-
const
|
|
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
|
-
const
|
|
259
|
-
const
|
|
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
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @svrnsec/pulse — Bio-Binding Layer
|
|
3
|
+
*
|
|
4
|
+
* Captures mouse-movement micro-stutters and keystroke-cadence dynamics
|
|
5
|
+
* WHILE the hardware entropy probe is running. Computes the
|
|
6
|
+
* "Interference Coefficient": how much human input jitters hardware timing.
|
|
7
|
+
*
|
|
8
|
+
* PRIVACY NOTE: Only timing deltas are retained. No key labels, no raw
|
|
9
|
+
* (x, y) coordinates, no content of any kind is stored or transmitted.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Internal state
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
const MAX_EVENTS = 500; // rolling buffer cap
|
|
16
|
+
const MAX_KEY_STATES = 50; // cap on simultaneous tracked key states
|
|
17
|
+
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// BioCollector class
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
export class BioCollector {
|
|
22
|
+
constructor() {
|
|
23
|
+
this._mouseEvents = []; // { t: DOMHighResTimeStamp, dx, dy }
|
|
24
|
+
this._keyEvents = []; // { t, type: 'down'|'up', dwell: ms|null }
|
|
25
|
+
this._lastKey = {}; // keyCode → { downAt: t }
|
|
26
|
+
this._lastMouse = null; // { t, x, y }
|
|
27
|
+
this._startTime = null;
|
|
28
|
+
this._active = false;
|
|
29
|
+
|
|
30
|
+
// Bound handlers (needed for removeEventListener)
|
|
31
|
+
this._onMouseMove = this._onMouseMove.bind(this);
|
|
32
|
+
this._onKeyDown = this._onKeyDown.bind(this);
|
|
33
|
+
this._onKeyUp = this._onKeyUp.bind(this);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ── Lifecycle ────────────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
start() {
|
|
39
|
+
if (this._active) return;
|
|
40
|
+
this._active = true;
|
|
41
|
+
this._startTime = performance.now();
|
|
42
|
+
|
|
43
|
+
if (typeof window !== 'undefined') {
|
|
44
|
+
window.addEventListener('pointermove', this._onMouseMove, { passive: true });
|
|
45
|
+
window.addEventListener('keydown', this._onKeyDown, { passive: true });
|
|
46
|
+
window.addEventListener('keyup', this._onKeyUp, { passive: true });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
stop() {
|
|
51
|
+
if (!this._active) return;
|
|
52
|
+
this._active = false;
|
|
53
|
+
|
|
54
|
+
if (typeof window !== 'undefined') {
|
|
55
|
+
window.removeEventListener('pointermove', this._onMouseMove);
|
|
56
|
+
window.removeEventListener('keydown', this._onKeyDown);
|
|
57
|
+
window.removeEventListener('keyup', this._onKeyUp);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
this._lastMouse = null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ── Event handlers ────────────────────────────────────────────────────────
|
|
64
|
+
|
|
65
|
+
_onMouseMove(e) {
|
|
66
|
+
if (!this._active) return;
|
|
67
|
+
const t = e.timeStamp ?? performance.now();
|
|
68
|
+
const cur = { t, x: e.clientX, y: e.clientY };
|
|
69
|
+
|
|
70
|
+
if (this._lastMouse) {
|
|
71
|
+
const dt = t - this._lastMouse.t;
|
|
72
|
+
const dx = cur.x - this._lastMouse.x;
|
|
73
|
+
const dy = cur.y - this._lastMouse.y;
|
|
74
|
+
// Only store the delta, not absolute position (privacy)
|
|
75
|
+
if (this._mouseEvents.length < MAX_EVENTS) {
|
|
76
|
+
this._mouseEvents.push({ t, dt, dx, dy,
|
|
77
|
+
pressure: e.pressure ?? 0,
|
|
78
|
+
pointerType: e.pointerType ?? 'mouse' });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
this._lastMouse = cur;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
_onKeyDown(e) {
|
|
85
|
+
if (!this._active) return;
|
|
86
|
+
const t = e.timeStamp ?? performance.now();
|
|
87
|
+
// Store timestamp keyed by code (NOT key label)
|
|
88
|
+
this._lastKey[e.code] = { downAt: t };
|
|
89
|
+
// Prevent unbounded growth if keys are held without keyup
|
|
90
|
+
if (Object.keys(this._lastKey).length > MAX_KEY_STATES) {
|
|
91
|
+
const oldest = Object.entries(this._lastKey)
|
|
92
|
+
.sort((a, b) => a[1].downAt - b[1].downAt)[0];
|
|
93
|
+
if (oldest) delete this._lastKey[oldest[0]];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
_onKeyUp(e) {
|
|
98
|
+
if (!this._active) return;
|
|
99
|
+
const t = e.timeStamp ?? performance.now();
|
|
100
|
+
const rec = this._lastKey[e.code];
|
|
101
|
+
const dwell = rec ? (t - rec.downAt) : null;
|
|
102
|
+
delete this._lastKey[e.code];
|
|
103
|
+
|
|
104
|
+
if (this._keyEvents.length < MAX_EVENTS) {
|
|
105
|
+
// Only dwell time; key identity NOT stored.
|
|
106
|
+
this._keyEvents.push({ t, dwell });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ── snapshot ─────────────────────────────────────────────────────────────
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Returns a privacy-preserving statistical snapshot of collected bio signals.
|
|
114
|
+
* Raw events are summarised; nothing identifiable is included in the output.
|
|
115
|
+
*
|
|
116
|
+
* @param {number[]} computationTimings - entropy probe timing array
|
|
117
|
+
* @returns {BioSnapshot}
|
|
118
|
+
*/
|
|
119
|
+
snapshot(computationTimings = []) {
|
|
120
|
+
const now = performance.now();
|
|
121
|
+
const durationMs = this._startTime != null ? (now - this._startTime) : 0;
|
|
122
|
+
|
|
123
|
+
// ── Mouse statistics ────────────────────────────────────────────────
|
|
124
|
+
const iei = this._mouseEvents.map(e => e.dt);
|
|
125
|
+
const velocities = this._mouseEvents.map(e =>
|
|
126
|
+
e.dt > 0 ? Math.hypot(e.dx, e.dy) / e.dt : 0
|
|
127
|
+
);
|
|
128
|
+
const pressure = this._mouseEvents.map(e => e.pressure);
|
|
129
|
+
const angJerk = _computeAngularJerk(this._mouseEvents);
|
|
130
|
+
|
|
131
|
+
const mouseStats = {
|
|
132
|
+
sampleCount: iei.length,
|
|
133
|
+
ieiMean: _mean(iei),
|
|
134
|
+
ieiCV: _cv(iei),
|
|
135
|
+
velocityP50: _percentile(velocities, 50),
|
|
136
|
+
velocityP95: _percentile(velocities, 95),
|
|
137
|
+
angularJerkMean: _mean(angJerk),
|
|
138
|
+
pressureVariance: _variance(pressure),
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// ── Keyboard statistics ───────────────────────────────────────────────
|
|
142
|
+
const dwellTimes = this._keyEvents.filter(e => e.dwell != null).map(e => e.dwell);
|
|
143
|
+
const iki = [];
|
|
144
|
+
for (let i = 1; i < this._keyEvents.length; i++) {
|
|
145
|
+
iki.push(this._keyEvents[i].t - this._keyEvents[i - 1].t);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const keyStats = {
|
|
149
|
+
sampleCount: dwellTimes.length,
|
|
150
|
+
dwellMean: _mean(dwellTimes),
|
|
151
|
+
dwellCV: _cv(dwellTimes),
|
|
152
|
+
ikiMean: _mean(iki),
|
|
153
|
+
ikiCV: _cv(iki),
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// ── Interference Coefficient ──────────────────────────────────────────
|
|
157
|
+
// Cross-correlate input event density with computation timing deviations.
|
|
158
|
+
// A real human on real hardware creates measurable CPU-scheduling pressure
|
|
159
|
+
// that perturbs the entropy probe's timing.
|
|
160
|
+
const interferenceCoefficient = _computeInterference(
|
|
161
|
+
this._mouseEvents,
|
|
162
|
+
this._keyEvents,
|
|
163
|
+
computationTimings,
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
mouse: mouseStats,
|
|
168
|
+
keyboard: keyStats,
|
|
169
|
+
interferenceCoefficient,
|
|
170
|
+
durationMs,
|
|
171
|
+
hasActivity: iei.length > 5 || dwellTimes.length > 2,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @typedef {object} BioSnapshot
|
|
178
|
+
* @property {object} mouse
|
|
179
|
+
* @property {object} keyboard
|
|
180
|
+
* @property {number} interferenceCoefficient – [−1, 1]; higher = more human
|
|
181
|
+
* @property {number} durationMs
|
|
182
|
+
* @property {boolean} hasActivity
|
|
183
|
+
*/
|
|
184
|
+
|
|
185
|
+
// ---------------------------------------------------------------------------
|
|
186
|
+
// Statistical helpers (private)
|
|
187
|
+
// ---------------------------------------------------------------------------
|
|
188
|
+
|
|
189
|
+
function _mean(arr) {
|
|
190
|
+
if (!arr.length) return 0;
|
|
191
|
+
return arr.reduce((a, b) => a + b, 0) / arr.length;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function _variance(arr) {
|
|
195
|
+
if (arr.length < 2) return 0;
|
|
196
|
+
const m = _mean(arr);
|
|
197
|
+
return arr.reduce((s, v) => s + (v - m) ** 2, 0) / (arr.length - 1);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function _cv(arr) {
|
|
201
|
+
if (!arr.length) return 0;
|
|
202
|
+
const m = _mean(arr);
|
|
203
|
+
if (m === 0) return 0;
|
|
204
|
+
return Math.sqrt(_variance(arr)) / Math.abs(m);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function _percentile(sorted, p) {
|
|
208
|
+
const arr = [...sorted].sort((a, b) => a - b);
|
|
209
|
+
if (!arr.length) return 0;
|
|
210
|
+
const idx = (p / 100) * (arr.length - 1);
|
|
211
|
+
const lo = Math.floor(idx);
|
|
212
|
+
const hi = Math.ceil(idx);
|
|
213
|
+
return arr[lo] + (arr[hi] - arr[lo]) * (idx - lo);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Angular jerk: second derivative of movement direction (radians / s²) */
|
|
217
|
+
function _computeAngularJerk(events) {
|
|
218
|
+
if (events.length < 3) return [];
|
|
219
|
+
const angles = [];
|
|
220
|
+
for (let i = 0; i < events.length; i++) {
|
|
221
|
+
const { dx, dy } = events[i];
|
|
222
|
+
angles.push(Math.atan2(dy, dx));
|
|
223
|
+
}
|
|
224
|
+
const d1 = [];
|
|
225
|
+
for (let i = 1; i < angles.length; i++) {
|
|
226
|
+
const dt = events[i].dt || 1;
|
|
227
|
+
d1.push((angles[i] - angles[i - 1]) / dt);
|
|
228
|
+
}
|
|
229
|
+
const d2 = [];
|
|
230
|
+
for (let i = 1; i < d1.length; i++) {
|
|
231
|
+
const dt = events[i].dt || 1;
|
|
232
|
+
d2.push(Math.abs((d1[i] - d1[i - 1]) / dt));
|
|
233
|
+
}
|
|
234
|
+
return d2;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Interference Coefficient
|
|
239
|
+
*
|
|
240
|
+
* For each computation sample, check whether an input event occurred within
|
|
241
|
+
* ±16 ms (one animation frame). Build two parallel series:
|
|
242
|
+
* X[i] = 1 if input near sample i, else 0
|
|
243
|
+
* Y[i] = deviation of timing[i] from mean timing
|
|
244
|
+
* Return the Pearson correlation between X and Y.
|
|
245
|
+
* A real human on real hardware produces positive correlation (input events
|
|
246
|
+
* cause measurable CPU scheduling perturbations).
|
|
247
|
+
*/
|
|
248
|
+
function _computeInterference(mouseEvents, keyEvents, timings) {
|
|
249
|
+
if (!timings.length) return 0;
|
|
250
|
+
|
|
251
|
+
const allInputTimes = [
|
|
252
|
+
...mouseEvents.map(e => e.t),
|
|
253
|
+
...keyEvents.map(e => e.t),
|
|
254
|
+
].sort((a, b) => a - b);
|
|
255
|
+
|
|
256
|
+
if (!allInputTimes.length) return 0;
|
|
257
|
+
|
|
258
|
+
const WINDOW_MS = 16;
|
|
259
|
+
const meanTiming = _mean(timings);
|
|
260
|
+
|
|
261
|
+
// Note: timing alignment is approximate; probe start timestamp would improve accuracy
|
|
262
|
+
// We need absolute timestamps for the probe samples.
|
|
263
|
+
// We don't have them directly – use relative index spacing as a proxy.
|
|
264
|
+
// The entropy probe runs for ~(mean * n) ms starting at collectedAt.
|
|
265
|
+
// This is a statistical approximation; the exact alignment improves
|
|
266
|
+
// when callers pass `collectedAt` from the entropy result.
|
|
267
|
+
// For now we distribute samples evenly across the collection window.
|
|
268
|
+
const first = allInputTimes[0];
|
|
269
|
+
const last = allInputTimes[allInputTimes.length - 1];
|
|
270
|
+
const span = Math.max(last - first, 1);
|
|
271
|
+
|
|
272
|
+
const X = timings.map((_, i) => {
|
|
273
|
+
const tSample = first + (i / timings.length) * span;
|
|
274
|
+
return allInputTimes.some(t => Math.abs(t - tSample) < WINDOW_MS) ? 1 : 0;
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
const Y = timings.map(t => t - meanTiming);
|
|
278
|
+
|
|
279
|
+
return _pearson(X, Y);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function _pearson(X, Y) {
|
|
283
|
+
const n = X.length;
|
|
284
|
+
if (n < 2) return 0;
|
|
285
|
+
const mx = _mean(X);
|
|
286
|
+
const my = _mean(Y);
|
|
287
|
+
let num = 0, da = 0, db = 0;
|
|
288
|
+
for (let i = 0; i < n; i++) {
|
|
289
|
+
const a = X[i] - mx;
|
|
290
|
+
const b = Y[i] - my;
|
|
291
|
+
num += a * b;
|
|
292
|
+
da += a * a;
|
|
293
|
+
db += b * b;
|
|
294
|
+
}
|
|
295
|
+
const denom = Math.sqrt(da * db);
|
|
296
|
+
return denom < 1e-14 ? 0 : num / denom;
|
|
297
|
+
}
|