Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
mpc_command.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright (c) 2015-2022 Clearmatics Technologies Ltd
4 #
5 # SPDX-License-Identifier: LGPL-3.0+
6 
7 from typing import Optional, List
8 from os.path import exists
9 import os.path
10 import subprocess
11 
12 
13 class MPCCommand:
14  """
15  Wrapper around the 'mpc' utility.
16  """
17 
18  def __init__(self, mpc_tool: Optional[str] = "", dry_run: bool = False):
19  self.mpc_tool = mpc_tool or _default_mpc_tool()
20  self.dry_run = dry_run
21  assert exists(self.mpc_tool)
22 
24  self,
25  powersoftau_file: str,
26  lagrange_file: str,
27  linear_comb_out_file: str,
28  pot_degree: Optional[int] = None) -> bool:
29  args = ["linear-combination"]
30  args += ["--pot-degree", str(pot_degree)] if pot_degree else []
31  args += [powersoftau_file, lagrange_file, linear_comb_out_file]
32  return self._exec(args)
33 
34  def phase2_begin(self, linear_comb_file: str, challenge_0_file: str) -> bool:
35  return self._exec(["phase2-begin", linear_comb_file, challenge_0_file])
36 
38  self,
39  orig_challenge: str,
40  response: str,
41  out_new_challenge: Optional[str] = None,
42  transcript: Optional[str] = None) -> bool:
43  args = ["phase2-verify-contribution"]
44  args += ["--new-challenge", out_new_challenge] \
45  if out_new_challenge else []
46  args += ["--transcript", transcript] if transcript else []
47  args += [orig_challenge, response]
48  return self._exec(args)
49 
51  self,
52  orig_challenge: str,
53  final_challenge: str,
54  transcript: str,
55  digest_file: Optional[str] = None) -> bool:
56  args = ["phase2-verify-transcript"]
57  args += ["--digest", digest_file] if digest_file else []
58  args += [orig_challenge, transcript, final_challenge]
59  return self._exec(args)
60 
62  self,
63  challenge_file: str,
64  output_file: str,
65  digest_file: Optional[str] = None,
66  skip_user_input: bool = False) -> bool:
67  args = ["phase2-contribute", challenge_file, output_file]
68  args += ["--digest", digest_file] if digest_file else []
69  args += ["--skip-user-input"] if skip_user_input else []
70  return self._exec(args)
71 
73  self,
74  powersoftau_file: str,
75  linear_comb_file: str,
76  final_challenge: str,
77  keypair_out_file: str,
78  pot_degree: Optional[int] = None) -> bool:
79  args = ["create-keypair"]
80  args += ["--pot-degree", str(pot_degree)] if pot_degree else []
81  args += [
82  powersoftau_file,
83  linear_comb_file,
84  final_challenge,
85  keypair_out_file]
86  return self._exec(args)
87 
88  def _exec(self, args: List[str]) -> bool:
89  cmd = [self.mpc_tool] + args
90  print(f"CMD: {' '.join(cmd)}")
91  return self.dry_run or \
92  subprocess.run(cmd, check=False).returncode == 0
93 
94 
95 def _default_mpc_tool() -> str:
96  return os.path.join(
97  os.path.dirname(__file__), "..", "..",
98  "build", "mpc_tools", "mpc_phase2", "mpc-coord-phase2")
test_commands.mock.str
str
Definition: mock.py:18
coordinator.mpc_command.MPCCommand.dry_run
dry_run
Definition: mpc_command.py:20
coordinator.mpc_command.MPCCommand.phase2_begin
bool phase2_begin(self, str linear_comb_file, str challenge_0_file)
Definition: mpc_command.py:34
coordinator.mpc_command.MPCCommand.phase2_contribute
bool phase2_contribute(self, str challenge_file, str output_file, Optional[str] digest_file=None, bool skip_user_input=False)
Definition: mpc_command.py:61
coordinator.mpc_command.MPCCommand
Definition: mpc_command.py:13
coordinator.mpc_command.MPCCommand.phase2_verify_transcript
bool phase2_verify_transcript(self, str orig_challenge, str final_challenge, str transcript, Optional[str] digest_file=None)
Definition: mpc_command.py:50
coordinator.mpc_command.MPCCommand.__init__
def __init__(self, Optional[str] mpc_tool="", bool dry_run=False)
Definition: mpc_command.py:18
coordinator.mpc_command.MPCCommand.create_keypair
bool create_keypair(self, str powersoftau_file, str linear_comb_file, str final_challenge, str keypair_out_file, Optional[int] pot_degree=None)
Definition: mpc_command.py:72
coordinator.mpc_command.MPCCommand.phase2_verify_contribution
bool phase2_verify_contribution(self, str orig_challenge, str response, Optional[str] out_new_challenge=None, Optional[str] transcript=None)
Definition: mpc_command.py:37
coordinator.mpc_command.MPCCommand._exec
bool _exec(self, List[str] args)
Definition: mpc_command.py:88
coordinator.mpc_command.MPCCommand.linear_combination
bool linear_combination(self, str powersoftau_file, str lagrange_file, str linear_comb_out_file, Optional[int] pot_degree=None)
Definition: mpc_command.py:23
coordinator.mpc_command.MPCCommand.mpc_tool
mpc_tool
Definition: mpc_command.py:19