openzklib / openzl

Zero-Knowledge Cryptography Infrastructure Stack
https://openzl.org
Other
126 stars 14 forks source link

Domain Tag Generation Macros #45

Open bhgomes opened 1 year ago

bhgomes commented 1 year ago

When building domain tags we usually use a trait like this:

/// Domain Tag
pub trait DomainTag<T>
where
    T: ParameterFieldType,
{
    /// Generates domain tag as a constant parameter.
    fn domain_tag() -> T::ParameterField;
}

and then build custom domain tags with code like this (from manta-rs):

/// Utxo Commitment Scheme Domain Tag
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct UtxoCommitmentSchemeDomainTag;

impl poseidon::hash::DomainTag<Poseidon5> for UtxoCommitmentSchemeDomainTag {
    #[inline]
    fn domain_tag() -> <Poseidon5 as ParameterFieldType>::ParameterField {
        todo!()
    }
}

impl<COM> Constant<COM> for UtxoCommitmentSchemeDomainTag {
    type Type = Self;

    #[inline]
    fn new_constant(this: &Self::Type, compiler: &mut COM) -> Self {
        let _ = (this, compiler);
        Self
    }
}

We should be able to simplify this to a generic domain tag type:

/// Domain Tag Parameter
#[component]
pub type DomainTagParameter;

/// Domain Tag
pub trait DomainTag<T>
where
    T: DomainTagParameterType,
{
    /// Generates a domain tag.
    fn domain_tag() -> T::DomainTagParameterType;
}

so that an implementation looks like this:

/// Utxo Commitment Scheme Domain Tag
#[domain_tag(Poseidon5)]
pub struct UtxoCommitmentSchemeDomainTag;