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