8 Implementation of Phase2ContributionHandler
11 from __future__
import annotations
12 from .server_configuration
import Configuration, JsonDict
13 from .icontributionhandler
import IContributionHandler
14 from .mpc_command
import MPCCommand
15 from .phase1_contribution_handler
import \
16 NEW_CHALLENGE_FILE, TRANSCRIPT_FILE, FINAL_OUTPUT, FINAL_TRANSCRIPT
18 from os.path
import exists
20 from typing
import Optional, cast
23 CHALLENGE_0_FILE =
"challenge_0.bin"
24 NEXT_CHALLENGE_FILE =
"next_challenge.bin"
29 Configuration object for phase2 server.
34 server_configuration: Configuration,
35 mpc_tool: Optional[str]):
44 phase2_config_json: str,
45 config_path: Optional[str] =
None) -> Phase2ServerConfig:
46 return Phase2ServerConfig._from_json_dict(
47 json.loads(phase2_config_json), config_path)
49 def _to_json_dict(self) -> JsonDict:
58 config_path: Optional[str]) -> Phase2ServerConfig:
60 server_configuration=Configuration._from_json_dict(
61 cast(JsonDict, json_dict[
"server"]), config_path),
62 mpc_tool=cast(Optional[str], json_dict.get(
"mpc_tool",
None)))
67 Handler processing phase2 challenges and contributions.
70 def __init__(self, phase2_config: Phase2ServerConfig):
72 if not exists(CHALLENGE_0_FILE):
73 raise Exception(f
"no {CHALLENGE_0_FILE} found in server dir")
76 if not exists(NEXT_CHALLENGE_FILE):
77 if exists(TRANSCRIPT_FILE):
78 raise Exception(f
"unexpected {TRANSCRIPT_FILE} in server dir")
87 have_next_challenge = exists(NEXT_CHALLENGE_FILE)
88 if have_next_challenge:
89 if contributor_idx == 0:
91 f
"unexpected {NEXT_CHALLENGE_FILE} for 0-th contributor")
92 return NEXT_CHALLENGE_FILE
93 return CHALLENGE_0_FILE
96 self, contribution_idx: int, file_name: str) -> bool:
98 contribution_valid = self.
mpc.phase2_verify_contribution(
99 orig_challenge=orig_challenge,
101 out_new_challenge=NEW_CHALLENGE_FILE,
102 transcript=TRANSCRIPT_FILE)
104 if contribution_valid:
105 if not exists(NEW_CHALLENGE_FILE):
106 raise Exception(
"unknown error creating new challenge")
110 rename(NEW_CHALLENGE_FILE, NEXT_CHALLENGE_FILE)
118 if not exists(NEXT_CHALLENGE_FILE):
119 raise Exception(
"no contributions made")
122 mpc_valid = self.
mpc.phase2_verify_transcript(
127 raise Exception(
"error in MPC transcript")
130 rename(NEXT_CHALLENGE_FILE, FINAL_OUTPUT)
131 rename(TRANSCRIPT_FILE, FINAL_TRANSCRIPT)
134 print(
"Phase 2 coordinator correctly executed.")
135 print(
"(CTRL-C to stop the server)")