def test_to_data():
    assert I.to_data(I.closedopen(2, 3)) == [(I.CLOSED, 2, 3, I.OPEN)]
    assert I.to_data(I.openclosed(2, I.inf)) == [(I.OPEN, 2, float('inf'), I.OPEN)]
    assert I.to_data(I.closed(-I.inf, 2)) == [(I.OPEN, float('-inf'), 2, I.CLOSED)]

    assert I.to_data(I.empty()) == [(I.OPEN, float('inf'), float('-inf'), I.OPEN)]

    i = I.openclosed(-I.inf, 4) | I.closedopen(6, I.inf)
    assert I.to_data(i) == [(I.OPEN, float('-inf'), 4, I.CLOSED), (I.CLOSED, 6, float('inf'), I.OPEN)]
    assert I.to_data(i, conv=str, pinf='highest', ninf='lowest') == [(I.OPEN, 'lowest', '4', I.CLOSED), (I.CLOSED, '6', 'highest', I.OPEN)]
Exemplo n.º 2
0
def main():
    getcontext().prec = 8192
    moggle = O47()
    e, n = moggle.pubkey()
    c = moggle.challenge()
    print('\nMoggle\'s challenge:\nc: ' + str(c), end='\n\n')

    a = 2 * B
    b = (3 * B) - 1
    M_last = [[a, b]]
    I_last = I.closed(a, b)

    s_i = ceil(n / (3 * B))
    while True:
        if moggle.query((c * pow(s_i, e, n)) % n):
            print('s1: ' + str(s_i))
            break
        s_i += 1

    ra = s3_calc_ra(M_last[0][0], s_i, n)
    rb = s3_calc_rb(M_last[0][1], s_i, n)
    print('r in: [' + str(ra) + ', ' + str(rb) + ']\n')
    r_i = [ra, rb]
    if ra > rb:
        print('r-val panic.')
        exit(1)

    m_i = I.empty()
    for little_r in range(ra, rb + 1):
        aa = s3_calc_aa(little_r, n, s_i)
        bb = s3_calc_bb(little_r, n, s_i)
        if (aa > b) or (bb < a):
            print('bad interval. continue.')
            continue
        aaa = max(M_last[0][0], aa)
        bbb = min(M_last[len(M_last) - 1][1], bb)
        m_i |= I.closed(aaa, bbb)
    I_last = I_last & m_i
    M = []
    for interval in I.to_data(m_i):
        M.append([interval[1], interval[2]])

    s_i_last = s_i
    M_last = M

    # step2 2nd iteration start
    #   and step 3
    while len(M_last) > 1:
        s_i += 1
        #    print('  multi-I, s_i = ' + str(s_i))
        if not moggle.query((c * pow(s_i, e, n)) % n):
            continue
        else:
            m_i = I.empty()
            ra = s3_calc_ra(M[0][0], s_i, n)
            rb = s3_calc_rb(M[len(M) - 1][1], s_i, n)
            if ra > rb:
                print('multi-I r-val panic.')
                exit(1)
            for little_r in range(ra, rb + 1):
                for little_interval in M_last:
                    aa = s3_calc_aa(little_r, n, s_i)
                    bb = s3_calc_bb(little_r, n, s_i)
                    aaa = max(little_interval[0], aa)
                    bbb = min(little_interval[1], bb)
                    m_i |= I.closed(aaa, bbb)
            I_last = I_last & m_i
            M = []
            for interval in I.to_data(I_last):
                M.append([interval[1], interval[2]])
                print('  s_i: ' + str(s_i) + '\n    [\n      ' +
                      str(interval[1]) + '\n      ' + str(interval[2]) +
                      '\n    ]\n')
            M_last = M
            s_i_last = s_i

    M_last = M[0]
    print('checkpoint')
    print('[\n  ' + str(M_last[0]) + ',\n  ' + str(M_last[1]) + '\n]\n')
    # start 2c, one interval left

    try:
        print('\n\nphase single-I start\n')
        S_I_FOUND = False
        r_i = step_2c_calc_ri_start(M_last[1], s_i_last, n) - 1
        while True:
            if S_I_FOUND:
                r_i = step_2c_calc_ri_start(M_last[1], s_i_last, n)
            else:
                r_i += 1
