예제 #1
0
 def test_generate_e_generates_e_of_right_length(self):
     s, prover_id, k = 9, 1, 0
     c = [1, 1, 0, 0, 1, 0, 1, 0, 1]
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.d = [1, 0, 0, 1, 1, 0, 1, 1, 1]
     zk._generate_e()
     self.assertEquals(s, len(zk.e))
예제 #2
0
파일: share.py 프로젝트: hanyunx/Viff
    def do_zk_proofs(list_of_enc_shares, field_elements):
        zk_results = []
        list_of_enc_shares = [eval(x) for x in list_of_enc_shares]

        # We expect all players to broadcast the same number of
        # encrypted shares.
        assert all([
            len(enc_shares) == len(list_of_enc_shares[0])
            for enc_shares in list_of_enc_shares
        ])

        for i in range(runtime.num_players):
            x, r = None, None
            if runtime.id == i + 1:
                x, r = [mpz(e.value)
                        for e in field_elements], list_of_random_elements
            zk_proof = ZKProof(len(field_elements),
                               i + 1,
                               k,
                               runtime,
                               list_of_enc_shares[i],
                               random=random,
                               x=x,
                               r=r,
                               paillier=paillier)
            zk_result = zk_proof.start()
            zk_results.append(zk_result)
        d = gatherResults(zk_results)
        runtime.schedule_callback(d, construct_partial_shares,
                                  list_of_enc_shares, field_elements)
        return d
예제 #3
0
 def test_generate_e_generates_e_of_right_length(self):
     s, prover_id, k = 9, 1, 0
     c = [1, 1, 0, 0, 1, 0, 1, 0, 1]
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.d = [1, 0, 0, 1, 1, 0, 1, 1, 1]
     zk._generate_e()
     self.assertEquals(s, len(zk.e))
예제 #4
0
 def test_vec_pow_is_correct(self):
     s, prover_id, k = 5, 1, 0
     c = [None] * s
     y = [mpz(i) for i in range(1, 6)]
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.e = [1, 0, 1, 1, 0]
     y_pow_E = zk._vec_pow_E(y, 117)
     self.assertEquals([mpz(v) for v in [1, 2, 3, 8, 30, 12, 20, 5, 1]], y_pow_E)
예제 #5
0
 def test_vec_pow_is_correct_2(self):
     s, k, prover_id = 3, 0, 1
     c = [None] * s
     y = [mpz(i) for i in [1, 7, 2]]
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.e = [0, 1, 1]
     y_pow_E = zk._vec_pow_E(y, 117)
     self.assertEquals([mpz(v) for v in [1, 1, 7, 14, 2]], y_pow_E)
예제 #6
0
 def test_vec_mul_E_is_correct_2(self):
     s, k, prover_id = 3, 0, 1
     c = [None] * s
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.e = [0, 1, 1]
     x = [2, -3, 0]
     x_mul_E = zk._vec_mul_E(x)
     self.assertEquals([v for v in [0, 2, -1, -3, 0]], x_mul_E)
예제 #7
0
 def test_vec_pow_is_correct_2(self):
     s, k, prover_id = 3, 0, 1
     c = [None] * s
     y = [mpz(i) for i in [1, 7, 2]]
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.e = [0, 1, 1]
     y_pow_E = zk._vec_pow_E(y, 117)
     self.assertEquals([mpz(v) for v in [1, 1, 7, 14, 2]], y_pow_E)
예제 #8
0
 def test_vec_mul_E_is_correct_2(self):
     s, k, prover_id = 3, 0, 1
     c = [None] * s
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.e = [0, 1, 1]
     x = [2, -3, 0]
     x_mul_E = zk._vec_mul_E(x)
     self.assertEquals([v for v in [0, 2, -1, -3, 0]], x_mul_E)
예제 #9
0
 def test_vec_mul_E_is_correct(self):
     s, prover_id, k, Zn = 5, 1, 0, GF(17)
     c = [None] * s
     y = [Zn(i) for i in range(1, 6)]
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.e = [1, 0, 1, 1, 0]
     x = [1, 2, 0, 1, 0]
     x_mul_E = zk._vec_mul_E(x)
     self.assertEquals([v for v in [1, 2, 1, 4, 2, 1, 1, 0, 0]], x_mul_E)
예제 #10
0
 def test_vec_mul_E_is_correct(self):
     s, prover_id, k, Zn = 5, 1, 0, GF(17)
     c = [None] * s
     y = [Zn(i) for i in range(1, 6)]
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.e = [1, 0, 1, 1, 0]
     x = [1, 2, 0, 1, 0]
     x_mul_E = zk._vec_mul_E(x)
     self.assertEquals([v for v in [1, 2, 1, 4, 2, 1, 1, 0, 0]], x_mul_E)
