| author | Raphael Velt <raph.velt@gmail.com> |
| Thu, 06 Oct 2011 12:39:29 +0200 | |
| changeset 306 | 70c9688a1486 |
| parent 305 | tweetcast/client/lib/gimite-web-socket-js-55ae639/flash-src/third-party/com/hurlant/math/MontgomeryReduction.as@436a31d11f1d |
| permissions | -rwxr-xr-x |
|
305
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
1 |
package com.hurlant.math |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
2 |
{ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
3 |
use namespace bi_internal; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
4 |
/** |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
5 |
* Montgomery reduction |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
6 |
*/ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
7 |
internal class MontgomeryReduction implements IReduction |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
8 |
{ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
9 |
private var m:BigInteger; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
10 |
private var mp:int; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
11 |
private var mpl:int; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
12 |
private var mph:int; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
13 |
private var um:int; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
14 |
private var mt2:int; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
15 |
public function MontgomeryReduction(m:BigInteger) { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
16 |
this.m = m; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
17 |
mp = m.invDigit(); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
18 |
mpl = mp & 0x7fff; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
19 |
mph = mp>>15; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
20 |
um = (1<<(BigInteger.DB-15))-1; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
21 |
mt2 = 2*m.t; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
22 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
23 |
/** |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
24 |
* xR mod m |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
25 |
*/ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
26 |
public function convert(x:BigInteger):BigInteger { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
27 |
var r:BigInteger = new BigInteger; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
28 |
x.abs().dlShiftTo(m.t, r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
29 |
r.divRemTo(m, null, r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
30 |
if (x.s<0 && r.compareTo(BigInteger.ZERO)>0) { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
31 |
m.subTo(r,r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
32 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
33 |
return r; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
34 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
35 |
/** |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
36 |
* x/R mod m |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
37 |
*/ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
38 |
public function revert(x:BigInteger):BigInteger { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
39 |
var r:BigInteger = new BigInteger; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
40 |
x.copyTo(r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
41 |
reduce(r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
42 |
return r; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
43 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
44 |
/** |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
45 |
* x = x/R mod m (HAC 14.32) |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
46 |
*/ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
47 |
public function reduce(x:BigInteger):void { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
48 |
while (x.t<=mt2) { // pad x so am has enough room later |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
49 |
x.a[x.t++] = 0; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
50 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
51 |
for (var i:int=0; i<m.t; ++i) { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
52 |
// faster way of calculating u0 = x[i]*mp mod DV |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
53 |
var j:int = x.a[i]&0x7fff; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
54 |
var u0:int = (j*mpl+(((j*mph+(x.a[i]>>15)*mpl)&um)<<15))&BigInteger.DM; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
55 |
// use am to combine the multiply-shift-add into one call |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
56 |
j = i+m.t; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
57 |
x.a[j] += m.am(0, u0, x, i, 0, m.t); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
58 |
// propagate carry |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
59 |
while (x.a[j]>=BigInteger.DV) { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
60 |
x.a[j] -= BigInteger.DV; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
61 |
x.a[++j]++; |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
62 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
63 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
64 |
x.clamp(); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
65 |
x.drShiftTo(m.t, x); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
66 |
if (x.compareTo(m)>=0) { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
67 |
x.subTo(m,x); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
68 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
69 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
70 |
/** |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
71 |
* r = "x^2/R mod m"; x != r |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
72 |
*/ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
73 |
public function sqrTo(x:BigInteger, r:BigInteger):void { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
74 |
x.squareTo(r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
75 |
reduce(r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
76 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
77 |
/** |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
78 |
* r = "xy/R mod m"; x,y != r |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
79 |
*/ |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
80 |
public function mulTo(x:BigInteger, y:BigInteger, r:BigInteger):void { |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
81 |
x.multiplyTo(y,r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
82 |
reduce(r); |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
83 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
84 |
} |
|
436a31d11f1d
Ajout du streaming des tweets par WebSocket
Raphael Velt <raph.velt@gmail.com>
parents:
diff
changeset
|
85 |
} |