Debian Codegraph @

rhino/testsrc/tests/js1_5/Regress/regress-452008.js

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