Exemplo n.º 1
0
def CRT(b_arr, m_arr):
    if (b_arr.__len__() != m_arr.__len__()) or (b_arr.__len__() == 0):
        return -1
    M = reduce(lambda x, y: x * y, m_arr)
    M_arr = [M // m for m in m_arr]
    ''' MR means M_inverse '''
    MR_arr = [Inverse(Mi % mi, mi) for Mi, mi in zip(M_arr, m_arr)]
    return sum([b * Mi * MiR for b, Mi, MiR in zip(b_arr, M_arr, MR_arr)]) % M
Exemplo n.º 2
0
 def decrypt(self, cipher):
     print(cipher)
     x0y0 = self.E.multi(
         self._point_decompress(cipher[0][0], cipher[0][1]),
         self.m
     )
     return Mul(
         cipher[1],
         Inverse(x0y0[0], self.E.p),
         self.E.p
     )
Exemplo n.º 3
0
def CongEq(a, b, n):
    d = GCD(a, n)
    if d != 1 and not DIVVerify(d, b):
        return [-1]
    elif d != 1:
        res = CongEq(a // d, b // d, n // d)
        k = 1
        while k * n // d < n:
            res.append(res[0] + k * n // d)
            k += 1
        return res
    else:
        return [Mul(b, Inverse(a, n), n)]
Exemplo n.º 4
0
def PohligHellman(n, alpha, beta, q, c):
    j = 0
    arr = []
    beta_j = beta
    while j <= c - 1:
        delta = pow(beta_j, (n // pow(q, j + 1)), n)
        i = 0
        while delta != pow(alpha, ((i * n // q) % n), n):
            i = i + 1
        arr.append(i)
        ''' beta_{j+1} = beta_{j}*alpha^{-a_j*q^j} (mod n) '''
        beta_j = Mul(
            beta_j,
            pow(Inverse(alpha, n), Mul(arr[arr.__len__() - 1], pow(q, j), n),
                n), n)
        j = j + 1
    return arr
Exemplo n.º 5
0
    def __init__(self, bit, opt='--null', p=None, q=None, a=None):
        if opt == '-pq' or opt == '-pqa':
            self.p = p
            self.q = q
        else:
            self.p = randprime(2**(bit - 1), 2**bit)
            self.q = randprime(2**(bit - 1), 2**bit)
        self.phi = (self.p - 1) * (self.q - 1)
        self.n = self.p * self.q

        if opt == '-pqa':
            self.a = a
        else:
            flag = True
            while flag:
                a_gen = randint(2**(2 * bit - 1), 2**(2 * bit))
                if GCD(a_gen, self.phi) == 1:
                    flag = False
            self.a = a_gen
        self.b = Inverse(self.a, self.phi)
Exemplo n.º 6
0
def Shanks(n, alpha, beta):
    m = ceil(sqrt(n))

    j_alpha = [{"j": j, "alpha^jm": pow(alpha, j * m, n)} for j in range(0, m)]
    j_alpha.sort(key=lambda item: item["alpha^jm"])

    i_ba_inv = [{
        "i": i,
        "beta*alpha^-i": Mul(beta, Inverse(pow(alpha, i, n), n), n)
    } for i in range(0, m)]
    i_ba_inv.sort(key=lambda item: item["beta*alpha^-i"])
    '''
    move index and jndex
    to find which i, j st. alpha^jm = beta*alpha^-i
    '''
    index, jndex = 0, 0
    while index != m and jndex != m:
        if j_alpha[jndex]["alpha^jm"] > i_ba_inv[index]["beta*alpha^-i"]:
            index += 1
        elif j_alpha[jndex]["alpha^jm"] < i_ba_inv[index]["beta*alpha^-i"]:
            jndex += 1
        else:
            return Add(m * j_alpha[jndex]["j"], i_ba_inv[index]["i"], n)
    return -1
Exemplo n.º 7
0
 def decrypt(self, y_1, y_2):
     return Mul(y_2, Inverse(pow(y_1, self.a, self.n), self.n), self.n)