@aintivirus-ai/snarkjs-to-solana
v1.0.3
Published
A comprehensive toolkit for converting snarkjs artifacts to Solana-compatible formats. This package provides both CLI tools and programmatic APIs for converting zero-knowledge proofs and verification keys from snarkjs to formats that work with Solana's gr
Maintainers
Readme
@aintivirus-ai/snarkjs-to-solana
A comprehensive toolkit for converting snarkjs artifacts to Solana-compatible formats. This package provides both CLI tools and programmatic APIs for converting zero-knowledge proofs and verification keys from snarkjs to formats that work with Solana's groth16 verifier.
Features
- Triple-target WASM compilation: Optimized for web, bundler, and Node.js/CommonJS environments
- CLI interface: Convert verification keys directly from the command line
- Programmatic API: Integrate proof conversion into your applications
- Automatic environment detection: Seamlessly works in Node.js, browsers, and bundlers
- TypeScript support: Full type definitions included
- CommonJS and ESM compatibility: Works with both module systems
Installation
Global Installation (Recommended for CLI usage)
npm install -g @aintivirus-ai/snarkjs-to-solanaLocal Installation
npm install @aintivirus-ai/snarkjs-to-solanaIf installed locally, you can run CLI commands using npx:
npx snarkjs-to-solana verification-key-to-rust verification_key.jsonCLI Usage
Convert Verification Key to Rust
Convert a snarkjs verification key JSON file to a Rust file compatible with Solana's groth16 verifier.
Usage:
snarkjs-to-solana verification-key-to-rust <verification-key-path> [options]Arguments:
<verification-key-path>: Path to the verification key JSON file generated by snarkjs
Options:
-o, --output <directory>: Output directory (defaults to current directory)-h, --help: Display help for the command
Examples:
# Convert verification key to current directory
snarkjs-to-solana verification-key-to-rust verification_key.json
# Convert verification key to specific directory
snarkjs-to-solana verification-key-to-rust verification_key.json --output ./rust_files
# Short form
snarkjs-to-solana verification-key-to-rust verification_key.json -o ./rust_files
# Show help
snarkjs-to-solana verification-key-to-rust --helpOutput:
The command generates a verifying_key.rs file containing:
- A Rust struct compatible with
groth16_solana::groth16::Groth16Verifyingkey - All verification key parameters properly formatted for Solana
- Ready-to-use constants for your Solana verification program
Programmatic Usage
Node.js / CommonJS Environment
// Works with both CommonJS (require) and ESM (import) in Node.js
const {
getSolanaCompatibleProof,
} = require("@aintivirus-ai/snarkjs-to-solana");
// OR
import { getSolanaCompatibleProof } from "@aintivirus-ai/snarkjs-to-solana";
// This automatically uses the Node.js-targeted WASM package (CommonJS compatible)
const result = await getSolanaCompatibleProof(proof);Modern Bundler Environment (Webpack, Vite, etc.)
// In bundler environments like Webpack, Vite, Rollup, etc.
import { getSolanaCompatibleProof } from "@aintivirus-ai/snarkjs-to-solana";
// This automatically uses the bundler-targeted WASM package
const result = await getSolanaCompatibleProof(proof);Browser Environment
// In a browser environment, the package automatically uses the web-targeted WASM
import { getSolanaCompatibleProof } from "@aintivirus-ai/snarkjs-to-solana";
// This automatically uses the web-targeted WASM package
const result = await getSolanaCompatibleProof(proof);Manual Target Selection
// If you need to explicitly control which target to use:
// For bundler environments (Node.js, Webpack, Vite, etc.)
import { getSolanaCompatibleProof } from "@aintivirus-ai/snarkjs-to-solana/dist/index.js";
// For web environments (browsers)
import { getSolanaCompatibleProof } from "@aintivirus-ai/snarkjs-to-solana/dist/index.browser.js";Complete Example
Here's a full example showing how to generate proofs with snarkjs and convert them for Solana:
import * as snarkjs from "snarkjs";
import { getSolanaCompatibleProof } from "@aintivirus-ai/snarkjs-to-solana";
// 1. Generate proof using snarkjs
const { proof: circuitProof, publicSignals } = await snarkjs.groth16.fullProve(
circuitInputs,
"../circom/ethDepositProof_js/ethDepositProof.wasm",
"../circom/ethDepositProof_js/1_0000.zkey"
);
// 2. Convert proof to Solana-compatible format
const { proofA, proofB, proofC } = await getSolanaCompatibleProof(circuitProof);
// 3. Use proofA, proofB, proofC in your Solana on-chain instruction
// These can now be passed to your Solana program for verificationIntegration with snarkjs Workflow
Generate your circuit and verification key with snarkjs:
snarkjs groth16 setup circuit.r1cs powersOfTau28_hez_final_10.ptau circuit_0000.zkey snarkjs zkey export verificationkey circuit_0000.zkey verification_key.jsonConvert verification key to Rust format:
snarkjs-to-solana verification-key-to-rust verification_key.jsonGenerate and convert proofs in your application:
// Generate proof with snarkjs const { proof: circuitProof, publicSignals } = await snarkjs.groth16.fullProve( circuitInputs, "circuit.wasm", "circuit.zkey" ); // Convert for Solana const { proofA, proofB, proofC } = await getSolanaCompatibleProof( circuitProof );Use in your Solana program:
use groth16_solana::groth16::Groth16Verifyingkey; // Include the generated verification key file mod verifying_key; use verifying_key::VERIFYINGKEY; // Verify the proof on-chain groth16_solana::groth16::verify(&VERIFYINGKEY, &proof, &public_inputs)?;Call Solana on-chain instruction with converted proofs: The
proofA,proofB, andproofCvalues from step 3 are passed to your Solana program instruction for on-chain verification.
How It Works
The package automatically detects the environment and loads the appropriate WASM target:
- Bundler Target: Optimized for Node.js and bundler environments (Webpack, Vite, Rollup, etc.)
- Web Target: Optimized for direct browser usage with proper ES6 module support
The conditional exports in package.json ensure that:
- Modern bundlers get the bundler-optimized version
- Browsers get the web-optimized version
- The appropriate WASM files are loaded dynamically
Build Process
The package uses a dual-target build system:
- WASM Compilation: Rust code is compiled to two WASM targets using
wasm-pack - Unified Package: Both targets are combined into a single package structure
- Conditional Exports: Package.json exports map environments to appropriate entry points
- TypeScript Compilation: TypeScript code is compiled to JavaScript for both targets
Error Handling
The CLI provides clear error messages for common issues:
- File not found: If the verification key file doesn't exist
- Invalid JSON: If the verification key file is not valid JSON
- Parse errors: If the verification key format is not compatible with ffjavascript
- Permission errors: If the output directory cannot be written to
Troubleshooting
CommonJS/ESM Compatibility Issues
If you encounter module loading errors in Node.js:
// Try using require for CommonJS environments
const {
getSolanaCompatibleProof,
} = require("@aintivirus-ai/snarkjs-to-solana");
// Or dynamic import for ESM environments
const { getSolanaCompatibleProof } = await import(
"@aintivirus-ai/snarkjs-to-solana"
);Command not found
If you get "command not found" error:
- Ensure the package is installed globally:
npm install -g @aintivirus-ai/snarkjs-to-solana - Or use with npx:
npx @aintivirus-ai/snarkjs-to-solana
Permission denied
If you get permission errors:
- Check that you have write permissions to the output directory
- Try specifying a different output directory with
-o
Invalid verification key format
Ensure your verification key was generated by snarkjs and follows the expected format:
{
"vk_alpha_1": [...],
"vk_beta_2": [...],
"vk_gamma_2": [...],
"vk_delta_2": [...],
"IC": [...]
}Package Structure
proof_utils/
├── pkg/
│ ├── web/ # Web-targeted WASM files
│ ├── bundler/ # Bundler-targeted WASM files
│ ├── index.js # Main entry point (bundler)
│ ├── index-web.js # Web entry point
│ └── package.json # Unified package configuration
├── src/
│ ├── index.ts # Main TypeScript entry (bundler)
│ └── index.browser.ts # Browser TypeScript entry (web)
└── dist/
├── index.js # Compiled bundler version
└── index.browser.js # Compiled web versionAPI Reference
getSolanaCompatibleProof(proof: any): Promise<object>
Converts a snarkjs proof to Solana-compatible format.
Parameters:
proof: A proof object generated by snarkjs
Returns:
- Promise resolving to an object with
proofA,proofB, andproofCproperties
Dependencies
ffjavascript: For handling finite field arithmetic and proof parsingcommander: For CLI argument parsing- Custom WASM module for proof conversion
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Keywords
- convert-snarkjs-proof
- snarkjs-proof-convert
- groth16-proof-convert
- solana-verifier
- verification-key-to-rust
- cryptography
- zero-knowledge
- zk-proofs
- blockchain
