def check_if_point_belongs_to_curve(x, y, A, B, p): if effectivePower.effective_power( y, 2, p) == (effectivePower.effective_power(x, 3, p) + A * x + B) % p: return True else: return False
def generate_point_on_curve(A, B, p): x = random.randint(0, p - 1) f_x = (effectivePower.effective_power(x, 3, p) + A * x + B) % p while not checkIfRestSquared.check_if_rest_squared(f_x, p): x = random.randint(0, p - 1) f_x = (effectivePower.effective_power(x, 3, p) + A * x + B) % p y1, y2 = squareRootInBody.square_root_in_body(f_x, p) if y1 >= 0: return x, y1 else: return x, y2
def sum_points(x1: int, y1: int, x2: int, y2: int, A: int, B: int, p: int): if (x1, y1) == opposed_point(x2, y2, p): return None, None elif x1 == x2 and y1 == y2: fi = ((3 * effectivePower.effective_power(x1, 2, p) + A) * reverseElement.reverse_element(p, (2 * y1) % p)) % p x3 = (effectivePower.effective_power(fi, 2, p) - 2 * x1) % p return x3, (fi * (x1 - x3) - y1) % p else: fi = ((y2 - y1) * reverseElement.reverse_element(p, (x2 - x1) % p)) % p x3 = (effectivePower.effective_power(fi, 2, p) - x1 - x2) % p return x3, (fi * (x1 - x3) - y1) % p
def generate_elliptic_curve(p: int): A = random.randint(0, p - 1) # A = 239614427021073265587611886177902927263167863041565491257781227550405368115731464059190159 # test B = random.randint(0, p - 1) # B = 447169285435982716467332439542997876345372330045685811964291613238129105735899852114277221 # test delta = 4 * effectivePower.effective_power( A, 3, p) + 27 * effectivePower.effective_power(B, 2, p) # print(delta % p) # test while delta % p == 0: A = random.randint(0, p) B = random.randint(0, p) delta = 4 * effectivePower.effective_power(A, 3, p) + 27 * (B, 2, p) return A, B, p
def check_if_rest_squared(b: int, p: int): temp: int = effectivePower.effective_power(b, int((p - 1) // 2), p) if temp == 1: return True else: return False
# Zaimplementuj algorytm (funkcję) obliczania odwrotności w grupie Φ(n). # Wykorzystaj Rozszerzony Algorytm Euklidesa. # Dane:n∈N,b∈Φ(n) # Wynik:b−1∈Φ(n) print(reverseElement.reverse_element(7, 6)) # print(reverseElement.reverse_element(714755753874294038472209875197470925790136955101537013834219803636515470795970872969183311384954013071855887627551449115792103595652278095914694551617362804215913432499413006687751873350515301081416609629081600732876172535569616947567263322860053950652618941030441551668294058973216806874984428266145117014661441900618686656150850748874921422072983047618094573078768832033763010726583638946250429569256391196014173690393150623958597147391275931411716442455737215316898025573901638987527999732402901435650871708957843731545984056732362097484684219331952445765843679816824939076287321146124029513354217, # 586415287886005945256415364856673789361634830599529970369953159457371797880498621476680842078870442883884206736061308878578202481965828949483342035306607412727247731129489003722107176544339121891058304556107450641845517076214759832220732083103477708296062658308709910111341015680864128408722098158429211163983133076642956241007276594280756931533687913979779606736751482450230516647885181338331851752020358528750514584753773181404184942128231982246579080812899402275455505128052758140697346910131020791692235360755935496686256682912441710402954405191952597983847415248179038414684281386860605203275367)) # ------ task 3 ------- task 3 -------- task 3 ------ task 3 ------ # Zaimplementuj algorytm (funkcję) efektywnego potęgowania w zbiorze Z∗n. # Wykorzystaj algorytm iterowanego podnoszenia do kwadratu. # Dane:n, k∈N, b∈Z∗n # Wynik:bk∈Z∗n print(effectivePower.effective_power(5, 3, 8)) # ------ task 4 ------- task 4 -------- task 4 ------ task 4 ------ # Niech p będzie liczbą pierwszą. Zaimplementuj test (funkcję), # który sprawdza czy element zbioru Z∗p jest resztą kwadratową wZ∗p. # Wykorzystaj twierdzenie Eulera. # Dane: b∈Z∗p # Wynik: True jeśli b jest resztą kwadratową, False w przeciwnym wypadku. print(checkIfRestSquared.check_if_rest_squared(26, 13)) # ------ task 5 ------- task 5 -------- task 5 ------ task 5 ------ # Zaimplementuj algorytm (funkcję), który oblicza pierwiastek kwadratowy w ciele F∗p, # gdzie p≡3(mod 4) jest liczbą pierwszą. Wykorzystaj twierdzenie Eulera. # Dane: a∈F∗p, b jest resztą kwadratową F∗p # Wynik: a∈F∗p taki, że a^2=b
def fermat_test(x): return effectivePower.effective_power(2, x - 1, x) == 1