viff

view viff/bedoza/util.py @ 1535:b4451e4ac76d

BeDOZa: Use gmpy for modular exponentiation.
author Thomas P Jakobsen <tpj@cs.au.dk>
date Tue Aug 10 16:03:54 2010 +0200 (21 months ago)
parents e45ed7224d20
children c8e7c5ee1583
line source
1 # Copyright 2010 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 Deferred, gatherResults
20 from gmpy import mpz
22 from viff.constants import TEXT
24 def _send(runtime, vals, serialize=str, deserialize=int):
25 """Send vals[i] to player i + 1. Returns deferred list.
27 Works as default for integers. If other stuff has to be
28 sent, supply another serialization, deserialition.
29 """
30 runtime.increment_pc()
32 pc = tuple(runtime.program_counter)
33 for p in runtime.players:
34 msg = serialize(vals[p - 1])
35 runtime.protocols[p].sendData(pc, TEXT, msg)
36 def err_handler(err):
37 print err
38 values = []
39 for p in runtime.players:
40 d = Deferred()
41 d.addCallbacks(deserialize, err_handler)
42 runtime._expect_data(p, TEXT, d)
43 values.append(d)
44 result = gatherResults(values)
45 return result
47 def _convolute(runtime, val, serialize=str, deserialize=int):
48 """As send, but sends the same val to all players."""
49 return _send(runtime, [val] * runtime.num_players,
50 serialize=serialize, deserialize=deserialize)
52 def _convolute_gf_elm(runtime, gf_elm):
53 return _convolute(runtime, gf_elm,
54 serialize=lambda x: str(x.value),
55 deserialize=lambda x: gf_elm.field(int(x)))
57 def _send_gf_elm(runtime, vals):
58 return _send(runtime, vals,
59 serialize=lambda x: str(x.value),
60 deserialize=lambda x: gf_elm.field(int(x)))
62 def fast_pow(a, b, modulus):
63 return long(pow(mpz(a), b, modulus))