SlideShare a Scribd company logo
CirC: Compiling Programs to Circuits
Common Compiler Infrastructure for: SNARKs, SMT solvers, etc.
Alex Ozdemir, Fraser Brown, Riad Wahby
R1CS
?
Image Credits: ZK Podcast
Idea: Zero-Knowledge Bounties?
Supposedly, 𝑐 = 𝑐 + 2𝑎𝑏
(OpenSSL, CVE-2014-3570)
Codebase Owner
Bug Finder
“Inputs 𝒘 cause ”
Idea: Zero-Knowledge Bounties?
Supposedly, 𝑐 = 𝑐 + 2𝑎𝑏
(OpenSSL, CVE-2014-3570)
Codebase Owner
Bug Finder
𝜋
“I know inputs that cause ”
Zero-Knowledge: Challenges
• For ZK-proofs, you need a system of arithmetic constraints (R1CS)
1. Need compilers from high level languages to R1CS with
• Small output (efficiency)
• Accurate output (correctness)
Pequin (C)
[SBVBPW’13]
[BFRSBW’13]
[WSRBW’15]
ZoKrates
[EbeTai’18]
Zinc
circom
xJSnark
[KosPapShi’18]
PySNARK (~py)
Leo
Snarky
Cairo
R1CS
A (General) Compilation Problem
High-level languages:
• stateful
• control-flow
• uniform
Circuits:
• state-free
• non-uniform
• non-deterministic
𝑥0
𝑥1
𝑥2
𝑤0
𝑤1
Compiler
“circuits” also used in:
• SMT solving/verification
• Optimization (ILP)
• Multiparty computation
Our Approach: Common Infrastructure
Common Infrastructure for Compiling to
(Existentially Quantified) Circuits
CirC
ZoKrates
SMT Solver
Proof System
ILP?
MPC?
Design of CirC
Design Overview
C
Frontend
Optimizer
ZoKrates
Frontend
SMT
Backend
R1CS
Backend
SMT
Solver
Proof
System
a.circ
a.smt
a.c
CirC Front-Ends: Extensibility
C
Frontend
Optimizer
ZoKrates
Frontend
SMT
Backend
R1CS
Backend
Extensibility Case Study: ZoKrates
ZoKrates: Language designed to be compiled to R1CS (2018).
Compiler Reference
Lines of Code ~28,000
Development Time 3 years
Contributors 36
Output Size good
Extensibility Case Study: ZoKrates
ZoKrates: Language designed to be compiled to R1CS (2018).
Compiler Reference CirC
Lines of Code ~28,000 ~700
Development Time 3 years 1 week
Contributors 36 1
Output Size good better
ZoKrates Performance Comparison
What Does a Front-End Look Like?
A frontend is written as an
interpreter…
… with state management
abstracted out!
Automatic:
• Variables (mutation, scope, etc.)
• Data-dependent arrays
• Control Flow
CirC-IR
(SMT-like)
Python
Interpreter Snippet
Benefits of Common Infrastructure
• Easy extension to new
languages/circuits
• Built a better ZoKrates compiler in
≈ 40 × less code
• common optimizations
• Constant folding
• Memory representation
• Mix & match targets
• Automated zero-knowledge proof-
of-bug
• SMT-assisted optimization of
arithmetic circuits
CirC’s Core: Language-Agnostic
Compilation
Applications & Techniques
CirC’s Core: Language Agnostic
Compilation & Optimzation
C
Frontend
Optimizer
ZoKrates
Frontend
SMT
Backend
R1CS
Backend
Compiling to Circuits: Techniques
Challenge Approach
Variable Mutation Variable Versioning
x = y
x = x * y
x1 = y
x2 = x1 * y
Compiling to Circuits : Techniques
Challenge Approach
Variable Mutation Variable Versioning
Memory Use SMT Arrays
x[y] = z
x2 = x1[z @ y]
Compiling to Circuits : Techniques
Challenge Approach
Variable Mutation Variable Versioning
Memory Use SMT Arrays
Loops Unrolling
x = 5
for x in {0,1}:
x *= x
x1 = 5
x2 = x1 * x1
x3 = x2 * x2
Compiling to Circuits : Techniques
Challenge Approach
Variable Mutation Variable Versioning
Memory Use SMT Arrays
Loops Unrolling
Branching Guards
x = 5
if y > 0:
x *= x
x1 = 5
x2 = y > 0
? x1 * x1
: x1
Optimizations: Basic
• Constant folding
• 5 + 6 → 11
• 𝑥 ≪ 2 → 𝑥 2: ∥ [0,0]
• Operator flattening
• 𝑥 + 𝑦 + 𝑧 → 𝑥 + 𝑦 + 𝑧
• Substitution
• Peephole
• c & t | ~c & f ->
ITE(c,t,f)
Optimizations: Array Elimination
• Oblivious Arrays
• Arrays accessed at constant
indices
• Replaced with distinct terms
• Like scalar replacement
x = [y,z];
x[0] = x[1]+x[0];
y = x[0];
x01 = y1; x11 = z;
x02 = x01 + x11;
y2 = x02;
Optimizations: Routing-Based Memory
• Replace memory operations (array encoded) with the following…
access 0 load addr val
access 1 load addr val
access 2 load addr val
access 3 load addr val
access 4 load addr val
Program Order
access id load addr val
access id load addr val
access id load addr val
access id load addr val
access id load addr val
Address Order
• Check
order
• Check read-
over-write
Waksman
routing network
(existential
switch settings)
[WSRBW’15] [BCTV’14] [BCGTV’13] [BEGKN’94]
CirC Back-Ends: Applications
C
Frontend
Optimizer
ZoKrates
Frontend
SMT
Backend
R1CS
Backend
Backends
SMT (Z3)
• Support for
• Bit-vectors
• Booleans
• Floating-point
• Arrays
• (all but finite fields)
R1CS (Finite Field Equations)
• Support for
• Bit-vectors (that fit in the field)
• Booleans
• Finite Fields
• Some arrays (via memory-
checking)
Common Optimization: Constant Folding
• Define a hash 𝐻.
• Assert ∃𝑥. 𝐻 𝑥 0. . 8 = 0
• Vary:
• Length of 𝑥
• Constant folding pass
• Measure
• SMT solver time
• R1CS constraint count
e.g. 5 + 6 → 11
Common Optimization: Array Elimination
• Define a program:
• Fills an array with non-
deterministic values
• Computes the sum
• Asserts a non-zero sum
• Vary:
• array size
• array elimination pass
Common Optimization: Array Granularity
• Program:
• Represents permutations on
{0,1, . . , 𝑛} with arrays
• Applies a sequence of
permutations (via indexing)
• Vary:
• Permutation size
• % permutations fused into a single
array
Stack
𝐴0
𝐴1
𝐴2
𝐴0
𝐴1
𝐴2
vs.
coarse fine
Cross-Over Applications
Cross-over: SMT-assisted Compilation
Frontend
SMT
Backend
R1CS
Backend
Use SMT!
How many times can the loop run?
Cross-Over: Proof-of-Bug
1. Compile program & assertion
to IR
2. Lower to SMT
3. Find violation with solver
4. Lower to R1CS
5. Prove knowledge of violation
Openssl’s 𝑐 = 𝑐 + 2𝑎𝑏
(𝑐 in 3 limbs)
CirC: Compilation Infrastructure for Circuits
Different circuits can share
compiler infrastructure
Benefits:
• Easy extension
• Shared optimizations
• Cross-over opportunities
Future Directions:
• More languages, more targets
CirC
ZoKrates
SMT Solver
Proof System
ILP?
MPC?
https://ia.cr/2020/1586
Backup Slides Follow
OpenSSL Proof-of-Bug Details
Wrapper function & assertion Visualization of macro
Comparison against Pequin
• Pequin
• State-of-the-art C-to-R1CS
compiler

More Related Content

PDF
64-bit Android
PDF
Pythonによるカスタム可能な高位設計技術 (Design Solution Forum 2016@新横浜)
PDF
助教が吼える! 各界の若手研究者大集合「ハードウェアはやわらかい」
PPTX
ONNC - 0.9.1 release
PDF
ゆるふわコンピュータ (IPSJ-ONE2017)
PDF
maXbox Starter 39 GEO Maps Tutorial
PDF
Improving Performance of a WebKit Port MIPS Platform (ELC 2014)
PPSX
Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14
64-bit Android
Pythonによるカスタム可能な高位設計技術 (Design Solution Forum 2016@新横浜)
助教が吼える! 各界の若手研究者大集合「ハードウェアはやわらかい」
ONNC - 0.9.1 release
ゆるふわコンピュータ (IPSJ-ONE2017)
maXbox Starter 39 GEO Maps Tutorial
Improving Performance of a WebKit Port MIPS Platform (ELC 2014)
Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14

What's hot (20)

PPSX
Dx11 performancereloaded
PDF
Android Developer Days: Increasing performance of big arrays processing on An...
PDF
Big Data for Mobile
PDF
Cache aware hybrid sorter
PPTX
Java performance jit
PDF
Juan josefumeroarray14
PDF
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
PDF
Automatic Generation of Compact Alphanumeric Shellcodes for x86
DOC
VLSI Anna University Practical Examination
ODP
Trash Robotic Router Platform - David Melendez - Codemotion Rome 2015
PPT
Lec12 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Ad...
PPT
EMBEDDED SYSTEMS 4&5
PPTX
JVM Memory Model - Yoav Abrahami, Wix
PDF
Megamodeling of Complex, Distributed, Heterogeneous CPS Systems
PPTX
[Sitcon2018] Analysis and Improvement of IOTA PoW Implementation
PDF
PDF
Efficient Bytecode Analysis: Linespeed Shellcode Detection
PDF
FPGA design with CλaSH
PDF
Java lejos-multithreading
DOC
FINISHED_CODE
Dx11 performancereloaded
Android Developer Days: Increasing performance of big arrays processing on An...
Big Data for Mobile
Cache aware hybrid sorter
Java performance jit
Juan josefumeroarray14
LAS16-501: Introduction to LLVM - Projects, Components, Integration, Internals
Automatic Generation of Compact Alphanumeric Shellcodes for x86
VLSI Anna University Practical Examination
Trash Robotic Router Platform - David Melendez - Codemotion Rome 2015
Lec12 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Ad...
EMBEDDED SYSTEMS 4&5
JVM Memory Model - Yoav Abrahami, Wix
Megamodeling of Complex, Distributed, Heterogeneous CPS Systems
[Sitcon2018] Analysis and Improvement of IOTA PoW Implementation
Efficient Bytecode Analysis: Linespeed Shellcode Detection
FPGA design with CλaSH
Java lejos-multithreading
FINISHED_CODE
Ad

