R1CS Binary File Parser

0.0.48 · active · verified Tue Apr 21

r1csfile is a JavaScript library designed for parsing and interpreting R1CS (Rank-1 Constraint System) binary files. These files are commonly generated by tools like Circom for zero-knowledge proof circuits, containing the constraints and witness information essential for SNARK proofs. The library provides programmatic access to read the structured data within an R1CS file, which typically includes details about the circuit's header, number of variables, number of constraints, and the wire mapping. As of version 0.0.48, the package is under active maintenance, primarily focusing on dependency updates to ensure security and compatibility. It exhibits a stable but infrequent release cadence, prioritizing robustness within the zero-knowledge ecosystem. Its key differentiator is its specific utility within the Iden3 ecosystem, offering a direct and reliable method to interact with R1CS files, which is crucial for building and integrating tools that depend on SNARK-based proof generation and verification without needing to recompile circuits repeatedly. It serves as a foundational component for developers working with these advanced cryptographic systems.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to asynchronously read and parse an R1CS binary file using `readR1cs`, including basic error handling for file access and parsing issues. It logs key metadata from the parsed R1CS object, such as variable and constraint counts.

import { readR1cs } from 'r1csfile';
import { promises as fs } from 'fs';

async function parseR1CSFile(filePath: string) {
  try {
    // Ensure the R1CS file exists before attempting to read
    await fs.access(filePath);
    console.log(`Attempting to read R1CS file: ${filePath}`);

    const r1cs = await readR1cs(filePath);

    console.log('Successfully parsed R1CS file.');
    console.log(`Number of wires: ${r1cs.nVars}`);
    console.log(`Number of constraints: ${r1cs.nConstraints}`);
    console.log('R1CS structure (truncated):', {
      header: r1cs.header,
      nVars: r1cs.nVars,
      nConstraints: r1cs.nConstraints,
      // Further properties like `constraints` or `witness` can be very large.
      // For demonstration, we'll just log header info.
    });

    // Example: Accessing specific header fields
    if (r1cs.header.q instanceof Uint8Array) {
      console.log(`Field prime (q): 0x${Buffer.from(r1cs.header.q).toString('hex')}`);
    } else {
      console.log(`Field prime (q): ${r1cs.header.q}`);
    }

  } catch (error) {
    if (error instanceof Error) {
      console.error(`Error parsing R1CS file ${filePath}: ${error.message}`);
      if ((error as any).code === 'ENOENT') {
        console.error('File not found. Please ensure the path is correct.');
      }
    } else {
      console.error(`An unknown error occurred: ${error}`);
    }
    process.exit(1);
  }
}

// To run this, you would need an actual .r1cs file.
// For demonstration, let's assume 'example.r1cs' exists in the current directory.
// Replace 'example.r1cs' with your actual file path.
// If you don't have one, this will output a 'File not found' error.
parseR1CSFile('example.r1cs');

view raw JSON →