Exemple #1
0
 def __init__(self, player, threshold=None, options=None):
     """Initialize runtime."""
     Runtime.__init__(self, player, threshold, options)
     self.threshold = self.num_players - 1
     self.s = 1
     self.d = 0
     self.s_lambda = 1
Exemple #2
0
def vcrun(f, fp=True, cb1=None, cb2=None):
    """ Verifiable computation entry point. Adding this to the end of the
        verifiable computation will load configurations, set up communication,
        and run the given function f. The qap_wrapper decorator should be
        applied to f, and f is assumed to give as public output the
        computation result. """

    parser = OptionParser()
    Runtime.add_options(parser)
    if cb1: cb1(parser)  # callback to add custom options to parser
    options, args = parser.parse_args()

    if len(args) < 1: parser.error("you must specify a config file")
    myid, players = load_config(args[0])

    if cb2: cb2(options, args[1:])  # handle parsing results

    if fp:
        from viff.comparison import Toft07Runtime
        from viff.division import DivisionSH12Mixin
        runtime_class = make_runtime_class(mixins=[
            DivisionSH12Mixin, ProbabilisticEqualityMixin, Toft07Runtime
        ])
        pre_runtime = create_runtime(myid, players, options.threshold, options,
                                     runtime_class)
        pre_runtime.addCallback(vc_init)
    else:
        pre_runtime = create_runtime(myid, players, options.threshold, options)
        pre_runtime.addCallback(vc_init)

    def callf(runtime):
        ret = f(runtime)

        retsh = []
        for_each_in(Share, lambda x: retsh.append(x), ret)

        def printAndExit(runtime, vals):
            valsrev = list(reversed(vals))
            retc = for_each_in(Share, lambda x: valsrev.pop(), ret)
            print "[ Verifiable computation result", retc, "]"

            if runtime.__class__.__name__ != "LocalRuntime": time.sleep(1)
            runtime.shutdown()

            return runtime

        if retsh != []:
            # print all returned values and then shut down
            gather_shares(retsh).addCallback(
                lambda x: printAndExit(runtime, x))
        else:
            if runtime.__class__.__name__ != "LocalRuntime": time.sleep(1)
            runtime.shutdown()

        return runtime

    pre_runtime.addCallback(callf)
    reactor.run()
Exemple #3
0
def main():
     # Parse command line arguments.
    parser = OptionParser(usage=__doc__)

    parser.add_option("--modulus",
                     help="lower limit for modulus (can be an expression)")

    parser.set_defaults(modulus=2**65)

    Runtime.add_options(parser)

    options, args = parser.parse_args()
    if len(args)==2:
        number = int(args[1])
    else:
        number = None

    if len(args) == 0:
        parser.error("you must specify a config file")

    Zp = GF(find_prime(options.modulus, blum=True))

    # Load configuration file.
    id, players = load_config(args[0])

    runtime_class = make_runtime_class(mixins=[ComparisonToft07Mixin])
    pre_runtime = create_runtime(id, players, 1, options, runtime_class)

    def run(runtime):
        print "Connected."

        # Players 1 and 2 are doing a sharing over the field Zp.
        # Our input is number (None for other players).
        if runtime.id == 3:
            print "I have no number"
        else:
            print "My number: %d." % number
        (x, y) = runtime.shamir_share([1, 2], Zp, number)

        # Do the secret computation.
        result = divide(x, y, 10) # 10 bits for the result.

        # Now open the result so we can see it.
        dprint("The two numbers divided are: %s", runtime.open(result))

        result.addCallback(lambda _: runtime.shutdown())

    pre_runtime.addCallback(run)

    # Start the Twisted event loop.
    reactor.run()
