Skip to content

Commit

Permalink
Auto-format code using black
Browse files Browse the repository at this point in the history
  • Loading branch information
sbellem authored and Drake-Eidukas committed Jul 2, 2019
1 parent 087e8ce commit 286e754
Show file tree
Hide file tree
Showing 108 changed files with 3,288 additions and 2,152 deletions.
2 changes: 2 additions & 0 deletions .ci/travis-install.sh
Expand Up @@ -7,6 +7,8 @@ pip install --upgrade pip
if [ "${BUILD}" == "tests" ]; then
pip install --upgrade codecov
docker-compose -f .travis.compose.yml build test-hbmpc
elif [ "${BUILD}" == "black" ]; then
pip install --upgrade black
elif [ "${BUILD}" == "flake8" ]; then
pip install --upgrade flake8 pep8-naming
elif [ "${BUILD}" == "docs" ]; then
Expand Down
3 changes: 2 additions & 1 deletion .flake8
Expand Up @@ -3,4 +3,5 @@ max_line_length=89
exclude =
charm/,
.eggs/,
apps/tutorial/
apps/tutorial/
ignore = E203, E266, E501, W503
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
@@ -0,0 +1,6 @@
repos:
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
language_version: python3.7
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -10,6 +10,7 @@ services:
matrix:
include:
- env: BUILD=tests
- env: BUILD=black
- env: BUILD=flake8
- env: BUILD=docs

Expand Down
175 changes: 89 additions & 86 deletions apps/asynchromix/asynchromix.py

Large diffs are not rendered by default.

31 changes: 20 additions & 11 deletions apps/asynchromix/butterfly_network.py
Expand Up @@ -2,14 +2,17 @@
import logging
from math import log
from honeybadgermpc.preprocessing import (
PreProcessedElements, wait_for_preprocessing, preprocessing_done)
PreProcessedElements,
wait_for_preprocessing,
preprocessing_done,
)
from time import time


