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

Create trait for builtins #227

Open
mimoo opened this issue Nov 10, 2024 · 2 comments
Open

Create trait for builtins #227

mimoo opened this issue Nov 10, 2024 · 2 comments
Labels

Comments

@mimoo
Copy link
Contributor

mimoo commented Nov 10, 2024

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 mimoo added the easy label Nov 10, 2024
@zedar
Copy link

zedar commented Dec 6, 2024

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

@mimoo
Copy link
Contributor Author

mimoo commented Dec 9, 2024

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: #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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants