### Run Circuit Setup with Circomkit CLI Source: https://context7.com/erhant/circomkit/llms.txt Perform the circuit setup process, which generates keys, using the `setup` command. You can specify a custom PTAU file if needed. ```bash # Run circuit-specific setup npx circomkit setup multiplier_3 npx circomkit setup multiplier_3 ./ptau/custom.ptau ``` -------------------------------- ### Run Circomkit Example with Bun Source: https://github.com/erhant/circomkit/blob/main/examples/bun-sha256/README.md Execute the main example script using Bun. ```sh bun start ``` -------------------------------- ### Install Circomkit with Bun Source: https://github.com/erhant/circomkit/blob/main/examples/bun-sha256/README.md Run this command to install project dependencies using Bun. ```sh bun install ``` -------------------------------- ### Circomkit CLI: Circuit Setup Source: https://github.com/erhant/circomkit/blob/main/README.md Perform circuit-specific setup, optionally providing a PTAU file path. If no path is given, Circomkit automatically selects and downloads the appropriate PTAU for `bn128` prime fields up to $2^{28}$ constraints. ```sh npx circomkit setup [ptau-path] ``` -------------------------------- ### Install Circomkit Source: https://github.com/erhant/circomkit/blob/main/README.md Install Circomkit using your preferred package manager. Ensure Circom is also installed separately. ```sh npm install circomkit pnpm install circomkit yarn add circomkit bun add circomkit ``` -------------------------------- ### Circomkit File Structure Example Source: https://github.com/erhant/circomkit/blob/main/README.md Illustrates the default opinionated file structure of Circomkit, showing the organization of circuit files, inputs, ptau files, and build artifacts. ```ml circomkit ├── circuits.json - "circuit configurations" ├── circomkit.json - "circomkit configurations" │ ├── circuits - "circuit codes are here" │ ├── main - "main components will be here" │ │ └── sudoku_9x9.circom - "auto-generated circuit instance" │ └── sudoku.circom - "circuit template" │ ├── inputs - "circuit inputs are here" │ └── sudoku_9x9 - "folder name is the circuit instance name" │ └── my_solution.json - "file name is the input name" │ ├── ptau - "PTAU files are here" │ └── powersOfTau28_hez_final_08.ptau │ └── build - "build artifacts are stored here" └── sudoku_9x9 - "folder name is the circuit instance name" ├── sudoku_9x9_js - "WASM outputs" │ │── generate_witness.js │ │── witness_calculator.js │ └── sudoku_9x9.wasm │ ├── my_solution - "folder name is the input name" │ │── groth16_proof.json - "proofs are created per protocol" │ │── public.json │ └── witness.wtns │ ├── sudoku_9x9.r1cs ├── sudoku_9x9.sym - "symbol file, used by tests" │ ├── groth16_pkey.zkey - "proving key per protocol" ├── groth16_vkey.json - "verification key per protocol" └── groth16_verifier.sol - "verifier contract" ``` -------------------------------- ### Initialize and Use Circomkit Class Source: https://context7.com/erhant/circomkit/llms.txt Demonstrates initializing the `Circomkit` class with custom configurations and performing core circuit operations including compilation, setup, witness generation, proof generation, and verification. Ensure the `circuits.json` file is correctly configured. ```typescript import { Circomkit } from 'circomkit'; // Initialize with custom configuration const circomkit = new Circomkit({ protocol: 'groth16', // 'groth16' | 'plonk' | 'fflonk' prime: 'bn128', // 'bn128' | 'bls12381' | 'goldilocks' verbose: true, logLevel: 'INFO', circuits: './circuits.json', dirCircuits: './circuits', dirInputs: './inputs', dirBuild: './build', dirPtau: './ptau', optimization: 1, // 0: none, 1: simple, 2: full Gaussian elimination }); // Define circuit configuration const circuitConfig = { file: 'multiplier', template: 'Multiplier', params: [3], pubs: [], }; // Compile a circuit const buildPath = await circomkit.compile('multiplier_3', circuitConfig); // Output: ./build/multiplier_3/ // Get circuit information const info = await circomkit.info('multiplier_3'); console.log(`Constraints: ${info.constraints}`); console.log(`Wires: ${info.wires}`); console.log(`Private Inputs: ${info.privateInputs}`); console.log(`Public Inputs: ${info.publicInputs}`); // Run circuit-specific setup (downloads PTAU automatically for bn128) const { proverKeyPath, verifierKeyPath } = await circomkit.setup('multiplier_3'); // Output: build/multiplier_3/groth16_pkey.zkey, build/multiplier_3/groth16_vkey.json // Generate a witness const witnessPath = await circomkit.witness('multiplier_3', 'default', { in: [3, 5, 7] }); // Output: build/multiplier_3/default/witness.wtns // Generate a proof const proofPath = await circomkit.prove('multiplier_3', 'default', { in: [3, 5, 7] }); // Output: build/multiplier_3/default/groth16_proof.json // Verify the proof const isValid = await circomkit.verify('multiplier_3', 'default'); console.log(`Proof valid: ${isValid}`); // true ``` -------------------------------- ### Example Input File Source: https://context7.com/erhant/circomkit/llms.txt Specifies the input values for a particular circuit instance. Ensure the input keys match the circuit's expected signal names. ```json // inputs/multiplier_3/default.json - Input file { "in": [2, 3, 4] } ``` -------------------------------- ### Circuit Configuration Example Source: https://github.com/erhant/circomkit/blob/main/README.md Example of a circuit configuration object within `circuits.json`. Defines the circuit's filename, template name, public signals, and template parameters. ```js sudoku_9x9: { file: 'sudoku', template: 'Sudoku', pubs: ['puzzle'], params: [3], // sqrt(9) } ``` -------------------------------- ### Proof Tester Example with Plonk Source: https://github.com/erhant/circomkit/blob/main/README.md Demonstrates how to use the Proof Tester for the Plonk protocol. Ensure necessary files like WASM, prover key, and verifier key are available or generated. ```typescript describe('proof tester', () => { // input signals and output signals can be given as type parameters // this makes all functions type-safe! let circuit: ProofTester<['in']>; const protocol = 'plonk'; beforeAll(async () => { const circomkit = new Circomkit({protocol}); circomkit.instantiate(CIRCUIT_NAME, CIRCUIT_CONFIG); await circomkit.setup(CIRCUIT_NAME, PTAU_PATH); circuit = await circomkit.ProofTester(CIRCUIT_NAME, protocol); }); it('should verify a proof correctly', async () => { const {proof, publicSignals} = await circuit.prove(INPUT); await circuit.expectPass(proof, publicSignals); }); it('should NOT verify a proof with invalid public signals', async () => { const {proof} = await circuit.prove(INPUT); await circuit.expectFail(proof, BAD_PUBLIC_SIGNALS); }); }); ``` -------------------------------- ### Format Code Source: https://github.com/erhant/circomkit/blob/main/README.md Command to check and apply code formatting according to the Google TypeScript Style Guide. ```sh pnpm format ``` -------------------------------- ### Compute Circuit Outputs with Witness Tester Source: https://github.com/erhant/circomkit/blob/main/README.md Use the `compute` function to get specific output signals from a circuit's witness. This is useful for detailed inspection of computed values. ```typescript it('should compute correctly', async () => { const output = await circuit.compute(INPUT, ['out']); expect(output).to.haveOwnProperty('out'); expect(output.out).to.eq(BigInt(OUTPUT.out)); }); ``` -------------------------------- ### Initialize Circomkit Project Source: https://context7.com/erhant/circomkit/llms.txt Initialize a new Circomkit project from a template using the `init` command. This sets up the basic project structure. ```bash # Initialize a new project from template npx circomkit init my-project ``` -------------------------------- ### List Circomkit Circuit Templates and Instances Source: https://context7.com/erhant/circomkit/llms.txt List all available circuit templates and their instantiated versions using the `list` command. ```bash # List all circuit templates and instances npx circomkit list ``` -------------------------------- ### Circomkit CLI: Instantiate Circuit Source: https://github.com/erhant/circomkit/blob/main/README.md Create the main component of a circuit. Replace `` with the name of your circuit. ```sh npx circomkit instantiate ``` -------------------------------- ### Circomkit CLI Help Source: https://github.com/erhant/circomkit/blob/main/README.md View available commands and their usage by running the help command. ```sh npx circomkit help ``` -------------------------------- ### Initialize ProofTester for Proof Generation Testing Source: https://context7.com/erhant/circomkit/llms.txt Set up ProofTester for testing the complete proof generation and verification cycle. Requires pre-existing WASM, prover key, and verifier key files. The protocol must be specified during Circomkit initialization. ```typescript import { Circomkit, ProofTester } from 'circomkit'; const circomkit = new Circomkit({ protocol: 'groth16', verbose: false, }); // Setup circuit first (creates necessary keys) circomkit.instantiate('multiplier_3', { file: 'multiplier', template: 'Multiplier', params: [3], }); await circomkit.setup('multiplier_3', './ptau/powersOfTau28_hez_final_08.ptau'); // Create proof tester with type-safe input signals let circuit: ProofTester<['in'], 'groth16'>; circuit = await circomkit.ProofTester('multiplier_3', 'groth16'); ``` -------------------------------- ### Instantiate Circuit Component with Circomkit CLI Source: https://context7.com/erhant/circomkit/llms.txt Create the main component for a circuit based on the `circuits.json` configuration using the `instantiate` command. ```bash # Create main component from circuits.json config npx circomkit instantiate multiplier_3 ``` -------------------------------- ### Circomkit CLI: List Circuits Source: https://github.com/erhant/circomkit/blob/main/README.md List all available circuit templates and their compiled instances managed by Circomkit. ```sh npx circomkit list ``` -------------------------------- ### Circomkit CLI Workflow with Bun Source: https://github.com/erhant/circomkit/blob/main/examples/bun-sha256/README.md A sequence of commands to compile a circuit, generate a proof, verify it, create a contract, and generate calldata using Circomkit with Bun. ```sh bunx circomkit compile sha256_32 ``` ```sh bunx circomkit prove sha256_32 default ``` ```sh bunx circomkit verify sha256_32 default ``` ```sh bunx circomkit contract sha256_32 ``` ```sh bunx circomkit calldata sha256_32 default ``` -------------------------------- ### Run Circomkit Tests with Bun Source: https://github.com/erhant/circomkit/blob/main/examples/bun-sha256/README.md Execute the test suite for the project using Bun. ```sh bun test ``` -------------------------------- ### Circomkit CLI: Create Verification Key Source: https://github.com/erhant/circomkit/blob/main/README.md Generate a verification key for a circuit. Optionally provide a path to the proving key. Replace `` with the circuit name. ```sh npx circomkit vkey [pkey-path] ``` -------------------------------- ### Circomkit CLI: Verify Proof Source: https://github.com/erhant/circomkit/blob/main/README.md Verify a proof for a circuit using public signals and input values. Input files are located at `inputs//.json`. Replace `` and ``. ```sh npx circomkit verify ``` -------------------------------- ### Circomkit CLI: Print Configuration Source: https://github.com/erhant/circomkit/blob/main/README.md Display the currently active Circomkit configuration, including protocol and prime field settings. ```sh npx circomkit config ``` -------------------------------- ### Generate Proof with Circomkit CLI Source: https://context7.com/erhant/circomkit/llms.txt Generate a zero-knowledge proof for a circuit using the `prove` command with a specified witness. ```bash # Generate a proof npx circomkit prove multiplier_3 default ``` -------------------------------- ### Compile, Prove, and Verify Circuits with Circomkit Source: https://github.com/erhant/circomkit/blob/main/README.md Use Circomkit programmatically to compile circuits, generate proofs, and verify them. Configuration can be provided directly, bypassing file-based configuration. ```typescript import {Circomkit} from 'circomkit'; const circomkit = new Circomkit({ // custom configurations protocol: 'plonk', }); // artifacts output at `build/multiplier_3` directory await circomkit.compile('multiplier_3', { file: 'multiplier', template: 'Multiplier', params: [3], }); // proof & public signals at `build/multiplier_3/my_input` directory await circomkit.prove('multiplier_3', 'my_input', {in: [3, 5, 7]}); // verify with proof & public signals at `build/multiplier_3/my_input` await circomkit.verify('multiplier_3', 'my_input'); ``` -------------------------------- ### Verify Proof with Circomkit CLI Source: https://context7.com/erhant/circomkit/llms.txt Verify a previously generated proof using the `verify` command. This confirms the validity of the proof against the circuit and public signals. ```bash # Verify a proof npx circomkit verify multiplier_3 default ``` -------------------------------- ### Circomkit CLI: Generate Proof Source: https://github.com/erhant/circomkit/blob/main/README.md Generate a cryptographic proof for a circuit with given inputs. Input files are expected in `inputs//.json`. Replace `` and ``. ```sh npx circomkit prove ``` -------------------------------- ### Circomkit CLI: Generate Witness Source: https://github.com/erhant/circomkit/blob/main/README.md Generate a witness for a circuit using specified input values. Input files should be located at `inputs//.json`. Replace `` and `` accordingly. ```sh npx circomkit witness ``` -------------------------------- ### Display Circuit Information with Circomkit CLI Source: https://context7.com/erhant/circomkit/llms.txt Retrieve and display detailed information about a circuit, such as prime field, number of wires, constraints, and inputs/outputs, using the `info` command. ```bash # Print circuit information npx circomkit info multiplier_3 # Output: # Prime Field: bn128 # Number of Wires: 12 # Number of Constraints: 8 # Number of Private Inputs: 3 # Number of Public Inputs: 0 # Number of Public Outputs: 1 ``` -------------------------------- ### Display Circomkit Configuration Source: https://context7.com/erhant/circomkit/llms.txt View the current Circomkit configuration settings using the `config` command. ```bash # Print current configuration npx circomkit config ``` -------------------------------- ### Circomkit CLI: Compile Circuit Source: https://github.com/erhant/circomkit/blob/main/README.md Compile a Circom circuit. Replace `` with the name of your circuit file. ```sh npx circomkit compile ``` -------------------------------- ### Run All Tests Source: https://github.com/erhant/circomkit/blob/main/README.md Command to execute all tests within the project using pnpm. ```sh pnpm test ``` -------------------------------- ### Circomkit CLI: Download PTAU Source: https://github.com/erhant/circomkit/blob/main/README.md Automatically download the Phase 1 Powers of Tau (PTAU) file for BN128 prime fields. Replace `` with the circuit name. ```sh npx circomkit ptau ``` -------------------------------- ### Generate and Verify Proof with ProofTester Source: https://context7.com/erhant/circomkit/llms.txt Generate a proof for given inputs using `prove` and then verify its validity using `expectPass`. This confirms the entire proof system is functioning correctly. ```typescript // Generate a proof const { proof, publicSignals } = await circuit.prove({ in: [2, 3, 4] }); // Verify the proof passes await circuit.expectPass(proof, publicSignals); ``` -------------------------------- ### Circomkit CLI: Generate Verifier Contract Source: https://github.com/erhant/circomkit/blob/main/README.md Generate a Solidity verifier contract for a circuit. Replace `` with the circuit name. ```sh npx circomkit contract ``` -------------------------------- ### Generate Witness with Circomkit CLI Source: https://context7.com/erhant/circomkit/llms.txt Generate the witness for a circuit given a default input using the `witness` command. This is a prerequisite for proving. ```bash # Generate witness for an input npx circomkit witness multiplier_3 default ``` -------------------------------- ### Use Circomkit CLI Locally Source: https://github.com/erhant/circomkit/blob/main/README.md Command to run the Circomkit CLI locally, useful for hands-on testing during development, mimicking npx circomkit. ```sh pnpm cli ``` -------------------------------- ### Download PTAU File with Circomkit CLI Source: https://context7.com/erhant/circomkit/llms.txt Download the necessary Powers of Tau (PTAU) file for a circuit using the `ptau` command. This is often automatic for standard curves like bn128. ```bash # Download PTAU file (automatic for bn128) npx circomkit ptau multiplier_3 ``` -------------------------------- ### Extract Verification Key with Circomkit CLI Source: https://context7.com/erhant/circomkit/llms.txt Extract the verification key from the prover key using the `vkey` command. This is useful for setting up verifiers. ```bash # Extract verification key from prover key npx circomkit vkey multiplier_3 ``` -------------------------------- ### Circomkit Global Configuration (circomkit.json) Source: https://context7.com/erhant/circomkit/llms.txt Defines global settings for Circomkit, including protocol, paths, and optimization levels. Ensure paths are correctly set for your project structure. ```json // circomkit.json - Global configuration { "version": "2.1.0", "protocol": "groth16", "prime": "bn128", "optimization": 1, "verbose": true, "logLevel": "INFO", "circuits": "./circuits.json", "dirCircuits": "./circuits", "dirInputs": "./inputs", "dirBuild": "./build", "dirPtau": "./ptau", "circomPath": "circom", "include": ["./node_modules"], "inspect": true, "groth16numContributions": 1, "groth16askForEntropy": false, "cWitness": false, "wasmWitness": true, "prettyCalldata": false } ``` -------------------------------- ### Circomkit Circuit Definitions (circuits.json) Source: https://context7.com/erhant/circomkit/llms.txt Defines individual circuits, specifying their source file, template name, parameters, and public inputs. This file maps circuit names to their Circom implementation details. ```json // circuits.json - Circuit definitions { "multiplier_3": { "file": "multiplier", "template": "Multiplier", "params": [3] }, "sudoku_9x9": { "file": "sudoku", "template": "Sudoku", "params": [3], "pubs": ["puzzle"] }, "arrays_2_3": { "file": "arrays", "template": "Arrays", "params": [2, 3], "pubs": ["in1D", "in2D"] } } ``` -------------------------------- ### Initialize WitnessTester for Type-Safe Circuit Testing Source: https://context7.com/erhant/circomkit/llms.txt Use WitnessTester to create a type-safe instance for testing circuit constraints and witness computation. Ensure the circuit file and template are correctly specified. Recompilation can be forced if needed. ```typescript import { Circomkit, WitnessTester } from 'circomkit'; const circomkit = new Circomkit({ verbose: false, logLevel: 'silent', }); // Create a type-safe witness tester with input and output signal names let circuit: WitnessTester<['in'], ['out']>; circuit = await circomkit.WitnessTester('multiplier_3', { file: 'multiplier', template: 'Multiplier', params: [3], recompile: true, // Force recompilation }); ``` -------------------------------- ### Export Calldata for Solidity Verifier Source: https://context7.com/erhant/circomkit/llms.txt Export the necessary calldata for verifying a proof in a Solidity contract using the `calldata` command. ```bash # Export calldata for Solidity verifier npx circomkit calldata multiplier_3 default ``` -------------------------------- ### Compile Circuit with Circomkit CLI Source: https://context7.com/erhant/circomkit/llms.txt Compile a specific circuit or multiple circuits matching a pattern using the `compile` command. This generates the necessary intermediate files. ```bash # Compile a circuit (or all circuits matching pattern) npx circomkit compile multiplier_3 npx circomkit compile "multiplier_.*" # Regex pattern ``` -------------------------------- ### Export JSON Files with Circomkit CLI Source: https://context7.com/erhant/circomkit/llms.txt Export various circuit artifacts in JSON format, including R1CS, zkey, and witness files, using the `json` command. ```bash # Export JSON files npx circomkit json r1cs multiplier_3 npx circomkit json zkey multiplier_3 npx circomkit json wtns multiplier_3 default ``` -------------------------------- ### Export Solidity Verifier Contract Source: https://context7.com/erhant/circomkit/llms.txt Export the Solidity verifier contract for a circuit using the `contract` command. This allows integration with Ethereum-based systems. ```bash # Export Solidity verifier contract npx circomkit contract multiplier_3 ``` -------------------------------- ### Circomkit CLI: Export Calldata Source: https://github.com/erhant/circomkit/blob/main/README.md Export the necessary calldata for verification to the console. Input files are expected at `inputs//.json`. Replace `` and ``. ```sh npx circomkit calldata ``` -------------------------------- ### Handle Multi-Dimensional Signals in WitnessTester Source: https://context7.com/erhant/circomkit/llms.txt Demonstrates how WitnessTester handles complex signal structures like 1D and multi-dimensional arrays. Use this to test circuits with intricate input and output signal shapes. ```typescript import { Circomkit, WitnessTester } from 'circomkit'; const circomkit = new Circomkit({ verbose: false }); // Circuit with 2D array signals let circuit: WitnessTester<['a', 'b'], ['aOut', 'bOut', 'cOut']>; circuit = await circomkit.WitnessTester('multiout', { file: 'multiout', template: 'Multiout', params: [10], // N = 10 }); const input = { a: Array(10).fill(2), // 1D array input b: Array(10).fill(3), // 1D array input }; // Compute multiple output signals const output = await circuit.compute(input, ['aOut', 'bOut', 'cOut']); // Output signals can be multi-dimensional arrays console.log(output.aOut); // Array of BigInt values console.log(output.bOut); // Array of BigInt values console.log(output.cOut); // Array of BigInt values // Verify with computed output await circuit.expectPass(input, output); // Read specific witness signals from components const witness = await circuit.calculateWitness(input); const signals = await circuit.readWitnessSignals(witness, ['aOut', 'cOut']); ``` -------------------------------- ### Direct Proof Verification with ProofTester Source: https://context7.com/erhant/circomkit/llms.txt Verify a generated proof directly using the `verify` method. This returns a boolean indicating the proof's validity. ```typescript // Verify using the verify method directly const isValid = await circuit.verify(proof, publicSignals); console.log(`Proof valid: ${isValid}`); // true ``` -------------------------------- ### Lint Code Source: https://github.com/erhant/circomkit/blob/main/README.md Command to perform linting on all project files to ensure code quality and style consistency. ```sh pnpm lint ``` -------------------------------- ### Generating Solidity Verifier Contract and Calldata Source: https://context7.com/erhant/circomkit/llms.txt Generates a Solidity verifier contract and retrieves calldata for on-chain proof verification. This process involves compiling, setting up, and proving the circuit. ```typescript import { Circomkit } from 'circomkit'; import { ethers } from 'ethers'; const circomkit = new Circomkit({ protocol: 'groth16' }); // Compile and setup the circuit await circomkit.compile('multiplier_3'); await circomkit.setup('multiplier_3'); // Generate proof await circomkit.prove('multiplier_3', 'default', { in: [2, 3, 4] }); // Export Solidity verifier contract const contractPath = await circomkit.contract('multiplier_3'); // Output: build/multiplier_3/groth16_verifier.sol // Get calldata for contract verification const calldata = await circomkit.calldata('multiplier_3', 'default'); // Output format for Groth16: // [["0x...", "0x..."], [["0x...", "0x..."], ["0x...", "0x..."]], ["0x...", "0x..."]] // ["0x..."] // Parse calldata for contract call const args = calldata .split('\n') .filter(x => !!x) .map(x => JSON.parse(x)); // Call verifier contract // const result = await verifierContract.verifyProof(...args); ``` -------------------------------- ### Circomkit CLI: Clear Circuit Artifacts Source: https://github.com/erhant/circomkit/blob/main/README.md Remove generated artifacts for a specific circuit. Replace `` with the circuit name. ```sh npx circomkit clear ``` -------------------------------- ### CircuitConfig Type Definition Source: https://context7.com/erhant/circomkit/llms.txt Defines the structure for configuring a circuit's main component instantiation. Use this to specify file, template, parameters, and other Circom-related options. ```typescript import type { CircuitConfig } from 'circomkit'; const config: CircuitConfig = { file: 'multiplier', // File path relative to dirCircuits (without .circom) template: 'Multiplier', // Template name to instantiate params: [3], // Template parameters (optional, defaults to []) pubs: ['puzzle'], // Public input signal names (optional, defaults to []) dir: 'main', // Output directory for main component (optional) version: '2.1.0', // Circom version pragma (optional) usesCustomTemplates: false, // Include pragma custom_templates (optional) }; // This generates: circuits/main/multiplier_3.circom // pragma circom 2.1.0; // include "../multiplier.circom"; // component main = Multiplier(3); ``` -------------------------------- ### Test Valid Input/Output with WitnessTester Source: https://context7.com/erhant/circomkit/llms.txt Assert that a given input produces the expected output using `expectPass`. This is useful for verifying correct circuit behavior. ```typescript // Test that valid input produces expected output await circuit.expectPass( { in: [2, 3, 4] }, // Input signals { out: BigInt(24) } // Expected output ); ``` -------------------------------- ### Clear Build Artifacts with Circomkit CLI Source: https://context7.com/erhant/circomkit/llms.txt Remove generated build artifacts for a circuit using the `clear` command. This is useful for cleaning up the project directory. ```bash # Clear build artifacts npx circomkit clear multiplier_3 ``` -------------------------------- ### Check Constraint Count with Witness Tester Source: https://github.com/erhant/circomkit/blob/main/README.md Verify the number of constraints in a circuit using `expectConstraintCount`. This function can check for an exact match or a minimum number of constraints. ```typescript it('should have correct number of constraints', async () => { // expects at least N constraints await circuit.expectConstraintCount(N); // expects exactly N constraints await circuit.expectConstraintCount(N, true); }); ``` -------------------------------- ### Exporting Circuit Artifacts to JSON Source: https://context7.com/erhant/circomkit/llms.txt Exports R1CS constraints, prover keys, and witness data to JSON format. This is useful for debugging and integrating with other tools. ```typescript import { Circomkit } from 'circomkit'; const circomkit = new Circomkit(); // Export R1CS constraints to JSON const { json: r1csJson, path: r1csPath } = await circomkit.json('r1cs', 'multiplier_3'); console.log(`R1CS exported to: ${r1csPath}`); // Contains: constraints, prime, nVars, nOutputs, nPubInputs, nPrvInputs // Export prover key to JSON (Groth16 only) const { json: zkeyJson, path: zkeyPath } = await circomkit.json('zkey', 'multiplier_3'); // Export witness to JSON const { json: wtnsJson, path: wtnsPath } = await circomkit.json('wtns', 'multiplier_3', 'default'); // Contains: array of witness values as strings ``` -------------------------------- ### Calculate Witness with WitnessTester Source: https://context7.com/erhant/circomkit/llms.txt Calculate the witness for a given input using `calculateWitness`. This witness can then be used for advanced testing or inspection. ```typescript // Calculate witness for advanced testing const witness = await circuit.calculateWitness({ in: [2, 3, 4] }); ``` -------------------------------- ### Compute Output Signals with WitnessTester Source: https://context7.com/erhant/circomkit/llms.txt Compute specific output signals for a given input without performing full assertions using `compute`. This is useful for debugging or inspecting intermediate values. ```typescript // Compute output signals without full assertion const output = await circuit.compute({ in: [2, 3, 4] }, ['out']); console.log(output.out); // 24n ``` -------------------------------- ### Test Soundness Errors by Editing Witness Source: https://context7.com/erhant/circomkit/llms.txt Use `editWitness` to modify a witness and then `expectConstraintFail` to test for soundness errors. This helps ensure the circuit correctly rejects tampered witnesses. ```typescript // Test for soundness errors by editing witness const fakeWitness = await circuit.editWitness(witness, { 'main.in[0]': BigInt(1), 'main.in[1]': BigInt(1), 'main.in[2]': BigInt(24), }); await circuit.expectConstraintFail(fakeWitness); // Should fail if circuit is sound ``` -------------------------------- ### Witness Tester for Circuit Assertions Source: https://github.com/erhant/circomkit/blob/main/README.md The Witness Tester extends circom_tester with type-safety and assertion functions for inputs and outputs. It helps reduce boilerplate code for testing circuit logic. ```typescript describe('witness tester', () => { // input signals and output signals can be given as type parameters // this makes all functions type-safe! let circuit: WitnessTester<['in'], ['out']>; beforeAll(async () => { const circomkit = new Circomkit(); circuit = await circomkit.WitnessTester(CIRCUIT_NAME, CIRCUIT_CONFIG); }); it('should pass on correct input & output', async () => { await circuit.expectPass(INPUT, OUTPUT); }); it('should fail on wrong output', async () => { await circuit.expectFail(INPUT, WRONG_OUTPUT); }); it('should fail on bad input', async () => { await circuit.expectFail(BAD_INPUT); }); }); ``` -------------------------------- ### Test Proof Failure with Incorrect Public Signals Source: https://context7.com/erhant/circomkit/llms.txt Use `expectFail` with a proof and incorrect public signals to test that the verification process correctly rejects invalid proofs. ```typescript // Test that proof fails with wrong public signals await circuit.expectFail(proof, ['999']); ``` -------------------------------- ### Test Invalid Input with WitnessTester Source: https://context7.com/erhant/circomkit/llms.txt Use `expectFail` to verify that invalid input causes the circuit constraints to fail. This helps in identifying constraint violations. ```typescript // Test that invalid input fails constraints await circuit.expectFail({ in: [1, 1, 1] }); // Returns error message like: "Error: Assert Failed." ``` -------------------------------- ### Test Circuit Errors with WitnessTester Source: https://context7.com/erhant/circomkit/llms.txt Utilize WitnessTester's `expectFail` method to catch various error conditions in circuit execution, such as incorrect input counts or assertion failures. Ensure your circuits handle invalid inputs gracefully. ```typescript import { Circomkit, WitnessTester } from 'circomkit'; const circomkit = new Circomkit({ verbose: false }); let circuit: WitnessTester<['in', 'inin'], ['out']>; circuit = await circomkit.WitnessTester('error_test', { file: 'errors', template: 'Errors', }); // Test fewer inputs than expected const error1 = await circuit.expectFail({ in: 0, inin: [1] }); // Error: "Not enough values for input signal inin" // Test more inputs than expected const error2 = await circuit.expectFail({ in: 0, inin: [1, 2, 3] }); // Error: "Too many values for input signal inin" // Test constraint/assertion failure const error3 = await circuit.expectFail({ in: 1, inin: [1, 2] }); // Error: "Error: Assert Failed." // Test missing signal (TypeScript will warn but runtime catches it) // @ts-expect-error const error4 = await circuit.expectFail({ inin: [1, 2] }); // Error: "Not all inputs have been set. Only 2 out of 3." ``` -------------------------------- ### Test Witness Constraints with Witness Tester Source: https://github.com/erhant/circomkit/blob/main/README.md Validate circuit constraints against a computed or modified witness. This is crucial for checking soundness errors by ensuring constraints fail on manipulated witnesses. ```typescript it('should pass on correct witness', async () => { const witness = await circuit.calculateWitness(INPUT); await circuit.expectConstraintPass(witness); }); it('should fail on fake witness', async () => { const witness = await circuit.calculateWitness(INPUT); const badWitness = await circuit.editWitness(witness, { 'main.signal': BigInt(1234), 'main.component.signal': BigInt('0xCAFE'), 'main.foo.bar[0]': BigInt('0b0101'), }); await circuit.expectConstraintFail(badWitness); }); ``` -------------------------------- ### Read Witness Symbol Values with WitnessTester Source: https://context7.com/erhant/circomkit/llms.txt Read specific symbol values from a calculated witness using `readWitness`. This allows inspection of individual signal values within the witness. ```typescript // Read specific symbol values from witness const symbols = await circuit.readWitness(witness, ['main.out', 'main.in[0]']); console.log(symbols['main.out']); // 24n ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.