davinci-paper/v2/sections/circuits.tex
v2/sections/circuits.texRaw
1% !TeX root = ../build/main.tex
2
3At the core of \davinci lie a set of arithmetic circuits that enforce the correctness of every step of the election. Each circuit corresponds to a distinct task and, taken together, they guarantee that all protocol rules are satisfied without revealing any private information. Specifically, the \emph{ballot circuit} (Section~\ref{sec:vocdoni-protocol:circuits:ballot}) ensures that encrypted votes are valid and well-formed, the \emph{verifier circuit} (Section~\ref{sec:vocdoni-protocol:circuits:verifier}) verifies the voter's census membership; the \emph{aggregation circuit} (Section~\ref{sec:vocdoni-protocol:circuits:aggregation}) combines multiple authenticated votes into a single proof; the \emph{state transition circuit} (Section~\ref{sec:vocdoni-protocol:circuits:state-transition}) updates the global state root and produces the \ac{ZK-SNARK} proof that is verified on-chain; and the \textit{results circuit} (Section~\ref{sec:vocdoni-protocol:circuits:results}) proves that the election results were computed correctly. The flow of these circuits and their interactions is shown in Figure~\ref{fig:circuits-flow}.
4\begin{figure}[h]
5 \centerline{\includegraphics[width=\linewidth,draft=false]{\figs/circuits-flow.pdf}}
6 \caption{
7 Overview of the circuit flow in \davinci.}
8 \label{fig:circuits-flow}
9\end{figure}
10
11For clarity, the circuits are described in this section with their full set of public inputs explicitly listed. In the actual implementation, however, we apply an optimization: rather than exposing all public inputs $\publicinputs$ individually, we compute a single commitment $h = \hlget{\Mimc}(\publicinputs)$ and use $h$ as the only public input. The proof verifier then checks that $\hlget{\Mimc}(\publicinputs) = h$, ensuring that the original public inputs are consistent while reducing both proof size and verification cost. Note that this optimization does not alter the semantics of the circuits but significantly reduces verifier complexity and on-chain verification costs. Other implementation aspects such as the exact number of constraints and the proving frameworks used are deferred to Section~\ref{sec:analysis:implementation}.
12
13% Old text: By structuring the process this way, we ensure that voting can be performed from any device-including smartphones and web browsers-while keeping the sequencer's computational requirements within the capabilities of accessible, CPU-based machines with 64 GiB of memory.
14
15\subsubsection{Ballot circuit}
16\label{sec:vocdoni-protocol:circuits:ballot}
17
18The ballot circuit (\hlset{\ballotcircuit}), illustrated in Figure~\ref{fig:circuit-ballot}, is generated locally by the voter at the time of casting a ballot. Its purpose is twofold: first, to prove that the encrypted ballot is valid and complies with the protocol rules defined by the organizer; and second, to prove that the vote identifier (\voteid) has been correctly derived. The correctness of this circuit is attested by a \ac{ZK} proof generated using the \ac{ZK-SNARK} protocol described in Section~\ref{sec:cryptographic-primitives:zkp}, instantiated over the BN254 curve. We call the resulting proof \textit{ballot proof} and denote it as $\hlset{\ballotproof}$. We detail below the inputs and constraints of the ballot circuit.
19
20\begin{spacing}{1.25}
21 \vspace{0.3cm}
22 \noi \textbf{Public inputs}
23 \begin{itemize}
24 \item $\public$ $\processid$: election identifier.
25 \item $\public$ $\ballotmode$: ballot protocol configuration.
26 \item $\public$ $\epk$ ($EPK$): encryption public key.
27 \item $\public$ $\address$: voter's address.
28 \item $\public$ $\weight$: voter's weight.
29 \item $\public$ $\encryptedballot$: encrypted ballot.
30 \item $\public$ $\voteid$: vote's unique identifier.
31 \end{itemize}
32 \textbf{Private inputs}
33 \begin{itemize}
34 \item \private $\ballot$: plaintext voter's voting choice.
35 \item \private $k$: random scalar.
36 \end{itemize}
37 \textbf{Constraints}
38 \begin{itemize}
39 \item \emph{Vote encryption}. Correct encryption of the ballot: \[\encryptedballot = \hlget{\Enc}_{\epk}(\ballot; k).\]
40 \item \emph{Protocol rules compliance}. The ballot meets the protocol rules (number of valid choices, etc.):
41 \[\text{protocolRulesCompliance}(\ballotmode, \weight, \ballot) = \true.\]
42 \item \emph{Vote identifier}. The vote's identifier is correctly computed:
43 \[\voteid = \text{Hash} (\processid \conc \address \conc k).\]
44 \end{itemize}
45 \vspace{-0.3cm}
46\end{spacing}
47
48\begin{figure}[H]
49 \centerline{\includegraphics[width=380pt,draft=false]{\figs/circuit1.pdf}}
50 \caption{Ballot circuit. All public values are framed in green.}
51 \label{fig:circuit-ballot}
52\end{figure}
53
54\subsubsection{Verifier circuit}
55\label{sec:vocdoni-protocol:circuits:verifier}
56
57The verifier circuit (\hlset{\verifiercircuit}), illustrated in Figure~\ref{fig:circuit-verifier}, is generated by the sequencer when processing a vote. This circuit verifies the ballot's proof generated by the voter and it also validates the voter’s eligibility in the census and the correctness of their digital signature. The correctness of this circuit is attested by a ZK proof generated using the ZK-SNARK protocol described in Section~\ref{sec:cryptographic-primitives:zkp}, instantiated over the BLS12-377 curve. We call the resulting proof the \textit{authentication proof} and denote it as $\hlset{\authenticationproof}$. We detail below the inputs and constraints of this circuit. {\color{blue}-- Authentication of verification proof?}
58
59\begin{spacing}{1.25}
60 \vspace{0.3cm}
61 \noi \textbf{Public inputs}
62 \begin{itemize}
63 \item \public $\processid$: election identifier.
64 \item \public $\ballotmode$: ballot protocol configuration.
65 \item \public $\epk$ ($EPK$): encryption public key.
66 \item \public $\encryptedballot$: encrypted ballot.
67 \item \public $\voteid$: voter's identifier.
68 \item \public $\address$: voter's address.
69 \item \public $\weight$: voter's weight.
70 \end{itemize}
71 \textbf{Private inputs}
72 \begin{itemize}
73 \item \private $\pk$: voter's public key.
74 \item \private $\signature$: voter's signature.
75 \item \private $\ballotproof$: voter's ballot proof.
76 \end{itemize}
77 \textbf{Constraints}
78 \begin{itemize}
79 \item \emph{Voter's proof verification.} The voter's submitted proof is valid for the inputs provided:
80 $$\hlget{\PVerify}\left(\ballotproof, PI = (\processid, \ballotmode, \epk, \encryptedballot, \voteid, \weight)\right) = \true.$$
81 \item Compute the address derived from the user's public key: $ \address = \hlget{\Keccak}(\pk)$.
82 \item \emph{Authentication + non-malleability.} The signature of the vote's identifier is valid for the given public key:
83 $$ \hlget{\SVer}_{\pk}(\voteid, \signature) = \true. $$
84 \end{itemize}
85 \vspace{-0.3cm}
86\end{spacing}
87
88\begin{figure}[H]
89 \centerline{\includegraphics[width=450pt,draft=false]{\figs/circuit2.pdf}}
90 \caption{Authentication circuit. All public values are framed in green.}
91 \label{fig:circuit-verifier}
92\end{figure}
93
94\subsubsection{Aggregation circuit}
95\label{sec:vocdoni-protocol:circuits:aggregation}
96
97The aggregation circuit (\hlset{\aggregationcircuit}), illustrated in Figure~\ref{fig:circuit-aggregate}, is generated by the sequencer to combine multiple authenticated votes into a single proof. Its purpose is to reduce verification overhead by recursively aggregating individual authentication proofs, while ensuring that all aggregated votes belong to the same election. The batch size, i.e., the maximum number of proofs aggregated in a single execution, is a fixed parameter (currently set to 60). If fewer proofs are available, the sequencer pads the batch with dummy proofs so that the circuit always operates on a fixed-size input. The correctness of this circuit is attested by a ZK proof generated using the \ac{ZK-SNARK} protocol described in Section~\ref{sec:cryptographic-primitives:zkp}, instantiated over the BW6-761 curve. We call the resulting proof the \textit{aggregation proof} and denote it as $\hlset{\aggregationproof}$. We detail below the inputs and constraints of the aggregation circuit.
98
99\begin{spacing}{1.25}
100 \vspace{0.3cm}
101 \noi \textbf{Public inputs}
102 \begin{itemize}
103 \item \public $\encryptedballot_i$: list of voters' encrypted ballots.
104 \item \public $\voteid_i$: set of vote's identifiers.
105 \item \public $\address_i$: list of voters' addresses.
106 \item \public $\processid$: election identifier.
107 \item \public $\ballotmode$: ballot protocol configuration.
108 \item \public $\epk$ ($EPK$): encryption public key.
109 \end{itemize}
110 \textbf{Private inputs}
111 \begin{itemize}
112 \item \private $\authenticationproof_i$: set of authentication proofs.
113 \end{itemize}
114 \textbf{Constraints} \\ \vspace{-0.4cm}
115 \begin{itemize}
116 \item \emph{Votes aggregation.} The accumulated authentication proofs are valid (for the given inputs). For every $i$:
117 $$\hlget{\PVerify}\left(\authenticationproof, \publicinputs = (\encryptedballot_i, \voteid_i, \address_i, \processid, \ballotmode, EPK)\right) = \true.$$
118 \item \emph{Shared public inputs.} The public inputs \processid, \ballotmode, \epk, and \censusroot are the same for all proofs.
119 \end{itemize}
120 \vspace{-0.3cm}
121\end{spacing}
122
123\begin{figure}[h]
124 \centerline{\includegraphics[width=400pt,draft=false]{\figs/circuit3.pdf}}
125 \caption{Aggregation circuit. All public values are framed in green.}
126 \label{fig:circuit-aggregate}
127\end{figure}
128
129\subsubsection{State transition circuit}
130\label{sec:vocdoni-protocol:circuits:state-transition}
131
132The state transition circuit (\hlset{\statetransitioncircuit}), illustrated in Figure~\ref{fig:circuit-state-transition-v2}, is generated by the sequencer to update the global state of the election. Its purpose is to verify that a batch of aggregated votes has been correctly incorporated into the state Merkle tree, that overwrites and vote identifiers are handled consistently, and that the accumulators of encrypted results are updated accordingly. The correctness of this circuit is attested by a \ac{ZK} proof generated using the \ac{ZK-SNARK} protocol described in Section~\ref{sec:cryptographic-primitives:zkp}, instantiated over the BN254 curve, which is supported by Ethereum’s native precompiles for proof verification. We call the resulting proof the \textit{state proof} and denote it as $\hlset{\stateproof}$. This proof is submitted on-chain and verified by the process management smart contract, ensuring that every accepted state transition complies with the protocol rules.
133
134To formalize the setting, let the sequencer collect a batch of $N$ votes ($N<60$), of which $n$ are new submissions and $m = N-n$ are overwrites of previously cast ballots. In the following, we describe the inputs and constraints of the state transition circuit in detail.
135
136
137\begin{spacing}{1.25}
138 \vspace{0.3cm}
139 \noi \textbf{Public inputs}
140 \begin{itemize}
141 \item \public $\censusroot$: census Merkle tree root.
142 \item \public $\stateroot^{old}$: current state tree root.
143 \item \public $\stateroot^{new}$: new tree root after all state transitions are applied.
144 \item \public $\numoverwrites^{old}$: current number of overwrites.
145 \item \public $\numoverwrites^{new}$: number of overwrites after all state transitions are applied.
146 \item \public $\numvotes^{old}$: current number of votes.
147 \item \public $\numvotes^{new}$: total number of votes after all state transitions are applied.
148 \item \public $C$: commitment to the data blob.
149 \end{itemize}
150 \textbf{Private inputs}
151 \begin{itemize}
152 \item \private $\censusorigin$: census origin parameter.
153 \item \private $\{\aggregationproof_i\}_{i= 1}^{N}$: set of aggregation proofs.
154 \item \private $\{\censuspf_i\}_{i = 1}^N$: census membership proofs.
155 \item \private $\ballotmode$: ballot protocol configuration.
156 \item \private $EPK$: encryption public key.
157 \item \private $\processid$: election identifier.
158 \item \private $\{\voteid_i\}_{i= 1}^{N}$: set of vote's identifiers.
159 \item \private $\{\encryptedballot_i\}_{i= 1}^{N}$: set of voters' encrypted ballots.
160 \item \private $\{\pk_i\}_{i= 1}^{N}$: list of voters' public keys.
161 \item \private $\{\merkleproofs^{encBallots}_i\}_{i= 1}^{N}$: Merkle tree proofs of encrypted ballots. If it is a new vote, the proof will be a non-inclusion proof, if it is an overwrite, an inclusion proof.
162 \item \private $\{\merkleproofs^{\voteid}_i\}_{i= 1}^{N}$: non-membership proofs of vote identifiers.
163 \item \private $\addaccumulator^{old}$: current accumulator of votes (before the transitions).
164 \item \private $\substractaccumulator^{old}$: current accumulator of votes that have been overwritten (before the transition).
165 \item \private $k$: random scalar.
166 \item \private $\openproof$: KZG opening proof.
167 \end{itemize}
168 \textbf{Constraints} \\ \vspace{-0.4cm}
169 \begin{itemize}
170 \item \emph{Aggregation verification.} The accumulated authentication proofs are valid. For every $i\in\{1,\dots,N\}$:
171 $$\hlget{\PVerify}(\authenticationproof, PI = (\encryptedballot_i, \voteid_i, \pk_i, \processid, \ballotmode, EPK, \censusroot)).$$
172 \item \emph{Census membership.} The address derived from the user's public key is part of the census, and verifies the census proof with the user's weight provided:
173 $$ \hlget{\censusTree}.\hlget{\MTVerify}\left(\weight, \address, \censuspf, \censusroot\right) = \true. $$
174 \item \emph{Transition verification.} Verify initial state and state transitions are applied correctly.
175 \begin{itemize}
176 \renewcommand{\labelitemii}{$\circ$}
177 \item Verify that the inputs $\processid$, $\censusroot$, $\ballotmode$, $\epk$, $\addaccumulator$, and $\substractaccumulator$ correspond to the content of the census Merkle tree leaves $\texttt{0x0}$, $\texttt{0x1}$, $\texttt{0x2}$, $\texttt{0x3}$, $\texttt{0x4}$, $\texttt{0x5}$, respectively. {\color{blue} The root of the tree, with all data and those six leaves, results in \stateroot.}
178 \[\stateTree\text{(with these six leaves and the rest)}.\MTRoot() = \stateroot.\]
179 \item Verify that none of the $\voteid_i$ for all $i\in\{1,\dots,N\}$ are initially part of the tree:
180 \[\stateTree.NonInclusionVerify(\voteid_i, \merkleproofs^{\voteid}_i, \stateroot).\]
181 \item Ensure that new votes are not part of the tree yet. For $i = 1, \dots, n$:
182 \[\stateTree \text{-- the leaf following the $\address_i = \hlget{\Keccak}(\pk_i)$ path is empty}.\]
183 \item Ensure that overwrites do correspond to votes that were already on the tree. For $i = n+1, \dots, N$:
184 \[\stateTree \text{-- the leaf following the $\address_i = \hlget{\Keccak}(\pk_i)$ path stores an encrypted ballot}.\]
185 \item State transitions.
186 \begin{itemize}
187 \item For new votes (keep iterating for all of them):
188 \begin{enumerate}
189 \item Add encrypted ballot to the tree: $\stateTree.\hlget{\MTInsert}(\address = \hlget{\Keccak}(\pk), \encryptedballot)$.
190 \item Add vote identifier: $\stateTree.\hlget{\MTInsert}(\voteid)$.
191 \item Increase votes counter: $\numvotes = \numvotes + 1$.
192 \item Aggregate the vote to the results accumulator: $\addaccumulator = \addaccumulator + \encryptedballot$.
193 \end{enumerate}
194 \item For overwritten votes (keep iterating for all of them):
195 \begin{enumerate}
196 \item Overwrite the encrypted ballot: $\stateTree.\hlget{\MTUpdate}(\address = \hlget{\Keccak}(\pk), \encryptedballot)$.
197 \item Add vote identifier: $\stateTree.\hlget{\MTInsert}(\voteid)$.
198 \item Increase votes counter: $\numvotes = \numvotes + 1$.
199 \item Increase overwrites counter: $\numoverwrites = \numoverwrites + 1$.
200 \item Aggregate the vote to the results accumulator: $\addaccumulator = \addaccumulator + \encryptedballot$.
201 \item Aggregate the old vote to the accumulator: $\substractaccumulator = \substractaccumulator + \encryptedballot^{old}$.
202 \end{enumerate}
203 {\color{blue}The old $\encryptedballot^{old}$ should also be inputs or retrieved from the MT inclusion proofs -- YES.}
204 \end{itemize}
205 \item After all previous state transitions are done, recompute new tree root:
206 \[\stateroot^{new} = \stateTree.\hlget{\MTRoot}().\]
207 \item Reencrypt all leafs of the tree (both old and new ones). {\color{blue}New are ok, but for those that are old, also verify MTProofs of membership? -- YES}.
208 \[newLeafContent = \hlget{\ReEnc}_{\epk}(\encryptedballot_i, k), \quad k = Hash(k).\]
209 \end{itemize}
210 \item \emph{Data blob}.
211 \begin{itemize}
212 \item Construct blob $B$ (as described in Section~X).
213 \item Derive polynomial $p(x)$ from $B$.
214 \item Compute evaluation point
215 \( z = \hlget{\Poseidon}(\processid, C, \openproof) \).
216 \item Compute evaluation \(y = p(z)\).
217 \item Verify
218 \(\hlget{\CVerify}(z, y, C, \openproof) = \true.\)\\
219 \end{itemize}
220 \end{itemize}
221 \vspace{-0.3cm}
222\end{spacing}
223
224{\color{blue}Mention that an advantage of splitting the circuits of the sequencers in 3 is that if the stateRoot changes in the meantime, the sequencer only needs to rerun the last circuit and not all three of them.}\\
225
226\martai{\numvotes and \numoverwrites are part of the smart contract state.}
227
228\begin{landscape}
229 \centering
230 {\setkeys{Gin}{draft=false}%shows image even in draft mode
231 \includegraphics[width=\linewidth]{\figs/circuit4.pdf}}
232 \captionof{figure}{State transition circuit. All public values are framed in green.}
233 \label{fig:circuit-state-transition-v2}
234\end{landscape}
235
236\subsubsection{Results circuit}
237\label{sec:vocdoni-protocol:circuits:results}
238
239The results circuit (\hlset{\resultscircuit}) proves that... .
240
241\begin{figure}[h]
242 \centerline{\includegraphics[width=400pt,draft=false]{\figs/circuit5.pdf}}
243 \caption{Aggregation circuit. All public values are framed in green. \color{red}Circuit is not correct.}
244 \label{fig:circuit-results}
245\end{figure}