You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
where the term $x_i \cdot y_j$ is the logical AND of the bits, and the weight of the resulting bit is $2^{i+j}$.
333
350
Therefore, a multiplication can be implemented as a summation of weighted bits, where the bits are generated by logical AND of the bits of the two operands.
334
351
335
-
A bit heap is a data structure that effectively represents the summation of weighted bits \cite{arithmetic_core_generation_bitheaps}. Bits in a bit heap are organized into columns based on their weights, and the height of each column corresponds to the number of bit values that need to be summed for that particular weight.
352
+
A bit heap is a data structure that effectively represents the summation of weighted bits \cite{arithmetic_core_generation_bitheaps}. Bits in a bit heap are organized into columns based on their weights, and the height of each column corresponds to the number of bit values that need to be summed for that particular weight.
336
353
337
354
\subsection{Compression Algorithms}
355
+
\label{comp_algo}
338
356
In arithmetic circuits, a main contributor to delay is carry propagation. In cases where multiple summands must be added, the naive approach chains carry-propagation adders, growing the delay linearly with the number of bits and summands. For example, multiplication is usually implemented as addition of multiple summands.
339
357
340
358
Compressor trees are built to address this issue by reducing multiple summands to two using carry-save arithmetic,
@@ -347,7 +365,7 @@ \subsubsection{Wallace Tree}
347
365
348
366
\subsubsection{Dadda's Algorithm}
349
367
Dadda's algorithm is a method for reducing the partial products of an $n$-bit multiplier to a two-row representation
350
-
using \acrfull{fa} and \acrfull{ha}, after which a single \acrfull{cpa} produces the final result \cite{application_specific_arithmetic}.
368
+
using \acrfull{fa} and \acrfull{ha}, after which a single \acrfull{cpa} produces the final result \cite{application_specific_arithmetic}\cite{dadda_tree}.
In this section, we present the formalization of the bit heap data structure and compression algorithms in Lean 4.
409
+
In this section, we present the formalization of the bit heap data structure as well as the underlying hardware-level constructs and compression algorithms in Lean 4.
392
410
We also discuss the correctness of the composition of adders and how we relate bit heaps to bit-vector arithmetic.
393
411
412
+
\subsection{Circuit}
413
+
The building block of the bit heap data structure is a single bit.
414
+
In our formalization, the Circuit data structure represent a boolean expression, that is named by a natural-number index.
where the component at index $i$ holds the bits of weight $2^{i}$.
412
466
\end{definition}
413
467
468
+
To compute the value of the bit heap, we compute the weighted sum of the value held by columns, which is the sum of the values of the bits under a certain environment.
469
+
The value of the bit heap is computed in modular arithmetic. Therefore, all correctness statements below are stated as modulo $2^w$.
By setting $x = 2$, so that each column at index $k$ contributes its value $\mathcal{G}\sem{h_k}\sigma$ weighted by $2^{k}$, and reducing modulo $2^{w}$, we obtain the value of the bit heap.
494
+
Horner's method is structurally recursive on the list of columns, which makes it convenient for induction in Lean.
427
495
428
-
This recursive structure makes this definition convenient for our proofs in Lean 4 ITP.
496
+
\begin{table*}[t]
497
+
\centering
498
+
\caption{Compression steps. $\maj$ denotes the majority function
Our framework is based on modular arithmetic, so correctness statements for operations on a bit heap hold only modulo $2^w$.
431
-
The reason lies in the fixed nature of hardware, the number of bits is fixed and cannot be changed.
432
520
That means when a carry is produced on the last column, it is dropped.
433
521
434
-
Adding a bit to the bit heap requires care for the case where the bit to-be-added is already in the bit heap, since \texttt{Column}s are defined as sets and sets do not allow duplicates of the same values.
435
-
If the bit is already in the column $i$, we remove the existing copy and insert the bit into the next column, which is correct
436
-
because $x + x = 2x$, and repeat if the next column also contains it.
437
-
Effectively this results in building the bit heap in an optimized way by construction.
438
-
Another important operation is the bit removal operation.
439
-
Introduction of the removal operation forces us to use \texttt{Int} instead of \texttt{Nat}, since in bit removal the value of the bit heap can go to negative temporarily.
440
-
441
-
Two theorems describe the effect of adding and removing bits from the bit heap.
442
-
Adding a bit $c$ to column $k$ increases the value by $2^k \cdot c$ (Theorem~\ref{thm:addBit}), and removing a bit $c$ from column $k$, provided $c$ is
443
-
actually in that column, decreases it by $2^k \cdot c$ (Theorem~\ref{thm:removeBit}), both modulo $2^w$.
444
-
The \texttt{evalMod} function is implemented as evaluation using Horner's method modulo $2^w$
522
+
Two theorems describe the primitive operations. Adding a bit $c$ at
523
+
column $k$ increases the value by $2^{k}\lift{\mathcal{C}\sem{c}\sigma}$
524
+
(\ref{thm:addBit}). Removing a bit $c$ from column $k$, provided
525
+
$c$ is present in that column, decreases it by the same amount
526
+
(\ref{thm:removeBit}).
445
527
446
528
\begin{theorem}[\texttt{evalMod\_heap\_addBit}]
447
529
\label{thm:addBit}
448
-
For all $h : \mathtt{BitHeap}\;w$, $k : \mathbb{N}$, $c : \mathtt{Circuit}$
449
-
and $\mathit{env} : \mathtt{BitEnv}$,
530
+
For all $h : \Heap_w$, $k : \mathbb{N}$, $c : \Circ$ and
On top of these theorems, we prove that applying a half adder and a full adder preserves the value of the bit heap.
471
-
These two adders are the building blocks of our compression algorithms (Section~\ref{sec:compression})
552
+
Since a \texttt{Column} is defined as a set, it cannot contain duplicates.
553
+
Therefore, if the same bit is added to a \texttt{Column} at index k, we propagete the bit to the next column, since $2^{k}c + 2^{k}c = 2^{k+1}c$, and the propagation repeats if the next column already contains the circuit as well.
554
+
Effectively this results in building the bit heap in an optimized way during construction.
555
+
\autoref{thm:addBit} is proved by functional induction on \texttt{addBit}, following the carry propagation described above.
556
+
557
+
558
+
\subsection{Compression Algorithms}
559
+
\label{sec:compression}
560
+
Compression reduces the height of a bit heap to two, after which a
561
+
single \acrshort{cpa} produces the result \cite{application_specific_arithmetic}.
562
+
The reduction is carried out by compressors, half adders ($2{:}2$),
563
+
full adders ($3{:}2$), or in general any $N{:}M$ compressor with
564
+
$N \ge M$. We formalize half and full adders, and build compressor trees
565
+
from them. However, our framework can easily be extended for any compressor.
472
566
473
-
\begin{theorem}[\texttt{halfAdder\_correct\_mod}]
474
-
\label{thm:halfAdder-correct}
475
-
For all $h : \mathtt{BitHeap}\;w$, $k : \mathbb{N}$, $i, j : \mathtt{Circuit}$
476
-
with $i, j \in h.\mathtt{get}(k)$ and $i \neq j$,
477
567
\[
478
-
\forall\,\mathit{env} : \mathtt{BitEnv},
479
-
\mathtt{evalMod}\big(\mathtt{halfAdder}(k, i, j, h).\mathtt{heap},\,\mathit{env}\big)
480
-
\;=\;
481
-
\mathtt{evalMod}(h, \mathit{env})
568
+
\begin{array}{r@{\;}c@{\;}l@{\qquad}l}
569
+
\Step\ni s &::=& \HA(k,c,c) \mid\FA(k,c,c,c) &\\[2pt]
570
+
\Chain\ni S &::=& [\,] \mid s :: S &
571
+
\end{array}
482
572
\]
483
-
\end{theorem}
484
573
485
-
\begin{theorem}[\texttt{fullAdder\_correct\_mod}]
486
-
\label{thm:fullAdder-correct}
487
-
For all $h : \mathtt{BitHeap}\;w$, $k : \mathbb{N}$, $i, j, l : \mathtt{Circuit}$
488
-
with $i, j, l \in h.\mathtt{get}(k)$ pairwise distinct,
574
+
Each compression step records the type of adder, the column it acts on, and the bits it consumes.
575
+
The effects of a single step on the heap with the applicability conditions are shown in Table~\ref{tab:steps}.
576
+
Both applicability conditions are forced by Theorem~\ref{thm:removeBit}, which requires bits to be present and that they are pairwise distinct.
577
+
Writing $s(h)$ for the heap obtained by applying step $s$ to $h$, we prove that each step preserves the value of the heap.
A half adder on column $c$ with bits $i$ and $j$ removes both from the column, inserts $i \oplus j$ into column $c$ and $i \wedge j$ into column $c+1$.
498
-
The correctness theorem for the half adder states that the resulting bit heap has the same value modulo $2^w$, under the hypothesis that $i$ and $j$ belong to column $c$ and that they are distinct.
499
-
The membership hypothesis is required by the remove operation, and the uniqueness is needed to ensure we do not remove the same bit twice from the bit heap.
500
-
We know the latter is already satisfied by our implementation of the bit insertion function, and the way we satisfy the former will be explained in \autoref{sec:compression}.
501
-
To prove Theorem~\ref{thm:halfAdder-correct}), we rewrite insertion and removal theorems and the goal becomes an arithmetic identity on boolean values of $i$ and $j$, after which a case split on the boolean values closes the goal.
502
-
The full adder is proved the same way (Theorem~\ref{thm:fullAdder-correct}), with an extra carry-in bit $k$, the sum becomes $i \oplus j \oplus k$, and a carry $(i \wedge j) \vee (i \wedge k) \vee (j \wedge k)$.
503
-
These few definitions and theorems form the basis of our formalization and they provide all the machinery the compression algorithms require.
588
+
With proofs being similar for both half and full adder cases, the proofs rewrite the Theorems \ref{thm:addBit} and \ref{thm:removeBit} until the
589
+
goal becomes an arithmetic identity over the Boolean values of bits, after which a case split on the boolean values closes the goal.
504
590
591
+
A compression algorithm produces a chain of compressors.
592
+
Applicability is a condition on the heap the step is applied to, so it must be checked against the intermediate bit heap, not the original.
505
593
506
-
\subsection{Compression Algorithms}
507
-
\label{sec:compression}
508
-
We implement Wallace and Dadda trees.
509
-
594
+
\begin{definition}[Well-formed chain]
595
+
\label{def:wellformed-chain}
596
+
A chain $s :: S$ is well-formed for $h$ if $s$ is applicable to $h$and $S$ is well-formed for $s(h)$.
597
+
\end{definition}
510
598
511
-
\subsubsection{Correctness of Composition of Adders}
\autoref{thm:chain-correct} is proven by induction on the chain, using Theorem~\ref{thm:step-correct} for the base case and the induction hypothesis for the tail of the chain.
513
609
514
-
Adder:
610
+
In our framework, we separate the correctness of the compression from the generation of the chain.
611
+
Theorem~\ref{thm:chain-correct} proves that any well-formed chain preserves the value of the heap.
612
+
Therefore, any algorithm producing a well-formed chain is proven correct.
613
+
That means adding a new algorithm does not require any new proof.
614
+
We use this to implement Wallace and Dadda trees against a single correctness theorem.
515
615
516
-
ChainPreconditions:
517
-
- applyChainSafe:
616
+
For chains produced by a compression algorithm, we provide a checked application \texttt{applyChainSafe}, which tests applicability at each step and returns $\mathtt{none}$ on failure.
518
617
519
618
\subsection{From Bit Heaps to Bit-Vector Arithmetic}
520
619
At the core of our formalization lies the correctness of Bit heap operations with respect to the semantics of bit-vector arithmetic.
0 commit comments