/ bin / zkrunner / zkrender.py
zkrender.py
 1  #!/usr/bin/env python3
 2  # This file is part of DarkFi (https://dark.fi)
 3  #
 4  # Copyright (C) 2020-2025 Dyne.org foundation
 5  #
 6  # This program is free software: you can redistribute it and/or modify
 7  # it under the terms of the GNU Affero General Public License as
 8  # published by the Free Software Foundation, either version 3 of the
 9  # License, or (at your option) any later version.
10  #
11  # This program is distributed in the hope that it will be useful,
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  # GNU Affero General Public License for more details.
15  #
16  # You should have received a copy of the GNU Affero General Public License
17  # along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  """
19  Python tool to render zkVM circuit layouts given zkas source code.
20  """
21  import sys
22  from darkfi_sdk.zkas import ZkBinary, ZkCircuit
23  from zkrunner import eprint, load_circuit_witness
24  
25  def main(witness_file, source_file, output, width, height, font_size):
26      print("Compiling zkas code...")
27      with open(source_file, "r", encoding="utf-8") as zkas_file:
28          zkas_source = zkas_file.read()
29  
30      zkbin = ZkBinary(source_file, zkas_source)
31      circuit = ZkCircuit(zkbin)
32      print("Decoding witnesses...")
33      load_circuit_witness(circuit, witness_file)
34      circuit = circuit.prover_build()
35  
36      if not circuit.render(zkbin.k(), output, width, height, font_size):
37          eprint("Rendering failed")
38      print(f"Written output to '{output}'")
39  
40  if __name__ == "__main__":
41      from argparse import ArgumentParser
42  
43      parser = ArgumentParser(
44          prog="zkrender",
45          description="Python util for rendering zk circuits",
46          epilog="This tool is only for prototyping purposes",
47      )
48      parser.add_argument(
49          "SOURCE",
50          help="Path to zkas source code",
51      )
52      parser.add_argument(
53          "-w",
54          "--witness",
55          required=True,
56          help="Path to JSON file holding witnesses",
57      )
58      parser.add_argument(
59          "OUTPUT",
60          help="Path to output image",
61      )
62  
63      parser.add_argument(
64          "-W", "--width", type=int,
65          default=800,
66          help="Image width",
67      )
68      parser.add_argument(
69          "-H", "--height", type=int,
70          default=600,
71          help="Image width",
72      )
73      parser.add_argument(
74          "-f", "--font-size", type=int,
75          default=20,
76          help="Image width",
77      )
78  
79      args = parser.parse_args()
80      sys.exit(main(args.witness, args.SOURCE, args.OUTPUT,
81                    args.width, args.height, args.font_size))
82