def test_secfld(self): secfld = mpc.SecFld(2, char=2) a = getrandbits(secfld, 1, True) a = mpc.run(mpc.output(a)) self.assertGreaterEqual(int(a[0]), 0) self.assertLessEqual(int(a[0]), 1) secfld = mpc.SecFld(3) a = getrandbits(secfld, 1) a = mpc.run(mpc.output(a)) self.assertGreaterEqual(int(a), 0) self.assertLessEqual(int(a), 1) a = mpc.run(mpc.output(randrange(secfld, 1, 3))) self.assertGreaterEqual(int(a), 1) self.assertLessEqual(int(a), 2) secfld = mpc.SecFld(256) a = getrandbits(secfld, 8) a = mpc.run(mpc.output(a)) self.assertGreaterEqual(int(a), 0) self.assertLessEqual(int(a), 2**8 - 1) a = mpc.run(mpc.output(randrange(secfld, 256))) self.assertGreaterEqual(int(a), 0) self.assertLessEqual(int(a), 2**8 - 1) a = mpc.run(mpc.output(randint(secfld, 0, 2))) self.assertIn(a, [0, 1, 2]) x = mpc.run(mpc.output(random_unit_vector(secfld, 2))) self.assertEqual(int(sum(x)), 1) x = mpc.run(mpc.output(random_permutation(secfld, range(1, 9)))) self.assertSetEqual(set(map(int, x)), set(range(1, 9))) x = mpc.run(mpc.output(random_derangement(secfld, range(2)))) self.assertListEqual(list(map(int, x)), [1, 0]) secfld = mpc.SecFld(257) a = getrandbits(secfld, 8) a = mpc.run(mpc.output(a)) self.assertGreaterEqual(int(a), 0) self.assertLessEqual(int(a), 2**8 - 1) a = mpc.run(mpc.output(randrange(secfld, 257))) self.assertGreaterEqual(int(a), 0) self.assertLessEqual(int(a), 2**8) a = mpc.run(mpc.output(randint(secfld, -1, 1))) self.assertIn(a, [-1, 0, 1]) x = mpc.run(mpc.output(random_unit_vector(secfld, 1))) self.assertEqual(int(sum(x)), 1) x = mpc.run(mpc.output(random_permutation(secfld, range(1, 9)))) self.assertSetEqual(set(map(int, x)), set(range(1, 9))) x = mpc.run(mpc.output(random_derangement(secfld, range(2)))) self.assertListEqual(list(map(int, x)), [1, 0])
def test_secfld(self): secfld = mpc.SecFld(2, char2=True) a = getrandbits(secfld, 1) a = mpc.run(mpc.output(a)) self.assertGreaterEqual(int(a), 0) self.assertLessEqual(int(a), 1) secfld = mpc.SecFld(3) a = getrandbits(secfld, 1) a = mpc.run(mpc.output(a)) self.assertGreaterEqual(int(a), 0) self.assertLessEqual(int(a), 1) a = mpc.run(mpc.output(randrange(secfld, 1, 3))) self.assertGreaterEqual(int(a), 1) self.assertLessEqual(int(a), 2) secfld = mpc.SecFld(256) a = getrandbits(secfld, 8) a = mpc.run(mpc.output(a)) self.assertGreaterEqual(int(a), 0) self.assertLessEqual(int(a), 2**8 - 1) a = mpc.run(mpc.output(randrange(secfld, 256))) self.assertGreaterEqual(int(a), 0) self.assertLessEqual(int(a), 2**8 - 1) a = mpc.run(mpc.output(randint(secfld, 0, 2))) self.assertIn(a, [0, 1, 2]) x = mpc.run(mpc.output(random_unit_vector(secfld, 6))) self.assertEqual(int(sum(x)), 1) x = mpc.run(mpc.output(random_permutation(secfld, range(1, 9)))) self.assertSetEqual({a for a in range(1, 9)}, {int(a) for a in x}) x = mpc.run(mpc.output(random_derangement(secfld, range(2)))) self.assertListEqual([1, 0], [int(a) for a in x]) secfld = mpc.SecFld(257) a = getrandbits(secfld, 8) a = mpc.run(mpc.output(a)) self.assertGreaterEqual(int(a), 0) self.assertLessEqual(int(a), 2**8 - 1) a = mpc.run(mpc.output(randrange(secfld, 257))) self.assertGreaterEqual(int(a), 0) self.assertLessEqual(int(a), 2**8) a = mpc.run(mpc.output(randint(secfld, -1, 1))) self.assertIn(a, [-1, 0, 1]) x = mpc.run(mpc.output(random_unit_vector(secfld, 6))) self.assertEqual(int(sum(x)), 1) x = mpc.run(mpc.output(random_permutation(secfld, range(1, 9)))) self.assertSetEqual({a for a in range(1, 9)}, {int(a) for a in x}) x = mpc.run(mpc.output(random_derangement(secfld, range(2)))) self.assertListEqual([1, 0], [int(a) for a in x])
async def main(): global secnum k = 1 if len(sys.argv) == 1 else float(sys.argv[1]) if k - int(k) == 0.5: secnum = mpc.SecFxp(10, 4) else: secnum = mpc.SecInt(37) batch_size = round(k - 0.01) await mpc.start() if len(sys.argv) <= 2: import mpyc.random as secrnd offset = await mpc.output(secrnd.randrange(secnum, 10001 - batch_size)) else: offset = sys.argv[2] offset = int(offset) f = 6 logging.info('--------------- INPUT -------------') print( f'Type = {secnum.__name__}, range = ({offset}, {offset + batch_size})') # 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[0, 0])) x = scale_to_int(1 << f)(x) logging.info('--------------- LAYER 1 -------------') W, b = load('conv1', f) x = convolvetensor(x, W, b) await mpc.barrier() if secnum.__name__.startswith('SecInt'): secnum.bit_length = 16 x = maxpool(x) await mpc.barrier() x = ReLU(x) await mpc.barrier() logging.info('--------------- LAYER 2 -------------') W, b = load('conv2', f, 3) x = convolvetensor(x, W, b) await mpc.barrier() if secnum.__name__.startswith('SecInt'): secnum.bit_length = 23 x = maxpool(x) await mpc.barrier() x = ReLU(x) await 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) await 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): prediction = int(await mpc.output(argmax(x[i]))) error = '******* ERROR *******' if prediction != labels[i] else '' print( f'Image #{offset+i} with label {labels[i]}: {prediction} predicted. {error}' ) print(await mpc.output(x[i])) await mpc.shutdown()
async def main(): global secint parser = argparse.ArgumentParser() parser.add_argument('-b', '--batch-size', type=int, metavar='B', help='number of images to classify') parser.add_argument('-o', '--offset', type=int, metavar='O', help='offset for batch (otherwise random in [0,10000-B])') parser.add_argument('-d', '--d-k-star', type=int, metavar='D', help='k=D=0,1,2 for Legendre-based comparison using d_k^*') parser.add_argument('--no-legendre', action='store_true', default=False, help='disable Legendre-based comparison') parser.add_argument('--no-vectorization', action='store_true', default=False, help='disable vectorization of comparisons') parser.set_defaults(batch_size=1, offset=-1, d_k_star=1) args = parser.parse_args() batch_size = args.batch_size offset = args.offset if args.no_legendre: secint = mpc.SecInt(14) # using vectorized MPyC integer comparison else: if args.d_k_star == 0: secint = mpc.SecInt(14, p=3546374752298322551) # Legendre-0 range [-134, 134] bsgn = bsgn_0 vector_bsgn = vector_bsgn_0 elif args.d_k_star == 1: secint = mpc.SecInt(14, p=9409569905028393239) # Legendre-1 range [-383, 383] bsgn = bsgn_1 vector_bsgn = vector_bsgn_1 else: secint = mpc.SecInt(14, p=15569949805843283171) # Legendre-2 range [-594, 594] bsgn = bsgn_2 vector_bsgn = vector_bsgn_2 one_by_one = args.no_vectorization await mpc.start() if offset < 0: import mpyc.random as secrnd offset = int(await mpc.output(secrnd.randrange(secint, 10001 - batch_size))) logging.info('--------------- INPUT -------------') print(f'Type = {secint.__name__}, range = ({offset}, {offset + batch_size})') # 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] L = np.array(list(d)).reshape(batch_size, 28**2) if batch_size == 1: x = np.array(L[0]).reshape(28, 28) print(np.vectorize(lambda a: int(bool(a / 255)))(x)) L = np.vectorize(lambda a: secint(int(a)))(L).tolist() logging.info('--------------- LAYER 1 -------------') logging.info('- - - - - - - - fc - - - - - - -') L = mpc.matrix_prod(L, load_W('fc1')) L = mpc.matrix_add(L, [load_b('fc1')] * len(L)) logging.info('- - - - - - - - bsgn - - - - - - -') if one_by_one: L = np.vectorize(lambda a: (a >= 0) * 2 - 1)(L).tolist() else: L = [vector_sge(_) for _ in L] await mpc.barrier() logging.info('--------------- LAYER 2 -------------') logging.info('- - - - - - - - fc - - - - - - -') L = mpc.matrix_prod(L, load_W('fc2')) L = mpc.matrix_add(L, [load_b('fc2')] * len(L)) await mpc.barrier() logging.info('- - - - - - - - bsgn - - - - - - -') if args.no_legendre: secint.bit_length = 10 if one_by_one: activate = np.vectorize(lambda a: (a >= 0) * 2 - 1) L = activate(L).tolist() else: L = [vector_sge(_) for _ in L] else: if one_by_one: activate = np.vectorize(bsgn) L = activate(L).tolist() else: L = [vector_bsgn(_) for _ in L] await mpc.barrier() logging.info('--------------- LAYER 3 -------------') logging.info('- - - - - - - - fc - - - - - - -') L = mpc.matrix_prod(L, load_W('fc3')) L = mpc.matrix_add(L, [load_b('fc3')] * len(L)) await mpc.barrier() logging.info('- - - - - - - - bsgn - - - - - - -') if args.no_legendre: secint.bit_length = 10 if one_by_one: activate = np.vectorize(lambda a: (a >= 0) * 2 - 1) L = activate(L).tolist() else: L = [vector_sge(_) for _ in L] else: if one_by_one: activate = np.vectorize(bsgn) L = activate(L).tolist() else: L = [vector_bsgn(_) for _ in L] await mpc.barrier() logging.info('--------------- LAYER 4 -------------') logging.info('- - - - - - - - fc - - - - - - -') L = mpc.matrix_prod(L, load_W('fc4')) L = mpc.matrix_add(L, [load_b('fc4')] * len(L)) await mpc.barrier() logging.info('--------------- OUTPUT -------------') if args.no_legendre: secint.bit_length = 14 for i in range(batch_size): prediction = await mpc.output(argmax(L[i])) error = '******* ERROR *******' if prediction != labels[i] else '' print(f'Image #{offset+i} with label {labels[i]}: {prediction} predicted. {error}') print(await mpc.output(L[i])) await mpc.shutdown()
def test_secint(self): secint = mpc.SecInt() a = mpc.run(mpc.output(getrandbits(secint, 10))) self.assertGreaterEqual(a, 0) self.assertLessEqual(a, 2**10 - 1) x = mpc.run(mpc.output(random_unit_vector(secint, 4))) self.assertEqual(sum(x), 1) a = mpc.run(mpc.output(randrange(secint, 37))) # French roulette self.assertGreaterEqual(a, 0) self.assertLessEqual(a, 36) a = mpc.run(mpc.output(randrange(secint, 1, 7))) # one die self.assertGreaterEqual(a, 1) self.assertLessEqual(a, 6) a = mpc.run(mpc.output(randrange(secint, 1, 7) + randrange(secint, 1, 7))) # two dice self.assertGreaterEqual(a, 2) self.assertLessEqual(a, 12) a = mpc.run(mpc.output(randrange(secint, 1, 32, 2))) self.assertGreaterEqual(a, 1) self.assertLessEqual(a, 31) self.assertEqual(a % 2, 1) a = mpc.run(mpc.output(randint(secint, -1, 1))) self.assertIn(a, [-1, 0, 1]) x = list(range(8)) shuffle(secint, x) shuffle(secint, x) x = mpc.run(mpc.output(x)) self.assertSetEqual(set(x), set(range(8))) x = mpc.run(mpc.output(random_permutation(secint, 8))) self.assertSetEqual(set(x), set(range(8))) x = mpc.run(mpc.output(random_derangement(secint, 2))) self.assertListEqual(x, [1, 0]) x = mpc.run(mpc.output(random_derangement(secint, range(1, 9)))) self.assertSetEqual(set(x), set(range(1, 9))) x = mpc.run(mpc.output(choice(secint, [1, 2, 3, 4, 5]))) self.assertIn(x, [1, 2, 3, 4, 5]) x = mpc.run(mpc.output(choice(secint, [secint(-100), secint(200), secint(-300)]))) self.assertIn(x, [-100, 200, -300]) x = mpc.run(mpc.output(mpc.sum(choices(secint, [0, 1], k=5)))) # uniform self.assertGreaterEqual(x, 0) self.assertLessEqual(x, 5) x = mpc.run(mpc.output(mpc.sum(choices(secint, [0, 1], [25, 75])))) # Bernoulli self.assertGreaterEqual(x, 0) self.assertLessEqual(x, 1) x = mpc.run(mpc.output(mpc.sum(choices(secint, [0, 1], [25, 75], k=10)))) # binomial self.assertGreaterEqual(x, 0) self.assertLessEqual(x, 10) x = mpc.run(mpc.output(sample(secint, [2**i for i in range(16)], 8))) self.assertGreaterEqual(sum(x), 2**8 - 1) self.assertLessEqual(sum(x), 2**8 * (2**8 - 1)) x = mpc.run(mpc.output(sample(secint, range(10**8), 10))) self.assertGreaterEqual(min(x), 0) self.assertLessEqual(max(x), 10**8 - 1) x = mpc.run(mpc.output(sample(secint, range(1000000, 3000000, 1000), 10))) self.assertGreaterEqual(min(x) // 1000, 1000) self.assertLessEqual(max(x) // 1000, 3000) self.assertEqual(sum(x) % 1000, 0)