changeset 1248:5ffc93111d70

Merged.
author Marcel Keller <mkeller@cs.au.dk>
date Thu, 17 Sep 2009 17:38:23 +0200
parents 591451fd23dc cd0f4d7d135b
children 0f35ae3f503b
files viff/passive.py
diffstat 3 files changed, 64 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/apps/aes.py	Thu Sep 17 14:35:26 2009 +0200
+++ b/apps/aes.py	Thu Sep 17 17:38:23 2009 +0200
@@ -120,9 +120,10 @@
     rt.schedule_complex_callback(s, encrypt, rt, key)
 
 def preprocess(rt):
+    start = time.time()
+    program_desc = {}
+
     if options.active:
-        start = time.time()
-
         if options.exponentiation is False:
             max = 301
             js = [3 + i * 15 + j for i in range(20) for j in range(7) + [8]]
@@ -140,13 +141,21 @@
                for k in range(1, options.count + 1)
                for i in range(10)
                for j in js]
+        program_desc[("generate_triples", (GF256,))] = pcs
 
-        preproc = rt.preprocess({("generate_triples", (GF256,)): pcs})
+    if options.exponentiation == 4:
+        pcs = [(2, 18, k) + (81,) * i + (1 + j * 4, 0)
+               for k in range(1, options.count + 1)
+               for i in range(10)
+               for j in range(20)]
 
+        program_desc[("prss_powerchains", ())] = pcs
+
+    if program_desc:
+        preproc = rt.preprocess(program_desc)
         def fin(_):
             print "Finished preprocessing after %f sec." % (time.time() - start)
             return rt
-
         preproc.addCallback(fin)
         rt.schedule_complex_callback(preproc, share_key)
 
--- a/viff/aes.py	Thu Sep 17 14:35:26 2009 +0200
+++ b/viff/aes.py	Thu Sep 17 17:38:23 2009 +0200
@@ -109,6 +109,8 @@
                 self.invert = self.invert_by_exponentiation_with_less_rounds
             elif (use_exponentiation == "chain_with_least_rounds"):
                 self.invert = self.invert_by_exponentiation_with_least_rounds
+            elif (use_exponentiation == "masked"):
+                self.invert = self.invert_by_masked_exponentiation
             else:
                 self.invert = self.invert_by_exponentiation
         else:
@@ -120,7 +122,8 @@
     exponentiation_variants = ["standard_square_and_multiply",
                                "shortest_sequential_chain",
                                "shortest_chain_with_least_rounds",
-                               "chain_with_least_rounds"]
+                               "chain_with_least_rounds",
+                               "masked"]
 
     def invert_by_masking(self, byte):
         bits = bit_decompose(byte)
@@ -156,6 +159,25 @@
         result.addCallback(lambda (c, r, b): c * r - b)
         return result
 
+    def invert_by_masked_exponentiation(self, byte):
+        def add_and_multiply(masked_powers, random_powers):
+            byte_powers = map(operator.add, masked_powers, random_powers)[1:]
+            while len(byte_powers) > 1:
+                byte_powers.append(byte_powers.pop(0) * byte_powers.pop(0))
+            return byte_powers[0]
+
+        def open_and_exponentiate(random_powers):
+            masked_byte = self.runtime.open(byte + random_powers[0])
+            masked_powers = self.runtime.schedule_complex_callback(masked_byte,
+                lambda masked_byte: self.runtime.powerchain(masked_byte, 7))
+            return self.runtime.schedule_complex_callback(
+                masked_powers, add_and_multiply, random_powers)
+
+        result = Share(self.runtime, GF256)
+        self.runtime.prss_powerchain().chainDeferred(result)
+        result = self.runtime.schedule_complex_callback(result, open_and_exponentiate)
+        return result
+
     def invert_by_exponentiation(self, byte):
         byte_2 = byte * byte
         byte_3 = byte_2 * byte
--- a/viff/passive.py	Thu Sep 17 14:35:26 2009 +0200
+++ b/viff/passive.py	Thu Sep 17 17:38:23 2009 +0200
@@ -22,11 +22,14 @@
 import operator
 
 from viff import shamir
-from viff.runtime import Runtime, increment_pc, Share, ShareList, gather_shares
+from viff.runtime import Runtime, increment_pc, Share, ShareList, \
+     gather_shares, preprocess
 from viff.prss import prss, prss_lsb, prss_zero, prss_multi
 from viff.field import GF256, FieldElement
 from viff.util import rand, profile
 
+from twisted.internet.defer import succeed
+
 
 class PassiveRuntime(Runtime):
     """The VIFF runtime.
@@ -449,6 +452,30 @@
         # Use r_lsb to flip b as needed.
         return (b_p, b ^ r_lsb)
 
+    def powerchain(self, share, max):
+        """Returns the list [*share*, *share*^2, *share*^4, ...,
+        *share*^(i^max)]."""
+        result = [share]
+        for i in range(max):
+            share = share * share
+            result.append(share)
+        return result
+
+    @increment_pc
+    @preprocess("prss_powerchains")
+    def prss_powerchain(self, max=7):
+        """Generate a random secret share in GF256 and returns
+        [*share*, *share*^2, *share*^4, ..., *share*^(i^max)]."""
+        share = self.prss_share_random(GF256)
+        return succeed(self.powerchain(share, max))
+
+    def prss_powerchains(self, max=7, quantity=20):
+        """Does *quantity* times the same as :meth:`prss_powerchain`.
+        Used for preprocessing."""
+        shares = self.prss_share_random_multi(GF256, quantity)
+        return quantity, succeed([self.powerchain(share, max)
+                                  for share in shares])
+
     def input(self, inputters, field, number=None, threshold=None):
         """Input *number* to the computation.