コード例 #1
0
def elemop(N=1000):
    r'''
    (Takes about 40ms on a first-generation Macbook Pro)
    '''
    for i in range(N):
        assert a+b == 579
        assert a-b == -333
        assert b*a == a*b == 56088
        assert b%a == 87
        assert divmod(a, b) == (0, 123)
        assert divmod(b, a) == (3, 87)
        assert -a == -123
        assert pow(a, 10) == 792594609605189126649
        assert pow(a, 7, b) == 99
        assert cmp(a, b) == -1
        assert '7' in str(c)
        assert '0' not in str(c)
        assert a.sqrt() == 11
        assert _g.lcm(a, b) == 18696
        assert _g.fac(7) == 5040
        assert _g.fib(17) == 1597
        assert _g.divm(b, a, 20) == 12
        assert _g.divm(4, 8, 20) == 3
        assert _g.divm(4, 8, 20) == 3
        assert _g.mpz(20) == 20
        assert _g.mpz(8) == 8
        assert _g.mpz(4) == 4
        assert a.invert(100) == 87
コード例 #2
0
ファイル: p104.py プロジェクト: daedalus/project_euler
def p104():
    k = 0
    while True:
        Fk = fib(k)
        sFk = str(Fk)
        s = "".join(sorted(sFk[-9:]))
        e = "".join(sorted(sFk[:9]))
        sys.stderr.write("%d %s %s\r" % (k, s, e))
        if s == e == 123456789:
            print("---")
            return k
        k += 1
コード例 #3
0
def decrypt_flag():
    enc_flag = bytearray.fromhex(
        '28f269981acf4c8c2ef36fa83df2689832fa699434c479922fee6f993cfe559401f5559504c46e980cfe559102e87ea853fe3bcf5da339c31b00'
    )
    key = ctypes.c_uint(gmpy2.fib(0x402)).value

    out = bytearray()
    for x in xrange(0x39):
        b = enc_flag[x] ^ ord(p32(key)[x & 3])
        out.append(b)
        if (x & 3) == 3:
            key += 1

    print(bytes(out))
コード例 #4
0
def attack(attack_rsa_obj, publickey, cipher=[]):
    """Run tests against fermat composites"""
    with timeout(attack_rsa_obj.args.timeout):
        try:
            limit = 10000
            p = q = None
            for x in tqdm(range(1, limit)):
                f = gcd(fib(x), publickey.n)
                if 1 < f < publickey.n:
                    p = publickey.n // f
                    q = f
                    break
            if p is not None and q is not None:
                priv_key = PrivateKey(int(p), int(q), int(publickey.e),
                                      int(publickey.n))
                return (priv_key, None)
            return (None, None)
        except TimeoutError:
            return (None, None)
コード例 #5
0
 def attack(self, publickey, cipher=[], progress=True):
     """Run tests against fermat composites"""
     with timeout(self.timeout):
         try:
             limit = 10000
             p = q = None
             for x in tqdm(range(1, limit), disable=(not progress)):
                 f = gcd(fib(x), publickey.n)
                 if 1 < f < publickey.n:
                     p = publickey.n // f
                     q = f
                     break
             if p is not None and q is not None:
                 priv_key = PrivateKey(int(p), int(q), int(publickey.e),
                                       int(publickey.n))
                 return (priv_key, None)
             return (None, None)
         except TimeoutError:
             return (None, None)
コード例 #6
0
ファイル: timing3.py プロジェクト: dgquintas/my-code-samples
import gmpy2 as _g
import time

print("Typical expected results would be:","""
D:\PySym>python timing.py
Factorial of 10000 took 0.0619989238859 (35660 digits)
Fibonacci of 10000 took 0.000744228458022 (2090 digits)
Factorial of 100000 took 4.44311764676 (456574 digits)
Fibonacci of 100000 took 0.022344453738 (20899 digits)
Factorial of 1000000 took 152.151135367 (5565709 digits)
Fibonacci of 1000000 took 0.670207059778 (208988 digits)
""")

print("Actual timings and results...:")
for i in (10000,100000,1000000):
    start=time.time()
    x=_g.fac(i)
    stend=time.time()
    print("Factorial of %d took %s (%d digits)" % (
        i, stend-start, x.numdigits()))

    start=time.time()
    x=_g.fib(i)
    stend=time.time()
    print("Fibonacci of %d took %s (%d digits)" % (
        i, stend-start, x.numdigits()))

コード例 #7
0
 def __call__(self, i):
     return gmpy2.fib(i + 1)
コード例 #8
0
def timedfibsp(n, one):
    start = time.clock()
    result = gmpy.fib(n)
    stend = time.clock()
    return type(one), stend - start, result
コード例 #9
0
ファイル: timing2.py プロジェクト: bnaegel/gmpy
def timedfibsp(n, one):
    start=time.clock()
    result=gmpy.fib(n)
    stend=time.clock()
    return type(one), stend-start, result
コード例 #10
0
def get_fib(number):
    ''' Return Fibonacci '''
    return str(gmpy2.fib(int(number)))
コード例 #11
0
import gmpy2 as _g
import time

print "Typical expected results would be:", """
D:\PySym>python timing.py
Factorial of 10000 took 0.0619989238859 (35660 digits)
Fibonacci of 10000 took 0.000744228458022 (2090 digits)
Factorial of 100000 took 4.44311764676 (456574 digits)
Fibonacci of 100000 took 0.022344453738 (20899 digits)
Factorial of 1000000 took 152.151135367 (5565709 digits)
Fibonacci of 1000000 took 0.670207059778 (208988 digits)
"""

print "Actual timings and results...:"
for i in (10000, 100000, 1000000):
    start = time.clock()
    x = _g.fac(i)
    stend = time.clock()
    print "Factorial of %d took %s (%d digits)" % (i, stend - start,
                                                   x.num_digits())

    start = time.clock()
    x = _g.fib(i)
    stend = time.clock()
    print "Fibonacci of %d took %s (%d digits)" % (i, stend - start,
                                                   x.num_digits())
コード例 #12
0
    
def s(stuff, condition):
    r.sendline(stuff)
    msg = ''
    while condition not in msg and 'UDCTF' not in msg:
        while r.can_recv(timeout=0.1):
            msg += r.recv()
            print msg
    return msg

r = remote('52.15.140.126', 6002)
m = ''
while r.can_recv(timeout=0.5):
    m += r.recv()
print m

cond = 'FIND THE FIB('
found = False
while not found:
    if 'UDCTF{' in m:
        found = True
    elif cond in m:
        n = m.split(cond)[1].split(')')[0]
        ans = str(gmpy2.fib(int(n)) % 10007)
        m = s(ans, cond)

temp = 'UDCTF{' + m.split('UDCTF{')[1].split('}')[0] + '}'
print flag(temp)
with open('flag.txt', 'w') as f:
    f.write(flag(temp))
コード例 #13
0
from gmpy2 import fib

from tools.euler import is_pandigital

fibA, fibB = 1, 1
fibN = 2
bFound = False
while not bFound:
    while True:
        fibN, fibA, fibB = fibN + 1, fibB, (fibA + fibB) % (10**9)
        if is_pandigital(fibB):
            break
    bFound = is_pandigital(str(fib(fibN))[:9])
print(fibN)
コード例 #14
0
def f3(n):
    return gmpy2.fib(n)