File size: 1,714 Bytes
93c7565
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// prover/latency.js
// Computes detection latency statistics for ProofBridge Liner.
//
// Assumes periodic polling with interval Δ.
// Tampering events occur uniformly within interval.
// Detection latency T ~ Uniform(0, Δ)
//
// Hazard function: λ(t) = 1/(Δ - t)
// Expected value: E[T] = Δ/2 + t_overhead
// 95th percentile: t_0.95 = 0.95 Δ

const POLL_INTERVAL_MS = 5 * 60 * 1000; // 5 minutes
const OVERHEAD_MS = 2000; // Estimated gateway latency + aggregation

/**
 * Computes expected detection latency.
 * @returns {number} Expected latency in milliseconds
 */
function expectedLatency() {
  return POLL_INTERVAL_MS / 2 + OVERHEAD_MS;
}

/**
 * Computes 95th percentile detection latency.
 * @returns {number} 95th percentile in milliseconds
 */
function percentile95Latency() {
  return 0.95 * POLL_INTERVAL_MS;
}

/**
 * Hazard function value at time t.
 * @param {number} t - Time in milliseconds from tampering event
 * @returns {number} Hazard rate (instantaneous detection probability per ms)
 */
function hazardFunction(t) {
  if (t >= POLL_INTERVAL_MS) return 0; // No detection after interval
  return 1 / (POLL_INTERVAL_MS - t);
}

/**
 * Generates latency statistics for dashboard or reports.
 * @returns {Object} Latency stats
 */
function getLatencyStats() {
  return {
    pollIntervalMs: POLL_INTERVAL_MS,
    overheadMs: OVERHEAD_MS,
    expectedMs: expectedLatency(),
    percentile95Ms: percentile95Latency(),
    expectedMinutes: (expectedLatency() / 60000).toFixed(2),
    percentile95Minutes: (percentile95Latency() / 60000).toFixed(2)
  };
}

module.exports = {
  POLL_INTERVAL_MS,
  OVERHEAD_MS,
  expectedLatency,
  percentile95Latency,
  hazardFunction,
  getLatencyStats
};