Changeset 262

Show
Ignore:
Timestamp:
04/04/09 22:55:12 (3 years ago)
Author:
ocjhb
Message:

fast again, using small e

Location:
trunk/sandbox/jhb/oc2
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/sandbox/jhb/oc2/protocols.py

    r259 r262  
    7373        return answer 
    7474 
     75 
     76 
     77 
    7578class CoinsSpendSender(Protocol): 
    7679 
  • trunk/sandbox/jhb/oc2/rsa.py

    r260 r262  
    162162    return 0 
    163163 
    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  
    213164def getUnblinder(n): 
    214165    while 1: 
     
    226177    n = p * q 
    227178     
     179    e = 17  
    228180    while 1: 
    229         e = getRandomNumber(17,t-1)  #getRandomNumber, ungerade < (p-1)*(q-1), coprime(e,(p-1)*(q-1)), e.g. gcd == 1 
    230         e = e % 2 == 0 and e-1 or e     
    231181        if  gcd(e,t) == 1: 
    232             break             
     182            break 
     183        e +=2 
     184    
    233185    d = invMod(e, t) 
    234186    keys =  ( {'e': e, 'n': n}, {'d': d, 'n': n} ) 
     
    288240    a = 2 #Use 2 as a base for first iteration speedup, per HAC 
    289241    for count in range(iterations): 
    290         v = powMod(a, s, n) 
     242        v = pow(a, s, n) 
    291243        if v==1: 
    292244            continue 
     
    296248                return False 
    297249            else: 
    298                 v, i = powMod(v, 2, n), i+1 
     250                v, i = pow(v, 2, n), i+1 
    299251        a = getRandomNumber(2, n) 
    300252    return True 
     
    370322 
    371323 
    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 
    380327 
    381328# Do doctest if we're not imported 
    382329if __name__ == "__main__": 
    383330    import time 
    384     t = time.time() 
    385331    (pub,priv) =  gen_pubpriv_keys(1024) 
    386332    print '=' * 40 
    387333    times = [] 
     334    t = time.time() 
    388335    #blinding 
    389336    #message = 'f'*65 
     
    411358    print sum(times) - times[2] 
    412359 
    413     if 1: 
     360    if 0: 
    414361        #full 
    415362        t = time.time()