tor-browser

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

regress-452008.js (4272B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 //-----------------------------------------------------------------------------
      7 var BUGNUMBER = 452008;
      8 var summary = 'Bad math with JIT';
      9 var actual = '';
     10 var expect = '';
     11 
     12 
     13 //-----------------------------------------------------------------------------
     14 test();
     15 //-----------------------------------------------------------------------------
     16 
     17 function test()
     18 {
     19  printBugNumber(BUGNUMBER);
     20  printStatus (summary);
     21 
     22 
     23 // regression test for Bug 452008 - TM: SRP in Clipperz crypto library fails when JIT (TraceMonkey) is enabled. 
     24 
     25  var x = [9385, 32112, 25383, 16317, 30138, 14565, 17812, 24500, 2719, 30174, 3546, 9096, 15352, 19120, 20648, 14334, 7426, 0, 0, 0];
     26  var n = [27875, 25925, 30422, 12227, 27798, 32170, 10873, 21748, 30629, 26296, 20697, 5125, 4815, 2221, 14392, 23369, 5560, 2, 0, 0];
     27  var np = 18229;
     28  var expected = [18770, 31456, 17999, 32635, 27508, 29131, 2856, 16233, 5439, 27580, 7093, 18192, 30804, 5472, 8529, 28649, 14852, 0, 0, 0];
     29 
     30 //globals
     31  bpe=0;         //bits stored per array element
     32  mask=0;        //AND this with an array element to chop it down to bpe bits
     33 
     34 //initialize the global variables
     35  for (bpe=0; (1<<(bpe+1)) > (1<<bpe); bpe++);  //bpe=number of bits in the mantissa on this platform
     36  bpe>>=1;                   //bpe=number of bits in one element of the array representing the bigInt
     37  mask=(1<<bpe)-1;           //AND the mask with an integer to get its bpe least significant bits
     38 
     39 
     40 //the following global variables are scratchpad memory to
     41 //reduce dynamic memory allocation in the inner loop
     42  sa = new Array(0); //used in mont_()
     43 
     44 //do x=y on bigInts x and y.  x must be an array at least as big as y (not counting the leading zeros in y).
     45  function copy_(x,y) {
     46    var i;
     47    var k=x.length<y.length ? x.length : y.length;
     48    for (i=0;i<k;i++)
     49      x[i]=y[i];
     50    for (i=k;i<x.length;i++)
     51      x[i]=0;
     52  }
     53 
     54 //do x=y on bigInt x and integer y.
     55  function copyInt_(x,n) {
     56    var i,c;
     57    for (c=n,i=0;i<x.length;i++) {
     58      x[i]=c & mask;
     59      c>>=bpe;
     60    }
     61  }
     62 
     63 //is x > y? (x and y both nonnegative)
     64  function greater(x,y) {
     65    var i;
     66    var k=(x.length<y.length) ? x.length : y.length;
     67 
     68    for (i=x.length;i<y.length;i++)
     69      if (y[i])
     70        return 0;  //y has more digits
     71 
     72    for (i=y.length;i<x.length;i++)
     73      if (x[i])
     74        return 1;  //x has more digits
     75 
     76    for (i=k-1;i>=0;i--)
     77      if (x[i]>y[i])
     78        return 1;
     79      else if (x[i]<y[i])
     80        return 0;
     81    return 0;
     82  }
     83 
     84 
     85 //do x=x*y*Ri mod n for bigInts x,y,n,
     86 //  where Ri = 2**(-kn*bpe) mod n, and kn is the
     87 //  number of elements in the n array, not
     88 //  counting leading zeros.
     89 //x must be large enough to hold the answer.
     90 //It's OK if x and y are the same variable.
     91 //must have:
     92 //  x,y < n
     93 //  n is odd
     94 //  np = -(n^(-1)) mod radix
     95  function mont_(x,y,n,np) {
     96    var i,j,c,ui,t;
     97    var kn=n.length;
     98    var ky=y.length;
     99 
    100    if (sa.length!=kn)
    101      sa=new Array(kn);
    102 
    103    for (;kn>0 && n[kn-1]==0;kn--); //ignore leading zeros of n
    104    for (;ky>0 && y[ky-1]==0;ky--); //ignore leading zeros of y
    105 
    106    copyInt_(sa,0);
    107 
    108    //the following loop consumes 95% of the runtime for randTruePrime_() and powMod_() for large keys
    109    for (i=0; i<kn; i++) {
    110      t=sa[0]+x[i]*y[0];
    111      ui=((t & mask) * np) & mask;  //the inner "& mask" is needed on Macintosh MSIE, but not windows MSIE
    112      c=(t+ui*n[0]) >> bpe;
    113      t=x[i];
    114 
    115      //do sa=(sa+x[i]*y+ui*n)/b   where b=2**bpe
    116      for (j=1;j<ky;j++) {
    117        c+=sa[j]+t*y[j]+ui*n[j];
    118        sa[j-1]=c & mask;
    119        c>>=bpe;
    120      }
    121      for (;j<kn;j++) {
    122        c+=sa[j]+ui*n[j];
    123        sa[j-1]=c & mask;
    124        c>>=bpe;
    125      }
    126      sa[j-1]=c & mask;
    127    }
    128 
    129    if (!greater(n,sa))
    130      sub_(sa,n);
    131    copy_(x,sa);
    132  }
    133 
    134  mont_(x, x, n, np);
    135 
    136  var passed = expected.length == x.length;
    137  for (var i = 0; i < expected.length; i++) {
    138    if (passed)
    139      passed = expected[i] == x[i];
    140  }
    141  print(passed);
    142 
    143 
    144  expect = true;
    145  actual = passed;
    146 
    147  reportCompare(expect, actual, summary);
    148 }