Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
exfinen committed Dec 26, 2024
1 parent 92c0dc2 commit b22a762
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
5 changes: 2 additions & 3 deletions mpc_demo_infra/client_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
CMD_TLSN_PROVER = "./binance_prover"

def locate_binance_prover():
# binance_prover is expected to be in the current working dir or missing.
# if missing, tlsn directory is expected to exist so that binance_prover
# can be built from the source.
# binance_prover is expected to be in the current working dir or
# built from the source
binance_provers = [
(Path('.').resolve(), CMD_TLSN_PROVER),
(TLSN_BINARY_PATH, CMD_TLSN_PROVER),
Expand Down
2 changes: 2 additions & 0 deletions mpc_demo_infra/client_lib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ def __init__(self, hosts, port_base, client_id, certs_path, cert_file, key_file,
for i, hostname in enumerate(hosts):
for j in range(10000):
try:
logging.info("Establishing socket connection to %s:%d...", hostname, port_base + i)
plain_socket = socket.create_connection(
(hostname, port_base + i), timeout=timeout)
logging.info("Etablished")
break
except ConnectionRefusedError:
if j < 600:
Expand Down
6 changes: 4 additions & 2 deletions mpc_demo_infra/client_lib/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ def run_data_sharing_client(
input_value: int,
nonce: str,
):
logger.info(f"Setting up data sharing client with {party_hosts=}, {port_base=}...")
client = Client(party_hosts, port_base, client_id, certs_path, cert_file, key_file, CLIENT_TIMEOUT)

for socket in client.sockets:
os = octetStream()
os.store(0)
os.Send(socket)

logger.info("Sending private inputs to computation party servers ...")
client.send_private_inputs([input_value, reverse_bytes(hex_to_int(nonce))])
logger.info("Finish sending private inputs")
logger.info("Finished sending private inputs")
outputs = client.receive_outputs(1)
logger.info(f"!@# data_sharing_client.py outputs: {outputs}")
commitment = outputs[0]
Expand Down Expand Up @@ -175,7 +177,7 @@ async def share_data(
if await validate_computation_key(coordination_server_url, access_key, computation_key) == False:
raise Exception(f"Computation key is invalid")
else:
logger.info(f"Validated computation key is {computation_key}")
logger.info(f"Validated computation key: {computation_key}")

client_id, cert_path, key_path = await generate_client_cert(MAX_CLIENT_ID, all_certs_path, client_id)
with open(cert_path, "r") as cert_file:
Expand Down
9 changes: 7 additions & 2 deletions mpc_demo_infra/computation_party_server/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def request_sharing_data_mpc(request: RequestSharingDataMPCRequest, db: Session
client_id = request.client_id
client_port_base = request.client_port_base
client_cert_file = request.client_cert_file
logger.info(f"Requesting sharing data MPC for {secret_index=}")
logger.info(f"Requesting sharing data MPC for {secret_index=}, {client_id=}, {mpc_port_base=}, {client_port_base=}")
if secret_index >= MAX_DATA_PROVIDERS:
raise HTTPException(status_code=400, detail=f"Secret index {secret_index} exceeds the maximum {MAX_DATA_PROVIDERS}")
# 1. Verify TLSN proof
Expand All @@ -81,16 +81,19 @@ def request_sharing_data_mpc(request: RequestSharingDataMPCRequest, db: Session
temp_file.write(tlsn_proof.encode('utf-8'))

# Run TLSN proof verifier
logger.info("Verifying TLSN proof...")
try:
subprocess.run(
f"cd {str(TLSN_VERIFIER_PATH)} && {CMD_VERIFY_TLSN_PROOF} {temp_file.name}",
f"{CMD_VERIFY_TLSN_PROOF} {temp_file.name}",
cwd=TLSN_VERIFIER_PATH,
check=True,
shell=True,
capture_output=True,
)
except subprocess.CalledProcessError as e:
logger.error(f"Failed to verify TLSN proof: {str(e)}")
raise HTTPException(status_code=400, detail="Failed when verifying TLSN proof")
logger.info("TLSN proof is valid")

# 2. Backup previous shares
backup_shares_path = backup_shares(settings.party_id)
Expand Down Expand Up @@ -290,6 +293,8 @@ def generate_data_sharing_program(
if is_first_run:
program_content = '\n'.join([line for line in program_content.split('\n') if "# NOTE: Skipped if it's the first run" not in line])

logger.info(f"Generated data sharing program from the template with parameters: {secret_index=}, {client_port_base=}, {max_data_providers=}, {is_first_run=}, {input_bytes=}, {tlsn_delta=}, {tlsn_zero_encodings=}")
logger.info(f"Generated program: {program_content}")
with open(target_program_path, "w") as program_file:
program_file.write(program_content)
return circuit_name, target_program_path
Expand Down
7 changes: 4 additions & 3 deletions mpc_demo_infra/coordination_server/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ async def finish_computation(request: RequestFinishComputationRequest, x: Reques
return RequestFinishComputationResponse(is_finished=is_finished)

def locate_binance_verifier():
# binance_verifier is expected to be in the current working dir or missing.
# if missing, tlsn directory is expected to exist so that binance_verifier
# can be built from the source.
# If pre-built binance_verifier exists, it's expected to be in
# the current working dir or TLSN_VERIFIER_PATH.
# Othereise, binance_verifier will be built from the source
binance_verifiers = [
(Path('.').resolve(), CMD_TLSN_VERIFIER),
(TLSN_VERIFIER_PATH, CMD_TLSN_VERIFIER),
Expand Down Expand Up @@ -407,4 +407,5 @@ def get_computation_query_mpc_ports() -> tuple[int, int]:
next_query_port_base = settings.free_ports_start + 2 * settings.num_parties
else:
next_query_port_base = client_port_base + settings.num_parties
logger.info(f"Computation party ports: {server_port_base=}, {client_port_base=}")
return server_port_base, client_port_base
3 changes: 2 additions & 1 deletion mpc_demo_infra/program/share_data.mpc
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ def main():
commitment_values.read_from_file(1 + MAX_DATA_PROVIDERS) # NOTE: Skipped if it's the first run

# Start listening for client socket connections
print_ln('Calling listen_for_clients(%s)...', PORTNUM)
listen_for_clients(PORTNUM)
print_ln('Listening for client connections on base port %s', PORTNUM)

client_socket_id = accept_client()
print_ln('client_socket_id: %s', client_socket_id)
print_ln('Accepted client connection. client_socket_id: %s', client_socket_id)

input_value, input_nonce = client_input(sint, client_socket_id)
client_values[SECRET_INDEX] = input_value
Expand Down

0 comments on commit b22a762

Please sign in to comment.