Turn a list of bytes into the corresponding list of Base58 characters.
(base58-encode bytes) → chars
This is described in bullets 4, 5, and 6 in Section `Creating a Base58Check string' of Page `Base58Check encoding' of [Wiki].
The bytes are treated as big bendian digits in base 256
and converted into the natural number that they denote.
This natural number is converted into
a minimal-length list of big endian digits in base 58,
which has no leading zeros.
These digits are converted into the corresponding characters.
The number of zeros in the input bytes is
the difference between the number of all those bytes
and the result of removing all their leading zero bytes.
As many
The addition of the ``zero'' (i.e.
Note that trim-bendian* fixes its argument to a true list of natural numbers, not to a true list of bytes. Thus, we use an mbe there, so that the encoding function fixes its input to a true list of bytes.
Function:
(defun base58-encode (bytes) (declare (xargs :guard (byte-listp bytes))) (let ((__function__ 'base58-encode)) (declare (ignorable __function__)) (b* ((nat (bebytes=>nat bytes)) (vals (nat=>bendian* 58 nat)) (number-of-zeros (- (len bytes) (len (trim-bendian* (mbe :logic (byte-list-fix bytes) :exec bytes))))) (chars (append (repeat number-of-zeros *base58-zero*) (base58-vals=>chars vals)))) chars)))
Theorem:
(defthm base58-character-listp-of-base58-encode (b* ((chars (base58-encode bytes))) (base58-character-listp chars)) :rule-classes :rewrite)
Theorem:
(defthm base58-encode-same-natural-number (equal (bendian=>nat 58 (base58-chars=>vals (base58-encode bytes))) (bebytes=>nat bytes)))
Theorem:
(defthm base58-encode-of-byte-list-fix-bytes (equal (base58-encode (byte-list-fix bytes)) (base58-encode bytes)))
Theorem:
(defthm base58-encode-byte-list-equiv-congruence-on-bytes (implies (byte-list-equiv bytes bytes-equiv) (equal (base58-encode bytes) (base58-encode bytes-equiv))) :rule-classes :congruence)