Ejemplo n.º 1
0
def main():
    if mpc.args:
        n = int(mpc.args[0])
    else:
        n = 5
        print('Setting input to default =', n)

    s = [(-1)**i * (i + (n//2))**2 for i in range(n)]

    mpc.start()

    global secnum

    secnum = mpc.SecInt()
    print('Using secure integers:', secnum)
    x = list(map(secnum, s))
    print('Array:', mpc.run(mpc.output(x)))
    print('Sorted array:', mpc.run(mpc.output(bsort(x))))

    secnum = mpc.SecFxp()
    print('Using secure fixed-point numbers:', secnum)
    x = list(map(secnum, s))
    print('Input array:', mpc.run(mpc.output(x)))
    print('Sorted array:', mpc.run(mpc.output(bsort(x))))

    mpc.shutdown()
Ejemplo n.º 2
0
def main():
    if not mpc.args:
        m = 8
        print('Setting input to default =', m)
    else:
        m = int(mpc.args[0])

    mpc.start()

    secfld = mpc.SecFld(l=max(len(mpc.parties), (m - 1)).bit_length() + 1)
    print('Using secure fields:', secfld)
    for n in range(2, m + 1):
        print(n, mpc.run(mpc.output(random_derangement(n, secfld))))

    secint = mpc.SecInt()
    print('Using secure integers:', secint)
    for n in range(2, m + 1):
        print(n, mpc.run(mpc.output(random_derangement(n, secint))))

    secfxp = mpc.SecFxp()
    print('Using secure fixed-point numbers:', secfxp)
    for n in range(2, m + 1):
        print(n, mpc.run(mpc.output(random_derangement(n, secfxp))))

    mpc.shutdown()
Ejemplo n.º 3
0
def main():
    data = pd.read_csv(DATA_PATH)

    mpc.run(mpc.start())

    n = len(mpc.parties)
    s = [secint(x) for x in range(1, 11)]

    samples = []
    for j in range(20):
        #s = [secint(x) for x in range(1, n + 1)]
        print("Iteration: %d, Sample %d" % (n, j))
        start = time.time()
        mpc.run(mpc.output(many_secure_mul(s, 100)))
        end = time.time()
        samples.append(end - start)

    mpc.run(mpc.shutdown())

    avg = np.average(samples)
    data.loc[data.shape[0]] = [n, avg]
    data.to_csv(DATA_PATH, index=False)

    return 0
Ejemplo n.º 4
0
def run(args):
    model = None
    # 获取训练和测试数据
    data = get_data(args.model)[0]
    test_data = get_data(args.model)[1]
    # 创建模型结果的目录
    if not os.path.exists('results'):
        os.makedirs('results')
    if len(os.listdir('results')) > 0:
        shutil.rmtree('results')
        os.makedirs('results')
    # 初始化模型
    model = GradientBoostingRegressor(learning_rate=args.lr,
                                      n_trees=args.trees,
                                      max_depth=args.depth,
                                      min_samples_split=args.count,
                                      is_log=args.log,
                                      is_plot=args.plot)

    # 训练模型
    mpc.run(mpc.start())
    model.fit(data)
    # 记录日志
    logger.removeHandler(logger.handlers[-1])
    logger.addHandler(
        logging.FileHandler('results/result.log'.format(iter),
                            mode='w',
                            encoding='utf-8'))
    logger.info(data)
    # 模型预测
    model.predict(test_data)
    # 记录日志
    logger.setLevel(logging.INFO)
    logger.info((test_data['predict_value']))
    mpc.run(mpc.shutdown())
    pass
Ejemplo n.º 5
0
Run with m parties to compute:

 - m    =  sum_{i=0}^{m-1} 1    =  sum(1     for i in range(m))
 - m**2 =  sum_{i=0}^{m-1} 2i+1 =  sum(2*i+1 for i in range(m))
 - 2**m = prod_{i=0}^{m-1} 2    = prod(2     for i in range(m))
 - m!   = prod_{i=0}^{m-1} i+1  = prod(i+1   for i in range(m))

Bit lengths of secure integers ensure each result fits for any m >= 1.
"""

from mpyc.runtime import mpc

m = len(mpc.parties)
l = m.bit_length()

mpc.run(mpc.start())
print('m    =', mpc.run(mpc.output(mpc.sum(mpc.input(mpc.SecInt(l + 1)(1))))))
print(
    'm**2 =',
    mpc.run(
        mpc.output(mpc.sum(mpc.input(mpc.SecInt(2 * l + 1)(2 * mpc.pid +
                                                           1))))))
print('2**m =', mpc.run(mpc.output(mpc.prod(mpc.input(mpc.SecInt(m + 2)(2))))))
print(
    'm!   =',
    mpc.run(
        mpc.output(
            mpc.prod(mpc.input(
                mpc.SecInt(int(m * (l - 1.4) + 3))(mpc.pid + 1))))))
mpc.run(mpc.shutdown())
Ejemplo n.º 6
0
to run the demo with m=3 parties on localhost.

When run with m parties, a total of m(m-1)/2 TCP connections will be created between
all parties, noting that TCP connections are full-duplex (bidirectional). So there are
no connections when run with m=1 party only, there is one connection for m=2 parties, and
there are three connections for m=3 parties.

With all m parties running on localhost, your OS may run out of available ports for large m,
and the program will therefore not terminate properly. For example, the default range for
dynamic (private) ports on Windows is 49152-65535, which will take you to around m=180 parties,
before exhausting the available ports. The range for dynamic ports can be increased like this,
requiring administrator privileges:

    netsh int ipv4 set dynamicport tcp start=16000 num=48000

Now run the demo (as a nice stress test) with m=300, for a total of 44850 TCP connections:

    python helloworld.py -M300 -T0

It it essential to use threshold t=0 (or, maybe t=1). Otherwise the time needed to set up
the PRSS keys, which is proportional to (m choose t) = m!/t!/(m-t)!, will be prohibitive.
"""

from mpyc.runtime import mpc

mpc.run(mpc.start())  # connect to all other parties
print(''.join(mpc.run(mpc.transfer('Hello world!'))))
mpc.run(mpc.shutdown()
        )  # disconnect, but only once all other parties reached this point
Ejemplo n.º 7
0
 def setUpClass(cls):
     mpc.logging(False)
     mpc.run(mpc.start())
Ejemplo n.º 8
0
# its local parameters
if not IS_SERVER:
    np_votes = np.load(f'DATA/{DATASET}/votes_client_{my_pid}.npy'
                       )  # For this PoC the path is hardcoded here
    # Compute my local sigma1 and sigma2, local fraction of the total
    # variance respecitively SIGMA1 and SIGMA2
    # The sum of all sigma^2 of all parties is equal to SIGMA^2
    nb_teachers = int(sum(np_votes[0]))
    sigma1 = float(
        np.sqrt(float(nb_teachers) / float(TOTAL_NB_TEACHERS)) * SIGMA1)
    sigma2 = float(
        np.sqrt(float(nb_teachers) / float(TOTAL_NB_TEACHERS)) * SIGMA2)

###############################################################################
print('\n' + "=" * 50)
mpc.run(mpc.start())  #### START THE MPC ROUNDS OF COMPUTATION ####
print("=" * 50, '\n')
###############################################################################

# Reset the timer before the actual computation begins
last_time = time.time()

# Iterate over all samples in the public dataset
for sample_id in range(NB_SAMPLES):

    if not IS_SERVER:
        # Take the sample_id°th votes of the list
        my_votes = list(map(int, np_votes[sample_id]))

        # Convert the votes to secure type
        my_sec_votes = list(map(secfxp, my_votes))
Ejemplo n.º 9
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--data', help='Filename for tableau.')
    parser.add_argument('options', nargs='*')
    parser.set_defaults(data='default')
    args = parser.parse_args(mpc.args)

    if not args.options:
        certificate_filename = "c" + str(mpc.id) + ".cert"
        logging.info('Setting certificate file to default = %s',
                     certificate_filename)
    else:
        certificate_filename = args.options[0]
    T = load_tableau(args.data)
    m = len(T) - 1
    n = len(T[0]) - 1
    l = mpc.options.bit_length
    secfxp = mpc.SecFxp(l)
    for i in range(len(T)):
        for j in range(len(T[0])):
            T[i][j] = secfxp(T[i][j])

    basis = [secfxp(i + n) for i in range(m)]
    cobasis = [secfxp(j) for j in range(n)]

    mpc.start()

    iteration = 0
    logging.info('%d Termination?...', iteration)
    p_col_index, minimum = argmin_int(T[-1][:-1])

    while mpc.run(mpc.output(minimum < 0)):
        iteration += 1

        logging.info('%d Determining pivot...', iteration)
        p_col = index_matrix_prod(p_col_index + [secfxp(0)], T, True)
        constraints = [(T[i][-1] + (p_col[i] <= 0), p_col[i])
                       for i in range(m)]
        p_row_index, _ = argmin_rat(constraints)
        pivot = mpc.in_prod(p_row_index, p_col)
        pivot1 = 1 / pivot

        mpc.run(mpc.barrier())

        logging.info('%d Updating tableau...', iteration)
        h = mpc.scalar_mul(pivot1, [(p_row_index[i] if i < m else 0) - p_col[i]
                                    for i in range(m + 1)])
        p_row = index_matrix_prod(p_row_index, T[:-1])
        v = mpc.vector_add(p_row, p_col_index + [0])
        for i in range(m + 1):
            T[i] = mpc.vector_add(T[i], mpc.scalar_mul(h[i], v))

        #swap basis entries
        delta = mpc.in_prod(basis, p_row_index) - mpc.in_prod(
            cobasis, p_col_index)
        p_row_index = mpc.scalar_mul(delta, p_row_index)
        basis = mpc.vector_sub(basis, p_row_index)
        p_col_index = mpc.scalar_mul(delta, p_col_index)
        cobasis = mpc.vector_add(cobasis, p_col_index)

        logging.info('%d Termination?...', iteration)
        p_col_index, minimum = argmin_int(T[-1][:-1])

    logging.info('Termination...')
    mx = mpc.run(mpc.output(T[-1][-1]))
    print(' max(f) =', mx)

    logging.info('Computing solution...')
    solution = [secfxp(0) for _ in range(n)]
    for i in range(m):
        x = unit_vector(basis[i], m + n)[:n]
        y = mpc.scalar_mul(T[i][-1], x)
        solution = mpc.vector_add(solution, y)
    solution = mpc.run(mpc.output(solution))

    logging.info('Computing dual solution...')
    dual_solution = [secfxp(0) for _ in range(m)]
    for j in range(n):
        x = unit_vector(cobasis[j], m + n)[n:]
        y = mpc.scalar_mul(T[-1][j], x)
        dual_solution = mpc.vector_add(dual_solution, y)
    dual_solution = mpc.run(mpc.output(dual_solution))

    mpc.shutdown()

    logging.info('Writing output to %s.', certificate_filename)
    with open(os.path.join('data', 'lp', certificate_filename), 'w') as f:
        f.write('# tableau = \n' + args.data + '\n')
        f.write('# bit-length = \n' + str(mpc.options.bit_length) + '\n')
        f.write('# security parameter = \n' +
                str(mpc.options.security_parameter) + '\n')
        f.write('# threshold = \n' + str(mpc.threshold) + '\n')
        f.write('# solution = \n')
        f.write('\t'.join(x.__repr__() for x in solution) + '\n')
        f.write('# dual solution = \n')
        f.write('\t'.join(x.__repr__() for x in dual_solution) + '\n')
Ejemplo n.º 10
0
# |---|:----------------------|
# | 2 | <p align="left">[1,0]  |
# | 3 | <p align="left">[1,2,0], [2,0,1]  |
# | 4 | <p align="left">[1,0,3,2], [1,2,3,0], [1,3,0,2], [2,0,3,1], [2,3,0,1], [2,3,1,0], [3,0,1,2], [3,2,0,1], [3,2,1,0]  |
#
# To represent *secret-shared* derangements, we will use a secure MPyC type of integers. For simplicity, we choose 32-bit (default) secure integers.

# In[2]:

secint = mpc.SecInt()  # 32-bit secure MPyC integers

# Finally, we start the `mpc` runtime, which means that point-to-point connections between each pair of parties will be established.

# In[3]:

mpc.run(mpc.start())  # required only when run with multiple parties

# Insertion of this call ensures that the Python code can also be run with multiple parties, as shown at the end of this notebook.

# #### _Quick note on MPyC coroutines_
#
# All computations pertaining to secure integers of type `secint` are done asynchronously in MPyC. The implementation of MPyC builds heavily on Python's `asyncio` module. Secure computations in MPyC are in fact implemented as MPyC coroutines, which are a special type of Python coroutines.
#
# For example, consider the following code fragment.

# In[4]:

a = secint(5)
b = secint(13)
c, d = a + b, a - b  # c, d are placeholders of type secint
e = c * d  # e is a placeholder of type secint
Ejemplo n.º 11
0
First, each party broadcasts a random number to all parties.
Then, the i-th party broadcasts the i-th smallest number to all parties.
This effectively sorts the random numbers.

The protocol provides no secrecy but shows that a parallel computation can
be seen as a special case of a secure multiparty computation. Accordingly, 
we set the threshold for the number of corrupt parties simply to 0.
"""

import random
from mpyc.runtime import mpc

mpc.threshold = 0  # no secret sharing

mpc.start()

secint = mpc.SecInt()
print('Using secure integers:', secint)
x = mpc.run(mpc.output(mpc.input(secint(random.randint(0, 99)))))
print('Random inputs, one per party: ', x)
x = [a.signed() for a in x]
x.sort()
x = mpc.run(mpc.output(mpc.input(secint(x[mpc.id]))))
print('Sorted outputs, one per party: ', x)

secfxp = mpc.SecFxp()
print('Using secure fixed-point numbers:', secfxp)
x = mpc.run(mpc.output(mpc.input(secfxp(0.5 - random.randint(0, 99)))))
print('Random inputs, one per party: ', x)
x = [a.signed() for a in x]
Ejemplo n.º 12
0
 def setUpClass(cls):
     mpc.start()
Ejemplo n.º 13
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--data', help='Filename for tableau.')
    parser.add_argument('options', nargs='*')
    parser.set_defaults(data='default')
    args = parser.parse_args(mpc.args)

    if not args.options:
        certificate_filename = 'c' + str(mpc.id) + '.cert'
        logging.info('Setting certificate file to default = %s', certificate_filename)
    else:
        certificate_filename = args.options[0]
    T = load_tableau(args.data)
    l = mpc.options.bit_length
    m = len(T) - 1
    n = len(T[0]) - 1
    secint = mpc.SecInt(l, m + n)
    for i in range(len(T)):
        for j in range(len(T[0])):
            T[i][j] = secint(T[i][j])

    Zp = secint.field
    p = Zp.modulus
    N = Zp.nth
    w = Zp.root
    w_powers = [Zp(1)]
    for _ in range(N - 1):
        w_powers.append(w_powers[-1] * w)
    assert w_powers[-1] * w == 1

    basis = [secint(w_powers[-(i+n)]) for i in range(m)]
    cobasis = [secint(w_powers[-j]) for j in range(n)]
    prev_pivot = secint(1)

    mpc.start()

    iteration = 0
    logging.info('%d Termination?...', iteration)
    p_col_index, minimum = argmin_int(T[-1][:-1])
    while mpc.run(mpc.output(minimum < 0)):
        iteration += 1

        logging.info('%d Determining pivot...', iteration)
        p_col = mpc.matrix_prod([p_col_index], T, True)[0]
        constraints = [(T[i][-1] + (p_col[i] <= 0), p_col[i]) for i in range(m)]
        p_row_index, (_, pivot) = argmin_rat(constraints)

        logging.info('%d Updating tableau...', iteration)
        #  T[i,j] = T[i,j]*p/p' - (C[i]/p' - p_row_index[i])*(R[j] + p * p_col_index[j])
        p_row = mpc.matrix_prod([p_row_index], T)[0]
        delta_row = mpc.scalar_mul(prev_pivot, p_col_index)
        delta_row.append(secint(0))
        p_row = mpc.vector_add(p_row, delta_row)
        prev_p_inv = 1 / prev_pivot
        p_col = mpc.scalar_mul(prev_p_inv, p_col)
        p_col = mpc.vector_sub(p_col, p_row_index + [secint(0)])
        T = mpc.gauss(T, pivot * prev_p_inv, p_col, p_row)
        prev_pivot = pivot
        # swap basis entries
        delta = mpc.in_prod(basis, p_row_index) - mpc.in_prod(cobasis, p_col_index)
        p_row_index = mpc.scalar_mul(delta, p_row_index)
        basis = mpc.vector_sub(basis, p_row_index)
        p_col_index = mpc.scalar_mul(delta, p_col_index)
        cobasis = mpc.vector_add(cobasis, p_col_index)

        logging.info('%d Termination?...', iteration)
        p_col_index, minimum = argmin_int(T[-1][:-1])

    logging.info('Termination...')
    mx = mpc.run(mpc.output(T[-1][-1]))
    cd = mpc.run(mpc.output(prev_pivot))
    print(' max(f) = %d / %d = %f' % (mx.value, cd.value, float(mx.value)/cd.value))

    logging.info('Computing solution...')
    sum_x_powers = [secint(0) for _ in range(N)]
    for i in range(m):
        x_powers = pow_list(T[i][-1] / N, basis[i], N)
        sum_x_powers = mpc.vector_add(sum_x_powers, x_powers)
    solution = [None] * n
    for j in range(n):
        coefs = [w_powers[(j*k)%N] for k in range(N)]
        solution[j] = mpc.lin_comb(coefs, sum_x_powers)
    solution = mpc.run(mpc.output(solution))

    logging.info('Computing dual solution...')
    sum_x_powers = [secint(0) for _ in range(N)]
    for j in range(n):
        x_powers = pow_list(T[-1][j] / N, cobasis[j], N)
        sum_x_powers = mpc.vector_add(sum_x_powers, x_powers)
    dual_solution = [None] * m
    for i in range(m):
        coefs = [w_powers[((n+i)*k)%N] for k in range(N)]
        dual_solution[i] = mpc.lin_comb(coefs, sum_x_powers)
    dual_solution = mpc.run(mpc.output(dual_solution))

    mpc.shutdown()

    logging.info('Writing output to %s.', certificate_filename)
    with open(os.path.join('data', 'lp', certificate_filename), 'w') as f:
        f.write('# tableau = \n' + args.data + '\n')
        f.write('# bit-length = \n' + str(mpc.options.bit_length) + '\n')
        f.write('# security parameter = \n' + str(mpc.options.security_parameter) + '\n')
        f.write('# threshold = \n' + str(mpc.threshold) + '\n')
        f.write('# common denominator = \n' + str(cd.value) + '\n')
        f.write('# solution = \n')
        f.write('\t'.join(str(x.value) for x in solution) + '\n')
        f.write('# dual solution = \n')
        f.write('\t'.join(str(x.value) for x in dual_solution) + '\n')
Ejemplo n.º 14
0
def main():
    global secnum

    k = 1 if len(mpc.args) == 0 else float(mpc.args[0])
    if k - int(k) == 0.5:
        secnum = mpc.SecFxp(10, 4)
    else:
        secnum = mpc.SecInt(37)
    batch_size = round(k - 0.01)
    if len(mpc.args) <= 1:
        offset = random.randrange(10001 - batch_size)
    else:
        offset = int(mpc.args[1])
    f = 6

    mpc.start()

    logging.info("--------------- INPUT   -------------")
    print('SecNum type = %s, range = (%d, %d)' % (secnum.__name__, offset, offset + k))
    # read batch_size labels and images at given offset
    df = gzip.open(os.path.join('data', 'cnn', 't10k-labels-idx1-ubyte.gz'))
    d = df.read()[8 + offset: 8 + offset + batch_size]
    labels = list(map(int, d))
    print('Labels:', labels)
    df = gzip.open(os.path.join('data', 'cnn', 't10k-images-idx3-ubyte.gz'))
    d = df.read()[16 + offset * 28**2: 16 + (offset + batch_size) * 28**2]
    x = list(map(lambda a: a / 255, d))
    x = np.array(x).reshape(batch_size, 1, 28, 28)
    if batch_size == 1:
        print(np.vectorize(lambda a: int(bool(a)))(x))
    x = scale_to_int(1 << f)(x)

    logging.info("--------------- LAYER 1 -------------")
    W, b = load('conv1', f)
    x = convolvetensor(x, W, b)
    mpc.run(mpc.barrier())
    if secnum.__name__.startswith('SecInt'):
        secnum.bit_length = 16
    x = maxpool(x)
    mpc.run(mpc.barrier())
    x = ReLU(x)
    mpc.run(mpc.barrier())

    logging.info("--------------- LAYER 2 -------------")
    W, b = load('conv2', f, 3)
    x = convolvetensor(x, W, b)
    mpc.run(mpc.barrier())
    if secnum.__name__.startswith('SecInt'):
        secnum.bit_length = 23
    x = maxpool(x)
    mpc.run(mpc.barrier())
    x = ReLU(x)
    mpc.run(mpc.barrier())

    logging.info("--------------- LAYER 3 -------------")
    x = x.reshape(batch_size, 64 * 7**2)
    W, b = load('fc1', f, 4)
    x = tensormatrix_prod(x, W, b)
    if secnum.__name__.startswith('SecInt'):
        secnum.bit_length = 30
    x = ReLU(x)
    mpc.run(mpc.barrier())

    logging.info("--------------- LAYER 4 -------------")
    W, b = load('fc2', f, 5)
    x = tensormatrix_prod(x, W, b)

    logging.info("--------------- OUTPUT  -------------")
    if secnum.__name__.startswith('SecInt'):
        secnum.bit_length = 37
    for i in range(batch_size):
        print(labels[i], mpc.run(mpc.output(argmax(x[i])[0])))
        print(mpc.run(mpc.output(x[i])))

    mpc.shutdown()