Circuit

class circuit.Circuit(circ_file, output_dir='./', working_dir='./', r1cs=None, sym_file=None, js_dir=None, wasm=None, witness=None, zkey=None, vkey=None)

Manages compiling, proving, and verifying Circom circuits.

Parameters:
  • circ_file (str) – Path to a circom circuit file.

  • output_dir (str, optional) – Path to where generated files should be outputted. Defaults to the current directory.

  • working_dir (str, optional) – Path that all given file paths are relative to. Defaults to the current directory

  • r1cs (str, optional) – Optional path to a pre-generated r1cs file.

  • sym_file (str, optional) – Optional path to a pre-generated symbols file.

  • js_dir (str, optional) – Optional path to a pre-generated directory with JS files.

  • wasm (str, optional) – Optional path to a pre-generated wasm file.

  • witness (str, optional) – Optional path to a witness file.

  • zkey (str, optional) – Optional path to a pre-generated zkey file.

  • vkey (str, optional) – Optional path to a pre-generated verification key file.

circ_file

Path to a circom circuit file. This circuit can be compiled, proved, etc. with this class.

Type:

str

output_dir

Path to where generated files will be outputted.

Type:

str

working_dir

Specifies the path that all given file paths are relative to.

Type:

str

r1cs_file

Path to an r1cs file. This can be generated by the class itself when a circuit is compiled, or the path can be given if already generated

Type:

str

sym_file

Path to a symbols file. This can be generated by the class itself when a circuit is compiled, or the path can be given if already generated

Type:

str

js_dir

Path to the JS directory generated by SnarkJS. This can be generated by the class itself when a circuit is compiled, or the path can be given if already generated

Type:

str

wasm_file

Path to a wasm file. This can be generated by the class itself when a circuit is compiled, or the path can be given if already generated

Type:

str

zkey

Path to a zkey file. This can be generated by the class itself when a circuit is compiled, or the path can be given if already generated

Type:

str

vkey

Path to a verification key file. This can be generated by the class itself when a circuit is compiled, or the path can be given if already generated

Type:

str

check_circ_compiled()

Checks that the circuit was compiled and that the relevant files exist

compile()

Compiles the circuit and generates an r1cs file, a symbols file, a wasm file, and a js dir

contribute_phase2(entropy='', output_file=None)

Contributes to phase 2. Required for the Groth16 proving scheme. The circuit must have been previously setup.

Parameters:
  • entropy (str) – Random entropy to contribute to the zkey.

  • output_file (str, optional) – Path of where the zkey file should be outputted. Defaults to a randomly generated file name.

export_r1cs_to_json()

Exports the r1cs file as a JSON file.

export_sol(output_file)

Convenience function that handles proving a circuit in its entirety. Performs PTau ceremony, compiles circuit, and proves it.

Parameters:
  • scheme (str) – The proving scheme to use. Can either be GROTH, PLONK, or FFLONK.

  • input_file (str) – Path to an input json file that specifies the inputs to the circuit.

export_vkey(zkey_file=None, output_file=None)

Exports a verification key.

Parameters:
  • zkey_file (str, optional) – Path to a zkey file from which the verification key should be created. Defaults to a previously generated zkey.

  • output_file (str, optional) – Path to where the verification key file should be outputted. Defaults to a randomly generated file name.

fullprove(scheme, input_file)

Convenience function that handles proving a circuit in its entirety. Performs PTau ceremony, compiles circuit, and proves it.

Parameters:
  • scheme (str) – The proving scheme to use. Can either be GROTH, PLONK, or FFLONK.

  • input_file (str) – Path to an input json file that specifies the inputs to the circuit.

gen_witness(input_file, output_file=None)

Generates a witness file. Requires that the circuit was compiled first.

Parameters:
  • input_file (str) – Path to an input json file that specifies the inputs to the circuit.

  • output_file (str) – Path of where the witness file should be outputted. Defaults to a randomly generated file name.

get_info()

Prints info about the circuit. Requires that the circuit was compiled first.

print_constraints()

Prints info about the constraints of the circuit. Requires that the circuit was compiled first.

prove(scheme, proof_out=None, public_out=None)

Generates a proof for the circuit.

Parameters:
  • scheme (str) – The proving scheme to use. Can either be GROTH, PLONK, or FFLONK.

  • proof_out (str, optional) – Path of where the proof file should be outputted. Defaults to ‘proof.json’.

  • public_out (str, optional) – Path of where the public file should be outputted. Defaults to ‘public.json’.

setup(scheme, ptau, output_file=None)

Prepares to generate proof and creates a zkey file.

Parameters:
  • scheme (str) – The proving scheme to use. Can either be GROTH, PLONK, or FFLONK.

  • ptau (PTau) – A PTau object with a completed powers of tau ceremony.

  • output_file (str) – Path of where the zkey file should be outputted. Defaults to a randomly generated file name.

verify(scheme, vkey_file=None, public_file=None, proof_file=None)

Verifies that a proof is valid.

Parameters:
  • vkey_file (str, optional) – Path to a verification key file. Defaults to a previously generated vkey.

  • public_file (str, optional) – Path to a public file. Defaults to a previously generated public_file.

  • proof_file (str, optional) – Path to the proof. Defaults to a previously generated proof.

Returns:

Whether the proof is valid or not.

Return type:

bool

verify_zkey(ptau, zkey_file=None)

Verifies a zkey is valid.

Parameters:
  • ptau (PTau) – A powers of tau ceremony.

  • zkey_file (str, optional) – Path to the zkey file to verify. Defaults to a previously generated zkey.