Пример #1
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()
Пример #2
0
    def __init__(self, input_map, config_file, options=None):
        self.config_file = config_file
        self.options = options

        self.id, self.parties = load_config(self.config_file)
        self.parties_list = [p for p in self.parties]

        self.input_map = input_map
        self.map_inputs_to_parties()

        runtime_class = make_runtime_class(mixins=[ComparisonToft07Mixin])
        self.pre_runtime = create_runtime(self.id, self.parties, 1,
                                          self.options, runtime_class)
Пример #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()
Пример #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()
Пример #5
0
actual_mixins = []
if options.mixins:
    actual_mixins = [mixins[mixin] for mixin in options.mixins.split(',')]

# Identify the operation and it mixin dependencies.
operation = operations[options.operation][0]
actual_mixins += operations[options.operation][1]
operation_arity = operations[options.operation][2]

print "Using the base runtime: %s." % base_runtime_class
if actual_mixins:
    print "With the following mixins:"
    for mixin in actual_mixins:
        print "- %s" % mixin.__name__

runtime_class = make_runtime_class(base_runtime_class, actual_mixins)

pre_runtime = create_runtime(id, players, options.threshold, options,
                             runtime_class)


def update_args(runtime, options):
    args = {}
    if options.args:
        for arg in options.args.split(','):
            id, value = arg.split('=')
            args[id] = long(value)
        runtime.set_args(args)
    return runtime

Пример #6
0
    output = runtime.open(output)

    dprint("output %s", output)

    actual = runtime.open(arr[9 * 3 + 2])
    dprint("actual %s", actual)

    print "Time taken: %.2f sec" % (time() - start_time)

    runtime.wait_for(output)

    # runtime.schedule_callback(results, lambda _: runtime.synchronize())
    # runtime.schedule_callback(results, lambda _: runtime.shutdown())


def errorHandler(failure):
    print "Error: %s" % failure


# Create a runtime
runtime_class = make_runtime_class(runtime_class=Toft05Runtime,
                                   mixins=[ProbabilisticEqualityMixin])
pre_runtime = create_runtime(id, players, 1, None, runtime_class)
pre_runtime.addCallback(protocol)
# This error handler will enable debugging by capturing
# any exceptions and print them on Standard Out.
pre_runtime.addErrback(errorHandler)

print "#### Starting reactor ###"
reactor.run()
Пример #7
0
if options.mixins:
    actual_mixins = [mixins[mixin] for mixin in options.mixins.split(',')]


# Identify the operation and it mixin dependencies.
operation = operations[options.operation][0]
actual_mixins += operations[options.operation][1]
operation_arity = operations[options.operation][2]

print "Using the base runtime: %s." % base_runtime_class
if actual_mixins:
    print "With the following mixins:"
    for mixin in actual_mixins:
        print "- %s" % mixin.__name__

runtime_class = make_runtime_class(base_runtime_class, actual_mixins)

pre_runtime = create_runtime(id, players, options.threshold,
                             options, runtime_class)

def update_args(runtime, options):
    args = {}
    if options.args:
        for arg in options.args.split(','):
            id, value = arg.split('=')
            args[id] = long(value)
        runtime.set_args(args)
    return runtime


pre_runtime.addCallback(update_args, options)
Пример #8
0
        def results_ready(opened_results):
            print "Results Ready"
            for i in range(len(opened_results)):
                node_name = results[i]
                value_map[node_name] = opened_results[
                    i] if opened_results[i] < INFINITY else float("inf")

        gathered = gather_shares(gathered)
        gathered.addCallback(results_ready)
        runtime.schedule_callback(gathered, lambda _: runtime.shutdown())
        print "Opening"
    except:
        import traceback
        traceback.print_exc()


# Intialize Runtime
runtime_class = make_runtime_class(mixins=[ComparisonToft07Mixin])
pre_runtime = create_runtime(id, parties, 1, options, runtime_class)
pre_runtime.addCallback(begin_MPC)

# Start the Twisted event loop.
reactor.run()

print "Evaluated!!"
time2 = time()

print "MPC TIME: " + str(time2 - time1)
print "TOTAL TIME: " + str(time2 - time_)
Пример #9
0

def errorHandler(failure):
    print("Error: %s" % failure)


# Parse command line arguments.
parser = OptionParser()
Toft05Runtime.add_options(parser)
options, args = parser.parse_args()

if len(args) == 0:
    parser.error("you must specify a config file")
else:
    id, players = load_config(args[0])
k = int(sys.argv[3])
# Create a deferred Runtime and ask it to run our protocol when ready.
runtime_class = make_runtime_class(
    runtime_class=BasicActiveRuntime,
    mixins=[TriplesHyperinvertibleMatricesMixin])
pre_runtime = create_runtime(id,
                             players,
                             1,
                             options,
                             runtime_class=runtime_class)
pre_runtime.addCallback(OnlineProtocol, k)
pre_runtime.addErrback(errorHandler)

# Start the Twisted event loop.
reactor.run()
Пример #10
0

# Parse command line arguments.
parser = OptionParser(usage=__doc__)

parser.add_option("--modulus",
                 help="lower limit for modulus (can be an expression)")
parser.add_option("-n", "--number", type="int",
                 help="number to compare")

parser.set_defaults(modulus=2**65, number=None)

Runtime.add_options(parser)

options, args = parser.parse_args()

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=[ProbabilisticEqualityMixin])
pre_runtime = create_runtime(id, players, 1, options, runtime_class)
pre_runtime.addCallback(Protocol)

# Start the Twisted event loop.
reactor.run()
Пример #11
0
        result.addCallback(lambda _: runtime.shutdown())


# Parse command line arguments.
parser = OptionParser(usage=__doc__)

parser.add_option("--modulus",
                  help="lower limit for modulus (can be an expression)")
parser.add_option("-n", "--number", type="int", help="number to compare")

parser.set_defaults(modulus=2**65, number=None)

Runtime.add_options(parser)

options, args = parser.parse_args()

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=[ProbabilisticEqualityMixin])
pre_runtime = create_runtime(id, players, 1, options, runtime_class)
pre_runtime.addCallback(Protocol)

# Start the Twisted event loop.
reactor.run()