Exemplo 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()
Exemplo 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()
Exemplo 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
Exemplo 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
Exemplo 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())
Exemplo n.º 6
0
 def tearDownClass(cls):
     mpc.run(mpc.shutdown())
Exemplo n.º 7
0
        # Compute the secure argmax on this vector of aggregated noisy votes
        sec_argmax = argmax(total_sec_noisy_votes)

        # Our label is the revealed (=recombined) argmax
        label = mpc.run(mpc.output(sec_argmax, receivers=[SERVER_ID]))

        if label and IS_SERVER:  # Should be a tautology as the server is the ONLY one receiving the 'label' variable
            label = int(label)
            print(f'[*] Sample {sample_id}: {label}')
            LABELS[sample_id] = label

            elapsed = time.time() - last_time
            # print('time elapsed:', elapsed)
            csv_writer.writerow([sample_id, label, elapsed])
            last_time = time.time()

        continue  # To the next sample_id

###############################################################################
print('\n' + '=' * 50)
mpc.run(mpc.shutdown())  #### END THE MPC ROUNDS OF COMPUTATION ####
print('=' * 50)
###############################################################################

# At the end of the computation, store these labels to a .npy file
# if IS_SERVER:
#     np.save(f'labels_{DATASET}_{len(mpc.parties)-1}c_{NB_SAMPLES}s_{timestamp}.npy', LABELS)

# Close the CSV file used for benchmark
csv_file.close()
Exemplo n.º 8
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')
Exemplo n.º 9
0
print('Random permutations:')
for n in range(1, N + 1):
    s = mpc.run(mpc.output(random_permutation(n)))
    print(f'{n:2} {s}')

# In[11]:

print('Random derangements:')
for n in range(2, N + 1):
    s = mpc.run(mpc.output(random_derangement(n)))
    print(f'{n:2} {s}')

# In[12]:

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

# In[ ]:

import sys
sys.exit(
)  # stop execution here when this notebook is run as a Python script, see below

# ## Deploying the Python code with multiple parties
#
# All the Python code contained in this notebook can be saved as a Python script using the `Download as` option from the `File` menu in Jupyter notebooks. Choose to download the notebook as `Python (.py)`.
#
# We have done so and the resulting file [SecretSantaExplained.py](SecretSantaExplained.py) is stored in the same directory as the present notebook. Now we can run the Python script, as shown below.
#
# First we show a run with one party only. However, this time the code runs outside the Jupyter notebook, using its own Python interpreter. Once the run is completed, the screen output is displayed in the output area below the cell:
#
Exemplo n.º 10
0
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]
x.sort()
x = mpc.run(mpc.output(mpc.input(secfxp(x[mpc.id]))))
print('Sorted outputs, one per party: ', x)

mpc.shutdown()
Exemplo n.º 11
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')
Exemplo n.º 12
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()