Esempio n. 1
0
    def __init__(self, l=DEFAULT_MSGSIZE):
        try:
            filepub = "Keys/pubkey" + str(DEFAULT_KEYSIZE) + ".txt"
            with open(filepub, 'r') as fin:
                data = [line.split() for line in fin]
            Np = int(data[0][0])
            pubkey = paillier.PaillierPublicKey(n=Np)

            filepriv = "Keys/privkey" + str(DEFAULT_KEYSIZE) + ".txt"
            with open(filepriv, 'r') as fin:
                data = [line.split() for line in fin]
            p = mpz(data[0][0])
            q = mpz(data[1][0])
            privkey = paillier.PaillierPrivateKey(pubkey, p, q)
            self.pubkey = pubkey
            self.privkey = privkey

        except:
            """If the files are not available, generate the keys """
            keypair = paillier.generate_paillier_keypair(
                n_length=DEFAULT_KEYSIZE)
            self.pubkey, self.privkey = keypair
            Np = self.pubkey.n
            file = 'Keys/pubkey' + str(DEFAULT_KEYSIZE) + ".txt"
            with open(file, 'w') as f:
                f.write("%d" % (self.pubkey.n))
            file = 'Keys/privkey' + str(DEFAULT_KEYSIZE) + ".txt"
            with open(file, 'w') as f:
                f.write("%d\n%d" % (self.privkey.p, self.privkey.q))
Esempio n. 2
0
    def __init__(self,
                 n,
                 m,
                 N,
                 T,
                 l=DEFAULT_MSGSIZE,
                 sigma=DEFAULT_SECURITYSIZE):
        self.l = l
        self.sigma = sigma
        filepub = "Keys/pubkey" + str(DEFAULT_KEYSIZE) + ".txt"
        with open(filepub, 'r') as fin:
            data = [line.split() for line in fin]
        Np = mpz(data[0][0])
        self.Np = Np
        pubkey = paillier.PaillierPublicKey(n=Np)
        self.pubkey = pubkey
        self.N_len = Np.bit_length()
        fileH = "Data/H" + str(n) + "_" + str(m) + "_" + str(N) + ".txt"
        H = np.loadtxt(fileH, delimiter=',')
        fileF = "Data/F" + str(n) + "_" + str(m) + "_" + str(N) + ".txt"
        F = np.loadtxt(fileF, delimiter=',')
        fileG0 = "Data/G0" + str(n) + "_" + str(m) + "_" + str(N) + ".txt"
        G0 = np.loadtxt(fileG0, delimiter=',')
        fileK = "Data/K" + str(n) + "_" + str(m) + "_" + str(N) + ".txt"
        K = np.loadtxt(fileK, delimiter=',')
        Kc = K[0]
        Kw = K[1]
        self.Kc = int(Kc)
        self.Kw = int(Kw)
        self.T = T
        self.m = m
        nc = m * N
        self.nc = nc
        Hq = Q_matrix(H)
        eigs = np.linalg.eigvals(Hq)
        L = np.real(max(eigs))
        mu = np.real(min(eigs))
        cond = Q_s(L / mu)
        eta = Q_s((np.sqrt(cond) - 1) / (np.sqrt(cond) + 1))
        Hf = Q_matrix([[h / Q_s(L) for h in hv] for hv in Hq])
        Ft = F.transpose()
        Ff = Q_matrix([[Q_s(h) / Q_s(L) for h in hv] for hv in Ft])
        self.eta = eta
        self.Hf = Hf
        mFf = np.negative(Ff)
        self.mFft = fp_matrix(mFf, 2 * DEFAULT_PRECISION)

        coeff_z = np.eye(nc) - Hf
        self.coeff_z = fp_matrix(coeff_z)
