UNPKG

lambdaworks-groth16-ts

Version:

TypeScript bindings for LambdaWorks Groth16 SNARK prover

124 lines (116 loc) 4.11 kB
import { Groth16Prover } from "./prover"; import { Groth16Verifier } from "./verifier"; import { TrustedSetupManager } from "./setup"; import { FieldOperations } from "./field"; import { CircuitProcessor } from "./circuit"; export class LambdaWorksGroth16 { constructor(wasmInstance) { this._prover = null; this._verifier = null; this._setupManager = null; this._fieldOps = null; this._circuitProcessor = null; this.wasmInstance = wasmInstance; } /** * Initialize the LambdaWorks Groth16 library */ static async init(wasmPath) { let wasm; if (typeof window !== 'undefined') { // Browser environment - fix the WASM filename const wasmUrl = wasmPath || './pkg/lambdaworks_groth16_wasm_bg.wasm'; const wasmModule = await import('../pkg/lambdaworks_groth16_wasm.js'); // ✅ Fixed await wasmModule.default(wasmUrl); wasm = wasmModule; } else { // Node.js environment - fix filename and Node.js imports const wasmModule = await import('../pkg/lambdaworks_groth16_wasm.js'); // ✅ Fixed // Fix Node.js imports - use require instead of dynamic import const fs = require('fs'); const path = require('path'); const wasmPath_ = wasmPath || path.join(__dirname, '../pkg/lambdaworks_groth16_wasm_bg.wasm'); // ✅ Fixed filename const wasmBuffer = fs.readFileSync(wasmPath_); await wasmModule.default(wasmBuffer); wasm = wasmModule; } return new LambdaWorksGroth16(wasm); } /** * Get the Groth16 prover instance */ get prover() { if (!this._prover) { this._prover = new Groth16Prover(this.wasmInstance); } return this._prover; } /** * Get the Groth16 verifier instance */ get verifier() { if (!this._verifier) { this._verifier = new Groth16Verifier(this.wasmInstance); } return this._verifier; } /** * Get the trusted setup manager instance */ get setupManager() { if (!this._setupManager) { this._setupManager = new TrustedSetupManager(this.wasmInstance); } return this._setupManager; } /** * Get the field operations instance */ get fieldOps() { if (!this._fieldOps) { this._fieldOps = new FieldOperations(this.wasmInstance); } return this._fieldOps; } /** * Get the circuit processor instance */ get circuitProcessor() { if (!this._circuitProcessor) { this._circuitProcessor = new CircuitProcessor(this.wasmInstance); } return this._circuitProcessor; } } // Export main class and types export default LambdaWorksGroth16; export * from './types'; // Usage example: /* import LambdaWorksGroth16 from 'lambdaworks-groth16-ts'; async function example() { // Initialize the library const groth16 = await LambdaWorksGroth16.init(); // Load circuit from Circom const r1csData = await fetch('./circuit.r1cs').then(r => r.arrayBuffer()); const wasmData = await fetch('./circuit.wasm').then(r => r.arrayBuffer()); const circuit = await groth16.circuitProcessor.loadFromCircom( new Uint8Array(r1csData), new Uint8Array(wasmData) ); // Generate trusted setup (unsafe - for testing only) const setup = groth16.setupManager.generateUnsafeSetup(circuit); // Generate proof const inputs = { x: "123", y: "456" }; const proof = await groth16.prover.proveWithInputs(circuit, setup.provingKey, inputs); // Verify proof const witness = groth16.circuitProcessor.computeWitness(circuit, inputs); const isValid = groth16.verifier.verify(setup.verifyingKey, witness.publicInputs, proof); console.log('Proof valid:', isValid); // Export for Solidity const solidityProof = groth16.prover.exportProof(proof, 'solidity'); console.log('Solidity proof:', solidityProof); } */ //# sourceMappingURL=index.js.map