openzklib / openzl

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

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

Open bhgomes opened 1 year ago

bhgomes commented 1 year 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),
        }
    }
}