Exemplo n.º 1
0
def frequency_test_with_block(key, n, M=20000, b_print=True):
    N = n // M
    split_key = list(split_list(key, M))

    if len(split_key[-1]) != len(split_key[0]):
        split_key = split_key[0:-1]

    pi = list(map(lambda x: (x.count(1)) / M, split_key))

    chi_squared_obs = 4 * M * sum(list(map(lambda x: (x - 1 / 2)**2, pi)))
    p = sc.gammaincc(N / 2, chi_squared_obs / 2)

    b = (p >= 0.01)

    __print(b_print,
            '{:25}( M = {:<5} )   : {:.3f} -> {}'.format(
                'frequency test with block', M, p, b),
            end=' ')

    if M >= 20 and M > 0.01 * n and N < 100:
        __print(b_print, '( M is selected correctly. )')

    else:
        __print(b_print, '( M is NOT selected correctly. )')

    return [p], b
Exemplo n.º 2
0
def maurers_universal_statical_test(key, n, b_print=True):
    if n <387840:
        __print (b_print, '{:40} : Error. Need at  387,840 bits. Got {}.'.format('maurers universal statical test',n))
        return [0], False

    L=6
    nlist = [904960, 2068480, 4654080, 10342400, 22753280, 49643520, 107560960, 231669760, 496435200, 1059061760]
    num= len(list(filter(lambda x:n > x, nlist)))
    L+=num

    Q=10*2**L

    key = ''.join(list(map(str, key)))
    split_key=list(split_list(key,L))

    if len(split_key[-1]) != len(split_key[0]):
        split_key=split_key[0:-1]

    K=len(split_key)-Q

    split_key=list(map(lambda x : int(x,2),split_key))

    T = [0] * (2**L)

    for i in range(Q):
        contents = split_key[i]
        T[contents]=i

    sums=0.0

    for i in range(Q,Q+K):
        contents=split_key[i]
        bnum=T[contents]

        sums+=math.log2(i-bnum)

        T[contents]=i

    fn=sums/float(K)

    expectedValue_list = [5.2177052,6.1962507,7.1836656,8.1764248,9.1723243,10.170032,11.168765,12.168070,13.167693,14.167488,15.167379]
    variance_list      = [2.954,3.125,3.238,3.311,3.356,3.384,3.401,3.410,3.416,3.419,3.421]

    myu = expectedValue_list[num]
    v   = variance_list[num]

    c   = 0.7-0.8/L+(4+32/L)*(K**(-3/L)/15)
    sig = c*math.sqrt(v/K)

    p=math.erfc(abs((fn-myu)/(math.sqrt(2)*sig)))

    b = (p >= 0.01)

    __print(b_print, '{:40} : {:.3f} -> {} '.format('maurers universal statical test',p,b))

    return [p], b
def liner_complexity_test(key, n, M=500, b_print=True):
    if n < 1000000:
        __print(
            b_print, '{:40} : Error. Need at  1,000,000 bits. Got {}.'.format(
                'liner complexity test', n))
        return [0], False

    N = n // M

    K = 6

    key = ''.join(list(map(str, key)))
    split_key = list(split_list(key, M))

    if len(split_key[-1]) != len(split_key[0]):
        split_key = split_key[0:-1]

    L = list(map(bma, split_key))

    myu = M / 2 + (9 + (-1)**(M + 1)) / 36 - (M / 3 + 2 / 9) / 2**M

    T = list(map(lambda x: (-1)**M * (x - myu) + 2 / 9, L))

    v = [0] * 7

    for i in T:
        if i <= -2.5: v[0] += 1
        elif i <= -1.5: v[1] += 1
        elif i <= -0.5: v[2] += 1
        elif i <= 0.5: v[3] += 1
        elif i <= 1.5: v[4] += 1
        elif i <= 2.5: v[5] += 1
        else: v[6] += 1

    pi = [0.010417, 0.03125, 0.125, 0.5, 0.25, 0.0625, 0.020833]

    chi_squared_obs = sum(
        list(map(lambda x, y: (x - N * y)**2 / (N * y), v, pi)))

    p = sc.gammaincc(K / 2, chi_squared_obs / 2)

    b = (p >= 0.01)

    __print(b_print, '{:40} : {:.3f} -> {} '.format('liner complexity test', p,
                                                    b))

    return [p], b
Exemplo n.º 4
0
    def search_longest(Mbit):
        N = n // M
        split_key = list(split_list(key, M))

        if len(split_key[-1]) != len(split_key[0]):
            split_key = split_key[0:-1]

        ll = []

        for i in split_key[:N]:
            longest = 0
            now_longest = 0

            for j in range(M):
                if i[j] == 1:
                    now_longest += 1

                    if now_longest > longest:
                        longest = now_longest
                else:
                    now_longest = 0
            ll.append(longest)

        return ll
    def compute(s, m):

        if m == 0:
            return 0

        if m == 1: head = ''
        else: head = s[0:(m - 1)]

        s = s + head

        v = [0] * 2**m

        for i in range(m):
            ss = s[i:]

            split_key_m = list(split_list(ss, m))
            if len(split_key_m[-1]) != len(split_key_m[0]):
                split_key_m = split_key_m[0:-1]

            split_key_m = list(map(lambda x: int(x, 2), split_key_m))
            for i in range(2**m):
                v[i] = v[i] + split_key_m.count(i)

        return v
