Advertisement
Skylighty

krypto-beta

Nov 24th, 2020
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.82 KB | None | 0 0
  1. import math
  2.  
  3. def euclides2(x,y):
  4.     temp = 0
  5.     coeff = 0
  6.     rest = -1
  7.     while rest != 0:
  8.         if (x > y):
  9.             rest = x % y
  10.             coeff = math.floor(x/y)
  11.             print(str(x) + ' = ' + str(coeff) + '*' + str(y) + ' + ' + str(rest) + ' -> ' +
  12.                   str(rest) + ' = ' + str(x) + ' - (' + str(y) + '*' + str(coeff) + ')')
  13.             x = y
  14.             y = rest
  15.         else:
  16.             rest = y % x
  17.             coeff = math.floor(y/x)
  18.             print(str(y) + ' = ' + str(coeff) + '*' + str(x) + ' + ' + str(rest) + ' -> ' +
  19.                   str(rest) + ' = ' + str(y) + ' - (' + str(x) + '*' + str(coeff) + ')')
  20.             y = x
  21.             x = rest
  22.  
  23.  
  24. # Rozklad na czynniki pierwsze
  25. # Zwraca w liscie, od gory
  26. def distribution(x):
  27.     ite = 2
  28.     dividers = []
  29.     while x > 1:
  30.         if x % ite == 0:
  31.             temp = x
  32.             x /= ite
  33.             dividers.append(ite)
  34.             print(str(temp) + '  |  ' + str(ite))
  35.             ite = 2
  36.         else:
  37.             ite += 1
  38.     return dividers
  39.  
  40.  
  41. # Strukturyzja do postaci slownika klucz-wartosc
  42. # Gdzie klucz to podstawa, a wartosc wykladnik (ilosc wystapien poszczegolnego dzielnika w rozkladzie)
  43. def structurize_divs(divers):
  44.     result = {}
  45.     for i in divers:
  46.         result[i] = divers.count(i)
  47.     return result
  48.  
  49.  
  50. # Funkcja Eulera (nie twierdzenie)
  51. def euler(x):
  52.     arr = distribution(x)
  53.     data = structurize_divs(arr)
  54.     result = 1
  55.     for base in data:
  56.         result *= pow(base, data[base] - 1) * (base - 1)
  57.         print(str(base)+'^('+str(data[base])+'-1)'+'*('+str(base)+'-1)', end=' * ')
  58.     print('\n')
  59.     print('Wynik wynosi : ' + str(result))
  60.  
  61.  
  62. def leftright(x, power, mod):
  63.     # Konwersja na binarke do listy
  64.     bins = [int(i) for i in list('{0:0b}'.format(power))]
  65.     it1 = 0
  66.     it2 = 1
  67.     it3 = len(bins)-1
  68.     z = 0
  69.     z = (pow(pow(x, bins[it1]), 2)) % mod
  70.     print('w' + str(it2) + ' = (' + str(x) + '^(n' + str(it3) + '))^2 mod' + str(mod) + ' = ' + str(z))
  71.     it1 += 1
  72.     it2 += 1
  73.     it3 -= 1
  74.     y = (z*pow(x, bins[it1])) % mod
  75.     print('w' + str(it2) + ' = ' + str(z) + '*' + str(x) + '^(n' + str(it3) + ') mod' + str(mod) + ' = ' + str(y))
  76.     it1 += 1
  77.     it2 += 1
  78.     it3 -= 1
  79.     print('\n')
  80.     while it1 < len(bins):
  81.         z = (pow(y, 2)) % mod
  82.         print('w' + str(it2) + ' = ' + str(y) + '^2 mod' + str(mod) + ' = ' + str(z))
  83.         it2 += 1
  84.         y = (z * pow(x, bins[it1])) % mod
  85.         print('w' + str(it2) + ' = ' + str(z) + '*' + str(x) + '^(n' + str(it3) + ') mod' + str(mod) + ' = ' + str(y))
  86.         print('\n')
  87.         it1 += 1
  88.         it2 += 1
  89.         it3 -= 1
  90.  
  91. def rightleft(x, power, mod):
  92.     # Konwersja na binarke do listy
  93.     bins = [int(i) for i in list('{0:0b}'.format(power))]
  94.     z = 0
  95.     y = 0
  96.     n = 0
  97.     i = 1
  98.     z = x % mod
  99.     print('z'+str(i)+' = '+str(x)+'^(2^0) mod'+str(mod)+' = '+str(z))
  100.     y = (pow(z, bins[n])) % mod
  101.     print('y'+str(i)+' = 1*'+str(x)+'^(n'+str(n)+') mod'+str(mod)+' = '+str(y)+'\n')
  102.     n += 1
  103.     i += 1
  104.     while n < len(bins):
  105.         tempz = z
  106.         tempy = y
  107.         z = (pow(z, 2)) % mod
  108.         print('z'+str(i)+' = '+str(tempz)+'^(2^0) mod'+str(mod)+' = '+str(z))
  109.         y = (y*pow(z, bins[n])) % mod
  110.         print('y' + str(i) + ' = ' + str(tempy) + '*' + str(x) + '^(n' + str(n) + ') mod' + str(mod) + ' = ' + str(y)+'\n')
  111.         n += 1
  112.         i += 1
  113.  
  114. rightleft(3,51,13)
  115. # Funkcje do uzycia:
  116. # NWD - euclides2(x,y) gdzie x,y to liczby dla ktorych liczone jest NWD
  117. # Funkcja Eulera - euler(x) gdzie x to liczba z ktorej jest funkcja, ostatnia gwiazdke w zapisie poteg ignoruj
  118. # Metoda z lewa na prawo - lefright(x, power, mod) gdzie x to liczba, power wykladnik, mod to wartosc modulo
  119. # Metoda z prawa na lewo - rightleft(x, power, mod) - tak samo jak z lewa na prawo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement