Manta-Network / manta-rs

Rust Crates for the Manta Network Ecosystem
https://github.com/Manta-Network
GNU General Public License v3.0
80 stars 50 forks source link

Derive Macro for `Variable` and `Constant` #230

Closed bhgomes closed 2 years ago

bhgomes commented 2 years ago

For Constant there is a canonical implementation for structs (each of its fields are constants). For Variable it will depend on the explicit allocation modes we want to add to each field, so maybe something like

#[derive(Variable)]
struct Thing<T> {
    #[secret]
    input: T,

    #[public]
    output: T
}

which would compile to something like:

impl<T> Variable<Derived, COM> for Thing<T>
where
    T: Variable<Public, COM> + Variable<Secret, COM>,
{
    type Type = Thing<T::Type>;

    #[inline]
    fn new_unknown(compiler: &mut COM) -> Self {
        Self {
            input: $crate::eclair::alloc::Allocator::allocate_unknown::<Secret, _>(compiler),
            output: $crate::eclair::alloc::Allocator::allocate_unknown::<Public, _>(compiler),
        }
    }

    #[inline]
    fn new_known(this: &Self::Type, compiler: &mut COM) -> Self {
        Self {
            input: $crate::eclair::alloc::Allocator::allocate_known::<Secret, _>(compiler, this.input),
            output: $crate::eclair::alloc::Allocator::allocate_unknown::<Public, _>(compiler, this.output),
        }
    }
}
bhgomes commented 2 years ago

Moved to https://github.com/openzklib/openzl/issues/5.