Exemple #4
0
def main():
    # Parse command line arguments.
    parser = OptionParser(usage=__doc__)

    parser.add_option("--modulus",
                      help="lower limit for modulus (can be an expression)")

    parser.set_defaults(modulus=2**65)

    Runtime.add_options(parser)

    options, args = parser.parse_args()
    if len(args) == 2:
        number = int(args[1])
    else:
        number = None

    if len(args) == 0:
        parser.error("you must specify a config file")

    Zp = GF(find_prime(options.modulus, blum=True))

    # Load configuration file.
    id, players = load_config(args[0])

    runtime_class = make_runtime_class(mixins=[ComparisonToft07Mixin])
    pre_runtime = create_runtime(id, players, 1, options, runtime_class)

    def run(runtime):
        print "Connected."

        # Players 1 and 2 are doing a sharing over the field Zp.
        # Our input is number (None for other players).
        if runtime.id == 3:
            print "I have no number"
        else:
            print "My number: %d." % number
        (x, y) = runtime.shamir_share([1, 2], Zp, number)

        # Do the secret computation.
        result = divide(x, y, 10)  # 10 bits for the result.

        # Now open the result so we can see it.
        dprint("The two numbers divided are: %s", runtime.open(result))

        result.addCallback(lambda _: runtime.shutdown())

    pre_runtime.addCallback(run)

    # Start the Twisted event loop.
    reactor.run()
    def evaluate(clazz,
                 input_map,
                 expressions,
                 weight_expressions,
                 config_file=None,
                 options=None):
        if config_file is None and options is None:
            parser = OptionParser()
            parser.set_defaults(modulus=2**65)

            Runtime.add_options(parser)
            options, args = parser.parse_args()
            config_file = args[0]

        evaluator = ViffEvaluator(input_map, config_file, options=options)
        return evaluator.evaluate_all(expressions, weight_expressions)
Exemple #6
0
import os, operator, string
from viff.field import GF
from viff.runtime import Runtime, Share, start
from viff.config import load_config
from viff.inlinecb import viffinlinecb, returnValue, declareReturnNop
from gmpy2 import is_prime
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-f", "--tableau", help="Filename for tableau.")
parser.set_defaults(tableau="default")
Runtime.add_options(parser)
options, args = parser.parse_args()
if len(args) < 2:
    parser.error("You must specify a config file and a certificate filename.")
else:
    id, players = load_config(args[0])
    certificate_filename = args[1]


def load_tableau(filename):
    T = []
    comment_sign = "#"
    separator = "\t"
    for line in open(os.path.join("data", filename + ".teau"), "r"):
        # strip whitespace and comments
        tmp = string.split(line, comment_sign)[0]
        tmp = string.strip(tmp)
        if tmp and not tmp.startswith(comment_sign):
            # skip comments and empty lines.
            parsed_line = string.split(tmp, separator)
Exemple #7
0
 def __init__(self, player, options):
     """Initialize runtime."""
     Runtime.__init__(self, player, options)
Exemple #8
0
 def __init__(self, player, threshold, options=None):
     """Initialize runtime."""
     Runtime.__init__(self, player, threshold, options)
Exemple #9
0
parser.add_option("-m", "--masking", action="store_false",
                  dest="exponentiation",
                  help="Use masking to invert bytes.")
parser.set_defaults(exponentiation=1)
parser.add_option("-o", "--at-once", action="store_true",help="Prepare "
                  "the whole computation at once instead of round-wise.")
parser.add_option("-c", "--count", action="store", type="int",
                  help="Number of blocks to encrypt. Defaults to 1.")
parser.set_defaults(count=1)
parser.add_option("-a", "--active", action="store_true", help="Use actively "
                  "secure runtime. Default is only passive security.")
parser.add_option("-p", "--preproc", action="store_true", help="Use "
                  "preprocessing. Default is no preprocessing.")

# Add standard VIFF options.
Runtime.add_options(parser)

(options, args) = parser.parse_args()

if len(args) == 0:
    parser.error("You must specify a config file.")

id, players = load_config(args[0])

def encrypt(_, rt, key):
    start = time.time()
    print "Started at %f." % start

    aes = AES(rt, options.keylength, use_exponentiation=options.exponentiation)

    ciphertext = []
Exemple #10
0
 def add_player(self, player, protocol):
     Runtime.add_player(self, player, protocol)
     if player.id == self.id:
         self.player = player
     else:
         self.peer = player
Exemple #11
0
 def add_player(self, player, protocol):
     Runtime.add_player(self, player, protocol)
     if player.id == self.id:
         self.player = player
     else:
         self.peer = player
Exemple #12
0
 def __init__(self, player, threshold, options=None):
     """Initialize runtime."""
     Runtime.__init__(self, player, threshold, options)