Skip to content

Commit 7569ae0

Browse files
authored
Fix natural number flat encoding on the spec (#7263)
1 parent 9fce2f9 commit 7569ae0

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

doc/plutus-core-spec/flat-serialisation.tex

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,34 @@ \subsection{Arrays}
194194
decoded list $l$ (from the remainder $s^{(k)}$) forms the final result.
195195

196196
\subsection{Natural numbers}
197-
We encode natural numbers by splitting their binary representations into
198-
sequences of 7-bit blocks, then emitting these as a list with the \textbf{least
199-
significant block first}:
197+
Every natural number $n$ of arbirary size can be uniquely written in the following form:
200198

201199
$$
202-
\E_{\N} (s, \sum_{i=0}^{n-1}k_i2^{7i}) = \Elist_7(s, [k_0, \ldots, k_{n-1}])
200+
n = \sum_{i=0}^{n}k_i2^{7i} \text{ where for any $i$ with } 0 \leq i \leq n \text{, } 0 \leq k_i \leq 127 \text{ and } k_n \ne 0
203201
$$
204-
\noindent(where $k_i \in \Z$ and $0 \leq k_i \leq 127$).
202+
203+
\noindent Here, each $k_i$ is 7 bit binary block; $k_0$ being the least significant block.
204+
205+
$$
206+
\E_{\N} (s, \sum_{i=0}^{n}k_i2^{7i}) =
207+
\begin{cases}
208+
\E_7(\Elist_7(s, []), k_{0}) & \text{if } n = 0 \\
209+
\E_7(\Elist_7(s, [k_0, \ldots, k_{n-1}]), k_{n}) & \text{if } n \ne 0
210+
\end{cases}
211+
$$
212+
205213
\noindent The decoder is
206214
$$
207-
\D_{\N}(s) = (s', \sum_{i=0}^{n-1}k_i2^{7i}) \quad \text{if $\Dlist_7(s) = (s', [k_0, \ldots, k_{n-1}])$}.
215+
\D_{\N}(s) =
216+
\begin{cases}
217+
\text{let } \Dlist_7(s) = (s', ks) \\
218+
D_7(s') & \text{if } ks = [] \\
219+
(s'', \sum_{i=0}^{n}k_i2^{7i}) & \text{if } ks = [k_0, \ldots, k_{n-1}] \text{ and } \D_7(s') = (s'', k_{n})
220+
\end{cases}
208221
$$
209222

223+
\noindent Intuitively, every 7 bit blocks except the last block are prefixed by 1 and the last block is prefixed by 0.
224+
210225
\subsection{Integers}
211226
Signed integers are encoded by converting them to natural numbers using the
212227
zigzag encoding ($0 \mapsto 0, -1 \mapsto 1, 1 \mapsto 2, -2 \mapsto 3, 2

0 commit comments

Comments
 (0)