viff

changeset 1145:58576bfc42e8

Added a benchmark for the AES inversion.
author Marcel Keller <mkeller@cs.au.dk>
date Wed, 25 Feb 2009 15:39:34 +0100
parents 28c946f07e18
children ecd0f77f5243
files apps/aes_inversion.py
diffstat 1 files changed, 83 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/apps/aes_inversion.py	Wed Feb 25 15:39:34 2009 +0100
     1.3 @@ -0,0 +1,83 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +# Copyright 2009 VIFF Development Team.
     1.7 +#
     1.8 +# This file is part of VIFF, the Virtual Ideal Functionality Framework.
     1.9 +#
    1.10 +# VIFF is free software: you can redistribute it and/or modify it
    1.11 +# under the terms of the GNU Lesser General Public License (LGPL) as
    1.12 +# published by the Free Software Foundation, either version 3 of the
    1.13 +# License, or (at your option) any later version.
    1.14 +#
    1.15 +# VIFF is distributed in the hope that it will be useful, but WITHOUT
    1.16 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.17 +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
    1.18 +# Public License for more details.
    1.19 +#
    1.20 +# You should have received a copy of the GNU Lesser General Public
    1.21 +# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
    1.22 +
    1.23 +# This program is a benchmark for the AES inversion.
    1.24 +
    1.25 +
    1.26 +import random
    1.27 +import time
    1.28 +from optparse import OptionParser
    1.29 +
    1.30 +from twisted.internet import reactor
    1.31 +
    1.32 +from viff.field import GF256
    1.33 +from viff.runtime import Runtime, create_runtime, gather_shares, Share
    1.34 +from viff.config import load_config
    1.35 +
    1.36 +from viff.aes import AES
    1.37 +
    1.38 +
    1.39 +parser = OptionParser(usage="Usage: %prog [options] config_file")
    1.40 +parser.add_option("-e", "--exponentiation", action="store", type="int",
    1.41 +                  metavar="variant", 
    1.42 +                  help="Use exponentiation to invert bytes. "
    1.43 +                  "Default is the shortest sequential chain. "
    1.44 +                  "Possibilities:                             " +
    1.45 +                  "\n".join(["%d: %s                           " % 
    1.46 +                             (i, s) for (i, s) 
    1.47 +                             in enumerate(AES.exponentiation_variants)]))
    1.48 +parser.add_option("-m", "--masking", action="store_false", 
    1.49 +                  dest="exponentiation", 
    1.50 +                  help="Use masking to invert bytes.")
    1.51 +parser.set_defaults(exponentiation=1)
    1.52 +parser.add_option("-c", "--count", action="store", type="int",
    1.53 +                  help="Number of bytes to invert. Defaults to 100.")
    1.54 +parser.set_defaults(count=100)
    1.55 +
    1.56 +# Add standard VIFF options.
    1.57 +Runtime.add_options(parser)
    1.58 +
    1.59 +(options, args) = parser.parse_args()
    1.60 +
    1.61 +if len(args) == 0:
    1.62 +    parser.error("You must specify a config file.")
    1.63 +
    1.64 +id, players = load_config(args[0])
    1.65 +
    1.66 +def invert(rt):
    1.67 +    aes = AES(rt, 192, use_exponentiation=options.exponentiation)
    1.68 +    bytes = [Share(rt, GF256, GF256(random.randint(0, 255)))
    1.69 +             for i in range(options.count)]
    1.70 +
    1.71 +    start = time.time()
    1.72 +
    1.73 +    done = gather_shares([aes.invert(byte) for byte in bytes])
    1.74 +
    1.75 +    def finish(_):
    1.76 +        duration = time.time() - start
    1.77 +        print "Finished after %.3f s." % duration
    1.78 +        print "Time per inversion: %.3f ms" % (1000 * duration / options.count)
    1.79 +        rt.shutdown()
    1.80 +
    1.81 +    done.addCallback(finish)
    1.82 +
    1.83 +rt = create_runtime(id, players, 1, options)
    1.84 +rt.addCallback(invert)
    1.85 +
    1.86 +reactor.run()