Files

90 lines
3.6 KiB
TeX

\section{The Precompile Interface}
\subsection{Go Interface}
Every precompile implements a standard Go interface:
\begin{lstlisting}[language=Go,caption={The precompile interface}]
// PrecompiledContract is the interface for native contracts.
type PrecompiledContract interface {
// RequiredGas returns the gas cost for the given input.
// Must be deterministic: same input -> same gas.
RequiredGas(input []byte) uint64
// Run executes the precompile with the given input.
// Returns output bytes and an error.
// Must be deterministic: same input -> same output.
Run(input []byte) ([]byte, error)
}
\end{lstlisting}
Two methods, both deterministic:
\begin{enumerate}[nosep]
\item \texttt{RequiredGas}: Called before execution to check if the caller has enough gas.
Must not perform expensive computation---it should be $O(1)$ or $O(\text{input length})$.
\item \texttt{Run}: Performs the actual computation. Returns output bytes or an error.
On error, all gas is consumed (precompile failure is an out-of-gas condition).
\end{enumerate}
\subsection{Registration}
Precompiles are registered in the VM configuration:
\begin{lstlisting}[language=Go,caption={Registering a precompile}]
var PrecompiledContractsLux = map[common.Address]PrecompiledContract{
// Standard Ethereum precompiles (0x01 - 0x09)
common.BytesToAddress([]byte{0x01}): &ecRecover{},
common.BytesToAddress([]byte{0x02}): &sha256hash{},
// ...
// Lux extensions (0x0200 - 0x02ff)
common.BytesToAddress([]byte{0x02, 0x00}): &bls12381Pairing{},
common.BytesToAddress([]byte{0x02, 0x01}): &bls12381G1Add{},
common.BytesToAddress([]byte{0x02, 0x02}): &bls12381G1Mul{},
common.BytesToAddress([]byte{0x02, 0x03}): &bls12381MapFp{},
common.BytesToAddress([]byte{0x02, 0x10}): &bn254Pairing{},
common.BytesToAddress([]byte{0x02, 0x20}): &batchEcRecover{},
common.BytesToAddress([]byte{0x02, 0x30}): &dilithiumVerify{},
common.BytesToAddress([]byte{0x02, 0x31}): &falconVerify{},
common.BytesToAddress([]byte{0x02, 0x40}): &fheEncrypt{},
common.BytesToAddress([]byte{0x02, 0x41}): &fheAdd{},
common.BytesToAddress([]byte{0x02, 0x42}): &fheMul{},
common.BytesToAddress([]byte{0x02, 0x50}): &warpVerify{},
common.BytesToAddress([]byte{0x02, 0x60}): &nttForward{},
common.BytesToAddress([]byte{0x02, 0x61}): &nttInverse{},
// ... (20 total)
}
\end{lstlisting}
\subsection{Address Space Convention}
\begin{table}[H]
\centering
\small
\begin{tabular}{@{}lll@{}}
\toprule
\textbf{Range} & \textbf{Owner} & \textbf{Purpose} \\
\midrule
\texttt{0x0001}--\texttt{0x0009} & Ethereum & Standard precompiles \\
\texttt{0x000a}--\texttt{0x00ff} & Ethereum & Reserved for EIPs \\
\texttt{0x0100}--\texttt{0x01ff} & Reserved & Chain-specific \\
\texttt{0x0200}--\texttt{0x020f} & Lux & BLS12-381 operations \\
\texttt{0x0210}--\texttt{0x021f} & Lux & BN254 extensions \\
\texttt{0x0220}--\texttt{0x022f} & Lux & Batch operations \\
\texttt{0x0230}--\texttt{0x023f} & Lux & Post-quantum signatures \\
\texttt{0x0240}--\texttt{0x024f} & Lux & FHE operations \\
\texttt{0x0250}--\texttt{0x025f} & Lux & Warp/cross-chain \\
\texttt{0x0260}--\texttt{0x026f} & Lux & Polynomial/NTT \\
\texttt{0x9000}--\texttt{0x90ff} & Lux & Application adapters (DEX) \\
\bottomrule
\end{tabular}
\caption{Precompile address space allocation. The \texttt{0x90xx}
application range holds non-cryptographic adapters; the canonical DEX CLOB
receipt-settlement adapter at \texttt{0x9999} (the deprecated
\texttt{0x9010} aliases to it) is documented in
Section~\ref{sec:dex-adapter}.}
\end{table}
%% ============================================================