Rev 846 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
827 | daniel-mar | 1 | <?php |
2 | |||
3 | /** |
||
4 | * Modular Exponentiation Engine |
||
5 | * |
||
6 | * PHP version 5 and 7 |
||
7 | * |
||
874 | daniel-mar | 8 | * @category Math |
9 | * @package BigInteger |
||
827 | daniel-mar | 10 | * @author Jim Wigginton <terrafrost@php.net> |
11 | * @copyright 2017 Jim Wigginton |
||
12 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
13 | * @link http://pear.php.net/package/Math_BigInteger |
||
14 | */ |
||
15 | |||
16 | namespace phpseclib3\Math\BigInteger\Engines\BCMath; |
||
17 | |||
18 | use phpseclib3\Math\BigInteger\Engines\BCMath; |
||
19 | |||
20 | /** |
||
21 | * Sliding Window Exponentiation Engine |
||
22 | * |
||
874 | daniel-mar | 23 | * @package PHP |
827 | daniel-mar | 24 | * @author Jim Wigginton <terrafrost@php.net> |
874 | daniel-mar | 25 | * @access public |
827 | daniel-mar | 26 | */ |
27 | abstract class Base extends BCMath |
||
28 | { |
||
29 | /** |
||
30 | * Cache constants |
||
31 | * |
||
32 | * $cache[self::VARIABLE] tells us whether or not the cached data is still valid. |
||
33 | * |
||
874 | daniel-mar | 34 | * @access private |
827 | daniel-mar | 35 | */ |
36 | const VARIABLE = 0; |
||
37 | /** |
||
38 | * $cache[self::DATA] contains the cached data. |
||
39 | * |
||
874 | daniel-mar | 40 | * @access private |
827 | daniel-mar | 41 | */ |
42 | const DATA = 1; |
||
43 | |||
44 | /** |
||
45 | * Test for engine validity |
||
46 | * |
||
47 | * @return bool |
||
48 | */ |
||
49 | public static function isValidEngine() |
||
50 | { |
||
51 | return static::class != __CLASS__; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Performs modular exponentiation. |
||
56 | * |
||
57 | * @param BCMath $x |
||
58 | * @param BCMath $e |
||
59 | * @param BCMath $n |
||
60 | * @param string $class |
||
61 | * @return BCMath |
||
62 | */ |
||
63 | protected static function powModHelper(BCMath $x, BCMath $e, BCMath $n, $class) |
||
64 | { |
||
65 | if (empty($e->value)) { |
||
66 | $temp = new $class(); |
||
67 | $temp->value = '1'; |
||
68 | return $x->normalize($temp); |
||
69 | } |
||
70 | |||
71 | return $x->normalize(static::slidingWindow($x, $e, $n, $class)); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Modular reduction preparation |
||
76 | * |
||
77 | * @param string $x |
||
78 | * @param string $n |
||
79 | * @param string $class |
||
80 | * @see self::slidingWindow() |
||
81 | * @return string |
||
82 | */ |
||
83 | protected static function prepareReduce($x, $n, $class) |
||
84 | { |
||
85 | return static::reduce($x, $n); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Modular multiply |
||
90 | * |
||
91 | * @param string $x |
||
92 | * @param string $y |
||
93 | * @param string $n |
||
94 | * @param string $class |
||
95 | * @see self::slidingWindow() |
||
96 | * @return string |
||
97 | */ |
||
98 | protected static function multiplyReduce($x, $y, $n, $class) |
||
99 | { |
||
100 | return static::reduce(bcmul($x, $y), $n); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Modular square |
||
105 | * |
||
106 | * @param string $x |
||
107 | * @param string $n |
||
108 | * @param string $class |
||
109 | * @see self::slidingWindow() |
||
110 | * @return string |
||
111 | */ |
||
112 | protected static function squareReduce($x, $n, $class) |
||
113 | { |
||
114 | return static::reduce(bcmul($x, $x), $n); |
||
115 | } |
||
116 | } |