tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Event-timestamp-safe-resolution.html (1386B)


      1 <!DOCTYPE html>
      2 <script src="/resources/testharness.js"></script>
      3 <script src="/resources/testharnessreport.js"></script>
      4 <script type="text/javascript">
      5 'use strict';
      6 
      7 // Computes greatest common divisor of a and b using Euclid's algorithm
      8 function computeGCD(a, b) {
      9  if (!Number.isInteger(a) || !Number.isInteger(b)) {
     10    throw new Error('Parameters must be integer numbers');
     11  }
     12 
     13  var r;
     14  while (b != 0) {
     15    r = a % b;
     16    a = b;
     17    b = r;
     18  }
     19  return (a < 0) ? -a : a;
     20 }
     21 
     22 // Finds minimum resolution Δ given a set of samples which are known to be in the form of N*Δ.
     23 // We use GCD of all samples as a simple estimator.
     24 function estimateMinimumResolution(samples) {
     25  var gcd;
     26  for (const sample of samples) {
     27    gcd = gcd ? computeGCD(gcd, sample) : sample;
     28  }
     29 
     30  return gcd;
     31 }
     32 
     33 test(function() {
     34  const samples = [];
     35  for (var i = 0; i < 1e3; i++) {
     36    var deltaInMicroSeconds = 0;
     37    const e1 = new MouseEvent('test1');
     38    do {
     39      const e2 = new MouseEvent('test2');
     40      deltaInMicroSeconds = Math.round((e2.timeStamp - e1.timeStamp) * 1000);
     41    } while (deltaInMicroSeconds == 0) // only collect non-zero samples
     42 
     43    samples.push(deltaInMicroSeconds);
     44  }
     45 
     46  const minResolution = estimateMinimumResolution(samples);
     47  assert_greater_than_equal(minResolution, 5);
     48 }, 'Event timestamp should not have a resolution better than 5 microseconds');
     49 </script>