Similar to zkStudyClub: CirC and Compiling Programs to Circuits (20)

PPTX
Java Jit. Compilation and optimization by Andrey Kovalenko
PDF
Vectorization in ATLAS
PPTX
CA-Lec4-RISCV-Instructions-1aaaaaaaaaa.pptx
PPT
Stream ciphers presentation
PDF
Unit-4_ESE.pdf
PPTX
Dot matrix display design using fpga
PDF
DEF CON 23 - Rodringo Almeida - embedded system design from electronics
PPTX
Tech Days 2015: Embedded Product Update
PDF
2020 icldla-updated
PPT
Nbsingh csir-ceeri-semiconductor-activities
PPT
ch11_031102.ppt
PPTX
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
PPTX
EMBEDDED SYSTEM BASICS
PDF
S12075-GPU-Accelerated-Video-Encoding.pdf
PPTX
Analog vs digital integrated circuit design
PDF
Handling inline assembly in Clang and LLVM
PDF
Greg Hogan – To Petascale and Beyond- Apache Flink in the Clouds
PPTX
Optimizing the Graphics Pipeline with Compute, GDC 2016
PDF
design-compiler.pdf
PDF
Дмитрий Вовк: Векторизация кода под мобильные платформы
Java Jit. Compilation and optimization by Andrey Kovalenko
Vectorization in ATLAS
CA-Lec4-RISCV-Instructions-1aaaaaaaaaa.pptx
Stream ciphers presentation
Unit-4_ESE.pdf
Dot matrix display design using fpga
DEF CON 23 - Rodringo Almeida - embedded system design from electronics
Tech Days 2015: Embedded Product Update
2020 icldla-updated
Nbsingh csir-ceeri-semiconductor-activities
ch11_031102.ppt
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
EMBEDDED SYSTEM BASICS
S12075-GPU-Accelerated-Video-Encoding.pdf
Analog vs digital integrated circuit design
Handling inline assembly in Clang and LLVM
Greg Hogan – To Petascale and Beyond- Apache Flink in the Clouds
Optimizing the Graphics Pipeline with Compute, GDC 2016
design-compiler.pdf
Дмитрий Вовк: Векторизация кода под мобильные платформы
Ad