async def batch_switch(ctx, xs, ys, n):
pp_elements = PreProcessedElements()
sbits = [pp_elements.get_one_minus_one_rand(ctx).v for _ in range(n//2)]
ns = [1 / ctx.field(2) for _ in range(n//2)]
sbits = [pp_elements.get_one_minus_one_rand(ctx).v for _ in range(n // 2)]
ns = [1 / ctx.field(2) for _ in range(n // 2)]

assert len(xs) == len(ys) == len(sbits) == n // 2
xs, ys, sbits = list(map(ctx.ShareArray, [xs, ys, sbits]))
Expand All @@ -25,9 +28,10 @@ async def iterated_butterfly_network(ctx, inputs, k):
# each of which has log k elements. The total number of switches is
# k (log k)^2
assert k == len(inputs)
assert k & (k-1) == 0, "Size of input must be a power of 2"
assert k & (k - 1) == 0, "Size of input must be a power of 2"
bench_logger = logging.LoggerAdapter(
logging.getLogger("benchmark_logger"), {"node_id": ctx.myid})
logging.getLogger("benchmark_logger"), {"node_id": ctx.myid}
)
iteration = 0
num_iterations = int(log(k, 2))
for _ in range(num_iterations):
Expand All @@ -54,7 +58,7 @@ async def iterated_butterfly_network(ctx, inputs, k):


async def butterfly_network_helper(ctx, **kwargs):
k = kwargs['k']
k = kwargs["k"]
pp_elements = PreProcessedElements()
inputs = [pp_elements.get_rand(ctx).v for _ in range(k)]
logging.info(f"[{ctx.myid}] Running permutation network.")
Expand All @@ -70,11 +74,13 @@ async def butterfly_network_helper(ctx, **kwargs):
async def _run(peers, n, t, my_id):
from honeybadgermpc.ipc import ProcessProgramRunner
from honeybadgermpc.progs.mixins.share_arithmetic import (
MixinConstants, BeaverMultiplyArrays)
MixinConstants,
BeaverMultiplyArrays,
)

mpc_config = {MixinConstants.MultiplyShareArray: BeaverMultiplyArrays()}
async with ProcessProgramRunner(peers, n, t, my_id, mpc_config) as runner:
runner.execute('0', butterfly_network_helper, k=k)
runner.execute("0", butterfly_network_helper, k=k)


if __name__ == "__main__":
Expand All @@ -91,15 +97,18 @@ async def _run(peers, n, t, my_id):
NUM_SWITCHES = k * int(log(k, 2)) ** 2
pp_elements = PreProcessedElements()
pp_elements.generate_one_minus_one_rands(
NUM_SWITCHES, HbmpcConfig.N, HbmpcConfig.t)
NUM_SWITCHES, HbmpcConfig.N, HbmpcConfig.t
)
pp_elements.generate_triples(
2 * NUM_SWITCHES, HbmpcConfig.N, HbmpcConfig.t)
2 * NUM_SWITCHES, HbmpcConfig.N, HbmpcConfig.t
)
pp_elements.generate_rands(k, HbmpcConfig.N, HbmpcConfig.t)
preprocessing_done()
else:
loop.run_until_complete(wait_for_preprocessing())

loop.run_until_complete(
_run(HbmpcConfig.peers, HbmpcConfig.N, HbmpcConfig.t, HbmpcConfig.my_id))
_run(HbmpcConfig.peers, HbmpcConfig.N, HbmpcConfig.t, HbmpcConfig.my_id)
)
finally:
loop.close()
30 changes: 17 additions & 13 deletions apps/asynchromix/powermixing.py
Expand Up @@ -10,7 +10,7 @@


async def all_secrets_phase1(context, **kwargs):
k, file_prefixes = kwargs['k'], kwargs['file_prefixes']
k, file_prefixes = kwargs["k"], kwargs["file_prefixes"]
as_, a_minus_b_shares, all_powers = [], [], []

pp_elements = PreProcessedElements()
Expand All @@ -23,13 +23,15 @@ async def all_secrets_phase1(context, **kwargs):
as_.append(a)
all_powers.append(powers)
bench_logger = logging.LoggerAdapter(
logging.getLogger("benchmark_logger"), {"node_id": context.myid})
logging.getLogger("benchmark_logger"), {"node_id": context.myid}
)
bench_logger.info(f"[Phase1] Read shares from file: {time() - stime}")

stime = time()
opened_shares = await context.ShareArray(a_minus_b_shares).open()
bench_logger.info(
f"[Phase1] Open [{len(a_minus_b_shares)}] a-b shares: {time() - stime}")
f"[Phase1] Open [{len(a_minus_b_shares)}] a-b shares: {time() - stime}"
)

stime = time()
for i in range(k):
Expand Down Expand Up @@ -61,9 +63,7 @@ async def phase2(node_id, run_id, file_prefix):

async def run_command_sync(command):
proc = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate()

Expand All @@ -74,13 +74,14 @@ async def run_command_sync(command):


async def phase3(context, **kwargs):
k, run_id = kwargs['k'], kwargs['run_id']
k, run_id = kwargs["k"], kwargs["run_id"]
sum_file_name = f"power-{run_id}_{context.myid}.sums"
sum_file_path = f"{PreProcessingConstants.SHARED_DATA_DIR}{sum_file_name}"
sum_shares = []

bench_logger = logging.LoggerAdapter(
logging.getLogger("benchmark_logger"), {"node_id": context.myid})
logging.getLogger("benchmark_logger"), {"node_id": context.myid}
)

stime = time()
with open(sum_file_path, "r") as f:
Expand Down Expand Up @@ -137,7 +138,7 @@ async def async_mixing_in_processes(network_info, n, t, k, run_id, node_id):

file_prefixes = [uuid.uuid4().hex for _ in range(k)]
async with ProcessProgramRunner(network_info, n, t, node_id) as runner:
await runner.execute('0', all_secrets_phase1, k=k, file_prefixes=file_prefixes)
await runner.execute("0", all_secrets_phase1, k=k, file_prefixes=file_prefixes)
logging.info("Phase 1 completed.")

pool = TaskPool(256)
Expand All @@ -147,12 +148,15 @@ async def async_mixing_in_processes(network_info, n, t, k, run_id, node_id):
await pool.close()

bench_logger = logging.LoggerAdapter(
logging.getLogger("benchmark_logger"), {"node_id": HbmpcConfig.my_id})
logging.getLogger("benchmark_logger"), {"node_id": HbmpcConfig.my_id}
)

bench_logger.info(f"[Phase2] Execute CPP code for all secrets: {time() - stime}")
bench_logger.info(
f"[Phase2] Execute CPP code for all secrets: {time() - stime}"
)
logging.info("Phase 2 completed.")

power_sums = await runner.execute('1', phase3, k=k, run_id=run_id)
power_sums = await runner.execute("1", phase3, k=k, run_id=run_id)

logging.info("Shares from C++ phase opened.")
stime = time()
Expand All @@ -176,7 +180,7 @@ async def async_mixing_in_processes(network_info, n, t, k, run_id, node_id):
if not HbmpcConfig.skip_preprocessing:
# Need to keep these fixed when running on processes.
field = GF(Subgroup.BLS12_381)
a_s = [field(i) for i in range(1000+k, 1000, -1)]
a_s = [field(i) for i in range(1000 + k, 1000, -1)]

