def blum_blum_shub(x,p,q): """ Blum Blum Shub Args: x -- seed value, coprime to pq, and greater than 1 p -- prime congruent to 3 mod 4 q -- prime congruent to 3 mod 4 """ m = p*q require_integers(["x","p","q"], [x,p,q]) require_prime( ["p","q"], [p,q]) require_true(["p","q"], [p,q], lambda x: x % 4 == 3, "must be congruent to 3 mod 4") require_geq(["x"], [x], 2) K = legendre_symbol(2,(p-1)//2) + legendre_symbol(2,(q-1)//2) if K == 2: raise Exception("both (p-1)/2 and (q-1)/2 have 2 as a quadratic residue") if gcd(m,x) != 1: raise Exception("x must be coprime to pq") while True: yield x x = (x*x)%m
def kronecker_symbol(a,n): """Extend the Legendre Symbol to all naturals""" fac = prime_factorization(n) out = 1 for f in fac: out *= legendre_symbol(a,f) return out
def MsgToPoint(m: int): for i in range(0, N): x = 100 * m + i z = x**3 z %= MODULO_P z += A * x + B if sympy.legendre_symbol(z, MODULO_P) == 1: y = sympy.sqrt_mod(z, MODULO_P) return Point(x, y)
def jacobi_symbols(flatten=False): """ Irreguar Array of Jacobi Symbols: 1 at quadratic residues, -1 at nonresideus, 0 at zero One row for each odd natural, n, of length n\n OEIS """ for p in odds(): pfacs = canonical_factorization(p) T = [] for a in range(p): out = 1 for f, e in pfacs.items(): out *= legendre_symbol(a, f)**e T.append(out) if flatten == True: yield from T else: yield tuple(T)
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 find_d(e, w): tmp = (w+1)//2 x = exgcd(e, w) nod = gcd(e,w) if tmp % nod == 0: d = (tmp*x//nod) % w return d''' C = find_C(p, q) print('C: ', C) A = find_A(C, R) print('A: ', A) w = (p - legendre_symbol(C, p)) * (q - legendre_symbol(C, q)) * exgcd(4, R) % R print('w: ', w) e = find_e(w) print('e: ', e) d = ((w + 1) * exgcd(2, w) * exgcd(e, w)) % w #d = (pow(e,-1,w)*(w+1)//2) % w print('Секретный ключ d: ', d) print('p, q, w, d держатся в секрете.') M = int(input('Введите передаваемое сообщение: ')) M = M % R print('Сообщение для шифрования: ', M) def find_XY(a, b, R, e): X = []