예제 #1
0
 def keygen(self, secparam):
     while True:
         p, q = randomPrime(secparam), randomPrime(secparam)
         print("p,q=>", p, q)
         if isPrime(p) and isPrime(q) and p != q:
             N = p * q
             phi_N = N - p - q + 1
             break
     pk = {'secparam': secparam, 'N': N, 'phi_N': phi_N}
     sk = {'p': p, 'q': q}
     return (pk, sk)
예제 #2
0
 def keygen(self, secparam=1024):
     while True:
         p, q = randomPrime(secparam), randomPrime(secparam)
         if isPrime(p) and isPrime(q) and gcd(p * q, (p - 1) * (q - 1)) == 1:
             break
     n = p * q
     g = n + 1
     n2 = n ** 2
     lam = lcm(p - 1, q - 1)
     u = (self.L(((g % n2) ** lam), n) % n) ** -1
     pk = {'n': n, 'g': g, 'n2': n2}
     sk = {'lam': lam, 'u': u}
     return pk, sk
 def keygen(self, secparam=1024):
     while True:
         p, q = randomPrime(secparam), randomPrime(secparam)
         if isPrime(p) and isPrime(q) and gcd(p * q,
                                              (p - 1) * (q - 1)) == 1:
             break
     n = p * q
     g = n + 1
     n2 = n**2
     lam = lcm(p - 1, q - 1)
     u = (self.L(((g % n2)**lam), n) % n)**-1
     pk = {'n': n, 'g': g, 'n2': n2}
     sk = {'lam': lam, 'u': u}
     return pk, sk
예제 #4
0
    def hash(self, pk, sk, message, r=0):
        # generate ephemeral trapdoors(p1,q1)
        while True:
            p1, q1 = randomPrime(pk['secparam']), randomPrime(pk['secparam'])
            # print("p1,q1=>",p1,q1)
            if isPrime(p1) and isPrime(q1) and p1 != q1:
                N1 = p1 * q1
                if not gcd(N1, pk['N']) == 1:
                    continue
                break

        if r == 0:
            r = random(N1 * pk['N'])
        # print("r=>",r)
        # print("(p1,q1,N1)=>", (p1,q1,N1))
        # print("N*N1=>",N1 * pk['N'])
        phi_NN1 = pk['phi_N'] * (N1 - p1 - q1 + 1)
        # print("phi_NN1=>", phi_NN1)

        # find e inverse mod N1 * N, so gcd(e,phi_NN1)==1
        while True:
            e = random(phi_NN1)
            if not gcd(e, phi_NN1) == 1:
                continue
            break

        M = Conversion.bytes2integer(message)
        # print("M =>",M)

        # to set hash modular N * N1()
        group.q = N1 * pk['N']
        group.p = group.q * 2 + 1
        # print("q=>",group.q)
        # print("M hash=>", group.hash(M))

        h = (group.hash(M) * (r**e)) % (N1 * pk['N'])
        xi = {'h': h, 'r': r, 'N1': N1, 'p1': p1, 'q1': q1, 'e': e}
        # print("e=>",xi['e'])
        return xi