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

feat: add Canonical[De]serialize bounds to CommitmentBounds #40

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Changes from 1 commit
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
34 changes: 31 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ pub trait Committable {
pub struct Commitment<T: ?Sized + Committable>(Array, PhantomData<fn(&T)>);

/// Consolidate trait bounds for cryptographic commitments.
pub trait CommitmentBoundsSerdeless:
///
/// Downstream users should prefer [`CommitmentBounds`], which is defined appropriately according to feature flags "ark-serialize", "serde".
///
/// "serde" is additive with "ark-serialize", so if "serde" is enabled then so is "ark-serialize". But it is possible to have "ark-serialize" without "serde".
pub trait CommitmentBoundsArkless:
AsRef<[u8]> + Clone + Copy + Debug + Eq + Hash + PartialEq + Send + Sync + 'static
{
/// Create a default commitment with no preimage.
Expand All @@ -107,7 +111,7 @@ pub trait CommitmentBoundsSerdeless:
fn default_commitment_no_preimage() -> Self;
}

impl<T> CommitmentBoundsSerdeless for Commitment<T>
impl<T> CommitmentBoundsArkless for Commitment<T>
where
T: Committable + 'static,
{
Expand All @@ -116,15 +120,37 @@ where
}
}

/// If "ark-serialize" feature enabled
/// then add `CanonicalSerialize`, `CanonicalDeserialize`
/// else add nothing
#[cfg(feature = "ark-serialize")]
pub trait CommitmentBoundsSerdeless:
CommitmentBoundsArkless + CanonicalDeserialize + CanonicalSerialize
{
}

#[cfg(not(feature = "ark-serialize"))]
pub trait CommitmentBoundsSerdeless: CommitmentBoundsArkless {}

impl<T> CommitmentBoundsSerdeless for Commitment<T> where T: Committable + 'static {}

/// If "serde" feature enabled
/// then add `Serialize`, `Deserialize`
/// else if "ark-serialize" enabled but not "serde"
/// then add nothing to [`CommitmentBoundsSerdeless`]
/// else add nothing to [`CommitmentBoundsArkless`]
ggutoski marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "serde")]
pub trait CommitmentBounds:
CommitmentBoundsSerdeless + for<'a> Deserialize<'a> + Serialize
{
}

#[cfg(not(feature = "serde"))]
#[cfg(all(feature = "ark-serialize", not(feature = "serde")))]
trait CommitmentBounds: CommitmentBoundsSerdeless {}

#[cfg(all(not(feature = "ark-serialize"), not(feature = "serde")))]
trait CommitmentBounds: CommitmentBoundsArkless {}

impl<T> CommitmentBounds for Commitment<T> where T: Committable + 'static {}

impl<T: ?Sized + Committable> Commitment<T> {
Expand Down Expand Up @@ -334,6 +360,8 @@ mod test {
where
T: for<'a> Arbitrary<'a>
+ AsRef<[u8]>
+ CanonicalDeserialize
+ CanonicalSerialize
+ Copy
+ Clone
+ Debug
Expand Down