viff

changeset 1090:a45e34e4ebf0

Improved AES example application.
author Marcel Keller <mkeller@cs.au.dk>
date Mon, 26 Jan 2009 10:55:59 +0100
parents 992c7eee2b3f
children bff9df62879f
files apps/aes.py
diffstat 1 files changed, 21 insertions(+), 20 deletions(-) [+]
line diff
     1.1 --- a/apps/aes.py	Mon Jan 26 10:02:48 2009 +0100
     1.2 +++ b/apps/aes.py	Mon Jan 26 10:55:59 2009 +0100
     1.3 @@ -26,6 +26,7 @@
     1.4  
     1.5  from twisted.internet import reactor
     1.6  
     1.7 +from viff.field import GF256
     1.8  from viff.runtime import BasicRuntime, create_runtime, gather_shares
     1.9  from viff.config import load_config
    1.10  
    1.11 @@ -50,38 +51,38 @@
    1.12  
    1.13  id, players = load_config(args[0])
    1.14  
    1.15 -def encrypt(_, rt):
    1.16 +def encrypt(_, rt, key):
    1.17      start = time.time()
    1.18      print "Started at %f." % start
    1.19  
    1.20      aes = AES(rt, 192, use_exponentiation=options.exponentiation)
    1.21 -    ciphertext = aes.encrypt("a" * 16, "b" * 24, True)
    1.22 +    ciphertext = aes.encrypt("a" * 16, key, True)
    1.23  
    1.24 -    opened = []
    1.25 -    result = [0] * 16
    1.26 +    opened_ciphertext = [rt.open(c) for c in ciphertext]
    1.27  
    1.28 -    for i, c in enumerate(ciphertext):
    1.29 -        o = rt.open(c)
    1.30 -        
    1.31 -        def res(x, i):
    1.32 -            result[i] = hex(x.value)
    1.33 -
    1.34 -        o.addCallback(res, i)
    1.35 -        opened.append(o)
    1.36 -
    1.37 -    def fin(g, result):
    1.38 +    def fin(ciphertext):
    1.39          print "Finished after %f sec." % (time.time() - start)
    1.40 -        print result
    1.41 +        print "Ciphertext:", [hex(c.value) for c in ciphertext]
    1.42          rt.shutdown()
    1.43  
    1.44 -    g = gather_shares(opened)
    1.45 -    g.addCallback(fin, result)
    1.46 +    g = gather_shares(opened_ciphertext)
    1.47 +    g.addCallback(fin)
    1.48  
    1.49 -def sync(rt):
    1.50 +def share_key(rt):
    1.51 +    key =  []
    1.52 +
    1.53 +    for i in range(24):
    1.54 +        inputter = i % 3 + 1
    1.55 +        
    1.56 +        if (inputter == id):
    1.57 +            key.append(rt.input([inputter], GF256, ord("b")))
    1.58 +        else:
    1.59 +            key.append(rt.input([inputter], GF256))
    1.60 +
    1.61      s = rt.synchronize()
    1.62 -    s.addCallback(encrypt, rt)
    1.63 +    s.addCallback(encrypt, rt, key)
    1.64  
    1.65  rt = create_runtime(id, players, 1, options)
    1.66 -rt.addCallback(sync)
    1.67 +rt.addCallback(share_key)
    1.68  
    1.69  reactor.run()