view viff/test/test_orlandi_runtime.py @ 1432:da995056d761
test_orlandi: Fixed bug.
| author |
Janus Dam Nielsen <janus.nielsen@alexandra.dk> |
| date |
Tue Feb 02 17:21:20 2010 +0100 (2 years ago) |
| parents |
efa1983063d6 |
| children |
6caded57e489 |
line source
1 # Copyright 2009 VIFF Development Team.
3 # This file is part of VIFF, the Virtual Ideal Functionality Framework.
5 # VIFF is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License (LGPL) as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
10 # VIFF is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 # Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
18 from twisted.internet.defer import gatherResults, DeferredList
20 from viff.test.util import RuntimeTestCase, protocol
21 from viff.runtime import gather_shares, Share
22 from viff.paillierutil import NaClPaillier
23 from viff.config import generate_configs
29 from pypaillier import encrypt_r, decrypt, tripple_2c, tripple_3a
30 from viff.orlandi import OrlandiRuntime, OrlandiShare
31 pypaillier = "Imported"
42 from viff.field import FieldElement, GF
44 from viff.util import rand
47 def _get_triple(runtime, field):
49 Ca = commitment.commit(6, 0, 0)
50 a = OrlandiShare(runtime, field, field(2), (n, n), Ca)
51 Cb = commitment.commit(12, 0, 0)
52 b = OrlandiShare(runtime, field, field(4), (n, n), Cb)
53 Cc = commitment.commit(72, 0, 0)
54 c = OrlandiShare(runtime, field, field(24), (n, n), Cc)
58 class OrlandiBasicCommandsTest(RuntimeTestCase):
59 """Test for basic commands."""
64 runtime_class = OrlandiRuntime
67 def test_secret_share(self, runtime):
68 """Test sharing of random numbers."""
70 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
72 def check((xi, (rho1, rho2), Cr)):
73 # Check that we got the expected number of shares.
74 self.assert_type(xi, FieldElement)
75 self.assert_type(rho1, FieldElement)
76 self.assert_type(rho2, FieldElement)
77 self.assert_type(Cr, commitment.Commitment)
80 share = runtime.secret_share([1], self.Zp, 42)
82 share = runtime.secret_share([1], self.Zp)
83 share.addCallback(check)
87 def test_open_secret_share(self, runtime):
88 """Test sharing and open of a number."""
90 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
93 self.assertEquals(v, 42)
96 x = runtime.secret_share([1], self.Zp, 42)
98 x = runtime.secret_share([1], self.Zp)
104 def test_open_multiple_secret_share(self, runtime):
105 """Test sharing and open of a number."""
107 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
110 for inx, v in enumerate(ls):
111 self.assertEquals(v, (inx + 1) * 42)
114 x = runtime.secret_share([1], self.Zp, 42)
116 x = runtime.secret_share([1], self.Zp)
118 y = runtime.secret_share([1], self.Zp, 84)
120 y = runtime.secret_share([1], self.Zp)
121 d = runtime.open_multiple_values([x, y])
126 def test_random_share(self, runtime):
127 """Test creation of a random shared number."""
129 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
132 self.assertEquals(True, True)
134 x = runtime.random_share(self.Zp)
140 def test_sum(self, runtime):
141 """Test addition of two numbers."""
143 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
149 self.assertEquals(v, x1 + y1)
152 x2 = runtime.secret_share([1], self.Zp, x1)
154 x2 = runtime.secret_share([1], self.Zp)
157 y2 = runtime.secret_share([3], self.Zp, y1)
159 y2 = runtime.secret_share([3], self.Zp)
161 z2 = runtime.add(x2, y2)
167 def test_sum_plus(self, runtime):
168 """Test addition of two numbers."""
170 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
176 self.assertEquals(v, x1 + y1)
179 x2 = runtime.secret_share([1], self.Zp, x1)
181 x2 = runtime.secret_share([1], self.Zp)
184 y2 = runtime.secret_share([3], self.Zp, y1)
186 y2 = runtime.secret_share([3], self.Zp)
194 def test_sum_constant(self, runtime):
195 """Test addition of two numbers."""
197 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
203 self.assertEquals(v, x1 + y1)
206 x2 = runtime.secret_share([1], self.Zp, x1)
208 x2 = runtime.secret_share([1], self.Zp)
216 def test_sub(self, runtime):
217 """Test subtration of two numbers."""
219 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
225 self.assertEquals(v, x1 - y1)
228 x2 = runtime.secret_share([1], self.Zp, x1)
230 x2 = runtime.secret_share([1], self.Zp)
233 y2 = runtime.secret_share([3], self.Zp, y1)
235 y2 = runtime.secret_share([3], self.Zp)
237 z2 = runtime.sub(x2, y2)
243 def test_sub_minus(self, runtime):
244 """Test subtration of two numbers."""
246 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
252 self.assertEquals(v, x1 - y1)
255 x2 = runtime.secret_share([1], self.Zp, x1)
257 x2 = runtime.secret_share([1], self.Zp)
260 y2 = runtime.secret_share([3], self.Zp, y1)
262 y2 = runtime.secret_share([3], self.Zp)
270 def test_sub_constant(self, runtime):
271 """Test subtration of two numbers."""
273 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
279 self.assertEquals(v, x1 - y1)
282 x2 = runtime.secret_share([1], self.Zp, x1)
284 x2 = runtime.secret_share([1], self.Zp)
295 class OrlandiAdvancedCommandsTest(RuntimeTestCase):
296 """Test for advanced commands."""
301 runtime_class = OrlandiRuntime
305 def generate_configs(self, *args):
308 keys = generate_configs(paillier=NaClPaillier(1024), *args)
313 def test_shift(self, runtime):
314 """Test addition of the shift command."""
316 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
319 self.assertEquals(v, 42)
321 x = runtime.shift([2], self.Zp, 42)
327 def test_shift_two_inputters(self, runtime):
328 """Test addition of the shift command."""
330 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
333 self.assertEquals(v, 42)
335 x, y = runtime.shift([1,3], self.Zp, 42)
337 d1.addCallback(check)
339 d2.addCallback(check)
340 return DeferredList([d1, d2])
343 def test_shift_two_consequtive_inputters(self, runtime):
344 """Test addition of the shift command."""
346 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
350 self.assertEquals(runtime.program_counter, [4])
352 x = runtime.shift([1], self.Zp, 42)
353 y = runtime.shift([2], self.Zp, 42)
354 r = gather_shares([x, y])
359 def test_shift_two_consequtive_inputters2(self, runtime):
360 """Test addition of the shift command."""
362 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
365 self.assertEquals(v, 42)
368 self.assertEquals(x, 42)
369 self.assertEquals(y, 42)
371 x = runtime.shift([1], self.Zp, 42)
372 y = runtime.shift([2], self.Zp, 42)
373 r = gather_shares([runtime.open(x), runtime.open(y)])
378 def test_input(self, runtime):
379 """Test of many uses of the input command."""
381 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
387 for i in range(count):
388 inputter = (i % len(runtime.players)) + 1
389 if inputter == runtime.id:
390 a = rand.randint(0, self.Zp.modulus)
391 b = rand.randint(0, self.Zp.modulus)
394 a_shares.append(runtime.input([inputter], self.Zp, a))
395 b_shares.append(runtime.input([inputter], self.Zp, b))
396 shares_ready = gather_shares(a_shares + b_shares)
400 def test_basic_multiply(self, runtime):
401 """Test multiplication of two numbers."""
403 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
409 self.assertEquals(v, x1 * y1)
411 x2 = runtime.shift([2], self.Zp, x1)
412 y2 = runtime.shift([3], self.Zp, y1)
414 a, b, c = _get_triple(self, self.Zp)
415 z2 = runtime._basic_multiplication(x2, y2, a, b, c)
421 def test_mul_mul(self, runtime):
422 """Test multiplication of two numbers."""
424 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
430 self.assertEquals(v, x1 * y1)
432 x2 = runtime.shift([2], self.Zp, x1)
433 y2 = runtime.shift([3], self.Zp, y1)
441 def test_basic_multiply_constant_right(self, runtime):
442 """Test multiplication of two numbers."""
444 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
450 self.assertEquals(v, x1 * y1)
452 x2 = runtime.shift([1], self.Zp, x1)
454 a, b, c = _get_triple(self, self.Zp)
455 z2 = runtime._basic_multiplication(x2, self.Zp(y1), a, b, c)
461 def test_basic_multiply_constant_left(self, runtime):
462 """Test multiplication of two numbers."""
464 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
470 self.assertEquals(v, x1 * y1)
472 x2 = runtime.shift([1], self.Zp, x1)
474 a, b, c = _get_triple(self, self.Zp)
475 z2 = runtime._basic_multiplication(self.Zp(y1), x2, a, b, c)
481 def test_constant_multiplication_constant_left(self, runtime):
482 """Test multiplication of two numbers."""
484 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
490 self.assertEquals(v, x1 * y1)
492 x2 = runtime.shift([1], self.Zp, x1)
494 z2 = runtime._cmul(self.Zp(y1), x2, self.Zp)
500 def test_constant_multiplication_constant_right(self, runtime):
501 """Test multiplication of two numbers."""
503 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
509 self.assertEquals(v, x1 * y1)
511 x2 = runtime.shift([1], self.Zp, x1)
513 z2 = runtime._cmul(x2, self.Zp(y1), self.Zp)
519 def test_constant_multiplication_constant_None(self, runtime):
520 """Test multiplication of two numbers."""
522 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
527 x2 = runtime.shift([1], self.Zp, x1)
528 y2 = runtime.shift([1], self.Zp, y1)
531 def test_sum_poly1(self, runtime):
532 """Test implementation of sum_poly."""
534 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
537 f.append((self.Zp(7), (self.Zp(7), self.Zp(7)), self.Zp(7)))
538 f.append((self.Zp(9), (self.Zp(9), self.Zp(9)), self.Zp(9)))
539 f.append((self.Zp(13), (self.Zp(13), self.Zp(13)), self.Zp(13)))
541 x, (rho1, rho2), Cx = runtime.sum_poly(1, f)
542 self.assertEquals(x, 29)
543 self.assertEquals(rho1, 29)
544 self.assertEquals(rho2, 29)
545 self.assertEquals(Cx, 819)
549 def test_sum_poly2(self, runtime):
550 """Test implementation of sum_poly."""
552 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
554 Cf1 = commitment.commit(21, 21, 21)
555 Cf2 = commitment.commit(27, 27, 27)
556 Cf3 = commitment.commit(39, 39, 39)
559 f.append((self.Zp(7), (self.Zp(7), self.Zp(7)), Cf1))
560 f.append((self.Zp(9), (self.Zp(9), self.Zp(9)), Cf2))
561 f.append((self.Zp(13), (self.Zp(13), self.Zp(13)), Cf3))
563 x, (rho1, rho2), Cx = runtime.sum_poly(3, f)
564 self.assertEquals(x, 453)
565 self.assertEquals(rho1, 453)
566 self.assertEquals(rho2, 453)
567 self.assertEquals(Cx, Cf1**3 * Cf2**9 * Cf3**27)
571 def test_delta(self, runtime):
572 """Test implementation of compute_delta."""
574 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
576 delta = runtime.compute_delta(3)
577 self.assertEquals(delta[0], 7)
578 self.assertEquals(delta[1], -21)
579 self.assertEquals(delta[2], 35)
580 self.assertEquals(delta[3], -35)
581 self.assertEquals(delta[4], 21)
582 self.assertEquals(delta[5], -7)
583 self.assertEquals(delta[6], 1)
588 def test_leak_mul(self, runtime):
589 """Test leaktolerant multiplication of two numbers."""
590 commitment.set_reference_string(long(2), long(6))
592 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
602 self.assertEquals(v, x1 * y1)
604 x2 = runtime.shift([1], self.Zp, x1)
605 y2 = runtime.shift([2], self.Zp, y1)
607 sls = gatherResults(runtime.random_triple(self.Zp, 2*runtime.d + 1))
610 M = [[Share(self, self.Zp, j) for j in i] for i in M]
611 z2 = runtime.leak_tolerant_mul(x2, y2, M)
615 sls.addCallbacks(cont, runtime.error_handler)
618 z2 = runtime._cmul(y2, x2, self.Zp)
619 self.assertEquals(z2, None)
623 def test_leak_mul1(self, runtime):
624 """Test leaktolerant multiplication of two numbers."""
625 commitment.set_reference_string(long(2), long(6))
627 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
640 self.assertEquals(v, x1 * y1)
642 x2 = runtime.shift([1], self.Zp, x1)
643 y2 = runtime.shift([2], self.Zp, y1)
645 sls = gatherResults(runtime.random_triple(self.Zp, 2*runtime.d + 1))
648 M = [[Share(self, self.Zp, j) for j in i] for i in M]
649 z2 = runtime.leak_tolerant_mul(x2, y2, M)
653 sls.addCallbacks(cont, runtime.error_handler)
656 z2 = runtime._cmul(y2, x2, self.Zp)
657 self.assertEquals(z2, None)
660 class TripleGenTest(RuntimeTestCase):
661 """Test for generation of triples."""
666 runtime_class = OrlandiRuntime
670 def generate_configs(self, *args):
673 keys = generate_configs(paillier=NaClPaillier(1024), *args)
677 def test_tripleGen(self, runtime):
678 """Test the triple_gen command."""
680 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
686 def check((a, b, c)):
687 self.assertEquals(c, a * b)
689 def open((a, b, c, _)):
693 d = gatherResults([d1, d2, d3])
696 d = runtime.triple_gen(self.Zp)
697 d.addCallbacks(open, runtime.error_handler)
701 def test_tripleGen2(self, runtime):
702 """Test the triple_gen command."""
704 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
706 def check((a, b, c, dx, dy, dz)):
707 self.assertEquals(c, a * b)
708 self.assertEquals(dz, dx * dy)
710 def open(((a, b, c, control), (x, y, z, _))):
717 d = gatherResults([d1, d2, d3, dx, dy, dz])
720 t1 = runtime.triple_gen(self.Zp)
721 t2 = runtime.triple_gen(self.Zp)
722 d = gatherResults([t1, t2])
723 d.addCallbacks(open, runtime.error_handler)
727 def test_tripleTest(self, runtime):
728 """Test the triple_test command."""
730 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
732 def check((a, b, c)):
733 self.assertEquals(c, a * b)
735 def open((a, b, c, _)):
739 d = gatherResults([d1, d2, d3])
742 d = runtime.triple_test(self.Zp)
743 d.addCallbacks(open, runtime.error_handler)
747 def test_random_triple(self, runtime):
748 """Test the triple_combiner command."""
750 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
753 for x in xrange(len(ls) // 3):
757 self.assertEquals(c, a * b)
762 d1 = runtime.open(Share(self, self.Zp, a))
763 d2 = runtime.open(Share(self, self.Zp, b))
764 d3 = runtime.open(Share(self, self.Zp, c))
769 d = gatherResults(ds)
772 d = gatherResults(runtime.random_triple(self.Zp, 1))
773 d.addCallbacks(open, runtime.error_handler)
777 def test_random_triple3_parallel(self, runtime):
778 """Test the triple_combiner command."""
780 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
783 for x in xrange(len(ls) // 3):
787 self.assertEquals(c, a * b)
791 for [(a, b, c)] in ls:
792 d1 = runtime.open(Share(self, self.Zp, a))
793 d2 = runtime.open(Share(self, self.Zp, b))
794 d3 = runtime.open(Share(self, self.Zp, c))
799 d = gatherResults(ds)
802 a = gatherResults(runtime.random_triple(self.Zp, 1))
803 b = gatherResults(runtime.random_triple(self.Zp, 1))
804 c = gatherResults(runtime.random_triple(self.Zp, 1))
805 d = gatherResults([a, b, c])
806 d.addCallbacks(open, runtime.error_handler)
810 def test_random_triple_parallel(self, runtime):
811 """Test the triple_combiner command."""
813 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
816 for x in xrange(len(ls) // 3):
820 self.assertEquals(c, a * b)
824 for [(a, b, c)] in ls:
832 d = gatherResults(ds)
841 while a_shares and b_shares:
844 c_shares.append(runtime.mul(a, b))
845 done = gather_shares(c_shares)
850 for i in range(count):
851 inputter = (i % len(runtime.players)) + 1
852 if inputter == runtime.id:
853 a = rand.randint(0, self.Zp.modulus)
854 b = rand.randint(0, self.Zp.modulus)
857 a_shares.append(runtime.input([inputter], self.Zp, a))
858 b_shares.append(runtime.input([inputter], self.Zp, b))
859 shares_ready = gather_shares(a_shares + b_shares)
861 runtime.schedule_callback(shares_ready, cont)
865 def skip_tests(module_name):
866 OrlandiAdvancedCommandsTest.skip = "Skipped due to missing " + module_name + " module."
867 OrlandiBasicCommandsTest.skip = "Skipped due to missing " + module_name + " module."
868 TripleGenTest.skip = "Skipped due to missing " + module_name + " module."
871 skip_tests("commitment")
874 skip_tests("pypaillier")