def factorize(n,e,d): s=0 s2=2 u=e*d-1 while u%2 == 0: s=s+1 s2*=2 u/=2 g=1 u=int(u) if pow(2,s)*u == e*d-1 and u%2 == 1: print("Decomposition ok") print("s is {}".format(s)) print("u is {}".format(u)) print("e*d-1 is {}".format(e*d-1)) while g ==1 or g==n: a=random.randrange(1,n) print("New Value for a generated : {}".format(a)) ggT=euclid.gcd(a,n) print("gcd(a,n)={}".format(ggT)) if ggT != 1: return [a, n/a ] au=sam2.multiply(a,u,n) print("a^u is : {}".format(au)) for j in range(0,s): g=euclid.gcd(sam2.multiply(au,pow(2,j),n)-1,n) print("Iteration {}: {}".format(j,g)) if g != 1 and g != n: return [g, n/g]
def check_identity(N): for i in range(N): for j in range(N): for m in range(1, N): if (i * j) % m == 1: assert gcd(i, m) == 1 assert gcd(j, m) == 1 if gcd(i, m) == 1 and gcd(j, m) == 1: assert (i * j) % m == 1
def __init__(self, x, y): divisor = euclid.gcd(x, y) self._x = x / divisor if y != 0: self._y = y / divisor else: print('被除数不能为0')
def rprime(x,y): """True if x is relatively prime to y >>> rprime(25,36) True >>> rprime(25,30) False """ return gcd(x,y) == 1
def factorRho(n,x_1): """ Factor using pollard's rho method """ x = x_1; xp = f(x) % n p = gcd(x - xp,n) #print ("x_i's: {") while p == 1: #print(x), # in the ith iteration x = x_i and x' = x_2i x = f(x) % n xp = f(xp) % n xp = f(xp) % n p = gcd(x-xp,n) #print ("}") if p == n: return -1 else: return p
def factor(n,b): """ Factor using Pollard's p-1 method """ a = 2; for j in range(2,b): a = a**j % n d = gcd(a-1,n); print ("d:",d,"a-1:",a-1) if 1 < d < n: return d; else: return -1;
def factor(n,b): """ Factor using Pollard's p-1 method """ a = 2; for j in range(2,b): a = a**j % n d = gcd(a-1,n); print "d:",d,"a-1:",a-1 if 1 < d < n: return d; else: return -1;
def factorRho(n,x_1): """ Factor using pollard's rho method """ x = x_1; xp = f(x) % n p = gcd(x - xp,n) print "x_i's: {" while p == 1: print x, # in the ith iteration x = x_i and x' = x_2i x = f(x) % n xp = f(xp) % n xp = f(xp) % n p = gcd(x-xp,n) print "}" if p == n: return -1 else: return p
def factorRho(n, x_1): """ Factor using pollard's rho method """ x = x_1 xp = f(x) % n p = gcd(x - xp, n) print "x_i's: {" while p == 1: print x, # in the ith iteration x = x_i and x' = x_2i x = f(x) % n xp = f(xp) % n xp = f(xp) % n p = gcd(x - xp, n) print "}" if p == n: return -1 else: return p
def compute_params(phi_n): '''Function to compute e and d''' d = 0 e = 0 found_e = False while found_e == False: e = random.randint(1, phi_n - 1) if gcd(e, phi_n) == 1: found_e = True _, d, _ = extgcd(e, phi_n) if (d < 0): d += phi_n return d, e
def find_keylength(crypt, n=3): """ Returns a dictionary of potential keylengths for provided crypt text The lhs of an entry tells us the potential keylength while the rhs tells us how likely it is to be correct. The n variable determins the length of segments to check on the crypt text. The greater the segment the fewer and more likely keylenghts gets returned, but the longer it takes to compute """ gcd_dict = {} for segment in set( window(crypt, n) ): # Notice that set is used. We don't iterate over duplicated strings # If segment matches something, save where the match started to a list # Matches is a list of positions where common segments occur matches = [match.start() for match in re.finditer(segment, crypt)] # Calcylate the offsets between each matching segment offsets = [ matches[i + 1] - matches[i] for i in range(len(matches) - 1) ] # Calcylate the gcd between each offset. gcds = [ gcd(offsets[i], offsets[i + 1]) for i in range(len(offsets) - 1) ] # Count all gcds for g in gcds: gcd_dict[g] = gcd_dict.get(g, 0) + 1 if n < 1 and len(gcd_dict) == 0: gcd_dict = find_keylength(crypt, n - 1) return gcd_dict
def pythmx(s): count = 0 s2 = s // 2 mmx = math.ceil(math.sqrt(s2)) tem = [] for m in range(2, mmx): if s2 % m == 0: sm = s2 // m while sm % 2 == 0: sm //= 2 k = m + 1 + m % 2 while k < 2 * m and k <= sm: if sm % k == 0 and euclid.gcd(k, m) == 1: d = s2 // (k * m) n = k - m a = d * (m * m - n * n) b = 2 * d * m * n c = d * (m * m + n * n) tem.append((a, b, c)) k += 2 return tem
def factor(n): base = [2,3,5,7,11,13,17,19,23]; start = int(sqrt(n)) pairs = []; factors = []; for i in range(start,n): for j in range(len(base)): lhs = i**2 % n rhs = base[j]**2 % n if lhs == rhs: print "%i^2 = %i = %i^2 = %i mod %i" % (i,lhs,base[j],rhs,n) pairs.append([i,base[j]]); print "pairs:",pairs for i in range(len(pairs)): factor = gcd(pairs[i][0]-pairs[i][1],n) if(factor != 1): print "factor:",factor
def factor(n): base = [2, 3, 5, 7, 11, 13, 17, 19, 23] start = int(sqrt(n)) pairs = [] factors = [] for i in range(start, n): for j in range(len(base)): lhs = i**2 % n rhs = base[j]**2 % n if lhs == rhs: print "%i^2 = %i = %i^2 = %i mod %i" % (i, lhs, base[j], rhs, n) pairs.append([i, base[j]]) print "pairs:", pairs for i in range(len(pairs)): factor = gcd(pairs[i][0] - pairs[i][1], n) if (factor != 1): print "factor:", factor