Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
powersoftau_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 os.path import exists, join, dirname
8 from typing import Optional, List, Mapping
9 
10 # These paths are hard-coded into the powersoftau code commands
11 CHALLENGE_FILE = "challenge"
12 NEW_CHALLENGE_FILE = "new_challenge"
13 RESPONSE_FILE = "response"
14 
15 CONFIG = "release"
16 
17 
19  """
20  Wrapper around the powersoftau commands
21  """
22  def __init__(
23  self,
24  powersoftau_path: Optional[str],
25  num_powers: Optional[int]):
26  self.powersoftau_path = powersoftau_path or _default_powersoftau_path()
27  self.num_powers = num_powers
28  if not exists(self.powersoftau_path):
29  raise Exception(f"expected powersoftau path: {self.powersoftau_path}")
30 
31  def begin(self) -> bool:
32  return self._exec("new")
33 
34  def verify_contribution(self) -> bool:
35  return self._exec("verify_transform")
36 
37  @staticmethod
39  response: str,
40  transcript_file: str) -> None:
41  import subprocess
42  with open(transcript_file, "ab") as transcript_f:
43  subprocess.run(
44  ["dd", f"if={response}", "bs=64", "skip=1"],
45  stdout=transcript_f,
46  check=True)
47 
49  self,
50  digest_file: Optional[str],
51  skip_user_input: bool) -> bool:
52  assert exists(CHALLENGE_FILE)
53  cmd_args: List[str] = []
54  kwargs = {}
55  if digest_file:
56  cmd_args += ["--digest", digest_file]
57  if skip_user_input:
58  kwargs["input"] = "any data\n".encode()
59  if self._exec("compute", cmd_args, kwargs):
60  assert exists(RESPONSE_FILE)
61  return True
62  return False
63 
64  def verify_transcript(self, num_rounds: int) -> bool:
65  return self._exec("verify", args=["--rounds", str(num_rounds)])
66 
67  def _exec(
68  self,
69  cmd: str,
70  args: List[str] = None,
71  kwargs: Mapping[str, object] = None) -> bool:
72  import subprocess
73  args = args or []
74  args = [join(self.powersoftau_path, cmd)] + args
75  kwargs = kwargs or {}
76  if self.num_powers:
77  args = args + ["-n", str(self.num_powers)]
78  print(f"CMD: {' '.join(args)}")
79  return subprocess.run(args=args, check=False, **kwargs).returncode == 0
80 
81 
82 def _default_powersoftau_path() -> str:
83  """
84  Return the default path to the PoT binaries directory
85  """
86  return join(
87  dirname(__file__), "..", "..", "..", "powersoftau", "target", CONFIG)
test_commands.mock.str
str
Definition: mock.py:18
coordinator.powersoftau_command.PowersOfTauCommand.powersoftau_path
powersoftau_path
Definition: powersoftau_command.py:23
coordinator.powersoftau_command.PowersOfTauCommand.contribute
bool contribute(self, Optional[str] digest_file, bool skip_user_input)
Definition: powersoftau_command.py:48
coordinator.powersoftau_command.PowersOfTauCommand.__init__
def __init__(self, Optional[str] powersoftau_path, Optional[int] num_powers)
Definition: powersoftau_command.py:22
coordinator.powersoftau_command.PowersOfTauCommand._exec
bool _exec(self, str cmd, List[str] args=None, Mapping[str, object] kwargs=None)
Definition: powersoftau_command.py:67
coordinator.powersoftau_command.PowersOfTauCommand.append_response_to_transcript
None append_response_to_transcript(str response, str transcript_file)
Definition: powersoftau_command.py:38
coordinator.powersoftau_command.PowersOfTauCommand.verify_transcript
bool verify_transcript(self, int num_rounds)
Definition: powersoftau_command.py:64
coordinator.powersoftau_command.PowersOfTauCommand
Definition: powersoftau_command.py:18
coordinator.powersoftau_command.PowersOfTauCommand.begin
bool begin(self)
Definition: powersoftau_command.py:31
coordinator.powersoftau_command.PowersOfTauCommand.num_powers
num_powers
Definition: powersoftau_command.py:24
coordinator.powersoftau_command.PowersOfTauCommand.verify_contribution
bool verify_contribution(self)
Definition: powersoftau_command.py:34