Skip to content

Commit b03e24a

Browse files
[coding] Introduce commonware-coding (#1272)
1 parent dcf2a15 commit b03e24a

13 files changed

Lines changed: 947 additions & 0 deletions

File tree

.github/workflows/publish.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ jobs:
7575
continue-on-error: true
7676
env:
7777
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
78+
- name: Publish coding
79+
run: cargo publish --manifest-path coding/Cargo.toml
80+
continue-on-error: true
81+
env:
82+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
7883
- name: Publish deployer
7984
run: cargo publish --manifest-path deployer/Cargo.toml
8085
continue-on-error: true

.github/workflows/slow.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ jobs:
6262
cargo_flags: ""
6363
file_suffix: ""
6464
benchmark_name: "commonware-stream"
65+
- package: commonware-coding
66+
cargo_flags: ""
67+
file_suffix: ""
68+
benchmark_name: "commonware-coding"
6569
steps:
6670
- name: Checkout repository
6771
uses: actions/checkout@v4

Cargo.lock

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
members = [
33
"broadcast",
44
"codec",
5+
"coding",
56
"collector",
67
"consensus",
78
"cryptography",

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ _Primitives are designed for deployment in adversarial environments. If you find
1313

1414
* [broadcast](./broadcast/README.md): Disseminate data over a wide-area network.
1515
* [codec](./codec/README.md): Serialize structured data.
16+
* [coding](./coding/README.md): Encode data to enable recovery from a subset of fragments.
1617
* [collector](./collector/README.md): Collect responses to committable requests.
1718
* [consensus](./consensus/README.md): Order opaque messages in a Byzantine environment.
1819
* [cryptography](./cryptography/README.md): Generate keys, sign arbitrary messages, and deterministically verify signatures.

coding/Cargo.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[package]
2+
name = "commonware-coding"
3+
edition = "2021"
4+
publish = true
5+
version = "0.0.55"
6+
license = "MIT OR Apache-2.0"
7+
description = "Encode data to enable recovery from a subset of fragments."
8+
readme = "README.md"
9+
homepage = "https://commonware.xyz"
10+
repository = "https://github.com/commonwarexyz/monorepo/tree/main/coding"
11+
documentation = "https://docs.rs/commonware-coding"
12+
13+
[dependencies]
14+
commonware-codec = { workspace = true }
15+
commonware-cryptography = { workspace = true }
16+
commonware-storage = { workspace = true }
17+
bytes = { workspace = true }
18+
thiserror = { workspace = true }
19+
reed-solomon-simd = "3.0.1"
20+
21+
[lib]
22+
bench = false
23+
24+
[dev-dependencies]
25+
criterion = { workspace = true }
26+
rand = { workspace = true }
27+
28+
[[bench]]
29+
name = "reed_solomon"
30+
harness = false
31+
path = "src/reed_solomon/benches/bench.rs"

coding/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# commonware-coding
2+
3+
[![Crates.io](https://img.shields.io/crates/v/commonware-coding.svg)](https://crates.io/crates/commonware-coding)
4+
[![Docs.rs](https://docs.rs/commonware-coding/badge.svg)](https://docs.rs/commonware-coding)
5+
6+
Encode data to enable recovery from a subset of fragments.
7+
8+
## Status
9+
10+
`commonware-coding` is **ALPHA** software and is not yet recommended for production use. Developers should expect breaking changes and occasional instability.

coding/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//! Encode data to enable recovery from a subset of fragments.
2+
//!
3+
//! # Status
4+
//!
5+
//! `commonware-coding` is **ALPHA** software and is not yet recommended for production use. Developers should
6+
//! expect breaking changes and occasional instability.
7+
8+
pub mod reed_solomon;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use criterion::criterion_main;
2+
3+
mod decode;
4+
mod encode;
5+
6+
criterion_main!(encode::benches, decode::benches);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use commonware_coding::reed_solomon::{decode, encode};
2+
use commonware_cryptography::Sha256;
3+
use criterion::{criterion_group, BatchSize, Criterion};
4+
use rand::{rngs::StdRng, seq::SliceRandom, RngCore, SeedableRng};
5+
6+
fn benchmark_decode(c: &mut Criterion) {
7+
let mut sampler = StdRng::seed_from_u64(0);
8+
let cases = [8, 12, 16, 19, 20, 24].map(|i| 2usize.pow(i));
9+
for data_length in cases.into_iter() {
10+
for chunks in [10, 25, 50, 100, 250] {
11+
let min = chunks / 3;
12+
c.bench_function(
13+
&format!(
14+
"{}/msg_len={} chunks={}",
15+
module_path!(),
16+
data_length,
17+
chunks
18+
),
19+
|b| {
20+
b.iter_batched(
21+
|| {
22+
// Generate random data
23+
let mut data = vec![0u8; data_length];
24+
sampler.fill_bytes(&mut data);
25+
26+
// Encode data
27+
let (root, proofs) = encode::<Sha256>(chunks, min, data).unwrap();
28+
29+
// Select min random chunks
30+
let mut shuffled = Vec::with_capacity(min as usize);
31+
let mut indices: Vec<u16> = (0..chunks).collect();
32+
indices.shuffle(&mut sampler);
33+
for &i in indices.iter().take(min as usize) {
34+
shuffled.push(proofs[i as usize].clone());
35+
}
36+
37+
(root, shuffled)
38+
},
39+
|(root, shuffled)| {
40+
decode::<Sha256>(chunks, min, &root, shuffled).unwrap();
41+
},
42+
BatchSize::SmallInput,
43+
);
44+
},
45+
);
46+
}
47+
}
48+
}
49+
50+
criterion_group! {
51+
name = benches;
52+
config = Criterion::default().sample_size(10);
53+
targets = benchmark_decode
54+
}

0 commit comments

Comments
 (0)