예제 #11
0
 def test_vec_pow_is_correct(self):
     s, prover_id, k = 5, 1, 0
     c = [None] * s
     y = [mpz(i) for i in range(1, 6)]
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.e = [1, 0, 1, 1, 0]
     y_pow_E = zk._vec_pow_E(y, 117)
     self.assertEquals([mpz(v) for v in [1, 2, 3, 8, 30, 12, 20, 5, 1]],
                       y_pow_E)
예제 #12
0
    def test_broadcast(self, runtime):
        s, k, prover_id = 0, 2, 1
        c = []
        zk = ZKProof(s, prover_id, k, runtime, c)
        res = zk._broadcast([5, 6, 7])

        def verify(res):
            self.assertEquals(eval(res), [5, 6, 7])

        runtime.schedule_callback(res, verify)
        return res
예제 #13
0
 def test_zk_matrix_entries_are_correct(self):
     s, k, prover_id = 5, 1, 1
     c = [None] * s
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.e = [1, 0, 0, 1, 1]
     for i in range(zk.s):
         for j in range(zk.m):
             if j >= i and j < i + zk.s:
                 self.assertEquals(zk.e[j - i], zk._E(j, i))
             else:
                 self.assertEquals(0, zk._E(j, i))
예제 #14
0
    def test_broadcast(self, runtime):
        s, k, prover_id = 0, 2, 1
        c = []
        zk = ZKProof(s, prover_id, k, runtime, c)
        res = zk._broadcast([5, 6, 7])

        def verify(res):
            self.assertEquals(eval(res), [5, 6, 7])

        runtime.schedule_callback(res, verify)
        return res
예제 #15
0
 def test_zk_matrix_entries_are_correct(self):
     s, k, prover_id = 5, 1, 1
     c = [None] * s
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.e = [1, 0, 0, 1, 1]
     for i in range(zk.s):
         for j in range(zk.m):
             if j >= i and j < i + zk.s:
                 self.assertEquals(zk.e[j - i], zk._E(j, i))
             else:
                 self.assertEquals(0, zk._E(j, i))
예제 #16
0
 def test_generate_Z_and_W_is_correct(self, runtime):
     s, prover_id, k = 3, 1, 0
     c = [None] * s
     zk = ZKProof(s, prover_id, k, runtime, c)
     zk.u = [1, -2, 0, 6, -3]
     zk.v = [3, 5, 2, 1, 7]
     zk.x = [2, -3, 0]
     zk.r = [1, 7, 2]
     zk.e = [0, 1, 1]
     zk._generate_Z_and_W()
     self.assertEquals([1, 0, -1, 3, -3], zk.Z)
     self.assertEquals([3, 5, 14, 14, 14], zk.W)
예제 #17
0
 def test_extract_bits(self):
     s, k, prover_id = 5, 1, 1
     c = [None] * s
     runtime = RuntimeStub()
     zk = ZKProof(s, prover_id, k, runtime, c)
     self.assertEquals([], zk._extract_bits('test', 0))
     self.assertEquals([0], zk._extract_bits('test', 1))
     self.assertEquals([0, 1], zk._extract_bits('test', 2))
     self.assertEquals([0, 1, 1, 1, 0, 1, 0], zk._extract_bits('test', 7))
     self.assertEquals([0, 1, 1, 1, 0, 1, 0, 0],
                       zk._extract_bits('test', 8))
     self.assertEquals([0, 1, 1, 1, 0, 1, 0, 0, 0],
                       zk._extract_bits('test', 9))
     self.assertEquals([0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1],
                       zk._extract_bits('test', 14))
예제 #18
0
 def test_generate_Z_and_W_is_correct(self, runtime):
     s, prover_id, k = 3, 1, 0
     c = [None] * s
     zk = ZKProof(s, prover_id, k, runtime, c)
     zk.u = [1, -2, 0, 6, -3]
     zk.v = [3, 5, 2, 1, 7]
     zk.x = [2, -3, 0]
     zk.r = [1, 7, 2]
     zk.e = [0, 1, 1]
     zk._generate_Z_and_W()
     self.assertEquals([1, 0, -1, 3, -3], zk.Z)
     self.assertEquals([3, 5, 14, 14, 14], zk.W)
