zkrender.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python3
  2. # This file is part of DarkFi (https://dark.fi)
  3. #
  4. # Copyright (C) 2020-2024 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. def main(witness_file, source_file, output, width, height, font_size):
  25. print("Compiling zkas code...")
  26. with open(source_file, "r", encoding="utf-8") as zkas_file:
  27. zkas_source = zkas_file.read()
  28. zkbin = ZkBinary(source_file, zkas_source)
  29. circuit = ZkCircuit(zkbin)
  30. print("Decoding witnesses...")
  31. load_circuit_witness(circuit, witness_file)
  32. circuit = circuit.prover_build()
  33. if not circuit.render(zkbin.k(), output, width, height, font_size):
  34. eprint("Rendering failed")
  35. print(f"Written output to '{output}'")
  36. if __name__ == "__main__":
  37. from argparse import ArgumentParser
  38. parser = ArgumentParser(
  39. prog="zkrender",
  40. description="Python util for rendering zk circuits",
  41. epilog="This tool is only for prototyping purposes",
  42. )
  43. parser.add_argument(
  44. "SOURCE",
  45. help="Path to zkas source code",
  46. )
  47. parser.add_argument(
  48. "-w",
  49. "--witness",
  50. required=True,
  51. help="Path to JSON file holding witnesses",
  52. )
  53. parser.add_argument(
  54. "OUTPUT",
  55. help="Path to output image",
  56. )
  57. parser.add_argument(
  58. "-W", "--width", type=int,
  59. default=800,
  60. help="Image width",
  61. )
  62. parser.add_argument(
  63. "-H", "--height", type=int,
  64. default=600,
  65. help="Image width",
  66. )
  67. parser.add_argument(
  68. "-f", "--font-size", type=int,
  69. default=20,
  70. help="Image width",
  71. )
  72. args = parser.parse_args()
  73. sys.exit(main(args.witness, args.SOURCE, args.OUTPUT,
  74. args.width, args.height, args.font_size))