def test_basic_multiply_constant_left(self, runtime): """Test multiplication of two numbers.""" x1 = 6 y1 = 6 def check(v): self.assertEquals(v, self.Zp(x1 * y1)) def do_stuff(triple, alpha): random = Random(3423993) share_random = Random(random.getrandbits(128)) paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128))) gen = TestShareGenerator(self.Zp, runtime, share_random, paillier, self.u_bound, alpha) x2 = gen.generate_share(x1) y2 = gen.generate_share(y1) z2 = runtime._basic_multiplication(self.Zp(x1), y2, triple.a, triple.b, triple.c) d = runtime.open(z2) d.addCallback(check) return d gen = TripleGenerator(runtime, self.security_parameter, self.Zp.modulus, Random(3423993)) alpha = gen.alpha [triple] = gen._generate_triples(1) runtime.schedule_callback(triple, do_stuff, alpha) return triple
def test_generate_triples_generates_correct_triples(self, runtime): p = 17 Zp = GF(p) random = Random(574566 + runtime.id) triple_generator = TripleGenerator(runtime, self.security_parameter, p, random) triples = triple_generator._generate_triples(10) def check((a, b, c)): self.assertEquals(c, a * b) def open(triple): d1 = runtime.open(triple.a) d2 = runtime.open(triple.b) d3 = runtime.open(triple.c) d = gatherResults([d1, d2, d3]) runtime.schedule_callback(d, check) return d for triple in triples: runtime.schedule_callback(triple, open) return gatherResults(triples)
def test_mul_mul(self, runtime): """Test multiplication of two numbers.""" x1 = 6 y1 = 6 def check(v): self.assertEquals(v, self.Zp(x1 * y1)) gen = TripleGenerator(runtime, self.security_parameter, self.Zp.modulus, Random(3423993)) alpha = gen.alpha triples = gen._generate_triples(1) def do_mult(triples, alpha): runtime.triples = triples random = Random(3423993) share_random = Random(random.getrandbits(128)) paillier = ModifiedPaillier(runtime, Random(random.getrandbits(128))) gen = TestShareGenerator(self.Zp, runtime, share_random, paillier, self.u_bound, alpha) x2 = gen.generate_share(x1) y2 = gen.generate_share(y1) z2 = x2 * y2 d = runtime.open(z2) d.addCallback(check) return d r = gatherResults(triples) runtime.schedule_callback(r, do_mult, alpha) return r
def test_mul_same_player_inputs_and_receives(self, runtime): p = 17 random = Random(283883) triple_generator = TripleGenerator(runtime, self.security_parameter, p, random) Zp = GF(p) ais = [Zp(6), Zp(6), Zp(6), Zp(6)] b2 = Zp(7) cs = [] for ai in ais: cs.append(triple_generator.paillier.encrypt(b2.value, 2)) n = len(ais) r1 = triple_generator._mul(2, 2, n, ais, cs) def check(shares): for share in shares: if runtime.id == 2: self.assertEquals(share.value, 8) return True r1.addCallback(check) return r1
def test_fullmul_encrypted_values_are_the_same_as_the_share(self, runtime): p = 17 Zp = GF(p) random = Random(283883) triple_generator = TripleGenerator(runtime, self.security_parameter, p, random) paillier = triple_generator.paillier share_as = [] share_bs = [] share_as.append( partial_share(random, runtime, GF(p), 6, paillier=paillier)) share_bs.append( partial_share(random, runtime, GF(p), 7, paillier=paillier)) share_as.append( partial_share(random, runtime, GF(p), 5, paillier=paillier)) share_bs.append( partial_share(random, runtime, GF(p), 4, paillier=paillier)) share_as.append( partial_share(random, runtime, GF(p), 2, paillier=paillier)) share_bs.append( partial_share(random, runtime, GF(p), 3, paillier=paillier)) share_zs = triple_generator._full_mul(share_as, share_bs) def check(shares): all_enc_shares = [] for share in shares: def test_enc(enc_shares, value): all_the_same, zi_enc = reduce( lambda x, y: (x[0] and x[1] == y, y), enc_shares, (True, enc_shares[0])) zi_enc = triple_generator.paillier.decrypt(zi_enc) self.assertEquals(value, Zp(zi_enc)) return True for inx, enc_share in enumerate(share.enc_shares): d = _convolute(runtime, enc_share) if runtime.id == inx + 1: d.addCallback(test_enc, share.value) all_enc_shares.append(d) return gatherResults(all_enc_shares) d = gatherResults(share_zs) d.addCallback(check) return d
def test_mul_computes_correct_result(self, runtime): p = 17 random = Random(283883) triple_generator = TripleGenerator(runtime, 32, p, random) Zp = GF(p) ais = [Zp(6), Zp(6), Zp(6), Zp(6)] b2 = Zp(7) cs = [] for ai in ais: cs.append(triple_generator.paillier.encrypt(b2.value, 2)) n = len(ais) if runtime.id == 1: r1 = triple_generator._mul(1, 2, n, ais, cs) def check1(shares): for share in shares: pc = tuple(runtime.program_counter) runtime.protocols[2].sendData(pc, TEXT, str(share.value)) return True r1.addCallback(check1) return r1 else: r1 = triple_generator._mul(1, 2, n) def check(shares): deferreds = [] for share in shares: if runtime.id == 2: def check_additivity(zi, zj): self.assertEquals((Zp(long(zi)) + zj).value, 8) return None d = Deferred() d.addCallback(check_additivity, share.value) runtime._expect_data(1, TEXT, d) deferreds.append(d) else: self.assertEquals(share.value, 0) return gatherResults(deferreds) r1.addCallback(check) return r1
def test_fullmul_computes_the_correct_result(self, runtime): p = 17 Zp = GF(p) random = Random(283883) triple_generator = TripleGenerator(runtime, self.security_parameter, p, random) paillier = triple_generator.paillier share_as = [] share_bs = [] share_as.append( partial_share(random, runtime, GF(p), 6, paillier=paillier)) share_bs.append( partial_share(random, runtime, GF(p), 7, paillier=paillier)) share_as.append( partial_share(random, runtime, GF(p), 5, paillier=paillier)) share_bs.append( partial_share(random, runtime, GF(p), 4, paillier=paillier)) share_as.append( partial_share(random, runtime, GF(p), 2, paillier=paillier)) share_bs.append( partial_share(random, runtime, GF(p), 3, paillier=paillier)) share_zs = triple_generator._full_mul(share_as, share_bs) def check(shares): def test_sum(ls): self.assertEquals(8, Zp(sum(ls[0]))) self.assertEquals(3, Zp(sum(ls[1]))) self.assertEquals(6, Zp(sum(ls[2]))) values = [] for share in shares: value = _convolute(runtime, share.value.value) values.append(value) d = gatherResults(values) runtime.schedule_callback(d, test_sum) return d d = gatherResults(share_zs) d.addCallback(check) return d
def test_generate_triple_candidates_generates_correct_triples(self, runtime): p = 17 Zp = GF(p) random = Random(283883) triple_generator = TripleGenerator(runtime, self.security_parameter, p, random) triples = triple_generator._generate_triple_candidates(5) def verify(triples): for inx in xrange(len(triples) // 3): self.assertEquals(triples[10 + inx], triples[inx] * triples[5 + inx]) opened_shares = [] for s in triples: opened_shares.append(runtime.open(s)) d = gather_shares(opened_shares) d.addCallback(verify) return d
def test_get_triple(self, runtime): """Test generation of a triple.""" def check((a, b, c)): self.assertEquals(c, a * b) def open(triple): d1 = runtime.open(triple.a) d2 = runtime.open(triple.b) d3 = runtime.open(triple.c) d = gather_shares([d1, d2, d3]) d.addCallback(check) return d random = Random(3423993) gen = TripleGenerator(runtime, self.security_parameter, self.Zp.modulus, random) [triple] = gen._generate_triples(1) triple.addCallback(open) return triple
def test_generate_triples_generates_correct_single_triple(self, runtime): p = 17 Zp = GF(p) random = Random(574566 + runtime.id) triple_generator = TripleGenerator(runtime, self.security_parameter, p, random) triples = triple_generator._generate_triples(1) def check((a, b, c)): self.assertEquals(c, a * b) def open(triple): d1 = runtime.open(triple.a) d2 = runtime.open(triple.b) d3 = runtime.open(triple.c) d = gatherResults([d1, d2, d3]) runtime.schedule_callback(d, check) return d for triple in triples: runtime.schedule_callback(triple, open) return gatherResults(triples)
def test_fullmul_encrypted_values_are_the_same_as_the_share(self, runtime): p = 17 Zp = GF(p) random = Random(283883) triple_generator = TripleGenerator(runtime, self.security_parameter, p, random) paillier = triple_generator.paillier share_as = [] share_bs = [] share_as.append(partial_share(random, runtime, GF(p), 6, paillier=paillier)) share_bs.append(partial_share(random, runtime, GF(p), 7, paillier=paillier)) share_as.append(partial_share(random, runtime, GF(p), 5, paillier=paillier)) share_bs.append(partial_share(random, runtime, GF(p), 4, paillier=paillier)) share_as.append(partial_share(random, runtime, GF(p), 2, paillier=paillier)) share_bs.append(partial_share(random, runtime, GF(p), 3, paillier=paillier)) share_zs = triple_generator._full_mul(share_as, share_bs) def check(shares): all_enc_shares = [] for share in shares: def test_enc(enc_shares, value): all_the_same, zi_enc = reduce(lambda x, y: (x[0] and x[1] == y, y), enc_shares, (True, enc_shares[0])) zi_enc = triple_generator.paillier.decrypt(zi_enc) self.assertEquals(value, Zp(zi_enc)) return True for inx, enc_share in enumerate(share.enc_shares): d = _convolute(runtime, enc_share) if runtime.id == inx + 1: d.addCallback(test_enc, share.value) all_enc_shares.append(d) return gatherResults(all_enc_shares) d = gatherResults(share_zs) d.addCallback(check) return d
def test_generate_triple_candidates_generates_correct_triples( self, runtime): p = 17 Zp = GF(p) random = Random(283883) triple_generator = TripleGenerator(runtime, self.security_parameter, p, random) triples = triple_generator._generate_triple_candidates(5) def verify(triples): for inx in xrange(len(triples) // 3): self.assertEquals(triples[10 + inx], triples[inx] * triples[5 + inx]) opened_shares = [] for s in triples: opened_shares.append(runtime.open(s)) d = gather_shares(opened_shares) d.addCallback(verify) return d
def test_fullmul_computes_the_correct_result(self, runtime): p = 17 Zp = GF(p) random = Random(283883) triple_generator = TripleGenerator(runtime, self.security_parameter, p, random) paillier = triple_generator.paillier share_as = [] share_bs = [] share_as.append(partial_share(random, runtime, GF(p), 6, paillier=paillier)) share_bs.append(partial_share(random, runtime, GF(p), 7, paillier=paillier)) share_as.append(partial_share(random, runtime, GF(p), 5, paillier=paillier)) share_bs.append(partial_share(random, runtime, GF(p), 4, paillier=paillier)) share_as.append(partial_share(random, runtime, GF(p), 2, paillier=paillier)) share_bs.append(partial_share(random, runtime, GF(p), 3, paillier=paillier)) share_zs = triple_generator._full_mul(share_as, share_bs) def check(shares): def test_sum(ls): self.assertEquals(8, Zp(sum(ls[0]))) self.assertEquals(3, Zp(sum(ls[1]))) self.assertEquals(6, Zp(sum(ls[2]))) values = [] for share in shares: value = _convolute(runtime, share.value.value) values.append(value) d = gatherResults(values) runtime.schedule_callback(d, test_sum) return d d = gatherResults(share_zs) d.addCallback(check) return d