Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding relations API #51

Merged
merged 19 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions earthstar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ willow-data-model = { path = "../data-model" }
arbitrary = { version = "1.0.2", features = ["derive"]}
ufotofu = { version = "0.4.2", features = ["std"] }
willow-encoding = { path = "../encoding" }
syncify = "0.1.0"

[lints]
workspace = true
154 changes: 83 additions & 71 deletions earthstar/src/cinn25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use arbitrary::{
Arbitrary, Error as ArbitraryError,
};
use either::Either;
use ufotofu::local_nb::{BulkConsumer, BulkProducer};
use willow_encoding::{Decodable, DecodeError, Encodable};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Shortname<const MIN_LENGTH: usize, const MAX_LENGTH: usize>(pub Vec<u8>);
Expand Down Expand Up @@ -97,94 +95,108 @@ impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Cinn25519PublicKey<MIN_LE
// TODO: fn verify
}

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Encodable
for Cinn25519PublicKey<MIN_LENGTH, MAX_LENGTH>
{
async fn encode<Consumer>(&self, consumer: &mut Consumer) -> Result<(), Consumer::Error>
where
Consumer: BulkConsumer<Item = u8>,
use syncify::syncify;
use syncify::syncify_replace;

#[syncify(encoding_sync)]
pub(super) mod encoding {
use super::*;

#[syncify_replace(use ufotofu::sync::{BulkConsumer, BulkProducer};)]
use ufotofu::local_nb::{BulkConsumer, BulkProducer};
use willow_encoding::DecodeError;
#[syncify_replace(use willow_encoding::sync::{Encodable, Decodable};)]
use willow_encoding::{Decodable, Encodable};

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Encodable
for Cinn25519PublicKey<MIN_LENGTH, MAX_LENGTH>
{
let mut vec = Vec::new();
async fn encode<Consumer>(&self, consumer: &mut Consumer) -> Result<(), Consumer::Error>
where
Consumer: BulkConsumer<Item = u8>,
{
let mut vec = Vec::new();

vec.extend_from_slice(&self.shortname.0);
vec.extend_from_slice(&self.shortname.0);

consumer
.bulk_consume_full_slice(&self.shortname.0)
.await
.map_err(|f| f.reason)?;
consumer
.bulk_consume_full_slice(&self.shortname.0)
.await
.map_err(|f| f.reason)?;

if MIN_LENGTH < MAX_LENGTH {
consumer.consume(0x0).await?;
}
if MIN_LENGTH < MAX_LENGTH {
consumer.consume(0x0).await?;
}

consumer
.bulk_consume_full_slice(&self.underlying)
.await
.map_err(|f| f.reason)?;
consumer
.bulk_consume_full_slice(&self.underlying)
.await
.map_err(|f| f.reason)?;

Ok(())
Ok(())
}
}
}

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Decodable
for Cinn25519PublicKey<MIN_LENGTH, MAX_LENGTH>
{
async fn decode<Producer>(
producer: &mut Producer,
) -> Result<Self, DecodeError<<Producer>::Error>>
where
Producer: BulkProducer<Item = u8>,
impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Decodable
for Cinn25519PublicKey<MIN_LENGTH, MAX_LENGTH>
{
if MIN_LENGTH == MAX_LENGTH {
let mut shortname_box = vec![0; MIN_LENGTH].into_boxed_slice();
async fn decode<Producer>(
producer: &mut Producer,
) -> Result<Self, DecodeError<<Producer>::Error>>
where
Producer: BulkProducer<Item = u8>,
{
if MIN_LENGTH == MAX_LENGTH {
let mut shortname_box = vec![0; MIN_LENGTH].into_boxed_slice();

producer
.bulk_overwrite_full_slice(shortname_box.as_mut())
.await?;

let mut underlying_slice = [0u8; 32];

producer
.bulk_overwrite_full_slice(&mut underlying_slice)
.await?;

return Ok(Self {
shortname: Shortname(shortname_box.into()),
underlying: underlying_slice,
});
}

producer
.bulk_overwrite_full_slice(shortname_box.as_mut())
.await?;
let mut shortname_vec: Vec<u8> = Vec::new();

loop {
match producer.produce().await {
Ok(Either::Left(item)) => {
// if item is 0x0, stop
if item == 0x0 {
break;
}

shortname_vec.push(item);
}
Ok(Either::Right(_)) => {
// whatever happens, we won't be able to make a full public key.
// Error!
return Err(DecodeError::InvalidInput);
}
Err(err) => return Err(DecodeError::Producer(err)),
}
}

let mut underlying_slice = [0u8; 32];

producer
.bulk_overwrite_full_slice(&mut underlying_slice)
.await?;

return Ok(Self {
shortname: Shortname(shortname_box.into()),
Ok(Self {
shortname: Shortname(shortname_vec),
underlying: underlying_slice,
});
})
}

let mut shortname_vec: Vec<u8> = Vec::new();

loop {
match producer.produce().await {
Ok(Either::Left(item)) => {
// if item is 0x0, stop
if item == 0x0 {
break;
}

shortname_vec.push(item);
}
Ok(Either::Right(_)) => {
// whatever happens, we won't be able to make a full public key.
// Error!
return Err(DecodeError::InvalidInput);
}
Err(err) => return Err(DecodeError::Producer(err)),
}
}

let mut underlying_slice = [0u8; 32];

producer
.bulk_overwrite_full_slice(&mut underlying_slice)
.await?;

Ok(Self {
shortname: Shortname(shortname_vec),
underlying: underlying_slice,
})
}
}

Expand Down
51 changes: 31 additions & 20 deletions earthstar/src/identity_id.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::cinn25519::{Cinn25519PublicKey, Shortname};
use arbitrary::Arbitrary;
use ufotofu::local_nb::{BulkConsumer, BulkProducer};
use willow_data_model::SubspaceId;
use willow_encoding::{Decodable, DecodeError, Encodable};

use crate::cinn25519::{Cinn25519PublicKey, Shortname};

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Arbitrary)]
pub struct IdentityIdentifier(Cinn25519PublicKey<4, 4>);
Expand All @@ -17,24 +14,38 @@ impl Default for IdentityIdentifier {
}
}

