| 164 | | def powMod(base, power, modulus): |
| 165 | | nBitScan = 5 |
| 166 | | |
| 167 | | """ Return base**power mod modulus, using multi bit scanning |
| 168 | | with nBitScan bits at a time.""" |
| 169 | | |
| 170 | | #TREV - Added support for negative exponents |
| 171 | | negativeResult = False |
| 172 | | if (power < 0): |
| 173 | | power *= -1 |
| 174 | | negativeResult = True |
| 175 | | |
| 176 | | exp2 = 2**nBitScan |
| 177 | | mask = exp2 - 1 |
| 178 | | |
| 179 | | # Break power into a list of digits of nBitScan bits. |
| 180 | | # The list is recursive so easy to read in reverse direction. |
| 181 | | nibbles = None |
| 182 | | while power: |
| 183 | | nibbles = int(power & mask), nibbles |
| 184 | | power = power >> nBitScan |
| 185 | | |
| 186 | | # Make a table of powers of base up to 2**nBitScan - 1 |
| 187 | | lowPowers = [1] |
| 188 | | for i in xrange(1, exp2): |
| 189 | | lowPowers.append((lowPowers[i-1] * base) % modulus) |
| 190 | | |
| 191 | | # To exponentiate by the first nibble, look it up in the table |
| 192 | | nib, nibbles = nibbles |
| 193 | | prod = lowPowers[nib] |
| 194 | | |
| 195 | | # For the rest, square nBitScan times, then multiply by |
| 196 | | # base^nibble |
| 197 | | while nibbles: |
| 198 | | nib, nibbles = nibbles |
| 199 | | for i in xrange(nBitScan): |
| 200 | | prod = (prod * prod) % modulus |
| 201 | | if nib: prod = (prod * lowPowers[nib]) % modulus |
| 202 | | |
| 203 | | #TREV - Added support for negative exponents |
| 204 | | if negativeResult: |
| 205 | | prodInv = invMod(prod, modulus) |
| 206 | | #Check to make sure the inverse is correct |
| 207 | | if (prod * prodInv) % modulus != 1: |
| 208 | | raise AssertionError() |
| 209 | | return prodInv |
| 210 | | return prod |
| 211 | | |
| 212 | | |
| 372 | | fast_exponentiation = pow |
| 373 | | |
| 374 | | |
| 375 | | |
| 376 | | dummypub = {'e': 14113314172904951708574681727178419945334488710884393819171664976779680200797597414114771594818675165309793637334221026797603177014256605018822198106314722836374110842070715219892097224490412698863094421694900145536562274616319932131625829121465675029881660966735997768513498069724329875965098488010383588739L, 'n': 145150538976217551367176809671350273750494602624899590324368244897722593745479036259822375443517603452852324994768211967284921909083542545166771234362330874393991730925949129652583929144910470338348353919907753285823861580558682180771896411177636929426768872822501699256786375850666075860261984182867372954251L} |
| 377 | | |
| 378 | | |
| 379 | | dummypriv = {'d': 123696117147104176428005963321746675718965595231084565714315557894285965050935507764060028854105522904648614509261060002795161536846964129662796047291190706239685012795528994459464895585008580460376045227460501329077360873393533273710765442351670778999647836893911520692642555450158992572095458544095097599595L, 'n': 145150538976217551367176809671350273750494602624899590324368244897722593745479036259822375443517603452852324994768211967284921909083542545166771234362330874393991730925949129652583929144910470338348353919907753285823861580558682180771896411177636929426768872822501699256786375850666075860261984182867372954251L} |
| | 324 | |
| | 325 | (dummypub,dummypriv) = ({'e': 17, 'n': 119854184191709851267115469806947480444279686024088476366095898577590388788441098128656550419021335940507973237749080463551087748856480956399611369963199842509280458397835875930007780781743559815353663167178392850613614395643351736086803768428705593212269700447923498009295529617805433375506688910349264456281L}, {'d': 112803938062785742369049853935950569829910292728553860109266728073026248271473974709323812159078904414595739517881487495106906116570805606023163642318305713478430901878058050836432331457217407052608225335053649488684207226225731712680194298917913785665189224809172156144674031166473829983092121536846612402225L, 'n': 119854184191709851267115469806947480444279686024088476366095898577590388788441098128656550419021335940507973237749080463551087748856480956399611369963199842509280458397835875930007780781743559815353663167178392850613614395643351736086803768428705593212269700447923498009295529617805433375506688910349264456281L}) |
| | 326 | |