Пример #1
0
    def RSASP1(k:golberg_key, m) -> any : 
        #Second form of the key. Step 2      
        
        s_1 = fastModularExponentation(m, k.d_np, k.p)
        s_2 = fastModularExponentation(m, k.d_nq, k.q)
        h = ((s_1 - s_2) * k.q_inv) % k.p

        return s_2 + k.q * h
Пример #2
0
def millerRabin_2(n: int, a: int) -> bool:
    if a > n - 2 or a < 2:
        print('random a must be between 2 and n-2')

    #Step 1
    prev_n = n - 1

    s = round(log2(prev_n))
    if s == 1:
        return False
    d = int(prev_n / s)

    # n - 1 must be even
    if (prev_n % 2 > 0):
        return False

    #Step 2. (seed() function has not be called, so random function's seed is by default 1970)
    #Step 3.
    x = pow(a, d, n)
    aux = x % n
    if (aux == 1 or aux == -1):
        return True

    r = 1
    #Step 4
    while True:
        x = fastModularExponentation(a, int((2 ^ r) * d), n)
        if x == 1:
            return False
        elif x == -1:
            return True

        r += 1
        if (r < s - 1):
            break

    #Step 5: r == s-1
    x = fastModularExponentation(a, int((2 ^ (s - 1)) * d), n)
    if x == -1:
        return True
    return False
    def gen(self, k: int):
        # Pick p, q primes such that p | q - 1, that is equvalent to
        # say that q = r*p + 1 for some r
        #p is prime of length k

        p = number.getPrime(k)
        r = 1
        while True:
            q = r * p + 1
            if number.isPrime(q):
                break
            r += 1

        # Compute elements of G = {i^r mod q | i in Z_q*}
        G = []
        ctr = 0
        self.t = q

        #Choose two random elements inside G
        last_i = 0
        while True:
            i = random.randint(1, q)
            if i != last_i:
                aux = pow(i, r, q)
                if aux != 1:
                    G.append(aux)
                    ctr += 1
                    last_i = i
                    if ctr == 2:
                        break

        # Since the order of G is prime, any element of G except 1 is a generator
        self.g = G[0]
        self.sk = G[1]

        # pk= g^s
        self.pk = fastModularExponentation(self.g, self.sk, q)
 def sign_sk_provided(self, m: int, sk: int):
     if m + sk == 0:
         return 1
     # g ^ 1/(x+sk)
     return fastModularExponentation(self.g, 1 / m + sk, self.t)