예제 #19
0
 def test_extract_bits(self):
     s, k, prover_id = 5, 1, 1
     c = [None] * s
     runtime = RuntimeStub()
     zk = ZKProof(s, prover_id, k, runtime, c)
     self.assertEquals([], zk._extract_bits("test", 0))
     self.assertEquals([0], zk._extract_bits("test", 1))
     self.assertEquals([0, 1], zk._extract_bits("test", 2))
     self.assertEquals([0, 1, 1, 1, 0, 1, 0], zk._extract_bits("test", 7))
     self.assertEquals([0, 1, 1, 1, 0, 1, 0, 0], zk._extract_bits("test", 8))
     self.assertEquals([0, 1, 1, 1, 0, 1, 0, 0, 0], zk._extract_bits("test", 9))
     self.assertEquals([0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1], zk._extract_bits("test", 14))
예제 #20
0
 def test_generate_e_is_deterministic(self):
     s, prover_id, k = 9, 1, 0
     c = [1, 1, 0, 0, 1, 0, 1, 0, 1]
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.d = [1, 0, 0, 1, 1, 0, 1, 1, 1]
     zk._generate_e()
     e1 = zk.e
     zk._generate_e()
     self.assertEquals(e1, zk.e)
예제 #21
0
    def test_succeeding_proof(self, runtime):
        seed = 2348838
        k, s, prover_id = 5, 3, 1
        player_random = Random(seed + runtime.id)
        shared_random = Random(seed)
        paillier = ModifiedPaillier(runtime, Random(player_random.getrandbits(128)))
        x, r, c = self._generate_test_ciphertexts(shared_random, runtime, k, s, prover_id)
        # print "Player", runtime.id, " x =", x
        # print "Player", runtime.id, " r =", r
        # print "Player", runtime.id, " c =", c
        if runtime.id == prover_id:
            zk = ZKProof(s, prover_id, k, runtime, c, paillier=paillier, random=player_random, x=x, r=r)
        else:
            zk = ZKProof(s, prover_id, k, runtime, c, paillier=paillier, random=player_random)

        deferred_proof = zk.start()

        def verify(result):
            self.assertTrue(result)

        runtime.schedule_callback(deferred_proof, verify)
        return deferred_proof
예제 #22
0
    def test_succeeding_proof(self, runtime):
        seed = 2348838
        k, s, prover_id = 5, 3, 1
        player_random = Random(seed + runtime.id)
        shared_random = Random(seed)
        paillier = ModifiedPaillier(runtime,
                                    Random(player_random.getrandbits(128)))
        x, r, c = self._generate_test_ciphertexts(shared_random, runtime, k, s,
                                                  prover_id)
        #print "Player", runtime.id, " x =", x
        #print "Player", runtime.id, " r =", r
        #print "Player", runtime.id, " c =", c
        if runtime.id == prover_id:
            zk = ZKProof(s,
                         prover_id,
                         k,
                         runtime,
                         c,
                         paillier=paillier,
                         random=player_random,
                         x=x,
                         r=r)
        else:
            zk = ZKProof(s,
                         prover_id,
                         k,
                         runtime,
                         c,
                         paillier=paillier,
                         random=player_random)

        deferred_proof = zk.start()

        def verify(result):
            self.assertTrue(result)

        runtime.schedule_callback(deferred_proof, verify)
        return deferred_proof
예제 #23
0
파일: share.py 프로젝트: MaxFangX/viff
    def do_zk_proofs(list_of_enc_shares, field_elements):
        zk_results = []
        list_of_enc_shares = [eval(x) for x in list_of_enc_shares]

        # We expect all players to broadcast the same number of
        # encrypted shares.
        assert all([len(enc_shares) == len(list_of_enc_shares[0]) 
                    for enc_shares in list_of_enc_shares])

        for i in range(runtime.num_players):
            x, r = None, None
            if runtime.id == i + 1:
                x, r = [mpz(e.value) 
                        for e in field_elements], list_of_random_elements
            zk_proof = ZKProof(
                len(field_elements), i + 1, k, runtime, list_of_enc_shares[i],
                random=random, x=x, r=r, paillier=paillier)
            zk_result = zk_proof.start()
            zk_results.append(zk_result)
        d = gatherResults(zk_results)
        runtime.schedule_callback(
            d, construct_partial_shares, list_of_enc_shares, field_elements)
        return d
예제 #24
0
 def test_generate_e_is_deterministic(self):
     s, prover_id, k = 9, 1, 0
     c = [1, 1, 0, 0, 1, 0, 1, 0, 1]
     zk = ZKProof(s, prover_id, k, RuntimeStub(), c)
     zk.d = [1, 0, 0, 1, 1, 0, 1, 1, 1]
     zk._generate_e()
     e1 = zk.e
     zk._generate_e()
     self.assertEquals(e1, zk.e)