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
 def _fx(self, x, a, b):
     if x in self.S1:
         return (
             Mul(self.beta, x, self.n),
             a,
             Add(b, 1, self.ord)
         )
     elif x in self.S2:
         return (
             pow(x, 2, self.n),
             Mul(2, a, self.ord),
             Mul(2, b, self.ord)
         )
     else:
         return (
             Mul(self.alpha, x, self.n),
             Add(a, 1, self.ord),
             b
         )
Example #3
0
 def encrypt(self, x):
     flag = True
     while flag:
         k = random.randint(2, self.n)
         if GCD(k, self.n) == 1:
             break
     x0y0 = self.E.multi(self.Q, k)
     y1 = self._point_compress(self.E.multi(self.P, k))
     y2 = Mul(x, x0y0[0], self.E.p)
     return (y1, y2)
Example #4
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
     )
Example #5
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)]
Example #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
Example #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)
Example #8
0
 def encrypt(self, x):
     return (pow(self.alpha, self.k,
                 self.n), Mul(x, pow(self.beta, self.k, self.n), self.n))