示例#1
0
def eval_poly(runtime):
    print "Starting protocol"
    start_time = time()

    modulus = find_prime(2**65, blum=True)
    Zp = GF(modulus)

    # In this example we just let Player 1 share the input values.
    if runtime.id == 1:
        x = runtime.shamir_share([1], Zp, 17)
        a = runtime.shamir_share([1], Zp, 42)
        b = runtime.shamir_share([1], Zp, -5)
        c = runtime.shamir_share([1], Zp, 87)
    else:
        x = runtime.shamir_share([1], Zp)
        a = runtime.shamir_share([1], Zp)
        b = runtime.shamir_share([1], Zp)
        c = runtime.shamir_share([1], Zp)

    # Evaluate the polynomial.
    p = a * (x * x) + b * x + c

    sign = (p < 0) * -1 + (p > 0) * 1
    output = runtime.open(sign)
    output.addCallback(done, start_time, runtime)
示例#2
0
    def __init__(self, runtime):
        # Save the Runtime for later use
        self.runtime = runtime

        # This is the value we will use in the protocol.
        self.millions = rand.randint(1, 200)
        print "I am Millionaire %d and I am worth %d millions." \
            % (runtime.id, self.millions)

        # For the comparison protocol to work, we need a field modulus
        # bigger than 2**(l+1) + 2**(l+k+1), where the bit length of
        # the input numbers is l and k is the security parameter.
        # Further more, the prime must be a Blum prime (a prime p such
        # that p % 4 == 3 holds). The find_prime function lets us find
        # a suitable prime.
        l = runtime.options.bit_length
        k = runtime.options.security_parameter
        Zp = GF(find_prime(2**(l + 1) + 2**(l + k + 1), blum=True))

        # We must secret share our input with the other parties. They
        # will do the same and we end up with three variables
        m1, m2, m3 = runtime.shamir_share([1, 2, 3], Zp, self.millions)

        # Now that everybody has secret shared their inputs we can
        # compare them. We compare the worth of the first millionaire
        # with the two others, and compare those two millionaires with
        # each other.
        m1_ge_m2 = m1 >= m2
        m1_ge_m3 = m1 >= m3
        m2_ge_m3 = m2 >= m3

        # The results are secret shared, so we must open them before
        # we can do anything usefull with them.
        open_m1_ge_m2 = runtime.open(m1_ge_m2)
        open_m1_ge_m3 = runtime.open(m1_ge_m3)
        open_m2_ge_m3 = runtime.open(m2_ge_m3)

        # We will now gather the results and call the
        # self.results_ready method when they have all been received.
        results = gather_shares([open_m1_ge_m2, open_m1_ge_m3, open_m2_ge_m3])
        results.addCallback(self.results_ready)

        # We can add more callbacks to the callback chain in results.
        # These are called in sequence when self.results_ready is
        # finished. The first callback acts like a barrier and makes
        # all players wait on each other.
        #
        # The callbacks are always called with an argument equal to
        # the return value of the preceeding callback. We do not need
        # the argument (which is None since self.results_ready does
        # not return anything), so we throw it away using a lambda
        # expressions which ignores its first argument.
        runtime.schedule_callback(results, lambda _: runtime.synchronize())
        # The next callback shuts the runtime down, killing the
        # connections between the players.
        runtime.schedule_callback(results, lambda _: runtime.shutdown())
示例#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
import sys

import viff.reactor

viff.reactor.install()
from twisted.internet import reactor

from viff.math.field import GF
from viff.runtime import create_runtime
from viff.runtimes.paillier import PaillierRuntime
from viff.config import load_config
from viff.utils.util import dprint, find_prime

id, players = load_config(sys.argv[1])
Zp = GF(find_prime(2**64))
input = int(sys.argv[2])

print "I am player %d and will input %s" % (id, input)


def protocol(runtime):
    print "-" * 64
    print "Program started"
    print

    a, b = runtime.share([1, 2], Zp, input)
    c = a * b

    dprint("a%d: %s", runtime.id, a)
    dprint("b%d: %s", runtime.id, b)
示例#5
0
viff.reactor.install()
from twisted.internet import reactor

from viff.math.field import GF
from viff.runtime import create_runtime, make_runtime_class
from viff.config import load_config
from viff.utils.util import dprint, find_prime
from viff.mixins.equality import ProbabilisticEqualityMixin
from viff.mixins.comparison import Toft05Runtime

# Load the configuration from the player configuration files.
id, players = load_config(sys.argv[1])

# Initialize the field we do arithmetic over.
field_length = 256
Zp = GF(find_prime(2**field_length, blum=True))


def get_accounts(length, runtime):
    accounts = []

    for x in range(length):
        if x == 9:
            accounts.append(
                runtime.input([1], Zp,
                              0x3134f954AFf7F5F8EB849a80Fb85447E5b2a3696))
        else:
            accounts.append(
                runtime.input([1], Zp, random.randint(0, Zp.field.modulus)))
        accounts.append(runtime.input([1], Zp, random.randint(0, 6000)))
        accounts.append(runtime.input([1], Zp, random.randint(0, 6000)))
示例#6
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()
示例#7
0
 def __init__(self, runtime):
     self.Zp = GF(find_prime(2**64))
     self.runtime = runtime
     self.last_time = time()
     self.share_next(0)