RLP decoding of a tree.
(rlp-decode-tree encoding) → (mv error? tree)
If the byte array encodes some tree, we return that tree,
along with a
This is a declarative, non-executable definition, which says that decoding is the inverse of encoding. This is the intention of [YP:B], which only specifies encoding, leaving decoding implicit.
More precisely, we define decoding as the right inverse of encoding
(with respect to byte arrays that are valid encodings of trees),
as explicated by the theorem
We use the injectivity of rlp-encode-tree,
proved
The decoding code in [Wiki:RLP] provides a reference implementation. Note that it is considerably more complicated than the encoding code. Our high-level specification of decoding is simpler.
Function:
(defun rlp-decode-tree (encoding) (declare (xargs :guard (byte-listp encoding))) (b* ((encoding (byte-list-fix encoding))) (if (rlp-tree-encoding-p encoding) (mv nil (rlp-tree-encoding-witness encoding)) (mv t (rlp-tree-leaf nil)))))
Theorem:
(defthm booleanp-of-rlp-decode-tree.error? (b* (((mv ?error? ?tree) (rlp-decode-tree encoding))) (booleanp error?)) :rule-classes :rewrite)
Theorem:
(defthm rlp-treep-of-rlp-decode-tree.tree (b* (((mv ?error? ?tree) (rlp-decode-tree encoding))) (rlp-treep tree)) :rule-classes :rewrite)
Theorem:
(defthm rlp-encode-tree-of-rlp-decode-tree (implies (rlp-tree-encoding-p encoding) (b* (((mv d-error? tree) (rlp-decode-tree encoding)) ((mv e-error? encoding1) (rlp-encode-tree tree))) (and (not d-error?) (not e-error?) (equal encoding1 (byte-list-fix encoding))))))
Theorem:
(defthm rlp-decode-tree-of-rlp-encode-tree (b* (((mv e-error? encoding) (rlp-encode-tree tree)) ((mv d-error? tree1) (rlp-decode-tree encoding))) (implies (not e-error?) (and (not d-error?) (equal tree1 (rlp-tree-fix tree))))))
Theorem:
(defthm rlp-decode-tree-of-byte-list-fix-encoding (equal (rlp-decode-tree (byte-list-fix encoding)) (rlp-decode-tree encoding)))
Theorem:
(defthm rlp-decode-tree-byte-list-equiv-congruence-on-encoding (implies (byte-list-equiv encoding encoding-equiv) (equal (rlp-decode-tree encoding) (rlp-decode-tree encoding-equiv))) :rule-classes :congruence)