1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_5/Regress/regress-452008.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,153 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +//----------------------------------------------------------------------------- 1.10 +var BUGNUMBER = 452008; 1.11 +var summary = 'Bad math with JIT'; 1.12 +var actual = ''; 1.13 +var expect = ''; 1.14 + 1.15 + 1.16 +//----------------------------------------------------------------------------- 1.17 +test(); 1.18 +//----------------------------------------------------------------------------- 1.19 + 1.20 +function test() 1.21 +{ 1.22 + enterFunc ('test'); 1.23 + printBugNumber(BUGNUMBER); 1.24 + printStatus (summary); 1.25 + 1.26 + jit(true); 1.27 + 1.28 +// regression test for Bug 452008 - TM: SRP in Clipperz crypto library fails when JIT (TraceMonkey) is enabled. 1.29 + 1.30 + var x = [9385, 32112, 25383, 16317, 30138, 14565, 17812, 24500, 2719, 30174, 3546, 9096, 15352, 19120, 20648, 14334, 7426, 0, 0, 0]; 1.31 + var n = [27875, 25925, 30422, 12227, 27798, 32170, 10873, 21748, 30629, 26296, 20697, 5125, 4815, 2221, 14392, 23369, 5560, 2, 0, 0]; 1.32 + var np = 18229; 1.33 + var expected = [18770, 31456, 17999, 32635, 27508, 29131, 2856, 16233, 5439, 27580, 7093, 18192, 30804, 5472, 8529, 28649, 14852, 0, 0, 0]; 1.34 + 1.35 +//globals 1.36 + bpe=0; //bits stored per array element 1.37 + mask=0; //AND this with an array element to chop it down to bpe bits 1.38 + 1.39 +//initialize the global variables 1.40 + for (bpe=0; (1<<(bpe+1)) > (1<<bpe); bpe++); //bpe=number of bits in the mantissa on this platform 1.41 + bpe>>=1; //bpe=number of bits in one element of the array representing the bigInt 1.42 + mask=(1<<bpe)-1; //AND the mask with an integer to get its bpe least significant bits 1.43 + 1.44 + 1.45 +//the following global variables are scratchpad memory to 1.46 +//reduce dynamic memory allocation in the inner loop 1.47 + sa = new Array(0); //used in mont_() 1.48 + 1.49 +//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). 1.50 + function copy_(x,y) { 1.51 + var i; 1.52 + var k=x.length<y.length ? x.length : y.length; 1.53 + for (i=0;i<k;i++) 1.54 + x[i]=y[i]; 1.55 + for (i=k;i<x.length;i++) 1.56 + x[i]=0; 1.57 + } 1.58 + 1.59 +//do x=y on bigInt x and integer y. 1.60 + function copyInt_(x,n) { 1.61 + var i,c; 1.62 + for (c=n,i=0;i<x.length;i++) { 1.63 + x[i]=c & mask; 1.64 + c>>=bpe; 1.65 + } 1.66 + } 1.67 + 1.68 +//is x > y? (x and y both nonnegative) 1.69 + function greater(x,y) { 1.70 + var i; 1.71 + var k=(x.length<y.length) ? x.length : y.length; 1.72 + 1.73 + for (i=x.length;i<y.length;i++) 1.74 + if (y[i]) 1.75 + return 0; //y has more digits 1.76 + 1.77 + for (i=y.length;i<x.length;i++) 1.78 + if (x[i]) 1.79 + return 1; //x has more digits 1.80 + 1.81 + for (i=k-1;i>=0;i--) 1.82 + if (x[i]>y[i]) 1.83 + return 1; 1.84 + else if (x[i]<y[i]) 1.85 + return 0; 1.86 + return 0; 1.87 + } 1.88 + 1.89 + 1.90 +//do x=x*y*Ri mod n for bigInts x,y,n, 1.91 +// where Ri = 2**(-kn*bpe) mod n, and kn is the 1.92 +// number of elements in the n array, not 1.93 +// counting leading zeros. 1.94 +//x must be large enough to hold the answer. 1.95 +//It's OK if x and y are the same variable. 1.96 +//must have: 1.97 +// x,y < n 1.98 +// n is odd 1.99 +// np = -(n^(-1)) mod radix 1.100 + function mont_(x,y,n,np) { 1.101 + var i,j,c,ui,t; 1.102 + var kn=n.length; 1.103 + var ky=y.length; 1.104 + 1.105 + if (sa.length!=kn) 1.106 + sa=new Array(kn); 1.107 + 1.108 + for (;kn>0 && n[kn-1]==0;kn--); //ignore leading zeros of n 1.109 + for (;ky>0 && y[ky-1]==0;ky--); //ignore leading zeros of y 1.110 + 1.111 + copyInt_(sa,0); 1.112 + 1.113 + //the following loop consumes 95% of the runtime for randTruePrime_() and powMod_() for large keys 1.114 + for (i=0; i<kn; i++) { 1.115 + t=sa[0]+x[i]*y[0]; 1.116 + ui=((t & mask) * np) & mask; //the inner "& mask" is needed on Macintosh MSIE, but not windows MSIE 1.117 + c=(t+ui*n[0]) >> bpe; 1.118 + t=x[i]; 1.119 + 1.120 + //do sa=(sa+x[i]*y+ui*n)/b where b=2**bpe 1.121 + for (j=1;j<ky;j++) { 1.122 + c+=sa[j]+t*y[j]+ui*n[j]; 1.123 + sa[j-1]=c & mask; 1.124 + c>>=bpe; 1.125 + } 1.126 + for (;j<kn;j++) { 1.127 + c+=sa[j]+ui*n[j]; 1.128 + sa[j-1]=c & mask; 1.129 + c>>=bpe; 1.130 + } 1.131 + sa[j-1]=c & mask; 1.132 + } 1.133 + 1.134 + if (!greater(n,sa)) 1.135 + sub_(sa,n); 1.136 + copy_(x,sa); 1.137 + } 1.138 + 1.139 + mont_(x, x, n, np); 1.140 + 1.141 + var passed = expected.length == x.length; 1.142 + for (var i = 0; i < expected.length; i++) { 1.143 + if (passed) 1.144 + passed = expected[i] == x[i]; 1.145 + } 1.146 + print(passed); 1.147 + 1.148 + jit(false); 1.149 + 1.150 + expect = true; 1.151 + actual = passed; 1.152 + 1.153 + reportCompare(expect, actual, summary); 1.154 + 1.155 + exitFunc ('test'); 1.156 +}