Note: These definitions were taken from https://github.com/w3f/polkadot-spec/blob/main/docs. They are provided here for reference. All credits to respective authors.
Notation
- Let be the set of all byte sequences.
- Let , denotes the -th byte of , and denotes the -th bit of the -th byte of .
Definitions
Little Endian
By the little-endian representation of a non-negative integer, , represented as
in base 256, we refer to a byte array such that
Accordingly, we define the function :
Scale Types
Fixed Length Integers
The SCALE codec, , for fixed length integers not defined here otherwise, is equal to the little-endian encoding of those values.
Varying Data Type
This library does not provide means for encoding/decoding varying data types, but the definitions are provided here for completeness and reference. The implementation is left to the user of the library.
We define a varying data type to be an ordered set of data types.
A value of varying data type is a pair where for some and is its value of type , which can be empty. We define , unless it is explicitly defined as another value in the definition of a particular varying data type.
The SCALE codec for value of varying data type , formally referred to as is defined as follows:
The SCALE codec does not encode the correspondence between the value and the data type it represents; the decoder needs prior knowledge of such correspondence to decode the data.
Boolean
The SCALE codec for a boolean value defined as a byte as follows:
Compact
SCALE Length encoding , also known as a compact encoding, of a non-negative number is defined as follows:
and the rest of the bits of store the value of in little-endian format in base-2 as follows:
such that:
Note that denotes the length of the original integer being encoded and does not include the extra byte describing the length. The encoding can be used for integers up to: $$2^{(63+4)8} -1 = 2^{536} -1$$
Sequence
The SCALE codec for sequence such that:
where ’s are values of the same type (and the decoder is unable to infer value of from the context) is defined as:
where is defined here.
In some cases, the length indicator is omitted if the length of the sequence is fixed and known by the decoder upfront. Such cases are explicitly stated by the definition of the corresponding type.
String
The SCALE codec for a string value is an encoded sequence consisting of UTF-8 encoded bytes.
This can be achieved via encoding the UTF-8 sequence as a
uint8[]array, which is supported by this library.