Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import datetime
- CONST_MENU = "Введите номер команды\n" \
- "1 - m-арный алгоритм\n" \
- "2 - Бинарный алгоритм\n" \
- "3 - Встроенные функции Sage\n" \
- "4 - Выход\n"
- def pre_calculations(m, P):
- P_arr = []
- P_arr.append(0 * P)
- P_arr.append(P)
- for i in range(2, 2 ** m):
- P_arr.append(i * P)
- return P_arr
- def slice_to_bits(number, bits):
- bitstring = "{0:b}".format(number)
- res = []
- while len(bitstring) % bits != 0:
- bitstring = '0' + bitstring
- i = 0
- while i < len(bitstring):
- j = i + bits
- part = bitstring[i:j]
- res.append(int(part, 2))
- i = j
- return res
- def m_alg(k, m, P, E, P_arr):
- Q_inf = E(0, 1, 0)
- fi_arr = slice_to_bits(k, m)
- for fi in fi_arr:
- Q_inf = 2 ** m * Q_inf
- if fi != 0:
- Q_inf = Q_inf + P_arr[fi]
- return Q_inf
- def bin_alg(P, k, E):
- t = int(ceil(log(k, 2)))
- Q_inf = E(0, 1, 0)
- binary_form_k = bin(k)[2::]
- for i in range(0, t):
- Q_inf = 2 * Q_inf
- if binary_form_k[i] == '1':
- Q_inf = Q_inf + P
- return Q_inf
- def show_sage_results(P, E, k):
- print("Sage: ")
- start_time = datetime.now()
- print("Умножение числа на точку: {} за время {}" . format(E(k* P), datetime.now() - start_time))
- def main():
- # Константы
- p = 115792089237316193816632940749697632394964361042935191217416087735021737577431
- A = 39911043712488959346023488649471543779004688285304668897256090422321915164205
- B = 26607362474992639564015659099647695852669792190203112598170726948214610109470
- xG = 1
- yG = 11226734598496010064741041559038474696573800958032815190522914592034679115175
- E = EllipticCurve(GF(p), [A, B])
- G = E(xG, yG)
- k = 222200022200022220002220002222
- # m задается произвольно
- m = 4
- while 1:
- user_choice = input(CONST_MENU)
- if not user_choice.isdigit():
- continue
- user_choice = int(user_choice)
- if user_choice == 1:
- all_time = None
- start_time = datetime.now()
- P_arr = pre_calculations(m, G)
- all_time = datetime_now() - start_time
- print("Время пред. расчетов: {}" . format(all_time))
- start_time = datetime.now()
- res = m_alg(k, m, G, E, P_arr)
- end_time = datetime.now() - start_time
- print("m-арный алгоритм: {} за время {}" . format(res, end_time ))
- print("Общее время работы: {}" . format(all_time + end_time))
- elif user_choice == 2:
- start_time = datetime.now()
- res = bin_alg(G, k, E)
- print("Своя реализация умножения точки на число: {} за время {}" . format(res, datetime.now() - start_time))
- elif user_choice == 3:
- show_sage_results(G, E, k)
- elif user_choice == 4:
- return
- pass
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement