コード例 #1
0
def dlog(b, a, m):
    counter = 0
    result = exp(b, 0, m)
    while (result != a):
        counter = counter + 1
        result = exp(b, counter, m)
    return counter
コード例 #2
0
def test(a, n):
    t, u = 0, n - 1
    while u % 2 == 0:
        t += 1
        u //= 2
    x = exp(a, u, n)
    if x == 1 or x == n - 1:
        return False
    for _ in range(t - 1):
        x = exp(x, 2, n)
        if x == n - 1:
            break
    else:
        return True
コード例 #3
0
def test(a, n):
    t, u = 0, n - 1
    while (u % 2 == 0):
        t += 1  #t >= 1, u is odd, n=1 = 2^t * u
        u //= 2  #initialization
    x = exp(a, u, n)  #initializing x0 = a^u mod n

    for _ in range(t - 1):  #for i = 1 to t
        x_prev = x  #xi-1
        x = exp(x_prev, 2, n)  #xi = (xi-1)^2 mod n
        if x == 1 and x_prev != 1 and x_prev != (n - 1):  #NSR test
            return True

    if x != 1:  #Fermat test
        return True
    return False
コード例 #4
0
def test(a, n):
    s = n - 1
    t = 0
    while (s % 2 == 0):  #2로 더이상 나누어지지 않을 때까지
        s //= 2
        t += 1
    u = s  # a=4 u=5 t=4
    x = exp(a, u, n)

    for i in range(t):  #NSR Test
        xx = exp(x, 2, n)
        if (xx == 1):
            if (exp(x, 1, n) != 1 and exp(x, 1, n) != n - 1):
                return True  # 확실히 합성수
        x = xx
    if (xx != 1):  # Fermat Test
        return True
    return False
コード例 #5
0
def test(a, n):
    # n-1을 인수분해
    t = 0
    nn = n - 1
    while ((nn % 2) == 0):
        t += 1
        nn //= 2
    u = nn
    #     print("a : ", a, "n : ", n, "u: ", u, "t: ", t)
    # t=1 부터 n까지 a^u를 거듭제곱하면서 NSR test
    x_0 = 0
    x_i = exp(a, u, n)
    for i in range(t):
        #         print("x_0 : ", x_0, "x_i : ", x_i)
        if (x_i == 1 or x_i == (n - 1)):
            return False  # prime이라는 증거
        x_0 = x_i
        x_i = exp(x_i, 2, n)
    return True  # composite이다.
コード例 #6
0
def test(a, n):
    y = n - 1
    cnt = 0
    while (1):
        if (y % 2 == 0):
            y //= 2
            cnt += 1
        else:
            break
    t = cnt
    u = y

    x_0 = exp(a, u, n)

    for i in range(t):
        x = exp(x_0, 2, n)
        if x == 1 and x_0 != 1 and x_0 != n - 1:
            return True
        x_0 = x

    if x != 1:
        return True
    return False
コード例 #7
0
def test(a, n):
    # 1. 정수 k,q를 찾는다.
    while True:
        k = 1
        q_remain = (n - 1) % (exp(2, k, n))
        if q_remain == 0:
            q = (n - 1) // (exp(2, k, n))
            break
        k += 1

    # 2. 1 < a < n-1 임의 정수 a 선택
    # -> 인수로 이미 a를 받았다.

    # 3. 첫 번쨰 조건
    if exp(a, q, n) % n == 1:
        return False

    # 4. 두 번째 조건
    for j in range(0, k):
        x = exp(2, j, n)
        if exp(a, x * q, n) % n == n - 1:
            return False

    return True
コード例 #8
0
def test(a, n):
    # n-1 = (2^t) * u
    t = 0
    u = n - 1
    while u % 2 == 0:
        t += 1
        u //= 2

    # x0 = (a^u) mod n
    x = exp(a, u, n)
    updated_x = x

    # NSR test
    for _ in range(t):
        updated_x = exp(x, 2, n)
        if updated_x == 1 and x != 1 and x != n - 1:
            return True
        x = updated_x

    # Fermat test
    if updated_x != 1:
        return True

    return False
コード例 #9
0
def dlog(b, a, m):
    cur = 1
    for i in range(1, m):
        cur = exp(b, i, m)
        if cur == a:
            return i
コード例 #10
0
# from tqdm import tqdm  # progress bar
from exponentiation import exp
import random


def dlog(b, a, m):
    cur = 1
    for i in range(1, m):
        cur = exp(b, i, m)
        if cur == a:
            return i


if __name__ == "__main__":
    base = 3
    modulus = 65537  # 2 ** 16 + 1

    expos = [1, 2, 65535]

    try:
        # for expo in tqdm(expos):
        for expo in expos:
            powed = exp(base, expo, modulus)
            loged = dlog(base, powed, modulus)
            print("({},{},{},{},{}),".format(expo, base, powed, loged,
                                             modulus))
            assert expo == loged
        print(">> All the tests passed <<")
    except:
        print("expo({}) and loged({}) is not equal".format(expo, loged))
コード例 #11
0
def dlog(a, b, n):
    x = 0
    for i in range(1,n):
        x = exp(a,i,n)
        if x == b:
            return i