def binary_matrix_rank_test(key, n, M=32, Q=32, b_print=True):
    if n < 38912:
        __print(
            b_print,
            '{:40} : Error. Need at least 38,912 bits. Got {}.'.format(
                'binary matrix rank test', n))
        return [0], False

    N = n // (M * Q)
    split_key = list(split_list(key, M * Q))

    if len(split_key[-1]) != len(split_key[0]):
        split_key = split_key[0:-1]

    f_split_key = list(
        map(lambda x: np.reshape(np.array(x), [M, Q]), split_key))

    ranks = list(map(lambda x: rank(list(x)), f_split_key))

    full_rank = M

    FM = ranks.count(full_rank)
    FM1 = ranks.count(full_rank - 1)
    NFMM1 = N - FM - FM1

    chi_squared_obs = (FM - 0.2888 * N)**2 / (0.2888 * N) + (
        FM1 - 0.5776 * N)**2 / (0.5776 * N) + (NFMM1 -
                                               0.1336 * N)**2 / (0.1336 * N)
    p = math.e**(-chi_squared_obs / 2)

    b = (p >= 0.01)

    __print(b_print, '{:40} : {:.3f} -> {}'.format('binary matrix rank test',
                                                   p, b))

    return [p], b
Exemplo n.º 7
0
def overlapping_template_matching_test(key, n, m=9, M=1032, b_print=True):
    if n <1000000:
        __print (b_print, '{:40} : Error. Need at  1,000,000 bits. Got {}.'.format('overlapping template matching test',n))
        return [0], False

    N=n//M
    K=5

    if not(n>=M*N):
        __print (b_print, '{:40} : Error. "n>=M*N" This conditional expression is not satisfied.'.format('overlapping template matching test'))
        return [0], False

    # lam=(int(M-m+1)/(2**m)==2)

    B = "".join(['1' for x in range(m)])

    split_key=list(split_list(key,M))
    if len(split_key[-1]) != len(split_key[0]):
        split_key=split_key[0:-1]

    def index_multi(txt, x, M):
        ind=0
        ind_total=0
        l=[]
        while ind_total <M:
            if x in txt:
                ind = txt.index(x)
                txt=txt[(ind+1):]
                ind_total+=ind
                l.append(ind_total)
            else:
                return l

    w=[]
    for i in split_key:
        i = ''.join(map(str,i))
        txt=index_multi(i, B, M)
        num=len(txt)
        w.append(num)

    v=[0 for x in range(K+1)]

    for i in range(K+1):
        if i < K:
            v[i]=w.count(i)
        else :
            v[i]=len([x for x in w if x  >= K ])

    pi = [0.364091, 0.185659, 0.139381, 0.100571, 0.0704323, 0.139865]  # sample???

    lambd = (M-m+1.0)/(2.0**m)
    eta = lambd/2.0

    pi[0] = math.e**(-eta)
    pi[1] = eta/2*math.e**(-eta)
    pi[2] = eta/8*math.e**(-eta)*(eta+2)
    pi[3] = eta/8*math.e**(-eta)*(eta**2/6+eta+1)
    pi[4] = eta/16*math.e**(-eta)*(eta**3/24+eta**2/2+3*eta/2+1)
    pi[5] = 1- sum(pi[0:5])

    if not (N*min(pi)>5):
        __print (b_print, '{:40} : Error. "N*min(pi)>5" This conditional expression is not satisfied.'.format('overlapping template matching test'))
        return [0], False

    v=list(map(float,v))

    chi_squared_obs = sum(list(map(lambda x, y : ((x-N*y)**2)/(N*y),v,pi)))

    p=sc.gammaincc(5/2,chi_squared_obs/2)

    b = (p >= 0.01)

    __print(b_print, '{:40} : {:.3f} -> {} '.format('overlapping template matching test',p,b))

    return [p] , b
Exemplo n.º 8
0
def non_overlapping_template_matching_test(key,
                                           n,
                                           m=9,
                                           B=None,
                                           N=8,
                                           b_print=True):
    if n < 1000000:
        __print(
            b_print, '{:40} : Error. Need at  1,000,000 bits. Got {}.'.format(
                'non overlapping template matching test', n))
        return [0], False

    path_list = glob.glob('tests/templates/*')
    path_list.sort()
    path = path_list[m - 2]
    with open(path) as f:
        template = ["".join(s.strip().split()) for s in f.readlines()]

    B = B if B is not None else random.choices(template)[-1]

    M = int(n / N)

    if M <= 0.01 * n:
        __print(
            b_print, '{:40} : Error. M is NOT selected correctly.'.format(
                'non overlapping template matching test'))
        return [0], False

    myu = (M - m + 1) / (2**m)
    sig2 = M * (1 / (2**m) - (2 * m - 1) / (2**(2 * m)))

    split_key = list(split_list(key, M))

    if len(split_key[-1]) != len(split_key[0]):
        split_key = split_key[0:-1]

    def index_multi(txt, x, M):
        ind = 0
        ind_total = 0
        l = []
        while ind_total < M:
            if x in txt:
                ind = txt.index(x)
                txt = txt[(ind + m):]
                ind_total += ind
                l.append(ind_total)
            else:
                return l

    w = []
    for i in split_key:
        i = ''.join(map(str, i))
        txt = index_multi(i, B, M)
        num = len(txt)
        w.append(num)

    w = list(map(float, w))

    chi_squared_obs = sum(list(map(lambda x: ((x - myu)**2) / sig2, w)))
    p = sc.gammaincc(N / 2, chi_squared_obs / 2)

    b = (p >= 0.01)

    __print(
        b_print, '{:40} : {:.3f} -> {} '.format(
            'non overlapping template matching test', p, b))

    return [p], b