def three(): N = mpz('720062263747350425279564435525583738338084451473999841826653057981916355690188337790423408664187663938485175264994017897083524079135686877441155132015188279331812309091996246361896836573643119174094961348524639707885238799396839230364676670221627018353299443241192173812729276147530748597302192751375739387929') A = mul(isqrt(mul(6,N)),2) A = add(A,1) x = isqrt(sub(mul(A,A),mul(24,N))) p = t_div(sub(A,x),6) q = t_div(add(A,x),4) if mul(p,q) == N: print "3. " + str(p)
def three(): N = mpz( '720062263747350425279564435525583738338084451473999841826653057981916355690188337790423408664187663938485175264994017897083524079135686877441155132015188279331812309091996246361896836573643119174094961348524639707885238799396839230364676670221627018353299443241192173812729276147530748597302192751375739387929' ) A = mul(isqrt(mul(6, N)), 2) A = add(A, 1) x = isqrt(sub(mul(A, A), mul(24, N))) p = t_div(sub(A, x), 6) q = t_div(add(A, x), 4) if mul(p, q) == N: print "3. " + str(p)
def _batchgcd(xs): tree = _product_tree(xs) rems = tree.pop() while tree: LOGGER.info('Calculating batch GCDs: %10d' % (len(tree))) xs = tree.pop() rems = [ gmpy2.mod(rems[i // 2], gmpy2.mul(xs[i], xs[i])) for i in range(len(xs)) ] return [gmpy2.gcd(gmpy2.t_div(r, n), n) for r, n in zip(rems, xs)]
def square_root(x, N): square_root_x = gmpy2.powmod(x, gmpy2.t_div(N+1, 4), N) return square_root_x
for i in range(0, TEST_N): indexX = random.randint(0, MAX_N - 1) indexY = random.randint(0, MAX_N - 1) X = arr[indexX] Y = arr[indexY] ans = gmpy2.add(X, Y) addition_content += generate_exp(indexX, indexY, '+') addition_content += generate_equal(ans) ans = gmpy2.sub(X, Y) subtraction_content += generate_exp(indexX, indexY, '-') subtraction_content += generate_equal(ans) ans = gmpy2.mul(X, Y) multiple_content += generate_exp(indexX, indexY, '*') multiple_content += generate_equal(ans) ans = gmpy2.t_div(X, Y) division_content += generate_exp(indexX, indexY, '/') division_content += generate_equal(ans) ans = gmpy2.t_mod(X, Y) division_content += generate_exp(indexX, indexY, '%') division_content += generate_equal(ans) def write_longer(test_case_name, file_name, content): test_case = generate_test_case(test_case_name, 'Longer', init_content + content) output = open(file_name, 'w') output.write(generate_header()) output.write(test_case) output.close()
print("The number:", numtofactor) print("has prime factors:") print("[", end="") def findprimefactor(numtofactor): for primenum in primelist2M.primelist: if (gmpy2.t_mod(numtofactor, primenum) == 0): return (primenum) return (0) while (True): if gmpy2.is_prime(numtofactor): # We are done. All prime factors found print(str(numtofactor) + "]") exit(0) primefactor = findprimefactor(numtofactor) if (primefactor == 0): # The remainder is not itself prime, and # has no factors in the prime list. # continue the 'hard way', maybe rho algorithm print("rem=" + str(numtofactor) + "]") exit(0) print(str(primefactor) + ",", flush=True, end="") numtofactor = gmpy2.t_div(numtofactor, primefactor) #print("new num to factor = ",numtofactor)
def L(x, n): return gmpy2.t_div(x - 1, n)
def get_random_safe_prime(start,end): i = random.randint(start,end) # better random nunber generator while not (gmpy2.is_prime(i) and gmpy2.is_prime(gmpy2.t_div((i-1),2))): i +=1 return i
def divide(x, y): return gmpy2.t_div(gmpy2.mpz(x), gmpy2.mpz(y))
2x == sqrt(4*A**2 - 4*A + 1 - 24*N) 3p == A - (x + 0.5) 6p == 2A - 2x - 1 p == 6p / 6 2q == A + (x - 0.5) 4q == 2A + 2x - 1 q = 4q / 4 """ t0 = 4 * (A**2) - 4 * A + 1 - 24 * N twox, is_exact = iroot(t0, 2) if not is_exact: print("twox is not an integer!!!") sys.exit(1) sixp = 2 * A - twox - 1 fourq = 2 * A + twox - 1 p = t_div(sixp, 6) q = t_div(fourq, 4) if (p * q) != N: print("3: Fail check") print("3:") print(p.digits() if p < q else q.digits()) """ Challenge 4. """ N = mpz( '179769313486231590772930519078902473361797697894230657273430081157732675805505620686985379449212982959585501387537164015710139858647833778606925583497541085196591615128057575940752635007475935288710823649949940771895617054361149474865046711015101563940680527540071584560878577663743040086340742855278549092581' ) C = mpz( '22096451867410381776306561134883418017410069787892831071731839143676135600120538004282329650473509424343946219751512256465839967942889460764542040581564748988013734864120452325229320176487916666402997509188729971690526083222067771600019329260870009579993724077458967773697817571267229951148662959627934791540' )
D.append(gmpy2.f_mod(gmpy2.mul(r, C[i]), p)) D_sum = gmpy2.mpz(0) for i in range(0, n): D_sum = gmpy2.add(D_sum, D[i]) D_sum = gmpy2.f_mod(D_sum, p) #P_0 retrieves E: sI = gmpy2.invert(s, p) E = gmpy2.f_mod(gmpy2.mul(sI, D_sum), p) if eval_correctness: #Evaluate Correctness of Protocol res = gmpy2.sub(E, gmpy2.f_mod(E, gmpy2.mul(alpha, alpha))) res = gmpy2.t_div(res, gmpy2.mul(alpha, alpha)) correct += abs(res - plain_res) #print("plaintext result:" + str(plain_res)) #print("protocol result:" + str(res) + "\n") else: #Evaluate the Attack by computing E'=E/alpha Ep = gmpy2.t_div(E, alpha) s_Ep = gmpy2.digits(Ep) b0a = [ gmpy2.mul(gmpy2.mul(ai, b), alpha) for b, ai in zip(b0, a) ] b1a = [ gmpy2.mul(gmpy2.mul(ai, b), alpha) for b, ai in zip(b1, a) ]