Skip to content

Commit 4e8b861

Browse files
authored
Migrate to Diem and BCS (#1)
Co-authored-by: Mathieu Baudet <[email protected]>
1 parent 45dcd3e commit 4e8b861

14 files changed

+98
-94
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22

3+
## [v0.1.1] - 2020-12-11
4+
- Renaming crate into "bcs".
5+
36
## [v0.1.0] - 2020-11-17
47
- Initial release.
58

6-
[v0.1.0]: https://github.com/libra/lcs/releases/tag/v0.1.0
9+
[v0.1.1]: https://github.com/diem/bcs/releases/tag/v0.1.1
10+
[v0.1.0]: https://github.com/diem/bcs/releases/tag/v0.1.0

CODE_OF_CONDUCT.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Code of Conduct
22

3-
The project has adopted a Code of Conduct that we expect project participants to adhere to. Please [read the full text](https://developers.libra.org/docs/policies/code-of-conduct) so that you can understand what actions will and will not be tolerated.
3+
The project has adopted a Code of Conduct that we expect project participants to adhere to. Please [read the full text](https://developers.diem.com/docs/policies/code-of-conduct) so that you can understand what actions will and will not be tolerated.

CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ This project welcomes contributions.
44

55
## Contributor License Agreement (CLA)
66

7-
For pull request to be accepted by any Libra projects, a CLA must be [signed](https://libra.org/en-US/cla-sign). You will only need to do this once to work on any of Libra's open source projects.
7+
For pull request to be accepted by any Diem projects, a CLA must be [signed](https://diem.com/en-US/cla-sign). You will only need to do this once to work on any of Diem's open source projects.
88

9-
When submitting a pull request (PR), the `libra-github-bot` will check your submission for a valid CLA. If one is not found, then you will need to [submit](https://libra.org/en-US/cla-sign) an Individual CLA for yourself or a Corporate CLA for your company.
9+
When submitting a pull request (PR), the `diem-github-bot` will check your submission for a valid CLA. If one is not found, then you will need to [submit](https://diem.com/en-US/cla-sign) an Individual CLA for yourself or a Corporate CLA for your company.
1010

1111
## Issues
1212

Cargo.toml

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[package]
2-
name = "libra-canonical-serialization"
3-
version = "0.1.0"
4-
authors = ["Libra Association <opensource@libra.org>"]
5-
description = "Libra Canonical Serialization (LCS)"
6-
documentation = "https://docs.rs/libra-canonical-serialization"
7-
repository = "https://github.com/libra/lcs"
2+
name = "bcs"
3+
version = "0.1.1"
4+
authors = ["Diem <opensource@diem.com>"]
5+
description = "Binary Canonical Serialization (BCS)"
6+
repository = "https://github.com/diem/bcs"
7+
homepage = "https://diem.com"
88
readme = "README.md"
99
license = "Apache-2.0"
1010
edition = "2018"
@@ -19,5 +19,5 @@ proptest = "0.10.1"
1919
proptest-derive = "0.2.0"
2020

2121
[[bench]]
22-
name = "lcs_bench"
22+
name = "bcs_bench"
2323
harness = false

README.md

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
> **Note to readers:** On December 1, 2020, the Libra Association was renamed to Diem Association. The project repos are in the process of being migrated. All projects will remain available for use here until the migration to a new GitHub Organization is complete.
22
3-
## Libra Canonical Serialization (LCS)
3+
## Binary Canonical Serialization (BCS)
44

5-
LCS defines a deterministic means for translating a message or data structure into bytes
5+
BCS defines a deterministic means for translating a message or data structure into bytes
66
irrespective of platform, architecture, or programming language.
77

88
### Background
99

10-
In Libra, participants pass around messages or data structures that often times need to be
10+
In Diem, participants pass around messages or data structures that often times need to be
1111
signed by a prover and verified by one or more verifiers. Serialization in this context refers
1212
to the process of converting a message into a byte array. Many serialization approaches support
1313
loose standards such that two implementations can produce two different byte streams that would
@@ -21,12 +21,12 @@ serialized bytes or risk losing the ability to verify messages. This creates a b
2121
participants to maintain both a copy of the serialized bytes and the deserialized message often
2222
leading to confusion about safety and correctness. While there exist a handful of existing
2323
deterministic serialization formats, there is no obvious choice. To address this, we propose
24-
Libra Canonical Serialization that defines a deterministic means for translating a message into
24+
Diem Canonical Serialization that defines a deterministic means for translating a message into
2525
bytes and back again.
2626

2727
### Specification
2828

29-
LCS supports the following data types:
29+
BCS supports the following data types:
3030

3131
* Booleans
3232
* Signed 8-bit, 16-bit, 32-bit, 64-bit, and 128-bit integers
@@ -42,20 +42,20 @@ LCS supports the following data types:
4242

4343
### General structure
4444

45-
LCS is not a self-describing format and as such, in order to deserialize a message, one must
45+
BCS is not a self-describing format and as such, in order to deserialize a message, one must
4646
know the message type and layout ahead of time.
4747

4848
Unless specified, all numbers are stored in little endian, two's complement format.
4949

50-
### Recursion and Depth of LCS Data
50+
### Recursion and Depth of BCS Data
5151

5252
Recursive data-structures (e.g. trees) are allowed. However, because of the possibility of stack
53-
overflow during (de)serialization, the *container depth* of any valid LCS data cannot exceed the constant
53+
overflow during (de)serialization, the *container depth* of any valid BCS data cannot exceed the constant
5454
`MAX_CONTAINER_DEPTH`. Formally, we define *container depth* as the number of structs and enums traversed
5555
during (de)serialization.
5656

5757
This definition aims to minimize the number of operations while ensuring that
58-
(de)serialization of a known LCS format cannot cause arbitrarily large stack allocations.
58+
(de)serialization of a known BCS format cannot cause arbitrarily large stack allocations.
5959

6060
As an example, if `v1` and `v2` are values of depth `n1` and `n2`,
6161
* a struct value `Foo { v1, v2 }` has depth `1 + max(n1, n2)`;
@@ -81,7 +81,7 @@ All string and integer values have depths `0`.
8181

8282
#### ULEB128-Encoded Integers
8383

84-
The LCS format also uses the [ULEB128 encoding](https://en.wikipedia.org/wiki/LEB128) internally
84+
The BCS format also uses the [ULEB128 encoding](https://en.wikipedia.org/wiki/LEB128) internally
8585
to represent unsigned 32-bit integers in two cases where small values are usually expected:
8686
(1) lengths of variable-length sequences and (2) tags of enum values (see the corresponding
8787
sections below).
@@ -99,15 +99,15 @@ In general, a ULEB128 encoding consists of a little-endian sequence of base-128
9999
digits. Each digit is completed into a byte by setting the highest bit to 1, except for the
100100
last (highest-significance) digit whose highest bit is set to 0.
101101

102-
In LCS, the result of decoding ULEB128 bytes is required to fit into a 32-bit unsigned
102+
In BCS, the result of decoding ULEB128 bytes is required to fit into a 32-bit unsigned
103103
integer and be in canonical form. For instance, the following values are rejected:
104104
* `[808080808001]` (2^36) is too large.
105105
* `[8080808010]` (2^33) is too large.
106106
* `[8000]` is not a minimal encoding of 0.
107107

108108
#### Optional Data
109109

110-
Optional or nullable data either exists in its full representation or does not. LCS represents
110+
Optional or nullable data either exists in its full representation or does not. BCS represents
111111
this as a single byte representing the presence `0x01` or absence `0x00` of data. If the data
112112
is present then the serialized form of that data follows. For example:
113113

@@ -121,9 +121,9 @@ assert_eq!(to_bytes(&no_data)?, vec![0]);
121121

122122
#### Fixed and Variable Length Sequences
123123

124-
Sequences can be made of up of any LCS supported types (even complex structures) but all
124+
Sequences can be made of up of any BCS supported types (even complex structures) but all
125125
elements in the sequence must be of the same type. If the length of a sequence is fixed and
126-
well known then LCS represents this as just the concatenation of the serialized form of each
126+
well known then BCS represents this as just the concatenation of the serialized form of each
127127
individual element in the sequence. If the length of the sequence can be variable, then the
128128
serialized sequence is length prefixed with a ULEB128-encoded unsigned integer indicating
129129
the number of elements in the sequence. All variable length sequences must be
@@ -142,7 +142,7 @@ assert_eq!(to_bytes(&large_variable_length)?, vec![0x8f, 0x4a]);
142142

143143
#### Strings
144144

145-
Only valid UTF-8 Strings are supported. LCS serializes such strings as a variable length byte
145+
Only valid UTF-8 Strings are supported. BCS serializes such strings as a variable length byte
146146
sequence, i.e. length prefixed with a ULEB128-encoded unsigned integer followed by the byte
147147
representation of the string.
148148

@@ -161,12 +161,12 @@ assert_eq!(to_bytes(&utf8_str)?, expecting);
161161
Tuples are typed composition of objects: `(Type0, Type1)`
162162

163163
Tuples are considered a fixed length sequence where each element in the sequence can be a
164-
different type supported by LCS. Each element of a tuple is serialized in the order it is
164+
different type supported by BCS. Each element of a tuple is serialized in the order it is
165165
defined within the tuple, i.e. [tuple.0, tuple.2].
166166

167167
```rust
168-
let tuple = (-1i8, "libra");
169-
let expecting = vec![0xFF, 5, b'l', b'i', b'b', b'r', b'a'];
168+
let tuple = (-1i8, "diem");
169+
let expecting = vec![0xFF, 4, b'd', b'i', b'e', b'm'];
170170
assert_eq!(to_bytes(&tuple)?, expecting);
171171
```
172172

@@ -175,7 +175,7 @@ assert_eq!(to_bytes(&tuple)?, expecting);
175175

176176
Structures are fixed length sequences consisting of fields with potentially different types.
177177
Each field within a struct is serialized in the order specified by the canonical structure
178-
definition. Structs can exist within other structs and as such, LCS recurses into each struct
178+
definition. Structs can exist within other structs and as such, BCS recurses into each struct
179179
and serializes them in order. There are no labels in the serialized format, the struct ordering
180180
defines the organization within the serialization stream.
181181

@@ -214,9 +214,9 @@ assert_eq!(w_bytes, expecting);
214214
#### Externally Tagged Enumerations
215215

216216
An enumeration is typically represented as a type that can take one of potentially many
217-
different variants. In LCS, each variant is mapped to a variant index, a ULEB128-encoded 32-bit unsigned
217+
different variants. In BCS, each variant is mapped to a variant index, a ULEB128-encoded 32-bit unsigned
218218
integer, followed by serialized data if the type has an associated value. An
219-
associated type can be any LCS supported type. The variant index is determined based on the
219+
associated type can be any BCS supported type. The variant index is determined based on the
220220
ordering of the variants in the canonical enum definition, where the first variant has an index
221221
of `0`, the second an index of `1`, etc.
222222

@@ -241,7 +241,7 @@ If you need to serialize a C-style enum, you should use a primitive integer type
241241
#### Maps (Key / Value Stores)
242242

243243
Maps are represented as a variable-length, sorted sequence of (Key, Value) tuples. Keys must be
244-
unique and the tuples sorted by increasing lexicographical order on the LCS bytes of each key.
244+
unique and the tuples sorted by increasing lexicographical order on the BCS bytes of each key.
245245
The representation is otherwise similar to that of a variable-length sequence. In particular,
246246
it is preceded by the number of tuples, encoded in ULEB128.
247247

@@ -258,7 +258,7 @@ assert_eq!(to_bytes(&map)?, to_bytes(&expecting)?);
258258

259259
### Backwards compatibility
260260

261-
Complex types dependent upon the specification in which they are used. LCS does not provide
261+
Complex types dependent upon the specification in which they are used. BCS does not provide
262262
direct provisions for versioning or backwards / forwards compatibility. A change in an objects
263263
structure could prevent historical clients from understanding new clients and vice-versa.
264264

README.tpl

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
> **Note to readers:** On December 1, 2020, the Libra Association was renamed to Diem Association. The project repos are in the process of being migrated. All projects will remain available for use here until the migration to a new GitHub Organization is complete.
2+
13
{{readme}}
24

35
## Contributing

SECURITY.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Security Policies and Procedures
22

3-
Please see Libra's
4-
[security policies](https://developers.libra.org/docs/policies/security) and
3+
Please see Diem's
4+
[security policies](https://developers.diem.com/docs/policies/security) and
55
procedures for reporting vulnerabilities.

benches/lcs_bench.rs benches/bcs_bench.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// Copyright (c) The Libra Core Contributors
1+
// Copyright (c) The Diem Core Contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use bcs::to_bytes;
45
use criterion::{criterion_group, criterion_main, Criterion};
5-
use libra_canonical_serialization::to_bytes;
66
use std::collections::{BTreeMap, HashMap};
77

8-
pub fn lcs_benchmark(c: &mut Criterion) {
8+
pub fn bcs_benchmark(c: &mut Criterion) {
99
let mut btree_map = BTreeMap::new();
1010
let mut hash_map = HashMap::new();
1111
for i in 0u32..2000u32 {
@@ -24,5 +24,5 @@ pub fn lcs_benchmark(c: &mut Criterion) {
2424
});
2525
}
2626

27-
criterion_group!(benches, lcs_benchmark);
27+
criterion_group!(benches, bcs_benchmark);
2828
criterion_main!(benches);

src/de.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) The Libra Core Contributors
1+
// Copyright (c) The Diem Core Contributors
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::error::{Error, Result};
@@ -7,13 +7,13 @@ use std::convert::TryFrom;
77

88
/// Deserializes a `&[u8]` into a type.
99
///
10-
/// This function will attempt to interpret `bytes` as the LCS serialized form of `T` and
10+
/// This function will attempt to interpret `bytes` as the BCS serialized form of `T` and
1111
/// deserialize `T` from `bytes`.
1212
///
1313
/// # Examples
1414
///
1515
/// ```
16-
/// use libra_canonical_serialization::from_bytes;
16+
/// use bcs::from_bytes;
1717
/// use serde::Deserialize;
1818
///
1919
/// #[derive(Deserialize)]
@@ -53,7 +53,7 @@ where
5353
deserializer.end().map(move |_| t)
5454
}
5555

56-
/// Deserialization implementation for LCS
56+
/// Deserialization implementation for BCS
5757
struct Deserializer<'de> {
5858
input: &'de [u8],
5959
max_remaining_depth: usize,
@@ -196,7 +196,7 @@ impl<'de> Deserializer<'de> {
196196
impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
197197
type Error = Error;
198198

199-
// LCS is not a self-describing format so we can't implement `deserialize_any`
199+
// BCS is not a self-describing format so we can't implement `deserialize_any`
200200
fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value>
201201
where
202202
V: Visitor<'de>,
@@ -438,23 +438,23 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
438438
r
439439
}
440440

441-
// LCS does not utilize identifiers, so throw them away
441+
// BCS does not utilize identifiers, so throw them away
442442
fn deserialize_identifier<V>(self, _visitor: V) -> Result<V::Value>
443443
where
444444
V: Visitor<'de>,
445445
{
446446
self.deserialize_bytes(_visitor)
447447
}
448448

449-
// LCS is not a self-describing format so we can't implement `deserialize_ignored_any`
449+
// BCS is not a self-describing format so we can't implement `deserialize_ignored_any`
450450
fn deserialize_ignored_any<V>(self, _visitor: V) -> Result<V::Value>
451451
where
452452
V: Visitor<'de>,
453453
{
454454
Err(Error::NotSupported("deserialize_ignored_any"))
455455
}
456456

457-
// LCS is not a human readable format
457+
// BCS is not a human readable format
458458
fn is_human_readable(&self) -> bool {
459459
false
460460
}

src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) The Libra Core Contributors
1+
// Copyright (c) The Diem Core Contributors
22
// SPDX-License-Identifier: Apache-2.0
33

44
use serde::{de, ser};

0 commit comments

Comments
 (0)