CVS: crypto/Demo chaum.py,NONE,1.1

"A.M. Kuchling" <[email protected]> Thu, 03 Apr 2003 12:26:03 -0800
Newsgroups gmane.comp.python.cryptography.cvs
Message-ID <[email protected]>
Update of /cvsroot/pycrypto/crypto/Demo
In directory sc8-pr-cvs1:/tmp/cvs-serv27561

Added Files:
	chaum.py 
Log Message:
Demo program for blinding

--- NEW FILE ---
#!/usr/bin/env python

import time, pprint, os, sha
from Crypto.PublicKey import *
from Crypto.Util.randpool import RandomPool
from Crypto.Util import number
import cPickle as pickle

pool = RandomPool()
pool.stir()

digest=sha.sha

KEYSIZE=1024
COINSIZE=256
HASHSIZE=sha.digestsize*8

DENOMINATIONPICKLE = 'denomination.pickle'

if os.path.isfile(DENOMINATIONPICKLE):
    print "Server: load denomination..."
    ff = file(DENOMINATIONPICKLE, 'r')
    key = pickle.load(ff)
    rsa = RSA.construct(key)
else:
    print "Server: create denomination (will take a while, only has to be done once for each denomiation of coin you want)..."
    begintime=time.time()
    rsa=RSA.generate(KEYSIZE, pool.get_bytes)
    endtime=time.time()
    print "Server: Generate %d bit RSA key: %f s" % (KEYSIZE, endtime-begintime)

    key = (rsa.n, rsa.e, rsa.d)
    ff = file(DENOMINATIONPICKLE, 'w')
    pickle.dump(key, ff)


print "n = %s" % pprint.pformat(rsa.n)
print "e = %s" % pprint.pformat(rsa.e)
print "d = %s" % pprint.pformat(rsa.d)
print

coinId = number.getRandomNumber(COINSIZE, pool.get_bytes)
while 1:
    blindingFactor = number.getRandomNumber(HASHSIZE, pool.get_bytes)
    if number.GCD(blindingFactor, rsa.n)==1:
        break
coinHash = number.bytes_to_long(digest(number.long_to_bytes(coinId)).digest())

print "coinId = %s" % pprint.pformat(coinId)
print "blindingFactor = %s" % pprint.pformat(blindingFactor)
print "coinHash = %s" % pprint.pformat(coinHash)
print

begintime=time.time()
blindedCoin=rsa.blind(coinHash, blindingFactor)
endtime=time.time()
print "Client: Blinding: %f s" % (endtime-begintime)
print "blindedCoin = %s" % pprint.pformat(blindedCoin)
print

begintime=time.time()
blindedSignature=rsa.sign(blindedCoin, None)[0]
endtime=time.time()
print "Server: Signing: %f s" % (endtime-begintime)
print "blindedSignature = %s" % pprint.pformat(blindedSignature)
print

begintime=time.time()
signature=rsa.unblind(blindedSignature, blindingFactor)
endtime=time.time()
print "Client: Unblinding: %f s" % (endtime-begintime)
print "signature = %s" % pprint.pformat(signature)
print


#rawSignature=rsa.sign(coinHash, None)[0]
#print "rawSignature = %s" % pprint.pformat(rawSignature)
#print

print "... user can now spend coin..."
print "... merchant deposits coin..."
print

begintime=time.time()
v=rsa.verify(coinHash, (signature,))
endtime=time.time()
print "Server: Verifying: %f s" % (endtime-begintime)

if v:
    print "OK"
else:
    print "NOT OK"





-------------------------------------------------------
This SF.net email is sponsored by: ValueWeb: 
Dedicated Hosting for just $79/mo with 500 GB of bandwidth! 
No other company gives more support or power for your dedicated server
http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/