pp_elements = PreProcessedElements()
if HbmpcConfig.my_id == 0:
Expand Down
6 changes: 3 additions & 3 deletions apps/asynchromix/solver/solver.py
Expand Up @@ -4,7 +4,7 @@


def int2hexbytes(n):
return bytes(hex(n), 'ascii')[2:]
return bytes(hex(n), "ascii")[2:]


P = Subgroup.BLS12_381
Expand All @@ -31,9 +31,9 @@ def solve(dc_sums):
or if my_message is not a solution.
"""

ffi_sums = [ffi.new('char[]', int2hexbytes(s)) for s in dc_sums]
ffi_sums = [ffi.new("char[]", int2hexbytes(s)) for s in dc_sums]
# Allocate result buffers (size of P in hex + 1 null char)
ffi_messages = [ffi.new('char[]', len(_HEX_P) + 1) for _ in dc_sums]
ffi_messages = [ffi.new("char[]", len(_HEX_P) + 1) for _ in dc_sums]

res = lib.solve(ffi_messages, _HEX_P, ffi_sums, len(dc_sums))

Expand Down
17 changes: 8 additions & 9 deletions apps/asynchromix/solver/solver_build.py
Expand Up @@ -6,18 +6,17 @@
here = os.path.abspath(os.path.dirname(__file__))
ffibuilder = FFI()

with open(os.path.join(here, 'solver.cpp')) as cpp:
with open(os.path.join(here, "solver.cpp")) as cpp:
ffibuilder.set_source(
'lib_solver',
cpp.read(),
source_extension='.cpp',
libraries=['gmp', 'flint'],
)
"lib_solver", cpp.read(), source_extension=".cpp", libraries=["gmp", "flint"]
)

ffibuilder.cdef("""
ffibuilder.cdef(
"""
int solve(char* out_messages[], const char* prime,
const char* sums[], size_t n);
""")
"""
)

if __name__ == '__main__':
if __name__ == "__main__":
ffibuilder.compile(verbose=True)
29 changes: 20 additions & 9 deletions apps/tutorial/hbmpc-tutorial-1.py
Expand Up @@ -4,14 +4,25 @@
import asyncio
from honeybadgermpc.mpc import TaskProgramRunner
from honeybadgermpc.progs.mixins.dataflow import (
Share, ShareArray, ShareFuture, GFElementFuture)
Share,
ShareArray,
ShareFuture,
GFElementFuture,
)
from honeybadgermpc.preprocessing import (
PreProcessedElements as FakePreProcessedElements)
PreProcessedElements as FakePreProcessedElements,
)
from honeybadgermpc.utils.typecheck import TypeCheck
from honeybadgermpc.progs.mixins.share_arithmetic import (
MixinConstants, BeaverMultiply, BeaverMultiplyArrays)
config = {MixinConstants.MultiplyShareArray: BeaverMultiplyArrays(),
MixinConstants.MultiplyShare: BeaverMultiply(), }
MixinConstants,
BeaverMultiply,
BeaverMultiplyArrays,
)

config = {
MixinConstants.MultiplyShareArray: BeaverMultiplyArrays(),
MixinConstants.MultiplyShare: BeaverMultiply(),
}


@TypeCheck()
Expand Down Expand Up @@ -84,7 +95,7 @@ async def prog(ctx):
Y = await y.open()
XY = await xy.open()
assert XY == X * Y
print(f'[{ctx.myid}] Beaver Multiplication OK')
print(f"[{ctx.myid}] Beaver Multiplication OK")
# print(f'x:{y} y:{x}: xy:{xy}')
# print(f'x.open(): {X} y.open(): {Y} xy.open(): {XY}')

Expand All @@ -97,7 +108,7 @@ async def prog(ctx):
res = dot_product(ctx, (a, b), (c, d))
res_ = await res.open()
assert res_ == res
print(f'[{ctx.myid}] Dot Product OK')
print(f"[{ctx.myid}] Dot Product OK")

# Randomly permute (x,y) or (y,x)
o1, o2 = await random_permute_pair(ctx, x, y)
Expand All @@ -106,7 +117,7 @@ async def prog(ctx):
O2 = await o2.open()
# print(f'O1:{O1} O2:{O2}')
assert O1 in (X, Y) and O2 in (X, Y)
print(f'[{ctx.myid}] Permute Pair OK')
print(f"[{ctx.myid}] Permute Pair OK")


async def tutorial_1():
Expand All @@ -130,6 +141,6 @@ def main():
# loop.run_until_complete(tutorial_2())


if __name__ == '__main__':
if __name__ == "__main__":
main()
print("Tutorial 1 ran successfully")

0 comments on commit 286e754

Please sign in to comment.