viff

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.
2 #
3 # This file is part of VIFF, the Virtual Ideal Functionality Framework.
4 #
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.
9 #
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.
14 #
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
25 pypaillier = None
26 try:
27 import commitment
28 try:
29 from pypaillier import encrypt_r, decrypt, tripple_2c, tripple_3a
30 from viff.orlandi import OrlandiRuntime, OrlandiShare
31 pypaillier = "Imported"
32 except ImportError:
33 pypaillier = None
34 OrlandiRuntime = None
35 OrlandiShare = None
37 except ImportError:
38 commitment = None
39 OrlandiRuntime = None
40 OrlandiShare = None
42 from viff.field import FieldElement, GF
44 from viff.util import rand
47 def _get_triple(runtime, field):
48 n = field(0)
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)
55 return (a, b, c)
58 class OrlandiBasicCommandsTest(RuntimeTestCase):
59 """Test for basic commands."""
61 # Number of players.
62 num_players = 3
64 runtime_class = OrlandiRuntime
66 @protocol
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)
79 if 1 == runtime.id:
80 share = runtime.secret_share([1], self.Zp, 42)
81 else:
82 share = runtime.secret_share([1], self.Zp)
83 share.addCallback(check)
84 return share
86 @protocol
87 def test_open_secret_share(self, runtime):
88 """Test sharing and open of a number."""
90 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
92 def check(v):
93 self.assertEquals(v, 42)
95 if 1 == runtime.id:
96 x = runtime.secret_share([1], self.Zp, 42)
97 else:
98 x = runtime.secret_share([1], self.Zp)
99 d = runtime.open(x)
100 d.addCallback(check)
101 return d
103 @protocol
104 def test_open_multiple_secret_share(self, runtime):
105 """Test sharing and open of a number."""
107 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
109 def check(ls):
110 for inx, v in enumerate(ls):
111 self.assertEquals(v, (inx + 1) * 42)
113 if 1 == runtime.id:
114 x = runtime.secret_share([1], self.Zp, 42)
115 else:
116 x = runtime.secret_share([1], self.Zp)
117 if 1 == runtime.id:
118 y = runtime.secret_share([1], self.Zp, 84)
119 else:
120 y = runtime.secret_share([1], self.Zp)
121 d = runtime.open_multiple_values([x, y])
122 d.addCallback(check)
123 return d
125 @protocol
126 def test_random_share(self, runtime):
127 """Test creation of a random shared number."""
129 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
131 def check(v):
132 self.assertEquals(True, True)
134 x = runtime.random_share(self.Zp)
135 d = runtime.open(x)
136 d.addCallback(check)
137 return d
139 @protocol
140 def test_sum(self, runtime):
141 """Test addition of two numbers."""
143 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
145 x1 = 42
146 y1 = 7
148 def check(v):
149 self.assertEquals(v, x1 + y1)
151 if 1 == runtime.id:
152 x2 = runtime.secret_share([1], self.Zp, x1)
153 else:
154 x2 = runtime.secret_share([1], self.Zp)
156 if 3 == runtime.id:
157 y2 = runtime.secret_share([3], self.Zp, y1)
158 else:
159 y2 = runtime.secret_share([3], self.Zp)
161 z2 = runtime.add(x2, y2)
162 d = runtime.open(z2)
163 d.addCallback(check)
164 return d
166 @protocol
167 def test_sum_plus(self, runtime):
168 """Test addition of two numbers."""
170 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
172 x1 = 42
173 y1 = 7
175 def check(v):
176 self.assertEquals(v, x1 + y1)
178 if 1 == runtime.id:
179 x2 = runtime.secret_share([1], self.Zp, x1)
180 else:
181 x2 = runtime.secret_share([1], self.Zp)
183 if 3 == runtime.id:
184 y2 = runtime.secret_share([3], self.Zp, y1)
185 else:
186 y2 = runtime.secret_share([3], self.Zp)
188 z2 = x2 + y2
189 d = runtime.open(z2)
190 d.addCallback(check)
191 return d
193 @protocol
194 def test_sum_constant(self, runtime):
195 """Test addition of two numbers."""
197 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
199 x1 = 42
200 y1 = 7
202 def check(v):
203 self.assertEquals(v, x1 + y1)
205 if 1 == runtime.id:
206 x2 = runtime.secret_share([1], self.Zp, x1)
207 else:
208 x2 = runtime.secret_share([1], self.Zp)
210 z2 = x2 + y1
211 d = runtime.open(z2)
212 d.addCallback(check)
213 return d
215 @protocol
216 def test_sub(self, runtime):
217 """Test subtration of two numbers."""
219 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
221 x1 = 42
222 y1 = 7
224 def check(v):
225 self.assertEquals(v, x1 - y1)
227 if 1 == runtime.id:
228 x2 = runtime.secret_share([1], self.Zp, x1)
229 else:
230 x2 = runtime.secret_share([1], self.Zp)
232 if 3 == runtime.id:
233 y2 = runtime.secret_share([3], self.Zp, y1)
234 else:
235 y2 = runtime.secret_share([3], self.Zp)
237 z2 = runtime.sub(x2, y2)
238 d = runtime.open(z2)
239 d.addCallback(check)
240 return d
242 @protocol
243 def test_sub_minus(self, runtime):
244 """Test subtration of two numbers."""
246 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
248 x1 = 42
249 y1 = 7
251 def check(v):
252 self.assertEquals(v, x1 - y1)
254 if 1 == runtime.id:
255 x2 = runtime.secret_share([1], self.Zp, x1)
256 else:
257 x2 = runtime.secret_share([1], self.Zp)
259 if 3 == runtime.id:
260 y2 = runtime.secret_share([3], self.Zp, y1)
261 else:
262 y2 = runtime.secret_share([3], self.Zp)
264 z2 = x2 - y2
265 d = runtime.open(z2)
266 d.addCallback(check)
267 return d
269 @protocol
270 def test_sub_constant(self, runtime):
271 """Test subtration of two numbers."""
273 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
275 x1 = 42
276 y1 = 7
278 def check(v):
279 self.assertEquals(v, x1 - y1)
281 if 1 == runtime.id:
282 x2 = runtime.secret_share([1], self.Zp, x1)
283 else:
284 x2 = runtime.secret_share([1], self.Zp)
286 z2 = x2 - y1
287 d = runtime.open(z2)
288 d.addCallback(check)
289 return d
292 keys = None
295 class OrlandiAdvancedCommandsTest(RuntimeTestCase):
296 """Test for advanced commands."""
298 # Number of players.
299 num_players = 3
301 runtime_class = OrlandiRuntime
303 timeout = 700
305 def generate_configs(self, *args):
306 global keys
307 if not keys:
308 keys = generate_configs(paillier=NaClPaillier(1024), *args)
309 return keys
312 @protocol
313 def test_shift(self, runtime):
314 """Test addition of the shift command."""
316 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
318 def check(v):
319 self.assertEquals(v, 42)
321 x = runtime.shift([2], self.Zp, 42)
322 d = runtime.open(x)
323 d.addCallback(check)
324 return d
326 @protocol
327 def test_shift_two_inputters(self, runtime):
328 """Test addition of the shift command."""
330 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
332 def check(v):
333 self.assertEquals(v, 42)
335 x, y = runtime.shift([1,3], self.Zp, 42)
336 d1 = runtime.open(x)
337 d1.addCallback(check)
338 d2 = runtime.open(y)
339 d2.addCallback(check)
340 return DeferredList([d1, d2])
342 @protocol
343 def test_shift_two_consequtive_inputters(self, runtime):
344 """Test addition of the shift command."""
346 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
348 def r1(ls):
349 x, y = ls
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])
355 r.addCallback(r1)
356 return r
358 @protocol
359 def test_shift_two_consequtive_inputters2(self, runtime):
360 """Test addition of the shift command."""
362 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
364 def check(v):
365 self.assertEquals(v, 42)
367 def r1((x, y)):
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)])
374 r.addCallback(r1)
375 return r
377 @protocol
378 def test_input(self, runtime):
379 """Test of many uses of the input command."""
381 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
383 count = 9
385 a_shares = []
386 b_shares = []
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)
392 else:
393 a, b = None, None
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)
397 return shares_ready
399 @protocol
400 def test_basic_multiply(self, runtime):
401 """Test multiplication of two numbers."""
403 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
405 x1 = 42
406 y1 = 7
408 def check(v):
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)
416 d = runtime.open(z2)
417 d.addCallback(check)
418 return d
420 @protocol
421 def test_mul_mul(self, runtime):
422 """Test multiplication of two numbers."""
424 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
426 x1 = 42
427 y1 = 7
429 def check(v):
430 self.assertEquals(v, x1 * y1)
432 x2 = runtime.shift([2], self.Zp, x1)
433 y2 = runtime.shift([3], self.Zp, y1)
435 z2 = x2 * y2
436 d = runtime.open(z2)
437 d.addCallback(check)
438 return d
440 @protocol
441 def test_basic_multiply_constant_right(self, runtime):
442 """Test multiplication of two numbers."""
444 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
446 x1 = 42
447 y1 = 7
449 def check(v):
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)
456 d = runtime.open(z2)
457 d.addCallback(check)
458 return d
460 @protocol
461 def test_basic_multiply_constant_left(self, runtime):
462 """Test multiplication of two numbers."""
464 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
466 x1 = 42
467 y1 = 7
469 def check(v):
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)
476 d = runtime.open(z2)
477 d.addCallback(check)
478 return d
480 @protocol
481 def test_constant_multiplication_constant_left(self, runtime):
482 """Test multiplication of two numbers."""
484 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
486 x1 = 42
487 y1 = 7
489 def check(v):
490 self.assertEquals(v, x1 * y1)
492 x2 = runtime.shift([1], self.Zp, x1)
494 z2 = runtime._cmul(self.Zp(y1), x2, self.Zp)
495 d = runtime.open(z2)
496 d.addCallback(check)
497 return d
499 @protocol
500 def test_constant_multiplication_constant_right(self, runtime):
501 """Test multiplication of two numbers."""
503 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
505 x1 = 42
506 y1 = 7
508 def check(v):
509 self.assertEquals(v, x1 * y1)
511 x2 = runtime.shift([1], self.Zp, x1)
513 z2 = runtime._cmul(x2, self.Zp(y1), self.Zp)
514 d = runtime.open(z2)
515 d.addCallback(check)
516 return d
518 @protocol
519 def test_constant_multiplication_constant_None(self, runtime):
520 """Test multiplication of two numbers."""
522 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
524 x1 = 42
525 y1 = 7
527 x2 = runtime.shift([1], self.Zp, x1)
528 y2 = runtime.shift([1], self.Zp, y1)
530 @protocol
531 def test_sum_poly1(self, runtime):
532 """Test implementation of sum_poly."""
534 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
536 f = []
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)
546 return x
548 @protocol
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)
558 f = []
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)
568 return x
570 @protocol
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)
585 return delta
587 @protocol
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)
594 x1 = 42
595 y1 = 7
597 runtime.s = 1
598 runtime.d = 0
599 runtime.s_lambda = 1
601 def check(v):
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))
609 def cont(M):
610 M = [[Share(self, self.Zp, j) for j in i] for i in M]
611 z2 = runtime.leak_tolerant_mul(x2, y2, M)
612 d = runtime.open(z2)
613 d.addCallback(check)
614 return d
615 sls.addCallbacks(cont, runtime.error_handler)
616 return sls
618 z2 = runtime._cmul(y2, x2, self.Zp)
619 self.assertEquals(z2, None)
620 return z2
622 @protocol
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)
629 x1 = 42
630 y1 = 7
632 runtime.s = 2
633 runtime.d = 2
634 runtime.s_lambda = 1
636 def check(v):
637 runtime.s = 1
638 runtime.d = 0
639 runtime.s_lambda = 1
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))
647 def cont(M):
648 M = [[Share(self, self.Zp, j) for j in i] for i in M]
649 z2 = runtime.leak_tolerant_mul(x2, y2, M)
650 d = runtime.open(z2)
651 d.addCallback(check)
652 return d
653 sls.addCallbacks(cont, runtime.error_handler)
654 return sls
656 z2 = runtime._cmul(y2, x2, self.Zp)
657 self.assertEquals(z2, None)
658 return z2
660 class TripleGenTest(RuntimeTestCase):
661 """Test for generation of triples."""
663 # Number of players.
664 num_players = 3
666 runtime_class = OrlandiRuntime
668 timeout = 1600
670 def generate_configs(self, *args):
671 global keys
672 if not keys:
673 keys = generate_configs(paillier=NaClPaillier(1024), *args)
674 return keys
676 @protocol
677 def test_tripleGen(self, runtime):
678 """Test the triple_gen command."""
680 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
682 runtime.s = 1
683 runtime.d = 0
684 runtime.s_lambda = 1
686 def check((a, b, c)):
687 self.assertEquals(c, a * b)
689 def open((a, b, c, _)):
690 d1 = runtime.open(a)
691 d2 = runtime.open(b)
692 d3 = runtime.open(c)
693 d = gatherResults([d1, d2, d3])
694 d.addCallback(check)
695 return d
696 d = runtime.triple_gen(self.Zp)
697 d.addCallbacks(open, runtime.error_handler)
698 return d
700 @protocol
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, _))):
711 d1 = runtime.open(a)
712 d2 = runtime.open(b)
713 d3 = runtime.open(c)
714 dx = runtime.open(x)
715 dy = runtime.open(y)
716 dz = runtime.open(z)
717 d = gatherResults([d1, d2, d3, dx, dy, dz])
718 d.addCallback(check)
719 return d
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)
724 return d
726 @protocol
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, _)):
736 d1 = runtime.open(a)
737 d2 = runtime.open(b)
738 d3 = runtime.open(c)
739 d = gatherResults([d1, d2, d3])
740 d.addCallback(check)
741 return d
742 d = runtime.triple_test(self.Zp)
743 d.addCallbacks(open, runtime.error_handler)
744 return d
746 @protocol
747 def test_random_triple(self, runtime):
748 """Test the triple_combiner command."""
750 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
752 def check(ls):
753 for x in xrange(len(ls) // 3):
754 a = ls[x * 3]
755 b = ls[x * 3 + 1]
756 c = ls[x * 3 + 2]
757 self.assertEquals(c, a * b)
759 def open(ls):
760 ds = []
761 for (a, b, c) in ls:
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))
765 ds.append(d1)
766 ds.append(d2)
767 ds.append(d3)
769 d = gatherResults(ds)
770 d.addCallback(check)
771 return d
772 d = gatherResults(runtime.random_triple(self.Zp, 1))
773 d.addCallbacks(open, runtime.error_handler)
774 return d
776 @protocol
777 def test_random_triple3_parallel(self, runtime):
778 """Test the triple_combiner command."""
780 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
782 def check(ls):
783 for x in xrange(len(ls) // 3):
784 a = ls[x * 3]
785 b = ls[x * 3 + 1]
786 c = ls[x * 3 + 2]
787 self.assertEquals(c, a * b)
789 def open(ls):
790 ds = []
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))
795 ds.append(d1)
796 ds.append(d2)
797 ds.append(d3)
799 d = gatherResults(ds)
800 d.addCallback(check)
801 return d
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)
807 return d
809 @protocol
810 def test_random_triple_parallel(self, runtime):
811 """Test the triple_combiner command."""
813 self.Zp = GF(6277101735386680763835789423176059013767194773182842284081)
815 def check(ls):
816 for x in xrange(len(ls) // 3):
817 a = ls[x * 3]
818 b = ls[x * 3 + 1]
819 c = ls[x * 3 + 2]
820 self.assertEquals(c, a * b)
822 def open(ls):
823 ds = []
824 for [(a, b, c)] in ls:
825 d1 = runtime.open(a)
826 d2 = runtime.open(b)
827 d3 = runtime.open(c)
828 ds.append(d1)
829 ds.append(d2)
830 ds.append(d3)
832 d = gatherResults(ds)
833 d.addCallback(check)
834 return d
836 a_shares = []
837 b_shares = []
838 c_shares = []
840 def cont(x):
841 while a_shares and b_shares:
842 a = a_shares.pop()
843 b = b_shares.pop()
844 c_shares.append(runtime.mul(a, b))
845 done = gather_shares(c_shares)
846 return done
848 count = 5
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)
855 else:
856 a, b = None, None
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)
862 return shares_ready
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."
870 if not commitment:
871 skip_tests("commitment")
873 if not pypaillier:
874 skip_tests("pypaillier")