impl Encodable for IdentityIdentifier {
async fn encode<C>(&self, consumer: &mut C) -> Result<(), C::Error>
where
C: BulkConsumer<Item = u8>,
{
self.0.encode(consumer).await?;
Ok(())
use syncify::syncify;
use syncify::syncify_replace;

#[syncify(encoding_sync)]
pub(super) mod encoding {
use super::*;

#[syncify_replace(use ufotofu::sync::{BulkConsumer, BulkProducer};)]
use ufotofu::local_nb::{BulkConsumer, BulkProducer};
use willow_encoding::DecodeError;
#[syncify_replace(use willow_encoding::sync::{Encodable, Decodable};)]
use willow_encoding::{Decodable, Encodable};

impl Encodable for IdentityIdentifier {
async fn encode<C>(&self, consumer: &mut C) -> Result<(), C::Error>
where
C: BulkConsumer<Item = u8>,
{
self.0.encode(consumer).await?;
Ok(())
}
}
}

impl Decodable for IdentityIdentifier {
async fn decode<P>(producer: &mut P) -> Result<Self, DecodeError<P::Error>>
where
P: BulkProducer<Item = u8>,
{
match Cinn25519PublicKey::decode(producer).await {
Ok(pk) => Ok(Self(pk)),
Err(err) => Err(err),
impl Decodable for IdentityIdentifier {
async fn decode<P>(producer: &mut P) -> Result<Self, DecodeError<P::Error>>
where
P: BulkProducer<Item = u8>,
{
match Cinn25519PublicKey::decode(producer).await {
Ok(pk) => Ok(Self(pk)),
Err(err) => Err(err),
}
}
}
}
Expand Down
49 changes: 31 additions & 18 deletions earthstar/src/namespace_id.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use arbitrary::Arbitrary;
use ufotofu::local_nb::{BulkConsumer, BulkProducer};

use willow_data_model::NamespaceId;
use willow_encoding::{Decodable, DecodeError, Encodable};

use crate::cinn25519::{Cinn25519PublicKey, Shortname};

Expand All @@ -17,24 +16,38 @@ impl Default for NamespaceIdentifier {
}
}

impl Encodable for NamespaceIdentifier {
async fn encode<C>(&self, consumer: &mut C) -> Result<(), C::Error>
where
C: BulkConsumer<Item = u8>,
{
self.0.encode(consumer).await?;
Ok(())
use syncify::syncify;
use syncify::syncify_replace;

#[syncify(encoding_sync)]
pub(super) mod encoding {
use super::*;

#[syncify_replace(use ufotofu::sync::{BulkConsumer, BulkProducer};)]
use ufotofu::local_nb::{BulkConsumer, BulkProducer};
use willow_encoding::DecodeError;
#[syncify_replace(use willow_encoding::sync::{Encodable, Decodable};)]
use willow_encoding::{Decodable, Encodable};

impl Encodable for NamespaceIdentifier {
async fn encode<C>(&self, consumer: &mut C) -> Result<(), C::Error>
where
C: BulkConsumer<Item = u8>,
{
self.0.encode(consumer).await?;
Ok(())
}
}
}

impl Decodable for NamespaceIdentifier {
async fn decode<P>(producer: &mut P) -> Result<Self, DecodeError<<P>::Error>>
where
P: BulkProducer<Item = u8>,
{
match Cinn25519PublicKey::decode(producer).await {
Ok(pk) => Ok(Self(pk)),
Err(err) => Err(err),
impl Decodable for NamespaceIdentifier {
async fn decode<P>(producer: &mut P) -> Result<Self, DecodeError<<P>::Error>>
where
P: BulkProducer<Item = u8>,
{
match Cinn25519PublicKey::decode(producer).await {
Ok(pk) => Ok(Self(pk)),
Err(err) => Err(err),
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions encoding/src/compact_width.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,23 @@ pub mod encoding {
Ok(())
}

/// Decode the bytes representing a [`CompactWidth`]-bytes integer into a `usize` as encoding relation.
pub async fn decode_relation_compact_width_be<Producer: BulkProducer<Item = u8>>(
compact_width: CompactWidth,
producer: &mut Producer,
) -> Result<u64, DecodeError<Producer::Error>> {
let decoded = match compact_width {
CompactWidth::One => U8BE::decode(producer).await.map(u64::from),
CompactWidth::Two => U16BE::decode(producer).await.map(u64::from),
CompactWidth::Four => U32BE::decode(producer).await.map(u64::from),
CompactWidth::Eight => U64BE::decode(producer).await.map(u64::from),
}?;

Ok(decoded)
}

/// Decode the bytes representing a [`CompactWidth`]-bytes integer into a `usize`.
/// Will fail if the decoded value could have been encoded with a smaller [`CompactWidth`].
pub async fn decode_compact_width_be<Producer: BulkProducer<Item = u8>>(
compact_width: CompactWidth,
producer: &mut Producer,
Expand Down
Loading
Loading