def test_residue(): assert n_order(2, 13) == 12 assert [n_order(a, 7) for a in range(1, 7)] == \ [1, 3, 6, 3, 6, 2] assert n_order(5, 17) == 16 assert n_order(17, 11) == n_order(6, 11) assert n_order(101, 119) == 6 assert is_primitive_root(2, 7) == False assert is_primitive_root(3, 8) == False assert is_primitive_root(11, 14) == False assert is_primitive_root(12, 17) == is_primitive_root(29, 17) assert is_quad_residue(3, 7) == False assert is_quad_residue(10, 13) == True assert is_quad_residue(12364, 139) == is_quad_residue(132, 139) assert is_quad_residue(207, 251) == True assert legendre_symbol(5, 11) == 1 assert legendre_symbol(25, 41) == 1 assert legendre_symbol(67, 101) == -1 assert legendre_symbol(0, 13) == 0 assert legendre_symbol(9, 3) == 0 raises(ValueError, 'legendre_symbol(2, 4)') assert jacobi_symbol(25, 41) == 1 assert jacobi_symbol(-23, 83) == -1 assert jacobi_symbol(3, 9) == 0 assert jacobi_symbol(42, 97) == -1 raises(ValueError, 'jacobi_symbol(3, 8)')
def get_alfa_beta(self, Num): if legendre_symbol(Num, self.P) == 1 and legendre_symbol(Num, self.Q) == 1: ALFAi = randrange(0, 2**self.LAM2) BETAi = randrange(0, 2**self.LAM2) return [ALFAi, BETAi] else: return False, False
def get_c(prime1, prime2): c = 0 for i in range(2, (prime1 if prime1 > prime2 else prime2) - 1): if (legendre_symbol(i, prime1) % 4 == (-prime1) % 4) and (legendre_symbol(i, prime2) % 4 == (-prime2) % 4): c = i break return c
def get_member_cert(self, Num): if legendre_symbol(Num, self.P) and legendre_symbol(Num, self.Q) == 1: Ei = randprime((2**self.GAMA1 - 2**self.GAMA2), (2**self.GAMA1 + 2**self.GAMA2)) INVE_Ei = igcdex(Ei, ((self.P - 1) * (self.Q - 1)))[0] % ((self.P - 1) * (self.Q - 1)) Ai = pow((Num * self.A0), INVE_Ei, self.N) return [Ai, Ei] else: return False, False
def cipolla(a: int, p: int) -> str: if legendre_symbol(a, p) == -1: return 'Немає розв*язку' elif legendre_symbol(a, p) == 1: for t in range(1, p-1): v = t ** 2 - a if legendre_symbol(v, p) == -1: break y = 1 t1, y1 = t, y for power in range(1, int((p+1)/2)): t1, y1 = (t1 * t + y1 * y * v) % p, (t1 * y + t * y1) % p return t1
def sqrt_mod(n, p): # вычисл кв корня по мод (алг Тонелли-Шэнкса) S, Q = find_two(p - 1) if S >= 2: # ищем произвольн квадратичн невычет z = 1 while legendre_symbol(z, p) != -1: z += 1 print("z = ", z) c = pow(z, Q, p) R = pow(n, (Q + 1) // 2, p) t = pow(n, Q, p) M = S while True: if t == 1: return R, p - R if t == 0: return 0 for i in range(1, M): t_new = pow(t, 2**i, p) if t_new == 1: b = pow(c, 2**(M - i - 1), p) R = R * b % p t = (t * ((b**2) % p)) % p c = b**2 % p M = i break
def sqrt_mod(a, p): #Алгоритм вычисления квадратного корня по модулю (Алгоритм Тонелли-Шэнкса) #p - 1 = 2**S*Q; x**2 = a (mod p) if p <= 3: raise Exception("p должно быть > 3!") S, Q = find_powers_two(p - 1) #print(f"S = {S}, Q = {Q}") #if S == 0: # raise Exception("p should be odd prime") if S == 1 and p % 4 == 3: R = pow(a, (p + 1) // 4, p) return R % p, p - R if S >= 2: #Ищем произвольный квадратичный невычет z = 1 while legendre_symbol(z, p) != -1: z += 1 # print(f"z = {z}") c = pow(z, Q, p) # print(f"c = {c}") R = pow(a, (Q + 1) // 2, p) # print(f"R = {R}") t = pow(a, Q, p) # print(f"t = {t}") M = S # print(f"M = {M}\n") while True: # print(f"t= {t}") # a = input() if t == 1: return R, p - R if t == 0: return 0 for i in range(1, M): tt = pow(t, 2**i, p) # print(f"tt = {tt}") # a = input() if tt == 1: # print(f"itter_____") b = pow(c, 2**(M - i - 1), p) # print(f"b = {b}") R = R * b % p # print(f"R = {R}") t = (t * ((b**2) % p)) % p # print(f"t = {t}") c = b**2 % p # print(f"c = {c}") M = i # print(f"M = {M}") break
def test_residue(): assert n_order(2, 13) == 12 assert [n_order(a, 7) for a in range(1, 7)] == \ [1, 3, 6, 3, 6, 2] assert n_order(5, 17) == 16 assert n_order(17, 11) == n_order(6, 11) assert n_order(101, 119) == 6 assert is_primitive_root(2, 7) == False assert is_primitive_root(3, 8) == False assert is_primitive_root(11, 14) == False assert is_primitive_root(12, 17) == is_primitive_root(29, 17) assert is_quad_residue(3, 7) == False assert is_quad_residue(10, 13) == True assert is_quad_residue(12364, 139) == is_quad_residue(12364 % 139, 139) assert is_quad_residue(207, 251) == True assert is_quad_residue(0, 1) == True assert is_quad_residue(1, 1) == True assert is_quad_residue(0, 2) == is_quad_residue(1, 2) == True assert is_quad_residue(1, 4) == True assert is_quad_residue(2, 27) == False assert [j for j in range(14) if is_quad_residue(j, 14)] == \ [0, 1, 2, 4, 7, 8, 9, 11] raises(ValueError, 'is_quad_residue(1.1, 2)') assert legendre_symbol(5, 11) == 1 assert legendre_symbol(25, 41) == 1 assert legendre_symbol(67, 101) == -1 assert legendre_symbol(0, 13) == 0 assert legendre_symbol(9, 3) == 0 raises(ValueError, 'legendre_symbol(2, 4)') assert jacobi_symbol(25, 41) == 1 assert jacobi_symbol(-23, 83) == -1 assert jacobi_symbol(3, 9) == 0 assert jacobi_symbol(42, 97) == -1 assert jacobi_symbol(3, 5) == -1 assert jacobi_symbol(7, 9) == 1 assert jacobi_symbol(0, 3) == 0 assert jacobi_symbol(0, 1) == 1 assert jacobi_symbol(2, 1) == 1 assert jacobi_symbol(1, 3) == 1 raises(ValueError, 'jacobi_symbol(3, 8)')
def test_residue(): assert n_order(2, 13) == 12 assert [n_order(a, 7) for a in range(1, 7)] == [1, 3, 6, 3, 6, 2] assert n_order(5, 17) == 16 assert n_order(17, 11) == n_order(6, 11) assert n_order(101, 119) == 6 assert is_primitive_root(2, 7) == False assert is_primitive_root(3, 8) == False assert is_primitive_root(11, 14) == False assert is_primitive_root(12, 17) == is_primitive_root(29, 17) assert is_quad_residue(3, 7) == False assert is_quad_residue(10, 13) == True assert is_quad_residue(12364, 139) == is_quad_residue(12364 % 139, 139) assert is_quad_residue(207, 251) == True assert is_quad_residue(0, 1) == True assert is_quad_residue(1, 1) == True assert is_quad_residue(0, 2) == is_quad_residue(1, 2) == True assert is_quad_residue(1, 4) == True assert is_quad_residue(2, 27) == False assert [j for j in range(14) if is_quad_residue(j, 14)] == [0, 1, 2, 4, 7, 8, 9, 11] raises(ValueError, "is_quad_residue(1.1, 2)") assert legendre_symbol(5, 11) == 1 assert legendre_symbol(25, 41) == 1 assert legendre_symbol(67, 101) == -1 assert legendre_symbol(0, 13) == 0 assert legendre_symbol(9, 3) == 0 raises(ValueError, "legendre_symbol(2, 4)") assert jacobi_symbol(25, 41) == 1 assert jacobi_symbol(-23, 83) == -1 assert jacobi_symbol(3, 9) == 0 assert jacobi_symbol(42, 97) == -1 assert jacobi_symbol(3, 5) == -1 assert jacobi_symbol(7, 9) == 1 assert jacobi_symbol(0, 3) == 0 assert jacobi_symbol(0, 1) == 1 assert jacobi_symbol(2, 1) == 1 assert jacobi_symbol(1, 3) == 1 raises(ValueError, "jacobi_symbol(3, 8)")
def num_to_point(a, b, p: int, m: int) -> Tuple['Point', int]: # m = text_to_num(text) k = p // (m + 1) # print("k =", k) x, y = 0, 0 for j in range(k + 1): x = m * k + j if legendre_symbol(x ** 3 + a * x + b, p) == 1: y = cipolla_y.cipolla(x ** 3 + a * x + b, p) break return Point(x, y, a, b), k
def random_point_in_curve(a, b, field): #Генерация случайно точки на эллиптической кривой if field != 3 and field != 2 and (4 * a**3 + 27 * b**2) == 0: raise Exception("Ограничение на a и b не выполнено!") x_cor = randint(1, field) f = x_cor**3 + a * x_cor + b while legendre_symbol(f, field) != 1: x_cor = randint(1, field) f = x_cor**3 + a * x_cor + b y1, y2 = sqrt_mod(f, field) return x_cor, y1
def test_residue(): assert n_order(2, 13) == 12 assert [n_order(a, 7) for a in range(1, 7)] == [1, 3, 6, 3, 6, 2] assert n_order(5, 17) == 16 assert n_order(17, 11) == n_order(6, 11) assert n_order(101, 119) == 6 assert is_primitive_root(2, 7) == False assert is_primitive_root(3, 8) == False assert is_primitive_root(11, 14) == False assert is_primitive_root(12, 17) == is_primitive_root(29, 17) assert is_quad_residue(3, 7) == False assert is_quad_residue(10, 13) == True assert is_quad_residue(12364, 139) == is_quad_residue(132, 139) assert is_quad_residue(207, 251) == True assert legendre_symbol(5, 11) == 1 assert legendre_symbol(25, 41) == 1 assert legendre_symbol(67, 101) == -1
def test_residue(): assert n_order(2, 13) == 12 assert [n_order(a,7) for a in range(1,7)]==\ [1,3,6,3,6,2] assert n_order(5, 17) == 16 assert n_order(17, 11) == n_order(6, 11) assert n_order(101, 119) == 6 assert is_primitive_root(2, 7) == False assert is_primitive_root(3, 8) == False assert is_primitive_root(11, 14) == False assert is_primitive_root(12, 17) == is_primitive_root(29, 17) assert is_quad_residue(3, 7) == False assert is_quad_residue(10, 13) == True assert is_quad_residue(12364, 139) == is_quad_residue(132, 139) assert is_quad_residue(207, 251) == True assert legendre_symbol(5, 11) == 1 assert legendre_symbol(25, 41) == 1 assert legendre_symbol(67, 101) == -1
def calculodeparametros(n): m = math.floor(math.sqrt(n)) B0 = math.floor( math.exp((1 / 2) * math.sqrt(math.log(n) * math.log(math.log(n))))) primos = [i for i in sieve.primerange(3, B0)] B = [-1, 2] b = ([legendre_symbol(16843009, i) for i in primos]) count = 0 for i in b: if i == 1: B.append(primos[count]) count += 1 M = B0**2 numerossuaves = len(B) + 1 S = range(m - M, m + M + 1) return S, B
def random_point(self): """ Generate random point from defined elliptic curve Алгоритм генерации случайной точки на эллиптической кривой """ x_cor = randint(1, self.char) f = x_cor**3 + self.A * x_cor + self.B while legendre_symbol(f, self.char) != 1: x_cor = randint(1, self.char) f = x_cor**3 + self.A * x_cor + self.B y1, y2 = sqrt_mod(f, self.char) return Point(x_cor, y1, self)
def check_QRn(self, Num): if legendre_symbol(Num, self.P) == 1 and legendre_symbol(Num, self.Q) == 1: return True else: return False
def test_residue(): assert n_order(2, 13) == 12 assert [n_order(a, 7) for a in range(1, 7)] == \ [1, 3, 6, 3, 6, 2] assert n_order(5, 17) == 16 assert n_order(17, 11) == n_order(6, 11) assert n_order(101, 119) == 6 assert n_order( 11, (10**50 + 151)**2 ) == 10000000000000000000000000000000000000000000000030100000000000000000000000000000000000000000000022650 raises(ValueError, lambda: n_order(6, 9)) assert is_primitive_root(2, 7) is False assert is_primitive_root(3, 8) is False assert is_primitive_root(11, 14) is False assert is_primitive_root(12, 17) == is_primitive_root(29, 17) raises(ValueError, lambda: is_primitive_root(3, 6)) for p in primerange(3, 100): it = _primitive_root_prime_iter(p) assert len(list(it)) == totient(totient(p)) assert primitive_root(97) == 5 assert primitive_root(97**2) == 5 assert primitive_root(40487) == 5 # note that primitive_root(40487) + 40487 = 40492 is a primitive root # of 40487**2, but it is not the smallest assert primitive_root(40487**2) == 10 assert primitive_root(82) == 7 p = 10**50 + 151 assert primitive_root(p) == 11 assert primitive_root(2 * p) == 11 assert primitive_root(p**2) == 11 raises(ValueError, lambda: primitive_root(-3)) assert is_quad_residue(3, 7) is False assert is_quad_residue(10, 13) is True assert is_quad_residue(12364, 139) == is_quad_residue(12364 % 139, 139) assert is_quad_residue(207, 251) is True assert is_quad_residue(0, 1) is True assert is_quad_residue(1, 1) is True assert is_quad_residue(0, 2) == is_quad_residue(1, 2) is True assert is_quad_residue(1, 4) is True assert is_quad_residue(2, 27) is False assert is_quad_residue(13122380800, 13604889600) is True assert [j for j in range(14) if is_quad_residue(j, 14)] == \ [0, 1, 2, 4, 7, 8, 9, 11] raises(ValueError, lambda: is_quad_residue(1.1, 2)) raises(ValueError, lambda: is_quad_residue(2, 0)) assert quadratic_residues(S.One) == [0] assert quadratic_residues(1) == [0] assert quadratic_residues(12) == [0, 1, 4, 9] assert quadratic_residues(12) == [0, 1, 4, 9] assert quadratic_residues(13) == [0, 1, 3, 4, 9, 10, 12] assert [len(quadratic_residues(i)) for i in range(1, 20)] == \ [1, 2, 2, 2, 3, 4, 4, 3, 4, 6, 6, 4, 7, 8, 6, 4, 9, 8, 10] assert list(sqrt_mod_iter(6, 2)) == [0] assert sqrt_mod(3, 13) == 4 assert sqrt_mod(3, -13) == 4 assert sqrt_mod(6, 23) == 11 assert sqrt_mod(345, 690) == 345 assert sqrt_mod(67, 101) == None assert sqrt_mod(1020, 104729) == None for p in range(3, 100): d = defaultdict(list) for i in range(p): d[pow(i, 2, p)].append(i) for i in range(1, p): it = sqrt_mod_iter(i, p) v = sqrt_mod(i, p, True) if v: v = sorted(v) assert d[i] == v else: assert not d[i] assert sqrt_mod(9, 27, True) == [3, 6, 12, 15, 21, 24] assert sqrt_mod(9, 81, True) == [3, 24, 30, 51, 57, 78] assert sqrt_mod(9, 3**5, True) == [3, 78, 84, 159, 165, 240] assert sqrt_mod(81, 3**4, True) == [0, 9, 18, 27, 36, 45, 54, 63, 72] assert sqrt_mod(81, 3**5, True) == [9, 18, 36, 45, 63, 72, 90, 99, 117,\ 126, 144, 153, 171, 180, 198, 207, 225, 234] assert sqrt_mod(81, 3**6, True) == [9, 72, 90, 153, 171, 234, 252, 315,\ 333, 396, 414, 477, 495, 558, 576, 639, 657, 720] assert sqrt_mod(81, 3**7, True) == [9, 234, 252, 477, 495, 720, 738, 963,\ 981, 1206, 1224, 1449, 1467, 1692, 1710, 1935, 1953, 2178] for a, p in [(26214400, 32768000000), (26214400, 16384000000), (262144, 1048576), (87169610025, 163443018796875), (22315420166400, 167365651248000000)]: assert pow(sqrt_mod(a, p), 2, p) == a n = 70 a, p = 5**2 * 3**n * 2**n, 5**6 * 3**(n + 1) * 2**(n + 2) it = sqrt_mod_iter(a, p) for i in range(10): assert pow(next(it), 2, p) == a a, p = 5**2 * 3**n * 2**n, 5**6 * 3**(n + 1) * 2**(n + 3) it = sqrt_mod_iter(a, p) for i in range(2): assert pow(next(it), 2, p) == a n = 100 a, p = 5**2 * 3**n * 2**n, 5**6 * 3**(n + 1) * 2**(n + 1) it = sqrt_mod_iter(a, p) for i in range(2): assert pow(next(it), 2, p) == a assert type(next(sqrt_mod_iter(9, 27))) is int assert type(next(sqrt_mod_iter(9, 27, ZZ))) is type(ZZ(1)) assert type(next(sqrt_mod_iter(1, 7, ZZ))) is type(ZZ(1)) assert is_nthpow_residue(2, 1, 5) #issue 10816 assert is_nthpow_residue(1, 0, 1) is False assert is_nthpow_residue(1, 0, 2) is True assert is_nthpow_residue(3, 0, 2) is False assert is_nthpow_residue(0, 1, 8) is True assert is_nthpow_residue(2, 3, 2) is True assert is_nthpow_residue(2, 3, 9) is False assert is_nthpow_residue(3, 5, 30) is True assert is_nthpow_residue(21, 11, 20) is True assert is_nthpow_residue(7, 10, 20) is False assert is_nthpow_residue(5, 10, 20) is True assert is_nthpow_residue(3, 10, 48) is False assert is_nthpow_residue(1, 10, 40) is True assert is_nthpow_residue(3, 10, 24) is False assert is_nthpow_residue(1, 10, 24) is True assert is_nthpow_residue(3, 10, 24) is False assert is_nthpow_residue(2, 10, 48) is False assert is_nthpow_residue(81, 3, 972) is False assert is_nthpow_residue(243, 5, 5103) is True assert is_nthpow_residue(243, 3, 1240029) is False assert is_nthpow_residue(36010, 8, 87382) is True assert is_nthpow_residue(28552, 6, 2218) is True assert is_nthpow_residue(92712, 9, 50026) is True x = set([pow(i, 56, 1024) for i in range(1024)]) assert set([a for a in range(1024) if is_nthpow_residue(a, 56, 1024)]) == x x = set([pow(i, 256, 2048) for i in range(2048)]) assert set([a for a in range(2048) if is_nthpow_residue(a, 256, 2048)]) == x x = set([pow(i, 11, 324000) for i in range(1000)]) assert [is_nthpow_residue(a, 11, 324000) for a in x] x = set([pow(i, 17, 22217575536) for i in range(1000)]) assert [is_nthpow_residue(a, 17, 22217575536) for a in x] assert is_nthpow_residue(676, 3, 5364) assert is_nthpow_residue(9, 12, 36) assert is_nthpow_residue(32, 10, 41) assert is_nthpow_residue(4, 2, 64) assert is_nthpow_residue(31, 4, 41) assert not is_nthpow_residue(2, 2, 5) assert is_nthpow_residue(8547, 12, 10007) assert nthroot_mod(29, 31, 74) == [45] assert nthroot_mod(1801, 11, 2663) == 44 for a, q, p in [(51922, 2, 203017), (43, 3, 109), (1801, 11, 2663), (26118163, 1303, 33333347), (1499, 7, 2663), (595, 6, 2663), (1714, 12, 2663), (28477, 9, 33343)]: r = nthroot_mod(a, q, p) assert pow(r, q, p) == a assert nthroot_mod(11, 3, 109) is None assert nthroot_mod(16, 5, 36, True) == [4, 22] assert nthroot_mod(9, 16, 36, True) == [3, 9, 15, 21, 27, 33] assert nthroot_mod(4, 3, 3249000) == [] assert nthroot_mod(36010, 8, 87382, True) == [40208, 47174] assert nthroot_mod(0, 12, 37, True) == [0] assert nthroot_mod(0, 7, 100, True) == [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] assert nthroot_mod(4, 4, 27, True) == [5, 22] assert nthroot_mod(4, 4, 121, True) == [19, 102] assert nthroot_mod(2, 3, 7, True) == [] for p in range(5, 100): qv = range(3, p, 4) for q in qv: d = defaultdict(list) for i in range(p): d[pow(i, q, p)].append(i) for a in range(1, p - 1): res = nthroot_mod(a, q, p, True) if d[a]: assert d[a] == res else: assert res == [] assert legendre_symbol(5, 11) == 1 assert legendre_symbol(25, 41) == 1 assert legendre_symbol(67, 101) == -1 assert legendre_symbol(0, 13) == 0 assert legendre_symbol(9, 3) == 0 raises(ValueError, lambda: legendre_symbol(2, 4)) assert jacobi_symbol(25, 41) == 1 assert jacobi_symbol(-23, 83) == -1 assert jacobi_symbol(3, 9) == 0 assert jacobi_symbol(42, 97) == -1 assert jacobi_symbol(3, 5) == -1 assert jacobi_symbol(7, 9) == 1 assert jacobi_symbol(0, 3) == 0 assert jacobi_symbol(0, 1) == 1 assert jacobi_symbol(2, 1) == 1 assert jacobi_symbol(1, 3) == 1 raises(ValueError, lambda: jacobi_symbol(3, 8)) assert mobius(13 * 7) == 1 assert mobius(1) == 1 assert mobius(13 * 7 * 5) == -1 assert mobius(13**2) == 0 raises(ValueError, lambda: mobius(-3)) p = Symbol('p', integer=True, positive=True, prime=True) x = Symbol('x', positive=True) i = Symbol('i', integer=True) assert mobius(p) == -1 raises(TypeError, lambda: mobius(x)) raises(ValueError, lambda: mobius(i)) assert _discrete_log_trial_mul(587, 2**7, 2) == 7 assert _discrete_log_trial_mul(941, 7**18, 7) == 18 assert _discrete_log_trial_mul(389, 3**81, 3) == 81 assert _discrete_log_trial_mul(191, 19**123, 19) == 123 assert _discrete_log_shanks_steps(442879, 7**2, 7) == 2 assert _discrete_log_shanks_steps(874323, 5**19, 5) == 19 assert _discrete_log_shanks_steps(6876342, 7**71, 7) == 71 assert _discrete_log_shanks_steps(2456747, 3**321, 3) == 321 assert _discrete_log_pollard_rho(6013199, 2**6, 2, rseed=0) == 6 assert _discrete_log_pollard_rho(6138719, 2**19, 2, rseed=0) == 19 assert _discrete_log_pollard_rho(36721943, 2**40, 2, rseed=0) == 40 assert _discrete_log_pollard_rho(24567899, 3**333, 3, rseed=0) == 333 raises(ValueError, lambda: _discrete_log_pollard_rho(11, 7, 31, rseed=0)) raises(ValueError, lambda: _discrete_log_pollard_rho(227, 3**7, 5, rseed=0)) assert _discrete_log_pohlig_hellman(98376431, 11**9, 11) == 9 assert _discrete_log_pohlig_hellman(78723213, 11**31, 11) == 31 assert _discrete_log_pohlig_hellman(32942478, 11**98, 11) == 98 assert _discrete_log_pohlig_hellman(14789363, 11**444, 11) == 444 assert discrete_log(587, 2**9, 2) == 9 assert discrete_log(2456747, 3**51, 3) == 51 assert discrete_log(32942478, 11**127, 11) == 127 assert discrete_log(432751500361, 7**324, 7) == 324 args = 5779, 3528, 6215 assert discrete_log(*args) == 687 assert discrete_log(*Tuple(*args)) == 687 assert quadratic_congruence(400, 85, 125, 1600) == [295, 615, 935, 1255, 1575] assert quadratic_congruence(3, 6, 5, 25) == [3, 20] assert quadratic_congruence(120, 80, 175, 500) == [] assert quadratic_congruence(15, 14, 7, 2) == [1] assert quadratic_congruence(8, 15, 7, 29) == [10, 28] assert quadratic_congruence(160, 200, 300, 461) == [144, 431] assert quadratic_congruence( 100000, 123456, 7415263, 48112959837082048697) == [30417843635344493501, 36001135160550533083] assert quadratic_congruence(65, 121, 72, 277) == [249, 252] assert quadratic_congruence(5, 10, 14, 2) == [0] assert quadratic_congruence(10, 17, 19, 2) == [1] assert quadratic_congruence(10, 14, 20, 2) == [0, 1] assert polynomial_congruence( 6 * x**5 + 10 * x**4 + 5 * x**3 + x**2 + x + 1, 972000) == [ 220999, 242999, 463999, 485999, 706999, 728999, 949999, 971999 ] assert polynomial_congruence(x**3 - 10 * x**2 + 12 * x - 82, 33075) == [30287] assert polynomial_congruence(x**2 + x + 47, 2401) == [785, 1615] assert polynomial_congruence(10 * x**2 + 14 * x + 20, 2) == [0, 1] assert polynomial_congruence(x**3 + 3, 16) == [5] assert polynomial_congruence(65 * x**2 + 121 * x + 72, 277) == [249, 252] assert polynomial_congruence(35 * x**3 - 6 * x**2 - 567 * x + 2308, 148225) == [86957, 111157, 122531, 146731] assert polynomial_congruence(x**16 - 9, 36) == [3, 9, 15, 21, 27, 33] assert polynomial_congruence(x**6 - 2 * x**5 - 35, 6125) == [3257] raises(ValueError, lambda: polynomial_congruence(x**x, 6125)) raises(ValueError, lambda: polynomial_congruence(x**i, 6125)) raises(ValueError, lambda: polynomial_congruence(0.1 * x**2 + 6, 100))
52: (111, 182), 53: (128, 944), 54: (153, 205), 55: (197, 23), 56: (199, 1), 57: (214, 792), 58: (256, 641), 59: (265, 83), 60: (291, 7), 61: (324, 111), 62: (326, 96), 63: (340, 92), } sec_N = 1021 sec_L = np.array([legendre_symbol(i, sec_N) for i in range(sec_N)]) sec_L[sec_L == -1] = 0 sec_L[0] = 0 sec_code_length = 100 def sec_b2ap(prn): w, p = b2ap_secondary_params[prn] W = np.array([sec_L[k] ^ sec_L[(k + w) % sec_N] for k in range(sec_N)]) c = np.array([W[(n + p - 1) % sec_N] for n in range(sec_code_length)]) return c secondary_codes = {}
def main(): start_time = time.time() isChoose = False while isChoose != True: print("Выбор простого числа\n") print("[1]. Вручную") print("[2]. Сгенерировать случайное\n") a = int(input("Выберите пункт: ")) if a == 1: p = int(input("Введите простой модуль: ")) isChoose = True elif a == 2: p = primegen.prime_gen(100) print(f"Сгенерировано простое число {p}\n") isChoose = True else: print("Введен не существующий пункт! \n") if p <= 3: print("p должен быть > 3") sys.exit() isSqr = False while isSqr != True: m = int( input("\nВведите число, из которого необходимо извлечь корень: ")) m = m % p if legendre_symbol(m, p) == 1: isSqr = True else: print( "Не выполнено условие разрешимости сравнения\nЧисло должно быть квадратичным вычетом по модулю p" ) print("\nЧисло: ", m) if p % 4 == 3: print("\np % 4 == 3") R = pow(m, (p + 1) // 4, p) y1 = R % p y2 = p - R elif p % 8 == 5: print("\np % 8 == 5") y1, y2 = p_5_mod_8(m, p) elif p % 48 == 41: print("\np % 48 == 41") y1, y2 = p_41_mod_48(m, p) else: print("\nИспользуем общий случай") y1, y2 = sqrt_mod(m, p) print("\nКорни:\n(1) {}\n\n(2) {}".format(y1, y2)) # проверка print("\nПроверка:\n(1) {}\n\n(2) {}".format(y1**2 % p, y2**2 % p)) print('\nЗатраченное время =', time.time() - start_time)
def lgr(a, p): return legendre_symbol(a, p)
def test_residue(): assert n_order(2, 13) == 12 assert [n_order(a, 7) for a in range(1, 7)] == \ [1, 3, 6, 3, 6, 2] assert n_order(5, 17) == 16 assert n_order(17, 11) == n_order(6, 11) assert n_order(101, 119) == 6 assert n_order(11, (10**50 + 151)**2) == 10000000000000000000000000000000000000000000000030100000000000000000000000000000000000000000000022650 raises(ValueError, lambda: n_order(6, 9)) assert is_primitive_root(2, 7) is False assert is_primitive_root(3, 8) is False assert is_primitive_root(11, 14) is False assert is_primitive_root(12, 17) == is_primitive_root(29, 17) raises(ValueError, lambda: is_primitive_root(3, 6)) assert [primitive_root(i) for i in range(2, 31)] == [1, 2, 3, 2, 5, 3, \ None, 2, 3, 2, None, 2, 3, None, None, 3, 5, 2, None, None, 7, 5, \ None, 2, 7, 2, None, 2, None] for p in primerange(3, 100): it = _primitive_root_prime_iter(p) assert len(list(it)) == totient(totient(p)) assert primitive_root(97) == 5 assert primitive_root(97**2) == 5 assert primitive_root(40487) == 5 # note that primitive_root(40487) + 40487 = 40492 is a primitive root # of 40487**2, but it is not the smallest assert primitive_root(40487**2) == 10 assert primitive_root(82) == 7 p = 10**50 + 151 assert primitive_root(p) == 11 assert primitive_root(2*p) == 11 assert primitive_root(p**2) == 11 raises(ValueError, lambda: primitive_root(-3)) assert is_quad_residue(3, 7) is False assert is_quad_residue(10, 13) is True assert is_quad_residue(12364, 139) == is_quad_residue(12364 % 139, 139) assert is_quad_residue(207, 251) is True assert is_quad_residue(0, 1) is True assert is_quad_residue(1, 1) is True assert is_quad_residue(0, 2) == is_quad_residue(1, 2) is True assert is_quad_residue(1, 4) is True assert is_quad_residue(2, 27) is False assert is_quad_residue(13122380800, 13604889600) is True assert [j for j in range(14) if is_quad_residue(j, 14)] == \ [0, 1, 2, 4, 7, 8, 9, 11] raises(ValueError, lambda: is_quad_residue(1.1, 2)) raises(ValueError, lambda: is_quad_residue(2, 0)) assert quadratic_residues(S.One) == [0] assert quadratic_residues(1) == [0] assert quadratic_residues(12) == [0, 1, 4, 9] assert quadratic_residues(12) == [0, 1, 4, 9] assert quadratic_residues(13) == [0, 1, 3, 4, 9, 10, 12] assert [len(quadratic_residues(i)) for i in range(1, 20)] == \ [1, 2, 2, 2, 3, 4, 4, 3, 4, 6, 6, 4, 7, 8, 6, 4, 9, 8, 10] assert list(sqrt_mod_iter(6, 2)) == [0] assert sqrt_mod(3, 13) == 4 assert sqrt_mod(3, -13) == 4 assert sqrt_mod(6, 23) == 11 assert sqrt_mod(345, 690) == 345 for p in range(3, 100): d = defaultdict(list) for i in range(p): d[pow(i, 2, p)].append(i) for i in range(1, p): it = sqrt_mod_iter(i, p) v = sqrt_mod(i, p, True) if v: v = sorted(v) assert d[i] == v else: assert not d[i] assert sqrt_mod(9, 27, True) == [3, 6, 12, 15, 21, 24] assert sqrt_mod(9, 81, True) == [3, 24, 30, 51, 57, 78] assert sqrt_mod(9, 3**5, True) == [3, 78, 84, 159, 165, 240] assert sqrt_mod(81, 3**4, True) == [0, 9, 18, 27, 36, 45, 54, 63, 72] assert sqrt_mod(81, 3**5, True) == [9, 18, 36, 45, 63, 72, 90, 99, 117,\ 126, 144, 153, 171, 180, 198, 207, 225, 234] assert sqrt_mod(81, 3**6, True) == [9, 72, 90, 153, 171, 234, 252, 315,\ 333, 396, 414, 477, 495, 558, 576, 639, 657, 720] assert sqrt_mod(81, 3**7, True) == [9, 234, 252, 477, 495, 720, 738, 963,\ 981, 1206, 1224, 1449, 1467, 1692, 1710, 1935, 1953, 2178] for a, p in [(26214400, 32768000000), (26214400, 16384000000), (262144, 1048576), (87169610025, 163443018796875), (22315420166400, 167365651248000000)]: assert pow(sqrt_mod(a, p), 2, p) == a n = 70 a, p = 5**2*3**n*2**n, 5**6*3**(n+1)*2**(n+2) it = sqrt_mod_iter(a, p) for i in range(10): assert pow(next(it), 2, p) == a a, p = 5**2*3**n*2**n, 5**6*3**(n+1)*2**(n+3) it = sqrt_mod_iter(a, p) for i in range(2): assert pow(next(it), 2, p) == a n = 100 a, p = 5**2*3**n*2**n, 5**6*3**(n+1)*2**(n+1) it = sqrt_mod_iter(a, p) for i in range(2): assert pow(next(it), 2, p) == a assert type(next(sqrt_mod_iter(9, 27))) is int assert type(next(sqrt_mod_iter(9, 27, ZZ))) is type(ZZ(1)) assert type(next(sqrt_mod_iter(1, 7, ZZ))) is type(ZZ(1)) assert is_nthpow_residue(2, 1, 5) #issue 10816 assert is_nthpow_residue(1, 0, 1) is False assert is_nthpow_residue(1, 0, 2) is True assert is_nthpow_residue(3, 0, 2) is False assert is_nthpow_residue(0, 1, 8) is True assert is_nthpow_residue(2, 3, 2) is False assert is_nthpow_residue(2, 3, 9) is False assert is_nthpow_residue(3, 5, 30) is True assert is_nthpow_residue(21, 11, 20) is True assert is_nthpow_residue(7, 10, 20) is False assert is_nthpow_residue(5, 10, 20) is True assert is_nthpow_residue(3, 10, 48) is False assert is_nthpow_residue(1, 10, 40) is True assert is_nthpow_residue(3, 10, 24) is False assert is_nthpow_residue(1, 10, 24) is True assert is_nthpow_residue(3, 10, 24) is False assert is_nthpow_residue(2, 10, 48) is False assert is_nthpow_residue(81, 3, 972) is False assert is_nthpow_residue(243, 5, 5103) is True assert is_nthpow_residue(243, 3, 1240029) is False x = set([pow(i, 56, 1024) for i in range(1024)]) assert set([a for a in range(1024) if is_nthpow_residue(a, 56, 1024)]) == x x = set([ pow(i, 256, 2048) for i in range(2048)]) assert set([a for a in range(2048) if is_nthpow_residue(a, 256, 2048)]) == x x = set([ pow(i, 11, 324000) for i in range(1000)]) assert [ is_nthpow_residue(a, 11, 324000) for a in x] x = set([ pow(i, 17, 22217575536) for i in range(1000)]) assert [ is_nthpow_residue(a, 17, 22217575536) for a in x] assert is_nthpow_residue(676, 3, 5364) assert is_nthpow_residue(9, 12, 36) assert is_nthpow_residue(32, 10, 41) assert is_nthpow_residue(4, 2, 64) assert is_nthpow_residue(31, 4, 41) assert not is_nthpow_residue(2, 2, 5) assert is_nthpow_residue(8547, 12, 10007) assert nthroot_mod(29, 31, 74) == 31 assert nthroot_mod(*Tuple(29, 31, 74)) == 31 assert nthroot_mod(1801, 11, 2663) == 44 for a, q, p in [(51922, 2, 203017), (43, 3, 109), (1801, 11, 2663), (26118163, 1303, 33333347), (1499, 7, 2663), (595, 6, 2663), (1714, 12, 2663), (28477, 9, 33343)]: r = nthroot_mod(a, q, p) assert pow(r, q, p) == a assert nthroot_mod(11, 3, 109) is None raises(NotImplementedError, lambda: nthroot_mod(16, 5, 36)) raises(NotImplementedError, lambda: nthroot_mod(9, 16, 36)) for p in primerange(5, 100): qv = range(3, p, 4) for q in qv: d = defaultdict(list) for i in range(p): d[pow(i, q, p)].append(i) for a in range(1, p - 1): res = nthroot_mod(a, q, p, True) if d[a]: assert d[a] == res else: assert res is None assert legendre_symbol(5, 11) == 1 assert legendre_symbol(25, 41) == 1 assert legendre_symbol(67, 101) == -1 assert legendre_symbol(0, 13) == 0 assert legendre_symbol(9, 3) == 0 raises(ValueError, lambda: legendre_symbol(2, 4)) assert jacobi_symbol(25, 41) == 1 assert jacobi_symbol(-23, 83) == -1 assert jacobi_symbol(3, 9) == 0 assert jacobi_symbol(42, 97) == -1 assert jacobi_symbol(3, 5) == -1 assert jacobi_symbol(7, 9) == 1 assert jacobi_symbol(0, 3) == 0 assert jacobi_symbol(0, 1) == 1 assert jacobi_symbol(2, 1) == 1 assert jacobi_symbol(1, 3) == 1 raises(ValueError, lambda: jacobi_symbol(3, 8)) assert mobius(13*7) == 1 assert mobius(1) == 1 assert mobius(13*7*5) == -1 assert mobius(13**2) == 0 raises(ValueError, lambda: mobius(-3)) p = Symbol('p', integer=True, positive=True, prime=True) x = Symbol('x', positive=True) i = Symbol('i', integer=True) assert mobius(p) == -1 raises(TypeError, lambda: mobius(x)) raises(ValueError, lambda: mobius(i)) assert _discrete_log_trial_mul(587, 2**7, 2) == 7 assert _discrete_log_trial_mul(941, 7**18, 7) == 18 assert _discrete_log_trial_mul(389, 3**81, 3) == 81 assert _discrete_log_trial_mul(191, 19**123, 19) == 123 assert _discrete_log_shanks_steps(442879, 7**2, 7) == 2 assert _discrete_log_shanks_steps(874323, 5**19, 5) == 19 assert _discrete_log_shanks_steps(6876342, 7**71, 7) == 71 assert _discrete_log_shanks_steps(2456747, 3**321, 3) == 321 assert _discrete_log_pollard_rho(6013199, 2**6, 2, rseed=0) == 6 assert _discrete_log_pollard_rho(6138719, 2**19, 2, rseed=0) == 19 assert _discrete_log_pollard_rho(36721943, 2**40, 2, rseed=0) == 40 assert _discrete_log_pollard_rho(24567899, 3**333, 3, rseed=0) == 333 raises(ValueError, lambda: _discrete_log_pollard_rho(11, 7, 31, rseed=0)) raises(ValueError, lambda: _discrete_log_pollard_rho(227, 3**7, 5, rseed=0)) assert _discrete_log_pohlig_hellman(98376431, 11**9, 11) == 9 assert _discrete_log_pohlig_hellman(78723213, 11**31, 11) == 31 assert _discrete_log_pohlig_hellman(32942478, 11**98, 11) == 98 assert _discrete_log_pohlig_hellman(14789363, 11**444, 11) == 444 assert discrete_log(587, 2**9, 2) == 9 assert discrete_log(2456747, 3**51, 3) == 51 assert discrete_log(32942478, 11**127, 11) == 127 assert discrete_log(432751500361, 7**324, 7) == 324 args = 5779, 3528, 6215 assert discrete_log(*args) == 687 assert discrete_log(*Tuple(*args)) == 687
def test_residue(): assert n_order(2, 13) == 12 assert [n_order(a, 7) for a in range(1, 7)] == \ [1, 3, 6, 3, 6, 2] assert n_order(5, 17) == 16 assert n_order(17, 11) == n_order(6, 11) assert n_order(101, 119) == 6 assert n_order( 11, (10**50 + 151)**2 ) == 10000000000000000000000000000000000000000000000030100000000000000000000000000000000000000000000022650 raises(ValueError, lambda: n_order(6, 9)) assert is_primitive_root(2, 7) is False assert is_primitive_root(3, 8) is False assert is_primitive_root(11, 14) is False assert is_primitive_root(12, 17) == is_primitive_root(29, 17) raises(ValueError, lambda: is_primitive_root(3, 6)) assert [primitive_root(i) for i in range(2, 31)] == [1, 2, 3, 2, 5, 3, \ None, 2, 3, 2, None, 2, 3, None, None, 3, 5, 2, None, None, 7, 5, \ None, 2, 7, 2, None, 2, None] for p in primerange(3, 100): it = _primitive_root_prime_iter(p) assert len(list(it)) == totient(totient(p)) assert primitive_root(97) == 5 assert primitive_root(97**2) == 5 assert primitive_root(40487) == 5 # note that primitive_root(40487) + 40487 = 40492 is a primitive root # of 40487**2, but it is not the smallest assert primitive_root(40487**2) == 10 assert primitive_root(82) == 7 p = 10**50 + 151 assert primitive_root(p) == 11 assert primitive_root(2 * p) == 11 assert primitive_root(p**2) == 11 raises(ValueError, lambda: primitive_root(-3)) assert is_quad_residue(3, 7) is False assert is_quad_residue(10, 13) is True assert is_quad_residue(12364, 139) == is_quad_residue(12364 % 139, 139) assert is_quad_residue(207, 251) is True assert is_quad_residue(0, 1) is True assert is_quad_residue(1, 1) is True assert is_quad_residue(0, 2) == is_quad_residue(1, 2) is True assert is_quad_residue(1, 4) is True assert is_quad_residue(2, 27) is False assert is_quad_residue(13122380800, 13604889600) is True assert [j for j in range(14) if is_quad_residue(j, 14)] == \ [0, 1, 2, 4, 7, 8, 9, 11] raises(ValueError, lambda: is_quad_residue(1.1, 2)) raises(ValueError, lambda: is_quad_residue(2, 0)) assert quadratic_residues(12) == [0, 1, 4, 9] assert quadratic_residues(13) == [0, 1, 3, 4, 9, 10, 12] assert [len(quadratic_residues(i)) for i in range(1, 20)] == \ [1, 2, 2, 2, 3, 4, 4, 3, 4, 6, 6, 4, 7, 8, 6, 4, 9, 8, 10] assert list(sqrt_mod_iter(6, 2)) == [0] assert sqrt_mod(3, 13) == 4 assert sqrt_mod(3, -13) == 4 assert sqrt_mod(6, 23) == 11 assert sqrt_mod(345, 690) == 345 for p in range(3, 100): d = defaultdict(list) for i in range(p): d[pow(i, 2, p)].append(i) for i in range(1, p): it = sqrt_mod_iter(i, p) v = sqrt_mod(i, p, True) if v: v = sorted(v) assert d[i] == v else: assert not d[i] assert sqrt_mod(9, 27, True) == [3, 6, 12, 15, 21, 24] assert sqrt_mod(9, 81, True) == [3, 24, 30, 51, 57, 78] assert sqrt_mod(9, 3**5, True) == [3, 78, 84, 159, 165, 240] assert sqrt_mod(81, 3**4, True) == [0, 9, 18, 27, 36, 45, 54, 63, 72] assert sqrt_mod(81, 3**5, True) == [9, 18, 36, 45, 63, 72, 90, 99, 117,\ 126, 144, 153, 171, 180, 198, 207, 225, 234] assert sqrt_mod(81, 3**6, True) == [9, 72, 90, 153, 171, 234, 252, 315,\ 333, 396, 414, 477, 495, 558, 576, 639, 657, 720] assert sqrt_mod(81, 3**7, True) == [9, 234, 252, 477, 495, 720, 738, 963,\ 981, 1206, 1224, 1449, 1467, 1692, 1710, 1935, 1953, 2178] for a, p in [(26214400, 32768000000), (26214400, 16384000000), (262144, 1048576), (87169610025, 163443018796875), (22315420166400, 167365651248000000)]: assert pow(sqrt_mod(a, p), 2, p) == a n = 70 a, p = 5**2 * 3**n * 2**n, 5**6 * 3**(n + 1) * 2**(n + 2) it = sqrt_mod_iter(a, p) for i in range(10): assert pow(next(it), 2, p) == a a, p = 5**2 * 3**n * 2**n, 5**6 * 3**(n + 1) * 2**(n + 3) it = sqrt_mod_iter(a, p) for i in range(2): assert pow(next(it), 2, p) == a n = 100 a, p = 5**2 * 3**n * 2**n, 5**6 * 3**(n + 1) * 2**(n + 1) it = sqrt_mod_iter(a, p) for i in range(2): assert pow(next(it), 2, p) == a assert type(next(sqrt_mod_iter(9, 27))) is int assert type(next(sqrt_mod_iter(9, 27, ZZ))) is type(ZZ(1)) assert type(next(sqrt_mod_iter(1, 7, ZZ))) is type(ZZ(1)) assert is_nthpow_residue(2, 1, 5) #issue 10816 assert is_nthpow_residue(1, 0, 1) is False assert is_nthpow_residue(1, 0, 2) is True assert is_nthpow_residue(3, 0, 2) is False assert is_nthpow_residue(0, 1, 8) is True assert is_nthpow_residue(2, 3, 2) is False assert is_nthpow_residue(2, 3, 9) is False assert is_nthpow_residue(3, 5, 30) is True assert is_nthpow_residue(21, 11, 20) is True assert is_nthpow_residue(7, 10, 20) is False assert is_nthpow_residue(5, 10, 20) is True assert is_nthpow_residue(3, 10, 48) is False assert is_nthpow_residue(1, 10, 40) is True assert is_nthpow_residue(3, 10, 24) is False assert is_nthpow_residue(1, 10, 24) is True assert is_nthpow_residue(3, 10, 24) is False assert is_nthpow_residue(2, 10, 48) is False assert is_nthpow_residue(81, 3, 972) is False assert is_nthpow_residue(243, 5, 5103) is True assert is_nthpow_residue(243, 3, 1240029) is False x = set([pow(i, 56, 1024) for i in range(1024)]) assert set([a for a in range(1024) if is_nthpow_residue(a, 56, 1024)]) == x x = set([pow(i, 256, 2048) for i in range(2048)]) assert set([a for a in range(2048) if is_nthpow_residue(a, 256, 2048)]) == x x = set([pow(i, 11, 324000) for i in range(1000)]) assert [is_nthpow_residue(a, 11, 324000) for a in x] x = set([pow(i, 17, 22217575536) for i in range(1000)]) assert [is_nthpow_residue(a, 17, 22217575536) for a in x] assert is_nthpow_residue(676, 3, 5364) assert is_nthpow_residue(9, 12, 36) assert is_nthpow_residue(32, 10, 41) assert is_nthpow_residue(4, 2, 64) assert is_nthpow_residue(31, 4, 41) assert not is_nthpow_residue(2, 2, 5) assert is_nthpow_residue(8547, 12, 10007) assert nthroot_mod(1801, 11, 2663) == 44 for a, q, p in [(51922, 2, 203017), (43, 3, 109), (1801, 11, 2663), (26118163, 1303, 33333347), (1499, 7, 2663), (595, 6, 2663), (1714, 12, 2663), (28477, 9, 33343)]: r = nthroot_mod(a, q, p) assert pow(r, q, p) == a assert nthroot_mod(11, 3, 109) is None raises(NotImplementedError, lambda: nthroot_mod(16, 5, 36)) raises(NotImplementedError, lambda: nthroot_mod(9, 16, 36)) for p in primerange(5, 100): qv = range(3, p, 4) for q in qv: d = defaultdict(list) for i in range(p): d[pow(i, q, p)].append(i) for a in range(1, p - 1): res = nthroot_mod(a, q, p, True) if d[a]: assert d[a] == res else: assert res is None assert legendre_symbol(5, 11) == 1 assert legendre_symbol(25, 41) == 1 assert legendre_symbol(67, 101) == -1 assert legendre_symbol(0, 13) == 0 assert legendre_symbol(9, 3) == 0 raises(ValueError, lambda: legendre_symbol(2, 4)) assert jacobi_symbol(25, 41) == 1 assert jacobi_symbol(-23, 83) == -1 assert jacobi_symbol(3, 9) == 0 assert jacobi_symbol(42, 97) == -1 assert jacobi_symbol(3, 5) == -1 assert jacobi_symbol(7, 9) == 1 assert jacobi_symbol(0, 3) == 0 assert jacobi_symbol(0, 1) == 1 assert jacobi_symbol(2, 1) == 1 assert jacobi_symbol(1, 3) == 1 raises(ValueError, lambda: jacobi_symbol(3, 8)) assert mobius(13 * 7) == 1 assert mobius(1) == 1 assert mobius(13 * 7 * 5) == -1 assert mobius(13**2) == 0 raises(ValueError, lambda: mobius(-3)) p = Symbol('p', integer=True, positive=True, prime=True) x = Symbol('x', positive=True) i = Symbol('i', integer=True) assert mobius(p) == -1 raises(TypeError, lambda: mobius(x)) raises(ValueError, lambda: mobius(i)) assert _discrete_log_trial_mul(587, 2**7, 2) == 7 assert _discrete_log_trial_mul(941, 7**18, 7) == 18 assert _discrete_log_trial_mul(389, 3**81, 3) == 81 assert _discrete_log_trial_mul(191, 19**123, 19) == 123 assert _discrete_log_shanks_steps(442879, 7**2, 7) == 2 assert _discrete_log_shanks_steps(874323, 5**19, 5) == 19 assert _discrete_log_shanks_steps(6876342, 7**71, 7) == 71 assert _discrete_log_shanks_steps(2456747, 3**321, 3) == 321 assert _discrete_log_pollard_rho(6013199, 2**6, 2, rseed=0) == 6 assert _discrete_log_pollard_rho(6138719, 2**19, 2, rseed=0) == 19 assert _discrete_log_pollard_rho(36721943, 2**40, 2, rseed=0) == 40 assert _discrete_log_pollard_rho(24567899, 3**333, 3, rseed=0) == 333 assert _discrete_log_pohlig_hellman(98376431, 11**9, 11) == 9 assert _discrete_log_pohlig_hellman(78723213, 11**31, 11) == 31 assert _discrete_log_pohlig_hellman(32942478, 11**98, 11) == 98 assert _discrete_log_pohlig_hellman(14789363, 11**444, 11) == 444 assert discrete_log(587, 2**9, 2) == 9 assert discrete_log(2456747, 3**51, 3) == 51 assert discrete_log(32942478, 11**127, 11) == 127 assert discrete_log(432751500361, 7**324, 7) == 324
def test_residue(): assert n_order(2, 13) == 12 assert [n_order(a, 7) for a in range(1, 7)] == \ [1, 3, 6, 3, 6, 2] assert n_order(5, 17) == 16 assert n_order(17, 11) == n_order(6, 11) assert n_order(101, 119) == 6 assert n_order(11, (10**50 + 151)**2) == 10000000000000000000000000000000000000000000000030100000000000000000000000000000000000000000000022650 raises(ValueError, lambda: n_order(6, 9)) assert is_primitive_root(2, 7) is False assert is_primitive_root(3, 8) is False assert is_primitive_root(11, 14) is False assert is_primitive_root(12, 17) == is_primitive_root(29, 17) raises(ValueError, lambda: is_primitive_root(3, 6)) assert [primitive_root(i) for i in range(2, 31)] == [1, 2, 3, 2, 5, 3, \ None, 2, 3, 2, None, 2, 3, None, None, 3, 5, 2, None, None, 7, 5, \ None, 2, 7, 2, None, 2, None] for p in primerange(3, 100): it = _primitive_root_prime_iter(p) assert len(list(it)) == totient(totient(p)) assert primitive_root(97) == 5 assert primitive_root(97**2) == 5 assert primitive_root(40487) == 5 # note that primitive_root(40487) + 40487 = 40492 is a primitive root # of 40487**2, but it is not the smallest assert primitive_root(40487**2) == 10 assert primitive_root(82) == 7 p = 10**50 + 151 assert primitive_root(p) == 11 assert primitive_root(2*p) == 11 assert primitive_root(p**2) == 11 raises(ValueError, lambda: primitive_root(-3)) assert is_quad_residue(3, 7) is False assert is_quad_residue(10, 13) is True assert is_quad_residue(12364, 139) == is_quad_residue(12364 % 139, 139) assert is_quad_residue(207, 251) is True assert is_quad_residue(0, 1) is True assert is_quad_residue(1, 1) is True assert is_quad_residue(0, 2) == is_quad_residue(1, 2) is True assert is_quad_residue(1, 4) is True assert is_quad_residue(2, 27) is False assert is_quad_residue(13122380800, 13604889600) is True assert [j for j in range(14) if is_quad_residue(j, 14)] == \ [0, 1, 2, 4, 7, 8, 9, 11] raises(ValueError, lambda: is_quad_residue(1.1, 2)) raises(ValueError, lambda: is_quad_residue(2, 0)) assert quadratic_residues(12) == [0, 1, 4, 9] assert quadratic_residues(13) == [0, 1, 3, 4, 9, 10, 12] assert [len(quadratic_residues(i)) for i in range(1, 20)] == \ [1, 2, 2, 2, 3, 4, 4, 3, 4, 6, 6, 4, 7, 8, 6, 4, 9, 8, 10] assert list(sqrt_mod_iter(6, 2)) == [0] assert sqrt_mod(3, 13) == 4 assert sqrt_mod(3, -13) == 4 assert sqrt_mod(6, 23) == 11 assert sqrt_mod(345, 690) == 345 for p in range(3, 100): d = defaultdict(list) for i in range(p): d[pow(i, 2, p)].append(i) for i in range(1, p): it = sqrt_mod_iter(i, p) v = sqrt_mod(i, p, True) if v: v = sorted(v) assert d[i] == v else: assert not d[i] assert sqrt_mod(9, 27, True) == [3, 6, 12, 15, 21, 24] assert sqrt_mod(9, 81, True) == [3, 24, 30, 51, 57, 78] assert sqrt_mod(9, 3**5, True) == [3, 78, 84, 159, 165, 240] assert sqrt_mod(81, 3**4, True) == [0, 9, 18, 27, 36, 45, 54, 63, 72] assert sqrt_mod(81, 3**5, True) == [9, 18, 36, 45, 63, 72, 90, 99, 117,\ 126, 144, 153, 171, 180, 198, 207, 225, 234] assert sqrt_mod(81, 3**6, True) == [9, 72, 90, 153, 171, 234, 252, 315,\ 333, 396, 414, 477, 495, 558, 576, 639, 657, 720] assert sqrt_mod(81, 3**7, True) == [9, 234, 252, 477, 495, 720, 738, 963,\ 981, 1206, 1224, 1449, 1467, 1692, 1710, 1935, 1953, 2178] for a, p in [(26214400, 32768000000), (26214400, 16384000000), (262144, 1048576), (87169610025, 163443018796875), (22315420166400, 167365651248000000)]: assert pow(sqrt_mod(a, p), 2, p) == a n = 70 a, p = 5**2*3**n*2**n, 5**6*3**(n+1)*2**(n+2) it = sqrt_mod_iter(a, p) for i in range(10): assert pow(next(it), 2, p) == a a, p = 5**2*3**n*2**n, 5**6*3**(n+1)*2**(n+3) it = sqrt_mod_iter(a, p) for i in range(2): assert pow(next(it), 2, p) == a n = 100 a, p = 5**2*3**n*2**n, 5**6*3**(n+1)*2**(n+1) it = sqrt_mod_iter(a, p) for i in range(2): assert pow(next(it), 2, p) == a assert type(next(sqrt_mod_iter(9, 27))) is int assert type(next(sqrt_mod_iter(9, 27, ZZ))) is type(ZZ(1)) assert type(next(sqrt_mod_iter(1, 7, ZZ))) is type(ZZ(1)) assert is_nthpow_residue(2, 1, 5) assert not is_nthpow_residue(2, 2, 5) assert is_nthpow_residue(8547, 12, 10007) assert nthroot_mod(1801, 11, 2663) == 44 for a, q, p in [(51922, 2, 203017), (43, 3, 109), (1801, 11, 2663), (26118163, 1303, 33333347), (1499, 7, 2663), (595, 6, 2663), (1714, 12, 2663), (28477, 9, 33343)]: r = nthroot_mod(a, q, p) assert pow(r, q, p) == a assert nthroot_mod(11, 3, 109) is None for p in primerange(5, 100): qv = range(3, p, 4) for q in qv: d = defaultdict(list) for i in range(p): d[pow(i, q, p)].append(i) for a in range(1, p - 1): res = nthroot_mod(a, q, p, True) if d[a]: assert d[a] == res else: assert res is None assert legendre_symbol(5, 11) == 1 assert legendre_symbol(25, 41) == 1 assert legendre_symbol(67, 101) == -1 assert legendre_symbol(0, 13) == 0 assert legendre_symbol(9, 3) == 0 raises(ValueError, lambda: legendre_symbol(2, 4)) assert jacobi_symbol(25, 41) == 1 assert jacobi_symbol(-23, 83) == -1 assert jacobi_symbol(3, 9) == 0 assert jacobi_symbol(42, 97) == -1 assert jacobi_symbol(3, 5) == -1 assert jacobi_symbol(7, 9) == 1 assert jacobi_symbol(0, 3) == 0 assert jacobi_symbol(0, 1) == 1 assert jacobi_symbol(2, 1) == 1 assert jacobi_symbol(1, 3) == 1 raises(ValueError, lambda: jacobi_symbol(3, 8)) assert mobius(13*7) == 1 assert mobius(1) == 1 assert mobius(13*7*5) == -1 assert mobius(13**2) == 0 raises(ValueError, lambda: mobius(-3)) p = Symbol('p', integer=True, positive=True, prime=True) x = Symbol('x', positive=True) i = Symbol('i', integer=True) assert mobius(p) == -1 raises(TypeError, lambda: mobius(x)) raises(ValueError, lambda: mobius(i))
56: (4197, 5), 57: (4096, 9937), 58: (3484, 430), 59: (3481, 5), 60: (3393, 355), 61: (3175, 909), 62: (2360, 1622), 63: (1852, 6284), 193: (4311, 9864), 194: (5024, 9753), 195: (4352, 9859), 199: (3646, 164), } N = 10223 L = np.array([legendre_symbol(i, N) for i in range(N)]) L[L == -1] = 0 L[0] = 0 def l1cp(prn): w, p = l1cp_params[prn] W = np.array([L[k] ^ L[(k + w) % N] for k in range(N)]) expansion = np.array([0, 1, 1, 0, 1, 0, 0]) c = np.concatenate((W[0:p - 1], expansion, W[p - 1:N])) return c codes = {}
def get_m(prime1, prime2, c): return (prime1 - legendre_symbol(c, prime1)) * ( prime2 - legendre_symbol(c, prime2)) // 4
def find_c(p, q): c = 2 while ((legendre_symbol(c, p)) % 4 != (-p) % 4) or ((legendre_symbol(c, q)) % 4 != (-q) % 4): c += 1 return c
def test_residue(): assert n_order(2, 13) == 12 assert [n_order(a, 7) for a in range(1, 7)] == \ [1, 3, 6, 3, 6, 2] assert n_order(5, 17) == 16 assert n_order(17, 11) == n_order(6, 11) assert n_order(101, 119) == 6 assert n_order( 11, (10**50 + 151)**2 ) == 10000000000000000000000000000000000000000000000030100000000000000000000000000000000000000000000022650 raises(ValueError, lambda: n_order(6, 9)) assert is_primitive_root(2, 7) is False assert is_primitive_root(3, 8) is False assert is_primitive_root(11, 14) is False assert is_primitive_root(12, 17) == is_primitive_root(29, 17) raises(ValueError, lambda: is_primitive_root(3, 6)) assert [primitive_root(i) for i in range(2, 31)] == [1, 2, 3, 2, 5, 3, \ None, 2, 3, 2, None, 2, 3, None, None, 3, 5, 2, None, None, 7, 5, \ None, 2, 7, 2, None, 2, None] for p in primerange(3, 100): it = _primitive_root_prime_iter(p) assert len(list(it)) == totient(totient(p)) assert primitive_root(97) == 5 assert primitive_root(97**2) == 5 assert primitive_root(40487) == 5 # note that primitive_root(40487) + 40487 = 40492 is a primitive root # of 40487**2, but it is not the smallest assert primitive_root(40487**2) == 10 assert primitive_root(82) == 7 p = 10**50 + 151 assert primitive_root(p) == 11 assert primitive_root(2 * p) == 11 assert primitive_root(p**2) == 11 raises(ValueError, lambda: primitive_root(-3)) assert is_quad_residue(3, 7) is False assert is_quad_residue(10, 13) is True assert is_quad_residue(12364, 139) == is_quad_residue(12364 % 139, 139) assert is_quad_residue(207, 251) is True assert is_quad_residue(0, 1) is True assert is_quad_residue(1, 1) is True assert is_quad_residue(0, 2) == is_quad_residue(1, 2) is True assert is_quad_residue(1, 4) is True assert is_quad_residue(2, 27) is False assert is_quad_residue(13122380800, 13604889600) is True assert [j for j in range(14) if is_quad_residue(j, 14)] == \ [0, 1, 2, 4, 7, 8, 9, 11] raises(ValueError, lambda: is_quad_residue(1.1, 2)) raises(ValueError, lambda: is_quad_residue(2, 0)) assert quadratic_residues(12) == [0, 1, 4, 9] assert quadratic_residues(13) == [0, 1, 3, 4, 9, 10, 12] assert [len(quadratic_residues(i)) for i in range(1, 20)] == \ [1, 2, 2, 2, 3, 4, 4, 3, 4, 6, 6, 4, 7, 8, 6, 4, 9, 8, 10] assert list(sqrt_mod_iter(6, 2)) == [0] assert sqrt_mod(3, 13) == 4 assert sqrt_mod(3, -13) == 4 assert sqrt_mod(6, 23) == 11 assert sqrt_mod(345, 690) == 345 for p in range(3, 100): d = defaultdict(list) for i in range(p): d[pow(i, 2, p)].append(i) for i in range(1, p): it = sqrt_mod_iter(i, p) v = sqrt_mod(i, p, True) if v: v = sorted(v) assert d[i] == v else: assert not d[i] assert sqrt_mod(9, 27, True) == [3, 6, 12, 15, 21, 24] assert sqrt_mod(9, 81, True) == [3, 24, 30, 51, 57, 78] assert sqrt_mod(9, 3**5, True) == [3, 78, 84, 159, 165, 240] assert sqrt_mod(81, 3**4, True) == [0, 9, 18, 27, 36, 45, 54, 63, 72] assert sqrt_mod(81, 3**5, True) == [9, 18, 36, 45, 63, 72, 90, 99, 117,\ 126, 144, 153, 171, 180, 198, 207, 225, 234] assert sqrt_mod(81, 3**6, True) == [9, 72, 90, 153, 171, 234, 252, 315,\ 333, 396, 414, 477, 495, 558, 576, 639, 657, 720] assert sqrt_mod(81, 3**7, True) == [9, 234, 252, 477, 495, 720, 738, 963,\ 981, 1206, 1224, 1449, 1467, 1692, 1710, 1935, 1953, 2178] for a, p in [(26214400, 32768000000), (26214400, 16384000000), (262144, 1048576), (87169610025, 163443018796875), (22315420166400, 167365651248000000)]: assert pow(sqrt_mod(a, p), 2, p) == a n = 70 a, p = 5**2 * 3**n * 2**n, 5**6 * 3**(n + 1) * 2**(n + 2) it = sqrt_mod_iter(a, p) for i in range(10): assert pow(next(it), 2, p) == a a, p = 5**2 * 3**n * 2**n, 5**6 * 3**(n + 1) * 2**(n + 3) it = sqrt_mod_iter(a, p) for i in range(2): assert pow(next(it), 2, p) == a n = 100 a, p = 5**2 * 3**n * 2**n, 5**6 * 3**(n + 1) * 2**(n + 1) it = sqrt_mod_iter(a, p) for i in range(2): assert pow(next(it), 2, p) == a assert type(next(sqrt_mod_iter(9, 27))) is int assert type(next(sqrt_mod_iter(9, 27, ZZ))) is type(ZZ(1)) assert type(next(sqrt_mod_iter(1, 7, ZZ))) is type(ZZ(1)) assert is_nthpow_residue(2, 1, 5) assert not is_nthpow_residue(2, 2, 5) assert is_nthpow_residue(8547, 12, 10007) assert nthroot_mod(1801, 11, 2663) == 44 for a, q, p in [(51922, 2, 203017), (43, 3, 109), (1801, 11, 2663), (26118163, 1303, 33333347), (1499, 7, 2663), (595, 6, 2663), (1714, 12, 2663), (28477, 9, 33343)]: r = nthroot_mod(a, q, p) assert pow(r, q, p) == a assert nthroot_mod(11, 3, 109) is None for p in primerange(5, 100): qv = range(3, p, 4) for q in qv: d = defaultdict(list) for i in range(p): d[pow(i, q, p)].append(i) for a in range(1, p - 1): res = nthroot_mod(a, q, p, True) if d[a]: assert d[a] == res else: assert res is None assert legendre_symbol(5, 11) == 1 assert legendre_symbol(25, 41) == 1 assert legendre_symbol(67, 101) == -1 assert legendre_symbol(0, 13) == 0 assert legendre_symbol(9, 3) == 0 raises(ValueError, lambda: legendre_symbol(2, 4)) assert jacobi_symbol(25, 41) == 1 assert jacobi_symbol(-23, 83) == -1 assert jacobi_symbol(3, 9) == 0 assert jacobi_symbol(42, 97) == -1 assert jacobi_symbol(3, 5) == -1 assert jacobi_symbol(7, 9) == 1 assert jacobi_symbol(0, 3) == 0 assert jacobi_symbol(0, 1) == 1 assert jacobi_symbol(2, 1) == 1 assert jacobi_symbol(1, 3) == 1 raises(ValueError, lambda: jacobi_symbol(3, 8)) assert mobius(13 * 7) == 1 assert mobius(1) == 1 assert mobius(13 * 7 * 5) == -1 assert mobius(13**2) == 0 raises(ValueError, lambda: mobius(-3)) p = Symbol('p', integer=True, positive=True, prime=True) x = Symbol('x', positive=True) i = Symbol('i', integer=True) assert mobius(p) == -1 raises(TypeError, lambda: mobius(x)) raises(ValueError, lambda: mobius(i))
N = p * q print("\nN = ", N) c = find_c(p, q) print("\nc = ", c) #kor_cp = modular_sqrt(c,p) #kor_cq = modular_sqrt(c,q) #kor_c = kor_cp*kor_cq%N kor_c = int(c**(1 / 2)) print(kor_c) s = find_s(N, c) print("\ns = ", s) leg_p = legendre_symbol(c, p) leg_q = legendre_symbol(c, q) w = (p - leg_p) * (q - leg_q) * exgcd(4, N) w = w % N print("\nw = ", w) d = find_d(w) #d=16 print("\nЗакрытый ключ d = ", d) e = (((w + 1) // 2) * exgcd(d, w)) % w #e=23 print("\ne = ", e) #шифрование