RLP encoding of a byte array.
(rlp-encode-bytes bytes) → (mv error? encoding)
This corresponds to
The first result of this function is an error flag,
which is
Encodings are never empty,
i.e. they always consist of at least one byte:
see theorem
The first byte of an encoding is always below 192:
see theorem
The total length of an encoding can be determined
from the first few bytes (i.e. a prefix) of the encoding:
see theorem
The total length of an encoding that uses a ``long'' length field
(i.e. when the initial byte is followed by the length of the length,
and the actual length consists of one or more bytes)
is larger than the length field itself:
see theorem
Function:
(defun rlp-encode-bytes (bytes) (declare (xargs :guard (byte-listp bytes))) (b* ((bytes (byte-list-fix bytes))) (cond ((and (= (len bytes) 1) (< (car bytes) 128)) (mv nil bytes)) ((< (len bytes) 56) (b* ((encoding (cons (+ 128 (len bytes)) bytes))) (mv nil encoding))) ((< (len bytes) (expt 2 64)) (b* ((be (nat=>bebytes* (len bytes))) (encoding (cons (+ 183 (len be)) (append be bytes)))) (mv nil encoding))) (t (mv t nil)))))
Theorem:
(defthm booleanp-of-rlp-encode-bytes.error? (b* (((mv ?error? ?encoding) (rlp-encode-bytes bytes))) (booleanp error?)) :rule-classes :rewrite)
Theorem:
(defthm byte-listp-of-rlp-encode-bytes.encoding (b* (((mv ?error? ?encoding) (rlp-encode-bytes bytes))) (byte-listp encoding)) :rule-classes :rewrite)
Theorem:
(defthm consp-of-rlp-encode-bytes-when-no-error (implies (not (mv-nth 0 (rlp-encode-bytes bytes))) (consp (mv-nth 1 (rlp-encode-bytes bytes)))) :rule-classes :type-prescription)
Theorem:
(defthm car-of-rlp-encode-bytes-upper-bound-when-no-error (implies (not (mv-nth 0 (rlp-encode-bytes bytes))) (<= (car (mv-nth 1 (rlp-encode-bytes bytes))) 191)) :rule-classes :linear)
Theorem:
(defthm len-of-rlp-encode-bytes-from-prefix (b* (((mv error? encoding) (rlp-encode-bytes bytes))) (implies (not error?) (equal (len encoding) (cond ((< (car encoding) 128) 1) ((< (car encoding) (+ 128 56)) (1+ (- (car encoding) 128))) (t (b* ((lenlen (- (car encoding) 183))) (+ 1 lenlen (bebytes=>nat (take lenlen (cdr encoding)))))))))))
Theorem:
(defthm len-of-rlp-encode-bytes-lower-bound-when-len-len (b* (((mv error? encoding) (rlp-encode-bytes bytes))) (implies (and (not error?) (>= (car encoding) (+ 128 56))) (> (len encoding) (- (car encoding) 183)))) :rule-classes :linear)
Theorem:
(defthm rlp-encode-bytes-of-byte-list-fix-bytes (equal (rlp-encode-bytes (byte-list-fix bytes)) (rlp-encode-bytes bytes)))
Theorem:
(defthm rlp-encode-bytes-byte-list-equiv-congruence-on-bytes (implies (byte-list-equiv bytes bytes-equiv) (equal (rlp-encode-bytes bytes) (rlp-encode-bytes bytes-equiv))) :rule-classes :congruence)