#      print('  r_i', r_i)
            sa = step_2c_calc_sa(r_i, n, M_last[1])
            sb = step_2c_calc_sb(r_i, n, M_last[0])
            si = sa
            S_I_FOUND = False
            #      print('    si:[', sa, ',', sb, ']')
            while True:
                if moggle.query((c * pow(si, e, n)) % n):
                    S_I_FOUND = True
                    s_i = si
                    break
                si += 1
                if si > sb:  # this should only trigger if no conformant s_i was found ; increment r_i and start over
                    break
            if not S_I_FOUND:  # remainder of try block should only execute if a conformant s_i was found
                continue


#      print('    s_i', s_i) ####

            aa = s3_calc_aa(r_i, n, s_i)
            bb = s3_calc_bb(r_i, n, s_i)
            aaa = max(M_last[0], aa)
            bbb = min(M_last[1], bb)
            m_i = I.closed(aaa, bbb)
            #      print('aa\n', aa, '\nbb\n', bb)
            #      print('m_i\n',aaa,'\n',bbb,end='\n\n')

            I_last = I_last & m_i
            if len(I_last) > 1:
                print('candidate intervals re-multiplied. panic.')
                exit(1)

            M_last = [I.to_data(I_last)[0][1], I.to_data(I_last)[0][2]]
            #      print('  new M\n  [\n    ' + str(M_last[0]) + ',\n    ' + str(M_last[1]) + '\n  ]\n')
            if M_last[0] == M_last[1]:
                break
            s_i_last = s_i
            print((M_last[0]).to_bytes(96, 'big'))

    except KeyboardInterrupt:
        print('\n\n======================================\n\ncrack')
        # TOGGLE
        #    print((M_last[0]).to_bytes(32, 'big'))
        print((M_last[0]).to_bytes(96, 'big'))
        print(M_last[0])
        print(M_last[1], end='\n\n')

        print('oracle')
        print(moggle.m)
        print(int.from_bytes(moggle.m, 'big'))
        print('\nc', c)
        print('n', moggle.n)
        print('d', moggle.d)
        print('\nqueried ' + str(moggle.qcount()) + ' times')
        print('true ' + str(moggle.tcount()) + ' times')
        exit(1)

    print('\n\n======================================\n\ncrack')
    # TOGGLE
    #  print((M_last[0]).to_bytes(32, 'big'))
    print((M_last[0]).to_bytes(96, 'big'))
    print(M_last[0], end='\n\n')

    print('oracle')
    print(moggle.m)
    print(int.from_bytes(moggle.m, 'big'))
    print('\nc', c)
    print('d', moggle.d)
    print('n', moggle.n)
    print('\nqueried ' + str(moggle.qcount()) + ' times')
    print('true ' + str(moggle.tcount()) + ' times')
Exemplo n.º 3
0
for k in range(7):
    T = I.closedopen("08:00", "12:00") | I.closedopen(
        "13:00", "17:00")  # 初始化, 可調整填入時間的上下限
    Te = I.empty()  #初始化
    for l in range(len(We[k])):  # 去除上課時間
        T = T - S[We[k][l]]
    if not T.is_empty():
        for l in range(len(T)):  # 找出小於30 min的T
            if (datetime.strptime(T[l].upper, '%H:%M') -
                    datetime.strptime(T[l].lower, '%H:%M')).seconds < 1800:
                Te = Te | T[l]
        T = T - Te  # 去除小於30 min
        for key, val in W.items():  # 將interval加入val
            if val[0] == k:
                for i in range(len(I.to_data(T))):
                    for j in range(1, 3):
                        val.append(int(I.to_data(T)[i][j].split(':')[0]))
                        val.append(int(I.to_data(T)[i][j].split(':')[1]))
print('W:')
for key, val in W.items():
    print(key, ':', val)
print()

for i in range(50):  # ProgressBar
    ProgressBar(i + 1, 50, unit='%', a=20, decimals=0, finish='')
    # sleep(0.06 - i * 0.0012)
    sleep(0.000024 * (i - 50)**2 + 0.01)
j = ' |'
for i in range(50):  # 作者資訊
    j += 'This program is writen by scout1256/0752525       '[i]