Why do we need yet another serialization format? Borsh is the first serializer that prioritizes the following qualities that are crucial for security-critical projects:
- Consistent and specified binary representation:
- Consistent means there is a bijective mapping between objects and their binary representations. There is no two binary representations that deserialize into the same object. This is extremely useful for applications that use binary representation to compute hash;
- Borsh comes with a full specification that can be used for implementations in other languages;
- Safe. Borsh implementations use safe coding practices. In Rust, Borsh uses almost only safe code, with one exception usage of
unsafe
to avoid an exhaustion attack; - Speed. In Rust, Borsh achieves high performance by opting out from Serde which makes it faster than bincode in some cases; which also reduces the code size.
Example
use ;
Features
Opting out from Serde allows borsh to have some features that currently are not available for serde-compatible serializers.
Currently we support two features: borsh_init
and borsh_skip
(the former one not available in Serde).
borsh_init
allows to automatically run an initialization function right after deserialization. This adds a lot of convenience for objects that are architectured to be used as strictly immutable. Usage example:
borsh_skip
allows to skip serializing/deserializing fields, assuming they implement Default
trait, similary to #[serde(skip)]
.
Benchmarks
We measured the following benchmarks on objects that blockchain projects care about the most: blocks, block headers, transactions, accounts. We took object structure from the nearprotocol blockchain. We used Criterion for building the following graphs.
The benchmarks were run on Google Cloud n1-standard-2 (2 vCPUs, 7.5 GB memory).
Block header serialization speed vs block header size in bytes (size only roughly corresponds to the serialization complexity which causes non-smoothness of the graph):
Block header de-serialization speed vs block header size in bytes:
Block serialization speed vs block size in bytes:
Block de-serialization speed vs block size in bytes:
See complete report here.
Specification
In short, Borsh is a non self-describing binary serialization format. It is designed to serialize any objects to canonical and deterministic set of bytes.
General principles:
- integers are little endian;
- sizes of dynamic containers are written before values as
u32
; - all unordered containers (hashmap/hashset) are ordered in lexicographic order by key (in tie breaker case on value);
- structs are serialized in the order of fields in the struct;
- enums are serialized with using
u8
for the enum ordinal and then storing data inside the enum value (if present).
Formal specification:
Note:
- Some parts of Rust grammar are not yet formalized, like enums and variants. We backwards derive EBNF forms of Rust grammar from syn types;
- We had to extend repetitions of EBNF and instead of defining them as
[ ident_field ':' ident_type ',' ] *
we define them asident_field0 ':' ident_type0 ',' ident_field1 ':' ident_type1 ',' ...
so that we can refer to individual elements in the pseudocode; - We use
repr()
function to denote that we are writing the representation of the given element into an imaginary buffer.
Releasing
After you merged your change into the master branch and bumped the versions of all three crates it is time to officially release the new version.
Make sure borsh
, borsh-derive
and borsh-derive-internal
all have the new crate versions. Then navigate to each folder and run (in the given order):
;
;
;
Make sure you are on the master branch, then tag the code and push the tag: