コード例 #1
0
ファイル: rsa.py プロジェクト: kevinywlui/CryptoProject
def rsa(p,q,k):

  t = time()              # we start the clock here
  rngs = deque()          # store the rngs for testing

  n = p*q
  phi = (p-1) * (q-1)

  e = (p-1)               # this block of code choses e so that gcd(e,phi)=1
  while ( gcd(e,phi) != 1):
    e = randint(2, phi-1)
	
  x_0 = (p-1)             # this block of code choses x_0 so that gcd(x_0,phi)=1
  while ( gcd(e,phi) != 1):
    x_0 = randint(2, phi-1)


  z=0 

  for a in range(k):      # here we generate k values
    x_0 = pow(x_0,e,n)
    rngs.append(x_0%2)

  t=time()-t
  print('--------')
  print('{0} bits generated with RSA'.format(k))
  print('Generating time: {0}'.format(t))
  
  sarray = stest(rngs)
  sarray.insert(0,t)
  
  return sarray
コード例 #2
0
ファイル: bbs.py プロジェクト: kevinywlui/CryptoProject
def bbs(p,q,k):					#Blum Blum Shub Random Bit generator. Input two large primes 3mod4 and the number of bits in the output

  t = time()				      # start the clock
  rngs = deque()          # store the rngs for testing

  n = p*q
  phi = (p-1) * (q-1)			# phi is the number of elements in the multiplicative group of Z/nZ

  x_0 = (p-1)					    # x_0 is the seed
  while ( gcd(x_0,phi) != 1):					#this block of code chooses x_0 so that gcd(x_0,phi)=1
    x_0 = randint(2, phi-1)


  z=0					            # initialize z (the bit component)

  for a in range(k):      # generate sequence of k integers
    x_0 = (x_0*x_0) % n

    if (x_0 > n / 2):
      x_0 = n - x_0
    z = x_0 % 2
    rngs.append(z)



  t=time()-t
  print('--------')
  print('{0} bits generated with BBS'.format(k))
  print('Generating time: {0}'.format(t))
  
  sarray = stest(rngs)
  sarray.insert(0,t)
  
  return sarray
コード例 #3
0
ファイル: bm.py プロジェクト: kevinywlui/CryptoProject
def bm(p,k):


  t=time()					#start the clock
  rngs=deque()


  #find primitive root mod p and set to g
  phi=p-1
  pf=pfac(phi)    # pf is the array of prime factors of phi

  works = 0					#find a primitive root modp
  while works == 0:
    works = 1
    g=randint(2,p-2)
    for j in pf:
      if pow(g,int(phi/j),p)==1:
        works=0
        break

  x_0 = randint(2,p-1)					#x_0 is the seed

  
  for a in range(k):					#generate a sequence of k integers
    x_0=pow(g,x_0,p)
    if x_0 < float(p-1)/2:
      z=1					#z is the actual bit
    else:
      z=0
    rngs.append(z)
  
  
  t=time()-t
  print('--------')
  print('{0} bits generated with BM'.format(k))
  print('Generating time: {0}'.format(t))
  
  sarray = stest(rngs)
  sarray.insert(0,t)
  
  return sarray
コード例 #4
0
ファイル: dec.py プロジェクト: kevinywlui/CryptoProject
def dec(p,k):					#Dual Elliptic Curve Deterministic Random Bit Generator with k bits generated by the curve y^2 = x^3 + ax + b over Z/pZ where p is a prime 3mod4
  
  rngs = deque()
  t = time()				        	# start the clock
  n = 2	              				# number seed is multiplied by at each step
  

  a=0
  b=0
  while(4*pow(a,3)+27*pow(b,2)==0): #here we ensure the curve is nonsingular
    a=randint(1,p-1)
    b=randint(1,p-1)

  c=0					                # used to produce seed

  while(c!=1):                # (x,y) is the seed
    x = randint(0,p-1)
    c = pow(x^3 + a*x + b, int((p-1)/2), p)


  y = pow(x^3 + a*x + b, int((p+1)/4), p)

  z=0					                # initialize z (the bit component)
  

  for a in range(k):					# generate sequence of k points
    (x,y) = ecmult(x,y,n,a,b,p)
    z = (x + y) % 2
    rngs.append(z)
		
  t=time()-t
  print('--------')
  print('{0} bits generated with Dual E. Curve'.format(k))
  print('Generating time: {0}'.format(t))
  
  sarray = stest(rngs)
  sarray.insert(0,t)
  
  return sarray