### PLONK Setup
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Performs the setup phase for the PLONK proving system using a universal powers of tau file.
```shell
snarkjs plonk setup circuit.r1cs pot14_final.ptau circuit_final.zkey
```
--------------------------------
### FFLONK Setup
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Performs the setup phase for the FFLONK proving system using a universal powers of tau file. Note: FFLONK is in Beta.
```shell
snarkjs fflonk setup circuit.r1cs pot14_final.ptau circuit.zkey
```
--------------------------------
### PLONK - Setup
Source: https://context7.com/iden3/snarkjs/llms.txt
Initializes the PLONK setup process, creating a proving key. PLONK does not require a circuit-specific trusted ceremony like Groth16.
```APIDOC
## PLONK - Setup
Create a PLONK proving key. Unlike Groth16, PLONK does not require a circuit-specific trusted ceremony.
### Method
POST (or equivalent function call)
### Endpoint
N/A (Function call within the library)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example (JavaScript)
```javascript
import * as snarkjs from "snarkjs";
await snarkjs.plonk.setup(
"circuit.r1cs",
"pot14_final.ptau",
"circuit_plonk.zkey"
);
```
### Request Example (CLI)
```bash
snarkjs plonk setup circuit.r1cs pot14_final.ptau circuit.zkey
```
### Response
#### Success Response (200)
Indicates successful setup completion. The output is a generated zkey file (`circuit_plonk.zkey`).
#### Response Example
N/A
```
--------------------------------
### PLONK Setup
Source: https://context7.com/iden3/snarkjs/llms.txt
Create a PLONK proving key. Unlike Groth16, PLONK does not require a circuit-specific trusted ceremony for setup.
```javascript
import * as snarkjs from "snarkjs";
// Setup PLONK (no phase 2 contributions needed)
await snarkjs.plonk.setup(
"circuit.r1cs",
"pot14_final.ptau",
"circuit_plonk.zkey"
);
```
```bash
# CLI equivalent
snarkjs plonk setup circuit.r1cs pot14_final.ptau circuit.zkey
```
--------------------------------
### Initialize and Install SnarkJS
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Commands to initialize a Node.js project and install the snarkjs library.
```sh
npm init
npm install snarkjs
```
--------------------------------
### Create new directory and navigate
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Sets up a new directory for snarkjs examples and changes the current directory into it.
```sh
mkdir snarkjs_example
cd snarkjs_example
```
--------------------------------
### Browser Example: Generate and Verify Proof
Source: https://github.com/iden3/snarkjs/blob/master/README.md
HTML and JavaScript example for generating and verifying a Groth16 proof in the browser using snarkjs.
```html
Snarkjs client example
Snarkjs client example
Proof:
Result:
```
--------------------------------
### Prepare Phase 2 of Trusted Setup
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Use this command to calculate the encrypted evaluation of Lagrange polynomials for phase 2 of the trusted setup. This requires a beacon Ptau file and outputs a final Ptau file for generating proving and verification keys.
```sh
snarkjs powersoftau prepare phase2 pot14_beacon.ptau pot14_final.ptau -v
```
--------------------------------
### Groth16 Setup
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Generates the initial reference zkey for Groth16 without phase 2 contributions. This zkey is not safe for production and requires further contributions.
```shell
snarkjs groth16 setup circuit.r1cs pot14_final.ptau circuit_0000.zkey
```
--------------------------------
### Setup and Prove FFLONK Proof
Source: https://context7.com/iden3/snarkjs/llms.txt
FFLONK is an efficient variant of PLONK. This snippet shows how to set up FFLONK, generate a proof, and verify it. Note: FFLONK is currently in beta. Requires circuit.r1cs, pot14_final.ptau, circuit.wasm, and circuit_fflonk.zkey.
```javascript
import * as snarkjs from "snarkjs";
// Setup FFLONK
await snarkjs.fflonk.setup(
"circuit.r1cs",
"pot14_final.ptau",
"circuit_fflonk.zkey"
);
// Generate FFLONK proof
const input = { a: 3, b: 11 };
const { proof, publicSignals } = await snarkjs.fflonk.fullProve(
input,
"circuit.wasm",
"circuit_fflonk.zkey"
);
// Verify FFLONK proof
const vKey = await snarkjs.zKey.exportVerificationKey("circuit_fflonk.zkey");
const isValid = await snarkjs.fflonk.verify(vKey, publicSignals, proof);
console.log("FFLONK proof valid:", isValid);
```
```bash
# CLI equivalent
snarkjs fflonk setup circuit.r1cs pot14_final.ptau circuit.zkey
snarkjs fflonk fullprove input.json circuit.wasm circuit.zkey proof.json public.json
snarkjs fflonk verify verification_key.json public.json proof.json
```
--------------------------------
### Define a Circom Circuit
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Create a circuit definition file using Circom. This example defines a `Multiplier` template with a specified number of constraints.
```circom
pragma circom 2.0.0;
template Multiplier(n) {
signal input a;
signal input b;
signal output c;
signal int[n];
int[0] <== a*a + b;
for (var i=1; i
```
--------------------------------
### Apply Random Beacon to Powers of Tau
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Finalize phase 1 of the trusted setup by applying a random beacon contribution to the Powers of Tau file.
```sh
snarkjs powersoftau beacon pot14_0003.ptau pot14_beacon.ptau 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon"
```
--------------------------------
### Complete zkSNARK Workflow with snarkjs
Source: https://context7.com/iden3/snarkjs/llms.txt
This function demonstrates the entire zkSNARK workflow, including trusted setup, proof generation, and verification. It requires pre-existing powers of tau and a compiled circom circuit.
```javascript
import * as snarkjs from "snarkjs";
import fs from "fs";
async function completeZkSnarkWorkflow() {
// Step 1: Use pre-existing powers of tau (download from Hermez ceremony)
// https://storage.googleapis.com/zkevm/ptau/powersOfTau28_hez_final_14.ptau
// Step 2: Compile circuit with circom (external step)
// circom circuit.circom --r1cs --wasm --sym
// Step 3: Setup Groth16
await snarkjs.zKey.newZKey(
"circuit.r1cs",
"powersOfTau28_hez_final_14.ptau",
"circuit_0000.zkey"
);
// Step 4: Contribute to ceremony
await snarkjs.zKey.contribute(
"circuit_0000.zkey",
"circuit_0001.zkey",
"Contributor 1",
"random entropy"
);
// Step 5: Apply beacon
await snarkjs.zKey.beacon(
"circuit_0001.zkey",
"circuit_final.zkey",
"Beacon",
"0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
10
);
// Step 6: Export verification key
const vKey = await snarkjs.zKey.exportVerificationKey("circuit_final.zkey");
// Step 7: Generate proof
const input = { a: "3", b: "11" };
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
input,
"circuit_js/circuit.wasm",
"circuit_final.zkey"
);
// Step 8: Verify proof
const isValid = await snarkjs.groth16.verify(vKey, publicSignals, proof);
console.log("Proof verified:", isValid);
// Step 9: Export Solidity verifier for on-chain verification
const templates = {
groth16: fs.readFileSync("templates/verifier_groth16.sol.ejs", "utf8")
};
const solidityVerifier = await snarkjs.zKey.exportSolidityVerifier(
"circuit_final.zkey",
templates
);
fs.writeFileSync("Verifier.sol", solidityVerifier);
return { proof, publicSignals, isValid };
}
completeZkSnarkWorkflow().then(console.log);
```
--------------------------------
### Node.js Groth16 Full Prove and Verify
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Example of using snarkjs in Node.js to generate a Groth16 proof and verify it.
```javascript
const snarkjs = require("snarkjs");
const fs = require("fs");
async function run() {
const { proof, publicSignals } = await snarkjs.groth16.fullProve({a: 10, b: 21}, "circuit.wasm", "circuit_final.zkey");
console.log("Proof: ");
console.log(JSON.stringify(proof, null, 1));
const vKey = JSON.parse(fs.readFileSync("verification_key.json"));
const res = await snarkjs.groth16.verify(vKey, publicSignals, proof);
if (res === true) {
console.log("Verification OK");
} else {
console.log("Invalid proof");
}
}
run().then(() => {
process.exit(0);
});
```
--------------------------------
### Install snarkjs
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Installs the latest version of snarkjs globally using npm. Ensure you have a compatible Node.js version (v18 or later).
```sh
npm install -g snarkjs@latest
```
--------------------------------
### Prepare Powers of Tau for Phase 2
Source: https://context7.com/iden3/snarkjs/llms.txt
Calculates encrypted Lagrange polynomial evaluations to prepare the powers of tau file for phase 2 of the trusted setup.
```javascript
import * as snarkjs from "snarkjs";
// Prepare phase 2 (calculates Lagrange polynomials at tau)
await snarkjs.powersOfTau.preparePhase2("pot14_beacon.ptau", "pot14_final.ptau");
```
```bash
# CLI equivalent
snarkjs powersoftau prepare phase2 pot14_beacon.ptau pot14_final.ptau -v
```
--------------------------------
### Get Circuit Information
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Use `snarkjs r1cs info` to display statistics about the compiled circuit, such as the number of wires, constraints, and inputs/outputs.
```sh
snarkjs r1cs info circuit.r1cs
```
--------------------------------
### Single-Threaded Proof Generation
Source: https://context7.com/iden3/snarkjs/llms.txt
Enables single-threaded proof generation, suitable for environments lacking Worker support such as Bun or browser extensions. This example uses the groth16 prove function with the singleThread option.
```javascript
import * as snarkjs from "snarkjs";
// Generate proof in single-threaded mode
const { proof, publicSignals } = await snarkjs.groth16.prove(
"circuit_final.zkey",
"witness.wtns",
undefined, // logger
{ singleThread: true } // options
);
console.log("Proof generated in single-threaded mode");
```
--------------------------------
### Verify zKey File Validity
Source: https://context7.com/iden3/snarkjs/llms.txt
Verify that a zkey file is valid and matches the circuit and powers of tau. This ensures the integrity of the trusted setup.
```javascript
import * as snarkjs from "snarkjs";
// Verify zkey against R1CS and ptau
const isValid = await snarkjs.zKey.verifyFromR1cs(
"circuit.r1cs",
"pot14_final.ptau",
"circuit_final.zkey"
);
console.log("zKey valid:", isValid);
```
```bash
# CLI equivalent
snarkjs zkey verify r1cs circuit.r1cs pot14_final.ptau circuit_final.zkey
```
--------------------------------
### Start Powers of Tau ceremony
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Initiates a new powers of tau ceremony for a specified elliptic curve (e.g., bn128) and constraint power (e.g., 14). The `-v` flag enables verbose output.
```sh
snarkjs powersoftau new bn128 14 pot14_0000.ptau -v
```
--------------------------------
### Get R1CS Circuit Information
Source: https://context7.com/iden3/snarkjs/llms.txt
Retrieves and displays statistics about an R1CS constraint system file. This includes details like the curve used, number of wires, constraints, and input/output counts. Requires the R1CS file.
```javascript
import * as snarkjs from "snarkjs";
// Print circuit statistics
await snarkjs.r1cs.info("circuit.r1cs");
// Output:
// Curve: bn-128
// # of Wires: 1003
// # of Constraints: 1000
// # of Private Inputs: 2
// # of Public Inputs: 0
// # of Outputs: 1
```
```bash
# CLI equivalent
snarkjs r1cs info circuit.r1cs
```
--------------------------------
### Start New Powers of Tau Ceremony (bn128)
Source: https://context7.com/iden3/snarkjs/llms.txt
Initiates a new powers of tau ceremony for the bn128 curve. Specify the maximum number of constraints via the power parameter. Supports up to 2^28 constraints.
```javascript
import * as snarkjs from "snarkjs";
// Start a new powers of tau ceremony for bn128 curve with power 14 (16,384 max constraints)
const curve = await snarkjs.curves.getCurveFromName("bn128");
await snarkjs.powersOfTau.newAccumulator(curve, 14, "pot14_0000.ptau");
// For larger circuits, use higher power (e.g., power 20 for 1M constraints)
// await snarkjs.powersOfTau.newAccumulator(curve, 20, "pot20_0000.ptau");
```
```bash
# CLI equivalent
snarkjs powersoftau new bn128 14 pot14_0000.ptau -v
```
--------------------------------
### zkey File Format Overview
Source: https://github.com/iden3/snarkjs/blob/master/doc/blockplonk_zkey_format.md
Provides a high-level overview of the zkey file structure, including magic bytes, version, and section count.
```text
┏━━━━┳━━━━━━━━━━━━━━━━━┓
┃ 4 ┃ 7A 6B 65 79 ┃ Magic "zkey"
┗━━━━┻━━━━━━━━━━━━━━━━━┛
┏━━━━┳━━━━━━━━━━━━━━━━━┓
┃ 4 ┃ 01 00 00 00 ┃ Version 1
┗━━━━┻━━━━━━━━━━━━━━━━━┛
┏━━━━┳━━━━━━━━━━━━━━━━━┓
┃ 4 ┃ 0A 00 00 00 ┃ Number of Sections
┗━━━━┻━━━━━━━━━━━━━━━━━┛
┏━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ 4 ┃ sectionType ┃ 8 ┃ SectionSize ┃
┗━━━━┻━━━━━━━━━━━━━━━━━┻━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━━━━━━━━━┓
┃ ┃
┃ ┃
┃ ┃
┃ Section Content ┃
┃ ┃
┃ ┃
┃ ┃
┗━━━━━━━━━━━━━━━━━━━━━┛
...
...
...
```
--------------------------------
### Groth16 - Full Prove (Witness + Proof in One Step)
Source: https://context7.com/iden3/snarkjs/llms.txt
Combines witness calculation and Groth16 proof generation into a single, convenient operation.
```APIDOC
## Groth16 - Full Prove (Witness + Proof in One Step)
Calculate witness and generate proof in a single operation.
### Method
POST (or equivalent function call)
### Endpoint
N/A (Function call within the library)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example (JavaScript)
```javascript
import * as snarkjs from "snarkjs";
const input = { a: 10, b: 21 };
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
input,
"circuit.wasm",
"circuit_final.zkey"
);
console.log("Proof generated successfully");
console.log("Public signals:", publicSignals);
```
### Request Example (CLI)
```bash
snarkjs groth16 fullprove input.json circuit.wasm circuit_final.zkey proof.json public.json
```
### Response
#### Success Response (200)
- **proof** (object) - Contains the proof components (pi_a, pi_b, pi_c).
- **publicSignals** (array) - Contains the public inputs and outputs of the circuit.
#### Response Example
```json
{
"proof": {
"pi_a": ["...", "..."],
"pi_b": [["...", "..."], ["...", "..."]],
"pi_c": ["...", "..."]
},
"publicSignals": ["...", "..."]
}
```
```
--------------------------------
### Full Prove (Witness + Proof in One Step) - Groth16
Source: https://context7.com/iden3/snarkjs/llms.txt
Calculate witness and generate proof in a single operation for Groth16. This is convenient when inputs are readily available.
```javascript
import * as snarkjs from "snarkjs";
// Generate proof directly from inputs (combines witness calculation and proving)
const input = { a: 10, b: 21 };
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
input,
"circuit.wasm",
"circuit_final.zkey"
);
console.log("Proof generated successfully");
console.log("Public signals:", publicSignals);
```
```bash
# CLI equivalent
snarkjs groth16 fullprove input.json circuit.wasm circuit_final.zkey proof.json public.json
```
--------------------------------
### Contribute to Powers of Tau Ceremony
Source: https://context7.com/iden3/snarkjs/llms.txt
Adds a contribution to an existing powers of tau ceremony using provided entropy. This enhances the security of the setup.
```javascript
import * as snarkjs from "snarkjs";
// Contribute to the ceremony with a name and entropy
await snarkjs.powersOfTau.contribute(
"pot14_0000.ptau", // Input ptau file
"pot14_0001.ptau", // Output ptau file
"First contribution", // Contributor name
"random entropy text" // Random entropy source
);
```
```bash
# CLI equivalent
snarkjs powersoftau contribute pot14_0000.ptau pot14_0001.ptau --name="First contribution" -e="random entropy text" -v
```
--------------------------------
### Contribute to zkey using Third-Party Software
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Demonstrates contributing to the zkey using external software. This involves exporting, contributing externally, and then importing the response.
```shell
snarkjs zkey export bellman circuit_0002.zkey challenge_phase2_0003
```
```shell
snarkjs zkey bellman contribute bn128 challenge_phase2_0003 response_phase2_0003 -e="some random text"
```
```shell
snarkjs zkey import bellman circuit_0002.zkey response_phase2_0003 circuit_0003.zkey -n="Third contribution name"
```
--------------------------------
### Create Input File for Circuit
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Creates a JSON file containing inputs for a circuit. Ensure big integers are enclosed in double quotes to maintain precision.
```shell
cat < input.json
{"a": "3", "b": "11"}
EOT
```
--------------------------------
### Generate FFLONK Proof and Witness
Source: https://github.com/iden3/snarkjs/blob/master/README.md
This command calculates the witness and generates a FFLONK proof in a single step.
```sh
snarkjs fflonk fullprove witness.json circuit.wasm circuit_final.zkey proof.json public.json
```
--------------------------------
### Generate FFLONK Proof
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Use this command to generate a FFLONK proof from a circuit.
```sh
snarkjs fflonk prove circuit.zkey witness.wtns proof.json public.json
```
--------------------------------
### Verify Latest zkey
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Verifies the zkey file against the circuit R1CS and powers of tau files, checking all phase 2 contributions.
```shell
snarkjs zkey verify circuit.r1cs pot14_final.ptau circuit_0003.zkey
```
--------------------------------
### Generate Groth16 Proof and Witness
Source: https://github.com/iden3/snarkjs/blob/master/README.md
This command calculates the witness and generates a Groth16 proof in a single step.
```sh
snarkjs groth16 fullprove input.json circuit.wasm circuit_final.zkey proof.json public.json
```
--------------------------------
### Generate Groth16 Proof
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Use this command to generate a Groth16 proof from a circuit.
```sh
snarkjs groth16 prove circuit_final.zkey witness.wtns proof.json public.json
```
--------------------------------
### Verify FFLONK Proof
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Use this command to verify a FFLONK proof.
```sh
snarkjs fflonk verify verification_key.json public.json proof.json
```
--------------------------------
### Create Initial Groth16 zKey from R1CS
Source: https://context7.com/iden3/snarkjs/llms.txt
Generates an initial Groth16 zkey file from an R1CS circuit and a powers of tau file. This zkey requires phase 2 contributions before production use.
```javascript
import * as snarkjs from "snarkjs";
// Create initial zkey (requires contributions before production use)
const csHash = await snarkjs.zKey.newZKey(
"circuit.r1cs", // R1CS constraint system
"pot14_final.ptau", // Powers of tau file
"circuit_0000.zkey" // Output zkey file
);
console.log("Circuit hash:", csHash);
```
```bash
# CLI equivalent
snarkjs groth16 setup circuit.r1cs pot14_final.ptau circuit_0000.zkey
```
--------------------------------
### Calculate Witness with Node.js
Source: https://github.com/iden3/snarkjs/blob/master/README.md
An alternative method to calculate the witness using a Node.js script generated by circom.
```shell
node circuit_js/generate_witness.js circuit_js/circuit.wasm input.json witness.wtns
```
--------------------------------
### Create PLONK Proof
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Generates a proof using the PLONK proving system with the final zkey, witness, and output files for proof and public inputs.
```shell
snarkjs plonk prove circuit_final.zkey witness.wtns proof.json public.json
```
--------------------------------
### View specific command help
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Shows detailed help for a specific snarkjs command, such as 'groth16 prove'. Shorter aliases like 'g16p' can also be used.
```sh
snarkjs groth16 prove --help
```
--------------------------------
### Add Contributions to zKey Ceremony (Groth16 Phase 2)
Source: https://context7.com/iden3/snarkjs/llms.txt
Contribute to the circuit-specific phase 2 ceremony for Groth16. Ensure you provide valid entropy for security.
```javascript
import * as snarkjs from "snarkjs";
// Add a contribution to the zkey
await snarkjs.zKey.contribute(
"circuit_0000.zkey", // Input zkey
"circuit_0001.zkey", // Output zkey
"1st Contributor", // Name
"random entropy text" // Entropy
);
// Apply beacon to finalize
await snarkjs.zKey.beacon(
"circuit_0001.zkey",
"circuit_final.zkey",
"Final Beacon",
"0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
10
);
```
```bash
# CLI equivalent
snarkjs zkey contribute circuit_0000.zkey circuit_0001.zkey --name="1st Contributor" -e="random entropy"
snarkjs zkey beacon circuit_0001.zkey circuit_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon"
```
--------------------------------
### zKey - Contribute to Phase 2 Ceremony
Source: https://context7.com/iden3/snarkjs/llms.txt
Allows contributions to the circuit-specific phase 2 ceremony for Groth16, enabling the creation of secure zkey files.
```APIDOC
## zKey - Contribute to Phase 2 Ceremony
Add contributions to the circuit-specific phase 2 ceremony for Groth16.
### Method
POST (or equivalent function call)
### Endpoint
N/A (Function call within the library)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example (JavaScript)
```javascript
import * as snarkjs from "snarkjs";
await snarkjs.zKey.contribute(
"circuit_0000.zkey", // Input zkey
"circuit_0001.zkey", // Output zkey
"1st Contributor", // Name
"random entropy text" // Entropy
);
await snarkjs.zKey.beacon(
"circuit_0001.zkey",
"circuit_final.zkey",
"Final Beacon",
"0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
10
);
```
### Request Example (CLI)
```bash
snarkjs zkey contribute circuit_0000.zkey circuit_0001.zkey --name="1st Contributor" -e="random entropy"
snarkjs zkey beacon circuit_0001.zkey circuit_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon"
```
### Response
#### Success Response (200)
Indicates successful contribution and beacon application. No specific return value documented, operation is side-effect based (file creation/modification).
#### Response Example
N/A
```
--------------------------------
### Generate PLONK Proof and Witness
Source: https://github.com/iden3/snarkjs/blob/master/README.md
This command calculates the witness and generates a PLONK proof in a single step.
```sh
snarkjs plonk fullprove witness.json circuit.wasm circuit_final.zkey proof.json public.json
```
--------------------------------
### Generate and Verify PLONK Proof
Source: https://context7.com/iden3/snarkjs/llms.txt
Use this to generate a PLONK proof from inputs and circuit files, export the verification key, and then verify the generated proof. Requires circuit.wasm and circuit_plonk.zkey.
```javascript
import * as snarkjs from "snarkjs";
import fs from "fs";
// Generate PLONK proof
const input = { a: 3, b: 11 };
const { proof, publicSignals } = await snarkjs.plonk.fullProve(
input,
"circuit.wasm",
"circuit_plonk.zkey"
);
// Export verification key
const vKey = await snarkjs.zKey.exportVerificationKey("circuit_plonk.zkey");
// Verify PLONK proof
const isValid = await snarkjs.plonk.verify(vKey, publicSignals, proof);
console.log("PLONK proof valid:", isValid);
```
```bash
# CLI equivalent
snarkjs plonk fullprove input.json circuit.wasm circuit.zkey proof.json public.json
snarkjs plonk verify verification_key.json public.json proof.json
```
--------------------------------
### View snarkjs help
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Displays a list of all available snarkjs commands and their usage. Use with specific commands for detailed help.
```sh
snarkjs --help
```
--------------------------------
### Export Solidity Calldata
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Simulate a verification call and export the calldata for use in a smart contract.
```sh
snarkjs zkey export soliditycalldata public.json proof.json
```
--------------------------------
### zKey - Export Verification Key
Source: https://context7.com/iden3/snarkjs/llms.txt
Exports the verification key from a generated zkey file, which is necessary for verifying proofs.
```APIDOC
## zKey - Export Verification Key
Export the verification key from a zkey file for proof verification.
### Method
GET (or equivalent function call)
### Endpoint
N/A (Function call within the library)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example (JavaScript)
```javascript
import * as snarkjs from "snarkjs";
import fs from "fs";
const vKey = await snarkjs.zKey.exportVerificationKey("circuit_final.zkey");
fs.writeFileSync("verification_key.json", JSON.stringify(vKey, null, 2));
```
### Request Example (CLI)
```bash
snarkjs zkey export verificationkey circuit_final.zkey verification_key.json
```
### Response
#### Success Response (200)
- **vKey** (object) - The exported verification key, typically saved to a JSON file.
#### Response Example
```json
{
"protocol": "groth16",
"curve": "bn128",
"vk_alpha_1": [
"...",
"..."
],
"beta_2": [
"...",
"..."
],
"gamma_2": [
"...",
"..."
],
"delta_2": [
"...",
"..."
],
"ic": [
[
"...",
"..."
]
]
}
```
```
--------------------------------
### Verify Groth16 Proof
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Use this command to verify a Groth16 proof.
```sh
snarkjs groth16 verify verification_key.json public.json proof.json
```
--------------------------------
### Export Solidity Verifier
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Export the verifier as a Solidity smart contract.
```sh
snarkjs zkey export solidityverifier circuit_final.zkey verifier.sol
```
--------------------------------
### zKey - Verify
Source: https://context7.com/iden3/snarkjs/llms.txt
Verifies the integrity and correctness of a zkey file against its corresponding R1CS and powers of tau.
```APIDOC
## zKey - Verify
Verify that a zkey file is valid and matches the circuit and powers of tau.
### Method
GET (or equivalent function call)
### Endpoint
N/A (Function call within the library)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example (JavaScript)
```javascript
import * as snarkjs from "snarkjs";
const isValid = await snarkjs.zKey.verifyFromR1cs(
"circuit.r1cs",
"pot14_final.ptau",
"circuit_final.zkey"
);
console.log("zKey valid:", isValid);
```
### Request Example (CLI)
```bash
snarkjs zkey verify r1cs circuit.r1cs pot14_final.ptau circuit_final.zkey
```
### Response
#### Success Response (200)
- **isValid** (boolean) - True if the zkey is valid, false otherwise.
#### Response Example
```json
{
"isValid": true
}
```
```
--------------------------------
### Calculate Witness for Circuit
Source: https://context7.com/iden3/snarkjs/llms.txt
Calculate the witness (all wire values) for a circuit given specific inputs. This is a prerequisite for generating proofs.
```javascript
import * as snarkjs from "snarkjs";
// Calculate witness from inputs
const input = { a: "3", b: "11" };
await snarkjs.wtns.calculate(input, "circuit.wasm", "witness.wtns");
// Verify witness against R1CS
const isValid = await snarkjs.wtns.check("circuit.r1cs", "witness.wtns");
console.log("Witness valid:", isValid);
```
```bash
# CLI equivalent
snarkjs wtns calculate circuit.wasm input.json witness.wtns
snarkjs wtns check circuit.r1cs witness.wtns
```
--------------------------------
### Contribute to Phase 2 Ceremony
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Adds a contribution to the zkey file for the phase 2 ceremony. You will be prompted for entropy.
```shell
snarkjs zkey contribute circuit_0000.zkey circuit_0001.zkey --name="1st Contributor Name" -v
```
--------------------------------
### Witness - Calculate
Source: https://context7.com/iden3/snarkjs/llms.txt
Calculates the witness (all wire values) for a given circuit and input, essential for proof generation.
```APIDOC
## Witness - Calculate
Calculate the witness (all wire values) for a circuit given specific inputs.
### Method
POST (or equivalent function call)
### Endpoint
N/A (Function call within the library)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example (JavaScript)
```javascript
import * as snarkjs from "snarkjs";
const input = { a: "3", b: "11" };
await snarkjs.wtns.calculate(input, "circuit.wasm", "witness.wtns");
const isValid = await snarkjs.wtns.check("circuit.r1cs", "witness.wtns");
console.log("Witness valid:", isValid);
```
### Request Example (CLI)
```bash
snarkjs wtns calculate circuit.wasm input.json witness.wtns
snarkjs wtns check circuit.r1cs witness.wtns
```
### Response
#### Success Response (200)
- **isValid** (boolean) - True if the witness is valid according to the R1CS, false otherwise.
#### Response Example
```json
{
"isValid": true
}
```
```
--------------------------------
### Provide Second Contribution to zkey
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Adds a second contribution to the zkey file, optionally providing entropy via a command-line flag.
```shell
snarkjs zkey contribute circuit_0001.zkey circuit_0002.zkey --name="Second contribution Name" -v -e="Another random entropy"
```
--------------------------------
### Verify PLONK Proof
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Use this command to verify a PLONK proof.
```sh
snarkjs plonk verify verification_key.json public.json proof.json
```
--------------------------------
### Groth16 - Generate Proof
Source: https://context7.com/iden3/snarkjs/llms.txt
Generates a Groth16 zero-knowledge proof using a pre-calculated witness and a valid zkey file.
```APIDOC
## Groth16 - Generate Proof
Generate a Groth16 zero-knowledge proof from a witness and zkey.
### Method
POST (or equivalent function call)
### Endpoint
N/A (Function call within the library)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example (JavaScript)
```javascript
import * as snarkjs from "snarkjs";
const { proof, publicSignals } = await snarkjs.groth16.prove(
"circuit_final.zkey",
"witness.wtns"
);
console.log("Proof:", JSON.stringify(proof, null, 2));
console.log("Public signals:", publicSignals);
```
### Request Example (CLI)
```bash
snarkjs groth16 prove circuit_final.zkey witness.wtns proof.json public.json
```
### Response
#### Success Response (200)
- **proof** (object) - Contains the proof components (pi_a, pi_b, pi_c).
- **publicSignals** (array) - Contains the public inputs and outputs of the circuit.
#### Response Example
```json
{
"proof": {
"pi_a": ["...", "..."],
"pi_b": [["...", "..."], ["...", "..."]],
"pi_c": ["...", "..."]
},
"publicSignals": ["...", "..."]
}
```
```
--------------------------------
### Calculate Witness with snarkjs
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Calculates the witness (all wire values) for a circuit using the generated WASM program and input file.
```shell
snarkjs wtns calculate circuit_js/circuit.wasm input.json witness.wtns
```
--------------------------------
### Generate Groth16 Proof
Source: https://context7.com/iden3/snarkjs/llms.txt
Generate a Groth16 zero-knowledge proof from a witness and zkey. This process requires the witness file and the final zkey.
```javascript
import * as snarkjs from "snarkjs";
// Generate proof from witness
const { proof, publicSignals } = await snarkjs.groth16.prove(
"circuit_final.zkey",
"witness.wtns"
);
console.log("Proof:", JSON.stringify(proof, null, 2));
console.log("Public signals:", publicSignals);
// Output: proof contains pi_a, pi_b, pi_c elliptic curve points
// Output: publicSignals contains the public inputs/outputs
```
```bash
# CLI equivalent
snarkjs groth16 prove circuit_final.zkey witness.wtns proof.json public.json
```
--------------------------------
### Block Plonk ZKEY File Header
Source: https://github.com/iden3/snarkjs/blob/master/doc/blockplonk_zkey_format.md
Details the initial header information for a Block Plonk zkey file, including magic bytes, version, and the number of sections.
```text
┏━━━━━━━━━━━━━┓
┃ 7A 6B 65 79 ┃ Magic "zkey"
┗━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━┓
┃ 01 00 00 00 ┃ Version 1
┗━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━┓
┃ 0A 00 00 00 ┃ Number of Sections
┗━━━━━━━━━━━━━┛
```
--------------------------------
### Copy SnarkJS Minified Build
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Copy the minified snarkjs build to the current directory for browser use.
```sh
cp node_modules/snarkjs/build/snarkjs.min.js .
```
--------------------------------
### Export Solidity Verifier Contract
Source: https://context7.com/iden3/snarkjs/llms.txt
Generates a Solidity smart contract for on-chain proof verification. This requires the final zkey file and template files for the desired circuit type (groth16, plonk, or fflonk).
```javascript
import * as snarkjs from "snarkjs";
import fs from "fs";
// Load templates
const templates = {
groth16: fs.readFileSync("node_modules/snarkjs/templates/verifier_groth16.sol.ejs", "utf8"),
plonk: fs.readFileSync("node_modules/snarkjs/templates/verifier_plonk.sol.ejs", "utf8"),
fflonk: fs.readFileSync("node_modules/snarkjs/templates/verifier_fflonk.sol.ejs", "utf8")
};
// Export Solidity verifier
const solidityCode = await snarkjs.zKey.exportSolidityVerifier(
"circuit_final.zkey",
templates
);
fs.writeFileSync("Verifier.sol", solidityCode);
```
```bash
# CLI equivalent
snarkjs zkey export solidityverifier circuit_final.zkey verifier.sol
```
--------------------------------
### Single-threaded Proof Calculation
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Use this option to switch to single-threaded proof calculation when multithreading is unavailable.
```javascript
const result = await snarkjs.groth16.prove(zkey_final, wtns, undefined, {singleThread: true});
```
--------------------------------
### Apply Random Beacon to zkey
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Applies a random beacon contribution to finalize the zkey file after all other contributions are made.
```shell
snarkjs zkey beacon circuit_0003.zkey circuit_final.zkey 0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f 10 -n="Final Beacon phase2"
```
--------------------------------
### Export Verification Key from zKey
Source: https://context7.com/iden3/snarkjs/llms.txt
Export the verification key from a zkey file. This key is essential for verifying proofs generated with the corresponding proving key.
```javascript
import * as snarkjs from "snarkjs";
import fs from "fs";
// Export verification key
const vKey = await snarkjs.zKey.exportVerificationKey("circuit_final.zkey");
fs.writeFileSync("verification_key.json", JSON.stringify(vKey, null, 2));
```
```bash
# CLI equivalent
snarkjs zkey export verificationkey circuit_final.zkey verification_key.json
```
--------------------------------
### Export Verification Key
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Exports the verification key from the final zkey file into a JSON file.
```shell
snarkjs zkey export verificationkey circuit_final.zkey verification_key.json
```
--------------------------------
### Control Initial Memory Allocation
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Pass this option to minimize memory allocation on the web during witness calculation.
```javascript
await wtnsCalculate(input, wasmFile, wtns, {memorySize: 0});
```
--------------------------------
### Check Witness Compliance
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Verifies if the generated witness conforms to the circuit's R1CS file.
```shell
snarkjs wtns check circuit.r1cs witness.wtns
```
--------------------------------
### Verify Final zkey
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Performs a final verification of the zkey file before exporting the verification key.
```shell
snarkjs zkey verify circuit.r1cs pot14_final.ptau circuit_final.zkey
```
--------------------------------
### Groth16 - Verify Proof
Source: https://context7.com/iden3/snarkjs/llms.txt
Verifies a Groth16 proof using the verification key, public signals, and the proof itself.
```APIDOC
## Groth16 - Verify Proof
Verify a Groth16 proof against the verification key and public signals.
### Method
GET (or equivalent function call)
### Endpoint
N/A (Function call within the library)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example (JavaScript)
```javascript
import * as snarkjs from "snarkjs";
import fs from "fs";
const vKey = JSON.parse(fs.readFileSync("verification_key.json"));
const proof = JSON.parse(fs.readFileSync("proof.json"));
const publicSignals = JSON.parse(fs.readFileSync("public.json"));
const isValid = await snarkjs.groth16.verify(vKey, publicSignals, proof);
if (isValid) {
console.log("Proof is valid!");
} else {
console.log("Invalid proof");
}
```
### Request Example (CLI)
```bash
snarkjs groth16 verify verification_key.json public.json proof.json
```
### Response
#### Success Response (200)
- **isValid** (boolean) - True if the proof is valid, false otherwise.
#### Response Example
```json
{
"isValid": true
}
```
```
--------------------------------
### Print Circuit Constraints
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Use `snarkjs r1cs print` to view the detailed constraints of the compiled circuit, which is useful for debugging and understanding the circuit's logic.
```sh
snarkjs r1cs print circuit.r1cs circuit.sym
```
--------------------------------
### Verify Powers of Tau File
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Use the `powersoftau verify` command to check the integrity of a powers of tau file. This is a crucial step before proceeding with circuit creation.
```sh
snarkjs powersoftau verify pot14_final.ptau
```
--------------------------------
### Compile Circom Circuit
Source: https://github.com/iden3/snarkjs/blob/master/README.md
Compile a Circom circuit using the `circom` command. This generates various output files including R1CS, WASM, C++ witness calculator code, and symbol files.
```sh
circom --r1cs --wasm --c --sym --inspect circuit.circom
```