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