noname icon indicating copy to clipboard operation
noname copied to clipboard

Create trait for builtins

Open mimoo opened this issue 1 year ago • 2 comments

we should standardize how we write builtins by using some trait that looks like this or something:

trait Builtin {
    const sig: &'static str;

    const no_typecheck: bool;

    fn typecheck(
        generics: &GenericParameters,
        vars: &[VarInfo<F, LinearCombination<F>>],
        span: Span,
    ) -> Result<()>;

    fn builtin(
        compiler: &mut CircuitWriter<R1CS<F>>,
        _generics: &GenericParameters,
        vars: &[VarInfo<F, LinearCombination<F>>],
        span: Span,
    ) -> Result<Option<Var<F, LinearCombination<F>>>>;
}

and the typecheck() function could contain all of the actual checks that need to be done.

mimoo avatar Nov 10 '24 10:11 mimoo

@mimoo I'm working on this issue and wanted to ask what type checks you mean?

zedar avatar Dec 06 '24 10:12 zedar

This was an attempt to cleanly separate the circuit compilation with the typechecking pass, even for builtins.

As you can see the typecheck for function calls happen here: https://github.com/zksecurity/noname/blob/main/src/type_checker/checker.rs#L817

and we disable it for some builtins here: https://github.com/zksecurity/noname/pull/238 (you should probably base your PR on that one), so that the builtin can do it by itself. Initially I was thinking that we should just have Builtin::no_typecheck = False; but we ended up storing that information elsewhere in #238

you can see that since the function is written in Rust, we do some typechecking inside the builtin manually. For example assert_eq(T, T) should check that both arguments are of the same type https://github.com/zksecurity/noname/blob/main/src/stdlib/builtins.rs#L57

I was thinking that these typechecks could be factored out of that function and placed in their own function for cleanliness. But you can try to remove no_typecheck and typecheck from my example, and then we can see if it makes sense to introduce them later :o

mimoo avatar Dec 09 '24 19:12 mimoo