Esempio n. 3
0
    def __init__(self, l=DEFAULT_MSGSIZE):
        """This would generate the keys on the spot"""
        # keypair = paillier.generate_paillier_keypair(n_length=DEFAULT_KEYSIZE)
        # self.pubkey, self.privkey = keypair
        # file = 'Keys/pubkey'+str(DEFAULT_KEYSIZE)+".txt"
        # with open(file, 'w') as f:
        # 	f.write("%d" % (self.pubkey.n))
        # file = 'Keys/privkey'+str(DEFAULT_KEYSIZE)+".txt"
        # with open(file, 'w') as f:
        # 	f.write("%d\n%d" % (self.privkey.p,self.privkey.q))
        filepub = "Keys/pubkey" + str(DEFAULT_KEYSIZE) + ".txt"
        with open(filepub, 'r') as fin:
            data = [line.split() for line in fin]
        Np = mpz(data[0][0])
        pubkey = paillier.PaillierPublicKey(n=Np)
        self.pubkey = pubkey

        filepriv = "Keys/privkey" + str(DEFAULT_KEYSIZE) + ".txt"
        with open(filepriv, 'r') as fin:
            data = [line.split() for line in fin]
        p = mpz(data[0][0])
        q = mpz(data[1][0])
        self.privkey = paillier.PaillierPrivateKey(pubkey, p, q)
Esempio n. 4
0
def main():
    parser = argparse.ArgumentParser()
    parser.description = 'Run an MPC node for majority judgment'
    parser.add_argument('host', nargs='?', default='localhost')
    parser.add_argument('port', nargs='?', default=4242, type=int)
    parser.add_argument('--honest', action='store_true')
    args = parser.parse_args()

    print('Connecting to {}:{}'.format(args.host, args.port))
    server = network.MessageSocket.connect((args.host, args.port))
    setup = server.receive_json()

    n_choices = setup['n_choices']
    n_candidates = setup['n_candidates']
    n_bits = setup['n_bits']

    # key setup
    pk = paillier.PaillierPublicKey(setup['n'], setup['g'])
    pk_shares = [
        paillier.PaillierPublicKeyShare(pk, setup['verification_base'], pk_share)
        for pk_share in setup['pk_shares']
    ]
    sk_share = paillier.PaillierSecretKeyShare(pk, setup['verification_base'], setup['sk_share'])
    if args.honest:
        protocols = HonestSharedPaillierClientProtocols(sk_share, server)
    else:
        protocols = SharedPaillierClientProtocols(pk_shares, sk_share, server)

        # pre-computations for proofs
        n_random_negate = (
            (n_candidates * n_choices + 2 * n_candidates) * (n_bits - 1) +  # lsbs
            ((n_choices-1)*n_candidates + n_candidates*n_candidates) * (2*n_bits-1) +  # gt_gate
            (n_choices-1) * (n_candidates-1) +  # big_and
            2*n_candidates*(n_choices-1) + 7*n_candidates*(n_candidates-1)  # and_gate
        )
        randoms = [random.choice([-1, 1]) for _ in range(n_random_negate)]
        pk.precompute_proofs(randoms)
        n_batched_decryptions = 4*n_bits + (n_candidates-1).bit_length() + 6
        sk_share.precompute_proofs(n_batched_decryptions)
        print('Pre-computations done')

    election = majorityjudgment.MPCMajorityJudgment(pk, protocols, n_choices, n_candidates, n_bits)
    election.random_bits = [
        paillier.PaillierCiphertext(pk, x)
        for x in setup['random_bits']
    ]
    election.random_ints = [
        paillier.PaillierCiphertext(pk, x)
        for x in setup['random_ints']
    ]
    server.send_json('READY')
    print('Ready to run the election')

    # retrieve A
    A = server.receive_json()
    A = [
        [paillier.PaillierCiphertext(pk, x) for x in row]
        for row in A
    ]

    # run the election
    print('Here we go!')
    start = datetime.datetime.now()
    election.run(A)
    elapsed = datetime.datetime.now() - start
    print('Finished in {}'.format(elapsed))

    if hasattr(pk, 'precomputed_values'):
        assert not pk.precomputed_values
    if hasattr(sk_share, 'precomputed_values'):
        assert not sk_share.precomputed_values