Skip to content

Commit

Permalink
McCapability encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
sgwilym committed Aug 2, 2024
1 parent 69773ae commit a4a4ba1
Show file tree
Hide file tree
Showing 14 changed files with 433 additions and 21 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion data-model/src/encoding/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use syncify::syncify;

#[syncify(encoding_sync)]
pub(super) mod encoding {
pub mod encoding {

use crate::encoding::error::DecodeError;

Expand Down
2 changes: 1 addition & 1 deletion data-model/src/encoding/compact_width.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ use syncify::syncify;
use syncify::syncify_replace;

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

#[syncify_replace(use ufotofu::sync::{BulkConsumer, BulkProducer};)]
Expand Down
3 changes: 1 addition & 2 deletions data-model/src/encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub mod unsigned_int;
pub mod max_power;
pub mod max_power_sync {
use super::max_power;

pub use max_power::encoding_sync::*;
pub use max_power::max_power;
}
}
15 changes: 15 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ smol = "2.0.0"
arbitrary = { version = "1.0.2", features = ["derive"] }
libfuzzer-sys = { version = "0.4", features = ["arbitrary-derive"] }
signature = "2.2.0"
syncify = "0.1.0"


[dependencies.willow-data-model]
Expand Down Expand Up @@ -344,3 +345,17 @@ path = "fuzz_targets/mc_is_authorised_write.rs"
test = false
doc = false
bench = false

[[bin]]
name = "mc_capability_rel_area_encoding"
path = "fuzz_targets/mc_capability_rel_area_encoding.rs"
test = false
doc = false
bench = false

[[bin]]
name = "mc_capability_rel_area_encoding_random"
path = "fuzz_targets/mc_capability_rel_area_encoding_random.rs"
test = false
doc = false
bench = false
49 changes: 49 additions & 0 deletions fuzz/fuzz_targets/mc_capability_rel_area_encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use meadowcap::mc_capability::McCapability;
use ufotofu::local_nb::consumer::TestConsumer;
use willow_data_model::grouping::area::Area;
use willow_data_model_fuzz::encode::relative_encoding_roundtrip;
use willow_data_model_fuzz::silly_sigs::{SillyPublicKey, SillySig};

fuzz_target!(|data: (
McCapability<3, 3, 3, SillyPublicKey, SillySig, SillyPublicKey, SillySig>,
Area<3, 3, 3, SillyPublicKey>,
Vec<SillyPublicKey>,
TestConsumer<u8, u16, ()>
)| {
let (mc_cap, out, delegees, mut consumer) = data;

if !out.includes_area(&mc_cap.granted_area()) {
return;
}

let mut cap_with_delegees = mc_cap.clone();
let mut last_receiver = mc_cap.receiver().clone();
let granted_area = mc_cap.granted_area();

for delegee in delegees {
cap_with_delegees = cap_with_delegees
.delegate(
&last_receiver.corresponding_secret_key(),
&delegee,
&granted_area,
)
.unwrap();
last_receiver = delegee;
}

if !out.includes_area(&cap_with_delegees.granted_area()) {
return;
}

smol::block_on(async {
relative_encoding_roundtrip::<
McCapability<3, 3, 3, SillyPublicKey, SillySig, SillyPublicKey, SillySig>,
Area<3, 3, 3, SillyPublicKey>,
TestConsumer<u8, u16, ()>,
>(cap_with_delegees, out, &mut consumer)
.await;
});
});
19 changes: 19 additions & 0 deletions fuzz/fuzz_targets/mc_capability_rel_area_encoding_random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![no_main]

use libfuzzer_sys::fuzz_target;
use meadowcap::mc_capability::McCapability;
use willow_data_model::grouping::area::Area;
use willow_data_model_fuzz::encode::relative_encoding_random_less_strict;
use willow_data_model_fuzz::silly_sigs::{SillyPublicKey, SillySig};

fuzz_target!(|data: (&[u8], Area<3, 3, 3, SillyPublicKey>,)| {
let (random_bytes, out) = data;

smol::block_on(async {
relative_encoding_random_less_strict::<
Area<3, 3, 3, SillyPublicKey>,
McCapability<3, 3, 3, SillyPublicKey, SillySig, SillyPublicKey, SillySig>,
>(out, random_bytes)
.await;
});
});
2 changes: 1 addition & 1 deletion fuzz/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub async fn relative_encoding_roundtrip<T, R, C>(
C: BulkConsumer<Item = u8>,
{
//println!("item {:?}", subject);
// println!("ref {:?}", reference);
//println!("ref {:?}", reference);

if let Err(_err) = subject.relative_encode(&reference, consumer).await {
return;
Expand Down
73 changes: 58 additions & 15 deletions fuzz/src/silly_sigs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,67 @@ impl IsCommunal for SillyPublicKey {
}
}

impl Encodable for SillyPublicKey {
fn encode<Consumer>(&self, consumer: &mut Consumer) -> Result<(), Consumer::Error>
where
Consumer: ufotofu::sync::BulkConsumer<Item = u8>,
{
consumer.consume(self.0)?;
use syncify::{syncify, syncify_replace};

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

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

use willow_data_model::encoding::error::DecodeError;
#[syncify_replace(use willow_data_model::encoding::parameters_sync::{Encodable, Decodable};)]
use willow_data_model::encoding::parameters::{Decodable, Encodable};

#[syncify_replace(use willow_data_model::encoding::bytes::encoding_sync::produce_byte;)]
use willow_data_model::encoding::bytes::encoding::produce_byte;

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

Ok(())
}
}
}

impl Encodable for SillySig {
fn encode<Consumer>(&self, consumer: &mut Consumer) -> Result<(), Consumer::Error>
where
Consumer: ufotofu::sync::BulkConsumer<Item = u8>,
{
consumer.consume(self.0)?;
impl Decodable for SillyPublicKey {
async fn decode<Producer>(
producer: &mut Producer,
) -> Result<Self, DecodeError<Producer::Error>>
where
Producer: BulkProducer<Item = u8>,
{
let num = produce_byte(producer).await?;

Ok(())
Ok(SillyPublicKey(num))
}
}

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

Ok(())
}
}

impl Decodable for SillySig {
async fn decode<Producer>(
producer: &mut Producer,
) -> Result<Self, DecodeError<Producer::Error>>
where
Producer: BulkProducer<Item = u8>,
{
let num = produce_byte(producer).await?;

Ok(SillySig(num))
}
}
}
1 change: 1 addition & 0 deletions meadowcap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ signature = "2.2.0"
ufotofu = "0.3.0"
arbitrary = { version = "1.0.2", features = ["derive"], optional = true }
either = "1.13.0"
syncify = "0.1.0"

[dependencies.willow-data-model]
path = "../data-model"
Expand Down
6 changes: 5 additions & 1 deletion meadowcap/src/communal_capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,14 @@ where
&self.delegations
}

/// Return the public key of the very first user this capability was issued to.
pub fn progenitor(&self) -> &UserPublicKey {
&self.user_key
}

/// A bytestring to be signed for a new [`Delegation`].
///
/// [Definition](https://willowprotocol.org/specs/meadowcap/index.html#communal_handover)
fn handover(
&self,
new_area: &Area<MCL, MCC, MPL, UserPublicKey>,
Expand Down
1 change: 1 addition & 0 deletions meadowcap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub enum FailedDelegationError<
}

/// Returned when an existing delegation was found to be invalid.
#[derive(Debug)]
pub enum InvalidDelegationError<
const MCL: usize,
const MCC: usize,
Expand Down
Loading

0 comments on commit a4a4ba1

Please sign in to comment.