More from Alex Pruden (17)

PDF
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
PDF
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
PPTX
zkStudyClub - zkSaaS (Sruthi Sekar, UCB)
PDF
zkStudyClub - Lasso/Jolt (Justin Thaler, GWU/a16z)
PDF
zkStudyClub - Improving performance of non-native arithmetic in SNARKs (Ivo K...
PDF
zkStudyClub - ProtoStar (Binyi Chen & Benedikt Bünz, Espresso Systems)
PDF
zkStudyClub - cqlin: Efficient linear operations on KZG commitments
PDF
ZK Study Club: Supernova (Srinath Setty - MS Research)
PDF
Eos - Efficient Private Delegation of zkSNARK provers
PDF
zkStudyClub: HyperPlonk (Binyi Chen, Benedikt Bünz)
PDF
Caulk: zkStudyClub: Caulk - Lookup Arguments in Sublinear Time (A. Zapico)
PDF
zkStudyClub: Zero-Knowledge Proofs Security, in Practice [JP Aumasson, Taurus]
PDF
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
PDF
zkStudy Club: Subquadratic SNARGs in the Random Oracle Model
PDF
ZK Study Club: Sumcheck Arguments and Their Applications
PDF
Ecfft zk studyclub 9.9
PDF
Quarks zk study-club
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - zkSaaS (Sruthi Sekar, UCB)
zkStudyClub - Lasso/Jolt (Justin Thaler, GWU/a16z)
zkStudyClub - Improving performance of non-native arithmetic in SNARKs (Ivo K...
zkStudyClub - ProtoStar (Binyi Chen & Benedikt Bünz, Espresso Systems)
zkStudyClub - cqlin: Efficient linear operations on KZG commitments
ZK Study Club: Supernova (Srinath Setty - MS Research)
Eos - Efficient Private Delegation of zkSNARK provers
zkStudyClub: HyperPlonk (Binyi Chen, Benedikt Bünz)
Caulk: zkStudyClub: Caulk - Lookup Arguments in Sublinear Time (A. Zapico)
zkStudyClub: Zero-Knowledge Proofs Security, in Practice [JP Aumasson, Taurus]
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
zkStudy Club: Subquadratic SNARGs in the Random Oracle Model
ZK Study Club: Sumcheck Arguments and Their Applications
Ecfft zk studyclub 9.9
Quarks zk study-club

Recently uploaded (20)

PDF
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PPTX
Safety Seminar civil to be ensured for safe working.
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PPTX
Artificial Intelligence
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PPTX
Current and future trends in Computer Vision.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PPTX
introduction to high performance computing
PPTX
communication and presentation skills 01
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PDF
Design Guidelines and solutions for Plastics parts
PPTX
Feature types and data preprocessing steps
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
Safety Seminar civil to be ensured for safe working.
Nature of X-rays, X- Ray Equipment, Fluoroscopy
Categorization of Factors Affecting Classification Algorithms Selection
Artificial Intelligence
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
Current and future trends in Computer Vision.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
introduction to high performance computing
communication and presentation skills 01
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Design Guidelines and solutions for Plastics parts
Feature types and data preprocessing steps
distributed database system" (DDBS) is often used to refer to both the distri...
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Fundamentals of safety and accident prevention -final (1).pptx
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx

zkStudyClub: CirC and Compiling Programs to Circuits