Пример #1
0
    def testSecProtocol(self):
        pub, priv = PaillierKeypair().generate_keypair()
        pub2 = AsyncPaillierPublicKey(pub)
        stat = defaultdict(float)
        for i in range(1000):
            # r = 0 is not compatible
            r = i + 1
            value = random.uniform(-1 * 2 << 40, 2 << 40)
            start = time.time()
            expected = pub.encrypt(value, random_value=r).ciphertext(False)
            stat["old_encrypt"] += time.time() - start
            start = time.time()
            actual = pub2.encrypt(value, random_value=r).ciphertext(False)
            stat["new_encrypt"] += time.time() - start

            self.assertEqual(expected, actual)
            start = time.time()
            encoding = pub2.encode(value).encoding
            stat["new_encrypt:encode"] += time.time() - start
            start = time.time()
            cipher_text = pub2.raw_encrypt(encoding)
            stat["new_encrypt:raw_encrypt"] += time.time() - start
            start = time.time()
            obf = pub2.gen_obfuscator(r)
            stat["new_encrypt:gen_obfuscator"] += time.time() - start
            start = time.time()
            actual = pub2.apply_obfuscator(cipher_text, obf=obf)
            stat["new_encrypt:apply_obfuscator"] += time.time() - start
            self.assertEqual(expected, actual)
        print("======stat_time=====")
        for k, v in stat.items():
            print(f"{k}\t{v}")
Пример #2
0
    def __init__(self,
                 name="ss",
                 q_field=None,
                 local_party=None,
                 all_parties=None,
                 use_mix_rand=False,
                 n_length=1024):
        self.name_service = naming.NamingService(name)
        self._prev_name_service = None
        self._pre_instance = None

        self.communicator = Communicator(local_party, all_parties)

        self.party_idx = self.communicator.party_idx
        self.other_parties = self.communicator.other_parties
        if len(self.other_parties) > 1:
            raise EnvironmentError("support 2-party secret share only")
        self.public_key, self.private_key = PaillierKeypair.generate_keypair(
            n_length=n_length)

        if q_field is None:
            q_field = self.public_key.n

        self.q_field = self._align_q_field(q_field)

        self.use_mix_rand = use_mix_rand
Пример #3
0
    def __init__(self, name="ss", q_field=2 << 60, local_party=None, all_parties=None):
        self.name_service = naming.NamingService(name)
        self._prev_name_service = None
        self._pre_instance = None

        self.communicator = Communicator(local_party, all_parties)

        self.party_idx = self.communicator.party_idx
        self.other_parties = self.communicator.other_parties
        if len(self.other_parties) > 1:
            raise EnvironmentError("support 2-party secret share only")
        self.public_key, self.private_key = PaillierKeypair.generate_keypair(1024)
        self.q_field = q_field
Пример #4
0
 def testNpTensor(self):
     pub, priv = PaillierKeypair().generate_keypair()
     pub2 = AsyncPaillierPublicKey(pub)
     np1 = np.array([[-5.457078], [-7.832643], [-14.418312], [-2.831291]])
     np2 = np.array([[6.18417921], [44.85495244], [124.03486276],
                     [22.37115939]])
     np1 = np2
     na1 = NumpyTensor(np1, pub2)
     obfs = []
     for i in range(np1.size):
         obfs.append(pub2.gen_obfuscator())
     obfs = np.array(obfs).reshape(np1.shape)
     ea1 = encrypt_and_obfuscate(np1, pub2, obfs=obfs)
     print("aa23", decryptdecode(ea1, pub2, priv))
     pa1 = na1.encrypt()
     pa1.decrypt(priv).out(priv, "aa22")
Пример #5
0
 def generate_key(self, n_length=1024):
     self.public_key, self.privacy_key = \
         PaillierKeypair.generate_keypair(n_length=n_length)
Пример #6
0
def keygen():
    pub, priv = PaillierKeypair().generate_keypair()
    return AsyncPaillierPublicKey(pub), priv
Пример #7
0
 def setUp(self):
     self.public_key, self.private_key = PaillierKeypair.generate_keypair()
Пример #8
0
 def test_generate_keypair(self):
     public_key, private_key = PaillierKeypair.generate_keypair()
     self.assertTrue(isinstance(public_key, PaillierPublicKey))
     self.assertTrue(isinstance(private_key, PaillierPrivateKey))
     self.assertEqual(private_key.public_key, public_key)
Пример #9
0
def keygen():
    pub, priv = PaillierKeypair().generate_keypair()
    paillier_gpu.init_gpu_keys(pub, priv)

    return pub, priv