## Apache Zeta Components Manual :: File Source for gmp_library.php

## Source for file gmp_library.php

Documentation is available at gmp_library.php

1. <?php
2. /**
3. * File containing the ezcAuthenticationGmpLibrary class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @filesource
24. * @package Authentication
25. * @version //autogen//
26. */
28. /**
29. * Wrapper class for the PHP gmp extension.
30. *
31. * @package Authentication
32. * @version //autogen//
33. * @access private
34. */
35. class ezcAuthenticationGmpLibrary extends ezcAuthenticationBignumLibrary
36. {
37. /**
38. * Creates a new big number from $number in the base $base.
39. *
40. * The number $number can be integer or string.
41. *
42. * @param mixed $number The number from which to create the result
43. * @param int $base The base in which the result will be
44. * @return resource
45. */
46. public function init( $number, $base = 10 )
47. {
48. return gmp_init( $number, $base );
49. }
51. /**
52. * Adds two numbers.
53. *
54. * @param resource $a The first number
55. * @param resource $b The second number
56. * @return resource
57. */
58. public function add( $a, $b )
59. {
60. return gmp_add( $a, $b );
61. }
63. /**
64. * Substracts two numbers.
65. *
66. * @param resource $a The first number
67. * @param resource $b The second number
68. * @return resource
69. */
70. public function sub( $a, $b )
71. {
72. return gmp_sub( $a, $b );
73. }
75. /**
76. * Multiplies two numbers.
77. *
78. * @param resource $a The first number
79. * @param resource $b The second number
80. * @return resource
81. */
82. public function mul( $a, $b )
83. {
84. return gmp_mul( $a, $b );
85. }
87. /**
88. * Divides two numbers.
89. *
90. * @param resource $a The first number
91. * @param resource $b The second number
92. * @return resource
93. */
94. public function div( $a, $b )
95. {
96. return gmp_div_q( $a, $b );
97. }
99. /**
100. * Computes $base modulo $modulus.
101. *
102. * @param resource $base The number to apply modulo to
103. * @param resource $modulus The modulo value to be applied to $base
104. * @return resource
105. */
106. public function mod( $base, $modulus )
107. {
108. return gmp_mod( $base, $modulus );
109. }
111. /**
112. * Computes $base to the power of $exponent.
113. *
114. * Warning! The $exponent cannot be a GMP number resource! It must be an
115. * integer (or a string as it would be converted to an integer).
116. *
117. * @param resource $base The number to be exponentiated
118. * @param int $exponent The exponent to apply to $base
119. * @return resource
120. */
121. public function pow( $base, $exponent )
122. {
123. return gmp_pow( $base, $exponent );
124. }
126. /**
127. * Computes $base to the power of $exponent and then applies modulo $modulus.
128. *
129. * Warning! The $exponent cannot be a GMP number resource! It must be an
130. * integer or a string.
131. *
132. * @param resource $base The number to be exponentiated
133. * @param int $exponent The exponent to apply to $base
134. * @param resource $modulus The modulo value to be applied to the result
135. * @return resource
136. */
137. public function powmod( $base, $exponent, $modulus )
138. {
139. return gmp_powm( $base, $exponent, $modulus );
140. }
142. /**
143. * Computes the inverse of $number in modulo $modulus.
144. *
145. * @param resource $number The number for which to calculate the inverse
146. * @param resource $modulus The modulo value in which the inverse is calculated
147. * @return resource
148. */
149. public function invert( $number, $modulus )
150. {
151. return gmp_invert( $number, $modulus );
152. }
154. /**
155. * Finds the greatest common denominator of two numbers using the extended
156. * Euclidean algorithm.
157. *
158. * The returned array is ( a0, b0, gcd( a, b ) ), where
159. * a0 * a + b0 * b = gcd( a, b )
160. *
161. * @param resource $a The first number
162. * @param resource $b The second number
163. * @return array(resource)
164. */
165. public function gcd( $a, $b )
166. {
167. $result = gmp_gcdext( $a, $b );
168. return array( $result['s'], $result['t'], $result['g'] );
169. }
171. /**
172. * Compares two numbers.
173. *
174. * Returns an integer:
175. * - a positive value if $a > $b
176. * - zero if $a == $b
177. * - a negative value if $a < $b
178. *
179. * @param resource $a The first number
180. * @param resource $b The second number
181. * @return int
182. */
183. public function cmp( $a, $b )
184. {
185. return gmp_cmp( $a, $b );
186. }
188. /**
189. * Returns the string representation of number $a.
190. *
191. * @param resource $a The number to be represented as a string
192. * @return string
193. */
194. public function toString( $a )
195. {
196. return gmp_strval( $a );
197. }
198. }
199. ?>

Documentation generated by phpDocumentor 1.4.3