zkPyTorch: A Hierarchical Optimized Compiler for Zero-Knowledge Machine Learning

Tiancheng Xie, Tao Lu, Zhiyong Fang, Siqi Wang, Zhenfei Zhang, Yongzheng Jia, Dawn Song, Jiaheng ZhangIACR ePrint 2025

Abstract

As machine learning (ML) models are increasingly integrated into sensitive domains, the need for verifiable and privacy-preserving computation grows. Zero-Knowledge Machine Learning (ZKML) allows a prover to demonstrate that a specific model produced a given output without revealing the model's internal weights. However, the complexity of translating high-level ML code into cryptographic circuits remains a significant barrier. This paper presents zkPyTorch, a hierarchical optimized compiler designed to bridge this gap. By leveraging the ONNX format, ZKP-friendly quantization, and multi-level circuit optimizations, zkPyTorch enables the conversion of complex models, including Llama-3, into efficient ZK circuits. Experimental results demonstrate proof generation for VGG-16 in 2.2 seconds and successful verification of the 8B-parameter Llama-3 model.

Introduction

The deployment of AI in high-stakes fields like healthcare and finance has created a "transparency-privacy" paradox. While regulators and users demand verification of AI outputs, providers must protect proprietary model weights and sensitive training data. Zero-Knowledge Machine Learning (ZKML) offers a mathematical solution: generating a proof of correct execution without disclosing internal parameters.

Despite its potential, ZKML is notoriously difficult to implement. It requires mapping standard ML operations, typically defined in floating-point arithmetic, to complex cryptographic circuits over finite fields. Existing tools often struggle with complex architectures or require manual circuit design. zkPyTorch addresses these challenges by providing an automated, optimized pipeline for converting PyTorch models into verifiable circuits.

Methods

The zkPyTorch architecture consists of three primary stages designed to automate and optimize the ZKML pipeline.

The zkPyTorch architecture overview, showing the flow from an original ML model through preprocessing, quantization, and optimization to the ZKP engine.
  1. ZKP Preprocessor: Utilizing the ONNX format, zkPyTorch represents models as Directed Acyclic Graphs (DAGs). This representation allows the compiler to handle complex architectures like Transformers and automatically inject necessary "auxiliary information," such as remainders for division or lookup tables for non-linear functions (e.g., Softmax).
  2. ZKP-friendly Quantization: Standard ML models use floating-point numbers, which are incompatible with the finite field arithmetic of ZKPs. zkPyTorch introduces a novel integer-based quantization strategy. Unlike previous methods that relied on large bit-widths and slow fields (e.g., BN254), this strategy enables efficient execution in smaller finite fields like M61 while maintaining high accuracy.
Comparison of quantization methods. zkPyTorch uses ZKP-friendly static quantization to map operations to integer-based table lookups and matrix multiplications suitable for finite fields.
  1. Hierarchical Circuit Optimizer: Optimization is performed at three levels:
  2. Model Level: For Large Language Models (LLMs), the compiler decouples sequential token generation. Since the verifier knows the final output, it can verify a batch of tokens simultaneously, significantly reducing overhead.
  3. Operation Level: Specialized cryptographic patterns, such as Fast Fourier Transforms (FFT) for convolutions, are embedded to speed up specific operations.
  4. Circuit Level: Proof generation is parallelized across multiple CPU cores to enhance performance.

Key Insights and Ideas

A fundamental insight of this work is that verification is distinct from computation. In ZKML, the prover's task is to prove the correctness of a result, not just calculate it. This distinction enables optimizations like batch verification for LLMs. Furthermore, the authors demonstrate that designing quantization specifically for ZKP constraints allows the use of smaller finite fields, providing a massive performance boost without a significant accuracy penalty. By leveraging ONNX, zkPyTorch also establishes a framework-agnostic path for ZKML support.

Key Results

The effectiveness of zkPyTorch was evaluated using the CIFAR-10 dataset for vision models and the Llama-3 (8B) model for language tasks.

  • Efficiency and Scalability: For the VGG-16 model, zkPyTorch generates a proof in just 2.2 seconds per image on a single core. Notably, it successfully handled the 8-billion parameter Llama-3 model, generating proofs at a rate of 150 seconds per token—a significant milestone for models of this scale.
  • Accuracy Preservation: The quantization method is highly effective. For VGG-16, accuracy only decreased slightly from 94.13% to 94.11%. The quantized Llama-3 model achieved a 99.32% cosine similarity compared to the original floating-point version.

Conclusion

zkPyTorch represents a significant step toward making ZKML accessible for state-of-the-art AI models. While 150 seconds per token for Llama-3 is a breakthrough, it is not yet suitable for real-time applications. Future work will focus on further parallelization and the use of hardware accelerators like GPUs or FPGAs to reduce latency. Additionally, expanding the library of ZKP-friendly primitive operations will support a wider range of experimental ML architectures.

Summary updated

BibTeX
@article{xie2025zkpytorch,
  author = {Tiancheng Xie and Tao Lu and Zhiyong Fang and Siqi Wang and Zhenfei Zhang and Yongzheng Jia and Dawn Song and Jiaheng Zhang},
  title = {{zkPyTorch: A Hierarchical Optimized Compiler for Zero-Knowledge Machine Learning}},
  journal = {IACR Cryptol. ePrint Arch.},
  year = {2025},
}
Figure